RandomDever wrote:OK so I've looked a bit at VBOs and I have this pseudo-code:I don't exactly know what the third parameter of 'glDrawArrays' does, but besides that, is this code correct?Code: Select all
GLuint colorBuffer = NULL; GLuint positionBuffer = NULL; GLubyte colors[] = { 255, 255, 255, 128 }; glGenBuffers( 1, &colors ); glBindBuffer( GL_ARRAY_BUFFER, colorBuffer ); glBufferData( GL_ARRAY_BUFFER, 4 * sizeof(GLubyte) ); glColorPointer( 4, GL_UNSIGNED_BYTE, 0, 0 ); GLfloat positions[] = { -1.0F, -1.0F, 1.0F, -1.0F, 1.0F, 1.0F, -1.0F, 1.0F }; glGenBuffers( 1, &positions ); glBindBuffer( GL_ARRAY_BUFFER, positionBuffer ); glBufferData( GL_ARRAY_BUFFER, 8 * sizeof(GLfloat) ); glVertexPointer( 2, GL_FLOAT, 0, 0 ); glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_COLOR_ARRAY ); glDrawArrays( GL_QUADS, 0, 4 ); //Not entirely sure how this works glDisableClientState( GL_COLOR_ARRAY ); glDisableClientState( GL_VERTEX_ARRAY );
Also the example I drew from to make the above code draws a square with 2 triangles ( for some reason )
but it doesn't have a texture so how would I do that?
Code: Select all
struct Vertex {
GLfloat position[3]; // x, y, z
GLfloat texture[2]; // u, v
GLfloat color[4]; // r, g, b, a
GLubyte padding[28]; // pads structure to 64 bytes for efficiency
};
////////////
// Uploading
GLuint name;
glGenBuffers(1, &name);
glBindBuffer(GL_ARRAY_BUFFER, name);
Vertex vertices[4] = {
// x y z u v r g b a
{ {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}, {1.0f, 0.0f, 0.0f, 0.0f}, { 0 }},
{ {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, { 0 }},
{ {1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 0.0f}, { 0 }},
{ {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 0.0f, 1.0f, 0.0f}, { 0 }},
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), reinterpret_cast<GLvoid const*>(&vertices), GL_STATIC_DRAW);
//////////
// Drawing
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer (3, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLvoid const*>(offsetof(Vertex, position)));
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLvoid const*>(offsetof(Vertex, texture)));
glColorPointer (4, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLvoid const*>(offsetof(Vertex, color)));
glDrawArrays(GL_QUADS, 0, sizeof vertices / sizeof(Vertex));
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);