【发布时间】:2021-11-02 11:08:13
【问题描述】:
我编写了一个由三角形组成的平坦地形,但似乎发生了一些镜像。我想让它模仿草,但在某些地方它是模糊的。我需要在 glTexParameteri 中添加一些参数吗?或者可能是与绘图代码相关的错误?
读取纹理的函数:
GLuint Terrain::write_model_texture(const char* filename)
{
GLuint tex;
// Activate texture 0
glActiveTexture(GL_TEXTURE0);
// Read into computers memory
std::vector<unsigned char> image;
unsigned width, height;
// Read the image
unsigned error = lodepng::decode(image, width, height, filename);
// Import to graphics card memory
glGenTextures(1, &tex); //Initialize one handle
glBindTexture(GL_TEXTURE_2D, tex); //Activate handle
// Copy image to graphics cards memory represented by the active handle
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)image.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return tex;
}
还有绘制三角形的代码:
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::rotate(M, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
M = glm::translate(M, glm::vec3(-5.0f, -5.0f, 0.0f));
//M = glm::scale(M, glm::vec3(10.0f, 10.0f, 10.0f));
for (int row = 0; row < terrain_height; row++)
{
int col;
// adding a row of vertices
for (col = 0; col < terrain_width; col++) {
// x, y, z, 1
//std::cout << random_num << std::endl;
terrain_verts.emplace_back(col, row, 0, 1);
terrain_norms.emplace_back(0.0f, 0.0f, 1.0f, 0);
}
// adding a row of indices
for (col = 0; col < terrain_width; 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);
}
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);
}
}
}
spTextured->use();
glUniformMatrix4fv(spTextured->u("P"), 1, false, glm::value_ptr(P));
glUniformMatrix4fv(spTextured->u("V"), 1, false, glm::value_ptr(V));
glEnableVertexAttribArray(spTextured->a("vertex"));
glEnableVertexAttribArray(spTextured->a("texCoord"));
glEnableVertexAttribArray(spTextured->a("normal"));
glUniformMatrix4fv(spTextured->u("M"), 1, false, glm::value_ptr(M));
glVertexAttribPointer(spTextured->a("vertex"), 4, GL_FLOAT, false, 0, terrain_verts.data());
glVertexAttribPointer(spTextured->a("texCoord"), 2, GL_FLOAT, false, 0, terrain_texture_coordinates.data());
glVertexAttribPointer(spTextured->a("normal"), 4, GL_FLOAT, false, 0, terrain_norms.data());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(spTextured->u("tex"), 0);
glDrawElements(GL_TRIANGLES, terrain_indices_count(), GL_UNSIGNED_INT, terrain_indices.data());
glDisableVertexAttribArray(spTextured->a("vertex"));
glDisableVertexAttribArray(spTextured->a("color"));
glDisableVertexAttribArray(spTextured->a("normal"));
【问题讨论】:
-
纹理可能很好,根据图像,这看起来像是 UV 或索引的问题。
标签: c++ opengl textures glm-math