Skip to main content
gamedev.net
🔒 Locked

Vertex Array trouble

Started by DuckWizard May 15, 2004 at 7:03 PM 1 replies 300+ views
Original Post
DuckWizard
DuckWizard
Okay, back story: I have a terrain engine, which uses a 2D heightmap and draws it with quads. This was working spectacularly. Then I wanted more speed. So I decided to restart it entirely with using an octree (well, more like a quadtree since it''s only one cube high). So I decided I would delve into vertex arrays. I decided to make one big array of my vertices, each chunk of the terrain (16x16 chunks, if it matters) would be represented as an array of indices into the vertex array. This doesn''t work. I''ve defined my vertex and normal arrays in 1 dimension as arrays of structs. The structs are as follows (vertex for the vertices, vector for the normals):
typedef struct vertex
{
	float x;
	float y;
	float z;
} vertex;

typedef struct vector
{
	float x;
	float y;
	float z;
} vector;
AFAIK this should be laid out in memory the same way as an array of floats, like {x, y, z, x, y, z, x, y, z...} etc. Am I wrong? Because this works:
for(int j = 248; j < 267 - 1; j++)
{
	glBegin(GL_QUAD_STRIP);
	for(int x = 248; x < 267; x++)
	{
		int index = j * width + x; //indices for quad strip (vertex and the vertex below it)

        int index2 = (j + 1) * width + x;

		glNormal3f(normals[index].x, normals[index].y, normals[index].z);
		glVertex3f(vertices[index].x, vertices[index].y, vertices[index].z);

		glNormal3f(normals[index2].x, normals[index2].y, normals[index2].z);
		glVertex3f(vertices[index2].x, vertices[index2].y, vertices[index2].z);
	}
	glEnd();
}
But this does not:
for(int j = 248; j < 267 - 1; j++)
{
	glBegin(GL_QUAD_STRIP);
	for(int x = 248; x < 267; x++)
	{
		int index = j * width + x;
        int index2 = (j + 1) * width + x;
		glArrayElement(index);
		glArrayElement(index2);
		
	}
	glEnd();
}
Shouldn''t these be equivalent? Am I not allowed to use structs here? BTW, I defined the arrays like this:
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
Thanks, -Jeremy
weasalmongler
weasalmongler
Well I dont think you should have the calls to glBegin or glEnd, as I think they are called from within the drawelement function. EDIT : Actually I'm not sure thats right, I might be thinking of the DrawElements function. I cant remember off hand.

Did u remember to enable vertex arrays? What does the second implementation do? draw garbage or nothing at all?

- James



[edited by - weasalmongler on May 16, 2004 6:05:01 AM]
DuckWizard
DuckWizard
I changed it to enable the arrays at the beginning of every frame instead of once during initialization - it must have gotten clobbered somehow. Now everything works like a charm.

Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.