Modul 2 (Pengantar OpenGL)

Contoh Dasar syntax Open GL yaitu membuat objek titik pada Dev C++ OpenGL.

Berikut Langkah-Langkahnya :

  1. Klik File > New > Project > OK
2. Setelah New Project akan tampil jenis lembar kerja yang akan digunakan, klik Console Application > OK

3. Setelah itu Save Project, klik Save

4. Klik kanan pada Bar Project, lalu pilih Project Options

5. Pada Project Options, klik Parameters > Linker
          Pada Box Linker ketik 
-lopengl32, 
-lfreeglut, 
-lglu32, 
lalu klik lah OK

6. Pada lembar kerja akan muncul Script seperti ini,


 7. Kosongkan script dan Ketikan script seperti dibawah ini :
  1.  #include <GL/glut.h>
  2.  
  3.         #include <GL/gl.h>
  4.  
  5.         #include <windows.h>
  6.  
  7.  
  8.  
  9.         void userdraw () {
  10.  
  11.  
  12.  
  13.         glBegin (GL_POINTS);
  14.  
  15.       glVertex2i (150,0);
  16.  
  17.       glVertex2i (150,200);
  18.  
  19.       glVertex2i (370,200);
  20.  
  21.  
  22.  
  23.         glEnd ();
  24.  
  25.         }
  26.  
  27.  
  28.  
  29.         void display (void) {
  30.  
  31.              glClear (GL_COLOR_BUFFER_BIT);
  32.  
  33.              userdraw ();
  34.  
  35.                 glutSwapBuffers ();
  36.  
  37.         }
  38.  
  39.         int main (int argc, char **argv) {
  40.  
  41.         glutInit (&argc,argv);
  42.  
  43.         glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGB);
  44.  
  45.         glutInitWindowSize (640,480);
  46.  
  47.         glutCreateWindow ("Membuat Objek Titik PUTRI PRATIWI");
  48.  
  49.         glClearColor (0.0, 0.0, 0.0, 0.0);
  50.  
  51.         gluOrtho2D (0.,640.,-240.,240.);
  52.  
  53.         glutIdleFunc (display);
  54.  
  55.         glutDisplayFunc (display);
  56.  
  57.         glutMainLoop ();
  58.  
  59.         return 0;
  60.  
  61.         }

 8. Compile dan Run (Execute, Compile & Run) dan output akan seperti ini : 




Setelah berhasil membuatnya mari kerjakan soal berikut : 
  1. Buat gambar segi empat berdasarkan percobaan program. 
  2. Buat warna pelangi yang terdiri dari 7 warna pada gambar segi empat diatas.

Script soal :
1. Gambar Segi Empat
    a. Lakukan cara pembuatan project seperti diatas lalu hapus scriptnya, 
apabila sudah dihapus maka masukkan script berikut :

#include <windows.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glut.h>

void display(void)

{

glClearColor (0.0,0.0,0.0,0.0);

glClear (GL_COLOR_BUFFER_BIT);

glBegin(GL_POLYGON);

glVertex2f(-0.5,-0.5);

glVertex2f(-0.5,0.5);

glVertex2f(0.5,0.5);

glVertex2f(0.5,-0.5);

glEnd ();

glFlush();

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutCreateWindow("Membuat Persegi PUTRI PRATIWI");

glutDisplayFunc(display);

glutMainLoop();

return 0;

}


 b. Apabila sudah maka jalankan dengan cara klik Execute>Compile & Run maka akan menjadi seperti ini


2. Gambar segi empat dengan  warna pelangi 7 warna :
    a. Lakukan cara pembuatan project seperti diatas lalu hapus scriptnya, apabila sudah dihapus maka masukkan script berikut :

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. #include <GL/glut.h>
  2. void persegi();
  3. int main(int argc, char** argv)
  4. {    
  5. glutInit(&argc,argv);    
  6. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  7. glutInitWindowSize(640,640);        
  8. glutCreateWindow("Membuat Persegi Pelangi");      
  9. glutDisplayFunc(persegi);    
  10. glutMainLoop();
  11. }
  12. void persegi()
  13. {        
  14. glBegin(GL_POLYGON);    
  15. glColor3f(1.0, 0.0 ,0.0);    
  16. glVertex2f(-0.8,0.7);
  17. glVertex2f(-0.8,0.8);  
  18. glVertex2f(0.8,0.8);
  19. glVertex2f(0.8,0.7);    
  20. glEnd();
  21. glBegin(GL_POLYGON);    
  22. glColor3f(1.0, 0.5, 0.0);    
  23. glVertex2f(-0.8,0.6);
  24. glVertex2f(-0.8,0.7);
  25. glVertex2f(0.8,0.7);
  26. glVertex2f(0.8,0.6);
  27. glEnd();
  28. glBegin(GL_POLYGON);    
  29. glColor3f(1.0, 1.0, 0.0);    
  30. glVertex2f(-0.8,0.5);
  31. glVertex2f(-0.8,0.6);
  32. glVertex2f(0.8,0.6);
  33. glVertex2f(0.8,0.5);    
  34. glEnd();  
  35. glBegin(GL_POLYGON);    
  36. glColor3f(0.0, 1.0, 0.0);    
  37. glVertex2f(-0.8,0.4);
  38. glVertex2f(-0.8,0.5);
  39. glVertex2f(0.8,0.5);
  40. glVertex2f(0.8,0.4);    
  41. glEnd();
  42. glBegin(GL_POLYGON);    
  43. glColor3f(0.0, 1.0, 1.0);    
  44. glVertex2f(-0.8,0.3);
  45. glVertex2f(-0.8,0.4);
  46. glVertex2f(0.8,0.4);
  47. glVertex2f(0.8,0.3);    
  48. glEnd();  
  49. glBegin(GL_POLYGON);    
  50. glColor3f(0.0, 0.0, 1.0);    
  51. glVertex2f(-0.8,0.2);
  52. glVertex2f(-0.8,0.3);
  53. glVertex2f(0.8,0.3);
  54. glVertex2f(0.8,0.2);    
  55. glEnd();
  56. glBegin(GL_POLYGON);    
  57. glColor3f(0.53, 0.12, 0.47);    
  58. glVertex2f(-0.8,0.1);
  59. glVertex2f(-0.8,0.2);
  60. glVertex2f(0.8,0.2);
  61. glVertex2f(0.8,0.1);    
  62. glEnd();                                  
  63.                                
  64. glFlush();
  65. }

   b. Apabila sudah maka jalankan dengan cara klik Execute>Compile & Run maka akan menjadi seperti ini



SEKIAN DAN TERIMAH KASIH

Komentar

Postingan Populer