views:

1454

answers:

2

Hi,

an artist friend has sent me an .obj file exported by 3DS Max 2009 which contains three texture coordinates as parameters to the vt command. And that's correct according to .obj specification. However, I'm not sure how to map U-V-W coordinates that are provided for a regular 2D .jpg texture.

This is relatively important for me, since I've played with 3DS Max trying to figure out how to force it to export just U-V coordinates, but there appears to be no straightfoward way. In this case this is just a skybox, but in other cases it might be something way more complex and not hand-fixable.

Thanks!

+1  A: 

Hi,

sorry if I misunderstand your question. But could't you simply ignore the third parameter, w? As I understand it, w is just a coordinate in a plane perpendicular to the plane described by u and v, so it is rarely needed for simple 2d texture mapping.

hope this helps.

david h
That's also the way I understand it, however, it may contain some more semantic information. Otherwise, why export it if it's clearly visible it's 2D mapping, like 3ds max 2009 does?
Ivan Vučica
+2  A: 

There's a section in the article What Is UVW Mapping? that explains:

You might question why you need a depth coordinate like W for a 2D plane. One reason is because it's sometimes useful to be able to flip the orientation of a map, relative to its geometry. To do this, you need the third coordinate. The W coordinate also has a meaning for 3-dimensional procedural materials.

In your case, you can simply ignore the W coordinate and only read the first two floats of lines that begin with vt. This assumes that you're not very worried about the extra space taken by the W coordinate since .obj is quite an inefficient file format to begin with.

Personally I don't like using .obj in OpenGL because it provides vertex normals per face instead of per vertex. To light an object properly you must either duplicate vertices or calculate normals by averaging the surrounding vertex normals in a face. If you want to explore more efficient alternatives, take a look at binary formats, such as the thoroughly documented .md2 format. Also consider using glDrawElements if you're not utilizing it already.

Kai
Thanks! I'm utilizing vertex arrays + display lists already (a space sim needs very little animation). I'll mark your answer as accepted, as soon as you EDIT one little thing: You're WRONG about .obj not providing VERTEX NORMALS :-) See the "vn" command and usage of vertex normals in "f": you specify a normal per vertex, not per face. You're mixing up .obj with .3ds in this case. Besides, I'm going with .obj for now, but I'll make a transition later on to something else. Perhaps I'll even use .obj as an intermediate format: another reason to know all the details of it.
Ivan Vučica
When I was using .obj files I found that the number of "vn" entries always exceeds the number of "v" entries. Export a simple model and check this for yourself. This is because .obj provides several normals per vertex for different faces. That means that an 8 vertex cube could have up to 24 "vn" entries, one for each vertex's adjoining face. To have a one-to-one correspondence with vertices and their normals, you must do the calculation I mentioned previously. Alternatively, you can duplicate a vertex for every face it shares and then use all 24 "vn" entries. I hope that's clearer!
Kai
Here's some reputation for you :)Ah yes, that's right. But it MUST provide several normals in case of sharp objects, such as cubes. I don't want a cube mapped smoothly, I want it to be mapped sharply! And that's what OBJ allows you to do. Averaging them is a completely wrong way to approach it; so yes, you must duplicate them and optimize them (if you wish). But the real reason why OBJ is problematic for OpenGL is that you need several indices for single face vertex; yet OpenGL doesn't support this. You'd need index array for indices... you see my point. THat's what annoys me; normals are ok
Ivan Vučica
Yes, we're on the same page :D I'm glad that I'm not the only one who person who's somewhat annoyed with the .obj format in Open GL, but if sharp edges is what you want then it makes sense. Good luck!
Kai
@Kai, @Ivan, is there are format that supports both sharp and smooth objects that's convenient for OpenGL? I'm stuck with .obj for now, but I'm still curious...
Drew Noakes
BTW I think the .obj file has the concept of smoothing groups to address what you're discussing here.
Drew Noakes