Ignore:
Timestamp:
Jan 28, 2013, 2:23:57 PM (7 years ago)
Author:
sam
Message:

easymesh: interface the shiny shader with new Light objects.

Location:
trunk/src/easymesh
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/easymesh/easymesh.cpp

    r2277 r2289  
    8080    m_gpu.normalmat = m_gpu.shader->GetUniformLocation("in_NormalMat");
    8181    m_gpu.damage = m_gpu.shader->GetUniformLocation("in_Damage");
     82    m_gpu.lights = m_gpu.shader->GetUniformLocation("u_Lights");
    8283    m_gpu.coord = m_gpu.shader->GetAttribLocation("in_Vertex",
    8384                                          VertexUsage::Position, 0);
     
    126127
    127128    m_gpu.shader->Bind();
     129
     130    /* FIXME: this should be hidden in the shader */
     131    /* FIXME: the 4th component of the position can be used for other things */
     132    Array<Light *> const lights = Scene::GetDefault()->GetLights();
     133    Array<vec4> light_data;
     134    for (int i = 0; i < lights.Count(); ++i)
     135        light_data << lights[i]->GetPosition() << lights[i]->GetColor();
     136    while (light_data.Count() < 8)
     137        light_data << vec4(0.f) << vec4(0.f);
     138    m_gpu.shader->SetUniform(m_gpu.lights, light_data);
     139
    128140    m_gpu.shader->SetUniform(m_gpu.modelview, modelview);
    129141    m_gpu.shader->SetUniform(m_gpu.view, Scene::GetDefault()->GetViewMatrix());
  • trunk/src/easymesh/easymesh.h

    r2229 r2289  
    120120        Shader *shader;
    121121        ShaderAttrib coord, norm, color;
    122         ShaderUniform modelview, view, invview, proj, normalmat, damage;
     122        ShaderUniform modelview, view, invview, proj, normalmat, damage, lights;
    123123        VertexDeclaration *vdecl;
    124124        VertexBuffer *vbo;
  • trunk/src/easymesh/shiny.lolfx

    r2232 r2289  
    3939uniform mat4 in_Inv_View;
    4040
     41uniform vec4 u_Lights[8 * 2];
     42
    4143varying vec4 pass_Vertex; /* View space */
    4244varying vec3 pass_TNormal;
    4345varying vec4 pass_Color;
    4446
    45 // FIXME: all the light parameters should be passed in the code
    46 //Dir Light
    47 vec3 in_LightDir = vec3(-0.3, -0.3, -0.7);
    48 
    49 //Point Light
    50 vec4 in_Light2_Pos = vec4(20.0, 10.0, 0.0, 1.0);
    51 float in_Light2_Radius = 20.0;
    52 vec3 in_Light2_diffuse = vec3(0.4, 0.4, 1.0);
    53 
     47#if 0
    5448//Cube Light
    5549vec4 in_Light3_Pos = vec4(-10.0, 10.0, 5.0, 1.0);
     
    5751vec3 in_Light3_Size_Outer = vec3(15.0, 15.0, 15.0);
    5852vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4);
     53#endif
    5954
    6055void main(void)
     
    6560
    6661    /* World properties */
    67     float ambient_mul = 0.5;
    68     vec3 ambient_color = vec3(0.0, 0.0, 0.0);
    69     vec3 diffuse_color = vec3(0.4, 0.4, 0.4);
    70     vec3 specular_color = vec3(1.0, 1.0, 0.6);
    71 
    72     vec3 ambient = ambient_color;
     62    vec3 ambient = vec3(0.1, 0.1, 0.1);
    7363    vec3 specular = vec3(0.0, 0.0, 0.0);
    7464    vec3 diffuse = vec3(0.0, 0.0, 0.0);
    75     vec3 s = vec3(0.0, 0.0, 0.0);
    76     vec3 v = vec3(0.0, 0.0, 0.0);
    77     vec3 r = vec3(0.0, 0.0, 0.0);
    78     float sdotn = 0.0;
    79     float light_radius_mod = 0.0;
    8065
    81     //Light calculation for directional light
    82     s = normalize(-in_LightDir);
    83     v = normalize(-pass_Vertex.xyz);
    84     r = reflect(s, pass_TNormal);
     66    /* Light precalculations */
     67    vec3 v = normalize(-pass_Vertex.xyz);
    8568
    86     sdotn = max(dot(s, pass_TNormal), 0.0);
    87     diffuse += diffuse_color * sdotn;
    88     if (sdotn > 0.0)
    89         specular += specular_color * specular_reflect
    90                  * pow(max(dot(r, v), 0.0), specular_power);
    91     //----------
     69    /* Apply lighting */
     70    for (int i = 0; i < 8; i++)
     71    {
     72        vec4 pos = u_Lights[i * 2];
     73        vec4 color = u_Lights[i * 2 + 1];
     74        vec3 s, r;
    9275
    93     //Light calculation for point light
    94     vec3 tmpLightDir = (in_View * in_Light2_Pos).xyz - pass_Vertex.xyz;
    95     light_radius_mod = max(0.0, 1.0 - (length(tmpLightDir) / in_Light2_Radius));
    96     s = normalize(tmpLightDir);
    97     v = normalize(-pass_Vertex.xyz);
    98     r = reflect(-s, pass_TNormal);
     76        if (pos.w > 0.0)
     77        {
     78            /* Point light -- no attenuation yet */
     79            s = normalize((in_View * pos).xyz - pass_Vertex.xyz);
     80            r = reflect(-s, pass_TNormal);
     81        }
     82        else
     83        {
     84            /* Directional light */
     85            s = normalize(-pos.xyz);
     86            r = reflect(s, pass_TNormal);
     87        }
    9988
    100     sdotn = max(dot(s, pass_TNormal), 0.0);
    101     diffuse += in_Light2_diffuse * min(sdotn, light_radius_mod);
    102     if (sdotn > 0.0 && light_radius_mod > 0.0)
    103         specular += specular_color * min(specular_reflect, light_radius_mod)
    104                  * pow(max(dot(r, v), 0.0), specular_power);
    105     //----------
     89        float sdotn = max(dot(s, pass_TNormal), 0.0);
     90        diffuse += color.xyz * sdotn;
     91        if (sdotn > 0.0)
     92            specular += color.xyz * specular_reflect
     93                         * pow(max(dot(r, v), 0.0), specular_power);
     94    }
    10695
     96#if 0
    10797    //Light calculation for cube light
     98    vec3 specular_color = vec3(1.0, 1.0, 0.6);
    10899    vec3 Local_Vertex = (in_Inv_View * pass_Vertex).xyz - (in_Light3_Pos).xyz;
    109100    vec3 Proj_Vertex = clamp(Local_Vertex.xyz, -in_Light3_Size_Inner, in_Light3_Size_Inner);
     
    111102
    112103    vec3 light_radius = max(vec3(0.0,0.0,0.0), vec3(1.0,1.0,1.0) - abs(new_LightDir / in_Light3_Size_Outer));
    113     light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z));
     104    float light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z));
    114105
    115106    if (length(new_LightDir) == 0.0)
     
    126117    diffuse += in_Light3_diffuse * min(sdotn, light_radius_mod);
    127118    //----------
     119#endif
    128120
    129121    vec3 light = ambient + diffuse + specular;
    130122
    131     vec4 real_color = in_Damage * vec4(1.2, 1.2, 1.2, 1.0)
    132                     + (1.0 - in_Damage) * pass_Color;
     123    vec4 real_color = mix(pass_Color, vec4(1.2, 1.2, 1.2, 1.0), in_Damage);
    133124    gl_FragColor = real_color * vec4(light, 1.0);
    134125}
Note: See TracChangeset for help on using the changeset viewer.