Sabtu, 19 November 2011

Membuat Polygon Pada Pelles C

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glfw.h>

void mulaiOpenGL(void);

int main(void)
{
    //
    // mRunning = TRUE, aplikasi masih berjalan
    // mRunning = FALSE, ??? :p
    GLuint mRunning = GL_TRUE;

    //
    // inisialisasi GLFW
    if( glfwInit() == GL_FALSE )
    {
        MessageBox( NULL, "ERROR :: gagal menginisialisasi GLFW", "Error!", MB_OK);
        return(0);
    }
    //
    // buat sebuah window yang akan digunakan untuk menggambar.
    if( glfwOpenWindow( 640, 480, 0, 0, 0, 0, 24, 0, GLFW_WINDOW ) == GL_FALSE )
    {
        MessageBox( NULL, "ERROR :: gagal membuat window", "Error!", MB_OK );
        glfwTerminate();
        return(0);
    }
    //
    // Set judul yang ada di window dan Swap interval.
    glfwSetWindowTitle( "Praktikum Grafik Komputer LabTI" );
    glfwSwapInterval( 1 );
    //
    // mulai OpenGL (melakukan setting awal OpenGL)
    mulaiOpenGL();
    //
    // mulai looping utama program
    while( mRunning )
    {
        //
        // bersihkan layar dan depth buffer
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glLoadIdentity();
        //
        // lakukan penggambaran di sini
        //------------------------------------------------------------------------------------


gluLookAt (20,20,35,0,0,0,0,1,0);
glRotatef(1,0,1,0);

glBegin (GL_POLYGON);
//depan
glColor3ub (255,0,255);
glVertex3f (-5,0,0);
glVertex3f (0,0,0);
glVertex3f (2,3,0);
glColor3ub (0,0,0);
glVertex3f (0,6,0);
glVertex3f (-5,6,0);
glVertex3f (-7,3,0);
glEnd();

glBegin (GL_POLYGON);
//belakang
glColor3ub (255,0,255);
glVertex3f (-5,0,-4);
glVertex3f (0,0,-4);
glVertex3f (2,3,-4);
glColor3ub (0,0,0);
glVertex3f (0,6,-4);
glVertex3f (-5,6,-4);
glVertex3f (-7,3,-4);
glEnd();

glBegin (GL_QUADS);
//kanan bawah
glColor3ub (223,105,200);
glVertex3f (0,0,0);
glVertex3f (0,0,-4);
glColor3ub (0,0,0);
glVertex3f (2,3,-4);
glVertex3f (2,3,0);
glEnd();


glBegin (GL_QUADS);
//kanan atas
glColor3ub (0,105,200);
glVertex3f (2,3,0);
glVertex3f (2,3,-4);
glColor3ub (0,0,0);
glVertex3f (0,6,-4);
glVertex3f (0,6,0);
glEnd();


glBegin (GL_QUADS);
//atas
glColor3ub (200,255,0);
glVertex3f (0,6,0);
glVertex3f (0,6,-4);
glColor3ub (0,0,0);
glVertex3f (-5,6,-4);
glVertex3f (-5,6,0);
glEnd();

glBegin (GL_QUADS);
//kiri atas
glColor3ub (109,105,200);
glVertex3f (-5,6,0);
glVertex3f (-5,6,-4);
glColor3ub (0,0,0);
glVertex3f (-7,3,-4);
glVertex3f (-7,3,0);
glEnd();

glBegin (GL_QUADS);
//kiri bawah
glColor3ub (223,105,200);
glVertex3f (-7,3,0);
glVertex3f (-7,3,-4);
glColor3ub (0,0,0);
glVertex3f (-5,0,-4);
glVertex3f (-5,0,0);
glEnd();

glBegin (GL_QUADS);
//bawah
glColor3ub (189,245,210);
glVertex3f (-5,0,0);
glVertex3f (-5,0,-4);
glColor3ub (0,0,0);
glVertex3f (0,0,-4);
glVertex3f (0,0,0);
glEnd();


glTranslatef(20,0,0);
    glScalef(2,2,2);
    glRotatef(60,0,1,0);


glBegin (GL_POLYGON);
//depan
glColor3ub (255,0,0);
glVertex3f (-5,0,0);
glVertex3f (0,0,0);
glVertex3f (2,3,0);
glColor3ub (0,0,0);
glVertex3f (0,6,0);
glVertex3f (-5,6,0);
glVertex3f (-7,3,0);
glEnd();

glBegin (GL_POLYGON);
//belakang
glColor3ub (255,0,0);
glVertex3f (-5,0,-4);
glVertex3f (0,0,-4);
glVertex3f (2,3,-4);
glColor3ub (0,0,0);
glVertex3f (0,6,-4);
glVertex3f (-5,6,-4);
glVertex3f (-7,3,-4);
glEnd();

glBegin (GL_QUADS);
//kanan bawah
glColor3ub (223,105,200);
glVertex3f (0,0,0);
glVertex3f (0,0,-4);
glColor3ub (0,0,0);
glVertex3f (2,3,-4);
glVertex3f (2,3,0);
glEnd();


glBegin (GL_QUADS);
//kanan atas
glColor3ub (109,105,200);
glVertex3f (2,3,0);
glVertex3f (2,3,-4);
glColor3ub (0,0,0);
glVertex3f (0,6,-4);
glVertex3f (0,6,0);
glEnd();


glBegin (GL_QUADS);
//atas
glColor3ub (189,245,210);
glVertex3f (0,6,0);
glVertex3f (0,6,-4);
glColor3ub (0,0,0);
glVertex3f (-5,6,-4);
glVertex3f (-5,6,0);
glEnd();

glBegin (GL_QUADS);
//kiri atas
glColor3ub (109,105,200);
glVertex3f (-5,6,0);
glVertex3f (-5,6,-4);
glColor3ub (0,0,0);
glVertex3f (-7,3,-4);
glVertex3f (-7,3,0);
glEnd();

glBegin (GL_QUADS);
//kiri bawah
glColor3ub (223,105,200);
glVertex3f (-7,3,0);
glVertex3f (-7,3,-4);
glColor3ub (0,0,0);
glVertex3f (-5,0,-4);
glVertex3f (-5,0,0);
glEnd();

glBegin (GL_QUADS);
//bawah
glColor3ub (189,245,210);
glVertex3f (-5,0,0);
glVertex3f (-5,0,-4);
glColor3ub (0,0,0);
glVertex3f (0,0,-4);
glVertex3f (0,0,0);
glEnd();




        //------------------------------------------------------------------------------------
        //
        // tampilkan ke layar (swap double buffer)
        glfwSwapBuffers();

        //
        // check input , apakah tombol esc ditekan atau tombol "close" diclick
        mRunning = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
    }

    glfwTerminate();
    return(0);
}

void mulaiOpenGL(void)
{
    //
    // Set viewport ke resolusi 640x480 viewport bisa diibaratkan
    // layar monitor anda
    glViewport( 0, 0, 640, 480 );
    //
    // Set mode OpenGL ke mode pryeksi (Projection) dan set proyeksi
    // menggunakan proyeksi perspective, dengan sudut pandang (Field Of
    // View) 60 derajat
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 60.0f, 640.0f/480.0f, 0.1f, 1000.0f );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    //
    // Set mode gradasi warna halus (Smooth)
    glShadeModel( GL_SMOOTH );
    //
    // warna yang digunakan untuk membersihkan layar
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    //
    // nilai untuk membersihkan depth buffer.
    glClearDepth( 1.0f );
    //
    // Depth test digunakan untuk menghindari polygon yang
    // tumpang tindih.
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    //
    // beritahu OpenGL untuk menggunakan perhitungan perspective
    // yang terbaik (perhitungan ini tidak bisa selalu 100% akurat)
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}

Tidak ada komentar:

Posting Komentar