Sign in to follow this  
handsomerob

VBOs drawn incorrectly

Recommended Posts

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

Share this post


Link to post
Share on other sites
Looks fine, you may have to watch out for padding of your structure.
If you have

struct Myvertex
{
};

use sizeof(Myvertex)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

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