我遇到的问题是找到构成给定opengl三角形条内所有三角形的顶点。我想知道是否有人可以通过一些伪代码或示例来帮助解决这个问题。
这是我绘制每个顶点的示例。我知道我需要将每个顶点分配给一个包含局部变量point1,point2和point3的三角形对象。这三个变量是一个矢量对象,每个都有一个x,y和z。但是我的问题总是最终会在哪里创建三角形对象,而我显然不应该为每个循环创建一个for循环? 另外,我需要知道每个三角形的点的原因是因为我正在计算表面法线。
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。
答案 0 :(得分:1)
假设您的代码有效:
// 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现在可以呈现所有三角形!
答案 1 :(得分:1)
如果你想获得一个三角形条带,你有一个数组中的每个顶点,你应该首先创建一个顶点数组。
标准矩形网格生成如下所示:(在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);
并且在渲染调用中,您可以使用VAO 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 ++中的vector。
三角形数组的大小为:
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