Go on...
package jscene3D;
Primitive.java extends Entity , must contain a geometry.
package jscene3D;
import javax.media.opengl.GL2;
public class Primitive extends Entity {
private IRenderable geometry;
public Primitive(IRenderable geometryIn){
if(geometryIn==null){
throw new IllegalArgumentException("geometry cant be null");
}
geometry=geometryIn;
}
protected void drawBasic(GL2 gl){
geometry.create(gl);
}
}
package jscene3D;
Light3D.java extends ObjectContainer3D .
package jscene3D;
import java.awt.Color;
import javax.media.opengl.GL2;
public class Light3D extends ObjectContainer3D{
private float[] position={0,0,1,0};
private float[] attenuationConstants={1,0,0};
private float[] spotDirection={0,0,-1};
private float spotCutoff=180;
private float spotExponent=0;
private boolean lit=true;
private float[] color;
private float[] specularColor, diffuseColor, ambientColor, emissionColor;
public Light3D(){
setAmbientColor(0,0,0);
setDiffuseColor(0.7f,0.7f,0.7f);
setSpecularColor(1,1,1);
}
protected int turnOnLightBasic(GL2 gl,int nextLightNumber){
if(!lit||nextLightNumber>7){
return nextLightNumber;
}
int light=GL2.GL_LIGHT0+nextLightNumber;
gl.glEnable(light);
gl.glLightfv(light, GL2.GL_POSITION, position,0);
gl.glLightfv(light, GL2.GL_AMBIENT, getAmbientColor(),0);
gl.glLightfv(light, GL2.GL_DIFFUSE, getDiffuseColor(),0);
gl.glLightfv(light, GL2.GL_SPECULAR,getSpecularColor(),0);
if(position[3]!=0){
gl.glLightf(light, GL2.GL_SPOT_CUTOFF, spotCutoff);
if(spotCutoff!=180){
gl.glLightfv(light, GL2.GL_SPOT_DIRECTION, spotDirection,0);
gl.glLightf(light, GL2.GL_SPOT_EXPONENT, spotExponent);
}
gl.glLightf(light, GL2.GL_CONSTANT_ATTENUATION, attenuationConstants[0]);
gl.glLightf(light, GL2.GL_LINEAR_ATTENUATION, attenuationConstants[1]);
gl.glLightf(light, GL2.GL_QUADRATIC_ATTENUATION, attenuationConstants[2]);
}
return nextLightNumber+1;
}
public Light3D setAmbientAndDiffuseColor(Color c) {
this.ambientColor = c == null? null : c.getRGBColorComponents(null);
this.diffuseColor = c == null? null : c.getRGBColorComponents(null);
return this;
}
public Light3D setAmbientAndDiffuseColor(float r, float g, float b) {
this.ambientColor = new float[] { r, g, b, 1 };
this.diffuseColor = new float[] { r, g, b, 1 };
return this;
}
public Light3D setAmbientAndDiffuseColor(float r, float g, float b, float alpha) {
this.ambientColor = new float[] { r, g, b, alpha };
this.diffuseColor = new float[] { r, g, b, alpha };
return this;
}
public Light3D setAmbientColor(Color c) {
this.ambientColor = c == null? null : c.getRGBColorComponents(null);
return this;
}
public Light3D setAmbientColor(float r, float g, float b) {
this.ambientColor = new float[] { r, g, b, 1 };
return this;
}
public Light3D setAmbientColor(float r, float g, float b, float alpha) {
this.ambientColor = new float[] { r, g, b, alpha };
return this;
}
public float[] getAmbientColor() {
return ambientColor;
}
public Light3D setDiffuseColor(Color c) {
this.diffuseColor = c == null? null : c.getRGBColorComponents(null);
return this;
}
public Light3D setDiffuseColor(float r, float g, float b) {
this.diffuseColor = new float[] { r, g, b, 1 };
return this;
}
public Light3D setDiffuseColor(float r, float g, float b, float alpha) {
this.diffuseColor = new float[] { r, g, b, alpha };
return this;
}
public float[] getDiffuseColor() {
return diffuseColor;
}
public Light3D setSpecularColor(Color c) {
this.specularColor = c == null? null : c.getRGBColorComponents(null);
return this;
}
public Light3D setSpecularColor(float r, float g, float b) {
this.specularColor = new float[] { r, g, b, 1 };
return this;
}
public Light3D setSpecularColor(float r, float g, float b, float alpha) {
this.specularColor = new float[] { r, g, b, alpha };
return this;
}
public float[] getSpecularColor() {
return specularColor;
}
public Light3D setEmissionColor(Color c) {
this.emissionColor = c == null? null : c.getRGBColorComponents(null);
return this;
}
public Light3D setSpotCutoff(float spotCutoff) {
if (spotCutoff < 0 || spotCutoff > 90)
this.spotCutoff = 180;
else
this.spotCutoff = spotCutoff;
return this;
}
public Light3D setDirection(float x, float y, float z) {
position = new float[] { x, y, z };
return this;
}
public Light3D setAttenuationConstants(float constantAttenuation,
float linearAttenuation, float quadraticAttenuation) {
this.attenuationConstants = new float[] { constantAttenuation,
linearAttenuation, quadraticAttenuation};
return this;
}
public Light3D setSpotDirection(float x, float y, float z) {
this.spotDirection = new float[] { x, y, z };
return this;
}
public Light3D setSpotExponent(float spotExponent) {
if (spotExponent < 0)
this.spotExponent = 0;
else if (spotExponent > 128)
this.spotExponent = 128;
else
this.spotExponent = spotExponent;
return this;
}
public float[] getAttenuationConstants() {
return attenuationConstants;
}
public float[] getSpotDirection() {
return spotDirection;
}
public float getSpotCutoff() {
return spotCutoff;
}
public float getSpotExponent() {
return spotExponent;
}
public Light3D setLit(boolean lit) {
this.lit = lit;
return this;
}
public boolean isLit() {
return lit;
}
}
package jscene3D;
IRenderabel.java . Geometry must implements IRenderable the render.
package jscene3D;
//package glutil;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
public interface IRenderable {
public void create(GL2 gl);
}
package jscene3D;
TrackBallControl.java . completely get from David J Eck
package jscene3D;
//package glutil;
import java.awt.Component;
import java.awt.event.*;
/**
* This class enables using the mouse to rotate the view in an OpenGL context.
* A TrackBall requires an OpenGL display and a Camera. It registers a mouse
* listener on the display. When the user drags the mouse, the Camera's view
* is adjusted and the display is redrawn. The action is supposed to be similar
* to rolling a track ball to change the view. Note that to use a Trackball
* on a GLJPanel or GLCanvas, drawable, that uses a Camera, camera, for setting
* its projection and view, it should only be necessary to say
* "new TrackBall(drawable,camera);". You don't have to do anything else with
* the trackball.
*/
public class TrackBallControl{
private final Component gldrawable;
private final Camera3D camera;
/**
* Create a trackball for rotating a given component, which is probably
* a GLJPanel or GLCanvas. A new Camera is created. This camera can
* be retrieved by calling getCamera. To implement the trackball, this
* camera should be used for setting the projection/view in the drawable.
*/
public TrackBallControl(Component gldrawable) {
this(gldrawable, new Camera3D());
}
/**
* Create a trackball for rotating a given component, which is probably
* a GLJPanel or GLCanvas. The camera that is passed to this constructor
* should be the one that is used for setting the projection/view in the drawable.
*/
public TrackBallControl(Component gldrawable, Camera3D camera) {
this.gldrawable = gldrawable;
this.camera = camera;
gldrawable.addMouseListener(new Mouser());
}
/**
* Return the camera whose view is rotated by this trackball.
*/
public Camera3D getCamera() {
return camera;
}
private class Mouser implements MouseListener, MouseMotionListener {
private boolean dragging;
private double[] prevRay;
public void mousePressed(MouseEvent e) {
if (dragging)
return;
dragging = true;
prevRay = mousePointToRay(e.getX(), e.getY());
gldrawable.addMouseMotionListener(this);
}
public void mouseReleased(MouseEvent e) {
if (!dragging)
return;
dragging = false;
gldrawable.removeMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e) {
if (!dragging)
return;
double[] thisRay = mousePointToRay(e.getX(), e.getY());
// camera.applyTransvection(prevRay, thisRay);
prevRay = thisRay;
gldrawable.repaint();
}
private double[] mousePointToRay(int x, int y) {
double dx, dy, dz, norm;
int centerX = gldrawable.getWidth()/2;
int centerY = gldrawable.getHeight()/2;
double scale = 0.8*Math.min(centerX, centerY);
dx = (x - centerX);
dy = (centerY - y);
norm = Math.sqrt(dx*dx + dy*dy);
if (norm >= scale)
dz = 0;
else
dz = Math.sqrt( scale*scale - dx*dx -dy*dy );
double length = Math.sqrt(dx*dx + dy*dy + dz*dz);
return new double[] { dx/length, dy/length, dz/length };
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
}
}