2

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

I also followed this tutorials, but nothing works for me: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/

Thanks a lot for the response.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • 2
    Try rotating/mirroring the image. It could be a problem of coordinate system. – Banex Aug 28 '17 at 13:29
  • Maybe not the problem since your positions are ok, but you are loading an indexed model and then ignore the indices. And if this is a core profile code then you are missing a VAO. – BDL Aug 28 '17 at 13:30
  • And you are generating mipmaps **before** even uploading the texture. Also doesn't matter here since `GL_LINEAR` interpolation doesn't use mipmaps, but is in general a bad idea. – BDL Aug 28 '17 at 13:33
  • I also rotated image, but not working. @BDL So you are suggesting that i dont need to load indxes? Its just filled vector array *(I think it has any impact for other code)*. And VAO I'm using, if you mean the commands **glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID);** – Adam Lasák Aug 28 '17 at 13:42
  • First thing I'd try is flipping the bitmap on Y (turn it upside down). – Robinson Aug 28 '17 at 14:38
  • I turn the image vertically in Paint, but it also doesn't work... – Adam Lasák Aug 28 '17 at 14:58
  • The main problem I think is, that the texture is applied on the object several times, not only once. – Adam Lasák Aug 28 '17 at 15:04

1 Answers1

3

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