
February 21st, 2008, 06:22 PM
|
 |
Contributing User
|
|
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 51
Time spent in forums: 6 h 56 m 22 sec
Reputation Power: 1
|
|
The Solution
Hey, I figured this out for myself (which just goes to show you can figure out even the most complicated code if you stare at it long enough). Here is my sample applet, for anyone who is interested. It loads a .3ds Gear model, and animates it, and it can be rotated by dragging the mouse. It should save you from having to spend hours fiddling around with those really long demo programs posted on the jPCT site.
Code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import javax.swing.JApplet;
import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Lights;
import com.threed.jpct.Loader;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.World;
public class jPCTTest extends JApplet implements MouseListener, MouseMotionListener
{
private Object3D redGear;
private FrameBuffer buffer = null;
private World world = null;
private Camera camera = null;
private int width = 640;
private int height = 480;
private float gear_rotation = 0.02f;
private int prevMouseX, prevMouseY;
// Initialize all components of the applet
@Override
public void init()
{
// sign the applet up to receive mouse messages:
addMouseListener( this );
addMouseMotionListener( this );
world = new World(); // create a new world
// create a new buffer to render the world on:
buffer = new FrameBuffer( width, height, FrameBuffer.SAMPLINGMODE_NORMAL );
buffer.enableRenderer( IRenderer.RENDERER_SOFTWARE );
// load some 3D objects and make sure they have the correct orientation:
redGear = loadMeshFile( "RedGear.3ds" );
redGear.rotateY( (float)Math.PI / 2.0f );
redGear.rotateMesh();
redGear.setRotationMatrix( new Matrix() );
redGear.setOrigin( new SimpleVector( 0, 0, 0 ) );
// add the objects our world:
world.addObject( redGear );
world.buildAllObjects();
lookAt( redGear ); // make sure the camera is facing towards the object
letThereBeLight(); // create light sources for the scene
}
// Draw the scene
@Override
public void paint( Graphics g )
{
// rotate the gear:
redGear.rotateAxis( redGear.getZAxis(), -gear_rotation );
buffer.clear(); // erase the previous frame
// render the world onto the buffer:
world.renderScene( buffer );
world.draw( buffer );
buffer.update();
buffer.display(g, 0, 0); // draw the buffer onto the applet frame
repaint( 200, 0, 0, width, height ); // keep the graphics auto-refreshing
}
// Load a 3Ds file, and return its Object3D handle
private Object3D loadMeshFile( String filename )
{
Object3D newObject;
Object3D[] objs = Loader.load3DS( this.getDocumentBase(), "models" + File.separatorChar + filename, 1.0f );
if( objs.length==1 )
{
// The object loaded fine, just need to initialize it
newObject=objs[0];
newObject.setCulling( Object3D.CULLING_DISABLED );
newObject.build();
}
else
{
// Didn't load anything, or loaded
// more than 1 object (not supposed to happen)
System.out.println( "Unknown file format: " + filename );
newObject = null;
}
return newObject;
}
// point the camera toward the given object
private void lookAt( Object3D obj )
{
camera = world.getCamera(); // grab a handle to the camera
camera.setPosition( 0, 0, 500 ); // set its *relative* position
camera.lookAt( obj.getTransformedCenter() ); // look toward the object
}
// create light sources for the scene
private void letThereBeLight()
{
world.getLights().setOverbrightLighting (
Lights.OVERBRIGHT_LIGHTING_DISABLED );
world.getLights().setRGBScale( Lights.RGB_SCALE_2X );
// Set the overall brightness of the world:
world.setAmbientLight( 50, 50, 50 );
// Create a main light-source:
world.addLight( new SimpleVector( 50, -50, 300 ), 20, 20, 20 );
}
// Mouse events:
public void mouseDragged( MouseEvent e )
{
// here we want to rotate the gear based on the mouse dragging
int x = e.getX();
int y = e.getY();
Dimension size = e.getComponent().getSize();
float thetaY = (float)Math.PI * ( (float)(x-prevMouseX)/(float)size.width );
float thetaX = (float)Math.PI * ( (float)(prevMouseY-y)/(float)size.height );
prevMouseX = x;
prevMouseY = y;
redGear.rotateX( thetaX );
redGear.rotateY( thetaY );
}
public void mousePressed( MouseEvent e )
{
// set the "previous" mouse location
// this prevent the gear from jerking to the new angle
// whenever mouseDragged gets called
prevMouseX = e.getX();
prevMouseY = e.getY();
}
public void mouseReleased( MouseEvent e ){}
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mouseClicked( MouseEvent e ) {}
public void mouseMoved( MouseEvent e ) {}
}
|