【发布时间】: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