20-Sided Sphere (Icosahedron)

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

20-Sided Sphere (Icosahedron)

Postby general_bradley » Wed Feb 24, 2010 8:11 am

While not new to programming (or Java), I'm relatively new to OpenGL programming. I ran into it a bit using JOGL, but never went much beyond basic shapes, with the exception of reading obj files. I tried searching these forums, but didn't find anything in complexity beyond a pyramid.

I've MOSTLY got this working for a starting point, but 8 faces aren't being filled in. I suspected it was probably due to listing some of the vertices in CCW order when CW order is specified. I used different easily identifiable colors to figure out which vertices weren't having the corresponding faces being drawn (each face has vertices of unique color), and simply tried just reversing the order, but the particular face didn't show up.

For example, one of the faces missing has vertices colored: green (3), red (0,9), and white(6,8). The only face that has each of those vertices is the (3,9,8) face, but when changed to (8,9,3) the face still doesn't show (even though it should now be in CW order if it wasn't before).

Code: Select all
      float[] colors = {
         1f, 0f, 0f, 1f,      //red      0   
         1f, 1f, 0f, 1f,      //yellow   1   
         1f, 0f, 1f, 1f,      //pink      2   
         0f, 1f, 0f, 1f,      //green      3
         0f, 1f, 1f, 1f,      //l.blue   4
         0f, 0f, 1f, 1f,      //blue      5
         1f, 1f, 1f, 1f,      //white      6
         1f, 1f, 0f, 1f,      //yellow   7
         1f, 1f, 1f, 1f,      //white      8
         1f, 0f, 0f, 1f,      //red      9
         1f, 1f, 0f, 1f,      //yellow   10
         0f, 0f, 1f, 1f,      //blue      11
      };
      
      //faces - clockwise
      short[] indices = new short[] {
          1,  2,  6,   //
          1,  7,  2,   //
          3,  4,  5,   //
          4,  3,  8,   //
          6,  5, 11,   //
          5,  6, 10,   //
          9, 10,  2,   //
         10,  9,  3,   //
          7,  8,  9,   //
          8,  7,  0,   //
         11,  0,  1,   //
          0, 11,  4,   //
          6,  2, 10,   //
          1,  6, 11,   //
          3,  5, 10,   //
          5,  4, 11,   //
          2,  7,  9,   //
          7,  1,  0,   //
          3,  9,  8,   //error? (based on color assignment)
          4,  8,  0,   //
      };


I've attached the whole file incase it helps. I can supply the rest of the program if needed.

Thanks in advance for any input.
Attachments
DieRenderer.java
Supposed to render a 20-sided die
(4.61 KiB) Downloaded 194 times
general_bradley
Freshman
Freshman
 
Posts: 8
Joined: Wed Feb 24, 2010 7:40 am

Top

Postby general_bradley » Thu Feb 25, 2010 5:03 pm

Solved.

After fiddling around with it some more and finding more examples of vertices and face orderings, I noticed they all had the same ordering for the faces (+/- 1 for each vertex, depending on whether or not the data structure holding the vertices was zero-based or one-based).

So I tried drawing them differently (triangles,triangle fan, triangle strip, line strip, etc...), as well as different variables each of the programs used, and some of them worked, but most of them didn't. I think this may be differences between OpenGL, OpenGL ES, and the java wrapper used for OpenGL ES.

In short:

gl.glDrawElements(GL10.GL_TRIANGLES, numOfVertices, GL10.GL_UNSIGNED_SHORT, indexBuffer);

needed to be changed to:

gl.glDrawElements(GL10.GL_TRIANGLES, 60, GL10.GL_UNSIGNED_SHORT, indexBuffer);

(or I could've used faces.length since that was a single dimensional array that defined the 20 faces * 3 vertices per face)
general_bradley
Freshman
Freshman
 
Posts: 8
Joined: Wed Feb 24, 2010 7:40 am

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests