在字符串带内获取三角形



我在查找构成给定opengl三角形带中所有三角形的顶点时遇到问题。我想知道是否有人可以用一些伪代码或示例来帮助解决这个问题。

这里有一个我画每个顶点的例子。我知道我需要将每个顶点分配给一个三角形对象,该对象包含局部变量point1、point2和point3。这三个变量是一个向量对象,每个向量对象都有一个x、y和z。但我的问题总是在于,当我显然不应该在循环的每一次迭代中都创建一个三角形对象时,我该在哪里创建三角形对象?此外,我需要知道每个三角形的点的原因是因为我正在计算曲面法线。

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();

此外,如果有任何帮助的话,我正在使用java中的lwjgl。

假设您的代码有效:

 // 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现在已经渲染了所有三角形!

如果你想为数组中的每个顶点都有一个三角形条,你应该首先制作一个顶点数组。

标准矩形网格生成如下所示:(它在c++中,但用作伪代码)

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.
}
}

然后你可以为这个做一个索引数组:

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
}
}

则索引数组的大小为

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

在渲染调用中,您可以使用VAOs-VBO,或者在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();

对于曲面法线,你可以很容易地获得三角形点,遍历顶点,获得它们的邻居,并计算叉积,而不是那么难。您也可以很容易地从索引数组中提取三角形。

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
}
}

编辑:三角形结构:

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(){};
};

顶点或索引数组可以是列表或任何容器。我更喜欢C++中的向量。

三角形阵列的大小为:

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

相关内容

  • 没有找到相关文章

最新更新