VBO

Started by
3 comments, last by Lord_Evil 14 years, 9 months ago
Can you help me with this VBO, I can't figure it out correctly : The cpyArray is declared like so : cpyArray[ROW][COL][3]; where row = 32, col =32.


        glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glVertexPointer(3,GL_FLOAT,0,cpyArray);
	glDrawArrays(GL_QUADS,0,ROW*COL*6*3);
	
        glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);


Our whole life is a opengl application.
Advertisement
I assume the array resembles a grid.

1. You don't specify texture coordinates, so you shouldn't enable GL_TEXTURE_COORD_ARRAY
2. Your array only has row*col vertices, so the last parameter of glDrawArrays should be ROW * COL (otherwise you'd read outside the VBO)
3. 4 consecutive vertices are used for one quad, so unless you specify most of the vertices multiple times (up to 4x) your grid will have gaps. I assume you want to use indices instead, i.e. glDrawElements(...)
4. Keep in mind that a quad must be planar, i.e. all vertices have to be in the same plane (e.g. same y-coord). If you want different heights for the vertices use 2 triangles instead.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Looking at your code it seems like you are using just vertex array, not vertex buffer objects (VBO). Here is a tutorial that demonstrates the use of VBOs:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45
(Note that in the article they somehow forgot the glEnableClientState calls, although they can be found in the code samples)

To use VBOs you have to call glBindBuffer with GL_ARRAY_BUFFER. Make sure GL_ARRAY_BUFFER is bound before calling glVertexPointer. When using VBOs as opposed to vertex arrays, the last parameter of glVertexPointer is relative to the beginning of the buffer object (so 0 in most cases). The buffer is called by calling glBufferData, which copies the vertex data to the GPU memory.
Sorry, I have the wrong terminology. This is just a test to get vertex arrays
working with rendering terrain.

I draw the quads like this
		/*glBegin(GL_QUADS);			for( i = 0; i < ROW-1-stepRow; i+=stepRow)			{				float x0 = (float)i/ROW;				float x1 = (float)(i+1)/ROW;				for(j = 0; j < COL-1-stepCol; j+=stepCol)				{					float y0 = (float)j/COL;					float y1 = (float)(j+1)/COL;					//glColor3ub(Array[j][0],Array[j][1],Array[j][2]);					//counter clockwise					glTexCoord2f(y0,x0);glVertex3f(Array[j][0],Array[j][1],Array[j][2]);					glTexCoord2f(y0,x1);glVertex3f(Array[i+1][j][0],Array[i+1][j][1],Array[i+1][j][2]);					glTexCoord2f(y1,x1);glVertex3f(Array[i+1][j+1][0],Array[i+1][j+1][1],Array[i+1][j+1][2]);					glTexCoord2f(y1,x0);glVertex3f(Array[j+1][0],Array[j+1][1],Array[j+1][2]);				}			}		glEnd();*/


But am not sure how to convert this array so either glDrawElements or
glDrawArray accepts it correctly.
Our whole life is a opengl application.
To use it with glDrawElements you have to create a index buffer.

The indices can be calculated like this:

for(int c = 0; c < (COLS - 1); x++){  for(int r = 0; r < (ROWS - 1); r++)  {    //indices: 1st triangle of quad    int i1t1 = r * COLS + c;    int i2t1 = (r + 1) * COLS + c;    int i3t1 = r * COLS + c + 1        //indices: 2nd triangle of quad    int i1t2 = (r + 1) * COLS + c;    int i2t2 = (r + 1) * COLS + c + 1;    int i3t2 = r * COLS + c + 1  }}


And you should reread the documentation for glDrawArrays and glDrawElements. You have to specify the number of indices or vertices, not the number of floats. And any number you specify has to be less than or equal to the number of indices/vertices you use.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement