# Thread: Vertex arrays and colors

1. ## Vertex arrays and colors

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.

Code :
```// 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).]

2. ## Re: Vertex arrays and colors

Try with stride = 0

3. ## Re: Vertex arrays and colors

That made no difference for some reason. I posted some pix at http://www.magnacom-inc.com/pleopard...ob/default.htm

4. ## Re: Vertex arrays and colors

I don't understand why you don't have a problem with your vertices, as well as your colours. Stride should be 0 for both glVertexPointer and glColorPointer given your structures.

5. ## Re: Vertex arrays and colors

Iceman,

The pictures seem to show several bands of color. Are you sure the color calculations are correct and do not need a scale-factor in the z direction (which from the pictures I see seems to run from left to right)?

HTH

Jean-Marc

6. ## Re: Vertex arrays and colors

knackered - stride is the offset between consecutive vertices (it's not a space between vertices), so putting sizeof(Vertex) will work fine, as his Vertex object is just 3 floats.

Setting the stride to 0 is also good, and a little easier to read (don't have to check the size of Vertex to understand it).

maular

[This message has been edited by maular (edited 02-21-2002).]

7. ## Re: Vertex arrays and colors

No, sorry maular, but you're wrong on this one.
Stride is the gap (in bytes) between n floats. A zero stride means no gap.
Check the docs if you don't believe me.

stride
The byte offset between consecutive vertices. When stride is zero, the vertices are tightly packed in the array.

You've already told gl how many bytes your vertex takes up with the first 2 arguments to glVertexPointer(3, GL_FLOAT....
The stride byte count will be added onto the vertex pointer AFTER the 3 floats have been accounted for.

Or maybe I'm wrong. I've never tested it. I've always worked with tightly packed arrays.

[This message has been edited by knackered (edited 02-21-2002).]

8. ## Re: Vertex arrays and colors

From the spec:
The values within each array element are stored sequentially in memory. If stride is specified as zero, then array elements are stored sequentially as well. Otherwise pointers to the ith and (i+1)st elements of an array differ by stride basic machine units (typically unsigned bytes), the pointer to the (i + 1)st element being greater.
Setting stride=0 is valid only if your data is tightly packed, whereas stride=sizeof(Vertex) works all the time. I personally prefer the latter.

Anyway, back to the problem at hand. Iceman, your vertex array setup code looks correct, but you could try rendering everything in immediate mode (glVertex3f, glColor4f) to rule out problems in that part of the code.

-- Tom

9. ## Re: Vertex arrays and colors

knackered, it's very easy to try. Use the following vertex array (pseudo array)
Code :
`float va[] = {x0, y0, z0, 0, 0, x1, y1, z1, 0, 0, x2, y2, z2, 0, 0};`
Where x0 to z2 is some coordinates for a triangle, and the zeros are dummy elements. Try set that one up with a stride of 8 (2 * sizeof float). It should work better with a stride of 20 (5 * sizeof float), which is the distance from the beginning of one vertex to the beginning of the next vertex.

Stride = 0 is only a special case.

10. ## Re: Vertex arrays and colors

Oh right, cheers. Sorry iceman and maular - talking out of my arse again.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•