/** * */ package lib; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLEventListener; import com.sun.opengl.util.Animator; /** * @author Serpaud * */ public abstract class Opengl extends GLCanvas implements GLEventListener, MouseListener, MouseWheelListener{ /** * */ private static final long serialVersionUID = -6928613448804828618L; /* creates an animator to redraw */ final Animator animator; private boolean BtMousePressed = false; private int MousePosX = 0; private int MousePosY = 0; private double ScaleStep = 0.95; private double ScaleX = 1; private double ScaleY = 1; private double ScaleZ = 1; private boolean isInit = false; /** * */ public Opengl() { super(); // Creation de la surface d'affichage GL /*GLCapabilities capabilities = new GLCapabilities (); capabilities.setDoubleBuffered (false); GLCanvas canvas = new GLCanvas (capabilities); */ animator = new Animator(this); this.addGLEventListener(this); //this.requestFocus(); } public void Set_Scale(GL gl, double X, double Y, double Z){ ScaleX *= X; ScaleY *= Y; ScaleZ *= Z; gl.glScaled(ScaleX/X, ScaleY/Y, ScaleZ/Z); } protected void DrawAxes(GL gl){ final double X0 = -0.9; final double Y0 = 0.8; final double Z0 = 0; final double L = 0.1; gl.glBegin(GL.GL_LINES); { gl.glColor3d(1.0,0.0,0.0); gl.glVertex3d(X0,Y0,Z0); gl.glVertex3d(X0+L,Y0,Z0); gl.glVertex3d(X0,Y0,Z0); gl.glVertex3d(X0,Y0+L,Z0); gl.glVertex3d(X0,Y0,Z0); gl.glVertex3d(X0,Y0,Z0+L); } gl.glEnd(); } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glClear(GL.GL_DEPTH_BUFFER_BIT); DrawAxes(gl); gl.glScaled(ScaleX, ScaleY, ScaleZ); if (BtMousePressed){ int NewMousePosX = MousePosX; int NewMousePosY = MousePosY; try{// in case of mouse is out of canvas NewMousePosX = this.getMousePosition().x; NewMousePosY = this.getMousePosition().y; }catch(Exception e){} gl.glRotated(MousePosX - NewMousePosX, 0, 1, 0); gl.glRotated(MousePosY - NewMousePosY, 1, 0, 0); // MousePosX = NewMousePosX; MousePosY = NewMousePosY; } //gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); gl.glTexEnvf(GL.GL_TEXTURE_ENV,GL.GL_TEXTURE_ENV_MODE,GL.GL_DECAL); gl.glEnable(GL.GL_BLEND); //gl.glColor4d(1,1,1,0); display(gl); ScaleX = 1; ScaleY = 1; ScaleZ = 1; gl.glDisable(GL.GL_BLEND); gl.glFlush(); } protected abstract void display(GL gl); /** * Start automatic display * */ public void Start(){ animator.start(); } /** * Defined if mouse control is enable * * @param Enable */ public void SetMouseControl(boolean Enable){ if (Enable){ this.addMouseWheelListener(this); this.addMouseListener(this); } else{ this.removeMouseListener(this); this.removeMouseWheelListener(this); } } public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { // TODO Auto-generated method stub } public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.setSwapInterval(1); // diseable vertical synchronization /* * Enables the Smooth color shading mode to create graduate color * The effect can be shown on the Pyramid of the Tutorial 04 */ gl.glShadeModel(GL.GL_SMOOTH); //gl.glShadeModel (GL.GL_FLAT); // No Smooth Shading /* * Defines the clearing color in RGB mode (Red Green Blue) * This color is the background color of the scene. * Components must be a float number in the interval [0,1] * Here: R=0, G=0, B=0 so the color is black * (r=1, G=1, B=1 is the white color) * * Rem: the last value is the Alpha components (also called RGBA). * We should show its effects in the Blending tutorial (09) */ gl.glClearColor (1, 1, 1, 0); // background draw in white /* * The Depth buffer * This buffer keeps tracks the depth of each points of the scene. * This buffer, when an object is drawn, tests if the object * is behind or in front of the objects already drawn. * GL_LEQUAL test if the current object to be drawn is closer * or as the same distance than */ gl.glClearDepth(1.0); //Enable Clearing of the Depth buffer gl.glDepthFunc(GL.GL_LEQUAL); //Type of Depth test gl.glEnable(GL.GL_DEPTH_TEST); //Enable Depth Testing //gl.glEnable (GL.GL_CULL_FACE); // Face culling //gl.glScaled(ScaleX, ScaleY, ScaleZ); //gl.glEnable(GL.GL_COLOR_MATERIAL); // Define the correction done to the perspective calculation (perspective looks a it better) gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); isInit = true; } /** * return true is opengl is initialized * @return */ public boolean isInit(){ return isInit; } /** * Wait toward opengl is initialized * */ public void WaitingInit(){ while(!isInit()){} } /** * this funciton is run when view is reshape */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { /*GL gl = drawable.getGL(); GLU glu = new GLU(); gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); //Select the Projection matrix gl.glLoadIdentity(); //Reset the current matrix glu.gluPerspective(45.0f, width / height, 0.1f, 1000.0f); //set the Viewing Volume gl.glMatrixMode(GL.GL_MODELVIEW); //select The Modelview Matrix gl.glLoadIdentity(); //set the ModalView matrix to identity*/ } /*********************************** Mouse action ***********************************/ public void mouseClicked(MouseEvent arg0) { if (arg0.getClickCount() > 1) //reinit view as start this.getGL().glLoadIdentity (); // Reset The View } public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mousePressed(MouseEvent e) { MousePosX = e.getX(); MousePosY = e.getY(); BtMousePressed = true; } public void mouseReleased(MouseEvent arg0) { BtMousePressed = false; } /** * Manage mouse wheel event */ public void mouseWheelMoved(MouseWheelEvent e) { if(e.getWheelRotation()>0) { ScaleX = ScaleStep; ScaleY = ScaleStep; ScaleZ = ScaleStep; } else{ ScaleX = 1/ScaleStep; ScaleY = 1/ScaleStep; ScaleZ = 1/ScaleStep; } } /* * stop animator before closing component * (non-Javadoc) * @see java.lang.Object#finalize() */ public void finalize() throws Throwable { animator.stop(); } }