Skip to main content

texture in a box - problem(wrong colours)

No replies
andy220
Offline
Joined: 2010-08-15
Points: 0

hello guys I want to create a box with a texture of chess board on top of it, but the problem is that it loads just a dark blue colour surface instead of blue and white squares surface,feel free to check what I mean, here is what the program shows
http://img97.imageshack.us/i/checkes.png/
and here the original texture I want to put in the box
http://img266.imageshack.us/i/arizonae.jpg/

here also is my code

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;

import javax.media.j3d.Transform3D;
import javax.swing.JFrame;

// BorderLayout stuff
import java.awt.*;
import javax.swing.*;

// Canvas3D
import javax.media.j3d.Canvas3D;

// The Universe
import com.sun.j3d.utils.universe.SimpleUniverse;

// The BranchGroup
import javax.media.j3d.BranchGroup;

// For the Box
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;

import javax.vecmath.*;

// The directional light
import javax.media.j3d.DirectionalLight;

// For the bouding sphere of the light source
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Appearance;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;

// Transformgroup
import javax.media.j3d.TransformGroup;
import com.sun.j3d.utils.behaviors.mouse.*;

public class vrlm extends JFrame {
/**
* The SimpleUniverse object
*/
protected SimpleUniverse simpleU;

/**
* The root BranchGroup Object.
*/
protected BranchGroup rootBranchGroup;

/**
* Constructor that consturcts the window with the given name.
*
* @param name
* The name of the window, in String format
*/
public vrlm(String name) {
// The next line will construct the window and name it
// with the given name
super(name);

// Perform the initial setup, just once
initial_setup();
}

/**
* Perform the essential setups for the Java3D
*/
protected void initial_setup() {
// A JFrame is a Container -- something that can hold
// other things, e.g a button, a textfield, etc..
// however, for a container to hold something, you need
// to specify the layout of the storage. For our
// example, we would like to use a BorderLayout.
// The next line does just this:
getContentPane().setLayout(new BorderLayout());

// The next step is to setup graphics configuration
// for Java3D. Since different machines/OS have differnt
// configuration for displaying stuff, therefore, for
// java3D to work, it is important to obtain the correct
// graphics configuration first.
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();

// construct the canvas.
Canvas3D canvas3D = new Canvas3D(config);

// And we need to add the "canvas to the centre of our
// window..
getContentPane().add("Center", canvas3D);

// Creates the universe
simpleU = new SimpleUniverse(canvas3D);

// First create the BranchGroup object
rootBranchGroup = new BranchGroup();
}

/**
* Adds a light source to the universe
*
* @param direction
* The inverse direction of the light
* @param color
* The color of the light
*/
public void addDirectionalLight(Vector3f direction, Color3f color) {
// Creates a bounding sphere for the lights
BoundingSphere bounds = new BoundingSphere();
bounds.setRadius(1000d);

// Then create a directional light with the given
// direction and color
DirectionalLight lightD = new DirectionalLight(color, direction);
lightD.setInfluencingBounds(bounds);

// Then add it to the root BranchGroup
rootBranchGroup.addChild(lightD);
}

/**
* Adds a box to the universe
*
* @param x
* x dimension of the box
* @param y
* y dimension of the box
* @param z
* z dimension of the box
*/

Appearance vasixrwma(){
Appearance vasixrwmaa = new Appearance();
//load the texture
String filename = "C:/Documents and Settings/Andy/Desktop/Arizona.jpg";
TextureLoader loader = new TextureLoader(filename, this);
ImageComponent2D image = loader.getImage();
if(image == null) {
System.out.println("load failed for texture: "+filename);
}
Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
image.getWidth(), image.getHeight());
texture.setEnable(true);
texture.setImage(0, image);
texture.setMagFilter(Texture.BASE_LEVEL_LINEAR);
texture.setMinFilter(Texture.BASE_LEVEL_LINEAR);

vasixrwmaa.setTexture(texture);
return vasixrwmaa;
}

public void addBox(float x, float y, float z, Color3f diffuse, Color3f spec, float a, float b, float c) {
// Add a box with the given dimension

// First setup an appearance for the box
Appearance app = new Appearance();
Material mat = new Material();
mat.setDiffuseColor(diffuse);
mat.setSpecularColor(spec);
mat.setShininess(5.0f);

app.setMaterial(mat);
Box box = new Box(x, y, z, app);
// Create a TransformGroup and make it the parent of the box
Transform3D meros = new Transform3D();
meros.setTranslation(new Vector3d(a, b, c));
TransformGroup tg = new TransformGroup(meros);
Appearance appear = vasixrwma();
box.getShape(Box.TOP).setAppearance(appear);
tg.addChild(box);

// Then add it to the rootBranchGroup
rootBranchGroup.addChild(tg);
//code for mouse navigation
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

MouseRotate myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(tg);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
rootBranchGroup.addChild(myMouseRotate);

MouseTranslate myMouseTranslate = new MouseTranslate();
myMouseTranslate.setTransformGroup(tg);
myMouseTranslate.setSchedulingBounds(new BoundingSphere());
rootBranchGroup.addChild(myMouseTranslate);

MouseZoom myMouseZoom = new MouseZoom();
myMouseZoom.setTransformGroup(tg);
myMouseZoom.setSchedulingBounds(new BoundingSphere());
rootBranchGroup.addChild(myMouseZoom);

// new code for key navigation
KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg);
keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(),1000.0));
rootBranchGroup.addChild(keyNavBeh);

}

/**
* Finalise everything to get ready
*/
public void finalise() {
// Then add the branch group into the Universe
simpleU.addBranchGraph(rootBranchGroup);

// And set up the camera position
simpleU.getViewingPlatform().setNominalViewingTransform();
}

public static void main(String[] argv) {
vrlm bc = new vrlm("checkers");

bc.setSize(250, 250);
bc.addBox(10f, 10f, 10f, new Color3f(0.8f, 0.52f, 0.25f), new Color3f(0.8f, 0.52f, 0.25f), 0.7f, -0.0415f, 0.7f);

bc.addDirectionalLight(new Vector3f(0f, 0f, -1),
new Color3f(4f, 4f, 0f));
bc.finalise();

bc.show();

return;
}
}

please everyone's help is appreciated, because its urgent, thank you very much!!!!

Message was edited by: andy220