Sign in to follow this  

Indexing troubles

This topic is 4343 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

For my batch 2D drawing function, I buffer the sprite's vertices into an array of vertices kept in the rendering class, which is binded to a vertex buffer upon either being filled, when a change of texture occurs, or when 2D batching has finished. My problem occurs when flushing the buffer on a change of texture. On some of the latter sprites, previous sprites stretched to the size of the current rendered sprite are drawn in conjunction with the sprite. After messing around for a while I discovered it has to do with my indexing, because when I render it without indexing it works fine. This bug has had me scratching my head for the past 5 hours. Anyone care to help me? Relevant source code:
VOID RX_GRAPHICS::BufferSpriteGL(RX_SPRITE* Sprite)
{
	if(currentTexId == -1)
		currentTexId = Sprite->CurrentFrame()->TexId;
	if(currentTexId != Sprite->CurrentFrame()->TexId && numBatchVerts > 0  )
	{
		UnloadSpriteBufferGL();
		currentTexId = Sprite->CurrentFrame()->TexId;
	}

	for(int i = 0; i < 4; i++)
	{
		D3DXCOLOR vertColour = Sprite->Verts[i].Col;
		p_texVert[numBatchVerts + i].U = Sprite->Verts[i].U;
		p_texVert[numBatchVerts + i].V = Sprite->Verts[i].V;
		p_posVert[numBatchVerts + i].X = Sprite->Verts[i].PosX;
		p_posVert[numBatchVerts + i].Y = Sprite->Verts[i].PosY;
		p_posVert[numBatchVerts + i].Z = Sprite->Verts[i].PosZ;		
		p_colVert[numBatchVerts + i].R = vertColour.r;
		p_colVert[numBatchVerts + i].G = vertColour.g;
		p_colVert[numBatchVerts + i].B = vertColour.b;
		p_colVert[numBatchVerts + i].A = vertColour.a;
		p_normVert[numBatchVerts + i].X = Sprite->Verts[i].NormX;
		p_normVert[numBatchVerts + i].Y = Sprite->Verts[i].NormY;
		p_normVert[numBatchVerts + i].Z = Sprite->Verts[i].NormZ;
	}
	numBatchVerts += 4;
	if(numBatchVerts == BUFFERSIZE )
	{
		UnloadSpriteBufferGL();
	}
}

VOID RX_GRAPHICS::UnloadSpriteBufferGL()
{
	
	glBindTexture(GL_TEXTURE_2D,textures[currentTexId].GLImageData.texID);
	if(vBuffers[VBUFFER_SPRITES].VBOVertex != -1)
	{
		glBindBuffer( GL_ARRAY_BUFFER_ARB, vBuffers[VBUFFER_SPRITES].VBOVertex);
		glBufferDataARB(GL_ARRAY_BUFFER_ARB, numBatchVerts*3*sizeof(float), p_posVert, GL_DYNAMIC_DRAW_ARB);
		glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );

		glBindBuffer( GL_ARRAY_BUFFER_ARB, vBuffers[VBUFFER_SPRITES].VBOTexCoords);
		glBufferDataARB(GL_ARRAY_BUFFER_ARB, numBatchVerts*2*sizeof(float), p_texVert, GL_DYNAMIC_DRAW_ARB);
		glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL);	

		glBindBuffer( GL_ARRAY_BUFFER_ARB, vBuffers[VBUFFER_SPRITES].VBOColour);
		glBufferDataARB(GL_ARRAY_BUFFER_ARB, numBatchVerts*4*sizeof(float), p_colVert, GL_DYNAMIC_DRAW_ARB);
		glColorPointer( 4, GL_FLOAT, 0, (char *) NULL);	

		glBindBuffer( GL_ARRAY_BUFFER_ARB, vBuffers[VBUFFER_SPRITES].VBONormal);
		glBufferDataARB(GL_ARRAY_BUFFER_ARB, numBatchVerts*3*sizeof(float), p_normVert, GL_DYNAMIC_DRAW_ARB);
		glNormalPointer(GL_FLOAT, 0, (char *) NULL);	

		glBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB,vBuffers[VBUFFER_SPRITES].VBOIndex);
		glDrawElements(GL_TRIANGLES,numBatchVerts*3,GL_UNSIGNED_SHORT,0);

	}
	else
	{
		glVertexPointer( 3, GL_FLOAT, 0, p_posVert);
		glTexCoordPointer( 2, GL_FLOAT, 0, p_texVert);	
		glNormalPointer(GL_FLOAT, 0, p_normVert);
		glColorPointer(4,GL_FLOAT,0, p_colVert);
		glIndexPointer(GL_TRIANGLES,0,p_indices);

		glDrawElements(GL_TRIANGLES, numBatchVerts*3, GL_UNSIGNED_SHORT,0);
	}


	numBatchVerts = 0;
}



Share this post


Link to post
Share on other sites
im not 100% sure whats going on
but dealing with VAs its always good to disable all VAs beforehand
with VBOs use
glBindBuffer( GL_ARRAY_BUFFER_ARB, 0 ); for each type
glBindBuffer( GL_ELEMENT_BUFFER_ARB, 0 );

also the following line is prolly not good, indexpointer is used for colorindexs (not used much this century)
glIndexPointer(GL_TRIANGLES,0,p_indices);

(in the else statement section) this is a problem here instead of 0 u want to (point) to the indices array. check out the opengl spec for info on how standard VAs work (also info on VBOs)
glDrawElements(GL_TRIANGLES, numBatchVerts*3, GL_UNSIGNED_SHORT,0);

GL_ELEMENT_ARRAY_BUFFER == indices
GL_ARRAY_BUFFER_ARB == other stuff eg verts/colors/norms etc

Share this post


Link to post
Share on other sites
Thanks for the help. Im not really used to OpenGL, so ive been having some troubles converting my DirectX code to OpenGL.
Im still having problems with it though. It seems that the corruption only occurs on the sprites rendered on the last texture swap.

Share this post


Link to post
Share on other sites

This topic is 4343 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