c++openglassimpsoil

Wrong UV mapping applied on object OpenGl


I just want to load a object via Assimp (that works) - vertices, uvs and normals. Then I load a texture via SOIL library - through many tutorials, that work also fine, but when the texture is applied to the object, UVs don't work correctly (as you can see it on the picture). I paste here some codes, in many tutorials that works for them correctly, but not for me. Now I am in trap.

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

In main file, before main loop:

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

In main loop:

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


Solution

  • I Solved it by adding aiProcess_flipUVs parametr into Assimp settings. Here's the code:

    unsigned int importOptions = aiProcess_Triangulate
        | aiProcess_OptimizeMeshes              
        | aiProcess_JoinIdenticalVertices       
        | aiProcess_Triangulate                
        | aiProcess_CalcTangentSpace           
        | aiProcess_FlipUVs;
    
    const aiScene* scene = importer.ReadFile(path, importOptions);