VBOs drawn incorrectly

Started by
4 comments, last by kRogue 16 years, 9 months ago
Mesh::Mesh()
{
   ... //some initializations
   m_pVertices[0].x = 0.0f;
   m_pVertices[0].y = 0.0f;
   m_pVertices[0].z = -2.0f;
   m_pTexCoords[0].u = 0.0f;
   m_pTexCoords[0].v = 0.0f;

   m_pVertices[1].x = 2.0f;
   m_pVertices[1].y = 0.0f;
   m_pVertices[1].z = -2.0f;
   m_pTexCoords[1].u = 1.0f;
   m_pTexCoords[1].v = 0.0f;

   m_pVertices[2].x =  2.0f;
   m_pVertices[2].y =  2.0f;
   m_pVertices[2].z =  -2.0f;
   m_pTexCoords[2].u = 1.0f;
   m_pTexCoords[2].v = 1.0f;
}

void Mesh::BuildVBOs()
{
   glGenBuffersARB( 1, &m_nVBOVertices );                     
   glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices );         
   glBufferDataARB( GL_ARRAY_BUFFER_ARB, m_nVertexCount*3*sizeof(float), m_pVertices, GL_STATIC_DRAW_ARB );

   glGenBuffersARB( 1, &m_nVBOTexCoords );                     
   glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords );
   glBufferDataARB( GL_ARRAY_BUFFER_ARB, m_nVertexCount*2*sizeof(float), m_pTexCoords, GL_STATIC_DRAW_ARB );
}

void Mesh::draw()
{
   if (isSet == false)
   {
      BuildVBOs();
      isSet = true;
   }

   glEnableClientState( GL_VERTEX_ARRAY );
   glEnableClientState( GL_TEXTURE_COORD_ARRAY );

   glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices );
   glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );
   glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords );
   glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL );

   glBindTexture(GL_TEXTURE_2D, getTexture("data/pictures/grass1.png"));
   glDrawArrays( GL_TRIANGLES, 0, m_nVertexCount );

   glDisableClientState( GL_VERTEX_ARRAY );
   glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}
k, whats wrong? First the texture is drawn wrong. Then the Triangle locks like this \| Also, the points are left/up, right/up and right/down. At left/up the Triangles comes to the front. A picture: http://www.student.tugraz.at/painsi/wrong.jpg I hope, somebody can find the fault
Advertisement
Looks fine, you may have to watch out for padding of your structure.
If you have

struct Myvertex
{
};

use sizeof(Myvertex)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
thx, but always the same problem
I use classes, not structs, but thats not important ;)

What I can't understand, is, how Opengl knows, what are the x, y and z coordinates. Do I have to tell it that?

I only can think, that's the problem...


PS: Thx, you solved the problem ;) I used struct instead of a Class now and it works perfectly. Perhaps i can fix it to work with classes too
This call
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );

should be

glVertexPointer( 3, GL_FLOAT, sizeof(Myvertex), (char *) NULL );

whether you use struct or class.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
hey

You also don't seem to unbind your VBO's after use, which I think could cause such problems. You can unbind your Vertex Buffer alike... glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);.

cyas
one thing to be aware of when yo use classes, if there are any virtual functions, then thre is a hidden v-table pointer, thus:

class tickleme{public: int x; tickleme(void); virtual ~tickleme();};


then sizeof(tickle) will be 8 bytes on 32-bit (and probably 16 or 12 bytes on 64bit).
Not to mention, you do not know where the v-table pointer is packed within the class tickleme.

where as
class tickleme_again{public: int x; tickleme_again(void);};will have size equal to sizeof(int).
Close this Gamedev account, I have outgrown Gamedev.

This topic is closed to new replies.

Advertisement