pahidla
JGO n00b  Posts: 42
Java games rock!
|
 |
«
Reply #11 on:
2005-01-15 00:00:56 » |
|
OK, thanks for all the help! After simplifying my code things just worked. Here's my on-demand mode demo if anyone finds it useful. Drag to rotate, shift-drag to zoom, double-click to change the presence of perspective, ctrl-drag to change the degree of perspective.
Please comment if I'm doing something wrong.
import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener;
import net.java.games.jogl.*;
public class OnDemandDemo { public static void main(String[] inArgs) { new OnDemandDemo(); }
private GLCanvas myGLCanvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
private double myTheta = Math.PI/2; private double myPhi = -Math.PI/2; private double myZoom = 2.0; private double myPerspAngle = Math.PI/3; private boolean myPerspectivePresent = true;
public OnDemandDemo() { super();
MouseListeners il = new MouseListeners(); myGLCanvas.addMouseListener(il); myGLCanvas.addMouseMotionListener(il); myGLCanvas.addGLEventListener(new OnScreenGLEventListener()); myGLCanvas.setSize(400, 400); myGLCanvas.repaint();
javax.swing.JFrame frame = new javax.swing.JFrame("On Demand Mode Demonstration"); frame.getContentPane().add(myGLCanvas); frame.pack(); frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
public class OnScreenGLEventListener implements GLEventListener { public void init(GLDrawable inDrawable) { GL gl = inDrawable.getGL();
gl.glClearColor(1f, 1f, 1f, 1f);
gl.glEnable(GL.GL_DEPTH_TEST); gl.glEnable(GL.GL_POLYGON_OFFSET_FILL); gl.glEnable(GL.GL_POLYGON_OFFSET_LINE);
gl.glEnable(GL.GL_LIGHTING); gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, new float[] { 1f, 1f, 1f, 1f });
gl.glDisable(GL.GL_CULL_FACE) ; gl.glLightModeli(GL.GL_LIGHT_MODEL_TWO_SIDE, GL.GL_TRUE) ; }
public void display(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {} public void displayChanged(GLDrawable inDrawable, boolean inWhat, boolean inWhen) {} public void reshape(GLDrawable drawable, int x, int y, int width, int height) {}
public void display(GLDrawable inDrawable) { GL gl = inDrawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
pSetView(inDrawable, false);
gl.glColor3d(0, 0, 0); gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE); gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, new float[] { 0f, 0f, 0f } ); gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, new float[] { 0f, 0f, 0f }); gl.glBegin(GL.GL_LINE_LOOP); gl.glVertex3d( 1, -1, -1); gl.glVertex3d( 1, -1, 1); gl.glVertex3d(-1, -1, 1); gl.glVertex3d(-1, -1, -1); gl.glEnd();
gl.glBegin(GL.GL_LINE_LOOP); gl.glVertex3d( 1, 1, -1); gl.glVertex3d( 1, 1, 1); gl.glVertex3d(-1, 1, 1); gl.glVertex3d(-1, 1, -1); gl.glEnd();
gl.glFlush(); } }
public void pSetView(GLDrawable inDrawable, boolean inFlip) { pSetView( (double) myGLCanvas.getSize().width / myGLCanvas.getSize().height, inDrawable, inFlip); }
public void pSetView(double inAspect, GLDrawable inDrawable, boolean inFlip) { GL gl = inDrawable.getGL(); GLU glu = inDrawable.getGLU();
gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity();
double dim = myZoom; if (myPerspectivePresent) { glu.gluPerspective(myPerspAngle*180/Math.PI, inAspect, .1, 100000.0); dim = myZoom/Math.tan(myPerspAngle/ 2); } else gl.glOrtho(-myZoom*inAspect, myZoom*inAspect, -myZoom, myZoom, -10.0, 100.0);
gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); double upOrDown = 1 - 2 * (int) (gMod(myTheta, 2*Math.PI)/Math.PI); glu.gluLookAt(dim*Math.sin(myTheta)*Math.cos(myPhi), dim*Math.sin(myTheta)*Math.sin(myPhi), dim*Math.cos(myTheta), 0.0, 0.0, 0.0, 0.0, 0.0, (!inFlip)?upOrDown:-upOrDown); }
public static double gMod(double inA, double inM) { while (inA > inM) inA -= inM; while (inA < 0) inA += inM; return inA; }
private class MouseListeners implements MouseListener, MouseMotionListener { private int myLastX, myLastY; public void mousePressed(MouseEvent inME) { if (inME.getButton() == MouseEvent.BUTTON1) { myLastX = inME.getX(); myLastY = inME.getY(); } }
public void mouseDragged(MouseEvent inME) { if ((inME.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0) return;
if ((inME.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0 ) myZoom += (inME.getY() - myLastY)/30.0; else if ((inME.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0 ) myPerspAngle += -(inME.getY() - myLastY)/600.0; else { myPhi += -(inME.getX() - myLastX)/300.0; myTheta += -(inME.getY() - myLastY)/300.0; } myLastX = inME.getX(); myLastY = inME.getY();
myGLCanvas.display(); }
public void mouseMoved(MouseEvent inME) { } public void mouseReleased(MouseEvent inME) { }
public void mouseClicked(MouseEvent inME) { if (inME.getButton() == MouseEvent.BUTTON1 && inME.getClickCount() == 2) myPerspectivePresent = !myPerspectivePresent; myGLCanvas.display(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } }
|