Bilgisayar Grafikleri(2015) – OpenGL de Küp Oluşturma

grafik_kup_2015_1

grafik_kup_2015_2 grafik_kup_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 ornek3 extends GLCanvas implements GLEventListener {

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


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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            GLCanvas canvas = new ornek3(); 
            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
      gl.glClearDepth(1.0f); // derinlik tamponunu temizle	
      gl.glEnable(GL_DEPTH_TEST); // derinlik testi aktif
      gl.glDepthFunc(GL_LESS); // gelen z degeri ile mevcut z degeri karsilastirilir, esit veya kucukse basarili.	
   }

   // 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_kup_x = 0.0f; // baslangic acisi
   private float aci_kup_y = 0.0f; // baslangic acisi
   private float aci_kup_z = 0.0f; // baslangic acisi		

   // 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(0.0f, 0.0f, -10.0f); // koordinatlar
      gl.glRotatef(aci_kup_x, 1.0f, 0.0f, 0.0f); // x ekseni etrafinda donus
      gl.glRotatef(aci_kup_y, 0.0f, 1.0f, 0.0f); // y ekseni etrafinda donus
      gl.glRotatef(aci_kup_z, 0.0f, 0.0f, 1.0f); // z ekseni etrafinda donus		
      gl.glBegin(GL_QUADS); // kup
         gl.glColor3f(1.0f, 0.0f, 0.0f); // kırmızı
         gl.glVertex3f(1.0f, 1.0f, 1.0f);
         gl.glVertex3f(-1.0f, 1.0f, 1.0f);
         gl.glVertex3f(-1.0f, -1.0f, 1.0f);
	 gl.glVertex3f(1.0f, -1.0f, 1.0f);

	 gl.glColor3f(0.0f, 0.0f, 1.0f); // mavi
         gl.glVertex3f(1.0f, 1.0f, -1.0f);
         gl.glVertex3f(1.0f, 1.0f, 1.0f);
         gl.glVertex3f(1.0f, -1.0f, 1.0f);
	 gl.glVertex3f(1.0f, -1.0f, -1.0f);

	 gl.glColor3f(0.0f, 1.0f, 0.0f); // yesil
         gl.glVertex3f(1.0f, 1.0f, -1.0f);
         gl.glVertex3f(-1.0f, 1.0f, -1.0f);
         gl.glVertex3f(-1.0f, -1.0f, -1.0f);
	 gl.glVertex3f(1.0f, -1.0f, -1.0f);


	 gl.glColor3f(1.0f, 1.0f, 0.0f); // kırmızı+yesil
         gl.glVertex3f(-1.0f, 1.0f, -1.0f);
         gl.glVertex3f(-1.0f, 1.0f, 1.0f);
         gl.glVertex3f(-1.0f, -1.0f, 1.0f);
	 gl.glVertex3f(-1.0f, -1.0f, -1.0f);

	 gl.glColor3f(1.0f, 0.0f, 1.0f); // kırmızı+mavi
	 gl.glVertex3f(1.0f, 1.0f, -1.0f);
	 gl.glVertex3f(-1.0f, 1.0f, -1.0f);
	 gl.glVertex3f(-1.0f, 1.0f, 1.0f);
	 gl.glVertex3f(1.0f, 1.0f, 1.0f);

	 gl.glColor3f(0.0f, 1.0f, 1.0f); // yesil+mavi
	 gl.glVertex3f(1.0f, -1.0f, -1.0f);
	 gl.glVertex3f(-1.0f, -1.0f, -1.0f);
	 gl.glVertex3f(-1.0f, -1.0f, 1.0f);
	 gl.glVertex3f(1.0f, -1.0f, 1.0f);
      gl.glEnd();

	aci_kup_x+=0.5f;
	aci_kup_y+=0.8f;
	aci_kup_z+=2.0f;

   }
}

 

Share