GLUT: Membuat Piramida 3D

#include <GL/glut.h>
#include <stdlib.h>

int xRot = 0, yRot = 0;
void grid3d(int baris, int kolom, int z) {

    int i;

    int kiri  = -kolom / 2; //batas kiri x
    int kanan = kolom / 2; //batas kanan x
    int bawah = -baris / 2; //batas bawah y
    int atas  = baris / 2; //batas atas y

    glBegin(GL_LINES);
    glLineWidth(1);

    //Baris digambar dari atas ke bawah
    for (i = 0; i < baris + 1; i++) {
        glVertex3f(kiri, baris / 2 - i, z);
        glVertex3f(kanan, baris / 2 - i, z);
    }

    //Kolom digambar dari kanan ke kiri
    for (i = 0; i < kolom + 1; i++) {
        glVertex3f(kolom / 2 - i, bawah, z);
        glVertex3f(kolom / 2 - i, atas, z);
    }

    glLineWidth(3);
    glColor3f(0.5, 0.5, 0.5);
    glVertex3f(kiri, 0, z);
    glVertex3f(kanan, 0, z);
    glVertex3f(0, bawah, z);
    glVertex3f(0, atas, z);

    glEnd();
}
void pressKey(int key, int x, int y)
{
     switch (key)
     {
            case GLUT_KEY_LEFT :
                 yRot -= 1;
                 break;
            case GLUT_KEY_RIGHT :
                 yRot += 1;
                 break;
            case GLUT_KEY_UP :
                 xRot -= 1;
                 break;
            case GLUT_KEY_DOWN :
                 xRot += 1;
                 break;
     }
     glutPostRedisplay();
}
void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
    //Let there be the grid!
    glColor3f(0.3, 0.3, 0.3);
    grid3d(20, 20, -5);
   
    glPushMatrix();
   glRotatef(xRot, 1.0, 0.0, 0.0);
   glRotatef(yRot, 0.0, 1.0, 0.0);
    // Your object here
   
   
    float V0[3] = { 0.0, 5.0, 0.0};
    float V1[3] = {-5.0,-5.0, 5.0};
    float V2[3] = { 5.0,-5.0, 5.0};
    float V3[3] = { 5.0,-5.0,-5.0};
    float V4[3] = {-5.0,-5.0,-5.0};
    glBegin(GL_TRIANGLE_FAN);
    glColor3ub(255, 0, 0);
    glVertex3fv(V0);
    glColor3ub(0, 255, 0);
    glVertex3fv(V1);
    glColor3ub(0, 0, 255);
    glVertex3fv(V2);
    glColor3ub(255, 255, 0);
    glVertex3fv(V3);
    glColor3ub(255, 0, 255);
    glVertex3fv(V4);
    glColor3ub(0, 0, 255);
    glVertex3fv(V1);
    glEnd();
   
    glBegin(GL_QUADS);
    glVertex3fv(V1);
    glVertex3fv(V2);
    glVertex3fv(V3);
    glVertex3fv(V4);
    glEnd();
   
    glPopMatrix();
   
    glutSwapBuffers();
    glFlush();
}

void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //glFrustum(-10.0, 10.0, -10.0, 10.0, 4.0, 50.0);
    gluPerspective(45, 1, 1, 100);
    gluLookAt(0, 0, 30, 0, 0, 0, 0.0, 1.0, 0.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char * * argv) {
    glutInit( & argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowSize(400, 400);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Grid");

    glutDisplayFunc(display);
    glutSpecialFunc(pressKey);
    init();

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);   
   
    glutMainLoop();

    return 0;
}

Comments

Popular posts from this blog

Kolam Renang Khusus Wanita di Bogor