// Define a 3D vertex
struct Vertex
{
float x,y,z;
};
// Define a 4 component color
struct Color
{
float r,g,b,a;
};
// Define a triangular face in terms of indices in an external vertex pool
struct IndexedTriangle
{
unsigned int vertices[3];
};
// Define the renderer
struct Renderer
{
// Construct given references to external data
Renderer(
unsigned int vCount,
unsigned int fCount,
Vertex* v,
Color* v,
IndexFace* f
)
{
vertices = v;
colors = c;
faces = f;
vertexCount = vCount;
faceCount = fCount;
}
// Number of vertices in vertex pool
unsigned int vertexCount;
// Number of faces
unsigned int faceCount;
// The external vertex pool
Vertex* vertices;
// The external color array
Color* colors;
// The external faces
IndexFace* faces;
// Prepare for rendering
void initialize();
// Render the model
void render();
};
void Renderer::initialize()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,sizeof(Vertex),vertices);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4,GL_FLOAT,sizeof(Color),colors);
}
void Renderer::render()
{
glDrawElements(
GL_TRIANGLES,
3*faceCount,
GL_UNSIGNED_INT,
faces
);
}