Pengen bikin miniatur rumah sederhana dengan glut? Oh? Pake animasi? Pake Interaksi keyboard juga? Lighting? Bisa.. bisa.. kaya gini bukan sih?
Silakan edit sendiri kalo bentuknya mirip-mirip kaya gitu. Warna, bentuk, terserah ya..
Itu pake lighting lho! Kalo ga percaya, coba deh jalanin source code di bawah ini!
#include <stdlib.h>
#include <stdio.h>
#include <gl/glut.h>
//inisialisasi variabel
int w=800, h=400;
float x=0, y=-1.2, z=-10, p=0, q=0;
float sudut=1, x1=0, y1=0, z1=0;
void balok(void)
{
glutSolidCube(5);
}
void pintujendela(float x, float y)
{
glBegin(GL_POLYGON);
glVertex2f(-x,-y);
glVertex2f(-x,y);
glVertex2f(x,y);
glVertex2f(x,-y);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(x,y,z);
glRotatef(sudut,x1,y1,z1);
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
//pintu dan jendela
glPushMatrix();
glColor3f(3,1,0);
glTranslatef(0,-0.8,3.26);
pintujendela(0.9,1.2);
glTranslatef(1.7,0.5,0);
pintujendela(0.5,0.5);
glTranslatef(0.85,0,0);
glRotatef(90, 0,1,0);
glTranslatef(1.8,0,0);
pintujendela(0.5,0.5);
glTranslatef(1.5,0,0);
pintujendela(0.5,0.5);
glTranslatef(1.5,0,0);
pintujendela(0.5,0.5);
glPopMatrix();
//dinding rumah
glPushMatrix();
glColor3f(0,1,3);
glScalef(1,0.8,1.3);
balok();
glPopMatrix();
//genting kanan
glPushMatrix();
glColor3f(3,0,0);
glTranslatef(1.8,2.85,0);
glRotatef(51,0,0,1);
glScalef(0.1,1,1.5);
balok();
glPopMatrix();
//genting kiri
glPushMatrix();
glTranslatef(-1.8,2.85,0);
glRotatef(-51,0,0,1);
glScalef(0.1,1,1.5);
balok();
glPopMatrix();
//pagar kiri
glPushMatrix();
glColor3f(0,3,0);
glScalef(1,0.5,1);
glTranslatef(0,-2,0);
glTranslatef(-3.5,-0.92,0);
glPushMatrix();
glScalef(0.07,0.4,1.6);
balok();
glPopMatrix();
//pagar kanan
glPushMatrix();
glTranslatef(7,0,0);
glScalef(0.07,0.4,1.6);
balok();
glPopMatrix();
//pagar belakang
glPushMatrix();
glTranslatef(3.5,0,-4.15);
glRotatef(90,0,1,0);
glScalef(0.07,0.4,1.47);
balok();
glPopMatrix();
//pagar depan
glTranslatef(1.25,0,4.15);
glRotatef(90,0,1,0);
glScalef(0.07,0.4,0.57);
balok();
glTranslatef(0,0,7.9);
balok();
glPopMatrix();
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glutSwapBuffers();
}
void resize(int w1, int h1)
{
glViewport(0,0,w1,h1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(float) w1/(float) h1, 1.0,300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myKeyboard(unsigned char key, int x, int y)
{
//deteksi tombol x untuk rotasi pada sumbu x
if (key == 'x')
{
x1=1;
y1=0;
z1=0;
sudut++;
}
//deteksi tombol y untuk rotasi pada sumbu y
else if (key == 'y')
{
y1=1;
x1=0;
z1=0;
sudut++;
}
//deteksi tombol z untuk rotasi pada sumbu z
else if (key == 'z')
{
y1=0;
x1=0;
z1=1;
sudut++;
}
}
void mySpecialKeyboard(int key, int x, int y)
{
switch(key)
{
//deteksi tombol tanda panah atas untuk memperbesar
case GLUT_KEY_UP:
z+=5;
break;
//deteksi tombol tanda panah bawah untuk memperkecil
case GLUT_KEY_DOWN:
z-=5;
break;
}
}
void mouse(int button, int state, int xmouse,int ymouse)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
p = (xmouse - (w/2)) + 0.0f; q = ((h/2) - ymouse) + 0.0f;
printf("x: %0.0f",p);
printf("y: %0.0f",q);
}
}
void init()
{
GLfloat LightPosition[] = {p, q, 50.0f, 0.0f};
GLfloat LightAmbient[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat LightDiffuse[] = {0.7f, 1.0f, 0.7f, 1.0f};
GLfloat LightSpecular[] = {1.6f, 1.0f, 0.5f, 0.0f};
GLfloat Shine[] = {50};
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);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
return;
}
//fungsi pembantu agar fungsi keyboard dapat selalu dideteksi
void update(int value)
{
glutPostRedisplay();
glutTimerFunc(50,update,0);
}
void main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(w,h);
glutCreateWindow("Madul 9 Lighting dan Material");
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutMouseFunc(mouse);
glutTimerFunc(1,update,0);
init();
glutMainLoop();
}
1 komentar:
ini glutnya g ada ya?
Posting Komentar