Modul 3 (Bangun-bangun Dasar Grafik)

Contoh Dasar syntax Open GL
Membuat bangun-bangun dasar pada Dev C++ OpenGL.
Berikut Langkah-Langkahnya :

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

3.  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 :


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <GL/glut.h>           
  2. #include <windows.h>   
  3. #include <GL/gl.h>
  4.  
  5. void init (void) { 
  6. glClearColor (1.0, 1.0, 1.0, 0.0); 
  7. glLineWidth (1.0); 
  8. glColor3f (1.0, 0.0, 0.0);         
  9. glOrtho (-6,6, -6,6, -6,6);
  10. }              
  11. void Display (void) {      
  12. glClear (GL_COLOR_BUFFER_BIT); 
  13. glBegin (GL_LINES);
  14. glVertex2f (-5.5, 0.0);    
  15. glColor3f (1.0, 0.0, 0.0);
  16. glVertex2f (5.5, 0.0);     
  17. glEnd ();      
  18. glBegin (GL_LINES);    
  19. glVertex2f (0.0, -5.5);
  20. glColor3f (1.0, 0.0, 0.0); 
  21. glVertex2f (0.0, 5.5);     
  22. glEnd ();      
  23.            
  24.            
  25. glBegin (GL_LINE_LOOP);        
  26. glColor3f (-1.0, 0.0, 0.0);    
  27. glVertex2f (1.0, 1.0);     
  28. glColor3f (-1.0, 0.0, 0.0);    
  29. glVertex2f (4.0, 1.0);
  30. glColor3f (0.0, 1.0, 0.0);
  31. glVertex2f (1.0, 5.0);
  32. glEnd ();
  33.  
  34. glutSwapBuffers ();
  35.  
  36. }
  37.  
  38. int main (int argc, char** argv) { 
  39. glutInit (&argc, argv);
  40. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);  
  41. glutInitWindowPosition (0, 0);
  42. glutInitWindowSize (1500, 1500);
  43. glutCreateWindow ("bangun dasar grafik");  
  44. init ();
  45.  
  46.  
  47. glutDisplayFunc (Display);
  48. glutMainLoop ();
  49. return 0;
  50. }

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



 Buat contoh program berikut dengan memodifikasikan script diatas :

1. 2 gambar segi tiga dan 2 gambar segi empat.
2. 3 gambar segi tiga.
3. 2 gambar segi tiga dan 2 gambar ketupat.

Script Soal

1. 2 gambar segi tiga dan 2 gambar segi empat
    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. #include <windows.h>   
  3. #include <GL/gl.h>
  4.  
  5. void init (void) { 
  6. glClearColor (1.0, 1.0, 1.0, 0.0); 
  7. glLineWidth (1.0); 
  8. glColor3f (1.0, 0.0, 0.0);         
  9. glOrtho (-6,6, -6,6, -6,6);
  10. }              
  11. void Display (void) {      
  12. glClear (GL_COLOR_BUFFER_BIT); 
  13. glBegin (GL_LINES);
  14. glVertex2f (-5.5, 0.0);    
  15. glColor3f (1.0, 0.0, 0.0);
  16. glVertex2f (5.5, 0.0);     
  17. glEnd ();      
  18. glBegin (GL_LINES);    
  19. glVertex2f (0.0, -5.5);
  20. glColor3f (1.0, 0.0, 0.0); 
  21. glVertex2f (0.0, 5.5);     
  22. glEnd ();      
  23.            
  24.            
  25. glBegin (GL_TRIANGLES);        
  26. glColor3f (1.0, 0.0, 0.0);     
  27. glVertex2f (1.0, 1.0);     
  28. glColor3f (0.0, 0.0, 1.0);     
  29. glVertex2f (4.0, 1.0);
  30. glColor3f (0.0, 1.0, 0.0);
  31. glVertex2f (1.0, 5.0);
  32. glEnd ();
  33.  
  34. glBegin (GL_POLYGON);          
  35. glColor3f (0.0, 1.0, 0.0);     
  36. glVertex2f (-1.0, -1.0);       
  37. glColor3f (0.0, 0.0, 1.0);     
  38. glVertex2f (-1.0, -5.0);
  39. glColor3f (1.0, 0.0, 0.0);
  40. glVertex2f (-4.0, -5.0);
  41. glColor3f (1.0, 0.43, 0.78);
  42. glVertex2f (-4.0, -1.0);
  43. glEnd ();
  44.  
  45. glBegin (GL_POLYGON);          
  46. glColor3f (1.0, 0.43, 0.78);       
  47. glVertex2f (1.0, -1.0);    
  48. glColor3f (0.0, 1.0, 1.0);     
  49. glVertex2f (1.0, -5.0);
  50. glColor3f (1.0, 0.43, 0.78);
  51. glVertex2f (4.0, -5.0);
  52. glColor3f (1.0, 1.0, 0.0);
  53. glVertex2f (4.0, -1.0);
  54. glEnd ();
  55.  
  56. glBegin (GL_TRIANGLES);        
  57. glColor3f (0.0, 1.0, 1.0);     
  58. glVertex2f (-1.0, 1.0);    
  59. glColor3f (1.0, 0.43, 0.78);       
  60. glVertex2f (-4.0, 1.0);
  61. glColor3f (1.0, 1.0, 0.0);
  62. glVertex2f (-1.0, 5.0);
  63. glEnd ();
  64.  
  65. glutSwapBuffers ();
  66.  
  67. }
  68.  
  69. int main (int argc, char** argv) { 
  70. glutInit (&argc, argv);
  71. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);  
  72. glutInitWindowPosition (0, 0);
  73. glutInitWindowSize (1500, 1500);
  74. glutCreateWindow ("bangun dasar grafik");  
  75. init ();
  76.  
  77.  
  78. glutDisplayFunc (Display);
  79. glutMainLoop ();
  80. return 0;
  81. }


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




2. 3 Gambar Segi Tiga
    aLakukan 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. #include <windows.h>   
  3. #include <GL/gl.h>
  4.  
  5. void init (void) { 
  6. glClearColor (1.0, 1.0, 1.0, 0.0); 
  7. glLineWidth (1.0); 
  8. glColor3f (1.0, 0.0, 0.0);         
  9. glOrtho (-6,6, -6,6, -6,6);
  10. }              
  11. void Display (void) {      
  12. glClear (GL_COLOR_BUFFER_BIT); 
  13. glBegin (GL_LINES);
  14. glVertex2f (-5.5, 0.0);    
  15. glColor3f (1.0, 0.0, 0.0);
  16. glVertex2f (5.5, 0.0);     
  17. glEnd ();      
  18. glBegin (GL_LINES);    
  19. glVertex2f (0.0, -5.5);
  20. glColor3f (1.0, 0.0, 0.0); 
  21. glVertex2f (0.0, 5.5);     
  22. glEnd ();      
  23.            
  24.            
  25. glBegin (GL_TRIANGLES);        
  26. glColor3f (1.0, 0.0, 0.0);     
  27. glVertex2f (1.0, 1.0);     
  28. glColor3f (0.0, 0.0, 1.0);     
  29. glVertex2f (5.0, 1.0);
  30. glColor3f (0.0, 1.0, 0.0);
  31. glVertex2f (3.0, 5.0);
  32. glEnd ();
  33.  
  34. glBegin (GL_TRIANGLES);        
  35. glColor3f (1.0, 0.43, 0.78);       
  36. glVertex2f (-1.0, 1.0);    
  37. glColor3f (0.0, 1.0, 1.0);     
  38. glVertex2f (-5.0, 1.0);
  39. glColor3f (1.0, 1.0, 0.0);
  40. glVertex2f (-3.0, 5.0);
  41. glEnd ();
  42.  
  43. glBegin (GL_TRIANGLES);        
  44. glColor3f (1.0, 0.43, 0.78);       
  45. glVertex2f (2.0, -1.0);    
  46. glColor3f (0.0, 1.0, 1.0);     
  47. glVertex2f (0.0, -5.0);
  48. glColor3f (1.0, 1.0, 0.0);
  49. glVertex2f (-2.0, -1.0);
  50. glEnd ();
  51.  
  52. glutSwapBuffers ();
  53.  
  54. }
  55.  
  56. int main (int argc, char** argv) { 
  57. glutInit (&argc, argv);
  58. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);  
  59. glutInitWindowPosition (0, 0);
  60. glutInitWindowSize (1500, 1500);
  61. glutCreateWindow ("bangun dasar grafik");  
  62. init ();
  63.  
  64.  
  65. glutDisplayFunc (Display);
  66. glutMainLoop ();
  67. return 0;
  68. }

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




3. 2 gambar segi tiga dan 2 gambar ketupat.
    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. #include <windows.h>   
  3. #include <GL/gl.h>
  4.  
  5. void init (void) { 
  6. glClearColor (1.0, 1.0, 1.0, 0.0); 
  7. glLineWidth (1.0); 
  8. glColor3f (1.0, 0.0, 0.0);         
  9. glOrtho (-6,6, -6,6, -6,6);
  10. }              
  11. void Display (void) {      
  12. glClear (GL_COLOR_BUFFER_BIT); 
  13. glBegin (GL_LINES);
  14. glVertex2f (-5.5, 0.0);    
  15. glColor3f (1.0, 0.0, 0.0);
  16. glVertex2f (5.5, 0.0);     
  17. glEnd ();      
  18. glBegin (GL_LINES);    
  19. glVertex2f (0.0, -5.5);
  20. glColor3f (1.0, 0.0, 0.0); 
  21. glVertex2f (0.0, 5.5);     
  22. glEnd ();      
  23.            
  24.            
  25. glBegin (GL_TRIANGLES);        
  26. glColor3f (1.0, 0.0, 0.0);     
  27. glVertex2f (1.0, 1.0);     
  28. glColor3f (0.0, 0.0, 1.0);     
  29. glVertex2f (4.0, 1.0);
  30. glColor3f (0.0, 1.0, 0.0);
  31. glVertex2f (1.0, 5.0);
  32. glEnd ();
  33.  
  34. glBegin (GL_POLYGON);          
  35. glColor3f (0.0, 1.0, 0.0);     
  36. glVertex2f (-2.0, -1.0);       
  37. glColor3f (0.0, 0.0, 1.0);     
  38. glVertex2f (-3.0, -3.0);
  39. glColor3f (1.0, 0.0, 0.0);
  40. glVertex2f (-2.0, -5.0);
  41. glColor3f (0.0, 0.0, 1.0);
  42. glVertex2f (-1.0, -3.0);
  43. glEnd ();
  44.  
  45. glBegin (GL_POLYGON);          
  46. glColor3f (1.0, 1.0, 0.0);     
  47. glVertex2f (2.0, -1.0);    
  48. glColor3f (1.0, 0.43, 0.78);       
  49. glVertex2f (1.0, -3.0);
  50. glColor3f (0.0, 1.0, 1.0);
  51. glVertex2f (2.0, -5.0);
  52. glColor3f (1.0, 0.43, 0.78);
  53. glVertex2f (3.0, -3.0);
  54. glEnd ();
  55.  
  56. glBegin (GL_TRIANGLES);        
  57. glColor3f (0.0, 1.0, 1.0);     
  58. glVertex2f (-1.0, 1.0);    
  59. glColor3f (1.0, 0.43, 0.78);       
  60. glVertex2f (-4.0, 1.0);
  61. glColor3f (1.0, 1.0, 0.0);
  62. glVertex2f (-1.0, 5.0);
  63. glEnd ();
  64.  
  65. glutSwapBuffers ();
  66.  
  67. }
  68.  
  69. int main (int argc, char** argv) { 
  70. glutInit (&argc, argv);
  71. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);  
  72. glutInitWindowPosition (0, 0);
  73. glutInitWindowSize (1500, 1500);
  74. glutCreateWindow ("bangun dasar grafik");  
  75. init ();
  76.  
  77.  
  78. glutDisplayFunc (Display);
  79. glutMainLoop ();
  80. return 0;
  81. }

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




SEKIAN DAN TERIMAH KASIH

Komentar

Postingan Populer