Help with my scene drawing code, please?

Started by
3 comments, last by Ahl 12 years, 11 months ago
I don't know if I'm doing this right. I've ironed out all of the compiler errors. There are no run time errors. I'm not entirely sure everything is being loaded into the VBOs properly but I wanted to make sure all my basic render code is good first.


bool AhlywoGraphix::DrawGLScene() // Here's Where We Do All The Drawing
{
RenderList.sort();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Matrix
glColor3f(1.0, 1.0, 1.0);

glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBO_ID ); // <== WTF to do with that?

glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
glEnableClientState( GL_NORMAL_ARRAY ); // Enable Normal Arrays
glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays

for (RenderIt = RenderList.begin(); RenderIt != RenderList.end(); ++RenderIt)
{
glTranslatef((*RenderIt)->x, (*RenderIt)->y, (*RenderIt)->z);

glRotatef((*RenderIt)->xrot, 1.0f, 0.0f, 0.0f);
glRotatef((*RenderIt)->yrot, 0.0f, 1.0f, 0.0f);
glRotatef((*RenderIt)->zrot, 0.0f, 0.0f, 1.0f);

glVertexPointer( 3, GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBOVertexOffset ); // Set The Vertex Pointer To The Vertex Buffer
glTexCoordPointer( 2, GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBOTexCoordOffset ); // Set The TexCoord Pointer To The TexCoord Buffer
glNormalPointer(GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBONormalOffset ); // Set the Normal Pointer to the texcoord Buffer

glDrawArrays( GL_TRIANGLES, 0, (*RenderIt)->Model->VertexCount ); // Draw All Of The Triangles At Once
}
glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
glDisableClientState( GL_NORMAL_ARRAY ); // Disable Normal Arrays
glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
return true; // Keep Going
}
Advertisement

I don't know if I'm doing this right. I've ironed out all of the compiler errors. There are no run time errors. I'm not entirely sure everything is being loaded into the VBOs properly but I wanted to make sure all my basic render code is good first.


bool AhlywoGraphix::DrawGLScene() // Here's Where We Do All The Drawing
{
RenderList.sort();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Matrix
glColor3f(1.0, 1.0, 1.0);

glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBO_ID ); // <== WTF to do with that?

glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
glEnableClientState( GL_NORMAL_ARRAY ); // Enable Normal Arrays
glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays

for (RenderIt = RenderList.begin(); RenderIt != RenderList.end(); ++RenderIt)
{
glTranslatef((*RenderIt)->x, (*RenderIt)->y, (*RenderIt)->z);

glRotatef((*RenderIt)->xrot, 1.0f, 0.0f, 0.0f);
glRotatef((*RenderIt)->yrot, 0.0f, 1.0f, 0.0f);
glRotatef((*RenderIt)->zrot, 0.0f, 0.0f, 1.0f);

glVertexPointer( 3, GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBOVertexOffset ); // Set The Vertex Pointer To The Vertex Buffer
glTexCoordPointer( 2, GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBOTexCoordOffset ); // Set The TexCoord Pointer To The TexCoord Buffer
glNormalPointer(GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBONormalOffset ); // Set the Normal Pointer to the texcoord Buffer

glDrawArrays( GL_TRIANGLES, 0, (*RenderIt)->Model->VertexCount ); // Draw All Of The Triangles At Once
}
glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
glDisableClientState( GL_NORMAL_ARRAY ); // Disable Normal Arrays
glDisableClientState( GL_TEXTURE_COORD_ARRAY ); // Disable Texture Coord Arrays
return true; // Keep Going
}



I read that you should set the vertex pointer as last (just before glDrawArrays). For the sake of readability I would dereference the iterator once at the start of the loop and then use it in the rest of the loop body.
After that you could also move the drawing code to the renderable (the elements in the RenderList).

Crafter 2D: the open source 2D game framework

?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]

1. The routine expects that the MODELVIEW matrix stack is active. (Its not a fault, I just wanted to mention it for clarity.)

2. Regarding your "WTF" line: The VBO_ID addresses the VBO you want the vertex attribute arrays to belong to. The VBO_ID must formerly be successfully generated by invoking glGenBuffers(...) or glGenBuffersARB(...), resp.

3. The glTranslatef(...) and glRotatef(...) invocations for all the RenderIt objects are concatenated. I.e. the position of the 2nd object depends on the position and orientation of the 1st object. You probably want to use some glPushMarix() / glPopMatrix() wrapping each set of transformations.

4. The used combination of glRotatef(...)'s is legal but uncommon.

5. Invoke glVertexPointer(...) after the other gl???Pointer routines.

6. Using the matrix stack is deprecated since OpenGL version 3.1 (IIRC).

ATM I don't see other issues.



you have

glVertexPointer( 3, GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBOVertexOffset ); // Set The Vertex Pointer To The Vertex Buffer
glTexCoordPointer( 2, GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBOTexCoordOffset ); // Set The TexCoord Pointer To The TexCoord Buffer
glNormalPointer(GL_FLOAT, 0, (GLvoid *)(*RenderIt)->Model->VBONormalOffset ); //








omg i do it like: so for vertex pointer i have an dynamic array of structure called t3dpoint where i have floats x,y,z

and the last param of func is the first index of array




glVertexPointer(3, GL_FLOAT, sizeof(t3dpoint), &VBO_V[0]);
glNormalPointer(GL_FLOAT, sizeof(t3dpoint),&VBO_N[0]);
glTexCoordPointer(2, GL_FLOAT, sizeof(textpoint),&VBO_T[0]);




this should fix the problem and one thing remove bindbufferarb :P




Alright I got it to work. It was a mixture of the issues you all pointed out as well as something being wrong with my VBO loading code. I can load one model in and it works fine but if I try to load another one neither model works. I'll get to that in a second.

What is the general policy on using glLoadIdentity() between every object you render? Ok? Bad idea? Better to use glPushMarix() / glPopMatrix()?

As for my VBO code...


bool AhlywoGraphix::BuildVBO(ModelData* Model)
{
glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBO_ID );
GLsizei VBOsize = Model->VertexCount*3*sizeof(float) + Model->NormalCount*3*sizeof(float) + Model->TexCoordCount*2*sizeof(float);
glBufferDataARB( GL_ARRAY_BUFFER_ARB, VBOsize, NULL, GL_STATIC_DRAW_ARB );
if (Model->VertexCount > 0.0)
{
glBufferSubDataARB( GL_ARRAY_BUFFER_ARB, VBO_Offset, Model->VertexCount*3*sizeof(float), Model->VertexData );
Model->VBOVertexOffset = VBO_Offset;
VBO_Offset += Model->VertexCount*3*sizeof(float);
}
if (Model->NormalCount > 0.0)
{
glBufferSubDataARB( GL_ARRAY_BUFFER_ARB, VBO_Offset, Model->NormalCount*3*sizeof(float), Model->NormalData );
Model->VBONormalOffset = VBO_Offset;
VBO_Offset += Model->NormalCount*3*sizeof(float);
}
if (Model->TexCoordCount > 0.0)
{
glBufferSubDataARB( GL_ARRAY_BUFFER_ARB, VBO_Offset, Model->TexCoordCount*2*sizeof(float), Model->TexCoordData );
Model->VBOTexCoordOffset = VBO_Offset;
VBO_Offset += Model->TexCoordCount*2*sizeof(float);
}
return true;
}


This gets called every time I load a model. As I said before if I load one model it works fine, more than one and nothing works. I'm guessing it has something to do with my glBufferDataARB() call but I can't figure out what. Any suggestions?

This topic is closed to new replies.

Advertisement