javaopenglgeometrylwjglgl-triangle-strip

Getting the triangles within a tringle strip?


I am having issues with finding the vertices that make up all the triangles within a given opengl triangle strip. I was wondering if anyone could help with ways of approaching that with some pseudo code or examples.

Here is an example of me drawing every vertex. I know that I need to assign each vertex to a triangle object which contains local variables point1, point2, and point3. These three variables are a vector object which each have an x, y, and z. But my problem always ends up with where do I create the triangle object when I obviously shouldn't create one every iteration of either for loop? Also, the reason I am needing to know the points of every triangle is because I am calculating surface normals.

GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
     for (float z=0.0f; z<=20.0f; z+=2.0f) {
          for (float x=0.0f; x<=20.0f; x+=2.0f) {

               GL11.glVertex3f(x, 0.0f, z); 
          }
     }

GL11.glEnd();

Also, I am using the lwjgl in java if that is of any help.


Solution

  • Assuming your code worked:

     // Triangle Class
     public class Triangle {  
          public Point points[] = {new Point(), new Point(), new Point()};
     }
    
     Triangle currentTriangle = new Triangle();
     int trianglePointIndex = 0;
     List<Triangle> triangleList = new ArrayList<Triangle>();
     GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
     for (float z=0.0f; z<=20.0f; z+=2.0f) {
          for (float x=0.0f; x<=20.0f; x+=2.0f) {
               GL11.glVertex3f(x, 0.0f, z); 
    
               Point currentPoint = currentTriangle.points[ trianglePointIndex ];
               currentPoint.x = x;
               currentPoint.y = 0.0f;
               currentPoint.z = z;
    
               trianglePointIndex++;
    
               if (trianglePointIndex == 3) {
                    triangleList.add( currentTriangle );
                    Triangle nextTriangle = new Triangle();
                    nextTriangle.points[0].set( currentTriangle.points[1] );
                    nextTriangle.points[1].set( currentTriangle.points[2] );
                    currentTriangle = nextTriangle;
                    trianglePointIndex = 2;
               }
          }
     }
     GL11.glEnd();
    

    triangleList now has all triangles being rendered!