Textures on a .obj file?

I’m SICK of colored models! How can I get some textures in there? I’ve been using Blender (http://www.blender.com) to create my models, then exporting them along with the appropriate .mtl files and accompanying images. Why, why oh WHY? ???

Yes the .obj format does support texturing. Google for the spec. I found a copy a year or two back on some russian site (don’t think there now, but I’m sure there’s others out there) Originally the .obj format was defined by ‘wavefront’, which might refine your search.

Note that you can define sets of texture coordinates for each polygon. These map into the texture specified in the material file. Thus you can map a texture onto several polygons, which is handy if you want to skin a model, but more work if you’re writing your own renderer. If you are using OpenGL, then this maps fairly well onto OpenGLs texture coordinate system, although some tinkering will be required to get textures the right way round.

If you don’t care about texture mapping, you could stick with one texture per polygon with an arbritary orgin. This works better for wall and floor texturing anyway, as you can set the origin position to achieve seamless texturing for all polygons in the same plane. You still get discontinuities at corners, but then you can’t have everything.

Happy texturing
Alan

/Edit
Example: shamelessly ripped off from somewhere…


# A 2 x 2 square mapped with a 1 x 1 square
# texture stretched to fit the square exactly.
mtllib master.mtl
v  0.000000 2.000000 0.000000
v  0.000000 0.000000 0.000000
v  2.000000 0.000000 0.000000
v  2.000000 2.000000 0.000000
vt 0.000000 1.000000 0.000000
vt 0.000000 0.000000 0.000000
vt 1.000000 0.000000 0.000000
vt 1.000000 1.000000 0.000000
# 4 vertices
usemtl wood
# The first number is the point,
# then the slash,
# and the second is the texture point
f 1/1 2/2 3/3 4/4

The vt commands (vertex texture) define texture coordinates on the texture surface. They are referenced by the number after the ‘/’ in the f command (face).

/Edit2: Nuts I just realised that this is posted under Java3D. I haven’t tried mapping .obj textures onto java3d.
In a vain attempt to redeem myself here is some stuff scrounged from the web…

The standard FileObject loader does understand texture coords.
http://www.vrupl.evl.uic.edu/oldsite/VRLabAcc/about_vrlab/java3d/lesson08/indexa.html
There is also an obj file loader demo in the java3d jdk, called objLoad.
Documentation for the demo:
http://java3d.j3d.org/utilities/loaders/obj/sun.html

What do you uses to load OBJ files in java3d ?
The standard one is is not to well thought out IMO and I’m still looking for another one.
All you really need is a loader that returns some arrays, like indices, vertices and texturecoords to create your own
geometry in whatever library you want. Suns (i think) returns a scenegraph with a bunch of nodes and the worst part is,
you can’t read or write the geometry arrays since they are byRef.

There’s various loaders listed here http://java3d.j3d.org/utilities/loaders.html

The way I load OBJ files is from this tutorial:

http://www.vrupl.evl.uic.edu/oldsite/VRLabAcc/about_vrlab/java3d/

Click on Lesson 8a. No matter what I do I can’t seem to get anything more than different colors. If I need to do a sign or something I render the text as polygons.

Also, I believe the cokeandcode Asteroids tutorial uses OBJ files with textures. Very basic implementation and should provide a starting point.

I wrote my custom loader (don’t ask fot it, it’ very application specific). I recommend you do the same, it’s easy and gives you a lot of insight into how things work. you’ll find all the specs on .obj tags in the source of the bundled J3D obj loader (as comments). Don’t forget that for textures to work, you need to export your model with normals (and then load them). I have no idea whether texture loading works or not with the bundled loader.

How come people keep forgetting that this is a Java3D board? I don’t want to sound frustrated but Coke and Code showed me exactly what I wanted to do… in OpenGL. I’d really prefer to use Java3D.

Hey is that what you call OpenGL ? http://www.cokeandcode.com/node/305

More like the poster refers to this:

http://www.cokeandcode.com/node/324

</blatant plug>

Seriously tho, if you just want to apply a single texture to an OBJ file … load the OBJ, traverse the branch group it returns and apply the texture you want to each appearance node.

The OBJ format does support textures in the mtl file but you might find it easier to programmatically apply your own.

Kev

Sorry, it didn’t look like any Java3D I recognized but on closer inspection I guess it is.