<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">//------------------------------------------------------------//
// Java3D_6.java - Copyright (C) 1996 by InWorld VR, Inc.     //
// All Rights Reserved.                                       //
//------------------------------------------------------------//
// Demo to show the stock objects and demonstrate flipping    //
// between shaded, wireframe and mixed mode.                  //
// User navigates with the mouse and keyboard.                //                                   //
//------------------------------------------------------------//
// Author - Jeffrey S. Donovan, InWorld VR, Inc.              //
// Last Revision - 05/29/97                                   //
//------------------------------------------------------------//

// Include nec. Java classes
import java.awt.*;
import java.awt.Image;
import java.awt.Event;

// Include InWorld's Java 3D API's
import inworld.spacecrafter.*;
import inworld.corespace.*;

// The actual demo
public class Java3D_6 extends java.applet.Applet implements Runnable{

    // Instance of a 2D CoreSpace(tm) graphics device
    CS_GDevice GDevice;
    // Instance of a 2D CoreSpace(tm) 2d graphic function engine
    CS_2DDevice G2DDevice;
    // Instance of a 3D CoreSpace(tm) flat shaded triangle engine
    CS_FSTDevice FlatShader;
    // Instance of a 3D CoreSpace(tm) polgon sorting device
    CS_PolySortDevice PolySorter;

    Thread runner;              // Thread for process
    boolean MouseDown=false;    // Was a mouse button pressed?
    byte    MouseWhere=0;       // Which mouse area pressed?

    World3D World;              // A 3D World.
    Viewpoint3D MyView;         // A 3D Viewpoint
    Light3D Light1,Light2;      // World lights

    Cube Obj1;                  // Mixed bag of 3D Objects
    Sphere Obj2;
    Hemisphere Obj3;
    Cone Obj4;
    Cylinder Obj5,Obj6;

    int BackWidth,BackHeight;   // Width and Height of world
    Button D3Buttons[];         // Render buttons

    boolean Running=true;

    // Used for flying objects in space
    float angle1=90.0f,angle2=180.0f,angle3=270.0f,angle4=360.0f,angle5=45.0f,angle6=270.0f;

    // init()
    //--------------------------------------------------------
    public void init(){
        System.out.println("Init Java 6");

        // Make the render buttons Buttons
        D3Buttons=new Button[3];
        D3Buttons[0]= (Button) add(new Button(" Shaded  "));
        D3Buttons[1]= (Button) add(new Button("Wireframe"));
        D3Buttons[2]= (Button) add(new Button("  Mixed  "));
    }

    // NewWorld() - Create new 3D Universe
    //--------------------------------------------------------
    public void NewWorld(){

        // Initiate a new 3d world
        World=new World3D(this);

        // Create a viewpoint and assign it to world.
        MyView=new Viewpoint3D();
        World.setViewpoint(MyView);

        // Set world dimensions
        World.set3DWindowDimensions(size().width,size().height);

        // Set clipping mode to FULL 3D Fullcrum. We'll need it when
        // objects fly into the viewer. A bit slower than the
        // fast model but accurate to the pixel which means we gain
        // some CPU time back by not needing any 2D clipping whatsoever,
        // as you need with the FAST fullcrum clipping model.
        // Note we can also set the hither plane down to 0, but we'll use 5.
        World.set3DClipType(World3D.CLIP3D_FASTFULCRUM);

        // Set the lighting mode, make 2 lights
        Light1=new Light3D(Light3D.POINTLIGHT);
        Light2=new Light3D(Light3D.POINTLIGHT);
        Light1.setDirectionDirect(-0.913913f,0.389759f,-0.113369f);
        Light2.setDirectionDirect( 0.013913f,-0.389759f,0.50f);
        Light1.setIntensity(1.0f);
        Light2.setIntensity(1.0f);

        // Set general world parameters
        World.set3DClipping(0,World.SCREEN_HEIGHT_LESSONE);
        World.setHitherYon(15,3500,World.SCREEN_HEIGHT);
        World.setAmbientLight(100);

        // Initialize the 2D CoreSpace(tm) Rendering Engine, True Color Model
        GDevice=new CS_GDevice_True(this);
        // Attach rendering engine to SpaceCrafter World
        World.setGraphicsDeviceEngine((CS_GDevice)GDevice);

        // Initialize the 2D CoreSpace(tm) Rendering Engine, True Color Model
        G2DDevice=new CS_2DDevice_True();
        // Attach rendering engine to SpaceCrafter World
        World.setGraphics2DDeviceEngine((CS_2DDevice)G2DDevice);

        // Initialize the 3D CoreSpace(tm) Flat Shaded Triangle Engine, True Color Model
        FlatShader=new CS_FSTDevice_True();
        // Attach triangle engine to SpaceCrafter world.
        World.setFlatShadedTriangleEngine((CS_FSTDevice)FlatShader);

        // Initialize the 3D CoreSpace(tm) polygon sorting engine
        PolySorter=new CS_PolySort_1();
        // Attach sorting engine to SpaceCrafter world.
        World.setPolygonSortingEngine((CS_PolySortDevice)PolySorter);


        World.setPaletteEntriesPerColor(0,255);

        // Object #1, 1'st cube in middle of world
        Obj1=new Cube(100.0f,100.0f,100.0f,false);
        Obj1.setPositionDirect(0.0f,0.0f,500.0f);
        Obj1.setColor(255,0,0,0);

        // Object #2, 2'nd cube in middle of world
        Obj2=new Sphere(100.0f,6,6,false);
        Obj2.setPositionDirect(-500.0f,0.0f,0.0f);
        Obj2.setColor(64,255,255,0);

        // Object #1, 1'st cube in middle of world
        Obj3=new Hemisphere(100.0f,5,5,true);
        Obj3.setPositionDirect(500.0f,0.0f,0.0f);
        Obj3.setColor(0,0,255,0);

        // Set objects 1,2 and 3 to wireframe mode so when
        // mixed mode is selected these will shade in wireframe.
        Obj1.setDrawMode(Object3D.DRAW_WIREFRAME);
        Obj2.setDrawMode(Object3D.DRAW_WIREFRAME);
        Obj3.setDrawMode(Object3D.DRAW_WIREFRAME);

        // Object #2, 2'nd cube in middle of world
        Obj4=new Cone(100.0f,50.0f,3,true,false);
        Obj4.setPositionDirect(0.0f,0.0f,-500.0f);
        Obj4.setColor(128,64,255,0);

        // Object #1, 1'st cube in middle of world
        Obj5=new Cylinder(70.0f,100.0f,5,false,false,true);
        Obj5.setPositionDirect(0.0f,-500.0f,0.0f);
        Obj5.setColor(255,255,255,0);

        // Object #2, 2'nd cube in middle of world
        Obj6=new Cylinder(200.0f,30.0f,5,true,true,false);
        Obj6.setPositionDirect(0.0f,500.0f,0.0f);
        Obj6.setColor(255,255,0,0);

        // Prep all for rendering.
        MyView.setPositionDirect(0.0f, 0.0f, -1500.0f);
        World.prepForRendering();
        World.prepForDisplay();

    }

    // start()
    //--------------------------------------------------------
    public void start(){
        setBackground(new Color(0x000000));
        showStatus("SpaceCrafter(tm) Ver 1.10 Copyright (C) 1996-1997 by InWorld VR, Inc. All Rights Reserved.");

        // Make a new world every time we start up. We will then
        // detroy it every time we stop so objects/lights/world is
        // actually deleted and not persistent.
        NewWorld();
        System.out.println("Start Java 6");

        if (runner==null){
            Running=true;
            runner=new Thread(this);
            runner.start();
        }
    }

    // stop()
    //--------------------------------------------------------
    public void stop(){

        Running=false;

        // Delete the World, set objects to null for Garbage Collector
        World.delete(true);
        Obj1=null;
        Obj2=null;
        Obj3=null;
        Obj4=null;
        Obj5=Obj6=null;
        Light1=Light2=null;
        MyView=null;
        World=null;

        // Make sure to shutdown all CoreSpace graphic drivers.
        GDevice.shutDownDevice();
        G2DDevice.shutDownDevice();
        FlatShader.shutDownDevice();
        PolySorter.shutDownDevice();

        GDevice=null;
        G2DDevice=null;
        FlatShader=null;
        PolySorter=null;

        if (runner!=null){
            System.out.println("Stop Java 6");
            runner.stop();
            try{runner.join();}
            catch(InterruptedException e){}
            runner=null;
        }
    }

    // ProcessMouse() - Do something with viewpoint
    //--------------------------------------------------------
    void ProcessMouse(){
        switch(MouseWhere){
            case 1:MyView.rotate(MyView.VIEW_FRAME,MyView.Y_AXIS,-3.0f);break;
            case 2:MyView.rotate(MyView.VIEW_FRAME,MyView.Y_AXIS, 3.0f);break;
            case 3:MyView.translate(MyView.VIEW_FRAME,MyView.X_AXIS,-10.0f);break;
            case 4:MyView.translate(MyView.VIEW_FRAME,MyView.X_AXIS, 10.0f);break;
            case 5:MyView.translate(MyView.VIEW_FRAME,MyView.Z_AXIS, 10.0f);break;
            case 6:MyView.translate(MyView.VIEW_FRAME,MyView.Z_AXIS,-10.0f);break;
            case 7:MyView.translate(MyView.WORLD_FRAME,MyView.Y_AXIS, 10.0f);break;
            case 8:MyView.translate(MyView.WORLD_FRAME,MyView.Y_AXIS,-10.0f);break;
        }
    }

    // run() - Here's where the action happens
    //--------------------------------------------------------
    public void run(){

        while ((runner!=null)&amp;&amp;(Running)){

            // Was a mouse button pressed?
            if (MouseDown){

                // Yes! Move or rotate viewer depending on where the mouse cursor is
                ProcessMouse();
            }

            // Spin 'em all
            Obj1.rotateMultiple(Object3D.WORLD_FRAME,10.0f,10.0f,10.0f);
            Obj2.rotateMultiple(Object3D.WORLD_FRAME,10.0f,10.0f,20.0f);
            Obj3.rotateMultiple(Object3D.WORLD_FRAME,20.0f,10.0f,10.0f);
            Obj4.rotateMultiple(Object3D.WORLD_FRAME,10.0f,20.0f,20.0f);
            Obj5.rotateMultiple(Object3D.WORLD_FRAME,20.0f,10.0f,10.0f);
            Obj6.rotateMultiple(Object3D.WORLD_FRAME,10.0f,20.0f,20.0f);

            // New angle amounts
            angle1+=2.0f;if (angle1 &gt; 360.0f) angle1-=360.0f;
            angle2+=4.0f;if (angle2 &gt; 360.0f) angle2-=360.0f;
            angle3+=6.0f;if (angle3 &gt; 360.0f) angle3-=360.0f;
            angle4+=8.0f;if (angle4 &gt; 360.0f) angle4-=360.0f;
            angle5+=6.0f;if (angle5 &gt; 360.0f) angle5-=360.0f;
            angle6+=8.0f;if (angle6 &gt; 360.0f) angle6-=360.0f;

            // Standard geomtry circle calculation
            // Spin Tri1
            Obj1.WorldPos.x=(float)( 1000.0f*Math.cos(World.radians(angle1)) );
            Obj1.WorldPos.y=(float)( 1000.0f*Math.sin(World.radians(angle1)) );
            Obj1.setPositionDirect(Obj1.WorldPos.x,Obj1.WorldPos.y,Obj1.WorldPos.z);

            // Spin Obj2
            Obj2.WorldPos.x=(float)( 1000.0f*Math.cos(World.radians(angle2)) );
            Obj2.WorldPos.y=(float)( 1000.0f*Math.sin(World.radians(angle2)) );
            Obj2.setPositionDirect(Obj2.WorldPos.x,Obj2.WorldPos.y,Obj2.WorldPos.z);

            // Spin Obj3
            Obj3.WorldPos.x=(float)( 1000.0f*Math.cos(World.radians(angle3)) );
            Obj3.WorldPos.y=(float)( 1000.0f*Math.sin(World.radians(angle3)) );
            Obj3.setPositionDirect(Obj3.WorldPos.x,Obj3.WorldPos.y,Obj3.WorldPos.z);

            // Spin Obj4
            Obj4.WorldPos.x=(float)( 1000.0f*Math.cos(World.radians(angle4)) );
            Obj4.WorldPos.y=(float)( 1000.0f*Math.sin(World.radians(angle4)) );
            Obj4.setPositionDirect(Obj4.WorldPos.x,Obj4.WorldPos.y,Obj4.WorldPos.z);

            // Spin Obj5
            Obj5.WorldPos.y=(float)( 1000.0f*Math.cos(World.radians(angle5)) );
            Obj5.WorldPos.z=(float)( 1000.0f*Math.sin(World.radians(angle5)) );
            Obj5.setPositionDirect(Obj5.WorldPos.x,Obj5.WorldPos.y,Obj5.WorldPos.z);

            // Spin Obj6
            Obj6.WorldPos.y=(float)( 1000.0f*Math.cos(World.radians(angle6)) );
            Obj6.WorldPos.z=(float)( 1000.0f*Math.sin(World.radians(angle6)) );
            Obj6.setPositionDirect(Obj6.WorldPos.x,Obj6.WorldPos.y,Obj6.WorldPos.z);

            //---------------------------------------------------------------------
            // We use the synchronized methods because demos need to run on ANY machine
            //----------------------------------------------------------------------
            synchronized (World)
		    {
		        repaint();
			    try { World.wait(100); }
   			    catch (Exception e) { }
  		    }
        }
    }


    // keyDown() - Called when user presses key or holds key press down.
    //--------------------------------------------------------
    public boolean keyDown(Event evt,int key){
        switch(key){

            // Move down
            case Event.DOWN:
                 MyView.rotate(MyView.VIEW_FRAME,MyView.X_AXIS,3.0f); // Rotate on x axis
                 break;

            // Move up
            case Event.UP:
                 MyView.rotate(MyView.VIEW_FRAME,MyView.X_AXIS,-3.0f);// Rotate on x axis
                 break;

            // Rotate left
            case Event.LEFT:
                 MyView.rotate(MyView.VIEW_FRAME,MyView.Y_AXIS,-3.0f);// Rotate on y axis
                 break;

            // Rotate right
            case Event.RIGHT:
                 MyView.rotate(MyView.VIEW_FRAME,MyView.Y_AXIS,3.0f);// Rotate on y axis
                 break;
        }
        return true;
    }

    // mouseUp() - Called when user releases mouse button
    //--------------------------------------------------------
    public boolean mouseUp(Event evt,int x,int y){
        MouseDown=false;
        MouseWhere=0;
        return true;
    }

    // mouseDown() - Called when user presses mouse button
    //--------------------------------------------------------
    public boolean mouseDown(Event evt,int x,int y){
        byte Where;

        // Set state to true, reset where
        MouseDown=true;
        MouseWhere=0;
        Where=0;

        // X-portion of screen divided into 2 halves
        if      ((x&gt;0)                    &amp;&amp;(x&lt;World.SCREEN_WIDTH/2))         Where|=1;
        else if  (x&gt;=(World.SCREEN_WIDTH/2))                                  Where|=2;

        // Y-portion of screen divided into 3 thirds
        if      ((y&gt;0)                     &amp;&amp;(y&lt;World.SCREEN_HEIGHT/3))       Where|=4;
        else if ((y&gt;=World.SCREEN_HEIGHT/3)&amp;&amp;(y&lt;((World.SCREEN_HEIGHT/3)*2))) Where|=8;
        else if  (y&gt;=((World.SCREEN_HEIGHT/3)*2))                             Where|=16;

        // If mouse in upper third, move forward
        if ((Where &amp; 4)==4)
            MouseWhere=1;

        // Else if mouse in middle third, hmm...
        else
        if ((Where &amp; 8)==8){

            // mouse in left hand side, move left
            if      ((Where &amp; 1)==1)   MouseWhere=2;

            // mouse in right hand side, move right
            else if ((Where &amp; 2)==2)   MouseWhere=3;
        }

        // Else mouse in lower third, move backward
        else
        if ((Where &amp; 16)==16)
            MouseWhere=4;

        return true;
    }

    // DoButton() - Process a render mode button press
    //--------------------------------------------------------
    void DoButton(String bname){

        // All objects shaded
        if (bname.equals(" Shaded  ")) {
            World.setDrawMode(World3D.DRAWMODE_ALLSOLID);
        }

        // All objects wireframe
        else if (bname.equals("Wireframe")) {
            World.setDrawMode(World3D.DRAWMODE_ALLWIREFRAME);
        }

        // Objects drawn by the object's setting
        else if (bname.equals("  Mixed  ")) {
            World.setDrawMode(World3D.DRAWMODE_BYOBJECT);
        }
    }


    // action() - Render Mode button events are sent here
    //--------------------------------------------------------
    public boolean action(Event evt,Object arg){

        // Do sound on button press
        if (evt.target instanceof Button){
            DoButton((String)arg);
        }
        return true;
    }


    // update() - Update the graphics system
    //--------------------------------------------------------
    public void update(Graphics g){
        paint(g);
    }


    // paint() - Paint the 3d World
    //--------------------------------------------------------
    public synchronized void paint(Graphics g){

        if ((runner==null)||(World==null)) return;

        // Remark out synchronized when using time-delay method for
        // a steady framerate. Default is synchronized, not time-delay
  		//-------------------------------------------------------------
        synchronized (World){
	        World.displayIt(g,0,0);
			getToolkit().sync();
			try {World.notify();}
			catch (Exception e) { }
	    }
    }

}

</pre></body></html>