【问题标题】:How to draw a flat plane in openGL by drawing triangles?如何通过绘制三角形在openGL中绘制平面?
【发布时间】:2021-11-01 01:23:11
【问题描述】:
void Terrain::draw_terrain(const Input& in)
{

    this->P = glm::mat4(1.0f);
    this->V = glm::mat4(1.0f);
    this->M = glm::mat4(1.0f);

    P = in.P;
    V = in.V;

    M = glm::scale(M, glm::vec3(1.0f, 1.0f, 1.0f));
    // loop through each row
    for (int row = 0; row < terrain_height; row++)
    {
        int col;
        // adding a row of vertices
        for (col = 0; col < terrain_width - 1; col++) {
            // x, y, z, 1
            terrain_verts.emplace_back(col, row, 0.0f, 1);

        }

        // adding a row of indices
        // blue color
        for (col = 0; col < terrain_width - 1; col++)
        {
            terrain_indices.emplace_back(col + row * terrain_width);
            terrain_indices.emplace_back(col + row * terrain_width + 1);
            terrain_indices.emplace_back(col + terrain_width * (row + 1) - 1);
        }

        // green color
        for (col = terrain_width - 1; col > 0; col--)
        {
            terrain_indices.emplace_back(col + row * terrain_width);
            terrain_indices.emplace_back(col + terrain_width * (row + 1) - 1);
            terrain_indices.emplace_back(col + terrain_width * (row + 1));
        }

        // adding a row of texture coordinates
        if (row % 2 == 0)
        {
            for (col = 0; col < terrain_width; col += 2)
            {
                terrain_texture_coordinates.emplace_back(0, 0);
                terrain_texture_coordinates.emplace_back(1, 0);
            }
            
        }
        else
        {
            for (col = 0; col < terrain_width; col += 2)
            {
                terrain_texture_coordinates.emplace_back(0, 1);
                terrain_texture_coordinates.emplace_back(1, 1);
            }
        }
    }

    spLambertTextured->use();
    glUniformMatrix4fv(spLambertTextured->u("P"), 1, false, glm::value_ptr(P));
    glUniformMatrix4fv(spLambertTextured->u("V"), 1, false, glm::value_ptr(V));
    glEnableVertexAttribArray(spLambertTextured->a("vertex"));
    glEnableVertexAttribArray(spLambertTextured->a("texCoord"));
    glEnableVertexAttribArray(spLambertTextured->a("normal"));

    glUniformMatrix4fv(spLambertTextured->u("M"), 1, false, glm::value_ptr(M));

    glVertexAttribPointer(spLambertTextured->a("vertex"), 4, GL_FLOAT, false, 0, terrain_verts.data());
    glVertexAttribPointer(spLambertTextured->a("texCoord"), 2, GL_FLOAT, false, 0, terrain_texture_coordinates.data());
    glVertexAttribPointer(spLambertTextured->a("normal"), 4, GL_FLOAT, false, 0, terrain_norms.data());

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex);
    glUniform1i(spLambertTextured->u("tex"), 0);

    glDrawElements(GL_TRIANGLES, terrain_indices_count(), GL_UNSIGNED_INT, terrain_indices.data());

    glDisableVertexAttribArray(spLambertTextured->a("vertex"));
    glDisableVertexAttribArray(spLambertTextured->a("color"));
    glDisableVertexAttribArray(spLambertTextured->a("normal"));

}

首先,我在第一个“for”循环中添加了所有顶点。然后我想通过首先进入蓝色方向,然后是绿色方向来添加索引。接下来,我会向上走,遍历每一行。我将terrain_width和terrain_height设置为6。结果是红色背景。

如何修复索引以获得平面?

【问题讨论】:

  • 你说的是什么飞机?数学正确吗?
  • 我的意思是画一个平面,上面会有草的纹理。
  • 如果它是平坦的,为什么还需要两个以上的三角形?
  • 我想要草马赛克,这样看起来更逼真。
  • 我看到我的数字不正确,但你明白了:)

标签: c++ opengl glm-math terrain


【解决方案1】:

您可能希望为顶点的 z 分量添加一些值。设置所有 z = 0.0f,本质上使它成为一个 2D 平面,您可能无法有效地看到纹理。

【讨论】:

  • 我检查了它,如果我设置 z=0.01f 或 z=0.1f ,表面会向上移动。
  • 对于纹理坐标,vert num 0,它将是一个坐标 (0,0),那么:vert 1 -> (1,0) vert 2 -> (0,0) vert 3 - > (1,0) vert 4 -> (0,0) vert 5 -> (1,0) 然后接下来... vert 6 -> (0,1) vert 7 -> (1,1) vert 8 -> (0,1) ...这是正确的还是我应该更多地分割它?
猜你喜欢
  • 2020-12-08
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
相关资源
最近更新 更多