Bilgisayar Grafikleri(2015) – OpenGL Hafta-9

bilgisayar_grafikleri_hafta-9-resim-1

bilgisayar_grafikleri_hafta-9-resim-2

 

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.*;  // GL sabitleri
import static javax.media.opengl.GL2.*; //


public class ev extends GLCanvas implements GLEventListener {

   private static String BASLIK = "Bilgisayar Grafikleri Hafta-9 - Ev";
   private static int CIZIMALANI_GENISLIK = 320; 
   private static int CIZIMALANI_YUKSEKLIK = 240;
   private static int FPS = 60; 
   
   private GLU glu;

   public ev() { //yapilandirici
      this.addGLEventListener(this);
   }	

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            GLCanvas canvas = new ev(); // upcasting, sınıftan nesne
            canvas.setPreferredSize(new Dimension(CIZIMALANI_GENISLIK, CIZIMALANI_YUKSEKLIK)); // cizim alani boyut tanimlamasi
            final FPSAnimator animator = new FPSAnimator(canvas, FPS, true); // cizim alani 
            JFrame frame = new JFrame(BASLIK); // pencere nesnesi
            frame.getContentPane().add(canvas); // cizim alaninin pencereye eklenmesi
            frame.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) { // program sonlandildiginda
                  new Thread() { 
                     public void run() {
                        if (animator.isStarted()) animator.stop(); // calisiyorsa durdur
                        System.exit(0);
                     }
                  }.start();
               }
            });
            frame.pack(); // pencere boyutunu icerige gore ayarla
            frame.setVisible(true); // pencereyi gorunur yap
            animator.start(); // cizimi baslat
         }
      });
   }
   
   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.glEnable(GL_DEPTH_TEST);	
   }

   // 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=(float)1.2; // olcek
      gl.glViewport(0, 0, 320, 240); // gorunecek sahnenin pencere uzerindeki koordinatlari ve boyutu (gorus alani) 
      gl.glMatrixMode(GL_PROJECTION);  //izdusum matrisini sec
      gl.glLoadIdentity();  // matrisi baslangic durumuna getir           
      glu.gluPerspective(45.0, aspect, 0.1, 100.0); // persipektif goruntu kameranin bulundugu noktan cikan isinlara ait aci, yeni cercevenin en/boy orani, yeni cercevenin bakis noktasina olan yakinlik ve uzakligi  
      gl.glMatrixMode(GL_MODELVIEW); // model matrisini sec
      gl.glLoadIdentity(); // matrisi baslangic durumuna getir
   }

   // cizim	
   float aci=0.0f;
   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(); // matrisleri sifirla

	// kod kismi baslangic
      gl.glTranslatef(0.0f, 0.0f, -50.0f); // koordinat 
      gl.glRotatef(aci,0.0f,1.0f,0.0f);	
      gl.glColor3f(0.0f, 0.0f, 1.0f); // mavi
      gl.glBegin(GL_QUADS); 
         gl.glVertex3f(0.0f, 0.0f, 8.0f);
	 gl.glVertex3f(8.0f, 0.0f, 8.0f);
	 gl.glVertex3f(8.0f, 4.0f, 8.0f);
	 gl.glVertex3f(0.0f, 4.0f, 8.0f);
      gl.glEnd();

      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(8.0f, 4.0f, 8.0f);
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
         gl.glVertex3f(3.0f, 10.0f, 8.0f);
	 gl.glVertex3f(3.0f, 4.0f, 8.0f);
      gl.glEnd();

      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(3.0f, 6.0f, 8.0f);
	 gl.glVertex3f(3.0f, 10.0f, 8.0f);
         gl.glVertex3f(0.0f, 10.0f, 8.0f);
	 gl.glVertex3f(0.0f, 6.0f, 8.0f);
      gl.glEnd();

      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(0.0f, 0.0f, 8.0f);
	 gl.glVertex3f(0.0f, 10.0f, 8.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(-8.0f, 0.0f, 8.0f);
      gl.glEnd();

      gl.glColor3f(1.0f, 0.0f, 0.0f); // kirmizi kapi
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(-5.0f, 6.0f, 8.01f);
	 gl.glVertex3f(-8.0f, 6.0f, 8.01f);
         gl.glVertex3f(-8.0f, 0.0f, 8.01f);
	 gl.glVertex3f(-5.0f, 0.0f, 8.01f);
      gl.glEnd();

      // cati
      gl.glColor3f(0.25f, 0.5f, 0.8f); //
      gl.glBegin(GL_TRIANGLES); 
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(0.0f, 15.0f, 0.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
      gl.glEnd();

      // baca

      gl.glColor3f(1.0f, 1.0f, 1.0f); 
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(6.0f, 11.0f, 0.5f);
	 gl.glVertex3f(6.0f, 15.0f, 0.5f);
	 gl.glVertex3f(5.0f, 15.0f, 0.5f);
	 gl.glVertex3f(5.0f, 11.0f, 0.5f);

	 gl.glVertex3f(6.0f, 11.0f, -0.5f);
	 gl.glVertex3f(6.0f, 15.0f, -0.5f);
	 gl.glVertex3f(5.0f, 15.0f, -0.5f);
	 gl.glVertex3f(5.0f, 11.0f, -0.5f);

	 gl.glVertex3f(6.0f, 11.0f, 0.5f);
	 gl.glVertex3f(6.0f, 11.0f, -0.5f);
	 gl.glVertex3f(6.0f, 15.0f, -0.5f);
	 gl.glVertex3f(6.0f, 15.0f, 0.5f);

         gl.glVertex3f(5.0f, 11.0f, 0.5f);
	 gl.glVertex3f(5.0f, 11.0f, -0.5f);
	 gl.glVertex3f(5.0f, 15.0f, -0.5f);
	 gl.glVertex3f(5.0f, 15.0f, 0.5f);


      gl.glEnd();

        // tavan

      gl.glColor3f(1.0f, 1.0f, 1.0f); 
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(8.0f, 10.0f, -8.0f);
	 gl.glVertex3f(-8.0f, 10.0f, -8.0f);
	 gl.glVertex3f(-8.0f, 10.0f, 8.0f);

      gl.glEnd();
	// taban	

      gl.glColor3f(1.0f, 1.0f, 1.0f); 
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(8.0f, 0.0f, 8.0f);
	 gl.glVertex3f(8.0f, 0.0f, -8.0f);
	 gl.glVertex3f(-8.0f, 0.0f, -8.0f);
	 gl.glVertex3f(-8.0f, 0.0f, 8.0f);

      gl.glEnd();

	// diger duvarlar
      gl.glRotatef(90.0f,0.0f,1.0f,0.0f);

      gl.glColor3f(0.0f, 1.0f, 0.0f); 
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(8.0f, 0.0f, 8.0f);
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(-8.0f, 0.0f, 8.0f);
      gl.glEnd();
      // cati
        gl.glColor3f(0.25f, 0.5f, 0.8f); //
      gl.glBegin(GL_TRIANGLES); 
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(0.0f, 15.0f, 0.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
      gl.glEnd();

      

      gl.glRotatef(90.0f,0.0f,1.0f,0.0f);

      gl.glColor3f(0.0f, 1.0f, 1.0f); 
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(8.0f, 0.0f, 8.0f);
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(-8.0f, 0.0f, 8.0f);
      gl.glEnd();
      // cati
        gl.glColor3f(0.25f, 0.5f, 0.8f); //
      gl.glBegin(GL_TRIANGLES); 
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(0.0f, 15.0f, 0.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
      gl.glEnd();	

      gl.glRotatef(90.0f,0.0f,1.0f,0.0f);

      gl.glColor3f(1.0f, 1.0f, 0.0f); 
      gl.glBegin(GL_QUADS); 
	 gl.glVertex3f(8.0f, 0.0f, 8.0f);
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(-8.0f, 0.0f, 8.0f);
      gl.glEnd();
      // cati
      gl.glColor3f(0.25f, 0.5f, 0.8f); //
      gl.glBegin(GL_TRIANGLES); 
	 gl.glVertex3f(8.0f, 10.0f, 8.0f);
	 gl.glVertex3f(0.0f, 15.0f, 0.0f);
         gl.glVertex3f(-8.0f, 10.0f, 8.0f);
      gl.glEnd();

	aci+=0.5f;

	// kod kismi bitis
   }
}

 

Share