Sign in to follow this  
paulscode

[java] Need jPCT (applet 3D engine) guidance

Recommended Posts

I am trying to develop a 3D browser-based game in JAVA. However, I must first learn the basics. I need to make some really simple programs to learn important concepts. I have done some work with JOGL run from AppletLauncher, and made I a few test programs. Unfortunately, this method seems to be rather unstable, and on many machines, runs rediculously slow or not at all. Also, it has no method for loading 3ds or other 3D formats. I tried out the DzzD library, which I really liked. The sample programs basic and easy to understand, and it is well documented. Unfortunately, I have been unable to get my 3ds files to load. I am not sure if I created them incorrectly, or what, so I decided to look into other libraries to see if my files will load from one of them I came across the jPCT library, which looks really awesome. I want to learn how to use this library, but the source code for the demo programs is far to complicated for me to just dive into and understand. I want to be able to make a simple test program that loads a 3ds file and draws it in an applet (and maybe can be rotated with the mouse). Does anyone out there know jPCT well enough to help me out with this? Thank you in advance!

Share this post


Link to post
Share on other sites
Hey! jPCT is a great choice for web based games, as it's applet optimized - as advertised [grin]. I'm currently using it to make a simple 3d browser-based pool game and have the basics required for rendering a world already boiled down into a few classes and packages (with the help of my good friend Jason). If you want to do some sort of collaboration, feel free to add me to msn: karan@uwstudios.com

-Karan Bhangui

Share this post


Link to post
Share on other sites
I eventually figured this out for myself (I guess if you stare at really complicated code for long enough, you'll eventually figure it out). In case anyone else out there is trying to do this, here is my code (which should save you several hours of staring at the other demo programs).

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 entire gear
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 ) {}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this