From 573c5053d9b8a63590cacf992085128783f4070e Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 18 Dec 2012 15:07:02 +0000 Subject: [PATCH] easymesh: fix point light handling. --- src/easymesh/easymesh.cpp | 4 +-- src/easymesh/easymesh.h | 2 +- src/easymesh/shiny.lolfx | 59 +++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/easymesh/easymesh.cpp b/src/easymesh/easymesh.cpp index e6c1607c..579c2889 100644 --- a/src/easymesh/easymesh.cpp +++ b/src/easymesh/easymesh.cpp @@ -67,7 +67,7 @@ void EasyMesh::MeshConvert() m_gpu.shader = Shader::Create(lolfx_shiny); m_gpu.modelview = m_gpu.shader->GetUniformLocation("in_ModelView"); - m_gpu.model = m_gpu.shader->GetUniformLocation("in_Model"); + m_gpu.view = m_gpu.shader->GetUniformLocation("in_View"); m_gpu.proj = m_gpu.shader->GetUniformLocation("in_Proj"); m_gpu.normalmat = m_gpu.shader->GetUniformLocation("in_NormalMat"); m_gpu.damage = m_gpu.shader->GetUniformLocation("in_Damage"); @@ -118,7 +118,7 @@ void EasyMesh::Render(mat4 const &model, float damage) m_gpu.shader->Bind(); m_gpu.shader->SetUniform(m_gpu.modelview, modelview); - m_gpu.shader->SetUniform(m_gpu.model, Scene::GetDefault()->GetViewMatrix()); + m_gpu.shader->SetUniform(m_gpu.view, Scene::GetDefault()->GetViewMatrix()); m_gpu.shader->SetUniform(m_gpu.proj, Scene::GetDefault()->GetProjMatrix()); m_gpu.shader->SetUniform(m_gpu.normalmat, normalmat); m_gpu.shader->SetUniform(m_gpu.damage, damage); diff --git a/src/easymesh/easymesh.h b/src/easymesh/easymesh.h index 3e0639c1..bf6342e9 100644 --- a/src/easymesh/easymesh.h +++ b/src/easymesh/easymesh.h @@ -101,7 +101,7 @@ private: { Shader *shader; ShaderAttrib coord, norm, color; - ShaderUniform modelview, model, proj, normalmat, damage; + ShaderUniform modelview, view, proj, normalmat, damage; VertexDeclaration *vdecl; VertexBuffer *vbo; IndexBuffer *ibo; diff --git a/src/easymesh/shiny.lolfx b/src/easymesh/shiny.lolfx index 4c380457..85b0f3e7 100644 --- a/src/easymesh/shiny.lolfx +++ b/src/easymesh/shiny.lolfx @@ -7,26 +7,24 @@ attribute vec3 in_Normal; attribute vec4 in_Color; uniform mat4 in_ModelView; -uniform mat4 in_Model; +uniform mat4 in_View; uniform mat4 in_Proj; uniform mat3 in_NormalMat; -varying vec4 pass_Eye; +varying vec4 pass_Vertex; /* View space */ varying vec3 pass_TNormal; -varying vec4 pass_TPos; varying vec4 pass_Color; void main(void) { - vec4 eye = in_ModelView * vec4(in_Vertex, 1.0); + vec4 vertex = in_ModelView * vec4(in_Vertex, 1.0); vec3 tnorm = normalize(in_NormalMat * in_Normal); - pass_Eye = eye; + pass_Vertex = vertex; pass_TNormal = tnorm; - pass_TPos = in_Model * vec4(in_Vertex, 1.0); pass_Color = in_Color; - gl_Position = in_Proj * eye; + gl_Position = in_Proj * vertex; } [frag.glsl] @@ -37,16 +35,15 @@ precision highp float; #endif uniform float in_Damage; -uniform mat4 in_Model; +uniform mat4 in_View; -varying vec4 pass_Eye; +varying vec4 pass_Vertex; /* View space */ varying vec3 pass_TNormal; -varying vec4 pass_TPos; varying vec4 pass_Color; // FIXME: the light direction should be passed in the code vec3 in_LightDir = vec3(0.3, 0.3, 0.7); -vec4 in_Light2_Pos = in_Model * vec4(0.0, 0.0, 0.0, 1.0); +vec4 in_Light2_Pos = vec4(-100.0, 100.0, -100.0, 1.0); void main(void) { @@ -61,17 +58,17 @@ void main(void) vec3 specular_color = vec3(1.0, 1.0, 0.6); vec3 ambient = ambient_color; - vec3 specular = vec3(0.0, 0.0, 0.0); - vec3 diffuse = vec3(0.0, 0.0, 0.0); - vec3 s = vec3(0.0, 0.0, 0.0); - vec3 v = vec3(0.0, 0.0, 0.0); - vec3 r = vec3(0.0, 0.0, 0.0); - float sdotn = 0.0; - - /* - //Light calculation for directional light + vec3 specular = vec3(0.0, 0.0, 0.0); + vec3 diffuse = vec3(0.0, 0.0, 0.0); + vec3 s = vec3(0.0, 0.0, 0.0); + vec3 v = vec3(0.0, 0.0, 0.0); + vec3 r = vec3(0.0, 0.0, 0.0); + float sdotn = 0.0; + + /* + //Light calculation for directional light s = normalize(in_LightDir); - v = normalize(-pass_Eye.xyz); + v = normalize(-pass_Vertex.xyz); r = reflect(-s, pass_TNormal); sdotn = max(dot(s, pass_TNormal), 0.0); @@ -79,11 +76,11 @@ void main(void) if (sdotn > 0.0) specular += specular_color * specular_reflect * pow(max(dot(r, v), 0.0), specular_power); - */ + */ - //Light calculation for point light - s = normalize(pass_Eye.xyz - in_Light2_Pos.xyz); //(pass_TPos.xyz - in_Light2_Pos); - v = normalize(-pass_Eye.xyz); + //Light calculation for point light + s = normalize((in_View * in_Light2_Pos).xyz - pass_Vertex.xyz); + v = normalize(-pass_Vertex.xyz); r = reflect(-s, pass_TNormal); sdotn = max(dot(s, pass_TNormal), 0.0); @@ -106,10 +103,10 @@ void main(float3 in_Vertex : POSITION, float3 in_Normal : NORMAL, float4 in_Color : COLOR, uniform float4x4 in_ModelView, - uniform float4x4 in_Model, + uniform float4x4 in_Model, uniform float4x4 in_Proj, uniform float3x3 in_NormalMat, - out float4 pass_Eye : TEXCOORD0, + out float4 pass_Vertex : TEXCOORD0, out float3 pass_TNormal : TEXCOORD1, out float4 pass_Color : COLOR, out float4 out_Position : POSITION) @@ -117,7 +114,7 @@ void main(float3 in_Vertex : POSITION, float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0)); float3 tnorm = normalize(mul(in_NormalMat, in_Normal)); - pass_Eye = eye; + pass_Vertex = eye; pass_TNormal = tnorm; #ifdef _XBOX pass_Color = in_Color.abgr; @@ -130,7 +127,7 @@ void main(float3 in_Vertex : POSITION, [frag.hlsl] -void main(float4 pass_Eye : TEXCOORD0, +void main(float4 pass_Vertex : TEXCOORD0, float3 pass_TNormal : TEXCOORD1, float4 pass_Color : COLOR, uniform float in_Damage, @@ -148,8 +145,8 @@ void main(float4 pass_Eye : TEXCOORD0, float3 diffuse_color = float3(1.0, 1.0, 0.6); float3 specular_color = float3(1.0, 1.0, 0.6); - float3 s = normalize(in_LightDir); /* normalize(pass_Eye - lightpos); */ - float3 v = normalize(-pass_Eye.xyz); + float3 s = normalize(in_LightDir); /* normalize(pass_Vertex - lightpos); */ + float3 v = normalize(-pass_Vertex.xyz); float3 r = reflect(-s, pass_TNormal); float3 ambient = ambient_color;