应用于对象OpenGl的UV映射错误

时间:2017-08-28 13:23:04

标签: c++ opengl assimp soil

我只想通过Assimp (有效)加载一个对象 - 顶点,uvs和法线。然后我通过SOIL库加载纹理 - 通过许多教程,也可以正常工作,但是当纹理应用于对象时,UV不能正常工作(正如你在图片上看到的那样)< / em>的。我在这里粘贴了一些代码,在许多正确适用它们的教程中,但不适合我。现在我陷入困境。

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

Result image of object

我也遵循了这个教程,但对我来说没有任何作用: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/

非常感谢您的回复。

1 个答案:

答案 0 :(得分:-1)

我通过在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);