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