#include <GL/glut.h>
float posisiY=0;
float posisiX=0;
bool turun = true;
bool jalan = false;
bool putar=false;
//=======================================================
void tampilan(){
glClear(GL_COLOR_BUFFER_BIT); // Menghapus layar
glBegin(GL_LINES);
glColor3f(0,1,0); // memberi warna
glVertex2f(0,-250);
glVertex2f(0,250);
glVertex2f(-250,0);
glVertex2f(250,0);
glVertex2f(250,250);
glVertex2f(-250,-250);
glVertex2f(-250,250);
glVertex2f(250,-250);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,0.5,1);
glVertex2f(posisiX,posisiY);
glVertex2f(posisiX+50,posisiY);
glVertex2f(posisiX+50,50+posisiY);
glVertex2f(posisiX,50+posisiY);
glEnd();
// timer
Sleep(20); // per 100 milidetik
if(jalan){
if(turun){
posisiY = posisiY – 5;
}
else{
posisiY = posisiY + 5;
}
}
if(posisiY == -250){
turun = false;
}
if(posisiY == 250-50){
turun = true;
}
if(putar){
glRotatef(10,0,0,10);
}
glutPostRedisplay();
glFlush();
}
void tekanKeyboard(unsigned char key, int x, int y){
if(key==’q’)
exit(0);
if(key==’w’&&posisiY<250-50)
posisiY=posisiY+50;
if(key==’s’&&posisiY>-250)
posisiY=posisiY-50;
if(key==’d’&&posisiX<250-50)
posisiX=posisiX+50;
if(key==’a’&&posisiX>-250)
posisiX=posisiX-50;
if(key == ‘j’)
jalan = true;
if(key == ‘k’)
jalan = false;
// rotasi
if(key == ‘z’){
glRotatef(90, 0, 0, -250);
}
if(key == ‘x’){
glRotatef(-10, 0, 0, 10);
}
if(key == ‘c’){
glRotatef(10, 0, 0, 0);
}
//putar
if(key == ‘p’){
putar=true;
}
//glutPostRedisplay();
}
//=======================================================
int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitWindowPosition(400,200);
glutInitWindowSize(500,500);
glutCreateWindow(“Dasar-dasar OpenGL dan glut “);
glutDisplayFunc(tampilan);
glClearColor(1,1,1,0);
gluOrtho2D(-250,250,-250,250);
glutKeyboardFunc(tekanKeyboard);
glutMainLoop();
return 0;
}
//=======================================================