- Back to Home »
- Grafika Komputer »
- Lighting dan Material
Posted by : Virginia Only
Kamis, 09 Mei 2013
Ada yang mau bikin kayak gini gaaaaaaaaak????? :D Yuk merapat! kita main-main sama Grafika Komputer.
Kita buat program yang menampilkan
sebuah rumah dengan atap yang berbeda warna dengan temboknya dan dikelilingi
oleh pagar.
Nih Syntaxnya:
#include
#include
#include
int millis=1;
int action=0;
int w = 640, h = 640;//variabel global
float x=0,y=0,z=-25, teta=0, masuk=0;//posisi awal koordinat
void atap(void){
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(0,0.5,-4);
glRotatef(teta,0.,1.,0.);
glColor3f(0,0,1);
glRotatef(90,-1.,0.,0.);
glutSolidCone(1.2,1.,20.,20);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
}
void pintu(float alpa, float x, float y,float z, float r, float g, float b){
glPushMatrix();
glTranslatef(x,y,z);
glRotatef(alpa,0,1,0);
glColor3f(1,0,1);
glScalef(0.6,1.,0.01);
glutSolidCube(0.8);
glPopMatrix();
}
void jendela(float x, float y,float z, float r, float g, float b){
glPushMatrix();
glTranslatef(x,y,z);
glColor3f(1,0,1);
glScalef(1,1.,0.01);
glutSolidCube(0.5);
glPopMatrix();
}
void pagar(float x, float y,float z, float r, float g, float b, float d, float e, float f){
glPushMatrix();
glTranslatef(x,y,z);
glColor3f(0,1,1);
glScalef(d,e,f);
glutSolidCube(0.5);
glPopMatrix();
}
void gedung(void){
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(0.,0.,-4.);
glRotatef(teta,0.,1.,0.);
glColor3f(.8,.6,.7);
glutSolidCube(1.3);
pintu(masuk,.3,-0.24,0.66,0,1,1);
pintu(0,.3,-0.24,0.65,0,0,0);
jendela(-0.3,-0.1,0.66,1,0,1);
jendela(-0.3,-0.1,-0.66,0,0,0);
pagar(-0.5,-0.4,1.01,0.7,0.7,0.7,2,1,0.2);
pagar(0.84,-0.4,1.01,0.7,0.7,0.7,0.8,1,0.2);
pagar(0.01,-0.4,-0.95,0.7,0.7,0.7,-4,1,0.2);
pagar(-1,-0.4,0.05,0.7,0.7,0.7,0.2,1,-4);
pagar(1,-0.4,0.05,0.7,0.7,0.7,0.2,1,-4);
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
}
void reshape(int w, int h){
glViewport(0,0,(GLsizei)w,(GLsizei)h); //sudut pandang default
glMatrixMode(GL_PROJECTION); //mengaktifkan mode matrix proyeksi
glLoadIdentity(); //load matrix identitas
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}
void init(){
GLfloat LightPosition[] = {0.f, 0.f, 1.f, 1.f};
GLfloat LightAmbient[] = {0.0f, 4.0f, 0.0f, 1.0f};
GLfloat LightDiffuse[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat LightSpecular[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat Shine[] = {100};
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMaterialfv(GL_FRONT, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glMaterialfv(GL_FRONT, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SHININESS, Shine);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
return;
}
void myDisplay(void){
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// /*
glPushMatrix();
atap();
gedung();
glPopMatrix();
// */
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
switch(button)
{
case GLUT_LEFT_BUTTON: /* move the light */
action = 1;
// motion(x, y);
break;
case GLUT_MIDDLE_BUTTON:
action = 3;
// motion(x, y);
break;
case GLUT_RIGHT_BUTTON: /* move the polygon */
action = 2;
// motion(x, y);
break;
}
}
void myKeyboard(unsigned char key, int x, int y){
if(key == 'a') z-=1;
else if(key == 's') teta+=1;
}
void timer(int value){
glutPostRedisplay();
glutTimerFunc(millis,timer,0);
}
void main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50,100);
glutInitWindowSize(w,h);
glutCreateWindow("House 1");
glutTimerFunc(millis,timer,0);
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glutKeyboardFunc(myKeyboard);
glutMouseFunc(mouse);
glutReshapeFunc(reshape);
init();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
Selamat mencoba :D