Membuat Gambar 3D dengan Visual C++

Posted by Hewry Agiel kurniawan on 11.31 in
Ehm,, saya mau posting tugas kuliah saya lagi neh, siapa tahu bisa membantu teman - teman saya yang sedang mengambil matakuliah Komputer Grafik. Tapi saya harap seh ini bukan dicopy paste lalu di kumpul. Saya seh gak masalah, tapi kan kalian jadi gak berkembang. Jadikan tugas saya ne sebagai bahan perbandingan atau contoh saja..!!

Ok langsung ke TKP kita! hehehe
Buka program visual C++, buat lembar baru dan copykan script berikut:
#include <GL/glut.h>
#include <math.h>

typedef
struct {
    float x,y,z;
} point3D_t;

typedef
struct {
    float v[4];
} vector3D_t;




typedef struct {
    int NumberofVertices;
    short int pnt[32];
}face_t;

typedef struct {
    int NumberofVertices;
    point3D_t pnt[100];
    int NumberofFaces;
    face_t fc[32];
}object3D_t;


GLfloat vertices[][3] = {{0.0,0.0,1.50},            
                         {0, 1,0.0},         
                         {0.866, 0.5},      
                         {0.866, -0.5},     
                         {0, -1},     
                         {-0.866, -0.5},     
                         {-0.866 , 0.5},     
                         {0.0,0.0,0.0}};

GLfloat normals[][3] = {{0.0,0.0,1.0},       
                         {0, 1,0.0},         
                         {0.866, 0.5},       
                         {0.866, -0.5},     
                         {0, -1},     
                         {-0.866, -0.5},    
                         {-0.866 , 0.5},    
                         {0.0,0.0,0.0}};    

GLfloat colors[][3] = {{0.0,0.0,0.0},       
                         {1.0,0.0,0.0},     
                         {1.0,1.0,0.0},     
                         {0.0,1.0,0.0},     
                         {0.0,1.0,1.0},     
                         {0.0,0.0,1.0},     
                         {1.0,0.0,1.0},     
                         {1.0,1.0,1.0}};   

void DrawPoints(int nNumPoints, GLfloat ctrlPoints[][3])
    {
    int i;   
    glPointSize(2.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_POINTS);
        for(i = 0; i < nNumPoints; i++)
           glVertex2fv(ctrlPoints[i]);
    glEnd();
    }

void polygon(int x, int y, int z )
{
    glBegin(GL_POLYGON);
        glColor3fv(colors[x]);
        glNormal3fv(normals[x]);
        glVertex3fv(vertices[x]);
        glColor3fv(colors[y]);
        glNormal3fv(normals[y]);
        glVertex3fv(vertices[y]);
        glColor3fv(colors[z]);
        glNormal3fv(normals[z]);
        glVertex3fv(vertices[z]);
    glEnd();
}

void colorkubus()
{
    polygon(1,7,2);
    polygon(2,7,3);
    polygon(3,7,4);
    polygon(4,7,5);
    polygon(5,7,6);
    polygon(6,7,1);
    polygon(1,0,2);
    polygon(2,0,3);
    polygon(3,0,4);
    polygon(4,0,5);
    polygon(5,0,6);
    polygon(6,0,1);
}

static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
static GLdouble viewer[]= {0.0, 0.0, 5.0};

void display(void)
{

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    glLoadIdentity();
    gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 1.0, 1.0, 0.0);



    colorkubus();
    //DrawPoints(7, vertices);

 glFlush();
    glutSwapBuffers();
}



void myReshape(int w, int h)
{
 glViewport(0, 0, w, h);


 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 gluPerspective(45.0, w/h, 2.0, 20.0);

 glMatrixMode(GL_MODELVIEW);
 glEnable(GL_DEPTH_TEST);
 glDisable(GL_CULL_FACE);
 // white background
 glClearColor(1.0f, 1.0f, 1.0f, 1.0f );

}

void
main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(500, 500);
 glutInitWindowPosition(250, 200);
 glutCreateWindow(".:OpenGL - 3Dku:.");
 glutReshapeFunc(myReshape);
 glutDisplayFunc(display);
 glutMainLoop();
}

Simpan dengan nama extention .cpp
Lalu di compile (CTR+F7) - build (F7) dan execute program (CTR+F5).
Maka hasilnya seperti gambar di bawah :
Selamat mencoba..
Terima kasih

Ditulis Oleh : Hewry Agiel kurniawan ~

Artikel Membuat Gambar 3D dengan Visual C++ ini diposting oleh Hewry Agiel kurniawan pada hari Jumat, 22 April 2011. Terimakasih atas kunjungan Anda serta kesediaan Anda membaca artikel ini. Kritik dan saran dapat anda sampaikan melalui kotak komentar.

:: Get this widget ! ::


|

Copyright © 2009 ILMU-ONLINE All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.