Modul 5 (Rotasi)

Contoh Dasar syntax Open GL yaitu membuat rotasi 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 :
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <GL/glut.h>
  2. #include <windows.h>
  3. GLfloat xangle=0.0, yangle=0.0;
  4. void init (void) {
  5. glClearColor (1.0, 1.0, 1.0, 0.0);
  6. glLineWidth (1.0);
  7. glColor3f (1.0, 0.0, 0.0);
  8. glMatrixMode (GL_PROJECTION);
  9. glLoadIdentity ();
  10. glOrtho (-6,6, -6,6, -6,6);
  11. }
  12.  
  13. void display (void) {
  14. glClear (GL_COLOR_BUFFER_BIT);
  15. glPushMatrix();
  16.  
  17.  
  18.  
  19. glBegin (GL_LINES);
  20. glVertex2f (-5.5,0.0);
  21.  
  22. glColor3f(1.0, 0.0, 0.0);
  23. glVertex2f (5.5,0.0);
  24. glEnd ();
  25. glBegin (GL_LINES);
  26. glVertex2f (0.0,-5.5);
  27. glColor3f(1.0, 0.0, 0.0);
  28. glVertex2f (0.0, 5.5);
  29. glEnd ();
  30.  
  31. glRotatef(xangle, 1.0, 0.0, 0.0);
  32. glBegin (GL_POLYGON);
  33. glColor3f(-1.0, 0.0, 0.0);
  34. glVertex2f (1.0, 1.0);
  35. glColor3f(-1.0, 0.0, 0.0);
  36. glVertex2f (4.0, 1.0);
  37. glColor3f(0.0, 1.0, 0.0);
  38. glVertex2f (1.0, 5.0);
  39. glEnd ();
  40.  
  41. glPopMatrix();
  42. glutSwapBuffers();
  43. glFlush ();
  44. }
  45.  
  46. void KeyboardAssign (GLubyte key, GLint x, GLint y) { switch (key) {
  47.  
  48. case 'x':
  49. xangle +=10.0;
  50. glColor3f (0.0, 0.0, 1.0);
  51. glutPostRedisplay ();
  52. break;
  53. }
  54. }
  55.  
  56. int main (int argc, char** argv) {
  57. glutInit (&argc, argv);
  58.  
  59. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition (0, 0); glutInitWindowSize (1500, 1500); glutCreateWindow ("Latihan Menggerakkan Objek"); init();
  60.  
  61. glutDisplayFunc (display);
  62. glutKeyboardFunc (KeyboardAssign);
  63. glutMainLoop ();
  64. }

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



  8. Tekan X dan output akan berubah seperti ini :



Setelah mengerjakannya mari buat contoh program berikut dengan memodifikasikan script diatas

1. Lakukan rotasi terhadap sumbu Y
2. Lakukan rotasi terhadap sumbu Z

Script

1. Lakukan rotasi terhadap sumbu Y
   a. Lakukan 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. GLfloat xangle=0.0, yangle=0.0;
  4. void init (void) {
  5. glClearColor (1.0, 1.0, 1.0, 0.0);
  6. glLineWidth (1.0);
  7. glColor3f (1.0, 0.0, 0.0);
  8. glMatrixMode (GL_PROJECTION);
  9. glLoadIdentity ();
  10. glOrtho (-6,6, -6,6, -6,6);
  11. }
  12.  
  13. void display (void) {
  14. glClear (GL_COLOR_BUFFER_BIT);
  15. glPushMatrix();
  16. glBegin (GL_LINES);
  17. glVertex2f (-5.5,0.0);
  18. glColor3f(1.0, 0.0, 0.0);
  19. glVertex2f (5.5,0.0);
  20. glEnd ();
  21.  
  22. glBegin (GL_LINES);
  23. glVertex2f (0.0,-5.5);
  24. glColor3f(1.0, 0.0, 0.0);
  25. glVertex2f (0.0, 5.5);
  26. glEnd ();
  27.  
  28. glRotatef(xangle, 0.0, 1.0, 0.0);
  29. glBegin (GL_POLYGON);
  30. glColor3f(-1.0, 0.0, 0.0);
  31. glVertex2f (1.0, 1.0);
  32. glColor3f(-1.0, 0.0, 0.0);
  33. glVertex2f (4.0, 1.0);
  34. glColor3f(0.0, 1.0, 0.0);
  35. glVertex2f (1.0, 5.0);
  36. glEnd ();
  37.  
  38. glPopMatrix();
  39. glutSwapBuffers();
  40. glFlush ();
  41. }
  42.  
  43. void KeyboardAssign (GLubyte key, GLint x, GLint y) { switch (key) {
  44.  
  45. case 'd':
  46. xangle +=10.0;
  47. glColor3f (0.0, 0.0, 1.0);
  48. glutPostRedisplay ();
  49. break;
  50. }
  51. }
  52.  
  53. int main (int argc, char** argv) {
  54. glutInit (&argc, argv);
  55.  
  56. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  57. glutInitWindowPosition (0, 0);
  58. glutInitWindowSize (1500, 1500);
  59. glutCreateWindow ("Latihan Menggerakkan Objek");
  60. init();
  61. glutDisplayFunc (display);
  62. glutKeyboardFunc (KeyboardAssign);
  63. glutMainLoop ();
  64. }

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


         c. Tekan "d" maka akan menjadi berubah menjadi seperti ini :



2. Lakukan rotasi terhadap sumbu Z
    a. Lakukan 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. GLfloat xangle=0.0, yangle=0.0;
  4. void init (void) {
  5. glClearColor (1.0, 1.0, 1.0, 0.0);
  6. glLineWidth (1.0);
  7. glColor3f (1.0, 0.0, 0.0);
  8. glMatrixMode (GL_PROJECTION);
  9. glLoadIdentity ();
  10. glOrtho (-6,6, -6,6, -6,6);
  11. }
  12.  
  13. void display (void) {
  14. glClear (GL_COLOR_BUFFER_BIT);
  15. glPushMatrix();
  16. glBegin (GL_LINES);
  17. glVertex2f (-5.5,0.0);
  18. glColor3f(1.0, 0.0, 0.0);
  19. glVertex2f (5.5,0.0);
  20. glEnd ();
  21.  
  22. glBegin (GL_LINES);
  23. glVertex2f (0.0,-5.5);
  24. glColor3f(1.0, 0.0, 0.0);
  25. glVertex2f (0.0, 5.5);
  26. glEnd ();
  27.  
  28. glRotatef(xangle, 0.0, 0.0, 1.0);
  29. glBegin (GL_POLYGON);
  30. glColor3f(-1.0, 0.0, 0.0);
  31. glVertex2f (1.0, 1.0);
  32. glColor3f(-1.0, 0.0, 0.0);
  33. glVertex2f (4.0, 1.0);
  34. glColor3f(0.0, 1.0, 0.0);
  35. glVertex2f (1.0, 5.0);
  36. glEnd ();
  37.  
  38. glPopMatrix();
  39. glutSwapBuffers();
  40. glFlush ();
  41. }
  42.  
  43. void KeyboardAssign (GLubyte key, GLint x, GLint y) { switch (key) {
  44.  
  45. case 'd':
  46. xangle +=10.0;
  47. glColor3f (0.0, 0.0, 1.0);
  48. glutPostRedisplay ();
  49. break;
  50. }
  51. }
  52.  
  53. int main (int argc, char** argv) {
  54. glutInit (&argc, argv);
  55.  
  56. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  57. glutInitWindowPosition (0, 0);
  58. glutInitWindowSize (1500, 1500);
  59. glutCreateWindow ("Latihan Menggerakkan Objek");
  60. init();
  61. glutDisplayFunc (display);
  62. glutKeyboardFunc (KeyboardAssign);
  63. glutMainLoop ();
  64. }

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


    c. Tekan "d" maka hasil akan berubah seperti ini :


Komentar

Postingan Populer