Anda di halaman 1dari 14

#include <windows.

h> // for MS Windows

#include <GL/glut.h> // GLUT, include glu.h and gl.h

bool depan = true;

/* Initialize OpenGL Graphics */

void initGL() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque

glClearDepth(1.0f); // Set background depth to farthest

glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling

glDepthFunc(GL_LEQUAL); // Set the type of depth-test

glShadeModel(GL_SMOOTH); // Enable smooth shading

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections

/* Handler for window-repaint event. Called back when the window first appears and

whenever the window needs to be re-painted. */

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers

glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads

if (depan) {

// pager depan kiri

glColor3f( 1.0, 1.0, 1.0 );


glVertex3f( -1.0, -4.0, 5.5 );

glVertex3f( -1.0, -2.0, 5.5 );

glVertex3f( -10.0, -2.0, 5.5 );

glVertex3f( -10.0, -4.0, 5.5 );

// pager depan kanan

glColor3f( 1.0, 1.0, 1.0 );

glVertex3f( 1.0, -4.0, 5.5 );

glVertex3f( 1.0, -2.0, 5.5 );

glVertex3f( 10.0, -2.0, 5.5 );

glVertex3f( 10.0, -4.0, 5.5 );

//tembok belakang

glColor3f( 0.0, 0.5, 0.5 );

glVertex3f( 6.0, -4.0, -2.5 );

glVertex3f( 6.0, 2.0, -2.5 );

glVertex3f( -6.0, 2.0, -2.5 );

glVertex3f( -6.0, -4.0, -2.5 );

//tembok depan

glColor3f( 0.0, 0.5, 0.5 );

glVertex3f( 6.0, -4.0, 2.5 );

glVertex3f( 6.0, 2.0, 2.5 );

glVertex3f( -6.0, 2.0, 2.5 );

glVertex3f( -6.0, -4.0, 2.5 );


//pintu

glColor3f (0.5, 0.3, 0.1);

glVertex3f(1.0, -4.0, 2.6);

glVertex3f(1.0, 0.0, 2.6);

glVertex3f(-1.0, 0.0, 2.6);

glVertex3f(-1.0, -4.0, 2.6);

//jendela depan kanan

glColor3f (0.5, 0.3, 0.2);

glVertex3f(5.0, -1.5, 2.6);

glVertex3f(5.0, 1.0, 2.6);

glVertex3f(3.0, 1.0, 2.6);

glVertex3f(3.0, -1.5, 2.6);

//kaca depan kanan

glColor3f(0.0, 0.0 ,0.0);

glVertex3f(3.1, -1.4, 2.6);

glVertex3f(3.1, 0.9, 2.6);

glVertex3f(4.9, 0.9, 2.6);

glVertex3f(4.9, -1.4, 2.6);


//jendela depan kiri

glColor3f (0.5, 0.3, 0.2);

glVertex3f(-5.0, -1.5, 2.6);

glVertex3f(-5.0, 1.0, 2.6);

glVertex3f(-3.0, 1.0, 2.6);

glVertex3f(-3.0, -1.5, 2.6);

//garis jendela depan kiri

glLineWidth(3);

glColor3f(0.0, 0.0 ,0.0);

glVertex3f(-4.9, -1.4, 2.6);

glVertex3f(-3.1, -1.4, 2.6);

glVertex3f(-3.1, 0.9, 2.6);

glVertex3f(-4.9, 0.9, 2.6);

//atap depan

glColor3f( 0.78, 0.69, 0.64 );

glVertex3f( 7.0, 2.0, 2.5 );

glVertex3f( 5.0, 4.0, 0.0 );

glVertex3f( -5.0, 4.0, 0.0 );

glVertex3f( -7.0, 2.0, 2.5 );

//atap kanan
glColor3f( 0.0, 0.29, 0.24 );

glVertex3f( 7.0, 2.0, 2.5 );

glVertex3f( 7.0, 2.0, -2.5 );

glVertex3f( 5.0, 4.0, 0.0 );

glVertex3f( 5.0, 4.0, 0.0 );

//atap kiri

glColor3f( 0.0, 0.29, 0.24 );

glVertex3f( -7.0, 2.0, 2.5 );

glVertex3f( -7.0, 2.0, -2.5 );

glVertex3f( -5.0, 4.0, 0.0 );

glVertex3f( -5.0, 4.0, 0.0 );

//tembok kiri

glColor3f( 0.0, 0.5, 0.5 );

glVertex3f( 6.0, -4.0, -2.5 );

glVertex3f( 6.0, 2.0, -2.5 );

glVertex3f( 6.0, 2.0, 2.5 );

glVertex3f( 6.0, -4.0, 2.5 );

//tembok kanan

glColor3f( 0.0, 0.5, 0.5 );


glVertex3f( -6.0, -4.0, 2.5 );

glVertex3f( -6.0, 2.0, 2.5 );

glVertex3f( -6.0, 2.0, -2.5 );

glVertex3f( -6.0, -4.0, -2.5 );

//bingkai jendela belakang kanan

glColor3f (0.5, 0.3, 0.2);

glVertex3f(-5.0, -1.0, -2.6);

glVertex3f(-5.0, 1.0, -2.6);

glVertex3f(-1.0, 1.0, -2.6);

glVertex3f(-1.0, -1.0, -2.6);

//jendela belakang kanan

glColor3f(0.0, 0.0, 0.0);

glVertex3f(-4.9, -0.9, -2.7);

glVertex3f(-4.9, 0.9, -2.7);

glVertex3f(-1.1, 0.9, -2.7);

glVertex3f(-1.1, -0.9, -2.7);

//bingkai jendela belakang kiri

glColor3f (0.5, 0.3, 0.2);

glVertex3f(5.0, -1.0, -2.6);


glVertex3f(5.0, 1.0, -2.6);

glVertex3f(1.0, 1.0, -2.6);

glVertex3f(1.0, -1.0, -2.6);

//jendela belakang kiri

glColor3f(0.0, 0.0, 0.0);

glVertex3f(4.9, -0.9, -2.7);

glVertex3f(4.9, 0.9, -2.7);

glVertex3f(1.1, 0.9, -2.7);

glVertex3f(1.1, -0.9, -2.7);

// jalan

glColor3f(0.8, 0.8, 0.8);

glVertex3f(1.0, -4.0, 2.6);

glVertex3f(1.0, -4.0, 12);

glVertex3f(-1.0, -4.0,12);

glVertex3f(-1.0, -4.0, 2.6);

//atap belakang

glColor3f( 0.78, 0.69, 0.64 );

glVertex3f( 7.0, 2.0, -2.5 );

glVertex3f( 5.0, 4.0, 0.0 );

glVertex3f( -5.0, 4.0, 0.0 );

glVertex3f( -7.0, 2.0, -2.5 );


// pager kiri

glColor3f( 1.0, 1.0, 1.0 );

glVertex3f( -10.0, -4.0, 5.5 );

glVertex3f( -10.0, -2.0, 5.5 );

glVertex3f( -10.0, -2.0, -10.5 );

glVertex3f( -10.0, -4.0, -10.5 );

// pager kanan

glColor3f( 1.0, 1.0, 1.0 );

glVertex3f( 10.0, -4.0, 5.5 );

glVertex3f( 10.0, -2.0, 5.5 );

glVertex3f( 10.0, -2.0, -10.5 );

glVertex3f( 10.0, -4.0, -10.5 );

// pager belakang

glColor3f( 1.0, 1.0, 1.0 );

glVertex3f( 10.0, -4.0, -10.5 );

glVertex3f( 10.0, -2.0, -10.5 );

glVertex3f( -10.0, -2.0, -10.5 );

glVertex3f( -10.0, -4.0, -10.5 );

//lantai

glColor3f( 0.0, 7.0, 0.0 );

glVertex3f( -10.0, -4.0, 5.5 );

glVertex3f( 10.0, -4.0, 5.5 );


glVertex3f( 10.0, -4.0, -10.5 );

glVertex3f( -10.0, -4.0, -10.5 );

//lantai

glColor3f( 1.0, 1.0, 1.0 );

glVertex3f( 6.0, -4.0, -2.5 );

glVertex3f( 6.0, -4.0, 2.5 );

glVertex3f( -6.0, -4.0, 2.5 );

glVertex3f( -6.0, -4.0, -2.5 );

//lantai

glColor3f( 0.0, 7.0, 0.0 );

glVertex3f( -10.0, -4.0, 5.5 );

glVertex3f( 10.0, -4.0, 5.5 );

glVertex3f( 10.0, -4.0, -10.5 );

glVertex3f( -10.0, -4.0, -10.5 );

glEnd();

glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)

/* Handler for window re-size event. Called back when the window first appears and

whenever the window is re-sized with its new width and height */

void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer

// Compute aspect ratio of the new window

if (height == 0) height = 1; // To prevent divide by 0


GLfloat aspect = (GLfloat)width / (GLfloat)height;

// Set the viewport to cover the new window

glViewport(0, 0, width, height);

// Set the aspect ratio of the clipping volume to match the viewport

glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix

glLoadIdentity(); // Reset

// Enable perspective projection with fovy, aspect, zNear and zFar

gluPerspective(45.0f, aspect, 0.1f, 100.0f);

void keyboard(unsigned char key, int x, int y) {

switch (key) {

//kotak merah ilang

case 'b':

case 'B':

depan = false;

break;

case 'v':

case 'V':

depan = true;

break;

//gerak ke kiri

case 'a':
case 'A':

glTranslatef(-1.0, 0.0, 0.0);

break;

//gerak ke kanan

case 'd':

case 'D':

glTranslatef(1.0, 0.0, 0.0);

break;

//gerak ke depan

case 'w':

case 'W':

glTranslatef(0.0, 0.0, 1.0);

break;

//gerak ke belakang

case 's':

case 'S':

glTranslatef(0.0, 0.0, -1.0);

break;

//gerak ke atas

case 'q':

case 'Q':

glTranslatef(0.0, 1.0, 0.0);

break;

//gerak ke bawah

case 'e':
case 'E':

glTranslatef(0.0, -1.0, 0.0);

break;

//rotate ke kiri

case 'j':

case 'J':

glRotatef(1.0, 0.0, -1.0, 0.0);

break;

//rotate ke kanan

case 'l':

case 'L':

glRotatef(1.0, 0.0, 1.0, 0.0);

break;

//rotate ke samping kanan

case 'i':

case 'I':

glRotatef(1.0, 0.0, 0.0, -1.0);

break;

//rotate ke samping kiri

case 'k':

case 'K':

glRotatef(1.0, 0.0, 0.0, 5.0);

break;

//rotate ke atas

case 'o':
case 'O':

glRotatef(1.0, -5.0, 0.0, 0.0);

break;

//rotate ke bawah

case 'u':

case 'U':

glRotatef(1.0, 5.0, 0.0, 0.0);

break;

display();

//glutPostRedisplay();

/* Main function: GLUT runs as a console application starting at main() */

int main(int argc, char** argv) {

glutInit(&argc, argv); // Initialize GLUT

glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode

glutInitWindowSize(640, 480); // Set the window's initial width & height

glutInitWindowPosition(350, 50); // Position the window's initial top-left corner

glutCreateWindow("Latihan 3D"); // Create window with the given title

glutDisplayFunc(display); // Register callback handler for window re-paint event

glutReshapeFunc(reshape); // Register callback handler for window re-size event

glutKeyboardFunc(keyboard);

initGL(); // Our own OpenGL initialization


glutMainLoop(); // Enter the infinite event-processing loop

return 0;

Anda mungkin juga menyukai