Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package Figuras;
  2.  
  3. import com.sun.opengl.util.Animator;
  4. import java.awt.Frame;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import javax.media.opengl.GL;
  8. import javax.media.opengl.GLAutoDrawable;
  9. import javax.media.opengl.GLCanvas;
  10. import javax.media.opengl.GLEventListener;
  11. import javax.media.opengl.glu.GLU;
  12.  
  13. /**
  14.  * TestOpenGL
  15.  * author: Rafa Gallardo
  16.  *
  17.  */
  18. public class TestOpenGL implements GLEventListener {
  19.  
  20.     public static void main(String[] args) {
  21.         Frame frame = new Frame("OpenGl Graficos");
  22.         GLCanvas canvas = new GLCanvas();
  23.  
  24.         canvas.addGLEventListener(new TestOpenGL());
  25.         frame.add(canvas);
  26.         frame.setSize(640, 550);
  27.         final Animator animator = new Animator(canvas);
  28.         frame.addWindowListener(new WindowAdapter() {
  29.  
  30.             @Override
  31.             public void windowClosing(WindowEvent e) {
  32. //                Ejecutar este en otro hilo de la cola de eventos AWT para
  33. //                asegúrese de que la llamada a Animator.stop () finaliza antes de
  34. //                salir
  35.                 new Thread(new Runnable() {
  36.  
  37.                     public void run() {
  38.                         animator.stop();
  39.                         System.exit(0);
  40.                     }
  41.                 }).start();
  42.             }
  43.         });
  44.         //Centrar Frame
  45.         frame.setLocationRelativeTo(null);
  46.         frame.setVisible(true);
  47.         animator.start();
  48.     }
  49.  
  50.     public void init(GLAutoDrawable drawable) {
  51.         GL gl = drawable.getGL();
  52.         System.err.println("INIT GL IS: " + gl.getClass().getName());
  53.  
  54.         //Enable VSync
  55.         gl.setSwapInterval(1);
  56.  
  57.         //Setup the drawing area and shading mode
  58.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  59.         gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
  60.     }
  61.  
  62.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  63.         GL gl = drawable.getGL();
  64.         GLU glu = new GLU();
  65.  
  66.         if (height <= 0) { // avoid a divide by zero error!
  67.             height = 1;
  68.         }
  69.        
  70.         final float h = (float) width / (float) height;
  71.         gl.glViewport(0, 0, width, height);
  72.         gl.glMatrixMode(GL.GL_PROJECTION);
  73.         gl.glLoadIdentity();
  74.         glu.gluPerspective(45.0f, h, 1.0, 20.0);
  75.         gl.glMatrixMode(GL.GL_MODELVIEW);
  76.         gl.glLoadIdentity();
  77.     }
  78.  
  79.     public void display(GLAutoDrawable drawable) {
  80.         GL gl = drawable.getGL();
  81.  
  82.         //Clear the drawing area
  83.         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  84.         //Reset the current matrix to the "identity"
  85.         gl.glLoadIdentity();
  86.  
  87.         //Move the "drawing cursor" around
  88.         gl.glTranslatef(-2.0f, 1.4f, -6.0f);
  89.  
  90.         //Dibuja triangulo
  91.         gl.glBegin(GL.GL_TRIANGLES);
  92.             gl.glColor3f(1.0f, 0.5f, 0.0f);    
  93.             gl.glVertex3f(0.0f, 1.0f, 0.0f);  
  94.             gl.glColor3f(0.5f, 1.0f, 0.0f);    
  95.             gl.glVertex3f(-1.0f, -1.0f, 0.0f);
  96.             gl.glColor3f(0.0f, 0.5f, 1.0f);    
  97.             gl.glVertex3f(1.0f, -1.0f, 0.0f);  
  98.         //Finished Drawing The Triangle
  99.         gl.glEnd();
  100.  
  101.         //Move the "drawing cursor" to another position
  102.         gl.glTranslatef(2.0f, 0.0f, -1.0f);
  103.         //Dibuja cuadrado strip
  104.         gl.glBegin(GL.GL_QUAD_STRIP);
  105.             gl.glColor3f(0.5f, 0.5f, 1.0f);    
  106.             gl.glVertex3f(-1.0f, 1.0f, 0.0f);
  107.             gl.glColor3f(1.0f, 0.0f, 0.5f);  
  108.             gl.glVertex3f(1.0f, 1.0f, 0.0f);
  109.             gl.glColor3f(0.0f, 1.0f, 0.5f);  
  110.             gl.glVertex3f(1.0f, -1.0f, 0.0f);  
  111.             gl.glColor3f(1.0f, 0.0f, 1.0f);  //establece color
  112.             gl.glVertex3f(-1.0f, -1.0f, 0.0f);
  113.         //Done Drawing The Quad
  114.         gl.glEnd();
  115.        
  116.          gl.glTranslatef(-1.5f, -2.6f, -0.2f);
  117.         //Dibuja cuadrado
  118.         gl.glBegin(GL.GL_POLYGON);
  119.             gl.glColor3f(0.8f, 1.0f, 0.5f);    
  120.             gl.glVertex3f(-1.0f, 1.0f, 0.0f);  
  121.             gl.glColor3f(0.8f, 0.5f, 0.5f);
  122.             gl.glVertex3f(0.0f, 0.0f, 0.0f);
  123.             gl.glColor3f(1.0f, 1.0f, 0.0f);
  124.             gl.glVertex3f(-1.0f, -1.0f, 0.0f);
  125.             gl.glColor3f(0.8f, 0.0f, 1.0f);
  126.             gl.glVertex3f(-2.0f, 0.0f, 0.0f);
  127.         //Done Drawing The Quad
  128.         gl.glEnd();
  129.        
  130.         gl.glTranslatef(2.5f, -0.5f, -0.2f);
  131.         //Dibuja Poligono
  132.         gl.glBegin(GL.GL_POLYGON);
  133.             gl.glColor3f(1.0f, 0.0f, 0.3f);    
  134.             gl.glVertex3f(-1.0f, 1.0f, 0.0f);  
  135.             gl.glColor3f(0.8f, 0.5f, 1.0f);
  136.             gl.glVertex3f(-0.5f, 1.0f, 0.0f);
  137.             gl.glVertex3f(0.0f, 0.0f, 0.0f);
  138.             gl.glColor3f(1.0f, 0.7f, 0.9f);
  139.             gl.glVertex3f(0.0f, 0.5f, 0.0f);
  140.             gl.glVertex3f(-1.0f, -1.0f, 0.0f);
  141.             gl.glColor3f(1.0f, 0.7f, 0.1f);
  142.             gl.glVertex3f(-2.0f, 0.0f, 0.0f);
  143.         //Done Drawing The Quad
  144.         gl.glEnd();
  145.        
  146.         gl.glTranslatef(0.5f, -0.5f, -3.0f);
  147.         gl.glLineWidth(4.0f); //establece grosor lineas
  148.         //Dibuja lineas
  149.         gl.glBegin(GL.GL_LINES);
  150.             gl.glColor3f(0.5f, 1.0f, 0.0f);
  151.             gl.glVertex3f(1.0f, 0.0f, 0.0f);
  152.             gl.glColor3f(0.2f, 0.7f, 0.0f);
  153.             gl.glVertex3f(2.0f, 6.0f, 0.0f);
  154.         gl.glEnd();
  155.  
  156.         //Flush all drawing operations to the graphics card
  157.         gl.glFlush();
  158.        
  159.         gl.glTranslatef(0.5f, -0.5f, -3.0f);
  160.         gl.glLineWidth(4.0f); //establece grosor lineas
  161.         //Dibuja lineas
  162.         gl.glBegin(GL.GL_LINES);
  163.             gl.glColor3f(1.0f, 0.2f, 0.5f);
  164.             gl.glVertex3f(4.0f, -2.0f, 0.0f);
  165.             gl.glColor3f(0.2f, 1.0f, 0.5f);
  166.             gl.glVertex3f(1.5f, 6.0f, 0.0f);
  167.         gl.glEnd();
  168.  
  169.         //Flush all drawing operations to the graphics card
  170.         gl.glFlush();
  171.     }
  172.  
  173.     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
  174.     }
  175. }