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

Need help with vertex arrays!

Started by cresty Oct 11, 2006 at 10:21 AM 0 replies 800+ views
Original Post
cresty
cresty
So i begun programming in opengl some week ago and its great. I got the book from the opengl.org site and its great. My problem is, vertex arrays. I do exactly as they say in the book, hell i even tried copying some code form there but still i get some weird results. For example i make a really simple triangle. When i draw it through a vertex array and finally display it i get some wird hexagonal or something. I think i know what should be in the parameters of both the vertex pointer and the drawarrays or whatever but im not 100% sure, so if someone could explain what data should be in the vertexpointer and drawarrays function paramters i would be grateful. Extra credit if you would explain it like a was a monkey, cause i dont know many technical terms in english. Thx in advance!
Subotron
Subotron
Well, first of all, it would be useful if you could provide some of your code, so we can see if you're doing anything wrong.

Vertex arrays are pretty simple to use, I'll try to make it as clear as I can:

One thing to do before using vertex arrays which I think is wise, is make a vertex class:

class CVertex{public:    float x, y, z;}


As simple as that. You might want to add constructors, operators, whatever, but start out with something simple, don't edit this class before you got vertex arrays to work!

Next, you need to make an array of instances of this class, like this:

CVertex vertices[NUMBER_OF_VERTICES];

You can now fill in this array. For example, if you want to draw a quad along the x and y axis (leaving z constant), you would do this:

vertices[0].x = 0.0f;vertices[0].y = 0.0f;vertices[0].z = 0.0f;vertices[1].x = 1.0f;vertices[1].y = 0.0f;vertices[1].z = 0.0f;vertices[2].x = 1.0f;vertices[2].y = 1.0f;vertices[2].z = 0.0f;vertices[3].x = 0.0f;vertices[3].y = 1.0f;vertices[3].z = 0.0f;


Now, you probably already know all of this, but since I'm not sure what your previous knowledge is, I'll just specify it as easy as possible. Note: there are much better ways to construct an array than I do above, but this is not about constructing an array, but using vertex arrays.

Moving on: The next thing you want to do now your vertex array is good to go, is draw it to the screen! Make sure your array works, by first drawing it without vertex array techniques. This way, you are sure the vertex array should show up correctly, so any problems will be in your specific code to draw the array.

glBegin(GL_QUADS);    for (int i = 0; i < NUMBER_OF_VERTICES; i++) // Loop through all the vertices        glVertex3f(vertices.x, vertices.y, vertices.z); // Draw the vertexglEnd();


If this works, you are ready to implement the vertex array rendering code (and of course, delete the code above, since you won't be needing it any more):

glEnableClientState(GL_VERTEX_ARRAY);

First, you enable the use of vertex arrays. You do this by calling the function glEnableClientState(). The parameter is an identifier for the kind of array you want to use. For vertices, you use GL_VERTEX_ARRAY. If you'd like to add specific colors for all the vertices, you would call the function a second time with the parameter GL_COLOR_ARRAY. There are several more possible parameters, but I'll assume vertices, since you want to draw those before you do anything else. All the other options work pretty much the same.
glVertexPointer(3, GL_FLOAT, 0, vertices);

Next, you specify WHICH vertex array you are going to draw. This is the one you set up, in this example it's called 'vertices'. The function to call is glVertexPointer(). The first parameter of this function is an integer value, specifying the 'size' of a vertex. In 3D programming, this is 3 (for x, y and z). In 2D programming, it would be 2 (for x and y). Next is the type of the x, y (and z) coordinates. This example uses floats, so I specify GL_FLOAT. Other options are for example GL_INT and GL_DOUBLE (I think, I never looked it up though). The third parameter is 'stride'. In this case, our vertex class only consists of the x, y and z values. They are all used in the vertex array. But suppose we would've also added a color. Colors are made up of 3 or 4 (fourth for the alpha channel) floats. This would mean, the vertex array rendering routine would have to jump OVER those 4 floats when drawing the vertex array, else colors would show up as points in 3D space! (which is actually pretty funny).

This is what stride is for, and why I told you not to edit the vertex class yet. If you add more variables, you need to increase the stride to compensate for this.

The last parameter points to your vertex array, in this case 'vertices'.
glDrawArrays(GL_QUADS, 0, NUMBER_OF_VERTICES);

Now we've specified what array we want to draw, we draw it with the glDrawArrays() function. There are other possible functions, to draw only certain vertices out of the array, but this is probably what you want. The first parameter is the same as the parameter in glBegin(). It specifies the type of polygons you want to draw. We want a quad, so we specify GL_QUADS. The second parameter is the first vertex in the array we would like to draw. Usually, this is 0. If you want to skip an amount of vertices for some reason, set this value to the first vertex you do want to draw.

The last parameter is the amount of vertices you want to draw. If the second parameter is 0, you want to set this parameter to the length of the vertex array (in other words, the amount of vertices in the array). If you set the second parameter to be higher than 0, remember to decrease the last parameter by that number, or else you'll end up outside the array (causing errors).

glDisableClientState(GL_VERTEX_ARRAY);

The last thing we do is disable the vertex array client state again, since we enabled it at the beginning.

Well, that's a really extensive explanation I think. If you have any questions left, please ask :)

Topic Locked

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

Sign in to reply to this topic.