Skip to main content
GameDev.net gamedev.net
🔒 Locked

Render a 2D array using vertex arrays

Started by Subotron Sep 22, 2006 at 12:22 PM 1 replies 4.1k views
Original Post
Subotron
Subotron
I'm trying to speed up my program by rendering my 2D vertices grid using vertex arrays. The problem is I saved the vertices in a 2-dimensional array (vertices[x][z]). x and z have the same limit, which is the map_length (length in one direction). Normally my drawing function (which draws the array in quads) draws the whole array somewhat like this: glBegin(GL_QUADS) for (int z = 0; z < map_length - 1; z++) { for (int x = 0; x < map_length - 1; x++) { glVertex3f(vertices[x][z].x, vertices[x][z].y, vertices[x][z].z); glVertex3f(vertices[x+1][z].x, vertices[x+1][z].y, vertices[x+1][z].z); glVertex3f(vertices[x+1][z+1].x, vertices[x+1][z+1].y, vertices[x+1][z+1].z); glVertex3f(vertices[x][z+1].x, vertices[x][z+1].y, vertices[x][z+1].z); } } glEnd(); Unfortunately, vertex array functions do not use 2-dimensional arrays, and even if I could use it, the points would only be used once and I would not get proper rendering (each vertex is used 4 times, except at edges). I thought about making a one-dimensional array out of the vertices, but this would use 4 times as much memory, which seems a bit steep. I also thought of making the one-dimensional vertex array an array of pointers, all pointing to the right place in the two-dimensional array (I'd very much like to keep usign the two-dimensional array, since it saves me quite a lot of code in functions that edit the grid). Well, the above method doesn't work for me. I suspect OpenGL Vertex Arrays don't like arrays of pointers. Who knows a/the solution? I'd be very grateful for any advice! Edit: I am not sure what is going wrong. I am quite unsure about stride and stuff, because vertices is a class with more than just an x, y and z. It's a full class with functions and these members: float x, y, z; float xn, yn, zn; (normal) float* u, *v; (pointers so I have texture coordinates for all the different textures (multitexturing) [Edited by - Subotron on September 22, 2006 12:51:48 PM]
Shadx
Shadx
Hiya,

You are trying to replace what you have with glVertexPointer? I'm not sure I understand when you say it doesn't support 2 dimensional arrays?

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2/*size*/, GL_BYTE,0, array);


if you need to pass more information, you can also do it with the other types of arrays (texture, normal for example).

You could also look up buffer objects if you can use it, Nehe has an example of it here: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45

I'm not sure that version 1.1 of the red book covered this topic unfortunately, but later version covers it quite well.

Good luck!

Shadx
Rasmadrak
Rasmadrak
I'd use the following structure instead:

struct mesh{vertex *Vertices;uvs *TexCoords;normal *Normals;};


I believe it's much simplier to have different sets of variables for different areas.. but that's just me! :)

And you can store a 2d array in a 1D...

Vertices[10][10] could be written like
Vertices[100];

Get a single vertex:
Vertices[ x + (y*10) ]


Cheers!
"Game Maker For Life, probably never professional thou." =)

Topic Locked

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

Sign in to reply to this topic.