PDA

View Full Version : opengl beginner problems with glDrawElements



hordak
07-13-2008, 04:46 AM
Hi,

I just started with OpenGL, so this question is certainly a silly one, but I hope it's easy to answer for a lot of you.
I just started experimenting with OpenGL, and what I'm trying to do is drawing an indexed vertex array, which is just a simple cube. I took the vertices and indices from a blender x3d export and copied them into an array. I also evaluated the vertices and triangels on a paper and they should be correct. The result on the screen however is not much of a cube. It's a square and a few triangles pointing in weird directions.
So here is the code of my draw method, any help would be highly appreciated.
I guess there's something completely wrong with the way I thought drawing indexed primitives might work.
Thank in advance.


those guys are defined globally:



float cameraX = 4.0f;
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)



and here's the drawing code, it draws a cube and updates the lookat to give a crappy flyby effect:



const GLfloat cubeVertices[] =
{
1, 1, -1,
1, -1, -1,
-1, -1, -1,
-1, 1, -1,
1, 1, 1,
1 -1, 1,
-1, -1, 1,
-1, 1, 1,
};

const GLushort cubeIndices[] =
{
0, 1, 2,
0, 2, 3,
4, 7, 6,
4, 6, 5,
0, 4, 5,
0, 5, 1,
1, 5, 6,
1, 6, 2,
2, 6, 7,
2, 7, 3,
4, 0, 3,
4, 3, 7,
};

const GLubyte squareColors[] =
{
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,

255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,

};


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLfloat zNear = 0.1, zFar = 1000.0, fieldOfView = 60.0;
GLfloat size = 0;
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

glFrustum(-size, size, -size / (640.0f / 480.0f), size / (640.0f / 480.0f), zNear, zFar);

cameraX -= 0.01f;

gluLookAt(4.0f, cameraX, cameraX, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

glMatrixMode(GL_MODELVIEW);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);


glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, cubeIndices);

glFlush();

zed
07-13-2008, 12:25 PM
you have to have the same number of things for each vertex
eg texcoords,colors,vertices etc

it looks like there u have 8 vertices + ~24colors

try it without the colors first

in your case with the colors u may have to duplicate the vertices
if a vertex can have 2 different colors

niniendowarrior
07-14-2008, 04:09 PM
I also have a problem with glDrawElements and Vertex Arrays. What I'd like to know is, what are things I have to look out for when debugging this. As usual, there are random triangles and the vertices look okay when I use GL_POINTS instead of GL_TRIANGLES.

I'm sort of inheriting the GL code from a previous effort, so I'm trying to sort out this problem.

Thanks!

Dark Photon
07-15-2008, 07:21 AM
I also have a problem with glDrawElements and Vertex Arrays. What I'd like to know is, what are things I have to look out for when debugging this. As usual, there are random triangles and the vertices look okay when I use GL_POINTS instead of GL_TRIANGLES.
When debugging, I'd go with zed's suggestion first. Make sure your positions are legit by disabling all the other vertex attribute arrays and rendering with fixed function pipe (no shaders). That'll also make sure your base state for rendering that batch is legit. Get that working first.

What problem are you seeing? If weird triangles, make sure your triangle vertex index order follows the glFrontFace convention you've set (counterclockwise by default), and glDisable GL_LIGHTING, GL_CULL_FACE, GL_ALPHA_TEST, GL_BLEND, plus shaders. Without knowing your app, hard to say what's going on. Isolate a test case and post.

As you learn, you'll find this page very useful:

* OpenGL API Reference Pages (http://www.opengl.org/sdk/docs/man/)

niniendowarrior
07-15-2008, 03:10 PM
Thanks for your insight Dark Photon. I think I'm getting close to fixing it. I will attempt to isolate a test case. The code is written in python and the vertices were being stored in tuples and the index is stored in lists. I understand that OpenGL needs byte arrays, so I managed to fix a significant portion of the random triangles by casting the storage into array instead of tuples or lists. I surmise that the same thing should be done for color arrays and textures.

niniendowarrior
07-16-2008, 02:57 AM
I'm thinking of how to post the code in a meaningful sort of way... but here's a picture so you can see what's up.

http://i105.photobucket.com/albums/m234/qwerty_warrior/neveredit/neverediterror2.png

It used to render absolutely random triangles everwhere. I think the problem is slightly different now. Putting it at wire-frame do help a lot in taking a look. I'll go disable some of the GL modes in a bit.

initGL:


self.width = Width
self.height = Height
glClearColor(0.0, 0.0, 0.0, 1.0)
glClearDepth(True) # Enables Clearing Of The Depth Buffer
#glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
#glEnable(GL_DEPTH_TEST) # Enables Depth Testing
#glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
#glEnable(GL_TEXTURE_2D)
#glEnable(GL_BLEND)
#glEnable(GL_ALPHA_TEST)
#glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
#glEnable(GL_LIGHTING)
#glEnable(GL_LIGHT0)
#glEnableClientState (GL_VERTEX_ARRAY)
#glEnableClientState (GL_TEXTURE_COORD_ARRAY)
glDisableClientState (GL_COLOR_ARRAY)
glDisableClientState (GL_EDGE_FLAG_ARRAY)
glDisableClientState (GL_INDEX_ARRAY)
glDisableClientState (GL_NORMAL_ARRAY)
#glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
#glEnable(GL_TEXTURE_2D)
#glAlphaFunc (GL_GREATER, 0.1)
glEnable(GL_POLYGON_OFFSET_FILL)
glPolygonOffset(0.1,0)
glInitNames()

self.SetupProjection()
#self.recomputeCamera()
#FIXME, wireframe is forced on.
self.wireOn()


Rendering code:


glVertexPointerf(node.vertices)
if node.normals:
glNormalPointerf(node.normals)
glEnableClientState(GL_NORMAL_ARRAY)
else:
glDisableClientState(GL_NORMAL_ARRAY)
for l in node.vertexIndexLists:
glDrawElementsus(GL_TRIANGLES,l)

zed
07-16-2008, 12:40 PM
disable everthing except the vertices (including the texture coordinates)
use glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
+ draw
u should quickly be able to see whats happening

+ then reenable the texture coords,colors etc one at a time + rerun

niniendowarrior
07-18-2008, 07:55 PM
Thanks zed. I've gotten some more minor improvements and it's not as wild. I'm going to try to solve these problems one at a time. What I cannot seem to figure right now is that the colours flicker on redraw.

http://i105.photobucket.com/albums/m234/qwerty_warrior/neveredit/neverediterror2-1.png
http://i105.photobucket.com/albums/m234/qwerty_warrior/neveredit/neverediterror3.png

Any ideas what could be happening here?

The polygons were a bit wacky but now it's okay, I think. Sometimes, some of the lines disappear when I'm rotating. I wonder if any cull effects are still enabled although I did disable everything.