Skip to main content
improved title and structure of the question
Source Link

Loading Texture mapping issue in my Wavefront *.obj file with my own parser; textures aren't mapped correctlyparser

I have writtenwrote a simple .obj*.obj parser for my OpenGL game engine that readsreading read vertices, uv'stexcoords and normals; butnormals. But when I get to drawing thedraw a model in the texture isn't mapped correctly.

I have already tried uv.y = 1.0f - uv.y to get inverted Y axis coordinates as suggested by a few people but my textures are still loadeddisplayed skewed.

this is how it currently looks

I know thethat both texture file and my rendering code are correct because. Because when I render a plane, with and use hand written and hard coded texture coordinates and vertices using the same texture, it renders fine. I can render a textured cube by hand fine, too.

this is how it should look

I am usinguse an index buffer to store vertex elements and I use the face elements provided by the obj*.obj file to fill texture coordinate buffer and normal buffers with the correct vectorsbuffer.

What is wrong with my parser?

The parser code:

Model::Model(const string &givenName, const string &filePath){ name = givenName; std::ifstream in(MODEL_PATH+filePath, std::ios::in); if(!in){ error("Model \"" + filePath + "\" does not exist"); return; } ModelMeshIndex tempindex; vector<vec3> normals; vector<vec2> uvs; string line; while(getline(in, line)){ if(line.substr(0,2) == "v "){ std::istringstream s(line.substr(2)); vec3 v(0.0f); s >> v.x >> v.y >> v.z; data.vertices.push_back(v); } else if(line.substr(0,2) == "vt"){ std::istringstream s(line.substr(2)); vec2 v(0.0f); s >> v.x >> v.y; //v.y = 1.0f-v.y; uvs.push_back(v); } else if(line.substr(0,2) == "vn"){ std::istringstream s(line.substr(2)); vec3 v(0.0f); s >> v.x >> v.y >> v.z; normals.push_back(v); } else if(line.substr(0,2) == "f "){ std::istringstream s(line.substr(1)); char tmp; // For the slashes between elements unsigned short a,b,c, d,e,f, g,h,i; s >> a >> tmp >> d >> tmp >>g; s >> b >> tmp >> e >> tmp >>h; s >> c >> tmp >> f >> tmp >>i; a--; b--; c--; d--; e--; f--; g--; h--; i--; data.index.vertexElements.push_back(a);  data.index.vertexElements.push_back(b);  data.index.vertexElements.push_back(c);   data.index.uvElements.push_back(d);  data.index.uvElements.push_back(e);  data.index.uvElements.push_back(f);   data.index.normalElements.push_back(g);  data.index.normalElements.push_back(h);  data.index.normalElements.push_back(i); } else if(line[0] == '#') continue; else continue; } for(unsigned int i = 0; i < data.index.normalElements.size(); ++i) data.normals.push_back(normals[data.index.normalElements[i]]); for(unsigned int i = 0; i < data.index.uvElements.size(); ++i) data.uvs.push_back(uvs[data.index.uvElements[i]]); //data.uvs.push_back(vec2( 0.0f, 0.0f)); // bottom left //data.uvs.push_back(vec2( 0.0f, 1.0f)); // top left //data.uvs.push_back(vec2( 1.0f, 0.0f)); // bottom right //data.uvs.push_back(vec2( 1.0f, 1.0f)); // top right good = true; } 

Output when rendering a simple quad:

What it should look like:

enter image description hereis wrong with my parser?

Loading .obj file with my own parser; textures aren't mapped correctly

I have written a simple .obj parser for my OpenGL game engine that reads vertices, uv's and normals; but when I get to drawing the model in the texture isn't mapped correctly.

I have tried uv.y = 1.0f-uv.y to get inverted Y axis coordinates as suggested by a few people but my textures are still loaded skewed.

I know the texture and my rendering are correct because when I render a plane, with hand written texture coordinates and vertices using the same texture, it renders fine. I can render a textured cube by hand fine too.

I am using an index buffer to store vertex elements and I use the face elements provided by the obj to fill texture coordinate and normal buffers with the correct vectors.

What is wrong with my parser?

The parser code:

Model::Model(const string &givenName, const string &filePath){ name = givenName; std::ifstream in(MODEL_PATH+filePath, std::ios::in); if(!in){ error("Model \"" + filePath + "\" does not exist"); return; } ModelMeshIndex tempindex; vector<vec3> normals; vector<vec2> uvs; string line; while(getline(in, line)){ if(line.substr(0,2) == "v "){ std::istringstream s(line.substr(2)); vec3 v(0.0f); s >> v.x >> v.y >> v.z; data.vertices.push_back(v); } else if(line.substr(0,2) == "vt"){ std::istringstream s(line.substr(2)); vec2 v(0.0f); s >> v.x >> v.y; //v.y = 1.0f-v.y; uvs.push_back(v); } else if(line.substr(0,2) == "vn"){ std::istringstream s(line.substr(2)); vec3 v(0.0f); s >> v.x >> v.y >> v.z; normals.push_back(v); } else if(line.substr(0,2) == "f "){ std::istringstream s(line.substr(1)); char tmp; // For the slashes between elements unsigned short a,b,c, d,e,f, g,h,i; s >> a >> tmp >> d >> tmp >>g; s >> b >> tmp >> e >> tmp >>h; s >> c >> tmp >> f >> tmp >>i; a--; b--; c--; d--; e--; f--; g--; h--; i--; data.index.vertexElements.push_back(a); data.index.vertexElements.push_back(b); data.index.vertexElements.push_back(c); data.index.uvElements.push_back(d); data.index.uvElements.push_back(e); data.index.uvElements.push_back(f); data.index.normalElements.push_back(g); data.index.normalElements.push_back(h); data.index.normalElements.push_back(i); } else if(line[0] == '#') continue; else continue; } for(unsigned int i = 0; i < data.index.normalElements.size(); ++i) data.normals.push_back(normals[data.index.normalElements[i]]); for(unsigned int i = 0; i < data.index.uvElements.size(); ++i) data.uvs.push_back(uvs[data.index.uvElements[i]]); //data.uvs.push_back(vec2( 0.0f, 0.0f)); // bottom left //data.uvs.push_back(vec2( 0.0f, 1.0f)); // top left //data.uvs.push_back(vec2( 1.0f, 0.0f)); // bottom right //data.uvs.push_back(vec2( 1.0f, 1.0f)); // top right good = true; } 

Output when rendering a simple quad:

What it should look like:

enter image description here

Texture mapping issue in my Wavefront *.obj parser

I wrote a simple *.obj parser reading read vertices, texcoords and normals. But when I draw a model the texture isn't mapped correctly.

I have already tried uv.y = 1.0f - uv.y to get inverted Y axis coordinates as suggested by a few people but my textures are still displayed skewed.

this is how it currently looks

I know that both texture file and rendering code are correct. Because when I render a plane and use hand written and hard coded texture coordinates and vertices it renders fine. I can render a textured cube by hand fine, too.

this is how it should look

I use an index buffer to store vertex elements and I use the face elements provided by the *.obj file to fill texture coordinate buffer and normal buffer.

Model::Model(const string &givenName, const string &filePath){ name = givenName; std::ifstream in(MODEL_PATH+filePath, std::ios::in); if(!in){ error("Model \"" + filePath + "\" does not exist"); return; } ModelMeshIndex tempindex; vector<vec3> normals; vector<vec2> uvs; string line; while(getline(in, line)){ if(line.substr(0,2) == "v "){ std::istringstream s(line.substr(2)); vec3 v(0.0f); s >> v.x >> v.y >> v.z; data.vertices.push_back(v); } else if(line.substr(0,2) == "vt"){ std::istringstream s(line.substr(2)); vec2 v(0.0f); s >> v.x >> v.y; //v.y = 1.0f-v.y; uvs.push_back(v); } else if(line.substr(0,2) == "vn"){ std::istringstream s(line.substr(2)); vec3 v(0.0f); s >> v.x >> v.y >> v.z; normals.push_back(v); } else if(line.substr(0,2) == "f "){ std::istringstream s(line.substr(1)); char tmp; // For the slashes between elements unsigned short a,b,c, d,e,f, g,h,i; s >> a >> tmp >> d >> tmp >>g; s >> b >> tmp >> e >> tmp >>h; s >> c >> tmp >> f >> tmp >>i; a--; b--; c--; d--; e--; f--; g--; h--; i--; data.index.vertexElements.push_back(a);  data.index.vertexElements.push_back(b);  data.index.vertexElements.push_back(c);   data.index.uvElements.push_back(d);  data.index.uvElements.push_back(e);  data.index.uvElements.push_back(f);   data.index.normalElements.push_back(g);  data.index.normalElements.push_back(h);  data.index.normalElements.push_back(i); } else if(line[0] == '#') continue; else continue; } for(unsigned int i = 0; i < data.index.normalElements.size(); ++i) data.normals.push_back(normals[data.index.normalElements[i]]); for(unsigned int i = 0; i < data.index.uvElements.size(); ++i) data.uvs.push_back(uvs[data.index.uvElements[i]]); //data.uvs.push_back(vec2( 0.0f, 0.0f)); // bottom left //data.uvs.push_back(vec2( 0.0f, 1.0f)); // top left //data.uvs.push_back(vec2( 1.0f, 0.0f)); // bottom right //data.uvs.push_back(vec2( 1.0f, 1.0f)); // top right good = true; } 

What is wrong with my parser?

Minor corrections. Added images to the post instead of an external non SE Imgur link.
Source Link

I have tried uv.y = 1.0f-uv.yuv.y = 1.0f-uv.y to get inverted Y axis coordinates as suggested by a few people but my textures are still loaded skewed.

PARSER CODE The parser code:

OUTPUT WHEN RENDERING A SIMPLE QUAD http://i44.tinypic.com/2u618on.jpg Output when rendering a simple quad:

HOW IT SHOULD LOOK http://i41.tinypic.com/33jpbua.jpg

What it should look like:

enter image description here

I have tried uv.y = 1.0f-uv.y to get inverted Y axis coordinates as suggested by a few people but my textures are still loaded skewed.

PARSER CODE

OUTPUT WHEN RENDERING A SIMPLE QUAD http://i44.tinypic.com/2u618on.jpg

HOW IT SHOULD LOOK http://i41.tinypic.com/33jpbua.jpg

I have tried uv.y = 1.0f-uv.y to get inverted Y axis coordinates as suggested by a few people but my textures are still loaded skewed.

The parser code:

Output when rendering a simple quad:

What it should look like:

enter image description here

More information about the problem.
Source Link
RamblingMad
  • 287
  • 3
  • 12

I have written a simple .obj parser for my OpenGL game engine that reads vertices, uv's and normalsnormals; but when I get to drawing the model in the texture isn't mapped correctly.

I know the texture and my rendering isare correct because when I renderedrender a plane, with hand written texture coordinates and vertices using the same texture, it renderedrenders fine. I rendered I can render a textured cube by hand fine too.

I am using an index buffer to store vertex elements and I use the face elements provided by the obj to fill texture coordinate and normal buffers with the correct vectors.

I have written a simple .obj parser for my game engine that reads vertices, uv's and normals but when I get to drawing the model the texture isn't mapped correctly.

I know my rendering is correct because when I rendered a plane with hand written texture coordinates and vertices it rendered fine. I rendered a textured cube by hand too.

I have written a simple .obj parser for my OpenGL game engine that reads vertices, uv's and normals; but when I get to drawing the model in the texture isn't mapped correctly.

I know the texture and my rendering are correct because when I render a plane, with hand written texture coordinates and vertices using the same texture, it renders fine. I can render a textured cube by hand fine too.

I am using an index buffer to store vertex elements and I use the face elements provided by the obj to fill texture coordinate and normal buffers with the correct vectors.

added 67 characters in body; edited tags
Source Link
RamblingMad
  • 287
  • 3
  • 12
Loading
Source Link
RamblingMad
  • 287
  • 3
  • 12
Loading