From: R.M. L. <u8...@cs...> - 2002-02-27 17:01:21
|
After having another attempt at picking i noticed something strange in my applet. When you move the spheres with the arrow keys ( y translation) and (x translation) or use the mouse zoom (z translation) you are still able to click on the spheres and the crosshair will appear as a test. However, when you use the rotation and camera rotation feature the program appears to lose track of the objects. If anyone can help, they're welcome to try it out... Left mouse button-pick, crosshair should appear on an object Hold right mouse button, camera movements Hold both buttons, y direction to zoom, x to rotate spheres. PS, it uses MatrixFuncs.class from the jausoft demos Thanks, Mark. ---------------------------------------------------------------------- import java.applet.*; import java.awt.*; import java.awt.event.*; //Gl4Java classes. import gl4java.GLContext; import gl4java.awt.GLAnimCanvas;//Constantly redraws canvas import gl4java.utils.textures.*; public class Windowt extends Applet { renderCanvas canvas = null; //Initialise applet public void init() { setLayout(new BorderLayout());//lays applet components out canvas = new renderCanvas(getSize().width, getSize().height); canvas.requestFocus(); add("Center", canvas); } //Begin the applet public void start() { canvas.start(); //Begins canvas animation } public void stop() { canvas.stop();//stops the canvas } public void destroy() { canvas.stop(); canvas.destroy();//destroys canvas } //NB put commas here private class renderCanvas extends GLAnimCanvas implements KeyListener,MouseListener, MouseMotionListener { //Keys initially //Putting in mouse float xrot = 0.0f;//rotation for x and y float yrot = 0.0f; //Think about mouse x and y and the out of window calculations //Make sure this works private MatrixFuncs mFun = null; private float rotation[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; float moveX = 0.0f;//For mouse movements, try as ints float moveY = 0.0f; float x = 0.0f; float y = 0.0f; float z = -8.0f; public renderCanvas(int w, int h) { super(w, h); addKeyListener(this);//Key listener back in addMouseListener(this);//Alter, will for mouse addMouseMotionListener(this); mFun = new MatrixFuncs(); } public void preInit() { //Understand this doubleBuffer = true; stereoView = false; } public void init() { /*float[] ambient = {0.5f, 0.5f, 0.5f, 1.0f}; float[] diffuse = {1.0f, 1.0f, 1.0f, 1.0f}; float[] position = {0.0f, 0.0f, 2.0f, 1.0f}; gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//Clears colour, change background gl.glClearDepth(1.0);//Clear depth buffer gl.glDepthFunc(GL_LESS);//Depth test gl.glEnable(GL_DEPTH_TEST);//enable test of depth gl.glShadeModel(GL_SMOOTH);//Smooth colour shading gl.glMatrixMode(GL_PROJECTION);//Projection matrix gl.glLoadIdentity();//Resets matrix glu.gluPerspective(45.0f, (float)getSize().width/(float)getSize().height, 0.1f, 100f);//Calculate window aspect ratio gl.glMatrixMode(GL_MODELVIEW);//Model view matrix /*gl.glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); gl.glLightfv(GL_LIGHT0, GL_POSITION, position);//Put in later gl.glEnable(GL_LIGHT0); gl.glEnable(GL_LIGHTING);*/ } public void destroy() { cvsDispose();//Destroys GL context } public void reshape(int width, int height) { //Might need to change viewport for object selection??? gl.glViewport(0, 0 , width, height);//Reset viewport & perspective gl.glMatrixMode(GL_PROJECTION);//select proj matrix gl.glLoadIdentity();//reset proj matrix glu.gluPerspective(45.0f, (float)getSize().width/(float)getSize().height,0.1f, 100.0f);//Aspect ratio of window gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); } public void display() {//Draw to the canvas //Changing to implement true camera... if (glj.gljMakeCurrent(true)==false) return;//Is GL initialised properly gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//Clears //screen & depth buffer gl.glLoadIdentity();//Reset view gl.glMultMatrixf(rotation); gl.glTranslatef(0.0f, 0.0f, z);//Move into environment gl.glTranslatef(x, 0.0f, 0.0f);//Strafe gl.glTranslatef(0.0f, y, 0.0f);//Strafe gl.glRotatef(xrot,0.0f,1.0f,0.0f);//Rotate on x axis gl.glRotatef(yrot,1.0f,0.0f,0.0f);//Rotate y..swapped drawLayout(GL_RENDER); gl.glFlush();//Might not be necessary gl.glEnd(); glj.gljSwap();//Swap buffers glj.gljFree(); } private void renderSphereRed(float x, float y, float z) {//Create sphere gl.glPushMatrix(); gl.glTranslatef(x,y,z); long qobj = glu.gluNewQuadric(); glu.gluQuadricOrientation(qobj , GLU_OUTSIDE); glu.gluQuadricNormals(qobj, GLU_SMOOTH); gl.glColor3f(1.0f,0.0f,0.0f);//Red glu.gluQuadricTexture(qobj, false); glu.gluSphere(qobj, 0.4f, 16, 16); glu.gluDeleteQuadric(qobj); gl.glPopMatrix(); } private void renderSphereYellow(float x, float y, float z) {//Create sphere gl.glPushMatrix(); gl.glTranslatef(x,y,z); long qobj = glu.gluNewQuadric(); glu.gluQuadricOrientation(qobj , GLU_OUTSIDE); glu.gluQuadricNormals(qobj, GLU_SMOOTH); gl.glColor3f(1.0f, 1.0f, 0.0f);//Yellow glu.gluQuadricTexture(qobj, false); glu.gluSphere(qobj, 0.4f, 16, 16); glu.gluDeleteQuadric(qobj); gl.glPopMatrix(); } private void renderSphereBlue(float x, float y, float z) {//Create sphere gl.glPushMatrix(); gl.glTranslatef(x,y,z); long qobj = glu.gluNewQuadric(); glu.gluQuadricOrientation(qobj , GLU_OUTSIDE); glu.gluQuadricNormals(qobj, GLU_SMOOTH); gl.glColor3f(0.0f, 0.0f, 1.0f);//Blue glu.gluQuadricTexture(qobj, false); glu.gluSphere(qobj, 0.4f, 16, 16); glu.gluDeleteQuadric(qobj); gl.glPopMatrix(); } void drawLayout(int mode) {//Check syntax if (mode == GL_SELECT){ //Might put in draw methods gl.glLoadName(1); } renderSphereRed(-2.0f, 1.0f, -1.0f);//Wrap bitmaps around renderSphereBlue(2.0f, 2.0f, 2.0f);//Need morphing operation renderSphereYellow(2.0f, 4.0f, 5.0f);//?Name convertor needed gl.glEnd(); } void processSelection(float moveX, float moveY) { int BUFSIZE = 512;//May change size int[] selectBuf = new int[BUFSIZE]; int hits; int[] viewport = new int[4]; gl.glGetIntegerv(GL_VIEWPORT, viewport); gl.glSelectBuffer(BUFSIZE, selectBuf); gl.glRenderMode(GL_SELECT);//Select mode gl.glInitNames(); gl.glPushName(-1);//Check implementation of stack names gl.glMatrixMode(GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); glu.gluPickMatrix((float) moveX, (float) (viewport[3] - moveY),5.0,5.0, viewport);//Size of pick cube, may change //Check perspective matrix glu.gluPerspective(45.0f, (float)getSize().width/(float)getSize().height,0.1f, 100.0f);//Aspect ratio of window //Need drawLayout method drawLayout(GL_SELECT); gl.glPopMatrix(); gl.glFlush(); hits = gl.glRenderMode(GL_RENDER); gl.glMatrixMode(GL_MODELVIEW); if (hits > 0) //Need process hits method processHits(hits, selectBuf); } void processHits (int hits, int buffer[]) { //Will have zoom and menu here setCursor( Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); /*glu.gluLookAt(0.0f, 0.0f, 0.0f);*/ /*objectZoom(moveX, moveY);*/ } /*public void objectZoom(float moveX, float moveY) { //Another picking operation, need reverse //Zoom to where clicked..first attempt, see red book //Check viewpoint gl.glPushMatrix(); gl.glGetIntegerv(GL_VIEWPORT, viewport); gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); glu.gluPickMatrix((double) moveX, (double) (viewport[3] - moveY),5.0,5.0, viewport); gl.glScalef(zoom, zoom, zoom); display(GL_RENDER); gl.glPopMatrix(); //Look in book }*///no //Required for implementation of mouselistener etc... public void mouseEntered(MouseEvent e) { Component comp = e.getComponent(); if(comp.equals(this)) { requestFocus(); } } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) {//Find a way to hide the mouse pointer moveX = e.getX();//Take new mouse reading moveY = e.getY(); //Selection here if ((e.getModifiers() & InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK) { //Test for cursor hiding, hand selector setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); if(!glj.gljMakeCurrent(true))//See if is initialised return; processSelection(moveX,moveY);//Send arguements to process selection glj.gljSwap(); glj.gljCheckGL(); glj.gljFree(); } } public void mouseReleased(MouseEvent e) { setCursor( Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } public void mouseClicked(MouseEvent e) { Component comp = e.getComponent(); if(comp.equals(this)) { requestFocus(); } } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { //Manual zoom.... if ((e.isMetaDown()) & ((e.getModifiers() & InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK)) { setCursor( Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); //Also two button for roataion of structure //Put in proper camera zoom. float newMoveX = e.getX(); float newMoveY = e.getY();//Need distance for zoom //See which feels better for user xrot -= newMoveX - moveX; //Might need method z -= newMoveY - moveY; //Update method moveX = newMoveX; moveY = newMoveY; } else if (e.isMetaDown()) { setCursor( Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));//Find correct method float newMoveX = e.getX(); float newMoveY = e.getY(); float angleX = (float)(newMoveX - moveX)*360.0f/(float)getSize().width;//Assign x angle float angleY = (float)(newMoveY - moveY)*360.0f/(float)getSize().height;//Assign y angle float mBuf1[] = new float[16]; float mBuf2[] = new float[16]; moveX = newMoveX; moveY = newMoveY; if ((angleX != 0.0f) || (angleY != 0.0f)) { mFun.rotateAroundY(((float)angleX), mBuf1); mFun.rotateAroundX(((float)angleY), mBuf2); //Accredit particle source..fair, for matrix mFun.multiplyMatrices(mBuf2,mBuf1, mBuf1);//Look at both mFun.multiplyMatrices(rotation, mBuf1, rotation); } } } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { //Ensure initialisation if (glj.gljMakeCurrent(true)== false) return; switch(e.getKeyCode()) { case KeyEvent.VK_PAGE_UP: {//Move back with page up z -=0.5f; break; } case KeyEvent.VK_PAGE_DOWN: {//Move forward with pd z +=0.5f; break; } //Change position, change to strafe case KeyEvent.VK_UP:{//height y -=0.1f; break; } case KeyEvent.VK_DOWN:{//height y +=0.1f; break; }//Give a camera like feel // case KeyEvent.VK_RIGHT:{//pan x -=0.1f; break; } case KeyEvent.VK_LEFT:{//pan x +=0.1f; break; } } glj.gljFree(); } public void keyReleased(KeyEvent e) { } } } |