Selasa, 04 Desember 2012

Membuat Rumah 3D



//by wendel H selsily (672011094)
#include <gl\gl.h>
#include <gl\glut.h>
#include <gl\glu.h> /* GLU extention library */

void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);

int is_depth; /* depth testing flag */

int main (int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(40, 40);
    glutCreateWindow("Rumah 3D Berwarna");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutReshapeFunc(resize); 
    glutMainLoop();
    return 0;
}

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    is_depth = 1;
    glMatrixMode(GL_MODELVIEW);
    glPointSize(9.0);   
    glLineWidth(6.0f);

}

void display(void)
{
    if (is_depth)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    else
    glClear(GL_COLOR_BUFFER_BIT);
    //ini untuk buat lantainya
    glBegin(GL_QUADS);
        glColor3f(0.2f, 0.2f, 0.2f);
        glVertex3f(-100.0, 0.0, -100.0);
        glColor3f(0.4f, 0.4f, 0.4f);
        glVertex3f(-100.0, 0.0, 100.0);
        glColor3f(0.6f, 0.6f, 0.6f);
        glVertex3f(100.0, 0.0, 100.0);
        glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(100.0, 0.0, -100.0);       
    glEnd();
   
     glBegin(GL_QUADS);//depan rumah
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(-50.0, 0.0, 100.0);
        glVertex3f(-50.0, 50.0, 100.0);
        glVertex3f(50.0, 50.0, 100.0);
           glVertex3f(50.0, 0.0, 100.0);       
    glEnd();
    glBegin(GL_QUADS);//garis depan rumah`
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(-50.0, 0.0, 100.1);
        glVertex3f(-50.0, 50.0, 100.1);
        glVertex3f(50.0, 50.0, 100.1);
           glVertex3f(50.0, 0.0, 100.1);       
    glEnd();
   
    glBegin(GL_QUADS);// pintu rumah
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-15.1, 0.0, 100.5);
        glVertex3f(-15.1, 40.0, 100.5);
        glVertex3f(15.1, 40.0, 100.5);
           glVertex3f(15.1, 0.0, 100.5);       
    glEnd();
    glBegin(GL_LINE_LOOP);//garis pintu rumah
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-15.1, 0.0, 100.6);
        glVertex3f(-15.1, 40.0, 100.6);
        glVertex3f(15.1, 40.0, 100.6);
           glVertex3f(15.1, 0.0, 100.6);       
    glEnd();


    glBegin(GL_POINTS);// tangan pintu
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-13.0, 20.0, 100.8);
    glEnd();
   
   
   glBegin(GL_TRIANGLES);//atap depan rumah
        glColor3f(1.0f,0.6f,0.0f);
        glVertex3f(0.0, 100.0, 100.0);
        glVertex3f(50.0, 50.0, 100.0);
           glVertex3f(-50.0, 50.0, 100.0);
    glEnd();
    glBegin(GL_LINE_LOOP);//garis atap depan rumah
        glColor3f(0.0f,0.0f,0.0f);
        glVertex3f(0.0, 100.0, 100.0);
        glVertex3f(50.0, 50.0, 100.0);
           glVertex3f(-50.0, 50.0, 100.0);
    glEnd();


    glBegin(GL_QUADS);//samping kiri
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(-50.0, 0.0, 100.0);
        glVertex3f(-50.0, 50.0, 100.0);
        glVertex3f(-50.0, 50.0, -100.0);
           glVertex3f(-50.0, 0.0, -100.0);       
    glEnd();
    glBegin(GL_LINE_LOOP);//garis samping kiri
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-50.0, 0.0, 100.0);
        glVertex3f(-50.0, 50.0, 100.0);
        glVertex3f(-50.0, 50.0, -100.0);
           glVertex3f(-50.0, 0.0, -100.0);       
    glEnd();
       
    glBegin(GL_QUADS);//jendela samping kiri (kiri atas)
        glColor3f(0.7f,0.7f,0.7f);
        glVertex3f(-51.1, 25.0, 0.0);
        glVertex3f(-51.1, 40.0, 0.0);
        glVertex3f(-51.1, 40.0, -50.0);
           glVertex3f(-51.1, 25.0, -50.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kiri atas)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-51.1, 25.0, 0.0);
        glVertex3f(-51.1, 40.0, 0.0);
        glVertex3f(-51.1, 40.0, -50.0);
           glVertex3f(-51.1, 25.0, -50.0);       
    glEnd();
   
    glBegin(GL_QUADS);//jendela samping kiri (kiri bawah)
        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(-51.1, 10.0, 0.0);
        glVertex3f(-51.1, 25.0, 0.0);
        glVertex3f(-51.1, 25.0, -50.0);
           glVertex3f(-51.1, 10.0, -50.0);       
    glEnd();
        glBegin(GL_QUADS);//garis jendela samping kiri (kiri bawah)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-51.1, 10.0, 0.0);
        glVertex3f(-51.1, 25.0, 0.0);
        glVertex3f(-51.1, 25.0, -50.0);
           glVertex3f(-51.1, 10.0, -50.0);       
    glEnd();
        glBegin(GL_QUADS);//jendela samping kiri (kanan bawah)
        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(-51.1, 10.0, 50.0);
        glVertex3f(-51.1, 25.0, 50.0);
        glVertex3f(-51.1, 25.0, -50.0);
           glVertex3f(-51.1, 10.0, -50.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kanan bawah)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-51.1, 10.0, 50.0);
        glVertex3f(-51.1, 25.0, 50.0);
        glVertex3f(-51.1, 25.0, -50.0);
           glVertex3f(-51.1, 10.0, -50.0);       
    glEnd();
   
    glBegin(GL_QUADS);//jendela samping kiri (kanan atas)
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-51.1, 25.0, 0.0);
        glVertex3f(-51.1, 40.0, 0.0);
        glVertex3f(-51.1, 40.0, 50.0);
           glVertex3f(-51.1, 25.0, 50.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kanan atas)
        glColor3f(0.0f,0.0f,0.0f);
        glVertex3f(-51.1, 25.0, 0.0);
        glVertex3f(-51.1, 40.0, 0.0);
        glVertex3f(-51.1, 40.0, 50.0);
           glVertex3f(-51.1, 25.0, 50.0);       
    glEnd();
   
   glBegin(GL_QUADS);//atap samping kiri
        glColor3f(1.0f,0.4f,0.0f);
        glVertex3f(-50.0, 50.0, -100.0);
        glVertex3f(0.0, 100.0, -100.0);
        glVertex3f(0.0, 100.0, 100.0);
           glVertex3f(-50.0, 50.0, 100.0);           
    glEnd();
       glBegin(GL_LINE_LOOP);//garis atap samping kiri
        glColor3f(0.0f,0.0f,0.0f);
        glVertex3f(-50.0, 50.0, -100.0);
        glVertex3f(0.0, 100.0, -100.0);
        glVertex3f(0.0, 100.0, 100.0);
           glVertex3f(-50.0, 50.0, 100.0);           
    glEnd();
   
    glBegin(GL_QUADS);//samping kanan
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(50.0, 0.0, 100.0);
        glVertex3f(50.0, 50.0, 100.0);
        glVertex3f(50.0, 50.0, -100.0);
           glVertex3f(50.0, 0.0, -100.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis samping kanan
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(50.0, 0.0, 100.0);
        glVertex3f(50.0, 50.0, 100.0);
        glVertex3f(50.0, 50.0, -100.0);
           glVertex3f(50.0, 0.0, -100.0);       
    glEnd();
   
   glBegin(GL_QUADS);//jendela samping kanan (kanan atas)
        glColor3f(0.7f,0.7f,0.7f);
        glVertex3f(51.1, 25.0, 0.0);
        glVertex3f(51.1, 40.0, 0.0);
        glVertex3f(51.1, 40.0, -50.0);
           glVertex3f(51.1, 25.0, -50.0);       
    glEnd();
       glBegin(GL_LINE_LOOP);//garis jendela samping kanan (kanan atas)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(51.1, 25.0, 0.0);
        glVertex3f(51.1, 40.0, 0.0);
        glVertex3f(51.1, 40.0, -50.0);
           glVertex3f(51.1, 25.0, -50.0);       
    glEnd();
   
    glBegin(GL_QUADS);//jendela samping kanan (kanan bawah)
        glColor3f(1.0f, 0.0f, 1.0f);
        glVertex3f(51.1, 10.0, 0.0);
        glVertex3f(51.1, 25.0, 0.0);
        glVertex3f(51.1, 25.0, -50.0);
           glVertex3f(51.1, 10.0, -50.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis jendela samping kanan (kanan bawah)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(51.1, 10.0, 0.0);
        glVertex3f(51.1, 25.0, 0.0);
        glVertex3f(51.1, 25.0, -50.0);
           glVertex3f(51.1, 10.0, -50.0);       
    glEnd();
   
    glBegin(GL_QUADS);//jendela samping kiri (kiri bawah)
        glColor3f(1.0f, 1.0f, 0.0f);
        glVertex3f(51.1, 10.0, 50.0);
        glVertex3f(51.1, 25.0, 50.0);
        glVertex3f(51.1, 25.0, -50.0);
           glVertex3f(51.1, 10.0, -50.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kiri bawah)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(51.1, 10.0, 50.0);
        glVertex3f(51.1, 25.0, 50.0);
        glVertex3f(51.1, 25.0, -50.0);
           glVertex3f(51.1, 10.0, -50.0);       
    glEnd();
   
    glBegin(GL_QUADS);//jendela samping kiri (kiri atas)
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(51.1, 25.0, 0.0);
        glVertex3f(51.1, 40.0, 0.0);
        glVertex3f(51.1, 40.0, 50.0);
           glVertex3f(51.1, 25.0, 50.0);       
    glEnd();
        glBegin(GL_LINE_LOOP);//garis jendela samping kiri (kiri atas)
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(51.1, 25.0, 0.0);
        glVertex3f(51.1, 40.0, 0.0);
        glVertex3f(51.1, 40.0, 50.0);
           glVertex3f(51.1, 25.0, 50.0);       
    glEnd();

   
    glBegin(GL_QUADS);//atap samping kanan
        glColor3f(1.0f,0.4f,0.0f);
        glVertex3f(50.0, 50.0, 100.0);
        glVertex3f(0.0, 100.0, 100.0);
        glVertex3f(0.0, 100.0, -100.0);
           glVertex3f(50.0, 50.0, -100.0);       
    glEnd();
    glBegin(GL_LINE_LOOP);//garis atap samping kanan
        glColor3f(0.0f,0.0f,0.0f);
        glVertex3f(50.0, 50.0, 100.0);
        glVertex3f(0.0, 100.0, 100.0);
        glVertex3f(0.0, 100.0, -100.0);
           glVertex3f(50.0, 50.0, -100.0);       
    glEnd();
       
    glBegin(GL_QUADS);//belakang
        glColor3f(0.0f, 1.0f, 1.0f);
        glVertex3f(-50.0, 0.0, -100.0);
        glVertex3f(-50.0, 50.0, -100.0);
        glVertex3f(50.0, 50.0, -100.0);
           glVertex3f(50.0, 0.0, -100.0);       
    glEnd();
    glBegin(GL_LINE_LOOP);//garis belakang
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-50.0, 0.0, -100.0);
        glVertex3f(-50.0, 50.0, -100.0);
        glVertex3f(50.0, 50.0, -100.0);
           glVertex3f(50.0, 0.0, -100.0);       
    glEnd();
   
   glBegin(GL_TRIANGLES);//atap belakang
         glColor3f(1.0f,0.6f,0.0f);
        glVertex3f(0.0, 100.0, -100.0);
        glVertex3f(-50.0, 50.0, -100.0);
        glVertex3f(50.0, 50.0, -100.0);
    glEnd();
    glBegin(GL_LINE_LOOP);//garis atap belakang
         glColor3f(0.0f,0.0f,0.0f);
        glVertex3f(0.0, 100.0, -100.0);
        glVertex3f(-50.0, 50.0, -100.0);
        glVertex3f(50.0, 50.0, -100.0);
    glEnd();

   
   
    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    /* Untuk kontrol dengan keyboard:
   
      "a": ke kiri
      "d": ke kanan
      "w": ke depan
      "s": ke belakang
      "t": toggle depth-testing
      "u" : ke atas
      "n" : ke bawah
        "i" : putar ke bawah terhadap sumbu X
        "j" : putar ke atas terhadap sumbu X       
        "o" : putar ke kiri terhadap sumbu Y
        "k" : putar ke kanan terhadap sumbu Y      
        "p" : putar ke kiri terhadap sumbu Z       
        "l" : putar ke kanan terhadap sumbu Z               
    */
    switch (key)
    {
    case 'a':
    case 'A':
        glTranslatef(5.0, 0.0, 0.0);
        break;
    case 'd':
    case 'D':
        glTranslatef(-5.0, 0.0, 0.0);
        break;
    case 'u' :
    case 'U' :
        glTranslatef(0.0,5.0,0.0);
        break;
    case 'n' :
    case 'N' :
        glTranslatef(0.0,-5.0, 0.0);
        break;
    case 'w':
    case 'W':
        glTranslatef(0.0, 0.0, 5.0);
        break;
    case 's':
    case 'S':
        glTranslatef(0.0, 0.0, -5.0);
        break;
    case 'i':
    case 'I':
        glRotatef(3.0,1.0,0.0,0.0);
        break;
    case 'j':
    case 'J':
        glRotatef(-3.0,1.0,0.0,0.0);
        break;
    case 'o':
    case 'O':
        glRotatef(3.0,0.0,1.0,0.0);
        break;
    case 'k':
    case 'K':
        glRotatef(-3.0,0.0,1.0,0.0);
        break;
    case 'p':
    case 'P':
        glRotatef(3.0,0.0,0.0,1.0);
        break;
    case 'l':
    case 'L':
        glRotatef(-3.0,0.0,0.0,1.0);
        break;
    case 't':
    case 'T':
        if (is_depth)
        {
            is_depth = 0;
            glDisable(GL_DEPTH_TEST);
        }
        else
        {
            is_depth = 1;
            glEnable(GL_DEPTH_TEST);
        }
    }
    display();
}

void resize(int width, int height)
{
    if (height == 0) height = 1;
   
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, width / height, 1.0, 400.0);
    /* Inisialisasi posisi */
    glTranslatef(0.0, -5.0, -150.0);
    glMatrixMode(GL_MODELVIEW);
}





ini adalah tombol Untuk kontrol posisi Rumah di atas dengan menggunakan keyboard :
     
 "a": ke kiri
 "d": ke kanan
 "w": ke depan
 "s": ke belakang
 "t": toggle depth-testing
 "u" : ke atas
 "n" : ke bawah
 "i" : putar ke bawah terhadap sumbu X
 "j" : putar ke atas terhadap sumbu X       
 "o" : putar ke kiri terhadap sumbu Y
 "k" : putar ke kanan terhadap sumbu Y      
 "p" : putar ke kiri terhadap sumbu Z       
 "l" : putar ke kanan terhadap sumbu Z    



silahkan download coding dan project jadinya klik di sini

Tidak ada komentar:

Posting Komentar