Sign in to follow this  
McGrane

Texture problems with vertex arrays - OpenGL

Recommended Posts

Hey people,

Im having trouble getting my textures to display right. 

 

5jf4JEa.png

 

The square in the top left is the image i am using, and as you can see, the images are coming out wrong.

The output of my program as far as i can see is in the correct format..

	Index: 1	tX: 0	tY: 0
	Index: 2	tX: 1	tY: 0
	Index: 11	tX: 1	tY: 1
	Index: 1	tX: 0	tY: 0
	Index: 11	tX: 1	tY: 1
	Index: 10	tX: 0	tY: 1
	Index: 3	tX: 0	tY: 0
	Index: 4	tX: 1	tY: 0
	Index: 13	tX: 1	tY: 1
	Index: 3	tX: 0	tY: 0
	Index: 13	tX: 1	tY: 1
	Index: 12	tX: 0	tY: 1
	Index: 5	tX: 0	tY: 0
	Index: 6	tX: 1	tY: 0
	Index: 15	tX: 1	tY: 1
	Index: 5	tX: 0	tY: 0
	Index: 15	tX: 1	tY: 1
	Index: 14	tX: 0	tY: 1
	Index: 7	tX: 0	tY: 0
	Index: 8	tX: 1	tY: 0
	Index: 17	tX: 1	tY: 1

 

This is my code for displaying the images.

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );

glVertexPointer( 3, GL_FLOAT, 0, m_vertices );
glTexCoordPointer( 2, GL_FLOAT, 0, texCoords );

glBindTexture(GL_TEXTURE_2D, m_textureOne );
glDrawElements( GL_TRIANGLES, m_ammountPoints, GL_UNSIGNED_INT, m_squareLight );
	
glDisableClientState( GL_VERTEX_ARRAY );	
glDisableClientState( GL_TEXTURE_COORD_ARRAY );

 

Any ideas what could be going wrong ? - as far as i can tell, the indices seem to line up with the texcoords.. But everysqure seems to be different ?

Share this post


Link to post
Share on other sites

This is certainly not my area of expertise, but if I had to hazard a guess, I'd say that the problem lies in the vertex/texcoord ordering. If you get those things goofed up somehow it tends to make it look strange, like your example.

 

What have you got in those two arrays (m_vertices, texCoords)?

Share this post


Link to post
Share on other sites

Does OpenGL do tex coords by the order of the vertex or the order of the indices? The m_vertices is just the x and z coords of the board..

int counter = 0;
for( float z = 0.0f; z < 90.0f; z+= 10 ) {
    for( float x = 0.0f; x < 90.0f; x+= 10 ) {
	m_vertices[counter]   =    x;	
	m_vertices[counter+1] = 0.0f;	
	m_vertices[counter+2] =   -z;
	counter+=3;
    }	
}

 

and the tex coords are worked out as..

	for( int i = 0; i < m_ammountPoints; i += 12 ) {
		m_texCoords[i]   = 0.0f; m_texCoords[i+1] = 0.0f; 
		m_texCoords[i+2] = 1.0f; m_texCoords[i+3] = 0.0f;
		m_texCoords[i+4] = 1.0f; m_texCoords[i+5] = 1.0f;

		m_texCoords[i+6]  = 0.0f; m_texCoords[i+7]  = 0.0f;
		m_texCoords[i+8]  = 1.0f; m_texCoords[i+9]  = 1.0f;
		m_texCoords[i+10] = 0.0f; m_texCoords[i+11] = 1.0f;
	}

 

I understand that the order messes this up.. but from the output i was reading above - where index is the index of the point drawn, and tX being the texture X and ty being the texture Y, and it being drawn in the order..

----3

  /

1...2

then

3...2

  /

1...

I think the ordering makes sence, unless i miss understand what way its ordered :S

Edited by McGrane

Share this post


Link to post
Share on other sites

I could be mistaken but I think Vertex and TexCoords are linked together such that index n will reference the vertex at n and the texCoord at n.

 

However you seem to be trying to reference the vertex at n but reference the texCoord at count.

Share this post


Link to post
Share on other sites

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