Posted by : Unknown Rabu, 01 Mei 2013

Sekedar sharing tugas kuliah Grafika Komputer saya tentang Lighting Objek 3 Dimensi menggunakan OpenGL yaitu membuat objek rumah 3 dimensi yang dilengkapi dengan interaksi keyboard untuk memutar objek dan lighting posisi cahaya yang berbeda-beda menggunakan interaksi mouse. Tampilan objek rumah 3D yang saya buat seperti berikut:


Pencahayaan menggunakan interaksi mouse:

Rotasi Objek menggunakan interaksi keyboard:

Script program lengkapnya untuk membuat desain objek rumah 3 dimensi seperti pada gambar di atas adalah sebagai berikut:


#include "stdlib.h"
#include "glut.h"

int w = 400, h = 400;
int sudut=0, o=0, p=0, q=0;
int a, b;

void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLfloat LightPosition[] = {10.0f, 20.0f, 30.0f, 0.0f};
LightPosition[0] = a;
LightPosition[1] = b;
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glRotatef(sudut, o, p, q);
glEnable(GL_COLOR_MATERIAL);

//dinding rumah
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(0, 0, -100);
glRotatef(50, 0, 1 ,0);
glColor3f(0, 0, 1);
glutSolidCube(20);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

//atap
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(0, 10, -100);
glScalef(20, 10, 20);
glRotatef(5, 0, 1, 0);
glColor3f(0, 1, 0);
glutSolidOctahedron();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

//pintu
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(1, 0, 0);
glTranslatef(-9,-5, -95);
glRotatef(140, 0, 1, 0);
glScalef(6,10,1);
glutSolidCube(1);
glPopMatrix();

//jendela
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(1, 1, 0);
glTranslatef(-5,-3, -90);
glRotatef(130, 0, 1, 0);
glScalef(2,2,1);
glutSolidCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(1, 1, 0);
glTranslatef(-2,-3, -80);
glRotatef(130, 0, 1, 0);
glScalef(2,2,1);
glutSolidCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(1, 1, 0);
glTranslatef(-2,0, -80);
glRotatef(130, 0, 1, 0);
glScalef(2,2,1);
glutSolidCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(1, 1, 0);
glTranslatef(-5,0, -90);
glRotatef(130, 0, 1, 0);
glScalef(2,2,1);
glutSolidCube(1);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

//pagar
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(0, 1, 0);
glTranslatef(-30, -5, -110);
glRotatef(50, 0, 1, 0);
glScalef(25,5,1);
glutSolidCube(2);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(0, 1, 0);
glTranslatef(10, -5, -110);
glRotatef(140, 0, 1, 0);
glScalef(30,5,1);
glutSolidCube(2);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(0, 1, 0);
glTranslatef(25, -5, -82);
glRotatef(50, 0, 1, 0);
glScalef(20,5,1);
glutSolidCube(2);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(0, 1, 0);
glTranslatef(-32, -5, -74);
glRotatef(140, 0, 1, 0);
glScalef(10,5,1);
glutSolidCube(2);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();

//lantai
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glColor3f(1, 1, 1);
glTranslatef(0, -11, -100);
glRotatef(90, 1, 0, 0);
glScalef(50,50,1);
glutSolidCube(2);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}

void resize(int w1, int h1){
glViewport(0,0,400,400);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(float) w1/(float) h1, 1.0,200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button, int state, int x, int y){
a = x-(w/2);
b = (h/2)-y;
}

void motion (int x, int y){
a = x-(w/2);
b = (h/2)-y;
}

void myKeyboard(unsigned char key, int x, int y){
if (key == 'a') {
o = 1;
p = 0;
q = 0;
sudut+=10;
}

else if (key == 'd'){
o = 0;
p = 1;
q = 0;
sudut+=10;
}

else if (key == 's'){
o = 0;
p = 0;
q = 1;
sudut+=10;
}
}

void timer(int value){
glutPostRedisplay();
glutTimerFunc(50,timer,0);
}

void init(){
GLfloat LightAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat LightDiffuse[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat LightSpecular[] = {0.5f, 10.0f, 20.0f, 10.0f};
GLfloat Shine[] = {80};
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMaterialfv(GL_FRONT, GL_SPECULAR, LightSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, Shine);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
return;
}

void main (int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(w,h);
glutCreateWindow("3D");
init();
glutDisplayFunc(renderScene);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(myKeyboard);
glutTimerFunc(1,timer,0);
glutMainLoop();
}
Selamat Mencoba!!! XD

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Date & Time

Follower

mira. Diberdayakan oleh Blogger.

Copyright © Myrrh's World -Black Rock Shooter- Powered by Blogger - Designed by Johanes Djogan