Iceman

02-20-2002, 08:32 AM

I have a kind of funky color problem with vertex arrays. I load arrays of vertices, colors, and triangle indices as in the following code. The vertices and color arrays are of the same size and have a 1-to-1 correspondence. The triangle array contains a set of faces defined in terms of indices in the vertices array. I compute the color RGB components according to the height (z) value of each vertex. As it turns out, I get a bogus color scheme when rendering the model, all the colors are mixed up. I suspect that my understanding of vertex arrays is flawed so if you see something blatant in the following code please let me know.

Thanks in advance!

// 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

);

}

[This message has been edited by Iceman (edited 02-20-2002).]

Thanks in advance!

// 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

);

}

[This message has been edited by Iceman (edited 02-20-2002).]