Bilgisayar Grafikleri(2016) – OpenGL Hafta-10

grafik_2016_hafta10_1

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

//sudo apt-get install libjogl2-java
//.bashrc classpath tanimlamasi
//export CLASSPATH=/usr/share/java/jogl2.jar:/usr/share/java/gluegen2-rt.jar:$CLASSPATH


class vertex{
	float x,y,z;
	vertex(float x,float y, float z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}
	void olustur(GL2 gl)
	{
		 gl.glVertex3f(x, y, z);
	}


}


public class taslak extends GLCanvas implements GLEventListener {

   private static String BASLIK = "başlık";
   private static int CIZIMALANI_GENISLIK = 320; 
   private static int CIZIMALANI_YUKSEKLIK = 240;
   private static int FPS = 60; 
   
   private GLU glu;

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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            GLCanvas canvas = new taslak(); // 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) { }	// kaynaklari serbest birakma 

   // baslangic durumlari

   vertex[] noktalar;		
   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);

      noktalar= new vertex[8];
      noktalar[0]=new vertex(1f,1f,1f); // A
      noktalar[1]=new vertex(1f,-1f,1f); // B
      noktalar[2]=new vertex(-1f,-1f,1f); // C
      noktalar[3]=new vertex(-1f,1f,1f); // D

      noktalar[4]=new vertex(1f,1f,-1f); // A'
      noktalar[5]=new vertex(1f,-1f,-1f); // B'
      noktalar[6]=new vertex(-1f,-1f,-1f); // C'
      noktalar[7]=new vertex(-1f,1f,-1f); // D'


   }

   public void cizdir(GL2 gl, int[] index)
   {
      gl.glBegin(GL_QUADS); 
        for(int i=0;i<index.length;i++)
		noktalar[index[i]].olustur(gl);
      gl.glEnd();
 
   } 		


   // 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 aci1=0f, aci2=0f, aci3=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

      //gl.glRotatef(aci, 0f, 1f, 0f);	
      gl.glTranslatef(0f, 0.0f, -10.0f);
      gl.glRotatef(aci1, 0f, 1f, 0f);	
      gl.glColor3f(0.0f, 0.0f, 1.0f); 
      cizdir(gl,new int[]{0,1,2,3}); // on	
      cizdir(gl,new int[]{0,1,5,4}); // sag yan
      cizdir(gl,new int[]{4,5,6,7}); // arka
      cizdir(gl,new int[]{4,0,3,7}); // ust
      cizdir(gl,new int[]{3,7,6,2}); // sol
      cizdir(gl,new int[]{1,5,6,2}); // alt
      aci1+=0.5f;

      gl.glRotatef(aci2, 0f, 1f, 0f); // kirmizinin mavi etrafindaki konumu
      gl.glTranslatef(3f, 0.0f, 0f);
      gl.glRotatef(aci3, 0f, 1f, 0f); // kirmizinin kendi ekseni etrafindaki konumu	
      gl.glColor3f(1.0f, 0.0f, 0.0f); 
      cizdir(gl,new int[]{0,1,2,3}); // on	
      cizdir(gl,new int[]{0,1,5,4}); // sag yan
      cizdir(gl,new int[]{4,5,6,7}); // arka
      cizdir(gl,new int[]{4,0,3,7}); // ust
      cizdir(gl,new int[]{3,7,6,2}); // sol
      cizdir(gl,new int[]{1,5,6,2}); // alt
      aci2-=0.8f;
      aci3+=2f;	


   }
}

 

grafik_2016_hafta10_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.*;
import com.jogamp.opengl.util.FPSAnimator;
import static javax.media.opengl.GL.*;  // GL sabitlerini import et
import static javax.media.opengl.GL2.*; //

//sudo apt-get install libjogl2-java
//.bashrc classpath tanimlamasi
//export CLASSPATH=/usr/share/java/jogl2.jar:/usr/share/java/gluegen2-rt.jar:$CLASSPATH


public class taslak extends GLCanvas implements GLEventListener {

   private static String BASLIK = "başlık";
   private static int CIZIMALANI_GENISLIK = 320; 
   private static int CIZIMALANI_YUKSEKLIK = 240;
   private static int FPS = 60; 
   
   private GLU glu;

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

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            GLCanvas canvas = new taslak(); // 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) { }	// kaynaklari serbest birakma 

   // 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 aci1=0f,aci2=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
 	

      gl.glTranslatef(0f, 0.0f, -30.0f);
      gl.glColor3f(1.0f, 1.0f, 0.0f);	// sarı
      GLUquadric gunes = glu.gluNewQuadric();
      glu.gluSphere(gunes,3f,15,15);
      glu.gluDeleteQuadric(gunes);

      gl.glRotatef(aci2,0.3f,1f,0f); // dunyanin gunes etrafindaki donusu icin
      gl.glTranslatef(10f, 0.0f, 0.0f);
      // gl.glRotatef(aci3,0f,1f,0f); // dunyanin kendi eksen donusu icin
      gl.glColor3f(0.0f, 0.0f, 1.0f);	// sarı
      GLUquadric dunya = glu.gluNewQuadric();
      glu.gluSphere(dunya,1f,15,15);
      glu.gluDeleteQuadric(dunya);	

      gl.glRotatef(aci1,0f,1f,0f);	
      gl.glTranslatef(2f, 0.0f, 0.0f);
      gl.glColor3f(1.0f, 1.0f, 1.0f);
      GLUquadric ay = glu.gluNewQuadric();
      glu.gluSphere(ay,0.1f,15,15);
      glu.gluDeleteQuadric(ay);


      aci1+=0.6f;
      aci2+=0.2f;	
   }
}

 

 

Bilgisayar Grafikleri 2016 Hafta-10 Uygulamaları

Share