Contoh Dasar syntax Open GL
Membuat bangun-bangun dasar pada Dev C++ OpenGL.Berikut Langkah-Langkahnya :
1. Klik
File > New > Project > OK![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0OmQQpi47eYTR-eu_-ZaAMGIXb2UkqtxaNMfFpukL1MkpMUXDC3HQmqV_ZGym5q19UHXTd6BiBgvMlpWDrPCw8PCJYg2BAEx6mOuJtH2l11xTDhk5gWzkdlknSvUuZEUyvLupSTYsWG2i/s400/1.png)
2. New Project akan tampil jenis lembar kerja yang akan digunakan, klik
Console Application > OK
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOf3Y21eGK-RN_scCmAqJsvCh4a__USoFMmclE5tY9yEuvdpOsQYDOiYbwKetUTm9ssdGf9gjPtkajzCnBxn1CyflPsPC9M3tqv8l1z2HjKo8csSi8Ot9ms3OQXBXJVgkIdHLmGPOHYUwy/s400/2.png)
3. Save Project, klik
Save
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7_YYlLnOVLu0qD-pGK7joMn_FXFVsj45fe1OizbXtoleGvO1XcrXc7iIkdjFJzQORyEaCGZ0psu4pN9W586GnEUSrgwIt20pIybNv2F-kFNkCU7pc0eDicxwU_cxB_1wXL-T_rHXC6zr3/s400/3.png)
4. Klik kanan pada Bar Project, lalu pilih
Project Options
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxbXJr4SvfBi2E-YhM5Sf2lrriwHOnm-PusR9PwO4_-NPXSzxCsmm6hnAecvjAUdFgma2JGBp6pB6oJe5qB4-AJ4GzxwhI1onlQRNCln4PSSVVql1f6UUc24ZDNJ1qJNCbMZfEIb8zo37U/s400/4.png)
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 :
#include <GL/glut.h>
#include <windows.h>
#include <GL/gl.h>
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glOrtho (-6,6, -6,6, -6,6);
}
void Display (void) {
glClear (GL_COLOR_BUFFER_BIT);
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 ();
glBegin (GL_LINE_LOOP);
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 ();
glutSwapBuffers ();
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (1500, 1500);
glutCreateWindow ("bangun dasar grafik");
init ();
glutDisplayFunc (Display);
glutMainLoop ();
return 0;
}
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 :
#include <GL/glut.h>
#include <windows.h>
#include <GL/gl.h>
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glOrtho (-6,6, -6,6, -6,6);
}
void Display (void) {
glClear (GL_COLOR_BUFFER_BIT);
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 ();
glBegin (GL_TRIANGLES);
glColor3f (1.0, 0.0, 0.0);
glVertex2f (1.0, 1.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2f (4.0, 1.0);
glColor3f (0.0, 1.0, 0.0);
glVertex2f (1.0, 5.0);
glEnd ();
glBegin (GL_POLYGON);
glColor3f (0.0, 1.0, 0.0);
glVertex2f (-1.0, -1.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2f (-1.0, -5.0);
glColor3f (1.0, 0.0, 0.0);
glVertex2f (-4.0, -5.0);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (-4.0, -1.0);
glEnd ();
glBegin (GL_POLYGON);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (1.0, -1.0);
glColor3f (0.0, 1.0, 1.0);
glVertex2f (1.0, -5.0);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (4.0, -5.0);
glColor3f (1.0, 1.0, 0.0);
glVertex2f (4.0, -1.0);
glEnd ();
glBegin (GL_TRIANGLES);
glColor3f (0.0, 1.0, 1.0);
glVertex2f (-1.0, 1.0);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (-4.0, 1.0);
glColor3f (1.0, 1.0, 0.0);
glVertex2f (-1.0, 5.0);
glEnd ();
glutSwapBuffers ();
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (1500, 1500);
glutCreateWindow ("bangun dasar grafik");
init ();
glutDisplayFunc (Display);
glutMainLoop ();
return 0;
}
b. Apabila sudah maka jalankan dengan cara klik Execute>Compile & Run maka akan menjadi seperti ini
2. 3 Gambar Segi Tiga a. Lakukan cara pembuatan project seperti diatas lalu hapus scriptnya, apabila sudah dihapus maka masukkan script berikut :
#include <GL/glut.h>
#include <windows.h>
#include <GL/gl.h>
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glOrtho (-6,6, -6,6, -6,6);
}
void Display (void) {
glClear (GL_COLOR_BUFFER_BIT);
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 ();
glBegin (GL_TRIANGLES);
glColor3f (1.0, 0.0, 0.0);
glVertex2f (1.0, 1.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2f (5.0, 1.0);
glColor3f (0.0, 1.0, 0.0);
glVertex2f (3.0, 5.0);
glEnd ();
glBegin (GL_TRIANGLES);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (-1.0, 1.0);
glColor3f (0.0, 1.0, 1.0);
glVertex2f (-5.0, 1.0);
glColor3f (1.0, 1.0, 0.0);
glVertex2f (-3.0, 5.0);
glEnd ();
glBegin (GL_TRIANGLES);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (2.0, -1.0);
glColor3f (0.0, 1.0, 1.0);
glVertex2f (0.0, -5.0);
glColor3f (1.0, 1.0, 0.0);
glVertex2f (-2.0, -1.0);
glEnd ();
glutSwapBuffers ();
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (1500, 1500);
glutCreateWindow ("bangun dasar grafik");
init ();
glutDisplayFunc (Display);
glutMainLoop ();
return 0;
}
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 :
#include <GL/glut.h>
#include <windows.h>
#include <GL/gl.h>
void init (void) {
glClearColor (1.0, 1.0, 1.0, 0.0);
glLineWidth (1.0);
glColor3f (1.0, 0.0, 0.0);
glOrtho (-6,6, -6,6, -6,6);
}
void Display (void) {
glClear (GL_COLOR_BUFFER_BIT);
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 ();
glBegin (GL_TRIANGLES);
glColor3f (1.0, 0.0, 0.0);
glVertex2f (1.0, 1.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2f (4.0, 1.0);
glColor3f (0.0, 1.0, 0.0);
glVertex2f (1.0, 5.0);
glEnd ();
glBegin (GL_POLYGON);
glColor3f (0.0, 1.0, 0.0);
glVertex2f (-2.0, -1.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2f (-3.0, -3.0);
glColor3f (1.0, 0.0, 0.0);
glVertex2f (-2.0, -5.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2f (-1.0, -3.0);
glEnd ();
glBegin (GL_POLYGON);
glColor3f (1.0, 1.0, 0.0);
glVertex2f (2.0, -1.0);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (1.0, -3.0);
glColor3f (0.0, 1.0, 1.0);
glVertex2f (2.0, -5.0);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (3.0, -3.0);
glEnd ();
glBegin (GL_TRIANGLES);
glColor3f (0.0, 1.0, 1.0);
glVertex2f (-1.0, 1.0);
glColor3f (1.0, 0.43, 0.78);
glVertex2f (-4.0, 1.0);
glColor3f (1.0, 1.0, 0.0);
glVertex2f (-1.0, 5.0);
glEnd ();
glutSwapBuffers ();
}
int main (int argc, char** argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (1500, 1500);
glutCreateWindow ("bangun dasar grafik");
init ();
glutDisplayFunc (Display);
glutMainLoop ();
return 0;
}
b. Apabila sudah maka jalankan dengan cara klik Execute>Compile & Run maka akan menjadi seperti ini
SEKIAN DAN TERIMAH KASIH
Komentar
Posting Komentar