Changeset 2500


Ignore:
Timestamp:
Feb 26, 2013, 2:21:50 AM (6 years ago)
Author:
sam
Message:

debug: add Debug::DrawLine() and Debug::DrawBox() methods.

Location:
trunk/src
Files:
5 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r2479 r2500  
    5151    lol/gpu/framebuffer.h lol/gpu/texture.h lol/gpu/lolfx.h \
    5252    \
     53    lol/debug/debug.h \
     54    lol/debug/line.h \
     55    \
    5356    lol/unit.h
    5457
     
    8184    \
    8285    gpu/defaultmaterial.lolfx \
    83     gpu/tile.lolfx \
     86    gpu/tile.lolfx gpu/line.lolfx \
    8487    gpu/emptymaterial.lolfx \
    8588    gpu/testmaterial.lolfx \
     
    102105    \
    103106    loldebug.h \
    104     debug/fps.cpp debug/fps.h \
     107    debug/fps.cpp debug/fps.h debug/lines.cpp \
    105108    debug/record.cpp debug/record.h debug/stats.cpp debug/stats.h
    106109
  • trunk/src/core.h

    r2354 r2500  
    8585#include <lol/image/image.h>
    8686#include <lol/gpu/gpu.h>
     87#include <lol/debug/debug.h>
    8788
    8889#include "numeric.h"
  • trunk/src/lolcore.vcxproj

    r2479 r2500  
    9696    <ClCompile Include="base\string.cpp" />
    9797    <ClCompile Include="debug\fps.cpp" />
     98    <ClCompile Include="debug\lines.cpp" />
    9899    <ClCompile Include="debug\record.cpp" />
    99100    <ClCompile Include="debug\stats.cpp" />
     
    204205    <ClInclude Include="lol\base\string.h" />
    205206    <ClInclude Include="lol\base\types.h" />
     207    <ClInclude Include="lol\debug\debug.h" />
     208    <ClInclude Include="lol\debug\lines.h" />
    206209    <ClInclude Include="lol\gpu\framebuffer.h" />
    207210    <ClInclude Include="lol\gpu\gpu.h" />
     
    264267    <LolFxCompile Include="gpu\defaultmaterial.lolfx" />
    265268    <LolFxCompile Include="gpu\emptymaterial.lolfx" />
     269    <LolFxCompile Include="gpu\line.lolfx" />
    266270    <LolFxCompile Include="gpu\testmaterial.lolfx" />
    267271    <LolFxCompile Include="gpu\tile.lolfx" />
  • trunk/src/lolcore.vcxproj.filters

    r2479 r2500  
    593593      <Filter>...</Filter>
    594594    </LolFxCompile>
     595    <LolFxCompile Include="gpu\line.lolfx">
     596      <Filter>...</Filter>
     597    </LolFxCompile>
    595598    <LolFxCompile Include="gpu\tile.lolfx">
    596599      <Filter>...</Filter>
  • trunk/src/scene.cpp

    r2476 r2500  
    2424
    2525LOLFX_RESOURCE_DECLARE(tile);
     26LOLFX_RESOURCE_DECLARE(line);
    2627
    2728namespace lol
     
    5859    }
    5960
     61    Array<vec3, vec3, vec4> m_lines;
     62    Shader *m_line_shader;
     63    VertexDeclaration *m_line_vdecl;
     64
    6065    Array<Tile> m_tiles;
    6166    Array<Light *> m_lights;
    6267
    63     Shader *m_shader;
    64     VertexDeclaration *m_vdecl;
    65     Array<VertexBuffer *> bufs;
     68    Shader *m_tile_shader;
     69    VertexDeclaration *m_tile_vdecl;
     70    Array<VertexBuffer *> m_tile_bufs;
    6671
    6772    Camera *m_default_cam;
     
    8792    PushCamera(data->m_default_cam);
    8893
    89     data->m_shader = 0;
    90     data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
    91                                           VertexStream<vec2>(VertexUsage::TexCoord));
     94    data->m_tile_shader = 0;
     95    data->m_tile_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
     96                                               VertexStream<vec2>(VertexUsage::TexCoord));
     97
     98    data->m_line_shader = 0;
     99    data->m_line_vdecl = new VertexDeclaration(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::Color));
    92100}
    93101
     
    102110    Reset();
    103111
    104     delete data->m_vdecl;
     112    delete data->m_line_vdecl;
     113    delete data->m_tile_vdecl;
    105114    delete data;
    106115}
     
    143152void Scene::Reset()
    144153{
    145     for (int i = 0; i < data->bufs.Count(); i++)
    146         delete data->bufs[i];
    147     data->bufs.Empty();
     154    for (int i = 0; i < data->m_tile_bufs.Count(); i++)
     155        delete data->m_tile_bufs[i];
     156    data->m_tile_bufs.Empty();
     157
    148158    data->m_lights.Empty();
    149159}
     
    163173}
    164174
     175void Scene::AddLine(vec3 a, vec3 b, vec4 color)
     176{
     177    data->m_lines.Push(a, b, color);
     178}
     179
    165180void Scene::AddLight(Light *l)
    166181{
     
    175190void Scene::Render() // XXX: rename to Blit()
    176191{
    177     /* Early exit if nothing needs to be rendered */
    178     if (!data->m_tiles.Count())
    179         return;
    180 
    181     if (!data->m_shader)
    182         data->m_shader = Shader::Create(LOLFX_RESOURCE_NAME(tile));
    183 
    184 #if 0
    185     // Randomise, then sort.
    186     for (int i = 0; i < data->m_tiles.Count(); i++)
    187     {
    188         Tile tmp = data->m_tiles[i];
    189         int j = std::rand() % data->m_tiles.Count();
    190         data->m_tiles[i] = data->m_tiles[j];
    191         data->m_tiles[j] = tmp;
    192     }
    193 #endif
    194     qsort(&data->m_tiles[0], data->m_tiles.Count(),
    195           sizeof(Tile), SceneData::Compare);
    196 
    197     ShaderUniform uni_mat, uni_tex;
    198     ShaderAttrib attr_pos, attr_tex;
    199     attr_pos = data->m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
    200     attr_tex = data->m_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0);
    201 
    202     data->m_shader->Bind();
    203 
    204     uni_mat = data->m_shader->GetUniformLocation("proj_matrix");
    205     data->m_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
    206     uni_mat = data->m_shader->GetUniformLocation("view_matrix");
    207     data->m_shader->SetUniform(uni_mat, GetCamera()->GetView());
    208     uni_mat = data->m_shader->GetUniformLocation("model_matrix");
    209     data->m_shader->SetUniform(uni_mat, mat4(1.f));
    210 
    211192#if defined USE_D3D9 || defined _XBOX
    212193#else
    213     uni_tex = data->m_shader->GetUniformLocation("in_Texture");
    214     data->m_shader->SetUniform(uni_tex, 0);
    215 
    216 #if !defined HAVE_GLES_2X
    217     glEnable(GL_TEXTURE_2D);
    218 #endif
    219194    glEnable(GL_DEPTH_TEST);
    220195    glDepthFunc(GL_LEQUAL);
     
    227202#endif
    228203
    229     for (int buf = 0, i = 0, n; i < data->m_tiles.Count(); i = n, buf += 2)
     204    /* Early test if nothing needs to be rendered */
     205    if (data->m_tiles.Count())
    230206    {
    231         /* Count how many quads will be needed */
    232         for (n = i + 1; n < data->m_tiles.Count(); n++)
    233             if (data->m_tiles[i].tileset != data->m_tiles[n].tileset)
    234                 break;
    235 
    236         /* Create a vertex array object */
    237         VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float));
    238         float *vertex = (float *)vb1->Lock(0, 0);
    239         VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float));
    240         float *texture = (float *)vb2->Lock(0, 0);
    241 
    242         data->bufs.Push(vb1);
    243         data->bufs.Push(vb2);
    244 
    245         for (int j = i; j < n; j++)
     207#if defined USE_D3D9 || defined _XBOX
     208#elif !defined HAVE_GLES_2X
     209        glEnable(GL_TEXTURE_2D);
     210#endif
     211        if (!data->m_tile_shader)
     212            data->m_tile_shader = Shader::Create(LOLFX_RESOURCE_NAME(tile));
     213
     214#if 0
     215        // Randomise, then sort.
     216        for (int i = 0; i < data->m_tiles.Count(); i++)
    246217        {
    247             data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id,
    248                             data->m_tiles[j].pos, data->m_tiles[j].o,
    249                             data->m_tiles[j].scale,
    250                             vertex + 18 * (j - i), texture + 12 * (j - i));
     218            Tile tmp = data->m_tiles[i];
     219            int j = std::rand() % data->m_tiles.Count();
     220            data->m_tiles[i] = data->m_tiles[j];
     221            data->m_tiles[j] = tmp;
    251222        }
    252 
    253         vb1->Unlock();
    254         vb2->Unlock();
    255 
    256         /* Bind texture */
    257         data->m_tiles[i].tileset->Bind();
    258 
    259         /* Bind vertex and texture coordinate buffers */
    260         data->m_vdecl->Bind();
    261         data->m_vdecl->SetStream(vb1, attr_pos);
    262         data->m_vdecl->SetStream(vb2, attr_tex);
    263 
    264         /* Draw arrays */
    265         data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
    266         data->m_vdecl->Unbind();
    267         data->m_tiles[i].tileset->Unbind();
     223#endif
     224        qsort(&data->m_tiles[0], data->m_tiles.Count(),
     225              sizeof(Tile), SceneData::Compare);
     226
     227        ShaderUniform uni_mat, uni_tex;
     228        ShaderAttrib attr_pos, attr_tex;
     229        attr_pos = data->m_tile_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
     230        attr_tex = data->m_tile_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0);
     231
     232        data->m_tile_shader->Bind();
     233
     234        uni_mat = data->m_tile_shader->GetUniformLocation("proj_matrix");
     235        data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
     236        uni_mat = data->m_tile_shader->GetUniformLocation("view_matrix");
     237        data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetView());
     238        uni_mat = data->m_tile_shader->GetUniformLocation("model_matrix");
     239        data->m_tile_shader->SetUniform(uni_mat, mat4(1.f));
     240
     241        uni_tex = data->m_tile_shader->GetUniformLocation("in_Texture");
     242        data->m_tile_shader->SetUniform(uni_tex, 0);
     243
     244        for (int buf = 0, i = 0, n; i < data->m_tiles.Count(); i = n, buf += 2)
     245        {
     246            /* Count how many quads will be needed */
     247            for (n = i + 1; n < data->m_tiles.Count(); n++)
     248                if (data->m_tiles[i].tileset != data->m_tiles[n].tileset)
     249                    break;
     250
     251            /* Create a vertex array object */
     252            VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float));
     253            float *vertex = (float *)vb1->Lock(0, 0);
     254            VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float));
     255            float *texture = (float *)vb2->Lock(0, 0);
     256
     257            data->m_tile_bufs.Push(vb1);
     258            data->m_tile_bufs.Push(vb2);
     259
     260            for (int j = i; j < n; j++)
     261            {
     262                data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id,
     263                                data->m_tiles[j].pos, data->m_tiles[j].o,
     264                                data->m_tiles[j].scale,
     265                                vertex + 18 * (j - i), texture + 12 * (j - i));
     266            }
     267
     268            vb1->Unlock();
     269            vb2->Unlock();
     270
     271            /* Bind texture */
     272            data->m_tiles[i].tileset->Bind();
     273
     274            /* Bind vertex and texture coordinate buffers */
     275            data->m_tile_vdecl->Bind();
     276            data->m_tile_vdecl->SetStream(vb1, attr_pos);
     277            data->m_tile_vdecl->SetStream(vb2, attr_tex);
     278
     279            /* Draw arrays */
     280            data->m_tile_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
     281            data->m_tile_vdecl->Unbind();
     282            data->m_tiles[i].tileset->Unbind();
     283        }
     284
     285        data->m_tiles.Empty();
     286
     287        data->m_tile_shader->Unbind();
     288
     289#if defined USE_D3D9 || defined _XBOX
     290        /* TODO */
     291#elif !defined HAVE_GLES_2X
     292        glDisable(GL_TEXTURE_2D);
     293#endif
    268294    }
    269295
    270     data->m_tiles.Empty();
    271 
    272     data->m_shader->Unbind();
     296    if (data->m_lines.Count())
     297    {
     298        int linecount = data->m_lines.Count();
     299
     300        if (!data->m_line_shader)
     301            data->m_line_shader = Shader::Create(LOLFX_RESOURCE_NAME(line));
     302
     303        VertexBuffer *vb = new VertexBuffer((sizeof(vec3) + sizeof(vec4)) * 2 * linecount);
     304        float *vertex = (float *)vb->Lock(0, 0);
     305        for (int i = 0; i < linecount; i++)
     306        {
     307            memcpy(vertex, &data->m_lines[i].m1, sizeof(vec3));
     308            vertex += 3;
     309            memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
     310            vertex += 4;
     311            memcpy(vertex, &data->m_lines[i].m2, sizeof(vec3));
     312            vertex += 3;
     313            memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
     314            vertex += 4;
     315        }
     316        vb->Unlock();
     317
     318        data->m_line_shader->Bind();
     319
     320        ShaderUniform uni_mat, uni_tex;
     321        ShaderAttrib attr_pos, attr_col;
     322        attr_pos = data->m_line_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
     323        attr_col = data->m_line_shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
     324
     325        data->m_line_shader->Bind();
     326
     327        uni_mat = data->m_line_shader->GetUniformLocation("proj_matrix");
     328        data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
     329        uni_mat = data->m_line_shader->GetUniformLocation("view_matrix");
     330        data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetView());
     331
     332        data->m_line_vdecl->Bind();
     333        data->m_line_vdecl->SetStream(vb, attr_pos, attr_col);
     334        data->m_line_vdecl->DrawElements(MeshPrimitive::Lines, 0, 2 * linecount);
     335        data->m_line_vdecl->Unbind();
     336        data->m_line_shader->Unbind();
     337
     338        data->m_lines.Empty();
     339        delete vb;
     340    }
    273341
    274342#if defined USE_D3D9 || defined _XBOX
    275343    /* TODO */
    276344#else
    277 #if !defined HAVE_GLES_2X
    278     glDisable(GL_TEXTURE_2D);
    279 #endif
    280345    glDisable(GL_DEPTH_TEST);
    281 #if defined HAVE_GL_2X && !defined __APPLE__
     346#   if defined HAVE_GL_2X && !defined __APPLE__
    282347    glDisable(GL_ALPHA_TEST);
    283 #endif
     348#   endif
    284349    glDisable(GL_BLEND);
    285350#endif
  • trunk/src/scene.h

    r2476 r2500  
    4646     * the architecture we want to build */
    4747    void AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale);
     48    void AddLine(vec3 a, vec3 b, vec4 color);
    4849
    4950    void AddLight(Light *light);
Note: See TracChangeset for help on using the changeset viewer.