Vertex, Color, Texture in one VBO

Started by
1 comment, last by DaysShadow 12 years, 8 months ago
Hi there,

i'm trying to get into OpenGL with C++ but sometimes it just knocks me out :mellow:

I was searching also some time but could not find any help, maybe i just did not find the right thing.

So:

I've first tried to get something on the screen with vbos in general, so i made a vbo for vertices, colors and texcoords. Worked, i had my textured quad( made of tri-strips :P ).

Then i tried to put all the data into one vbo.

I wrote a vertex struct, reserved space for 4 of them in the vbo and then went on as before.

I passed the size of the vertex struct as stride and the offset inside the struct for each gl*Pointer call.
The problem i got is simple: There is nothing to see, no quad, no colors, no texture, just the blank background.


I hope i give you enough information, if not just tell me what you need.

Thanks to everyone.



So, see the codes yourself:

First try with 3 vbos:

GLfloat vertices[] = { 200.f, 228.f, 0.f,
200.f, 100.f, 0.f,
456.f, 228.f, 0.f,
456.f, 100.f, 0.f };

GLubyte colors[] = { 255, 255, 255,
255, 255, 255,
255, 255, 255,
255, 255, 255 };

GLfloat texCoords[] = { 0.f, 1.f,
0.f, 0.f,
1.f, 1.f,
1.f, 0.f };

GLuint vertId = -1;
GLuint colId = -1;
GLuint texId = -1;

glGenBuffers( 1, &vertId );
glGenBuffers( 1, &colId );
glGenBuffers( 1, &texId );

// Vertices
glBindBuffer( GL_ARRAY_BUFFER, vertId );
glBufferData( GL_ARRAY_BUFFER, 4 * 3 * sizeof( GLfloat ), vertices, GL_STATIC_DRAW );

// Colors
glBindBuffer( GL_ARRAY_BUFFER, colId );
glBufferData( GL_ARRAY_BUFFER, 4 * 3 * sizeof( GLubyte ), colors, GL_STATIC_DRAW );

// Texture
glBindBuffer( GL_ARRAY_BUFFER, texId );
glBufferData( GL_ARRAY_BUFFER, 4 * 2 * sizeof( GLfloat ), texCoords, GL_STATIC_DRAW );

glBindBuffer( GL_ARRAY_BUFFER, 0 );

glEnable( GL_TEXTURE_2D );

// Drawing in render call

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );

glBindBuffer( GL_ARRAY_BUFFER, vertId );
glVertexPointer( 3, GL_FLOAT, 0, 0 );

glBindBuffer( GL_ARRAY_BUFFER, colId );
glColorPointer( 3, GL_UNSIGNED_BYTE, 0, 0 );

glBindBuffer( GL_ARRAY_BUFFER, texId );
glTexCoordPointer( 2, GL_FLOAT, 0, 0 );

glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );



Second try with vertex struct and one vbo:



#define BUFFER_OFFSET(i) ((char *)NULL + (i)) // taken from the opengl wiki

typedef struct _DSE_Vertex
{
float x,y,z;
float r,g,b,a;
float u,v;
} Vertex;

Vertex quad[4];

// setVertexValues( Vertex& vert, x, y, z, r, g, b, a, u, v ) // all float

setVertexValues( quad[0], 200.f, 228.f, 1.f, 1.f, 1.f, 1.f, 1.f,
0.f, 1.f );

setVertexValues( quad[1], 200.f, 100.f, 1.f, 1.f, 1.f, 1.f, 1.f,
0.f, 0.f );

setVertexValues( quad[2], 456.f, 228.f, 1.f, 1.f, 1.f, 1.f, 1.f,
1.f, 1.f );

setVertexValues( quad[3], 456.f, 100.f, 1.f, 1.f, 1.f, 1.f, 1.f,
1.f, 0.f );


GLuint vbo = -1;

glGenBuffers( 1, &vbo );

glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( Vertex ) * 4, quad, GL_STATIC_DRAW );

uint stride = sizeof( Vertex ); // It's 36 bytes

glEnable( GL_TEXTURE_2D );

// Drawing in render call

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );

glBindBuffer( GL_ARRAY_BUFFER, vbo );
glVertexPointer( 3, GL_FLOAT, stride, 0 );
glColorPointer( 4, GL_FLOAT, stride, BUFFER_OFFSET( 12 ) );
glTexCoordPointer( 2, GL_FLOAT, stride, BUFFER_OFFSET( 28 ) );

glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
Advertisement
Most peculiar indeed. Could it be because Z coordinate of your vertices is different in second snippet?
WBW, capricorn
Great god how i hate this, it is the z-coordinate.
I can't see things behind my camera, of course. I have a orthogonal view and depth test enabled, so -1.f would have been working but +1.f lies behind my cam so i cant see it...

Damned carelessness.

Spent 2 days for sure on this with reading and searching in the internet...

Thanks a lot for freeing me from this pain!

This topic is closed to new replies.

Advertisement