Hi, can anybody please give me a link to an example for moving picked objects with the mouse or can t ake a look at my code, and tell me whats wrong and why it isnt working so far… the cube(left) should rotate and the right sphere should moved with the cursor on picking. And can somebody explain me whats wrong with the matrices, the programm runs so slow …
the code in 2 parts
part I:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.nio.IntBuffer;
import java.awt.*;
import net.java.games.jogl.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import net.java.games.jogl.Animator;
import net.java.games.jogl.DebugGL;
import net.java.games.jogl.GL;
import net.java.games.jogl.GLCanvas;
import net.java.games.jogl.GLCapabilities;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.GLDrawableFactory;
import net.java.games.jogl.GLEventListener;
import net.java.games.jogl.GLU;
import net.java.games.jogl.util.GLUT;
import net.java.games.jogl.util.BufferUtils;
public class AllInOne implements GLEventListener, MouseListener, MouseMotionListener{
// für die aktuelle Mausposition;
private float mouseX;
private float mouseY;
// für die letzte Mausposition;
float oldmouseposX;
float oldmouseposY;
// variablem zum Rotieren
float rotateX = 0.0f;
float rotateY = 0.0f;
// variablen zum bewegen
float transX;
float transY;
// wenn picked dann...
private boolean picked;
// welches objekt gepicked wird
int pickedName;
float h;
int sphere;
int cube;
Animator animator;
GLCanvas canvas;
Lichtquelle light0;
/**
* @param args
*/
public AllInOne(){
try{
//hier wird die Oberflaeche erstellt...
MenuBar mb = new MenuBar();
Menu add = new Menu ("add");
MenuItem lightsrc = new MenuItem("Lightsource");
Frame frame = new Frame("OpenGL: light und picking versuch");
add.add(lightsrc);
mb.add(add);
frame.setMenuBar(mb);
frame.setSize( 512, 384 );
// das übliche...
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(this);
canvas.setGL(new DebugGL(canvas.getGL()));
canvas.addMouseListener(this);
animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
System.exit(0);
}
});
frame.add(canvas) ;
frame.setVisible(true);
}
catch( Exception e )
{
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AllInOne al = new AllInOne();
al.run();
}
public void run(){
animator.start();
}
public void init(GLDrawable drawable){
GL gl = drawable.getGL();
// projektionsmatrix laden
gl.glMatrixMode(GL.GL_PROJECTION);
// darstellungsmatrixladen
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH);
gl.glEnable(GL.GL_DEPTH_TEST);
// den Displaylisten namen geben
sphere = gl.glGenLists(1);
cube = gl.glGenLists(2);
// displaylisten erzeugen
makeSphere(drawable);
makeCube(drawable);
}
public void display(GLDrawable drawable){
//draw(drawable);
if (this.picked){
System.out.println(this.mouseX);
System.out.println(this.mouseY);
renderPicking(drawable, mouseX, mouseY);
}else{
draw(drawable);
}}
public void reshape(GLDrawable drawable, int x, int y, int width, int height){
final GL gl = drawable.getGL();
final GLU glu = drawable.getGLU();
if (height <= 0){ // avoid a divide by zero error!
height = 1;
}
this.h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged){
}
public void draw(GLDrawable drawable){
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
GLUT glut = new GLUT();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -6.0f);
// laden der Objekte auf den namestack
// leider funktioniert die "animation" (rotation und bewegung noch nicht)
gl.glPushMatrix();
gl.glTranslatef(this.transX, this.transY, 0.0f);
gl.glPushName(this.sphere);
gl.glCallList(this.sphere);
gl.glPopName();
gl.glPopMatrix();
gl.glPushMatrix();
gl.glRotatef(this.rotateX, 1.0f, 0.0f, 0.0f);
gl.glRotatef(this.rotateY, 0.0f, 1.0f, 0.0f);
gl.glPushName(this.cube);
gl.glCallList(this.cube);
gl.glPopName();
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(transX, transY, -6.0f);
gl.glPushName(3);
gl.glLoadName(3);
glut.glutWireSphere(glu, 0.5, 100, 100);
// glut.glutSolidCube(gl, 2.0f);
gl.glPopName();
gl.glPopMatrix();
gl.glFlush();