سؤال

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.

هل كانت مفيدة؟

المحلول

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!

نصائح أخرى

If you want to get a triangle strip for witch you have every vertex in an array, you should make an array of the vertices first.

The standard rectangle shaped mesh generation looks like this:(its in c++, but use it as pseudocode)

for(int x=0; x<sizex; x+=stepsize)
{
for(int y=0; y<sizey; y+=stepsize)
{
vertexarray.push_back(vec3(x,y,z)); //for a plane set z to any const.
}
}

then you can make an index array for this:

for(int x=0; x<sizex-1; x+=stepsize)
{
for(int y=0; y<sizey-1; y+=stepsize)
{
indexarray.push_back(y*sizex+x);
indexarray.push_back(y*sizex+x+1);
indexarray.push_back((y+1)*sizex+x); //first triangle in the strip

indexarray.push_back((y+1)*sizex+x);
indexarray.push_back(y*sizex+x+1);
indexarray.push_back((y+1)*sizex+x+1); //second triangle
}
}

then the size of the index array is

GLint count = 6(sizex-1)(sizey-1);

and in the render call you can use VAOs VBOs, or in FFP:

//the following is your code
GL11.glBegin(GL11.GL_TRIANGLE_STRIP);

          for (int i = 0; i< count i++)
          {
              GL11.glVertex3f(vertexarray[indexarray[i]].x, vertexarray[indexarray[i]].y, vertexarray[indexarray[i]].z);
          }

GL11.glEnd();

for the surface normals you can easily get the triangle points, iterating through the vertices, get their neighbours and calculating the cross products not that hard at all. You can easily extract the triangles from the index array too.

int i = 0, t = 0;
for(int x=0; x<sizex-1; x+=stepsize)
{
for(int y=0; y<sizey-1; y+=stepsize)
{
indexarray.push_back(y*sizex+x);
indexarray.push_back(y*sizex+x+1);
indexarray.push_back((y+1)*sizex+x); //first triangle in the strip

indexarray.push_back((y+1)*sizex+x);
indexarray.push_back(y*sizex+x+1);
indexarray.push_back((y+1)*sizex+x+1); //second triangle

triangles[t].vertex1.xyz = vertexarray[indexarray[i]].xyz;
triangles[t].vertex2.xyz = vertexarray[indexarray[i+1]].xyz;
triangles[t].vertex3.xyz = vertexarray[indexarray[i+2]].xyz;

triangles[t+1].vertex1.xyz = vertexarray[indexarray[i+3]].xyz;
triangles[t+1].vertex2.xyz = vertexarray[indexarray[i+4]].xyz;
triangles[t+1].vertex3.xyz = vertexarray[indexarray[i+5]].xyz;
t+=2; //you made 2 triangles
i+=6; //passed over 6 indeces that make up the 2 triangles
}
}

EDIT: the triangle structure:

struct TRIANGLE{

vec3 vertex1, vertex2,... //or vertex[3]; to declare it as an array too

};

struct vec3{

int x, y, z;
//float f_x,f_y,f_z;

vec3(int X; int Y; in Z):x(X),y(Y),z(Z){};
~vec3(){};

};

the vertices or indexes array could be list or any container. I prefer vector in C++.

The size of the triangle array is:

TRIANGLE* triangles = new TRIANGLES[count/3];
//3 vertices for a triangle, same as (sizex-1)*(sizey-1)*2 for 2
//triangles for every quad that makes up the mesh, and it has (sizex-1)(sizey-1) quads
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top