[英]Issues with making an OpenGL plane out of triangles
I am trying to make a plane using OpenGL, to some extent the code works but I get weird results on some indices. 我正在尝试使用OpenGL制作飞机,在某种程度上代码可以工作,但是在某些索引上却得到了奇怪的结果。 I will try to explain my code the best I can, it's a mixture of my code and what I have found online.
我将尽力解释我的代码,这是我的代码和我在网上找到的混合物。
The MAIN: 主要的:
All the set-up happens in the main so the function would know all the values needed 所有设置都在主设备中进行,因此该功能将知道所需的所有值
float zoom = 6.0f;
float vertical = 1.2f;
float horizontal = 1.2f;
const int planeWidth = 4; //columns
const int planeHeight = 2; //rows
const int totalVertices = (planeWidth + 1) * (planeHeight + 1);
//GLfloat* vertices = new GLfloat[totalVertices];
GLfloat vertices[totalVertices] = { 0.0 };
const int indPerRow = planeWidth * 2 + 2;
const int indDegenReq = (planeHeight - 1) * 2;
const int totalIndices = indPerRow * planeWidth + indDegenReq;
//GLuint* indices = new GLuint[totalIndices];
GLuint indices[totalIndices] = { 0 };
GLfloat texCoords[totalVertices] = { 0 };
makePlane(planeWidth, planeHeight, vertices, indices, texCoords);
The FUNCTION: 功能:
First for loop creates vertices and the second does the indices 第一个for循环创建顶点,第二个执行索引
void makePlane(int width, int height, GLfloat *vertices, GLuint *indices)
{
width++; //columns
height++; //rows
int size = sizeof(GLfloat);
for (int y = 0; y < height; y++)
{
int base = y * width;
for (int x = 0; x < width; x++)
{
int index = (base + x) * 2;
vertices[index] = (float)x;
vertices[index +1] = (float)y;
}
}
int i = 0;
height--;
for (int y = 0; y < height; y++)
{
int base = y * width;
for (int x = 0; x < width; x++)
{
indices[i++] = base + x;
indices[i++] = base + width + x;
}
if (y < height - 1)
{
indices[i++] = ((y + 1) * width + (width - 1));
indices[i++] = ((y + 1) * width);
}
}
}
The RESULT: 结果:
4 x 2 4 x 2
Indices 0, 5, 1, 6, 2, 7, 3, 8, 4, 9, 9, 5, 5, 10, 6, 11, 7, 12, 8, 13, 9, 14, 0, 0, 0, 0, 0, 0, ...} unsigned int[42] 索引0、5、1、6、2、7、3、8、4、9、9、5、5、10、6、11、7、12、8、9、14、0、0、0 ,0,0,0,...} unsigned int [42]
It does 22 values right and then the rest are zeros. 它会正确执行22个值,然后其余均为零。
Any Idea why? 知道为什么吗?
I think your loop needs to look something like this (beware untested :). 我认为您的循环需要看起来像这样(请注意,未经测试的:)。 Basically think of it as looping over quads and outputting indices for triangle pairs.
基本上将其视为遍历四边形并输出三角形对的索引。
for (int y = 0; y < height; y++)
{
unsigned int base = y * width;
unsigned int top = base + width;
for (int x = 0; x < (width-1); x++)
{
indices[i++] = base + x;
indices[i++] = top + x;
indices[i++] = top + x + 1;
indices[i++] = top + x + 1;
indices[i++] = base + x + 1;
indices[i++] = base + x;
}
}
I realised that vertices were being created from bottom to top and indices were ordered to create triangles from top to bottom. 我意识到顶点是从下到上创建的,索引被命令从上到下创建三角形。 So I changed the vertex creation for loop.
因此,我将顶点创建更改为循环。
int index = 0;
for (int y = height; y >= 0; y--)
{
for (int x = 0; x <= width; x++)
{
vertices[index] = ((float)x/4)-0.5;
vertices[index +1] = ((float)y/4)-0.5;
vertices[index + 2] = 0.0f;
index += 3;
}
}
It creates the vertices from top left to right bottom. 它从左上角到右下角创建顶点。 Loop for the indices stayed the same:
索引的循环保持不变:
int i = 0;
++width;
GLuint indices2[170] = { 0 };
for (int y = 0; y < height; y++)
{
int base = y * width;
for (int x = 0; x < width; x++)
{
indices[i++] = base + x;
indices[i++] = base + width + x;
}
if (y < height - 1)
{
indices[i++] = ((y + 1) * width + (width - 1));
indices[i++] = ((y + 1) * width);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.