Membuat Objek Ruangan Menggunakan OpenGL [Source Code]

Ilmu Informatika : Membuat Objek Ruangan Menggunakan OpenGL [Source Code]. Membuat Objek Ruangan dan Interior Menggunakan OpenGL (Kamar)+Source Code - Berikut ini adalah source code dan gambar dari projek akhir mata kuliah pemrograman grafik saya (Pendidikan Teknik Informatika dan Komputer UNNES Semarang).
semua objek yang dibuat ini adalah rangkaian dari banyak vertex, jadi masih perlu untuk disempurnakan dengan penambah glPushMatrix() dan glPopmatrix() agar dalam merubah bentuk dan posisi lebih mudah yaitu : glScalef, glRotatef, glTranslate. (harus dikembangkan lagi).

Contoh Objek yang saya buat (Cuplikan)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <GL/glut.h>
#include "SOIL.h"

float z_pos=-2.0f;
float y_pos=0.50f;
float x_pos=0.0f;

float sudut=0.0f;
float  rot =0.0f;

//tempat mengambil texture
GLuint tex_1;
GLuint tex_2;
GLuint tex_3;
GLuint tex_4;
GLuint tex_5;
GLuint tex_6;

GLuint texture[1];
void init(void);
void myKeyboard(unsigned char, int, int);
void myDisplay(void);
void myTimeOut(int);
void resize(int, int);

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500, 400);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("5302412099");
    glutDisplayFunc(myDisplay);
    glutKeyboardFunc(myKeyboard);
    glutTimerFunc(100, myTimeOut, 0);
    glutReshapeFunc(resize);
    init();
    glutMainLoop();
    return 0;
    }

GLuint LoadGLTextures(const char* filename)
{
    GLuint tex;
    tex = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
    if(tex == 0)
        {
            printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
        }
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    return tex;
    // Return Success
}

void init(void)
{

    tex_1 = LoadGLTextures("gambar/biru.png"); //tembok
    tex_2 = LoadGLTextures("gambar/atas.bmp"); //langit2 kamar
    tex_3 = LoadGLTextures("gambar/lemari.bmp"); //pintu almari
    tex_4 = LoadGLTextures("gambar/almari_sisi.bmp"); //cat almari
    tex_5 = LoadGLTextures("gambar/pintu.bmp"); //pintu kamar
    tex_6 = LoadGLTextures("gambar/keramik.jpg");

    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0, 0.0, 0.0, 1.0);// A background clear color
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

}
void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glTranslatef(x_pos, y_pos, z_pos);
    glRotatef(rot, 0.0f, 1.0f, 0.0f);

    /*MEMBUAT RUANGAN*/
    glBindTexture(GL_TEXTURE_2D, tex_1);
    glBegin(GL_QUADS);
    // tembok belakang
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f,  0.5f, -1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f,  0.5f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glEnd();
    //atas || langit2
    glBindTexture(GL_TEXTURE_2D, tex_2);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, 0.5f, 1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f,  0.5f, -1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f,  0.5f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, 0.5f, 1.0f);
    glEnd();
    // lantai keramik
    glBindTexture(GL_TEXTURE_2D, tex_6);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f,  -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f,  -1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glEnd();
    // tembok kanan
    glBindTexture(GL_TEXTURE_2D, tex_1);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( 1.0f,  -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0f,  0.5f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(1.0f, 0.5f, 1.0f);
    glEnd();
    // tembok kiri
    glBindTexture(GL_TEXTURE_2D, tex_1);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-1.0f,  -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-1.0f,  0.5f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0f, 0.5f, 1.0f);
    glEnd();

    /*ALMARI*/
    //depan
    glBindTexture(GL_TEXTURE_2D, tex_3);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0.1f,  -1.0f, -0.75f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0.1f, 0.0f, -0.75f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-0.65f, 0.0f, -0.75f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-0.65f,  -1.0f, -0.75f);
    glEnd();
    //kanan
    glBindTexture(GL_TEXTURE_2D, tex_4);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0.1f,  -1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0.1f, 0.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(0.1f, 0.0f, -0.75f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0.1f,  -1.0f, -0.75f);
    glEnd();
    //kiri
    glBindTexture(GL_TEXTURE_2D, tex_4);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-0.65f,  -1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-0.65f, 0.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-0.65f, 0.0f, -0.75f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-0.65f,  -1.0f, -0.75f);
    glEnd();
    //atas
    glBindTexture(GL_TEXTURE_2D, tex_4);
    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0.1f,  0.0f, -0.75f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(0.1f, 0.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-0.65f, 0.0f, -1.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-0.65f,  0.0f, -0.75f);
    glEnd();

    glFlush();
    glutSwapBuffers();
}
void myKeyboard(unsigned char key, int x, int y)
{

  if((key=='<')||(key==',')) z_pos-=0.01f;
  if((key=='>')||(key=='.')) z_pos+=0.01f;
  if((key=='s')||(key=='S')) y_pos+=0.01f;
  if((key=='w')||(key=='W')) y_pos-=0.01f;
  if((key=='d')||(key=='D')) x_pos-=0.01f;
  if((key=='a')||(key=='A')) x_pos+=0.01f;

        if(sudut==0) //berhenti dan berputar
            {
                if(key=='q') sudut=3.0f;
            }
        else  {
                if(key=='q') sudut=0.0f;
            }
        if(sudut==0) //berhenti dan berputar
            {
                if(key=='e') sudut=-3.0f;
            }
        else {
                if(key=='e') sudut=0.0f;
            }
}
void myTimeOut(int id)
{
    rot+=sudut;
    glutPostRedisplay();
    glutTimerFunc(100, myTimeOut, 0);
}
void resize(int width, int height)
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);
    glMatrixMode(GL_PROJECTION);    glLoadIdentity();
    gluPerspective(45.0, (GLdouble)width / (GLdouble)height, 0.01f, 700.0f);
    glMatrixMode(GL_MODELVIEW);     glLoadIdentity();
}

Dan inilah hasilnya:


Silahkan bagi yang ingin mendownload bisa di download disini. (password : isrul.net)
Demikian artikel mengenai Membuat Objek Ruangan Menggunakan OpenGL [Source Code]. Semoga bermanfaat untuk sobat semua.
Nikmati Jasa & Layanan yang ada di www.isrul.com, diantaranya:
1. Jasa Pembuatan dan Verifikasi Akun Paypal dengan VCC (Valid 2 atau 3 Tahun)
2. Jual Akun Adsense Non Hosted Indonesia (0899-5832-699)
3. Penulis Tamu di Isrul.com

0 Response to "Membuat Objek Ruangan Menggunakan OpenGL [Source Code]"

Post a Comment

Jika anda bertanya, jangan lupa mencentang "Notify Me" di bagian kanan bawah, agar mendapat pemberitahuan jika pertanyaan anda telah kami jawab.
Terimakasih telah menyempatkan waktu berkomentar...