错误的 UV 贴图应用于对象 OpenGl
Wrong UV mapping applied on object OpenGl
我只想通过 Assimp 加载对象 (有效) - 顶点、uvs 和法线。然后我通过 SOIL 库加载纹理 - 通过许多教程,效果也很好,但是当纹理应用于对象时,UV 无法正常工作 (如您在图片上看到的那样)。我在此处粘贴了一些代码,在许多教程中都可以正确使用它们,但不适用于我。现在我中计了。
GLuint Texture::load(const char *name) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
int width, height;
unsigned char* image = SOIL_load_image(name, &width, &height, 0, SOIL_LOAD_RGB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenerateMipmap(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
return textureID;}
在主文件中,在主循环之前:
std::vector<unsigned short> indices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
bool res = loader->loadAssImp("Models/cube/test.obj", indices, vertices, uvs, normals);
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
在主循环中:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
我也遵循了这个教程,但对我没有任何用处:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
非常感谢您的回复。
我通过在 Assimp 设置中添加 aiProcess_flipUVs 参数解决了这个问题。这是代码:
unsigned int importOptions = aiProcess_Triangulate
| aiProcess_OptimizeMeshes
| aiProcess_JoinIdenticalVertices
| aiProcess_Triangulate
| aiProcess_CalcTangentSpace
| aiProcess_FlipUVs;
const aiScene* scene = importer.ReadFile(path, importOptions);
我只想通过 Assimp 加载对象 (有效) - 顶点、uvs 和法线。然后我通过 SOIL 库加载纹理 - 通过许多教程,效果也很好,但是当纹理应用于对象时,UV 无法正常工作 (如您在图片上看到的那样)。我在此处粘贴了一些代码,在许多教程中都可以正确使用它们,但不适用于我。现在我中计了。
GLuint Texture::load(const char *name) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
int width, height;
unsigned char* image = SOIL_load_image(name, &width, &height, 0, SOIL_LOAD_RGB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenerateMipmap(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
return textureID;}
在主文件中,在主循环之前:
std::vector<unsigned short> indices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
bool res = loader->loadAssImp("Models/cube/test.obj", indices, vertices, uvs, normals);
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
在主循环中:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
我也遵循了这个教程,但对我没有任何用处: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/
非常感谢您的回复。
我通过在 Assimp 设置中添加 aiProcess_flipUVs 参数解决了这个问题。这是代码:
unsigned int importOptions = aiProcess_Triangulate
| aiProcess_OptimizeMeshes
| aiProcess_JoinIdenticalVertices
| aiProcess_Triangulate
| aiProcess_CalcTangentSpace
| aiProcess_FlipUVs;
const aiScene* scene = importer.ReadFile(path, importOptions);