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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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};
Note: See TracChangeset for help on using the changeset viewer.