Contoh Dasar syntax Open GL yaitu membuat rotasi pada Dev C++ OpenGL.Berikut Langkah-Langkahnya :
- 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,
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxhLpX51ziImFdPSl0nwGC7UZp_RT-kfLTuqv6VuFf6ABlDmBn9RrAh-up7m8OYm6Zb_YVoZ4DTt4CstrzeIjy4PH5tH3rjJJGG1Cy3FHp8qowDq-1MVk_ami11fCmxHVPa_c_7rWY9Hxi/s400/6.png)
7. Kosongkan Script dan ketikan script seperti dibawah ini :
#include <GL/glut.h>
#include <windows.h>
GLfloat xangle=0.0, yangle=0.0;
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-6,6, -6,6, -6,6);
}
void display (void) {
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin (GL_LINES);
glVertex2f (-5.5,0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (5.5,0.0);
glEnd ();
glBegin (GL_LINES);
glVertex2f (0.0,-5.5);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (0.0, 5.5);
glEnd ();
glRotatef(xangle, 1.0, 0.0, 0.0);
glBegin (GL_POLYGON);
glColor3f(-1.0, 0.0, 0.0);
glVertex2f (1.0, 1.0);
glColor3f(-1.0, 0.0, 0.0);
glVertex2f (4.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f (1.0, 5.0);
glEnd ();
glPopMatrix();
glutSwapBuffers();
glFlush ();
}
void KeyboardAssign (GLubyte key, GLint x, GLint y) { switch (key) {
case 'x':
xangle +=10.0;
glColor3f (0.0, 0.0, 1.0);
glutPostRedisplay ();
break;
}
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition (0, 0); glutInitWindowSize (1500, 1500); glutCreateWindow ("Latihan Menggerakkan Objek"); init();
glutDisplayFunc (display);
glutKeyboardFunc (KeyboardAssign);
glutMainLoop ();
}
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 :
#include <GL/glut.h>
#include <windows.h>
GLfloat xangle=0.0, yangle=0.0;
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-6,6, -6,6, -6,6);
}
void display (void) {
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin (GL_LINES);
glVertex2f (-5.5,0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (5.5,0.0);
glEnd ();
glBegin (GL_LINES);
glVertex2f (0.0,-5.5);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (0.0, 5.5);
glEnd ();
glRotatef(xangle, 0.0, 1.0, 0.0);
glBegin (GL_POLYGON);
glColor3f(-1.0, 0.0, 0.0);
glVertex2f (1.0, 1.0);
glColor3f(-1.0, 0.0, 0.0);
glVertex2f (4.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f (1.0, 5.0);
glEnd ();
glPopMatrix();
glutSwapBuffers();
glFlush ();
}
void KeyboardAssign (GLubyte key, GLint x, GLint y) { switch (key) {
case 'd':
xangle +=10.0;
glColor3f (0.0, 0.0, 1.0);
glutPostRedisplay ();
break;
}
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (1500, 1500);
glutCreateWindow ("Latihan Menggerakkan Objek");
init();
glutDisplayFunc (display);
glutKeyboardFunc (KeyboardAssign);
glutMainLoop ();
}
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 :
#include <GL/glut.h>
#include <windows.h>
GLfloat xangle=0.0, yangle=0.0;
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-6,6, -6,6, -6,6);
}
void display (void) {
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin (GL_LINES);
glVertex2f (-5.5,0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (5.5,0.0);
glEnd ();
glBegin (GL_LINES);
glVertex2f (0.0,-5.5);
glColor3f(1.0, 0.0, 0.0);
glVertex2f (0.0, 5.5);
glEnd ();
glRotatef(xangle, 0.0, 0.0, 1.0);
glBegin (GL_POLYGON);
glColor3f(-1.0, 0.0, 0.0);
glVertex2f (1.0, 1.0);
glColor3f(-1.0, 0.0, 0.0);
glVertex2f (4.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f (1.0, 5.0);
glEnd ();
glPopMatrix();
glutSwapBuffers();
glFlush ();
}
void KeyboardAssign (GLubyte key, GLint x, GLint y) { switch (key) {
case 'd':
xangle +=10.0;
glColor3f (0.0, 0.0, 1.0);
glutPostRedisplay ();
break;
}
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (1500, 1500);
glutCreateWindow ("Latihan Menggerakkan Objek");
init();
glutDisplayFunc (display);
glutKeyboardFunc (KeyboardAssign);
glutMainLoop ();
}
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
Posting Komentar