3

I load a 3d model of a torus (obj file) into my program, using these lines:

Shape3D torus=null;
Scene t1 = getSceneFromFile("Some local path\torus.obj");

Then I grab the model from the scene using this code:

BranchGroup branchGroup = t1.getSceneGroup();
torus = (Shape3D) branchGroup.getChild(0);

The following piece of code set an image as texture and then apply that texture to an Appearance object.

TextureLoader textureLoader=new TextureLoader("Another local path\myImage.jpg",null);
ImageComponent2D image=textureLoader.getImage();
Texture2D texture=new Texture2D(Texture.BASE_LEVEL,Texture.RGBA,image.getWidth(),image.getHeight());
texture.setImage(0, image);

Appearance app = new Appearance();
app.setTexture(texture);

torus.setAppearance(app);

When I run the code the torus model is loaded correctly but the texture is not assigned correctly. More accurately, the whole 3d model has a single color instead of an image as texture. Mentioned color, is the color of the pixel at bottom-left of the image.

What's the solution? Thanks in advance.

Pouya
  • 1,266
  • 3
  • 18
  • 44
  • Do you need to set texture coordinates when using an Appearance? If so, that could be the problem. – S.L. Barth is on codidact.com Oct 04 '11 at 09:35
  • I thought about it but the question is "how?". I mean, I can set texture coordinates when it's a QuadArray or TriangleArray. But how to set texture coordinate for an imported complicated 3d model? – Pouya Oct 04 '11 at 09:46

1 Answers1

0

A Program, for example Mudbox That you use to create the obj file, also allows you to assign UV model images. Assign a paint layer than export it to the correct format. Thought this you can load the texture, you wont have the best detail to the obj. Try using this textur loader code instead.

static TextureLoader loader = new TextureLoader("C:\\Users\\Sawyera\\Desktop\\Paint Layer 1.jpg",
    "RGP", new Container());
static Texture texture = loader.getTexture();

   texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
Appearance ap = new Appearance();
ap.setTexture(texture);
ap.setTextureAttributes(texAttr);

Then take your 3d models name, example mines cleverly named model, and set apperance to ap or

model.setAppearance(ap);

This will load it almost 100% were you origanly had it. Loading a texture strait up is not going to load it to the desired coridants.

fftk4323
  • 130
  • 1
  • 12