Nov 30, 2010

Membuat Gambar Rumah Meggunakan Pelles C

Bagaimana cara membuat gambar rumah 2D menggunakan Pelles C ?? Coding ini bisa di execute karena menggunakan wizard dari lab TI Universitas Gunadarma karena ini merupakan laporan akhir Grafik Komputer. Jika tidak ada wizard tersebut, anda bisa copy coding dari  

gluLookAt (........) 

atau coding di bawah kata - kata 

         // lakukan penggambaran di sini
        //---------------------------------------------

Berikut ini adalah LISTING PROGRAMNYA :



#include
#include
#include
#include

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 (0,0,30,
            0,0,0,
            0,1,0);

glBegin (GL_QUADS);
glColor3f (1,1,1);
glVertex2f (-5,-5);
glVertex2f (5,-5);
glVertex2f (5,5);
glVertex2f (-5,5);
glEnd();

glBegin (GL_QUADS);
glColor3f (1,0,1);
glVertex2f (-2,-5);
glVertex2f (2,-5);
glVertex2f (2,-1);
glVertex2f (-2,-1);
glEnd();

glBegin (GL_TRIANGLES);
glColor3f (1,0,0);
glVertex2f (-5,5);
glVertex2f (5,5);
glVertex2f (0,10);
glEnd();

glBegin (GL_POLYGON);
glColor3f (0,0,1);
glVertex2f (-3,1);
glVertex2f (-1.5,1);
glVertex2f (-0.5,3);
glColor3f (1,1,1);
glVertex2f (-1.5,4);
glVertex2f (-3,4);
glVertex2f (-4,3);
glEnd();

glBegin (GL_POLYGON);
glColor3f (0,0,1);
glVertex2f (3,1);
glVertex2f (1.5,1);
glVertex2f (0.5,3);
glColor3f (1,1,1);
glVertex2f (1.5,4);
glVertex2f (3,4);
glVertex2f (4,3);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (5,-3);
glVertex2f (7,-3);
glVertex2f (7,-1);
glVertex2f (5,-1);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (9,-5);
glVertex2f (7,-5);
glVertex2f (7,0);
glVertex2f (9,0);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (9,-3);
glVertex2f (11,-3);
glVertex2f (11,-1);
glVertex2f (9,-1);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (11,-5);
glVertex2f (13,-5);
glVertex2f (13,0);
glVertex2f (11,0);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (-5,-3);
glVertex2f (-7,-3);
glVertex2f (-7,-1);
glVertex2f (-5,-1);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (-9,-5);
glVertex2f (-7,-5);
glVertex2f (-7,0);
glVertex2f (-9,0);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (-9,-3);
glVertex2f (-11,-3);
glVertex2f (-11,-1);
glVertex2f (-9,-1);
glEnd();

glBegin (GL_QUADS);
glColor3f (0,0,20);
glVertex2f (-11,-5);
glVertex2f (-13,-5);
glVertex2f (-13,0);
glVertex2f (-11,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 );
}

OUTPUT



No comments:

Post a Comment