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