W
walk_on_deadline
Gast
Hallo Leute,
mein Problem ist bestimmt ganz simple aber im Moment stehe ich da echt auf dem Schlauch, irgendwie platziere ist die TextureCoordinate's nicht richtig (oder zumindest glaube ich das hier das Problem liegt). Ich habe das ganze Konzept der TextureCoordinate noch nicht so ganz verstanden. Logisch wäre für mich die Coordinaten auf den Eckpunkte von meinem Array zu legen, aber bei fast alle Tutorials liegen sie woanders, auffällig nie im negativen Bereich.
Ich hab mein Programm mal bis auf die nötigsten Teile zusammengestrichen damit ihr euch ein Bild von meiner Lage machen könnt. eigentlich will ich nur eine simple Textur auf der Oberseite des Würfel anbringen, aber ich krieg es einfach nicht hin.
Gruß Ole
mein Problem ist bestimmt ganz simple aber im Moment stehe ich da echt auf dem Schlauch, irgendwie platziere ist die TextureCoordinate's nicht richtig (oder zumindest glaube ich das hier das Problem liegt). Ich habe das ganze Konzept der TextureCoordinate noch nicht so ganz verstanden. Logisch wäre für mich die Coordinaten auf den Eckpunkte von meinem Array zu legen, aber bei fast alle Tutorials liegen sie woanders, auffällig nie im negativen Bereich.
Ich hab mein Programm mal bis auf die nötigsten Teile zusammengestrichen damit ihr euch ein Bild von meiner Lage machen könnt. eigentlich will ich nur eine simple Textur auf der Oberseite des Würfel anbringen, aber ich krieg es einfach nicht hin.
Gruß Ole
Code:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import javax.media.j3d.Background;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import java.awt.GraphicsConfiguration;
import java.util.Vector;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.media.j3d.Shape3D;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.Transform3D;
public class Car extends Applet {
//---- Attribute -----------------------
private SimpleUniverse universe;
private Canvas3D canvas3D;
private static Frame frame;
private BranchGroup szene;
private String backgroundImage;
private Background background = null;
private ImageComponent2D[] imageComponents = null;
public Car() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
canvas3D = new Canvas3D(config); // Die Erstellung des Canvas3D Objektes
add("Center", canvas3D);
TextureLoader myLoader = new TextureLoader(
"/home/ole/studium/Java3dTut/src/bg.jpg", this);
ImageComponent2D myImage = myLoader.getImage();
Background myBack = new Background();
myBack.setImage(myImage);
BoundingSphere myBounds = new BoundingSphere(new Point3d(), 1000.0);
myBack.setApplicationBounds(myBounds);
BranchGroup szene = macheSzene(); // content branch
szene.addChild(myBack);
szene.compile();
universe = new SimpleUniverse(canvas3D); // Erstellung des SimleUniverse
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(szene); // Einfügen des content branch ins SimpleUniverse
}
/**
* Erstellt den Szenegraphen
* @return BranchGroup
*/
public BranchGroup macheSzene() {
BranchGroup objRoot = new BranchGroup();
BoundingBox bounds = new BoundingBox();
//Car1
Transform3D t3d_car1 = new Transform3D();
TransformGroup tg_car1 = new TransformGroup(t3d_car1);
tg_car1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg_car1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg_car1.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
Shape3D car1 = new Shape3D(makeCube(), car1Appearance());
tg_car1.addChild(car1);
objRoot.addChild(tg_car1);
//Car2
// Transform3D t3d_car2 = new Transform3D();
// TransformGroup tg_car2 = new TransformGroup(t3d_car2);
// tg_car2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// tg_car2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
// tg_car2.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
// Shape3D car2 = new Shape3D(makeCube(), car2Appearance());
// tg_car2.addChild(car2);
// objRoot.addChild(tg_car2);
//
return objRoot;
}
public QuadArray makeCube() {
QuadArray plane = new QuadArray(24, GeometryArray.COORDINATES
| QuadArray.TEXTURE_COORDINATE_2); //
// Punkte
Point3f pa = new Point3f(-.1f, .05f, -.001f);
Point3f pb = new Point3f(-.1f, -.05f, -.001f);
Point3f pc = new Point3f(.1f, -.05f, -.001f);
Point3f pd = new Point3f(.1f, .05f, -.001f);
Point3f pe = new Point3f(-.1f, .05f, .001f);
Point3f pf = new Point3f(-.1f, -.05f, .001f);
Point3f pg = new Point3f(.1f, -.05f, .001f);
Point3f ph = new Point3f(.1f, .05f, .001f);
// 1. Seite
plane.setCoordinate(0, pa);
plane.setCoordinate(1, pb);
plane.setCoordinate(2, pc);
plane.setCoordinate(3, pd);
// 2. Seite
plane.setCoordinate(4, pe);
plane.setCoordinate(5, pf);
plane.setCoordinate(6, pg);
plane.setCoordinate(7, ph);
// 3. Seite
plane.setCoordinate(8, pe);
plane.setCoordinate(9, pf);
plane.setCoordinate(10, pb);
plane.setCoordinate(11, pa);
// 4. Seite
plane.setCoordinate(12, pd);
plane.setCoordinate(13, pc);
plane.setCoordinate(14, pg);
plane.setCoordinate(15, ph);
// 5. Seite
plane.setCoordinate(16, pe);
plane.setCoordinate(17, pa);
plane.setCoordinate(18, pd);
plane.setCoordinate(19, ph);
// 6. Seite
plane.setCoordinate(20, pf);
plane.setCoordinate(21, pb);
plane.setCoordinate(22, pc);
plane.setCoordinate(23, pg);
TexCoord3f q = new TexCoord3f();
//Seite 1
// q.set(pe);
// plane.setTextureCoordinate(0, 0, q);
// q.set(pf);
// plane.setTextureCoordinate(0, 1, q);
// q.set(pg);
// plane.setTextureCoordinate(0, 2, q);
// q.set(ph);
// plane.setTextureCoordinate(0, 3, q);
plane.setTextureCoordinate(0,0,new TexCoord2f(0.0f,0.0f));
plane.setTextureCoordinate(0,1,new TexCoord2f(0f,0.5f));
plane.setTextureCoordinate(0,2,new TexCoord2f(1f,0.5f));
plane.setTextureCoordinate(0,3,new TexCoord2f(1f,0.0f));
//Seite 2
// q.set(pe);
// plane.setTextureCoordinate(0, 4, q);
// q.set(pf);
// plane.setTextureCoordinate(0, 5, q);
// q.set(pg);
// plane.setTextureCoordinate(0, 6, q);
// q.set(ph);
// plane.setTextureCoordinate(0, 7, q);
//
// //Seite 3
// q.set(pe);
// plane.setTextureCoordinate(0, 8, q);
// q.set(pf);
// plane.setTextureCoordinate(0, 9, q);
// q.set(pb);
// plane.setTextureCoordinate(0, 10, q);
// q.set(pa);
// plane.setTextureCoordinate(0, 11, q);
//
// //Seite 4
// q.set(pd);
// plane.setTextureCoordinate(0, 12, q);
// q.set(pc);
// plane.setTextureCoordinate(0, 13, q);
// q.set(pg);
// plane.setTextureCoordinate(0, 14, q);
// q.set(ph);
// plane.setTextureCoordinate(0, 15, q);
//
// //Seite 5
// q.set(pe);
// plane.setTextureCoordinate(0, 16, q);
// q.set(pa);
// plane.setTextureCoordinate(0, 17, q);
// q.set(pd);
// plane.setTextureCoordinate(0, 18, q);
// q.set(ph);
// plane.setTextureCoordinate(0, 19, q);
//
// //Seite 5
// q.set(pf);
// plane.setTextureCoordinate(0, 20, q);
// q.set(pb);
// plane.setTextureCoordinate(0, 21, q);
// q.set(pc);
// plane.setTextureCoordinate(0, 22, q);
// q.set(pg);
// plane.setTextureCoordinate(0, 23, q);
// übergib den Würfel
return plane;
}
/**
* gibt speicher frei
*/
public void destroy() {
universe.removeAllLocales();
}
private Appearance car1Appearance() {
TexCoordGeneration textCoorder = new TexCoordGeneration(
TexCoordGeneration.OBJECT_LINEAR,
//TexCoordGeneration.EYE_LINEAR,
TexCoordGeneration.TEXTURE_COORDINATE_2);
//Load the texture from the external image file
TextureLoader textLoad = new TextureLoader("/home/ole/studium/Java3dTut/src/car_red.png", this);
//Access the image from the loaded texture
ImageComponent2D textImage = textLoad.getImage();
//Create a two dimensional texture
Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB,
textImage.getWidth(), textImage.getHeight());
//Set the texture from the image loaded
texture.setImage(0, textImage);
texture.setEnable(true);
//Create the appearance that will use the texture
Appearance app = new Appearance();
app.setTexture(texture);
//Pass the coordinate generator to the appearance
app.setTexCoordGeneration(textCoorder);
//Define how the texture will be mapped onto the surface
//by creating the appropriate texture attributes
TextureAttributes textAttr = new TextureAttributes();
textAttr.setTextureMode(TextureAttributes.MODULATE);
app.setTextureAttributes(textAttr);
return app;
}
// private Appearance car2Appearance() {
//
// Appearance a = new Appearance();
// TextureLoader loader = new TextureLoader(
// "/home/ole/studium/Java3dTut/src/car_green.png", this);
// Texture2D texture = (Texture2D) loader.getTexture();
// a.setTexture(texture);
// return a;
//
// }
public static void main(String[] args) {
frame = new MainFrame(new Car(), 550, 550);
frame.setTitle("Cartest");
}
}