Avatar billede fredand Forsker
15. august 2001 - 13:07 Der er 1 løsning

Problems with CULL_NONE in java 3D

Hello!

I\'m trying to get the \"PolygonAttributes.CULL_NONE\" to work in an 3D applet. But I can\'t get it right
<p>

I ad here 1a full class wich extends Shape3D wich in my opinion use \"PolygonAttributes.CULL_NONE\" wich also in my opinion shuld be solid.<p>

I also add the html-file to run it and an applet.
<b>When you have started the applet, start navigate with ARROW_DOWN-key. Then You will see the Shape3d</b> <p>

<br> The shape3d-class</b><p>
import java.applet.*;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.util.Enumeration;



public class Test3dBrick extends Shape3D
{

    public Test3dBrick()
    {
        createAppearance();
        addGeometry(getContour());
        addGeometry(getBody());
    }

    public void createAppearance()
    {
        Appearance appearance = new Appearance();
        PolygonAttributes polygonAttributes = new PolygonAttributes();
        polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
        polygonAttributes.setBackFaceNormalFlip(true);
        appearance.setPolygonAttributes(polygonAttributes);
        setAppearance(appearance);
    }


    public Geometry getContour()
    {
        Color3f darkgray = new Color3f(0.2f, 0.2f, 0.2f);

        IndexedLineArray indexedLineArray = new IndexedLineArray(8,  GeometryArray.COORDINATES | GeometryArray.COLOR_3 , 24);

        indexedLineArray.setCoordinate(0, new Point3f(  -0.201f, -0.001f,  -0.201f ));
        indexedLineArray.setCoordinate(1, new Point3f( -0.201f, -0.001f, 0.201f ));
        indexedLineArray.setCoordinate(2, new Point3f( 0.201f, -0.001f,  0.201f ));
        indexedLineArray.setCoordinate(3, new Point3f(  0.201f, -0.001f,  -0.201f ));

        indexedLineArray.setCoordinate(4, new Point3f(  -0.201f, 0.201f,  -0.201f ));
        indexedLineArray.setCoordinate(5, new Point3f( -0.201f, 0.201f, 0.201f ));
        indexedLineArray.setCoordinate(6, new Point3f( 0.201f, 0.201f,  0.201f ));
        indexedLineArray.setCoordinate(7, new Point3f(  0.201f, 0.201f,  -0.201f ));

        indexedLineArray.setCoordinateIndex( 0, 0);
        indexedLineArray.setCoordinateIndex( 1, 1);

        indexedLineArray.setCoordinateIndex( 2, 1);
        indexedLineArray.setCoordinateIndex( 3, 2);

        indexedLineArray.setCoordinateIndex( 4, 2);
        indexedLineArray.setCoordinateIndex( 5, 3);

        indexedLineArray.setCoordinateIndex( 6, 3);
        indexedLineArray.setCoordinateIndex( 7, 0);

        indexedLineArray.setCoordinateIndex( 10, 4);
        indexedLineArray.setCoordinateIndex( 11, 5);

        indexedLineArray.setCoordinateIndex( 12, 5);
        indexedLineArray.setCoordinateIndex( 13, 6);

        indexedLineArray.setCoordinateIndex( 14, 6);
        indexedLineArray.setCoordinateIndex( 15, 7);

        indexedLineArray.setCoordinateIndex( 16, 7);
        indexedLineArray.setCoordinateIndex( 17, 4);

        indexedLineArray.setCoordinateIndex( 8, 0);
        indexedLineArray.setCoordinateIndex( 9, 4);

        indexedLineArray.setCoordinateIndex( 18, 1);
        indexedLineArray.setCoordinateIndex( 19, 5);

        indexedLineArray.setCoordinateIndex( 20, 2);
        indexedLineArray.setCoordinateIndex( 21, 6);

        indexedLineArray.setCoordinateIndex( 22, 3);
        indexedLineArray.setCoordinateIndex( 23, 7);

        indexedLineArray.setColor(0, darkgray);

        return indexedLineArray;
    }

    public Geometry getBody()
    {
        Color3f lightgray = new Color3f(0.8f, 0.8f, 0.8f);

        IndexedQuadArray indexedQuadArray = new IndexedQuadArray(8,  GeometryArray.COORDINATES | GeometryArray.COLOR_3 , 24);

        indexedQuadArray.setCoordinate(0, new Point3f(  -0.2f, 0.0f,  -0.2f ));
        indexedQuadArray.setCoordinate(1, new Point3f( -0.2f, 0.0f, 0.2f ));
        indexedQuadArray.setCoordinate(2, new Point3f( 0.2f, 0.0f,  0.2f ));
        indexedQuadArray.setCoordinate(3, new Point3f(  0.2f, 0.0f,  -0.2f ));

        indexedQuadArray.setCoordinate(4, new Point3f(  -0.2f, 0.2f,  -0.2f ));
        indexedQuadArray.setCoordinate(5, new Point3f( -0.2f, 0.2f, 0.2f ));
        indexedQuadArray.setCoordinate(6, new Point3f( 0.2f, 0.2f,  0.2f ));
        indexedQuadArray.setCoordinate(7, new Point3f(  0.2f, 0.2f,  -0.2f ));

        indexedQuadArray.setCoordinateIndex( 0, 0);
        indexedQuadArray.setCoordinateIndex( 1, 1);
        indexedQuadArray.setCoordinateIndex( 2, 2);
        indexedQuadArray.setCoordinateIndex( 3, 3);

        indexedQuadArray.setCoordinateIndex( 4, 4);
        indexedQuadArray.setCoordinateIndex( 5, 5);
        indexedQuadArray.setCoordinateIndex( 6, 6);
        indexedQuadArray.setCoordinateIndex( 7, 7);

        indexedQuadArray.setCoordinateIndex( 8, 0);
        indexedQuadArray.setCoordinateIndex( 9, 4);
        indexedQuadArray.setCoordinateIndex( 10, 5);
        indexedQuadArray.setCoordinateIndex( 11, 1);

        indexedQuadArray.setCoordinateIndex( 12, 1);
        indexedQuadArray.setCoordinateIndex( 13, 5);
        indexedQuadArray.setCoordinateIndex( 14, 6);
        indexedQuadArray.setCoordinateIndex( 15, 7);

        indexedQuadArray.setCoordinateIndex( 16, 2);
        indexedQuadArray.setCoordinateIndex( 17, 6);
        indexedQuadArray.setCoordinateIndex( 18, 7);
        indexedQuadArray.setCoordinateIndex( 19, 3);

        indexedQuadArray.setCoordinateIndex( 20, 0);
        indexedQuadArray.setCoordinateIndex( 21, 4);
        indexedQuadArray.setCoordinateIndex( 22, 7);
        indexedQuadArray.setCoordinateIndex( 23, 3);

        indexedQuadArray.setColor(0, lightgray);

        return indexedQuadArray;

    }



}<p>
<b>The html-file:</b><p>
<html>
    <head></head>
    <body bgcolor=Yellow>
    <!--\"CONVERTED_APPLET\"-->
<!-- CONVERTER VERSION 1.3 -->
<OBJECT classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\"
WIDTH = \"600\" HEIGHT = \"400\"  codebase=\"http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0\">
<PARAM NAME = CODE VALUE = \"Shape3DTesterApplet.class\" >
<PARAM NAME = CODEBASE VALUE = \".\" >

<PARAM NAME=\"type\" VALUE=\"application/x-java-applet;version=1.3\">
<PARAM NAME=\"scriptable\" VALUE=\"false\">
<PARAM NAME = \"archive\" VALUE =\"java.zip\">
<COMMENT>
<EMBED type=\"application/x-java-applet;version=1.3\"  CODE = \"Shape3DTesterApplet.class\" CODEBASE = \".\" WIDTH = \"600\" HEIGHT = \"400\" archive = \"java.zip\"  scriptable=false pluginspage=\"http://java.sun.com/products/plugin/1.3/plugin-install.html\"><NOEMBED></COMMENT>

</NOEMBED></EMBED>
</OBJECT>


<!--\"END_CONVERTED_APPLET\"-->

    </body>
</html>

<p>

<b>The applet</b>
<p>
import java.applet.*;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.keyboard.*;


public class Shape3DTesterApplet extends Applet
{
    public void init()
    {

        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

        Canvas3D canvas3D = new Canvas3D(config);
        add(\"Center\", canvas3D);

        SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3D);

        BranchGroup branchGroup = new BranchGroup();

        branchGroup.addChild(addShape3D());


        TransformGroup transformGroup1 = simpleUniverse.getViewingPlatform().getViewPlatformTransform();
        KeyNavigatorBehavior keyNavigatorBehavior = new KeyNavigatorBehavior(transformGroup1);
        keyNavigatorBehavior.setSchedulingBounds(new BoundingSphere(new Point3d(-3.0f, -3.0f, -3.0f),10000.0));

        branchGroup.addChild(keyNavigatorBehavior);


        simpleUniverse.addBranchGraph(branchGroup);

    }

      public Shape3D addShape3D()
      {
        Test3dBrick test3dBrick = new Test3dBrick();
        return test3dBrick;
        }

}<p>

Best regards <br>
Fredrik
Avatar billede fredand Forsker
24. august 2001 - 12:40 #1
This did the trick!

import java.applet.*;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.util.Enumeration;



public class Test3dBrick extends Shape3D
{
    float x = 2;
    float y = 4;
    float z = 2;

    float posX = 0;
    float posY = 0;
    float posZ = 0;

    public Test3dBrick()
    {
        createAppearance();
        addGeometry(getContour());
        //addGeometry(getBody());
        addGeometry(getIndexedQuadArray());
    }

    public Test3dBrick(float x, float y, float z)
    {
        this.x = x/2;
        this.y = y;
        this.z = z/2;

        createAppearance();
        addGeometry(getContour());
        //addGeometry(getBody());
        addGeometry(getIndexedQuadArray());
    }

    public Test3dBrick(float x, float y, float z, Point3f point3f)
    {
        this.x = x/2;
        this.y = y;
        this.z = z/2;

        posX = point3f.x;
        posY = point3f.y;
        posZ = point3f.z;

        System.out.println(posX);
        System.out.println(posY);
        System.out.println(posZ);

        createAppearance();
        addGeometry(getContour());
        //addGeometry(getBody());
        addGeometry(getIndexedQuadArray());
    }

    public void createAppearance()
    {
        Appearance appearance = new Appearance();
        PolygonAttributes polygonAttributes = new PolygonAttributes();
        polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
        polygonAttributes.setBackFaceNormalFlip(true);
        appearance.setPolygonAttributes(polygonAttributes);
        setAppearance(appearance);
    }


    public Geometry getContour()
    {
        Color3f darkgray = new Color3f(0.3f, 0.3f, 0.3f);

        IndexedLineArray indexedLineArray = new IndexedLineArray(8,  GeometryArray.COORDINATES | GeometryArray.COLOR_3 , 24);

        indexedLineArray.setCoordinate(0, new Point3f(posX+(-x), posY+(-0.0f), posZ+(z) ));
        indexedLineArray.setCoordinate(1, new Point3f(posX+(-x), posY+(-0.0f), posZ+(-z) ));
        indexedLineArray.setCoordinate(2, new Point3f(posX+(x), posY+(-0.0f), posZ+(-z) ));
        indexedLineArray.setCoordinate(3, new Point3f(posX+(x), posY+(-0.0f), posZ+(z) ));

        indexedLineArray.setCoordinate(4, new Point3f(posX+(-x), posY+(-y), posZ+(z) ));
        indexedLineArray.setCoordinate(5, new Point3f(posX+(-x), posY+(-y), posZ+(-z) ));
        indexedLineArray.setCoordinate(6, new Point3f(posX+(x), posY+(-y), posZ+(-z) ));
        indexedLineArray.setCoordinate(7, new Point3f(posX+(x), posY+(-y), posZ+(z) ));

        /*
        indexedLineArray.setCoordinate(0, new Point3f(-x, -0.001f,  z ));
        indexedLineArray.setCoordinate(1, new Point3f(-x, -0.001f, -z ));
        indexedLineArray.setCoordinate(2, new Point3f( x, -0.001f, -z ));
        indexedLineArray.setCoordinate(3, new Point3f( x, -0.001f, z ));

        indexedLineArray.setCoordinate(4, new Point3f(-x, -y,  z ));
        indexedLineArray.setCoordinate(5, new Point3f(-x, -y, -z ));
        indexedLineArray.setCoordinate(6, new Point3f( x, -y, -z ));
        indexedLineArray.setCoordinate(7, new Point3f( x, -y,  z ));
        */

        indexedLineArray.setCoordinateIndex( 0, 0);
        indexedLineArray.setCoordinateIndex( 1, 1);

        indexedLineArray.setCoordinateIndex( 2, 1);
        indexedLineArray.setCoordinateIndex( 3, 2);

        indexedLineArray.setCoordinateIndex( 4, 2);
        indexedLineArray.setCoordinateIndex( 5, 3);

        indexedLineArray.setCoordinateIndex( 6, 3);
        indexedLineArray.setCoordinateIndex( 7, 0);

        indexedLineArray.setCoordinateIndex( 10, 4);
        indexedLineArray.setCoordinateIndex( 11, 5);

        indexedLineArray.setCoordinateIndex( 12, 5);
        indexedLineArray.setCoordinateIndex( 13, 6);

        indexedLineArray.setCoordinateIndex( 14, 6);
        indexedLineArray.setCoordinateIndex( 15, 7);

        indexedLineArray.setCoordinateIndex( 16, 7);
        indexedLineArray.setCoordinateIndex( 17, 4);

        indexedLineArray.setCoordinateIndex( 8, 0);
        indexedLineArray.setCoordinateIndex( 9, 4);

        indexedLineArray.setCoordinateIndex( 18, 1);
        indexedLineArray.setCoordinateIndex( 19, 5);

        indexedLineArray.setCoordinateIndex( 20, 2);
        indexedLineArray.setCoordinateIndex( 21, 6);

        indexedLineArray.setCoordinateIndex( 22, 3);
        indexedLineArray.setCoordinateIndex( 23, 7);

        indexedLineArray.setColor(0, darkgray);

        return indexedLineArray;
    }


    public Geometry getBody()
    {
        Color3f lightgray = new Color3f(0.8f, 0.8f, 0.8f);

        IndexedQuadArray indexedQuadArray = new IndexedQuadArray(8,  GeometryArray.COORDINATES | GeometryArray.COLOR_3 , 24);

        indexedQuadArray.setCoordinate(0, new Point3f(  -0.2f, 0.0f,  -0.2f ));
        indexedQuadArray.setCoordinate(1, new Point3f( -0.2f, 0.0f, 0.2f ));
        indexedQuadArray.setCoordinate(2, new Point3f( 0.2f, 0.0f,  0.2f ));
        indexedQuadArray.setCoordinate(3, new Point3f(  0.2f, 0.0f,  -0.2f ));

        indexedQuadArray.setCoordinate(4, new Point3f(  -0.2f, 0.2f,  -0.2f ));
        indexedQuadArray.setCoordinate(5, new Point3f( -0.2f, 0.2f, 0.2f ));
        indexedQuadArray.setCoordinate(6, new Point3f( 0.2f, 0.2f,  0.2f ));
        indexedQuadArray.setCoordinate(7, new Point3f(  0.2f, 0.2f,  -0.2f ));

        indexedQuadArray.setCoordinateIndex( 0, 0);
        indexedQuadArray.setCoordinateIndex( 1, 1);
        indexedQuadArray.setCoordinateIndex( 2, 2);
        indexedQuadArray.setCoordinateIndex( 3, 3);

        indexedQuadArray.setCoordinateIndex( 4, 4);
        indexedQuadArray.setCoordinateIndex( 5, 5);
        indexedQuadArray.setCoordinateIndex( 6, 6);
        indexedQuadArray.setCoordinateIndex( 7, 7);

        indexedQuadArray.setCoordinateIndex( 8, 0);
        indexedQuadArray.setCoordinateIndex( 9, 4);
        indexedQuadArray.setCoordinateIndex( 10, 5);
        indexedQuadArray.setCoordinateIndex( 11, 1);

        indexedQuadArray.setCoordinateIndex( 12, 1);
        indexedQuadArray.setCoordinateIndex( 13, 5);
        indexedQuadArray.setCoordinateIndex( 14, 6);
        indexedQuadArray.setCoordinateIndex( 15, 7);

        indexedQuadArray.setCoordinateIndex( 16, 2);
        indexedQuadArray.setCoordinateIndex( 17, 6);
        indexedQuadArray.setCoordinateIndex( 18, 7);
        indexedQuadArray.setCoordinateIndex( 19, 3);

        indexedQuadArray.setCoordinateIndex( 20, 0);
        indexedQuadArray.setCoordinateIndex( 21, 4);
        indexedQuadArray.setCoordinateIndex( 22, 7);
        indexedQuadArray.setCoordinateIndex( 23, 3);

        indexedQuadArray.setColor(0, lightgray);

        return indexedQuadArray;

    }

    public Geometry getIndexedQuadArray()
      {
        Color3f lightgray = new Color3f(0.8f, 0.8f, 0.8f);

        float x = this.x - 0.001f;
        float y = this.y - 0.001f;
        float z = this.z - 0.001f;

        IndexedQuadArray indexedQuadArray = new IndexedQuadArray(8, GeometryArray.COORDINATES | GeometryArray.COLOR_3, 24);

        indexedQuadArray.setCoordinate(0, new Point3f(posX+(-x), posY+(0.001f), posZ+(z) ));
        indexedQuadArray.setCoordinate(1, new Point3f(posX+(-x), posY+(0.001f), posZ+(-z) ));
        indexedQuadArray.setCoordinate(2, new Point3f(posX+(x), posY+(0.001f), posZ+(-z) ));
        indexedQuadArray.setCoordinate(3, new Point3f(posX+(x), posY+(0.001f), posZ+(z) ));

        indexedQuadArray.setCoordinate(4, new Point3f(posX+(-x), posY+(-y),  posZ+(z) ));
        indexedQuadArray.setCoordinate(5, new Point3f(posX+(-x), posY+(-y), posZ+(-z) ));
        indexedQuadArray.setCoordinate(6, new Point3f(posX+(x), posY+(-y), posZ+(-z) ));
        indexedQuadArray.setCoordinate(7, new Point3f(posX+(x), posY+(-y), posZ+(z) ));

        //Botten
        indexedQuadArray.setCoordinateIndex( 0, 0);
        indexedQuadArray.setCoordinateIndex( 1, 3);
        indexedQuadArray.setCoordinateIndex( 2, 2);
        indexedQuadArray.setCoordinateIndex( 3, 1);

        //Top
        indexedQuadArray.setCoordinateIndex( 4, 4);
        indexedQuadArray.setCoordinateIndex( 5, 5);
        indexedQuadArray.setCoordinateIndex( 6, 6);
        indexedQuadArray.setCoordinateIndex( 7, 7);

        //Höger sida
        indexedQuadArray.setCoordinateIndex( 8, 7);
        indexedQuadArray.setCoordinateIndex( 9, 6);
        indexedQuadArray.setCoordinateIndex( 10, 2);
        indexedQuadArray.setCoordinateIndex( 11, 3);

        //Vänster sida
        indexedQuadArray.setCoordinateIndex( 12, 0);
        indexedQuadArray.setCoordinateIndex( 13, 1);
        indexedQuadArray.setCoordinateIndex( 14, 5);
        indexedQuadArray.setCoordinateIndex( 15, 4);

        //Bak sida
        indexedQuadArray.setCoordinateIndex( 16, 4);
        indexedQuadArray.setCoordinateIndex( 17, 7);
        indexedQuadArray.setCoordinateIndex( 18, 3);
        indexedQuadArray.setCoordinateIndex( 19, 0);


        //Fram sida
        indexedQuadArray.setCoordinateIndex( 20, 5);
        indexedQuadArray.setCoordinateIndex( 21, 1);
        indexedQuadArray.setCoordinateIndex( 22, 2);
        indexedQuadArray.setCoordinateIndex( 23, 6);


        /*
        //Fram sida
        indexedQuadArray.setCoordinateIndex( 20, 6);
        indexedQuadArray.setCoordinateIndex( 21, 2);
        indexedQuadArray.setCoordinateIndex( 22, 1);
        indexedQuadArray.setCoordinateIndex( 23, 5);
        */


        /*//Ger ändå bara samma färg
        indexedQuadArray.setColor(0, new Color3f(1, 0, 0));
        indexedQuadArray.setColor(1, new Color3f(0, 1, 0));
        indexedQuadArray.setColor(2, new Color3f(0, 0, 1));
        indexedQuadArray.setColor(3, new Color3f(1, 1, 1));
        indexedQuadArray.setColor(4, new Color3f(1, 0, 1));
        indexedQuadArray.setColor(5, new Color3f(1, 1, 0));
        indexedQuadArray.setColor(6, new Color3f(0, 1, 1));
        indexedQuadArray.setColor(7, new Color3f(0, 0, 0));
        */

        indexedQuadArray.setColor(0, lightgray);
        /*//Endats den översta behövs
        indexedQuadArray.setColor(1, lightgray);
        indexedQuadArray.setColor(2, lightgray);
        indexedQuadArray.setColor(3, lightgray);
        indexedQuadArray.setColor(4, lightgray);
        indexedQuadArray.setColor(5, lightgray);
        indexedQuadArray.setColor(6, lightgray);
        indexedQuadArray.setColor(7, lightgray);
        */


        return indexedQuadArray;
    }




}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester