Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Figuras;
- import com.sun.opengl.util.Animator;
- import com.sun.opengl.util.GLUT;
- import java.awt.Frame;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.media.opengl.GL;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLCanvas;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.glu.GLU;
- /**
- * TestOpenGL
- * author: Rafa Gallardo
- *
- */
- public class EsferaIluminacion implements GLEventListener {
- public static void main(String[] args) {
- Frame frame = new Frame("OpenGl Graficos");
- GLCanvas canvas = new GLCanvas();
- canvas.addGLEventListener(new EsferaIluminacion());
- frame.add(canvas);
- frame.setSize(640, 550);
- final Animator animator = new Animator(canvas);
- frame.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- // Ejecutar este en otro hilo de la cola de eventos AWT para
- // asegúrese de que la llamada a Animator.stop () finaliza antes de
- // salir
- new Thread(new Runnable() {
- public void run() {
- animator.stop();
- System.exit(0);
- }
- }).start();
- }
- });
- //Centrar Frame
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- animator.start();
- }
- public void init(GLAutoDrawable drawable) {
- display(drawable);
- GL gl = drawable.getGL();
- System.err.println("INIT GL IS: " + gl.getClass().getName());
- //Enable VSync
- gl.setSwapInterval(1);
- //Setup the drawing area and shading mode
- gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
- }
- public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
- GL gl = drawable.getGL();
- GLU glu = new GLU();
- if (height <= 0) { // avoid a divide by zero error!
- height = 1;
- }
- final float h = (float) width / (float) height;
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL.GL_PROJECTION);
- gl.glLoadIdentity();
- glu.gluPerspective(45.0f, h, 1.0, 20.0);
- gl.glMatrixMode(GL.GL_MODELVIEW);
- gl.glLoadIdentity();
- }
- public void display(GLAutoDrawable drawable) {
- GL gl = drawable.getGL();
- GLUT glut = new GLUT();
- GLU glu = new GLU();
- //Clear the drawing area
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
- //Reset the current matrix to the "identity"
- gl.glLoadIdentity();
- //creamos la ESFERA
- gl.glTranslatef(0.0f, 0.0f, -4.0f); //Permite enfocar la esfera
- float difuso[] = {0.0f, 0.0f, 1.0f, 1.0f};
- float ambiente[] = {0.0f, 0.0f, 0.0f, 1.0f};
- float LuzPosicion[] = {0.0f, 1.0f, 1.0f, 1.0f};
- float especular[] = {1.0f, 1.0f, 0.0f, 0.0f};
- gl.glEnable(gl.GL_LIGHTING);
- gl.glEnable(gl.GL_LIGHT0);
- // gl.glEnable(gl.GL_DEPTH_TEST);
- gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, difuso, 2);
- gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, ambiente, 2);
- gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, difuso, 1);
- gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, especular, 2);
- gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, LuzPosicion, 1);
- //Enfoca la esfera en la ventana
- glu.gluLookAt(1, 1, 5, 0, 0, 0, 1, 1, 0);
- //Dibuja la esfera
- glut.glutSolidSphere(2d, 30, 50);
- gl.glFlush();
- }
- public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement