Changeset 1260


Ignore:
Timestamp:
Apr 21, 2012, 6:58:36 PM (11 years ago)
Author:
sam
Message:

orbital: basic lighting in the mesh builder.

Location:
trunk/orbital
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/orbital/mesh.h

    r1256 r1260  
    3333            "uniform mat4 in_ModelView;"
    3434            "uniform mat4 in_Proj;"
     35            "uniform mat3 in_NormalMat;"
    3536            "varying vec4 pass_Color;"
    3637            ""
     38            "vec4 Light = vec4(10.0, 1.0, 1.0, 1.0);"
     39            "vec3 Kd = vec3(1.0, 0.8, 0.8);"
     40            "vec3 Ld = vec3(1.0, 0.8, 0.8);"
     41            ""
    3742            "void main(void) {"
    38             "    vec4 tmp = in_ModelView * vec4(in_Vertex, 1.0);"
    39             "    gl_Position = in_Proj * tmp;"
    40             "    pass_Color = in_Color;"
     43            "    vec3 tnorm = normalize(in_NormalMat * in_Normal);"
     44            "    vec4 Eye = in_ModelView * vec4(in_Vertex, 1.0);"
     45            "    vec3 s = normalize((Light - Eye).xyz);"
     46            ""
     47            "    pass_Color = vec4(Ld * Kd * max(dot(s, tnorm), 0.0), 1.0);"
     48            "    gl_Position = in_Proj * Eye;"
    4149            "}",
    4250
     
    5361            "          uniform float4x4 in_ModelView,"
    5462            "          uniform float4x4 in_Proj,"
     63            "          uniform float3x3 in_NormalMat,"
    5564            "          out float4 out_Position : POSITION,"
    5665            "          out float4 pass_Color : COLOR) {"
    57             "    pass_Color = in_Color;"
    58             "    float4 tmp = mul(in_ModelView, float4(in_Vertex, 1.0));"
    59             "    out_Position = mul(in_Proj, tmp);"
     66            "    float4x4 Light = float4x4(10.0, 1.0, 1.0, 1.0);"
     67            "    float3x3 Kd = float3x3(1.0, 0.8, 0.8);"
     68            "    float3x3 Ld = float3x3(1.0, 0.8, 0.8);"
     69            ""
     70            "    float3x3 tnorm = normalize(mul(in_NormalMat, in_Normal));"
     71            "    float4x4 Eye = mul(in_ModelView, float4x4(in_Vertex, 1.0));"
     72            "    float3x3 s = normalize((Light - Eye).xyz);"
     73            ""
     74            "    pass_Color = float4x4(Ld * Kd * max(dot(s, tnorm), 0.0), 1.0);"
     75            "    out_Position = mul(in_Proj, Eye);"
    6076            "}",
    6177
     
    6884        m_gpu.modelview = m_gpu.shader->GetUniformLocation("in_ModelView");
    6985        m_gpu.proj = m_gpu.shader->GetUniformLocation("in_Proj");
     86        m_gpu.normalmat = m_gpu.shader->GetUniformLocation("in_NormalMat");
    7087        m_gpu.coord = m_gpu.shader->GetAttribLocation("in_Vertex",
    7188                                              VertexUsage::Position, 0);
     
    118135    }
    119136
    120     void Render(mat4 const &modelview, mat4 const &proj)
     137    void Render(mat4 const &modelview, mat4 const &proj, mat3 const &normalmat)
    121138    {
    122139        m_gpu.shader->Bind();
    123140        m_gpu.shader->SetUniform(m_gpu.modelview, modelview);
    124141        m_gpu.shader->SetUniform(m_gpu.proj, proj);
     142        m_gpu.shader->SetUniform(m_gpu.normalmat, normalmat);
    125143        m_gpu.vdecl->SetStream(m_gpu.vbo, m_gpu.coord, m_gpu.norm, m_gpu.color);
    126144        m_gpu.vdecl->Bind();
     
    389407        Shader *shader;
    390408        ShaderAttrib coord, norm, color;
    391         ShaderUniform modelview, proj;
     409        ShaderUniform modelview, proj, normalmat;
    392410        VertexDeclaration *vdecl;
    393411        VertexBuffer *vbo;
  • trunk/orbital/orbital.cpp

    r1256 r1260  
    5454    m_modelview = view * model * anim;
    5555    m_proj = proj;
     56    m_normalmat = transpose(inverse(mat3(m_modelview)));
    5657}
    5758
     
    6869    }
    6970
    70     m.Render(m_modelview, m_proj);
     71    m.Render(m_modelview, m_proj, m_normalmat);
    7172}
    7273
  • trunk/orbital/orbital.h

    r1256 r1260  
    2020    float m_angle;
    2121    mat4 m_modelview, m_proj;
     22    mat3 m_normalmat;
    2223
    2324    bool m_ready;
Note: See TracChangeset for help on using the changeset viewer.