Texture is not applied to OBJ model properly

Hello everyone.
I’m relatively new to java3d and I’m having a problem. I want to assign a texture (an image) to an obj 3d model. The problem is, when I run the code the 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. Here is the code:

Shape3D torus=null;
Scene t1 = getSceneFromFile("Some local path\torus.obj");
\\Grabbing the model from the scene
BranchGroup branchGroup = t1.getSceneGroup();
torus = (Shape3D) branchGroup.getChild(0);
\\loading the image, generating the texture and making the appearance
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);

How to solve this issue?

I’m pretty sure Java3D is pretty ancient now. You should try something else, like LWJGL. :wink:

For some reasons I have to use java3d. Do you have any suggestions that solves the problem in java3d?

No, sorry… Because of Java3D being so old, I don’t think you’ll be able to get much help here, but I may be wrong. :-\ I’ve only ever used LWJGL for 3D stuff… Do you have to enable texturing in some way? OpenGL has glEnable(GL_TEXTURE_2D), or you only get a single color on everything…

Hi

Have you tried to open your model with a 3D modeler like Blender?

Are you sure the path is correct? Are you sure you use the appropriate color component type? As far as I know, the loading of the image is asynchronous and you don’t use any observer.

Try to use TextureLoader.getTexture(). If it returns null, it means the loading of the image failed.

You say it displays the color of the pixel in the bottom left corner, maybe your texture coordinates are wrong.

I’m sorry, I’m not a specialist of Java3D. As theagentd said, Java3D is obsolete. You should rather use an API which looks like Java3D but which is still maintained like Xith3D. This engine has a JOGL renderer and a LWJGL renderer.

You have to set TextureCoordinates or let them generate by the engine for you (which doesn’t produce good results most of the time).
Here is a simple method for generating an appearance object with a given texture:


	/**
	 * Generate Apperance with given texture
	 * @param withTexCoordGeneration
	 * @param texture
	 * @param wrapTexture
	 * @return Appearance
	 */
	private Appearance generateAppearanceWithTexture(Texture2D texture, boolean withTexCoordGeneration) {
		// Create the appearance that will use the texture
		Appearance appearance = new Appearance();
		appearance.setTexture(texture);

		// If TexCoordGeneration is wanted then have it
		if (withTexCoordGeneration) {
			TexCoordGeneration texCoordGeneration = new TexCoordGeneration(TexCoordGeneration.NORMAL_MAP,
					TexCoordGeneration.TEXTURE_COORDINATE_2);
			appearance.setTexCoordGeneration(texCoordGeneration);
		}

		// Define how the texture will be mapped onto the surface
		// by creating the appropriate texture attributes
		TextureAttributes textAttr = new TextureAttributes();
		textAttr.setTextureMode(TextureAttributes.REPLACE);

		PolygonAttributes polygonAttributes = new PolygonAttributes();
		polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
		appearance.setPolygonAttributes(polygonAttributes);

		appearance.setTextureAttributes(textAttr);
		return appearance;
	}

Or another example which generates a quadarray (for a plane) with setting the TexCoodinates manually (see the .setTextureCoordinate() calls):


	/**
	 * Generate a QuadArray (for a plane) by giving the side of a cuboid and the dimension for all 3
	 * directions
	 * @param side
	 * @param x
	 * @param y
	 * @param z
	 * @param texCoordPoint (if null then TexCoords for full size will be set)
	 * @return QuadArray
	 */
	public static final QuadArray makePlaneForCuboid(int side, float x, float y, float z, Point2f texCoordPoint) {
		QuadArray plane = new QuadArray(24, GeometryArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);

		Point3f pa = new Point3f(-1.0f * x, 1.0f * y, 1.0f * z); // = upper corner
		Point3f pb = new Point3f(-1.0f * x, -1.0f * y, 1.0f * z);
		Point3f pc = new Point3f(1.0f * x, -1.0f * y, 1.0f * z);
		Point3f pd = new Point3f(1.0f * x, 1.0f * y, 1.0f * z);
		Point3f pe = new Point3f(-1.0f * x, 1.0f * y, -1.0f * z);
		Point3f pf = new Point3f(-1.0f * x, -1.0f * y, -1.0f * z);
		Point3f pg = new Point3f(1.0f * x, -1.0f * y, -1.0f * z); // = lower corner
		Point3f ph = new Point3f(1.0f * x, 1.0f * y, -1.0f * z);

		switch (side) {
		case 1:
			plane.setCoordinate(0, pa);
			plane.setCoordinate(1, pb);
			plane.setCoordinate(2, pc);
			plane.setCoordinate(3, pd);
			break;

		case 2:
			plane.setCoordinate(0, pe);
			plane.setCoordinate(1, pf);
			plane.setCoordinate(2, pg);
			plane.setCoordinate(3, ph);
			break;

		case 3:
			plane.setCoordinate(0, pe);
			plane.setCoordinate(1, pf);
			plane.setCoordinate(2, pb);
			plane.setCoordinate(3, pa);
			break;

		case 4:
			plane.setCoordinate(0, pd);
			plane.setCoordinate(1, pc);
			plane.setCoordinate(2, pg);
			plane.setCoordinate(3, ph);
			break;

		case 5:
			plane.setCoordinate(0, pe);
			plane.setCoordinate(1, pa);
			plane.setCoordinate(2, pd);
			plane.setCoordinate(3, ph);
			break;

		case 6:
			plane.setCoordinate(0, pf);
			plane.setCoordinate(1, pb);
			plane.setCoordinate(2, pc);
			plane.setCoordinate(3, pg);
			break;

		default:
			plane.setCoordinate(0, pa);
			plane.setCoordinate(1, pb);
			plane.setCoordinate(2, pc);
			plane.setCoordinate(3, pd);
			break;
		}

		if (texCoordPoint == null) {
		  plane.setTextureCoordinate(0, 0, new TexCoord2f(0f, 1f));
		  plane.setTextureCoordinate(0, 1, new TexCoord2f(0f, 0f));
		  plane.setTextureCoordinate(0, 2, new TexCoord2f(1f, 0f));
		  plane.setTextureCoordinate(0, 3, new TexCoord2f(1f, 1f));
		} else {
		  plane.setTextureCoordinate(0, 0, new TexCoord2f(0f, texCoordPoint.y));
		  plane.setTextureCoordinate(0, 1, new TexCoord2f(0f, 0f));
		  plane.setTextureCoordinate(0, 2, new TexCoord2f(texCoordPoint.x, 0f));
		  plane.setTextureCoordinate(0, 3, new TexCoord2f(texCoordPoint.x, texCoordPoint.y));
		}

		return plane;
	}

Xith3D hasn’t made a stable release since 2006, a beta since 2008, or a “cooker” since 2010. As the Magic 8-Ball says, Outlook Not So Good.