Changeset 2289 for trunk


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
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r2265 r2289  
    1515    worldentity.cpp worldentity.h gradient.cpp gradient.h gradient.lolfx \
    1616    platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \
     17    light.cpp light.h \
    1718    \
    1819    lol/base/log.h lol/base/array.h lol/base/types.h lol/base/array.h \
  • trunk/src/core.h

    r2277 r2289  
    117117
    118118#include "camera.h"
     119#include "light.h"
    119120#include "emitter.h"
    120121#include "font.h"
  • 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}
  • trunk/src/entity.h

    r2216 r2289  
    6060    {
    6161        DRAWGROUP_BEFORE = GAMEGROUP_END,
     62        DRAWGROUP_LIGHT,
    6263        DRAWGROUP_CAMERA,
    6364        DRAWGROUP_DEFAULT,
  • trunk/src/lolcore.vcxproj

    r2265 r2289  
    274274    <ClCompile Include="input\stick.cpp" />
    275275    <ClCompile Include="layer.cpp" />
     276    <ClCompile Include="light.cpp" />
    276277    <ClCompile Include="map.cpp" />
    277278    <ClCompile Include="math\geometry.cpp" />
     
    584585    <ClInclude Include="input\stick.h" />
    585586    <ClInclude Include="layer.h" />
     587    <ClInclude Include="light.h" />
    586588    <ClInclude Include="loldebug.h" />
    587589    <ClInclude Include="lolgl.h" />
  • trunk/src/lolcore.vcxproj.filters

    r2265 r2289  
    190190      <Filter>...</Filter>
    191191    </ClCompile>
     192    <ClCompile Include="light.cpp">
     193      <Filter>...</Filter>
     194    </ClCompile>
    192195    <ClCompile Include="ticker.cpp">
    193196      <Filter>...</Filter>
     
    775778    </ClInclude>
    776779    <ClInclude Include="layer.h">
     780      <Filter>...</Filter>
     781    </ClInclude>
     782    <ClInclude Include="light.h">
    777783      <Filter>...</Filter>
    778784    </ClInclude>
  • trunk/src/scene.cpp

    r2277 r2289  
    6262    mat4 m_proj_matrix;
    6363
    64     Array<Tile> tiles;
     64    Array<Tile> m_tiles;
     65    Array<Light *> m_lights;
    6566
    6667    Shader *m_shader;
     
    114115        delete data->bufs[i];
    115116    data->bufs.Empty();
     117    data->m_lights.Empty();
    116118}
    117119
     
    147149    t.scale = scale;
    148150
    149     data->tiles.Push(t);
     151    data->m_tiles.Push(t);
     152}
     153
     154void Scene::AddLight(Light *l)
     155{
     156    data->m_lights.Push(l);
     157}
     158
     159Array<Light *> const &Scene::GetLights() const
     160{
     161    return data->m_lights;
    150162}
    151163
     
    153165{
    154166    /* Early exit if nothing needs to be rendered */
    155     if (!data->tiles.Count())
     167    if (!data->m_tiles.Count())
    156168        return;
    157169
     
    161173#if 0
    162174    // Randomise, then sort.
    163     for (int i = 0; i < data->tiles.Count(); i++)
     175    for (int i = 0; i < data->m_tiles.Count(); i++)
    164176    {
    165         Tile tmp = data->tiles[i];
    166         int j = rand() % data->tiles.Count();
    167         data->tiles[i] = data->tiles[j];
    168         data->tiles[j] = tmp;
     177        Tile tmp = data->m_tiles[i];
     178        int j = rand() % data->m_tiles.Count();
     179        data->m_tiles[i] = data->m_tiles[j];
     180        data->m_tiles[j] = tmp;
    169181    }
    170182#endif
    171     qsort(&data->tiles[0], data->tiles.Count(),
     183    qsort(&data->m_tiles[0], data->m_tiles.Count(),
    172184          sizeof(Tile), SceneData::Compare);
    173185
     
    220232#endif
    221233
    222     for (int buf = 0, i = 0, n; i < data->tiles.Count(); i = n, buf += 2)
     234    for (int buf = 0, i = 0, n; i < data->m_tiles.Count(); i = n, buf += 2)
    223235    {
    224236        /* Count how many quads will be needed */
    225         for (n = i + 1; n < data->tiles.Count(); n++)
    226             if (data->tiles[i].tileset != data->tiles[n].tileset)
     237        for (n = i + 1; n < data->m_tiles.Count(); n++)
     238            if (data->m_tiles[i].tileset != data->m_tiles[n].tileset)
    227239                break;
    228240
     
    238250        for (int j = i; j < n; j++)
    239251        {
    240             data->tiles[i].tileset->BlitTile(data->tiles[j].id,
    241                             data->tiles[j].pos, data->tiles[j].o,
    242                             data->tiles[j].scale,
     252            data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id,
     253                            data->m_tiles[j].pos, data->m_tiles[j].o,
     254                            data->m_tiles[j].scale,
    243255                            vertex + 18 * (j - i), texture + 12 * (j - i));
    244256        }
     
    248260
    249261        /* Bind texture */
    250         data->tiles[i].tileset->Bind();
     262        data->m_tiles[i].tileset->Bind();
    251263
    252264        /* Bind vertex and texture coordinate buffers */
     
    258270        data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
    259271        data->m_vdecl->Unbind();
    260         data->tiles[i].tileset->Unbind();
     272        data->m_tiles[i].tileset->Unbind();
    261273    }
    262274
    263     data->tiles.Empty();
     275    data->m_tiles.Empty();
    264276
    265277    data->m_shader->Unbind();
  • trunk/src/scene.h

    r2216 r2289  
    2020
    2121#include "tileset.h"
     22#include "light.h"
    2223
    2324namespace lol
     
    4647    void AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale);
    4748
     49    void AddLight(Light *light);
     50    Array<Light *> const &GetLights() const;
     51
    4852private:
    4953    SceneData *data;
  • trunk/tutorial/05_easymesh.cpp

    r2237 r2289  
    5555        Ticker::Ref(m_camera);
    5656
     57        /* Add a white directional light */
     58        m_light1 = new Light();
     59        m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
     60        m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
     61        Ticker::Ref(m_light1);
     62
     63        /* Add an orangeish point light */
     64        m_light2 = new Light();
     65        m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
     66        m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
     67        Ticker::Ref(m_light2);
     68
    5769        m_ready = false;
    5870    }
     
    6173    {
    6274        Ticker::Unref(m_camera);
     75        Ticker::Unref(m_light1);
     76        Ticker::Unref(m_light2);
    6377    }
    6478
     
    115129    mat4 m_mat;
    116130    Camera *m_camera;
     131    Light *m_light1, *m_light2;
    117132
    118133    bool m_ready;
Note: See TracChangeset for help on using the changeset viewer.