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 PuntosOpenGL implements GLEventListener {
- private float velRotacion = 0.0f; //controla velocidad de rotacion de la esfera
- private float vel = 0.0f;
- public static final double Pi = 3.14159265358979323846;
- public static void main(String[] args) {
- Frame frame = new Frame("OpenGl Graficos");
- GLCanvas canvas = new GLCanvas();
- canvas.addGLEventListener(new PuntosOpenGL());
- 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();
- velRotacion += 0.5f; //Velocidad a la que rota la esfera
- vel += 0.02f;
- //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
- //Dibuja circunferencia grande
- gl.glPointSize(5);
- gl.glColor3f(1.0f, 0.4f, 0.8f);
- gl.glBegin(gl.GL_POINTS);
- for(float angul = 0.0f; angul<2*Pi; angul +=0.1){
- float x = (float) ((float) 1.5*Math.sin(angul));
- float y = (float) ((float) 1.5*Math.cos(angul));
- gl.glVertex2f(x,y);
- }
- gl.glEnd();
- //Dibuja circunferencia mediana
- gl.glRotatef(vel, 0.0f, 0.0f, -1.0f);
- gl.glPointSize(5);
- gl.glColor3f(0.0f, 1.0f, 1.0f);
- gl.glBegin(gl.GL_POINTS);
- for(float angulo = 0.0f; angulo<2*Pi; angulo +=0.1){
- float x = (float) ((float) 1*Math.sin(angulo));
- float y = (float) ((float) 1*Math.cos(angulo));
- gl.glVertex2f(x,y);
- }
- gl.glEnd();
- //Dibuja circunferencia pequena
- // gl.glRotatef(vel, 0.0f, 0.0f, -1.0f);
- gl.glPointSize(5);
- gl.glColor3f(0.4f, 1.0f, 0.0f);
- gl.glBegin(gl.GL_POINTS);
- for(float aungulo = 0.0f; aungulo<2*Pi; aungulo +=0.05){ //ANG
- float x = (float) ((float) 0.5*Math.sin(aungulo));
- float y = (float) ((float) 0.5*Math.cos(aungulo));
- gl.glVertex2f(x,y);
- }
- gl.glEnd();
- gl.glFlush();
- }
- public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement