Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package Figuras;
  2.  
  3. import com.sun.opengl.util.Animator;
  4. import com.sun.opengl.util.GLUT;
  5. import java.awt.Frame;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import javax.media.opengl.GL;
  9. import javax.media.opengl.GLAutoDrawable;
  10. import javax.media.opengl.GLCanvas;
  11. import javax.media.opengl.GLEventListener;
  12. import javax.media.opengl.glu.GLU;
  13.  
  14. /**
  15.  * TestOpenGL
  16.  * author: Rafa Gallardo
  17.  *
  18.  */
  19. public class EsferaRotate implements GLEventListener {
  20.     private float velRotacion = 0.0f; //controla velocidad de rotacion de la esfera
  21.    
  22.     public static void main(String[] args) {
  23.         Frame frame = new Frame("OpenGl Graficos");
  24.         GLCanvas canvas = new GLCanvas();
  25.  
  26.         canvas.addGLEventListener(new EsferaRotate());
  27.         frame.add(canvas);
  28.         frame.setSize(640, 550);
  29.         final Animator animator = new Animator(canvas);
  30.         frame.addWindowListener(new WindowAdapter() {
  31.  
  32.             @Override
  33.             public void windowClosing(WindowEvent e) {
  34. //                Ejecutar este en otro hilo de la cola de eventos AWT para
  35. //                asegúrese de que la llamada a Animator.stop () finaliza antes de
  36. //                salir
  37.                 new Thread(new Runnable() {
  38.  
  39.                     public void run() {
  40.                         animator.stop();
  41.                         System.exit(0);
  42.                     }
  43.                 }).start();
  44.             }
  45.         });
  46.         //Centrar Frame
  47.         frame.setLocationRelativeTo(null);
  48.         frame.setVisible(true);
  49.         animator.start();
  50.     }
  51.  
  52.     public void init(GLAutoDrawable drawable) {
  53.         display(drawable);
  54.        
  55.         GL gl = drawable.getGL();
  56.         System.err.println("INIT GL IS: " + gl.getClass().getName());
  57.  
  58.         //Enable VSync
  59.         gl.setSwapInterval(1);
  60.  
  61.         //Setup the drawing area and shading mode
  62.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  63.         gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
  64.     }
  65.  
  66.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  67.         GL gl = drawable.getGL();
  68.         GLU glu = new GLU();
  69.  
  70.         if (height <= 0) { // avoid a divide by zero error!
  71.             height = 1;
  72.         }
  73.        
  74.         final float h = (float) width / (float) height;
  75.         gl.glViewport(0, 0, width, height);
  76.         gl.glMatrixMode(GL.GL_PROJECTION);
  77.         gl.glLoadIdentity();
  78.         glu.gluPerspective(45.0f, h, 1.0, 20.0);
  79.         gl.glMatrixMode(GL.GL_MODELVIEW);
  80.         gl.glLoadIdentity();
  81.     }
  82.  
  83.     public void display(GLAutoDrawable drawable) {
  84.         GL gl = drawable.getGL();
  85.         GLUT glut = new GLUT();
  86.         GLU glu = new GLU();
  87.  
  88.         velRotacion += 2.0f; //Velocidad a la que rota la esfera
  89.        
  90.         //Clear the drawing area
  91.         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  92.         //Reset the current matrix to the "identity"
  93.         gl.glLoadIdentity();
  94.  
  95.         //creamos la ESFERA
  96.         gl.glTranslatef(0.0f, 0.0f, -1.0f); //Permite enfocar la esfera
  97.         gl.glRotatef(velRotacion, 0.0f, 0.0f, -2.0f); //Permite la rotacion de la esfera
  98.         gl.glColor3f( 0.6f , 0.5f , 0.7f );
  99.         glu.gluLookAt(1, 1, 5, 0, 0, 0, 1, 1, 0);
  100.         glut.glutWireSphere(2d, 40, 50);            // Se coloca con el centro en <0, 0, 0>
  101.         //glut.glutSolidSphere(1d, 5, 5);
  102.  
  103.         //Flush all drawing operations to the graphics card
  104.         gl.glFlush();
  105.     }
  106.  
  107.     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
  108.     }
  109. }