From 169a16b63ee5585ab1d70402c7de23dca8b84db7 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 15 Apr 2012 21:20:05 +0000 Subject: [PATCH] gpu: move the platform-specific triangle drawing call to the VertexDeclaration class, so that the calling code does not need to know what the backend is. --- src/gpu/vertexbuffer.cpp | 31 ++++++++++++++++++++---- src/gpu/vertexbuffer.h | 13 ++++++++++ test/tutorial/tut01.cpp | 52 ++-------------------------------------- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/src/gpu/vertexbuffer.cpp b/src/gpu/vertexbuffer.cpp index e8c586c2..79a4f88f 100644 --- a/src/gpu/vertexbuffer.cpp +++ b/src/gpu/vertexbuffer.cpp @@ -118,15 +118,39 @@ void VertexDeclaration::Bind() #endif } +void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count) +{ +#if defined _XBOX || defined USE_D3D9 + g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + switch (type) + { + case MeshPrimitive::Triangles: + g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, skip, count); + break; + } +#else + switch (type) + { + case MeshPrimitive::Triangles: + glDrawArrays(GL_TRIANGLES, skip * 3, count * 3); + break; + } +#endif +} + void VertexDeclaration::Unbind() { #if defined _XBOX || defined USE_D3D9 /* FIXME: Nothing to do? */ #else - /* FIXME: we need to record what happens */ + /* FIXME: we need to unbind what we bound */ //glDisableVertexAttribArray(m_attrib); /* FIXME: only useful for VAOs */ //glBindBuffer(GL_ARRAY_BUFFER, 0); + /* Or: */ + //glDisableVertexAttribArray(m_attrib); + /* Or even: */ + //glDisableClientState(GL_VERTEX_ARRAY); #endif } @@ -185,15 +209,12 @@ void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1, /* We need to parse the whole vertex declaration to retrieve * the information. It sucks. */ - int attr_index = 0, usage_index = 0, stream = -1; + int attr_index = 0, usage_index = 0; /* First, find the stream index */ for (; attr_index < m_count; attr_index++) if (m_streams[attr_index].usage == usage) if (usage_index++ == index) - { - stream = m_streams[attr_index].index; break; - } /* Now compute the stride and offset up to this stream index */ int stride = 0, offset = 0; diff --git a/src/gpu/vertexbuffer.h b/src/gpu/vertexbuffer.h index 2545c157..1ea63f42 100644 --- a/src/gpu/vertexbuffer.h +++ b/src/gpu/vertexbuffer.h @@ -61,6 +61,18 @@ struct VertexUsage inline operator Value() { return m_value; } }; +struct MeshPrimitive +{ + enum Value + { + Triangles, + } + m_value; + + inline MeshPrimitive(Value v) { m_value = v; } + inline operator Value() { return m_value; } +}; + class VertexStreamBase { friend class VertexDeclaration; @@ -164,6 +176,7 @@ public: ~VertexDeclaration(); void Bind(); + void DrawElements(MeshPrimitive type, int skip, int count); void Unbind(); void SetStream(VertexBuffer *vb, ShaderAttrib attr1, ShaderAttrib attr2 = ShaderAttrib(), diff --git a/test/tutorial/tut01.cpp b/test/tutorial/tut01.cpp index 4d5a4145..59717f42 100644 --- a/test/tutorial/tut01.cpp +++ b/test/tutorial/tut01.cpp @@ -13,18 +13,11 @@ #endif #include "core.h" -#include "lolgl.h" #include "loldebug.h" using namespace std; using namespace lol; -#if defined _WIN32 && defined USE_D3D9 -# define FAR -# define NEAR -# include -#endif - #if USE_SDL && defined __APPLE__ # include #endif @@ -38,12 +31,6 @@ using namespace lol; # include #endif -#if defined USE_D3D9 -extern IDirect3DDevice9 *g_d3ddevice; -#elif defined _XBOX -extern D3DDevice *g_d3ddevice; -#endif - class Triangle : public WorldEntity { public: @@ -61,21 +48,6 @@ public: if (!m_ready) { - - - - - - - - - - - - - - - m_shader = Shader::Create( #if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9 "#version 120\n" @@ -116,36 +88,16 @@ public: m_shader->Bind(); m_vdecl->Bind(); m_vdecl->SetStream(m_vbo, m_coord); -#if defined _XBOX || defined USE_D3D9 - g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); -#endif - -#if defined _XBOX || defined USE_D3D9 - g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); -#else - glDrawArrays(GL_TRIANGLES, 0, 3); -#endif - + m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1); m_vdecl->Unbind(); -#if defined _XBOX || defined USE_D3D9 - /* FIXME: do we need to unset anything here? */ -#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ - //glDisableVertexAttribArray(m_attrib); - //glBindBuffer(GL_ARRAY_BUFFER, 0); -#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ - /* Never used for now */ - glDisableVertexAttribArray(m_attrib); -#else - glDisableClientState(GL_VERTEX_ARRAY); -#endif } private: vec2 m_vertices[3]; Shader *m_shader; + ShaderAttrib m_coord; VertexDeclaration *m_vdecl; VertexBuffer *m_vbo; - ShaderAttrib m_coord; bool m_ready; };