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 Cuarto3D implements GLEventListener {
  20.     public static final double Pi = 3.14159265358979323846;
  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 Cuarto3D());
  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.  
  53.     public void init(GLAutoDrawable drawable) {
  54.         display(drawable);
  55.        
  56.         GL gl = drawable.getGL();
  57.         System.err.println("INIT GL IS: " + gl.getClass().getName());
  58.  
  59.         //Enable VSync
  60.         gl.setSwapInterval(1);
  61.  
  62.         //Setup the drawing area and shading mode
  63.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  64.         gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
  65.     }
  66.  
  67.     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
  68.         GL gl = drawable.getGL();
  69.         GLU glu = new GLU();
  70.  
  71.         if (height <= 0) { // avoid a divide by zero error!
  72.             height = 1;
  73.         }
  74.        
  75.         final float h = (float) width / (float) height;
  76.         gl.glViewport(0, 0, width, height);
  77.         gl.glMatrixMode(GL.GL_PROJECTION);
  78.         gl.glLoadIdentity();
  79.         glu.gluPerspective(45.0f, h, 1.0, 20.0);
  80.         gl.glMatrixMode(GL.GL_MODELVIEW);
  81.         gl.glLoadIdentity();
  82.     }
  83.  
  84.     public void display(GLAutoDrawable drawable) {
  85.         GL gl = drawable.getGL();
  86.         GLUT glut = new GLUT();
  87.         GLU glu = new GLU();
  88.      
  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, -4.0f); //Permite enfocar
  97.        
  98.         //Dibujo piso
  99.         gl.glColor3f(0.36f, 0.278f, 0.196f);
  100.         gl.glBegin(gl.GL_QUADS);
  101.             gl.glVertex3f(-2.0f, -1.7f, 0.0f);
  102.             gl.glVertex3f(2.0f, -1.7f, 0.0f);
  103.             gl.glVertex3f(2.0f, -0.5f, 0.0f);
  104.             gl.glVertex3f(-0.5f, -0.5f, 0.0f);
  105.         gl.glEnd();
  106.        
  107.         //dibuja pared izquierda
  108.         gl.glColor3f(0.176f, 0.475f, 0.423f);
  109.         gl.glBegin(gl.GL_QUADS);
  110.             gl.glVertex3f(-2.0f, -1.7f, 0.0f);
  111.             gl.glVertex3f(-0.5f, -0.5f, 0.0f);
  112.             gl.glVertex3f(-0.5f, 1.7f, 0.0f);
  113.             gl.glVertex3f(-2.0f, 1.7f, 0.0f);
  114.         gl.glEnd();
  115.        
  116.         //dibuja pared derecha
  117.          gl.glColor3f(0.176f, 0.675f, 0.433f);
  118.          gl.glBegin(gl.GL_QUADS);
  119.             gl.glVertex3f(-0.5f, 1.7f, 0.0f);
  120.             gl.glVertex3f(2.0f, 1.7f, 0.0f);
  121.             gl.glVertex3f(2.0f, -0.5f, 0.0f);
  122.             gl.glVertex3f(-0.5f, -0.5f, 0.0f);
  123.         gl.glEnd();
  124.        
  125.         //dibuja marco exterior ventana
  126.         gl.glColor3f(0.0f, 0.0f, 0.0f);
  127.          gl.glBegin(gl.GL_QUADS);
  128.             gl.glVertex3f(0.0f, 0.2f, 0.0f);
  129.             gl.glVertex3f(1.0f, 0.2f, 0.0f);
  130.             gl.glVertex3f(1.0f, 1.2f, 0.0f);
  131.             gl.glVertex3f(0.0f, 1.2f, 0.0f);
  132.         gl.glEnd();
  133.        
  134.         //dibuja marco interno ventana
  135.         gl.glColor3f(0.314f, 0.74f, 0.894f);
  136.         gl.glBegin(gl.GL_QUADS);
  137.             gl.glVertex3f(0.2f, 0.35f, 0.0f);
  138.             gl.glVertex3f(1.0f, 0.35f, 0.0f);
  139.             gl.glVertex3f(1.0f, 1.2f, 0.0f);
  140.             gl.glVertex3f(0.2f, 1.2f, 0.0f);
  141.         gl.glEnd();
  142.        
  143.         //Para inferior ventana
  144.         gl.glColor3f(0.36f, 0.278f, 0.196f);
  145.         gl.glBegin(gl.GL_QUADS);
  146.             gl.glVertex3f(0.0f, 0.2f, 0.0f);
  147.             gl.glVertex3f(1.0f, 0.2f, 0.0f);
  148.             gl.glVertex3f(1.0f, 0.35f, 0.0f);
  149.             gl.glVertex3f(0.2f, 0.35f, 0.0f);
  150.         gl.glEnd();
  151.         //gl.glTranslatef(0.0f, 0.0f, -4.0f); //Permite enfocar
  152.        
  153.           //Dibuja cuadro pared izquierda
  154.         gl.glColor3f(0.57f, 0.376f, 0.0f);
  155.         gl.glBegin(gl.GL_QUADS);
  156.             gl.glVertex3f(-1.6f, -0.5f, 0.0f);
  157.             gl.glVertex3f(-0.7f, 0.1f, 0.0f);
  158.             gl.glVertex3f(-0.7f, 1.1f, 0.0f);
  159.             gl.glVertex3f(-1.6f, 0.5f, 0.0f);
  160.         gl.glEnd();
  161.        
  162.          //Dibuja cuarteto de la pared izquierda
  163.         gl.glColor3f(1.0f, 0.0f, 0.0f);
  164.         gl.glBegin(gl.GL_QUADS);
  165.             gl.glVertex3f(-1.15f, -0.2f, 0.0f);
  166.             gl.glVertex3f(-0.7f, 0.6f, 0.0f);
  167.             gl.glVertex3f(-1.15f, 0.8f, 0.0f);
  168.             gl.glVertex3f(-1.6f, 0.0f, 0.0f);
  169.         gl.glEnd();
  170.        
  171.         gl.glTranslatef(2.8f, 3.5f, -12.0f); //Permite enfocar el sol
  172.         gl.glColor3f(1.0f, 1.0f, 0.0f); //pinta de amarillo el sol
  173.         //Dibuja el sol
  174.         gl.glBegin(gl.GL_POLYGON);
  175.             for(int i=0; i<100; i++){
  176.                float x = (float) Math.cos(i*2*Pi/100);
  177.                float y = (float) Math.sin(i*2*Pi/100);
  178.                gl.glVertex2f(x, y);
  179.             }
  180.         gl.glEnd();
  181.        
  182.      
  183.        
  184.        
  185.         gl.glFlush();
  186.     }
  187.  
  188.     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
  189.     }
  190. }