PDA

View Full Version : Textures not mapping correctly



andrei.alex
07-14-2015, 11:55 AM
I made a parser for OBJ file and the oject is displayed correctly ( also tryed w but the texture is not applied correctly at all.
I read documents about how it needs to be done and watched videos but I can't seem to get it working.

I use vectors and GLM to store data in vec2 and vec3 and GLuint for indices.
After reading the vertecies, texture coordinates and normals I use the function below to arrenge vertices with normals and texture coordinates.



void Obj::addIndices(std::string src, const std::vector<glm::vec3> &temp_vertices, const std::vector<glm::vec2> &temp_textureCoords, const std::vector<glm::vec3> &temp_normals, std::vector<glm::vec3> &vertices, std::vector<glm::vec2> &textureCoords, std::vector<glm::vec3> &normals, std::vector<GLuint> &indices)
{
GLfloat v1, v2, v3, t1, t2, t3, n1, n2, n3;
std::string scanStr = "f %f/%f/%f %f/%f/%f %f/%f/%f";
sscanf_s(src.c_str(), scanStr.c_str(), &v1, &t1, &n1, &v2, &t2, &n2, &v3, &t3, &n3);

v1--; v2--; v3--;
t1--; t2--; t3--;
n1--; n2--; n3--;

GLuint index = indices.size();
indices.push_back(index);
vertices.push_back(temp_vertices[v1]);
textureCoords.push_back(temp_textureCoords[t1]);
normals.push_back(temp_normals[n1]);

index = indices.size();
indices.push_back(index);
vertices.push_back(temp_vertices[v2]);
textureCoords.push_back(temp_textureCoords[t2]);
normals.push_back(temp_normals[n2]);

index = indices.size();
indices.push_back(index);
vertices.push_back(temp_vertices[v3]);
textureCoords.push_back(temp_textureCoords[t3]);
normals.push_back(temp_normals[n3]);
}


This is how it looks:
1931

This is how it should look:
1932

GClements
07-14-2015, 06:38 PM
There's nothing wrong with the snippet of code you posted.

And from the attached image, it seems more likely that the texture itself is incorrect rather than the texture coordinates.

andrei.alex
07-14-2015, 09:52 PM
The image is correct and I tested the utiliy that loads the image with a quad and different image. I will test the image using the quad and see what happens.
I am using FreeImage to load images.

andrei.alex
07-14-2015, 10:09 PM
Tested and it is working now.
The problem was that I use GL_RGB and GL_BGR without the alpha when loading the image with glTexImage2D.

Thanks.