Question

I'm trying to model an object described in a WRL (VRML) file using OpenGL.

I'm not really concerned with parsing the file, I figure that part will be fairly straight forward. At this stage I am just trying to hard-code in a vertex array and index array so that I can get a good understanding of how this works so that I can generalise for any WRL input file.

I'm trying a basic box (rectangular prism) model first. I currently have this vertex array:

GLfloat vertices[] = {
     -0.200000, -0.025000, -0.050000,
     -0.200000, -0.025000, 0.050000, 
     -0.200000, 0.025000, -0.050000, 
     -0.200000, 0.025000, 0.050000, 
      0.200000, -0.025000, -0.050000,
      0.200000, -0.025000, 0.050000, 
      0.200000, 0.025000, -0.050000, 
      0.200000, 0.025000, 0.050000
};

and this index array:

GLubyte indices[] = {
     7, 3, 5, -1, 5, 3, 1, -1,
     6, 2, 7, -1, 7, 2, 3, -1, 
     4, 0, 6, -1, 6, 0, 2, -1,
     5, 1, 4, -1, 4, 1, 0, -1,
     2, 0, 3, -1, 3, 0, 1, -1,
     4, 6, 5, -1, 5, 6, 7, -1
};

which came directly from the WRL file Coordinate3 {point []} and IndexedFaceSet {coordIndex []}.

I then enable vertex array functionality by calling:

glEnableClientState(GL_VERTEX_ARRAY);

and set up the glVertexPointer:

glVertexPointer(3, GL_FLOAT, 0, vertices);

finally I use the glDrawElements function to draw the box:

glDrawElements(GL_POLYGON, 24, GL_UNSIGNED_BYTE, indices);

and then deactivate vertex array functionality:

glDisableClientState(GL_VERTEX_ARRAY);

So after this, I would expect a box to be drawn, and when I use glDrawElements(GL_POINTS, 24, GL_UNSIGNED_BYTE, indices); it shows the 8 vertices as epected in what, if the correct vertices were joined with lines, would represent the box expected (except there is a point in the middle, but when I use 26 as the count argument, then the point in the middle dissappears)

However when I use GL_POLYGON or GL_LINE_LOOP at the first argument to glDrawElements, I get rubbish. The 8 vertices are obviously there, but they're joined up in really strange ways.

I'm pretty confused by now, and I'm not even sure I'm doing this correctly. Perhaps someone could put me in the right direction at least?

Was it helpful?

Solution

A rectangular prism is not a GL_POLYGON. Note the singular form of that word: polygon. As in one polygon. A rectangular prism is composed of many polygons, not just one.

What you want is to draw some GL_TRIANGLES. Create an index list that shows each of the triangles that compose the box. That means each box face is made of two triangles, so you need 12 triangles total. That means 36 indices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top