Bilgisayar Grafikleri(2015) – OpenGL Hafta-12

bilgisayar_grafikleri_2015_hafta12_resim3

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.*; //

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 aci=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

	vertex[] v = new vertex[8];
	v[0] = new vertex(1.0f, 0f, 1.0f);
	v[1] = new vertex(1.0f, 0f, -1.0f);
	v[2] = new vertex(-1.0f, 0f, -1.0f);
	v[3] = new vertex(-1.0f, 0f, 1.0f);
	v[4] = new vertex(0.5f, 3.0f, 0.5f);
	v[5] = new vertex(0.5f, 3.0f, -0.5f);
	v[6] = new vertex(-0.5f, 3.0f, -0.5f);
	v[7] = new vertex(-0.5f, 3.0f, 0.5f);


	int[][] f = new int[6][];
	f[0] = new int[]{1,2,3,4};
	f[1] = new int[]{1,2,6,5};
	f[2] = new int[]{5,6,7,8};
	f[3] = new int[]{2,6,7,3};
	f[4] = new int[]{3,7,8,4};
	f[5] = new int[]{1,5,8,4};
	


	// kod kismi baslangic
      gl.glTranslatef(0f, 0.0f, -15.0f); // koordinat 
      gl.glRotatef(aci,1f,1f,1f);

      gl.glColor3f(1.0f, 1.0f, 1.0f);	
      gl.glBegin(GL_QUADS); // dortgen

	for(int i=0;i<f.length;i++)
	{
		gl.glColor3f(0.1f*(i+0.2), 0.3f, 0.2f*(i));
		for(int j=0;j<f[i].length;j++)
		{
			int index=f[i][j]-1;
			v[index].vertex3f(gl);
		}
		
	}
	

      gl.glEnd();
	// kod kismi bitis
	aci+=0.5f;	
   }
}

class vertex{
public float x=0f,y=0f,z=0f;
vertex(float xx, float yy, float zz)
{
	x=xx;
	y=yy;
	z=zz;
}
public void vertex3f(GL2 gl)
{
	gl.glVertex3f(x,y,z);	
}
}

 

 

bilgisayar_grafikleri_2015_hafta12_resim2

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.*; //

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 aci=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

	vertex[] v = new vertex[5];
	v[0] = new vertex(1.0f, 0f, 1.0f);
	v[1] = new vertex(1.0f, 0f, -1.0f);
	v[2] = new vertex(-1.0f, 0f, -1.0f);
	v[3] = new vertex(-1.0f, 0f, 1.0f);
	v[4] = new vertex(0f, 1.41f, 0f);


	int[][] f = new int[6][];
	f[0] = new int[]{1,2,4};
	f[1] = new int[]{2,3,4};
	f[2] = new int[]{1,2,5};
	f[3] = new int[]{2,3,5};
	f[4] = new int[]{3,5,4};
	f[5] = new int[]{1,5,4};
	


	// kod kismi baslangic
      gl.glTranslatef(0f, 0.0f, -6.0f); // koordinat 
      gl.glRotatef(aci,1f,1f,1f);

      gl.glColor3f(1.0f, 1.0f, 1.0f);	
      gl.glBegin(GL_TRIANGLES); // dortgen

	for(int i=0;i<f.length;i++)
	{
		gl.glColor3f(0.1f+(i*0.3f), 0.3f+(i*0.1f), 0.2f+(i*0.2f));
		for(int j=0;j<f[i].length;j++)
		{
			int index=f[i][j]-1;
			v[index].vertex3f(gl);
		}
		
	}
	

      gl.glEnd();
	// kod kismi bitis
	aci+=0.5f;	
   }
}

class vertex{
public float x=0f,y=0f,z=0f;
vertex(float xx, float yy, float zz)
{
	x=xx;
	y=yy;
	z=zz;
}
public void vertex3f(GL2 gl)
{
	gl.glVertex3f(x,y,z);	
}
}

 

 

 

bilgisayar_grafikleri_2015_hafta12_resim1

 

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.*; //

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 aci=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

	vertex[] v = new vertex[5];
	v[0] = new vertex(1.0f, 0f, 1.0f);
	v[1] = new vertex(1.0f, 0f, -1.0f);
	v[2] = new vertex(-1.0f, 0f, -1.0f);
	v[3] = new vertex(-1.0f, 0f, 1.0f);
	v[4] = new vertex(0f, 1.41f, 0f);


	int[][][] f = new int[6][2][];
	f[0][0] = new int[]{1,2,4};
	f[1][0] = new int[]{2,3,4};
	f[2][0] = new int[]{1,2,5};
	f[3][0] = new int[]{2,3,5};
	f[4][0] = new int[]{3,5,4};
	f[5][0] = new int[]{1,5,4};

	// yuzey renk tanimlari 0-10 araliginda R-G-B
	f[0][1] = new int[]{10,10,10};
	f[1][1] = new int[]{10,10,10};
	f[2][1] = new int[]{10,0,0};
	f[3][1] = new int[]{0,10,0};
	f[4][1] = new int[]{0,0,10};
	f[5][1] = new int[]{1,5,4};
	


	// kod kismi baslangic
      gl.glTranslatef(0f, 0.0f, -6.0f); // koordinat 
      gl.glRotatef(aci,1f,1f,1f);

      gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glBegin(GL_TRIANGLES); // dortgen

	for(int i=0;i<f.length;i++)
	{
		gl.glColor3f(0.1f*f[i][1][0], 0.1f*f[i][1][1], 0.1f*f[i][1][2]);
		for(int j=0;j<f[i][0].length;j++)
		{
			int index=f[i][0][j]-1;
			v[index].vertex3f(gl);
		}
		
	}
	

      gl.glEnd();
	// kod kismi bitis
	aci+=0.5f;	
   }
}

class vertex{
public float x=0f,y=0f,z=0f;
vertex(float xx, float yy, float zz)
{
	x=xx;
	y=yy;
	z=zz;
}
public void vertex3f(GL2 gl)
{
	gl.glVertex3f(x,y,z);	
}
}

Bir maymun(Suzanne) çizimine ait yüzey ve vertexler

Share