如果你想为女巫获得一个三角形带,你有一个数组中的每个顶点,你应该首先制作一个顶点数组。
标准的矩形网格生成看起来像这样:(它在 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