Translasi 3D OpenGL di C++

#include <GL/glut.h>
float x=0.5, y=0.5, z=0.5;
//=======================================================
void tampilan(){
glClear(GL_COLOR_BUFFER_BIT); // Menghapus layar

glBegin(GL_LINE_LOOP);
glVertex3f(0,0,0);
glVertex3f(x,0,0);
glVertex3f(x,y,0);
glVertex3f(0,y,0);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0,0,0);
glVertex3f(0,0,z);
glVertex3f(0,y,z);
glVertex3f(0,y,0);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(x,y,0);
glVertex3f(x,y,z);
glVertex3f(x,0,z);
glVertex3f(x,0,0);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0,0,0);
glVertex3f(0,0,z);
glVertex3f(x,0,z);
glVertex3f(x,0,0);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(x,y,0);
glVertex3f(x,y,z);
glVertex3f(0,y,z);
glVertex3f(0,y,0);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0,y,z);
glVertex3f(x,y,z);
glVertex3f(x,0,z);
glVertex3f(0,0,z);
glEnd();

glBegin(GL_POLYGON);
glColor3f(1,0.5,1);
glVertex2f(-0.1,-0.1);
glVertex2f(-0.5,-0.1);
glVertex2f(-0.5,-0.5);
glVertex2f(-0.1,-0.5);
glEnd();

glEnd();

Sleep(10);
glRotatef(1,0,0,1);
glRotatef(1,1,0,0);
glutPostRedisplay();

glFlush();
}

void tekanKeyboard(unsigned char key, int x, int y){
if(key==’a’)
glTranslatef(-0.5,0,0);

if(key==’d’)
glTranslatef(0.5,0,0);

if(key==’w’)
glTranslatef(0,0.5,0);

if(key==’s’)
glTranslatef(0,-0.5,0);

if(key == ‘z’)
glRotatef(5, 0, 0, 1);

if(key == ‘x’)
glRotatef(5, 1, 0, 0);

if(key == ‘c’)
glRotatef(5, 0, 1, 0);

glutPostRedisplay();
}
//=======================================================
int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitWindowPosition(200,300);
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;
}
//=======================================================

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s