Explorar el Código

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.
legacy
Sam Hocevar sam hace 12 años
padre
commit
169a16b63e
Se han modificado 3 ficheros con 41 adiciones y 55 borrados
  1. +26
    -5
      src/gpu/vertexbuffer.cpp
  2. +13
    -0
      src/gpu/vertexbuffer.h
  3. +2
    -50
      test/tutorial/tut01.cpp

+ 26
- 5
src/gpu/vertexbuffer.cpp Ver fichero

@@ -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;


+ 13
- 0
src/gpu/vertexbuffer.h Ver fichero

@@ -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(),


+ 2
- 50
test/tutorial/tut01.cpp Ver fichero

@@ -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 <d3d9.h>
#endif

#if USE_SDL && defined __APPLE__
# include <SDL_main.h>
#endif
@@ -38,12 +31,6 @@ using namespace lol;
# include <direct.h>
#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;
};



Cargando…
Cancelar
Guardar