complexObj.setAppearance(paneAppearance) geht nicht

Status
Nicht offen für weitere Antworten.

Dominicana

Aktives Mitglied
Hallo,

habe mir ein komplexes 3D-Object zusammengestellt, dem ich jetzt eine bestimmte Oberflächen-Farbe zuordnen möchte. Das sollte ja mit :
Code:
paneAppearance.setMaterial(new Material(new Color3f(0f,0f,1f),new Color3f(0f,0f,0f),new Color3f(1f,0f,0f),new Color3f(1f,1f,1f),100f));
funktionieren (in einem anderen Beispiel ergibt das ein sattes Violet). Trotzdem erhalte ich,auch bei Änderungen, immer ein weißes Objekt.
Hier die ganze Methode, in der alles stehen sollte :
Code:
public BranchGroup createSceneGraph() {
        BranchGroup rootBG = new BranchGroup();
        TransformGroup paneTG = new TransformGroup();
        Transform3D paneT3D = new Transform3D();
        Appearance paneAppearance = new Appearance();
        DirectionalLight dLight = new DirectionalLight(new Color3f(0.1f,0.1f,0.1f),new Vector3f(-0.5f,-0.5f,-1f));
        AmbientLight aLight = new AmbientLight(new Color3f(0.1f,0.1f,0.1f));
        BoundingSphere bigBounds = new BoundingSphere(new Point3d(),100000);
        Shape3D complexObj;
        aLight.setInfluencingBounds(bigBounds);
        dLight.setInfluencingBounds(bigBounds);
        paneT3D.setTranslation(new Vector3f(0f,0f,-1.5f));
        paneT3D.setRotation(new AxisAngle4f(0f,0f,1f,(float)Math.toRadians(0)));
        paneT3D.setScale(1.0);
        paneTG.setTransform(paneT3D);
        paneAppearance.setMaterial(new Material(new Color3f(0f,0f,1f),new Color3f(0f,0f,0f),new Color3f(1f,0f,0f),new Color3f(1f,1f,1f),100f));
        complexObj = new MyShape3DPanel().getPanel3D();
        complexObj.setAppearance(paneAppearance);
        paneTG.addChild(complexObj);
        rootBG.addChild(paneTG);
        rootBG.addChild(dLight);
        rootBG.addChild(aLight);
        rootBG.compile();
        return rootBG;
Sollte daran etwas falsch sein?

Dom.
 

Illuvatar

Top Contributor
Was mach denn die new MyShape3DPanel().getPanel3D(); Methode? Ich bin mir grade nicht sicher, aber es kann sein, dass du dafür bei der Geometry Normals erzeugen lassen haben musst.
 

Dominicana

Aktives Mitglied
"MyShape3DPanel" ist eine Klasse , die ich nicht komplett wiedergeben möchte, da sie ziemlich groß ist. Hier ist ein Ausschnitt :
Code:
import javax.media.j3d.Shape3D;
import javax.media.j3d.IndexedTriangleArray;
import javax.vecmath.Point3f;

class MyShape3DPanel {
    Shape3D s3D = new Shape3D();
    Point3f[] coordArray;
    int[] vertexInd;
    IndexedTriangleArray triArray = new IndexedTriangleArray(452,IndexedTriangleArray.COORDINATES,2700); //452 , 900
    MyShape3DPanel() {
        Point3f[] coordArray = {new Point3f(-6.621269f,-6.587365f,-0.200000f),
                                new Point3f(-6.621268f,-6.827234f,-0.100000f),
.
.
.
                                new Point3f( 0.626845f,-0.904152f, 0.200000f)};
        int[] vertexInd = { 404, 405, 451,   450, 403, 404,   450, 404, 451,   403, 450, 402,   402, 450, 449,
.
.
.
                              2,  24,  23,     2,  23,   1,    22,   0,   1,    22,   1,  23,    22,   5,   0};
        triArray.setCoordinates(0,coordArray);
        triArray.setCoordinateIndices(0,vertexInd);
        s3D.addGeometry(triArray);
        this.coordArray = coordArray;
        this.vertexInd = vertexInd;
    }
    public Shape3D getPanel3D() {
        return s3D;
    }
}
Wie man sieht, wird hier "nur" das Objekt erstellt. Im Tutorial von Michael Pfeifer wurde es so beschrieben. Allerdings sieht der Pfeil , der dort als Beispiel dargestell wird , auch nur weiß aus.
Muß ich hier die Flags setzen?? ???:L
 

Dominicana

Aktives Mitglied
Habe mir nochmal die Doku angeschaut und festgestellt , daß man dem IndexedTriangleArray einen "NORMALS"-Flag mitgeben kann :
Code:
IndexedTriangleArray triArray = new IndexedTriangleArray(452,IndexedTriangleArray.COORDINATES|IndexedTriangleArray.NORMALS,2700);
Jetzt ist das Objekt nicht mehr weiß, sondern blau, allerdings sieht es aus wie eine Fläche und nicht wie ein 3D-Objekt.
 

Dominicana

Aktives Mitglied
Da sich ja niemand für dieses Thema interressiert, brauche ich die Lösung des Textur-Problems nicht zuposten und der Thread kann geschlossen werden. :bahnhof:
 

Illuvatar

Top Contributor
Wer sagt denn, dass sich keiner interessiert ???:L

Ich wusste es halt einfach nicht... aber die Lösung würde mich schon interessieren.
 

Dominicana

Aktives Mitglied
Nun Illuvatar, da wenigstens du hier etwas gepostest hast, gebe ich dir einen Tip! Aber nur einen.
Ich hab das ganze Internet nach Infos zu dem Thema "setTextureCoordinates(); " und "setTextureCoordinateIndices(); auf ein Shape3D abgesucht. Nur 600 Einträge hab ich gefunden und ÜBERALL nur oberflächliche Beschreibungen der Methode, bis ich dann auf ein anderes Java-Forum stieß, in dem gerade dieses Thema behandelt wurde. Dabei viel mir dieser Code auf:
Code:
import javax.swing.JFrame; 
import javax.media.j3d.*; 
import com.sun.j3d.utils.universe.*; 
import javax.vecmath.*; 
import com.sun.j3d.utils.geometry.*; 
import com.sun.j3d.utils.image.*; 
import java.io.*; 

import java.awt.BorderLayout; 
import java.awt.GraphicsConfiguration; 


public class Main extends JFrame{ 

        private SimpleUniverse un; 

        public Main() { 
                setLayout(new BorderLayout()); 
                setSize(500,500); 
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                
                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); 
                
                Canvas3D c=new Canvas3D(config); 
                getContentPane().add(c); 
                
                un = new SimpleUniverse(c); 
                    un.getViewingPlatform().setNominalViewingTransform(); 
                    un.addBranchGraph(createSceneGraph()); 
                setViewPosition(); 
                    
                setVisible(true); 
        } 
        
        
        private BranchGroup createSceneGraph() { 
                BranchGroup bg = new BranchGroup(); 
                BoundingSphere bndSphere = new BoundingSphere(new Point3d(0f,0f,0f),100000); 
                
                /////////////////////////////////////////////////// 
                // LIGHT 
                /////////////////////////////////////////////////// 
                Transform3D dLT3D = new Transform3D(); 
                        dLT3D.setTranslation(new Vector3f(0f,0f,2f)); 
                TransformGroup dLTG = new TransformGroup(dLT3D); 
                DirectionalLight dLight = new DirectionalLight(new Color3f(1f,0f,0f), new Vector3f(0f,0f,-1f)); 
                        dLight.setInfluencingBounds(bndSphere); 
                dLTG.addChild(dLight); 
                bg.addChild(dLTG); 
                
                
                
                Point3f[] points = { 
                        new Point3f(1f,0f,1f), 
                        new Point3f(-1f,0f,1f), 
                        new Point3f(0f,0f,-1f), 
                        new Point3f(0f,1f,0f), 
                }; 
                int[] indices = {0,2,1, 1,3,0, 0,3,2, 2,3,1}; 
                
                Point2f[] texPoints = { 
                        new Point2f(0f,0f), 
                        new Point2f(1f,0f), 
                        new Point2f(1f,1f), 
                        new Point2f(0f,1f), 
                }; 
                int[] texIndices = {0,2,1, 0,2,1, 0,2,1, 0,2,1}; 
                
                IndexedTriangleArray tri = new IndexedTriangleArray(4, IndexedTriangleArray.COORDINATES | 
                                      IndexedTriangleArray.TEXTURE_COORDINATE_2, 12); 
                        tri.setCoordinates(0, points); 
                        tri.setCoordinateIndices(0, indices); 
                        tri.setTextureCoordinates(0, texPoints); 
                        tri.setTextureCoordinateIndices(0, texIndices); 
                
                TextureLoader loader = new TextureLoader("./img/035.jpg", null); 
                Texture2D tex = (Texture2D)loader.getTexture(); 
                
                Appearance app = new Appearance(); 
                        app.setTexture(tex); 
                        app.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL,                      
                                         PolygonAttributes.CULL_NONE, 0f)); 
                        
                Transform3D rotT3D = new Transform3D(); 
                        rotT3D.rotX(Math.toRadians(30)); 
                TransformGroup rotTG = new TransformGroup(rotT3D); 
                
                Transform3D spinT3D = new Transform3D(); 
                        spinT3D.rotY(Math.toRadians(2)); 
                TransformGroup spin = new TransformGroup(); 
                        spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
                        spin.addChild(new Shape3D(tri, app)); 
                
                Alpha a = new Alpha(-1, 4000); 
                RotationInterpolator interp = new RotationInterpolator(a, spin); 
                        interp.setTransformAxis(spinT3D); 
                        interp.setSchedulingBounds(bndSphere); 
                spin.addChild(interp); 
                
                        
                rotTG.addChild(spin); 
                bg.addChild(rotTG); 
                
                bg.compile(); 
                return bg; 
        } 
        
    private void setViewPosition() { 
            TransformGroup ViewTG; 
            Transform3D  ViewT3D = new Transform3D(); 
    
            ViewTG        = un.getViewingPlatform().getViewPlatformTransform(); 
                    ViewTG.getTransform(ViewT3D); 
                    ViewT3D.setTranslation(new Vector3f(0f,0f,8f)); 
            ViewTG.setTransform(ViewT3D); 
    } 

        public static void main(String[] args) { 
                new Main(); 
        } 

}
man muß sich die Zeilen 69-74 anschauen!!! DAS ist die Lösung, allerdings wurde sie dort im Forum NICHT erkannt , und durch falsches setzen der Appearance-Werte in diesem Code wieder versaut! Weitere Versuche gingen immer weiter vom eigentlichen Ziel weg:
Code:
import javax.media.j3d.*; 
import javax.vecmath.*; 
import com.sun.j3d.utils.geometry.*; 
import com.sun.j3d.utils.image.TextureLoader; 

public class Pyrimid extends Shape3D { 
        
        Point3f[] points = {        new Point3f(1f,0f,1f), new Point3f(-1f,0f,1f), 
                                                        new Point3f(0f,0f,-1f), new Point3f(0f,1f,0f) }; 
        int[] indices = {0,2,1, 1,3,0, 0,3,2, 2,3,1}; 
        
        public Pyrimid() { 
                        IndexedTriangleArray tri = new IndexedTriangleArray(4, IndexedTriangleArray.COORDINATES | IndexedTriangleArray.TEXTURE_COORDINATE_2, 12); 
                                tri.setCoordinates(0, points); 
                                tri.setCoordinateIndices(0, indices); 
                
                        GeometryInfo geoInf = new GeometryInfo(tri); 
                        
                        NormalGenerator normGe = new NormalGenerator(); 
                                normGe.generateNormals(geoInf); 
                                
                        Stripifier st = new Stripifier(); 
                           st.stripify(geoInf); 
                                
                        IndexedTriangleStripArray geo = (IndexedTriangleStripArray)geoInf.getIndexedGeometryArray(); 
                
                        TextureLoader texLoader = new TextureLoader("./img/cobblestone.gif", null); 
                        
                        TextureUnitState tuState[] = {new TextureUnitState()}; 
                        tuState[0].setTexture(texLoader.getTexture()); 

                        TexCoordGeneration texGen = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2); 
                        tuState[0].setTexCoordGeneration(texGen); 
                        
                        TextureAttributes texAtt = new TextureAttributes(TextureAttributes.REPLACE, new Transform3D(), new Color4f(1f,1f,1f,0f), TextureAttributes.NICEST); 
                        tuState[0].setTextureAttributes(texAtt); 
                        
                        Appearance app = new Appearance(); 
                                app.setTextureUnitState(tuState); 
                                app.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0f)); 
                                
                        this.setGeometry(geo); 
                        this.setAppearance(app); 
        } 
}
Tja, wäre schön wenn uns irgendwer helfen könnte :/ Oxy zum Beispiel ???

mfg
jagdfalke
Also stehen sie wieder da, wo sie angefangen hatten.

Nach weiterem Googeln, jetzt mit einem richtigen Ziel vor Augen, fand ich diesen Code, der die Absolute Lösung ist :
Code:
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.image.TextureLoader;
 
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.vecmath.TexCoord2f;
import javax.vecmath.Point3f;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
/**
 * Quick draw function:
 * f(x, y) = (x^2 + 3*y^2) * exp(1-(x^2 + y^2))
 *
 * @author T55555
 * @version 2004-08-16
 */
public class MathGeom extends JFrame {
    public static void main(String[] args) {
        new MathGeom().show();
    }
 
    public MathGeom() {
        super("z = (x^2 + 3*y^2) * exp(1-(x^2 + y^2)) ----- By T55555");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(450, 450);
        setLocationRelativeTo(null);
        Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        getContentPane().add(canvas3D);
 
        SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
        simpleU.getViewingPlatform().setNominalViewingTransform();
 
        TextureLoader loader = new TextureLoader("T55555.jpg", this);
        ImageComponent2D image = loader.getImage();
        Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,image.getWidth(), image.getHeight());
        texture.setImage(0, image);        
 
        BranchGroup objRoot = new BranchGroup();
        Appearance appearance = new Appearance();
        appearance.setTexture(texture);
        TextureAttributes textureAttributes = new TextureAttributes();
        textureAttributes.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
        appearance.setTextureAttributes(textureAttributes);
        objRoot.addChild(new Shape3D(createGeometry(), appearance));
        
        objRoot = addMouseBehavior(objRoot);        
        objRoot.compile();
        simpleU.addBranchGraph(objRoot);
    }
 
    private Geometry createGeometry() {
        int nx = (int) ((2.5 * 2) / 0.125 + 1);
        int ny = (int) ((2.5 * 2) / 0.125 + 1);
        Point3f coords[] = new Point3f[nx * ny];
        float z =0f;        
        int count = 0;
        for (float y = -2.5f; y <= 2.5f; y += 0.125f) {
            for (float x = -2.5f; x <= 2.5f; x += 0.125f) {
                float xx = x * x;
                float yy = y * y;
                z = (float) ((xx + 3 * yy) * Math.exp(1 - (xx + yy)));
                coords[count++] = new Point3f(x/2.5f, y/2.5f, z/4f);
            }
        }
        int[] indices = new int[(nx-1) * (ny-1) * 4];        
        count = 0;
        for (int y = 0, endY = ny - 1; y < endY; y++) {
            for (int x = 0, endX = nx - 1; x < endX; x++) {
                int v = nx * y + x;
                indices[count++] = v;
                indices[count++] = v + 1;
                v += nx;
                indices[count++] = v + 1;
                indices[count++] = v;
            }
        }
 
        count = 0;
        TexCoord2f texCoords[] = new TexCoord2f[nx * ny];
        for (int x = 0; x < nx; x++) {
            for (int y = 0; y < ny; y++) {
                texCoords[count++] = new TexCoord2f(((float)x)/(nx-1), ((float)y)/(ny-1));
            }
        }        
        int[] indices2 = new int[(nx-1) * (ny-1) * 4];
        System.arraycopy(indices, 0, indices2, 0, indices.length);            
            
        IndexedQuadArray iqa = new IndexedQuadArray(coords.length, IndexedQuadArray.COORDINATES | IndexedQuadArray.TEXTURE_COORDINATE_2, indices.length);
        iqa.setCoordinates(0, coords);
        iqa.setCoordinateIndices(0, indices);
        iqa.setTextureCoordinates(0, 0, texCoords);
        iqa.setTextureCoordinateIndices(0, 0, indices2);
        return iqa;
    }    
    
    private BranchGroup addMouseBehavior(BranchGroup child) {
        BranchGroup objRoot = new BranchGroup();
        BoundingSphere boundingSphere = new BoundingSphere();
        TransformGroup tg = new TransformGroup();
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        objRoot.addChild(tg);
        tg.addChild(child);
 
        MouseRotate mouseRotate = new MouseRotate();
        mouseRotate.setTransformGroup(tg);
        mouseRotate.setSchedulingBounds(boundingSphere);
        objRoot.addChild(mouseRotate);
 
        MouseTranslate mouseTranslate = new MouseTranslate();
        mouseTranslate.setTransformGroup(tg);
        mouseTranslate.setSchedulingBounds(boundingSphere);
        objRoot.addChild(mouseTranslate);
        
        MouseZoom mouseZoom = new MouseZoom();
        mouseZoom.setTransformGroup(tg);
        mouseZoom.setSchedulingBounds(boundingSphere);
        objRoot.addChild(mouseZoom);
 
        return objRoot;
    }
}
Das ganze funktioniert allerdings NUR mit einem Bild mit 2^n Pixel * 2^n Pixel (also 2*2,4*4,8*8,16*16,...1024*1024,2048*2048,.....usw und so fort. Schau dir den Code an, und mach mal ein Tutorial für die anderen, du hast ja Erfahrung :wink: Wenn du Fragen hast, ich helfe dir. Beispiele hab ich mir jetzt auch schon erstellt : Beispiel 1 mit einer Textur und Beispiel 2 mit 2 Texturen . Und wer will, dem kann ich noch ein Applet schicken, dem man eine *.obj-Datei und ein *.jpg übergeben kann.
 

jagdfalke

Bekanntes Mitglied
Lass mich raten, deine Lights sind so fast schon schwarz, dass sie kaum Einfluss auf das Objekt haben???

mfg
jagdfalke
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben