Jump to content
  • Advertisement
Sign in to follow this  
QQemka

Obj file loading weird problem

This topic is 800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I made converter from obj file to my format. The problem is that one subset in rendered with wrong texture. I already added tons of debug prints and for some reason one subset is rendered with wrong positioned texture it seems. Everything in the fromat seems fine, but i see one house totally grey... (model viewer shows everything properly tho). Are there any common pitfalls or something i may be unaware of? I am trying to resolve it since a few hours :(

 

I cant find any mistake, even going manually through the file and comparing results with my exporter. Some obj files load just fine and in this one i got a grey house...

I fuond 2 models on the internet, properly loaded both, but still the first one renders wrong in my engine. What may be wrong?

Edited by QQemka

Share this post


Link to post
Share on other sites
Advertisement

If I had to guess it is material mapping.

 

Usually multi material objs use materials with "usemtl" you had better then use the correct textures defined in the accompanying mtl file.

 

For example here is a simple cube.obj:

 

# Blender v2.77 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 0.000000 -1.000000
vn 1.000000 -0.000000 0.000000
vn -1.000000 -0.000000 -0.000000
usemtl Red
s off
f 2//1 4//1 1//1
f 8//2 6//2 5//2
f 6//3 3//3 2//3
f 1//4 8//4 5//4
f 2//1 3//1 4//1
f 8//2 7//2 6//2
f 6//3 7//3 3//3
f 1//4 4//4 8//4
usemtl Blue
f 5//5 2//5 1//5
f 3//6 8//6 4//6
f 5//5 6//5 2//5
f 3//6 7//6 8//6
 
Here is its mtl:
 
# Blender MTL File: 'None'
# Material Count: 2
 
newmtl Blue
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.000000 0.004612 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd  blue.tga
 
newmtl Red
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.000000 0.000630
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd  red.tga
 
So load the appropriate material and texture, don't bind materials in the order the editor assigned them and do not use the order from the file (as you can see obj references Red, Blue; but the material file lists Blue Red)
Edited by efolkertsma

Share this post


Link to post
Share on other sites

Any chance the V coordinate is flipped upside down? (i.e. top-to-bottom vs. bottom-to-top) I don't think there's much you can do if the two models are using different conventions other than to flip the texture itself.

 

Maybe showing a screenshot would be useful? (EDIT: especially since "totally gray" sounds more like the texture isn't being used =P though that's more likely to show white?)

Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites

Check your UV coordinate settings.

 

I have found many OBJ files with UV coordinates less than zero and greater than one.

 

If you are clamping your texture coords, then anything less than 0 will be 0 and anything greater than 1 will be 1.

Share this post


Link to post
Share on other sites

I seriosuly did not expect that many responses from you guys, thanks a lot.

 

i am currently digging in ASSIMP source code (because it properly renders it). he mesh is "The City" from this link:

http://tf3dm.com/3d-model/the-city-39441.html

 

The problematic group is g_bldgs2 (i cut the other subsets, the rest are rendered properly), this is how it should look like:

http://i.imgur.com/Qkr7Esy.png

 

I attach the rar with .obj and .mtl

 

And how it looks like here http://i.imgur.com/JI6BhVH.jpg

 

I already added tons of debug prints, it says that it uses the "cty2x.jpg" for that group which is the proper one. Can anyone reproduce the problem? I've literally no idea what may be wrong. I downloaded 5 additional meshes from the internet and they render properly, but the city texture problem...

 

The weird thing in this mesh i see is that there is one "g" for group and usemtl changes inside without "g" change- i split that into two separate groups with separate materials

 

I just read the topic carefully again and thats exactly what efolkertsma is talkin about. But im still bit confused (and why there is not "g" in your example? That would crash my parser... :) ). But anyways, after removing unnecessary groups in the attached file there is no usemtl inside group amd the file looks "clean", but still renders incorrectly.

From what i see, i can completely ignore "g" and separate the groups basing on material, right? Or maybe you recommend any other, common well documented format?

 

Greetz

Edited by QQemka

Share this post


Link to post
Share on other sites

Okay so i spent today optimising my converter, and actually ALL the textures are mapped wrong way. It was just impression of "good looking houses".

 

This is how it looks here http://i.imgur.com/PVDd6zr.jpg

While it should like http://i.imgur.com/ZNBqI9L.png

 

So the conclusion is it is not a tiny mistake but some bigger issue with UV mapping i am unaware of. I went through the mesh tutorial once again, but well... i am fed the UVs from the file and i just pass it to the shader. What may be wrong?

 

BINGO!

texCoord.v = 1.0f - it.texCoord.v;

 

Now it maps proerply. Why?? Is it something with left/right handed issue? Can someone explain why reversing the v coordinate fixed the problem?

 

Okay, i got the maths behind lh and rh.... :) Well I wish i knew it 2 days before :)

Edited by QQemka

Share this post


Link to post
Share on other sites

Good to hear that you resolved your issue. I thought about the material mapping since you had stated that you had some objects come in correctly, which in my mind excluded the UV mapping.

 

Anyways, here is a reasonable explanation for it http://thedev-log.blogspot.ca/2012/07/texture-coordinates-tutorial-opengl-and.html

 

TL;DR version: In DirectX top-left is (0,0) in OpenGL bottom-left is (0,0). This has to do with DirectX being more centered around memory -> screen layout (address 0 is top-left) whereas OpenGL is taking the more mathematical approach of x,y axis which means y increases upwards, aka bottom-left is (0,0)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!