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 PuntosOpenGL 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.     public static void main(String[] args) {
  25.         Frame frame = new Frame("OpenGl Graficos");
  26.         GLCanvas canvas = new GLCanvas();
  27.  
  28.         canvas.addGLEventListener(new PuntosOpenGL());
  29.         frame.add(canvas);
  30.         frame.setSize(640, 550);
  31.         final Animator animator = new Animator(canvas);
  32.         frame.addWindowListener(new WindowAdapter() {
  33.  
  34.             @Override
  35.             public void windowClosing(WindowEvent e) {
  36. //                Ejecutar este en otro hilo de la cola de eventos AWT para
  37. //                asegúrese de que la llamada a Animator.stop () finaliza antes de
  38. //                salir
  39.                 new Thread(new Runnable() {
  40.  
  41.                     public void run() {
  42.                         animator.stop();
  43.                         System.exit(0);
  44.                     }
  45.                 }).start();
  46.             }
  47.         });
  48.         //Centrar Frame
  49.         frame.setLocationRelativeTo(null);
  50.         frame.setVisible(true);
  51.         animator.start();
  52.     }
  53.    
  54.  
  55.     public void init(GLAutoDrawable drawable) {
  56.         display(drawable);
  57.        
  58.         GL gl = drawable.getGL();
  59.         System.err.println("INIT GL IS: " + gl.getClass().getName());
  60.  
  61.         //Enable VSync
  62.         gl.setSwapInterval(1);
  63.  
  64.         //Setup the drawing area and shading mode
  65.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  66.         gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
  67.     }
  68.  
  69.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  70.         GL gl = drawable.getGL();
  71.         GLU glu = new GLU();
  72.  
  73.         if (height <= 0) { // avoid a divide by zero error!
  74.             height = 1;
  75.         }
  76.        
  77.         final float h = (float) width / (float) height;
  78.         gl.glViewport(0, 0, width, height);
  79.         gl.glMatrixMode(GL.GL_PROJECTION);
  80.         gl.glLoadIdentity();
  81.         glu.gluPerspective(45.0f, h, 1.0, 20.0);
  82.         gl.glMatrixMode(GL.GL_MODELVIEW);
  83.         gl.glLoadIdentity();
  84.     }
  85.  
  86.     public void display(GLAutoDrawable drawable) {
  87.         GL gl = drawable.getGL();
  88.         GLUT glut = new GLUT();
  89.         GLU glu = new GLU();
  90.        
  91.         velRotacion += 0.5f; //Velocidad a la que rota la esfera
  92.         vel += 0.02f;
  93.        
  94.         //Clear the drawing area
  95.         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  96.         //Reset the current matrix to the "identity"
  97.         gl.glLoadIdentity();
  98.  
  99.         //creamos la ESFERA
  100.         gl.glTranslatef(0.0f, 0.0f, -4.0f); //Permite enfocar la esfera
  101.        
  102.         //Dibuja circunferencia grande
  103.         gl.glPointSize(5);
  104.         gl.glColor3f(1.0f, 0.4f, 0.8f);
  105.         gl.glBegin(gl.GL_POINTS);
  106.             for(float angul = 0.0f; angul<2*Pi; angul +=0.1){
  107.                 float x = (float) ((float) 1.5*Math.sin(angul));
  108.                 float y = (float) ((float) 1.5*Math.cos(angul));
  109.                 gl.glVertex2f(x,y);
  110.             }
  111.         gl.glEnd();
  112.        
  113.         //Dibuja circunferencia mediana
  114.         gl.glRotatef(vel, 0.0f, 0.0f, -1.0f);
  115.         gl.glPointSize(5);
  116.         gl.glColor3f(0.0f, 1.0f, 1.0f);
  117.         gl.glBegin(gl.GL_POINTS);
  118.             for(float angulo = 0.0f; angulo<2*Pi; angulo +=0.1){
  119.                 float x = (float) ((float) 1*Math.sin(angulo));
  120.                 float y = (float) ((float) 1*Math.cos(angulo));
  121.                 gl.glVertex2f(x,y);
  122.             }
  123.         gl.glEnd();
  124.        
  125.          //Dibuja circunferencia pequena
  126.        // gl.glRotatef(vel, 0.0f, 0.0f, -1.0f);
  127.         gl.glPointSize(5);
  128.         gl.glColor3f(0.4f, 1.0f, 0.0f);
  129.         gl.glBegin(gl.GL_POINTS);
  130.             for(float aungulo = 0.0f; aungulo<2*Pi; aungulo +=0.05){ //ANG
  131.                 float x = (float) ((float) 0.5*Math.sin(aungulo));
  132.                 float y = (float) ((float) 0.5*Math.cos(aungulo));
  133.                 gl.glVertex2f(x,y);
  134.             }
  135.         gl.glEnd();
  136.        
  137.         gl.glFlush();
  138.     }
  139.  
  140.     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
  141.     }
  142. }