Berikut ini merupakan contoh penggunaan OpenGl untuk menggambar bintang:

#include <GL/glut.h>
#include <iostream.h>
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex2d(-0.4,-0.5);
glColor3d(1.0, 0.0, 0.0);
glVertex2d(0.0, -0.2);
glColor3d(1.0, 0.0, 0.0);
glVertex2d(-0.3, 0.0);
glVertex2d(0.0, -0.2);
glColor3d(1.0, 1.0, 1.0);
glVertex2d(0.5, -0.5);
glVertex2d(0.3, 0.0);
glVertex2d(0.3, 0.0);
glVertex2d(0.8, 0.5);
glColor3d(1.0, 1.0, 0.0);
glVertex2d(0.2, 0.5);
glColor3d(1.0, 0.0, 0.0);
glVertex2d(-0.2, 0.5);
glColor3d(0.0, 1.0, 1.0);
glVertex2d(0.2, 0.5);
glColor3d(1.0, 0.0, 1.0);
glVertex2d(0.0, 0.9);
glVertex2d(-0.3, 0.0);
glColor3d(1.0, 0.0, 1.0);
glVertex2d(-0.2, 0.5);
glVertex2d(-0.8, 0.5);
glColor3d(0.0, 1.0, 0.0);
glVertex2d(-0.3, 0.0);
glVertex2d(0.2, 0.5);
glVertex2d(-0.2, 0.5);
glColor3d(0.0, 1.0, 0.0);
glEnd();
glBegin(GL_POLYGON);
glVertex2d(-0.3,0.0);
glVertex2d(0.0,-0.2);
glVertex2d(0.3,0.0);
glVertex2d(0.2,0.5);
glEnd();
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case ‘x’:
case ‘X’:
glRotatef(60.,0.0,1.0,0.0);
glutPostRedisplay();
break;
case ‘y’:
case ‘Y’:
glRotatef(90.,0.0,0.0,1.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}

void init (void)

{
glClearColor(0.0, 0.0, 0.1, 1.0);
}
int main (int argc, char*argv[])
{
glutInitWindowPosition(100,100);
glutInitWindowSize(320,240);
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_RGBA);
glutCreateWindow (argv[0]);
glutKeyboardFunc (keyboard);
glutDisplayFunc (display);
init();
glutMainLoop();
return 0;
}

Program ini akan menghasilkan gambar seperti di bawah ini:

Doc3

program di buat dengan memanfaatkan fungsi keyboard yang tersedia pada opengl sehingga ketika huruf x atau y pada keyboar di tekan bintang akan bergerak. silahkan mencoba sendiri.

k.

About kostanM

saya seorang mahasiswa fisika FST UNDANA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s