【问题标题】:Issues with making an OpenGL plane out of triangles用三角形制作 OpenGL 平面的问题
【发布时间】:2015-08-30 16:31:49
【问题描述】:

我正在尝试使用 OpenGL 制作飞机,在某种程度上代码有效,但我在某些索引上得到了奇怪的结果。我会尽力解释我的代码,它是我的代码和我在网上找到的代码的混合体。

主要:

所有设置都发生在 main 中,因此函数会知道所有需要的值

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

功能:

第一个 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);
    }   
}
}

结果:

4 x 2

指数 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]

它做了 22 个正确的值,然后其余的都是零。

知道为什么吗?

【问题讨论】:

  • 我认为您需要向索引添加 3 个元素,因为您使用三角形 (indices[i++] = base + x; indices[i++] = base + width + x; indices[i++] = ( (y - 1) * 宽度) + x)
  • 每个第三个值似乎都非常奇怪{0, 5, 4294967291, 1, 6, 4294967292, 2, 7, 4294967293, 3, 8, 4294967294, 4, 9, 4294967295, ...} unsigned int[42]
  • 你看到这个帖子了吗:stackoverflow.com/questions/5915753/…?你是如何渲染三角形的?
  • 是的,我确实看到了,我从中获得了大部分代码,但我不断收到索引数组未正确填充的奇怪问题。只是无法弄清楚出了什么问题。
  • 那些奇怪的值,当你添加额外的索引时,是负数。第一行的 y-1 为负数。

标签: c++ opengl gl-triangle-strip


【解决方案1】:

我认为您的循环需要看起来像这样(注意未经测试:)。基本上可以将其视为在四边形上循环并输出三角形对的索引。

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

【讨论】:

  • 您是否正在绘制每个三角形 [0,1][1,2][2,0] 的所有 3 条线?我只看到四边形的外环。
  • 我的循环输出了 48 个索引。 4 x 2 个四边形,每个 6 个索引。你之前的代码只分配了42,还是这样吗?
  • 我们取得了进展,现在它确实完成了 4 x 2 四边形,但那条线仍然让我很烦。 i.imgur.com/1MhcvKi.png
【解决方案2】:

我意识到顶点是从下到上创建的,并且索引被命令从上到下创建三角形。所以我改变了for循环的顶点创建。

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

它从左上角到右下角创建顶点。索引的循环保持不变:

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

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多