Changeset 1247


Ignore:
Timestamp:
Apr 21, 2012, 10:18:24 AM (9 years ago)
Author:
sam
Message:

gpu: abstraction class for index buffers; the cube tutorial no longer
needs to know about Direct3D headers etc.

Location:
trunk
Files:
1 added
1 deleted
7 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r1244 r1247  
    3030    \
    3131    gpu/shader.cpp gpu/shader.h \
    32     gpu/vbo.cpp gpu/vbo.h \
     32    gpu/indexbuffer.cpp gpu/indexbuffer.h \
    3333    gpu/vertexbuffer.cpp gpu/vertexbuffer.h \
    3434    \
  • trunk/src/core.h

    r1243 r1247  
    101101#include "layer.h"
    102102#include "gpu/shader.h"
     103#include "gpu/indexbuffer.h"
    103104#include "gpu/vertexbuffer.h"
    104 #include "gpu/vbo.h"
    105105#include "image/image.h"
    106106#include "application/application.h"
  • trunk/src/gpu/indexbuffer.h

    r1246 r1247  
    22// Lol Engine
    33//
    4 // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
     4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
    55//   This program is free software; you can redistribute it and/or
    66//   modify it under the terms of the Do What The Fuck You Want To
     
    1010
    1111//
    12 // The GpuVbo class
    13 // ----------------
     12// The IndexBuffer class
     13// ---------------------
    1414//
    1515
    16 #if !defined __LOL_VBO_H__
    17 #define __LOL_VBO_H__
     16#if !defined __LOL_INDEXBUFFER_H__
     17#define __LOL_INDEXBUFFER_H__
     18
     19#include <cstring>
    1820
    1921namespace lol
    2022{
    2123
    22 class GpuVboData;
    23 
    24 class GpuVbo
     24class IndexBuffer
    2525{
    2626public:
    27     GpuVbo();
    28     ~GpuVbo();
     27    IndexBuffer(size_t size);
     28    ~IndexBuffer();
    2929
    30     void SetSize(size_t elemsize, size_t elemcount);
    31     size_t GetSize();
    32     uint8_t *GetData();
    33     uint8_t const *GetData() const;
     30    void *Lock(size_t offset, size_t size);
     31    void Unlock();
    3432
    3533    void Bind();
     
    3735
    3836private:
    39     GpuVboData *data;
     37    class IndexBufferData *m_data;
    4038};
    4139
    4240} /* namespace lol */
    4341
    44 #endif // __LOL_VBO_H__
     42#endif // __LOL_INDEXBUFFER_H__
    4543
  • trunk/src/gpu/vertexbuffer.cpp

    r1246 r1247  
    138138    case MeshPrimitive::Triangles:
    139139        glDrawArrays(GL_TRIANGLES, skip * 3, count * 3);
     140        break;
     141    }
     142#endif
     143}
     144
     145void VertexDeclaration::DrawIndexedElements(MeshPrimitive type, int vbase,
     146                                            int vskip, int vcount,
     147                                            int skip, int count)
     148{
     149#if defined _XBOX || defined USE_D3D9
     150    if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
     151        Abort();
     152    switch (type)
     153    {
     154    case MeshPrimitive::Triangles:
     155        if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vbase, vskip, vcount, skip, count)))
     156            Abort();
     157        break;
     158    }
     159#else
     160    switch (type)
     161    {
     162    case MeshPrimitive::Triangles:
     163        /* FIXME: ignores most of the arguments! */
     164        glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_SHORT, 0);
    140165        break;
    141166    }
  • trunk/src/gpu/vertexbuffer.h

    r1231 r1247  
    178178    void Bind();
    179179    void DrawElements(MeshPrimitive type, int skip, int count);
     180    void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip,
     181                             int vcount, int skip, int count);
    180182    void Unbind();
    181183    void SetStream(VertexBuffer *vb, ShaderAttrib attr1,
  • trunk/test/tutorial/02_cube.cpp

    r1243 r1247  
    1414
    1515#include "core.h"
    16 #include "lolgl.h"
    1716#include "loldebug.h"
    1817
    1918using namespace std;
    2019using namespace lol;
    21 
    22 #if defined _WIN32 && defined USE_D3D9
    23 #   define FAR
    24 #   define NEAR
    25 #   include <d3d9.h>
    26 #endif
    2720
    2821#if USE_SDL && defined __APPLE__
     
    3326#   undef main /* FIXME: still needed? */
    3427#   include <direct.h>
    35 #endif
    36 
    37 #if defined USE_D3D9
    38 extern IDirect3DDevice9 *g_d3ddevice;
    39 #elif defined _XBOX
    40 extern D3DDevice *g_d3ddevice;
    4128#endif
    4229
     
    145132            m_vbo->Unlock();
    146133
    147 #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
    148             /* Method 1: store vertex buffer on the GPU memory */
    149             glGenBuffers(1, &m_ibo);
    150             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
    151             glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.Bytes(),
    152                          &m_indices[0], GL_STATIC_DRAW);
    153 #elif defined _XBOX || defined USE_D3D9
    154             int16_t *indices;
    155             if (FAILED(g_d3ddevice->CreateIndexBuffer(m_indices.Bytes(), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL)))
    156                 Abort();
    157             if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0)))
    158                 Abort();
     134            m_ibo = new IndexBuffer(m_indices.Bytes());
     135            void *indices = m_ibo->Lock(0, 0);
    159136            memcpy(indices, &m_indices[0], m_indices.Bytes());
    160137            m_ibo->Unlock();
    161 #else
    162             /* TODO */
    163 #endif
    164138
    165139            /* FIXME: this object never cleans up */
     
    173147        m_vdecl->SetStream(m_vbo, m_coord, m_color);
    174148        m_vdecl->Bind();
    175 
    176 #if defined _XBOX || defined USE_D3D9
    177         if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
    178             Abort();
    179         if (FAILED(g_d3ddevice->SetIndices(m_ibo)))
    180             Abort();
    181         if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, m_indices.Count())))
    182             Abort();
    183 #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
    184         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
    185         int size;
    186         glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
    187 
    188 
    189         glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
    190 
    191         glBindBuffer(GL_ARRAY_BUFFER, 0);
    192 #else
    193         /* FIXME */
    194         glEnableClientState(GL_VERTEX_ARRAY);
    195         glVertexPointer(3, GL_FLOAT, 0, m_vertices);
    196         glDisableClientState(GL_VERTEX_ARRAY);
    197 #endif
     149        m_ibo->Bind();
     150        m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, 8, 0,
     151                                     m_indices.Count());
     152        m_ibo->Unbind();
    198153        m_vdecl->Unbind();
    199154    }
     
    209164    VertexDeclaration *m_vdecl;
    210165    VertexBuffer *m_vbo;
    211 #if defined USE_D3D9
    212     IDirect3DIndexBuffer9 *m_ibo;
    213 #elif defined _XBOX
    214     D3DIndexBuffer *m_ibo;
    215 #else
    216 #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
    217     GLuint m_ibo;
    218 #endif
    219 #endif
     166    IndexBuffer *m_ibo;
    220167    bool m_ready;
    221168};
  • trunk/win32/lolcore.vcxproj

    r1237 r1247  
    8787    <ClCompile Include="..\src\forge.cpp" />
    8888    <ClCompile Include="..\src\gpu\shader.cpp" />
    89     <ClCompile Include="..\src\gpu\vbo.cpp" />
    9089    <ClCompile Include="..\src\gpu\vertexbuffer.cpp" />
    9190    <ClCompile Include="..\src\gradient.cpp" />
     
    141140    <ClInclude Include="..\src\forge.h" />
    142141    <ClInclude Include="..\src\gpu\shader.h" />
    143     <ClInclude Include="..\src\gpu\vbo.h" />
    144142    <ClInclude Include="..\src\gpu\vertexbuffer.h" />
    145143    <ClInclude Include="..\src\gradient.h" />
  • trunk/win32/lolcore.vcxproj.filters

    r1224 r1247  
    161161      <Filter>src</Filter>
    162162    </ClCompile>
     163    <ClCompile Include="..\src\gpu\indexbuffer.cpp">
     164      <Filter>src\gpu</Filter>
     165    </ClCompile>
    163166    <ClCompile Include="..\src\gpu\shader.cpp">
    164       <Filter>src\gpu</Filter>
    165     </ClCompile>
    166     <ClCompile Include="..\src\gpu\vbo.cpp">
    167167      <Filter>src\gpu</Filter>
    168168    </ClCompile>
     
    337337      <Filter>src\lol</Filter>
    338338    </ClInclude>
     339    <ClInclude Include="..\src\gpu\indexbuffer.h">
     340      <Filter>src\gpu</Filter>
     341    </ClInclude>
    339342    <ClInclude Include="..\src\gpu\shader.h">
    340       <Filter>src\gpu</Filter>
    341     </ClInclude>
    342     <ClInclude Include="..\src\gpu\vbo.h">
    343343      <Filter>src\gpu</Filter>
    344344    </ClInclude>
Note: See TracChangeset for help on using the changeset viewer.