Sign in to follow this  

[solved]VBO - texture problems

This topic is 2811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My head hurts so I believe I am entitled to start a thread. :) I am moving from Display List to check out the Vertex Buffer Object. Although I got a working quad up, there is still some problems occuring when trying to apply either color arrays or texture coord arrays. I am sure I have missed some concepts but I have checked out two different books on the subjects aswell as some threads. There seems to be very little room for errors. --- The following function creates a vertex array and a index array (as you can see there is also two more arrays for texture coords and color). The vertex array is just one quad. The function gets called once after the initialization before the game loop. "BUILD"
	//Create a QUAD array
	GLfloat vertexCoord[]	= {	0.0f,	00.0f,
					00.0f,	50.0f,
					50.0f,	50.0f,
					50.0f,	0.0f	};
	//Create a index array ( Can it only be a ubyte? )
	GLubyte indexCoord[] = { 0, 1, 2, 3 };

	//Create a color array ( Does this create a color for each point in the quad? )
	GLfloat colorCoord[]	= {	1.0f, 1.0f, 1.0f,
					1.0f, 1.0f, 0.0f,
					1.0f, 0.0f, 0.0f,
					1.0f, 0.0f, 1.0f	};
	//Create a texture array ( Does this work the same as when doing it between glBegin() and glEnd()? )
	GLfloat textureCoord[]	= {	0.0f,	1.0f,
					0.0f,	0.0f,
					1.0f,	0.0f,	
					1.0f,	1.0f	};
	//Generate buffer object
	glGenBuffersARB( BUFFER_OBJECT_SIZE, myVboFont );

	/*=================================
		COPY DATA TO VIDEO MEMORY
	=================================*/
	//VERTEX ARRAY
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ VERTEX_DATA ] );
	glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof( vertexCoord ), vertexCoord, GL_STATIC_DRAW_ARB );
	//COLOR ARRAY
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ TEXTURE_DATA ] );
	glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof( textureCoord ), textureCoord, GL_STATIC_DRAW_ARB );
	//INDEX ARRAY
	glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, myVboFont[ INDEX_DATA ] );
	glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof( indexCoord ), indexCoord, GL_STATIC_DRAW_ARB );



"RENDER"
	//Manipulate matrix model view stack
	glMatrixMode( GL_MODELVIEW );
	//Save current matrix
	glPushMatrix();
	//Change position
	glTranslatef( 50.0f, 50.0f, 0.0f );
	//Enable ClientStates
	glEnableClientState( GL_VERTEX_ARRAY );
	//Activate arrays
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ VERTEX_DATA ] );
	glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, myVboFont[ INDEX_DATA ] );
	//Assign array
	glVertexPointer( 2, GL_FLOAT, 0, 0 );
	//Draw
	glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, 0 );
	//Clear ClientState
	glDisableClientState( GL_VERTEX_ARRAY );
	//Clear current matrix
	glPopMatrix();



This works, I get a white quad to be drawn - btw I am using gluOrtho2D - and everything seems to be working as intended. Though some questions araise since the books did not mention it, why do I not need to enableClientState( GL_INDEX_ARRAY ) or use glIndexPointer()? This would obviously not work if the equivalent vertex array were removed. Now I want to use the texture coords and apply a texture on the quad. "RENDER WITH TEXTURE"
	//Manipulate matrix model view stack
	glMatrixMode( GL_MODELVIEW );
	//Save current matrix
	glPushMatrix();
	//Change position
	glTranslatef( 50.0f, 50.0f, 0.0f );
	//Enable ClientStates
	glEnableClientState( GL_VERTEX_ARRAY );
	glEnableClientState( GL_TEXTURE_COORD_ARRAY );
	//Activate arrays
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ VERTEX_DATA ] );
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ TEXTURE_DATA ] );
	glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, myVboFont[ INDEX_DATA ] );
	//Activate texture
	glBindTexture( GL_TEXTURE_2D, myTextureFontBucket[ FONT_SINISTAR ] );
	//Assign array
	glVertexPointer( 2, GL_FLOAT, 0, 0 );
	glTexCoordPointer( 2, GL_FLOAT, 0, 0 );
	glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, 0 );
	//Clear ClientState
	glDisableClientState( GL_VERTEX_ARRAY );
	glDisableClientState( GL_TEXTURE_COORD_ARRAY );
	//Clear current matrix
	glPopMatrix();



All I get now is just one dot. Same thing happens if I try something similar with the color array. Whats wrong? I can't see anything different when looking through similar codes. [Edited by - energydrink on April 1, 2010 9:43:09 AM]

Share this post


Link to post
Share on other sites
You're close, but you've just got something out of order:

glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ VERTEX_DATA ] );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ TEXTURE_DATA ] );
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, myVboFont[ INDEX_DATA ] );
//Activate texture
glBindTexture( GL_TEXTURE_2D, myTextureFontBucket[ FONT_SINISTAR ] );
//Assign array
glVertexPointer( 2, GL_FLOAT, 0, 0 );
glTexCoordPointer( 2, GL_FLOAT, 0, 0 );
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, 0 );


This section is your problem. When you call a gl<something>Pointer, it generates a pointer into the array that is currently bound into the GL_ARRAY_BUFFER. In your code both your vertexpointer and your texcoord pointer are pointing into your texture buffer, because that is bound when both pointer functions are called. You need to reorganize this to:


glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ VERTEX_DATA ] );
glVertexPointer( 2, GL_FLOAT, 0, 0 );

glBindBufferARB( GL_ARRAY_BUFFER_ARB, myVboFont[ TEXTURE_DATA ] );
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, myVboFont[ INDEX_DATA ] );
//Activate texture
glBindTexture( GL_TEXTURE_2D, myTextureFontBucket[ FONT_SINISTAR ] );
//Assign array

glTexCoordPointer( 2, GL_FLOAT, 0, 0 );
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, 0 );



The index buffer doesn't interfere because it is bound to a different target, the GL_ELEMENT_ARRAY_BUFFER.




Also, a few answers to your questions:
//Create a index array ( Can it only be a ubyte? )
- Can be ubyte, ushort, or uint (needs to match the arg in glDrawElements)


//Create a color array ( Does this create a color for each point in the quad? )
- Each vertex in your vertex array needs a color in your color array. Not a color for each point, but a color for each vertex.


why do I not need to enableClientState( GL_INDEX_ARRAY ) or use glIndexPointer()?
These refer to something totally different, although your confusion is warranted. These refer to "color indexing", and have absolutely nothing to do with your index array. I would imagine this is rarely used, if ever.

Share this post


Link to post
Share on other sites

This topic is 2811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this