Bilgisayar Grafikleri(2015) – OpenGL Hafta-11
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.*; // public class ornek1 extends GLCanvas implements GLEventListener { private static String BASLIK = "başlık"; private static int CIZIMALANI_GENISLIK = 640; private static int CIZIMALANI_YUKSEKLIK = 480; private static int FPS = 60; private GLU glu; public ornek1() { //yapilandirici this.addGLEventListener(this); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { GLCanvas canvas = new ornek1(); // 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.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, 640, 480); // 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, 800.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 } float aci=0f,aci1=0f; // 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(); // matrisleri sifirla // kod kismi baslangic gl.glTranslatef(0f, 0.0f, -250.0f); // koordinat gl.glRotatef(aci,0f,1f,0f); gl.glColor3f(1.0f, 1.0f, 0.0f); // GLUquadric kure1 = glu.gluNewQuadric(); //glu.gluQuadricDrawStyle(kure,GLU.GLU_FILL); glu.gluSphere(kure1,20f,15,15); glu.gluDeleteQuadric(kure1); gl.glColor3f(1.0f, 0.0f, 0.0f); // gl.glBegin(GL_QUADS); gl.glVertex3f(1f,1f,20.001f); gl.glVertex3f(-1f,1f,20.001f); gl.glVertex3f(-1f,-1f,20.001f); gl.glVertex3f(1f,-1f,20.001f); gl.glEnd(); gl.glRotatef(aci,0f,1f,0f); gl.glTranslatef(0f, 0.0f, -80.0f); // koordinat gl.glColor3f(1.0f, 1.0f, 1.0f); // gl.glRotatef(aci1,0f,-1f,0f); GLUquadric kure = glu.gluNewQuadric(); //glu.gluQuadricDrawStyle(kure,GLU.GLU_LINE); glu.gluSphere(kure,6f,15,15); glu.gluDeleteQuadric(kure); gl.glColor3f(1.0f, 0.0f, 0.0f); // gl.glBegin(GL_QUADS); gl.glVertex3f(1f,1f,6.001f); gl.glVertex3f(-1f,1f,6.001f); gl.glVertex3f(-1f,-1f,6.001f); gl.glVertex3f(1f,-1f,6.001f); gl.glEnd(); // kod kismi bitis aci+=1f; aci1+=5f; } }
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 ornek2 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 ornek2() { //yapilandirici this.addGLEventListener(this); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { GLCanvas canvas = new ornek2(); // 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 } // 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 get 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 // kod kismi baslangic gl.glTranslatef(0f, 0.0f, -20.0f); gl.glRotatef(aci,0f,1f,0f); gl.glColor3f(1.0f, 1.0f, 1.0f); for(int i=0;i<7;i++) { gl.glRotatef(60f,0f,0f,1f); gl.glBegin(GL_QUADS); gl.glVertex3f(2.0f, 3.46f, 2.0f); gl.glVertex3f(-2.0f, 3.46f, 2.0f); gl.glVertex3f(-1.0f, 1.73f, 2.0f); gl.glVertex3f(1.0f, 1.73f, 2.0f); gl.glVertex3f(2.0f, 3.46f, -2.0f); gl.glVertex3f(-2.0f, 3.46f, -2.0f); gl.glVertex3f(-1.0f, 1.73f, -2.0f); gl.glVertex3f(1.0f, 1.73f, -2.0f); gl.glVertex3f(2.0f, 3.46f, 2.0f); gl.glVertex3f(2.0f, 3.46f, -2.0f); gl.glVertex3f(-2.0f, 3.46f, -2.0f); gl.glVertex3f(-2.0f, 3.46f, 2.0f); gl.glVertex3f(1.0f, 1.73f, 2.0f); gl.glVertex3f(1.0f, 1.73f, -2.0f); gl.glVertex3f(-1.0f, 1.73f, -2.0f); gl.glVertex3f(-1.0f, 1.73f, 2.0f); gl.glEnd(); } aci+=1.5f; // kod kismi bitis } }