Bilgisayar Grafikleri(2015) – OpenGL de Nesneleri Eksenlere Göre Döndürme

grafik_ornek2_2015_1

grafik_ornek2_2015_2 grafik_ornek2_2015_3

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
import static javax.media.opengl.GL.*;  
import static javax.media.opengl.GL2.*;

public class ornek2 extends GLCanvas implements GLEventListener {

   private static String BASLIK = "Örnek2";
   private static int CIZIMALANI_GENISLIK = 320; 
   private static int CIZIMALANI_YUKSEKLIK = 240;
   private static int FPS = 60; 
   
   private GLU glu;


   public ornek2() { 
      this.addGLEventListener(this);
   }	

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            GLCanvas canvas = new ornek2(); 
            canvas.setPreferredSize(new Dimension(CIZIMALANI_GENISLIK, CIZIMALANI_YUKSEKLIK));
            final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
            JFrame frame = new JFrame();
            frame.getContentPane().add(canvas);
            frame.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                  new Thread() { 
                     public void run() {
                        if (animator.isStarted()) animator.stop();
                        System.exit(0);
                     }
                  }.start();
               }
            });
            frame.setTitle(BASLIK);
            frame.pack();
            frame.setVisible(true);
            animator.start();
         }
      });
   }
   
   public void dispose(GLAutoDrawable drawable) { }	

   // baslangic durumlari	
   public void init(GLAutoDrawable drawable) {
      GL2 gl = drawable.getGL().getGL2();      
      glu = new GLU();                         
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // arka plan rengi
   }

   // cizim alani degisince cizimi olcekle
   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
      GL2 gl = drawable.getGL().getGL2();  

      float aspect=1.2f; // olcek
      gl.glViewport(0, 0, CIZIMALANI_GENISLIK, CIZIMALANI_YUKSEKLIK);
      gl.glMatrixMode(GL_PROJECTION);  
      gl.glLoadIdentity();             
      glu.gluPerspective(45.0, aspect, 0.1, 100.0); 
      gl.glMatrixMode(GL_MODELVIEW);
      gl.glLoadIdentity(); 
   }

   private float aci_ucgen = 0.0f; // baslangic acisi
   private float aci_dortgen = 0.0f; // baslangic acisi
   private float hiz_ucgen = 0.5f; // aci artimi
   private float hiz_dortgen = -0.4f; // aci artimi

   // cizim	
   public void display(GLAutoDrawable drawable) {
      GL2 gl = drawable.getGL().getGL2();  
      gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //yeniden cizim icin tampon bellek temizligi
      
      gl.glLoadIdentity(); // model-gorunus matrisini sifirla
      gl.glTranslatef(-1.5f, 0.0f, -6.0f); // koordinatlar
      gl.glRotatef(aci_ucgen, 0.0f, 1.0f, 0.0f); // y ekseni etrafinda donus
      gl.glBegin(GL_TRIANGLES); // ucgen
         gl.glColor3f(1.0f, 0.0f, 0.0f); // kırmızı
         gl.glVertex3f(0.0f, 1.0f, 0.0f);
         gl.glColor3f(0.0f, 1.0f, 0.0f); // yesil
         gl.glVertex3f(-1.0f, -1.0f, 0.0f);
         gl.glColor3f(0.0f, 0.0f, 1.0f); // mavi
         gl.glVertex3f(1.0f, -1.0f, 0.0f);
      gl.glEnd();

      gl.glLoadIdentity(); // model-gorunus matrisini sifirla
      gl.glTranslatef(1.5f, 0.0f, -6.0f); // koordinatlar
      gl.glRotatef(aci_dortgen, 1.0f, 0.0f, 0.0f); // x ekseni etrafinda donus
      gl.glColor3f(1.0f, 0.0f, 0.0f); // kirmizi
      gl.glBegin(GL_QUADS); // dortgen
         gl.glVertex3f(-1.0f, 1.0f, 0.0f);
         gl.glVertex3f(1.0f, 1.0f, 0.0f);
         gl.glVertex3f(1.0f, -1.0f, 0.0f);
         gl.glVertex3f(-1.0f, -1.0f, 0.0f);
      gl.glEnd();

      aci_ucgen += hiz_ucgen; // aciyi arttir
      aci_dortgen += hiz_dortgen; // aciyi arttir


   }
}

 

Share