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 CircunferenciaOpenGL implements GLEventListener {
  20.     private float velRotacion = 0.0f; //controla velocidad de rotacion de la esfera
  21.     private float vel = 0.0f;
  22.     public static final double Pi = 3.14159265358979323846;
  23.    
  24.    
  25.     public static void main(String[] args) {
  26.         Frame frame = new Frame("OpenGl Graficos");
  27.         GLCanvas canvas = new GLCanvas();
  28.  
  29.         canvas.addGLEventListener(new CircunferenciaOpenGL());
  30.         frame.add(canvas);
  31.         frame.setSize(640, 550);
  32.         final Animator animator = new Animator(canvas);
  33.         frame.addWindowListener(new WindowAdapter() {
  34.  
  35.             @Override
  36.             public void windowClosing(WindowEvent e) {
  37. //                Ejecutar este en otro hilo de la cola de eventos AWT para
  38. //                asegúrese de que la llamada a Animator.stop () finaliza antes de
  39. //                salir
  40.                 new Thread(new Runnable() {
  41.  
  42.                     public void run() {
  43.                         animator.stop();
  44.                         System.exit(0);
  45.                     }
  46.                 }).start();
  47.             }
  48.         });
  49.         //Centrar Frame
  50.         frame.setLocationRelativeTo(null);
  51.         frame.setVisible(true);
  52.         animator.start();
  53.     }
  54.    
  55.  
  56.     public void init(GLAutoDrawable drawable) {
  57.         display(drawable);
  58.        
  59.         GL gl = drawable.getGL();
  60.         System.err.println("INIT GL IS: " + gl.getClass().getName());
  61.  
  62.         //Enable VSync
  63.         gl.setSwapInterval(1);
  64.  
  65.         //Setup the drawing area and shading mode
  66.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  67.         gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
  68.     }
  69.  
  70.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  71.         GL gl = drawable.getGL();
  72.         GLU glu = new GLU();
  73.  
  74.         if (height <= 0) { // avoid a divide by zero error!
  75.             height = 1;
  76.         }
  77.        
  78.         final float h = (float) width / (float) height;
  79.         gl.glViewport(0, 0, width, height);
  80.         gl.glMatrixMode(GL.GL_PROJECTION);
  81.         gl.glLoadIdentity();
  82.         glu.gluPerspective(45.0f, h, 1.0, 20.0);
  83.         gl.glMatrixMode(GL.GL_MODELVIEW);
  84.         gl.glLoadIdentity();
  85.     }
  86.  
  87.     public void display(GLAutoDrawable drawable) {
  88.         GL gl = drawable.getGL();
  89.         GLUT glut = new GLUT();
  90.         GLU glu = new GLU();
  91.        
  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 la ESFERA
  99.         gl.glTranslatef(-1.5f, 0.0f, -5.5f); //Permite enfocar la esfera
  100.        
  101.         //Dibuja circunferencia
  102.         gl.glBegin(gl.GL_POLYGON);
  103.             for(int i=0; i<100; i++){
  104.                float x = (float) Math.cos(i*2*Pi/100);
  105.                float y = (float) Math.sin(i*2*Pi/100);
  106.                gl.glVertex2f(x, y); gl.glColor3f(1.0f, 0.8f, 0.2f);
  107.             }
  108.         gl.glEnd();
  109.         gl.glFlush();
  110.        
  111.         gl.glTranslatef(3.0f, 0.0f, -0.5f); //Permite enfocar la esfera
  112.         //dibuja semiCircunferencia
  113.         gl.glBegin(gl.GL_POLYGON);
  114.             for(int i=0; i<51; i++){
  115.                float x = (float) Math.cos(i*2*Pi/100);
  116.                float y = (float) Math.sin(i*2*Pi/100);
  117.                gl.glVertex2f(x, y); gl.glColor3f(0.0f, 1.0f, 1.0f);
  118.             }
  119.         gl.glEnd();
  120.         gl.glFlush();
  121.     }
  122.  
  123.     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
  124.     }
  125. }