Membuat Rumah Sederhana dengan OpenGL


Berikut ini tampilannya :

Tampak Depan



Tampak Atas



Tampak Samping


Tampak Belakang



Langsung saja, berikut ini saya sajikan source code nya  ^___^
#include
#include

int w= 1200 ; int h=600;
int x1=0 , y1= 0, z1=0;
int sudut=0;

int cx, cy;


void init ()
{
       GLfloat LightPosition[] = {10.0f, 10.0f, 20.0f, 0.0f};
       GLfloat LightAmbient [] = {0.0f, 1.0f, 0.0f, 1.0f};
       GLfloat LightDiffuse [] = {0.7f, 0.7f, 0.7f, 1.0f};
       GLfloat LightSpecular[] = {0.5f, 0.5f, 0.5f, 1.0f};
       GLfloat Shine [] = { 80 };

       glShadeModel (GL_SMOOTH );
       glClearColor (0.0f, 0.0f, 0.0f, 0.5f);
       glClearDepth (1.0f);
       glEnable (GL_DEPTH_TEST );
       glHint (GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST );

       glMaterialfv (GL_FRONT , GL_SPECULAR  , LightSpecular   );
       glMaterialfv (GL_FRONT , GL_SHININESS , Shine);

       glEnable (GL_LIGHTING );
       glEnable (GL_LIGHT0 );
       return;
}

void renderScene ()
{
       GLfloat LightPosition[] = {10.0f, 10.0f, 20.0f, 0.0f};
       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
       glClearColor (0.3, 0, 6, 0);
       LightPosition[1] = cx;
       LightPosition[0] = cy;

       glLightfv (GL_LIGHT0 , GL_POSITION , LightPosition );

       glLoadIdentity ();
       glTranslatef (0, 0, -200);
       glRotatef (sudut, x1, y1 , z1);

       glEnable (GL_COLOR_MATERIAL );

       //tembok rumah
       glPushMatrix ();
              glColor3f (0 , 4., 2);
              glTranslatef (0 , 5, 0);
             
              glScalef (25, 18, 15);
              glutSolidCube(3);
       glPopMatrix ();

       //lantai 2
       glPushMatrix ();
              glColor3f (4 , 4, 4);
              glTranslatef (0 , 5.5, 0);
             
              glScalef (25.1, 0.25, 16);
              glutSolidCube(3);
       glPopMatrix ();


       //atap rumah
       glPushMatrix ();
              glColor3f (9 , 2, 1);
              glTranslatef (0 , 32.5, 0);
              glRotatef (45, 1, 0, 0);  
              glScalef (14.8, 7, 7);
              glutSolidCube(5);   
       glPopMatrix ();

       glPushMatrix ();
              glColor3f (3 , 1, 0);
              glTranslatef (0 , 35, 0);
              glRotatef (45, 1, 0, 0);  
              glScalef (14, 7, 7);
              glutSolidCube(5);   
       glPopMatrix ();

       //atap tambahan depan
       glPushMatrix ();
              glColor3f (1 , 1, 1);
              glTranslatef (0 , 30, 25);
              glRotatef (90, 0, 0, 1);
              glScalef (1 ,60 , 5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //atap tambahan belakang
       glPushMatrix ();
              glColor3f (1 , 1, 1);
              glTranslatef (0 , 30, -25);
              glRotatef (90, 0, 0, 1);
              glScalef (1 ,60 , 5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pintu depan
       glPushMatrix ();
              glColor3f (4 , 0, 0);
              glTranslatef (6 , -10, 22.4);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 20, 10);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pintu depan 2
       glPushMatrix ();
              glColor3f (4 , 0, 0);
              glTranslatef (-6.25 , -10, 22.4);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 20, 10);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pintu belakang
       glPushMatrix ();
              glColor3f (4 , 0, 0);
              glTranslatef (-18 , -13, -22.4);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 15, 8);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jendela atas kanan
       glPushMatrix ();
              glColor3f (0 , 0, 0);
              glTranslatef (25 , 15, 22.4);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 12, 6);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jendela atas kiri
       glPushMatrix ();
              glColor3f (0 , 0, 0);
              glTranslatef (-25 , 15, 22.4);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 12, 6);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jendela atas samping kiri
       glPushMatrix ();
              glColor3f (0 , 0, 0);
              glTranslatef (-38 , 15, 15);
              glScalef (1, 12, 6);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jendela atas samping kanan
       glPushMatrix ();
              glColor3f (4 , 8, 0);
              glTranslatef (38 , 15, -15);
              glScalef (1, 12, 6);
              glutSolidCube(1.2);
       glPopMatrix ();
      
       //jendela bawah kanan
       glPushMatrix ();
              glColor3f (4 , 8, 0);
              glTranslatef (30 , -7, 22);
              glRotatef (90, 0, 1, 0);
              glScalef (2, 9, 5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jendela bawah kiri
       glPushMatrix ();
              glColor3f (4 , 8, 0);
              glTranslatef (-30 , -7, 22);
              glRotatef (90, 0, 1, 0);
              glScalef (2, 9, 5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pagar depan kiri
       glPushMatrix ();
              glColor3f (5 , 3, 9);
              glTranslatef (-32 , -18, 60);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 10, 36.5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pagar depan kanan
       glPushMatrix ();
              glColor3f (8 , 0, 4);
              glTranslatef (32 , -18, 60);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 10, 36.5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pagar belakang
       glPushMatrix ();
              glColor3f (8 , 0, 4);
              glTranslatef (0 , -18, -60);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 10, 90);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pagar kiri
       glPushMatrix ();
              glColor3f (8 , 0, 4);
              glTranslatef (53.5 , -18, 0);
              glScalef (1, 10, 101);
              glutSolidCube(1.2);
       glPopMatrix ();

       //pagar kiri
       glPushMatrix ();
              glColor3f (8 , 0, 4);
              glTranslatef (-53.5 , -18, 0);
              glScalef (1, 10, 100);
              glutSolidCube(1.2);
       glPopMatrix ();
      
       //lampu pagar depan kiri
       glPushMatrix ();
              glColor3f (6 , 7, 0);
              glTranslatef (-51.5 , -9.5, 59.5);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 1, 1);
              glutSolidSphere (3, 36, 36);
       glPopMatrix ();

       //lampu pagar belakang kiri
       glPushMatrix ();
              glColor3f (6 , 7, 0);
              glTranslatef (-51.5 , -9.5, -59.5);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 1, 1);
              glutSolidSphere (3, 36, 36);
       glPopMatrix ();

       //lampu pagar depan kanan
       glPushMatrix ();
              glColor3f (6 , 7, 0);
              glTranslatef (51.5 , -9.5, 59.5);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 1, 1);
              glutSolidSphere (3, 36, 36);
       glPopMatrix ();

       //lampu pagar belakang kanan
       glPushMatrix ();
              glColor3f (6 , 7, 0);
              glTranslatef (51.5 , -9.5, -59.5);
              glRotatef (90, 0, 1, 0);
              glScalef (1, 1, 1);
              glutSolidSphere (3, 36, 36);
       glPopMatrix ();

       //jalan depan pintu
       glPushMatrix ();
              glColor3f (0, 3, 0);
              glTranslatef (0 , -23.05, 40);
              glRotatef (90, 0, 0, 1);
              glScalef (1 ,15 , 30.5);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jalan depan
       glPushMatrix ();
              glColor3f (1 , 1, 1);
              glTranslatef (0 , -25, 80);
              glRotatef (90, 0, 0, 1);
              glScalef (1, 400, 25);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jalan belakang
       glPushMatrix ();
              glColor3f (1 , 1, 1);
              glTranslatef (0 , -25, -80);
              glRotatef (90, 0, 0, 1);
              glScalef (1, 400, 25);
              glutSolidCube(1.2);
       glPopMatrix ();


       //jalan samping kanan
       glPushMatrix ();
              glColor3f (1 , 1, 1);
              glTranslatef (80 , -25, 80);
              glRotatef (90, 0, 0, 1);
              glScalef (1,25 , 400);
              glutSolidCube(1.2);
       glPopMatrix ();

       //jalan samping kiri
       glPushMatrix ();
              glColor3f (1 , 1, 1);
              glTranslatef (-80 , -25, 80);
              glRotatef (90, 0, 0, 1);
              glScalef (1,25 , 400);
              glutSolidCube(1.2);
       glPopMatrix ();

       glDisable (GL_COLOR_MATERIAL );

       glutSwapBuffers ();
}

void resize (int w1, int h1)
{
       glViewport (0, 0 , w1, h1);
       glMatrixMode (GL_PROJECTION );
       glLoadIdentity ();
       gluPerspective (45.0 , (float) w1/ (float) h1, 1.0, 300.0);
       glMatrixMode (GL_MODELVIEW );
       glLoadIdentity ();
}

void myKeyboard (unsigned char  key, int x, int y)
{
       switch (key)
       {
              case 'x' :
                     x1 = 1;
                     y1 = 0;
                     z1 = 0;
                     sudut+=-10;
                     break ;
              case 'y' :
                     x1 = 0;
                     y1 = 1;
                     z1 = 0;
                     sudut+=-10;
                     break ;
              case 'z' :
                     x1 = 0;
                     y1 = 0;
                     z1 = 1;
                     sudut+=-10;
                     break ;
       }
}

void mouse(int tombol, int state, int x, int y){
 cx = x-(w/2);
 cy = (h/2)-y;
}

void motion (int x, int y)
{
 cx = x-(w/2);
 cy = (h/2)-y;
}

void update(int value)
{
       glutPostRedisplay ();
       glutTimerFunc (50 , update , 0);
}

void main (int argc, char **argv)
{
       glutInit (&argc, argv);
       glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA );
       glutInitWindowPosition (10, 10);
       glutInitWindowSize (w ,h);
       glutCreateWindow ("Ria R. Lestari | Ilma Sofiana");

       glutReshapeFunc (resize );
       glutDisplayFunc (renderScene );
       glutMouseFunc(mouse);
       glutMotionFunc(motion);
       glutTimerFunc (50, update , 0);
       glutKeyboardFunc (myKeyboard );
       init ();
       glutMainLoop ();
}
















Komentar

Posting Komentar

Mohon Berkomentar dengan bahasa yang sopan. Terimakasih ^^

Postingan populer dari blog ini

Kita Masih diperjalanan

Salah Tujuan