Jump to content
  • Advertisement
Sign in to follow this  
crazedfool

Vertex Array problem

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

I'm having some problems with rendering vertex arrays. I've searched here and looked at the docs and I don't see anything, but at this point I'm sure it's just some dumb misstype that I keep reading over. This renderers correctly:
        glBindTexture(GL_TEXTURE_2D, m_Vertices->GetVertices()[0].GetMaterialList()[m_Vertices->GetVertices()[0].GetMaterialID()].GetTexture());
	glBegin(GL_TRIANGLES);
	for(int x=0; x<m_Vertices->GetNumUsed(); x+=3)
	{
		glTexCoord2fv(&m_Vertices->GetVertices()[x].GetTexCoordinates()->x);
		glVertex3fv(&m_Vertices->GetVertices()[x].GetCoordinates()->x);
		glTexCoord2fv(&m_Vertices->GetVertices()[x+1].GetTexCoordinates()->x);
		glVertex3fv(&m_Vertices->GetVertices()[x+1].GetCoordinates()->x);
		glTexCoord2fv(&m_Vertices->GetVertices()[x+2].GetTexCoordinates()->x);
		glVertex3fv(&m_Vertices->GetVertices()[x+2].GetCoordinates()->x);
	}
	glEnd();

This doesn't render anything:
        glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), &m_Vertices->GetVertices()[0].GetCoordinates()->x );	// Set The Vertex Pointer To Our Vertex Data
	glTexCoordPointer( 2, GL_FLOAT, sizeof(Vertex), &m_Vertices->GetVertices()[0].GetTexCoordinates()->x );	// Set The Vertex Pointer To Our TexCoord Data

	glEnableClientState( GL_VERTEX_ARRAY );
	glEnableClientState( GL_TEXTURE_COORD_ARRAY );

        glBindTexture(GL_TEXTURE_2D, m_Vertices->GetVertices()[0].GetMaterialList()[m_Vertices->GetVertices()[0].GetMaterialID()].GetTexture());
		glDrawArrays( GL_TRIANGLES, 0, m_Vertices->GetNumUsed() );		// Draw All Of The Triangles with this texture
        glDisableClientState( GL_VERTEX_ARRAY );					// Disable Vertex Arrays
	glDisableClientState( GL_TEXTURE_COORD_ARRAY );				// Disable Texture Coord Arrays

Share this post


Link to post
Share on other sites
Advertisement
Enable the client state before setting the pointers and see if that works.

Else try this:

gl*Pointer( *, GL_FLOAT, 0, *);

instead of this:

gl*Pointer( *, GL_FLOAT, sizeof(Vertex), * );

Share this post


Link to post
Share on other sites
Setting the client states before calling VertexPointer has no effect. The stride value should be sizeof(Vertex) because the actual coordinates are within a Vertex structure(should have mentioned that before).

Share this post


Link to post
Share on other sites
Quote:
Original post by crazedfool
the actual coordinates are within a Vertex structure(should have mentioned that before).


that sounds like a likely cause for your problem. post the structure def for your Vertex struct.

incidentally here's a good post on flipcode for how to properly use interleaved arrays and whatnot:

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=00002281&forum=3dtheory&id=-1

i notice that the poster there uses calls to glInterleavedArrays and you don't. i've never used them before myself but it makes sense that your method wouldn't work b/c openGL would not know the format of your vertex array (i.e. 3 vertices then 2 tex coords).


-me

Share this post


Link to post
Share on other sites
God, I'm dumb. Yeah OpenGL probabely wouldn't know to dereference that pointer would it. It seems a lot of my problems end with me wondering how I could be so retarded. Oh well, thanks and rating++ for you both.

Share this post


Link to post
Share on other sites
yeah, so you can get the same effect without using glInterleavedArrays but it'd be ineligant with your Vertex struct. here's an example from the openGL red book on interleaved arrays without using the glInterleavedArrays(...) method


//assume this is filled with repeated 1 3D vertex coord then 2 texture coords
float * interleavedArray;

glVertexPointer(3, GL_FLOAT, 6*sizeof(float), interleavedArray);
glTexCoordPointer(2, GL_FLOAT, 6*sizeof(float), &interleavedArray[3]);



note that you have to tell the glTexCoordPointer to start a 3 floats in from the beginning of the array (the last argument of glTexCoordPointer). this is the part you're missing. so your tex coords are actually on the correct stride but openGL is using the x,y coords of your vertex instead of the u,v coords that come after the vertex.

i'd just use glInterleavedArrays as it's more elegant. the hack would be to cast your array to a float* then do the same as the above, but it's prettier the other way.

-me

Share this post


Link to post
Share on other sites

You might also want to consider your structure packing, if it is say 8 bytes and you have a size that is a multiple of four (floats) then you'll get 4 bytes of padding in there.

Example:

typedef struct
{
float crrd[3]; // 4 bytes of padding added here (12 -> 16)
float texc[2]; // no padding added here (8 bytes)
float color[3]; // another 4 bytes of padding added here
}vert_t;

Hope that this helps, ~SPH

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!