前言:
我已经阅读了Generate a plane with triangles strips中的帖子。下面的代码来自那里,但它不允许我纹理或点亮网格。
客观的:
给定一个数字“n”,使用 OpenGL生成、纹理和绘制width = n
&的平面。height = n
平面应由单位大小的单个四边形组成。
到目前为止的代码:
生成顶点的代码
float * Plane::getUniqueVertices()
{
if (uniqueVertices) return uniqueVertices;
uniqueVertices = new float[NUM_VERTICES];
int i = 0;
for (int row = 0; row<height; row++) {
for (int col = 0; col<width; col++) {
uniqueVertices[i++] = (float)col; //x
uniqueVertices[i++] = (float)row; //y
uniqueVertices[i++] = 0.0f; //z
}
}
return uniqueVertices;
}
生成索引的代码
int * Plane::getIndices()
{
if (indices) return indices;
indices = new int[NUM_DRAW_ELEMENTS];
int i = 0;
for (int row = 0; row<height - 1; row++)
{
if ((row & 1) == 0) // even rows
{
for (int col = 0; col<width; col++)
{
indices[i++] = col + row * width;
indices[i++] = col + (row + 1) * width;
}
}
else // odd rows
{
for (int col = width - 1; col>0; col--)
{
indices[i++] = col + (row + 1) * width;
indices[i++] = col - 1 + +row * width;
}
}
}
if ((height & 1) && height > 2)
{
indices[i++] = (height - 1) * width;
}
return indices;
}
然后我使用 GL_TRIANGLE_STRIP 和索引缓冲区绘制平面。
我需要什么?:
虽然上述方法有效,但我无法纹理/ uv 贴图或获取法线,因为它使用GL_TRIANGLE_STRIP
因此三角形之间共享的某些顶点的 uv 坐标不同。我需要一些方法来获取顶点数组、它们的纹理坐标和法线,这样我就可以对平面进行纹理化和光照,最终得到这样的结果。只要飞机由不同的四边形组成并且我可以更改飞机的尺寸,我如何获得它们并不重要。