ini nih contoh Source code buat bikin Danbo dan interaksinya di Glut. Coba dan tinggalkan comment ya..
#include
#include
int w=480, h=480, z=-20;
int x1=0, y1=0, sudut=0, z1=0, skalaX=0, skalaY=0;
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
//glClearColor(1,1,1,1);
glLoadIdentity();
//camera
awal
glTranslatef(0,0,z);
glRotatef(sudut,x1,y1,z1);
//kaki
kanan
glPushMatrix();
glRotatef(0,1,0,-1);
glTranslatef(-0.9,-4,0);
glColor3f(0.8,0.55,0.3);
glScalef(1,2.5,1);
glutSolidCube(1.3);
glPopMatrix();
//kaki
kiri
glPushMatrix();
glRotatef(0,1,0,1);
glTranslatef(0.9,-4,0);
glColor3f(0.8,0.55,0.3);
glScalef(1,2.5,1);
glutSolidCube(1.3);
glPopMatrix();
//badan
glPushMatrix();
glTranslatef(0,0,0);
glColor3f(0.8,0.6,0.3);
glScalef(2,3,2);
glutSolidCube(2);
glPopMatrix();
//tangan
kanan
glPushMatrix();
glRotatef(110,1,0,0);
glTranslatef(-2.7,2.3,0);
glColor3f(0.8,0.55,0.3);
glScalef(1,3,1);
glutSolidCube(1.3);
glPopMatrix();
//Sendi
Kanan
glPushMatrix();
glTranslatef(2.6,0,0);
glColor3f(0.8,0.55,0.3);
glScalef(1,1,1);
glutSolidSphere(1,100,100);
glPopMatrix();
//tangan
kiri
glPushMatrix();
glRotatef(-120,1,0,0);
glTranslatef(2.7,2.3,0);
glColor3f(0.8,0.55,0.3);
glScalef(1,3,1);
glutSolidCube(1.3);
glPopMatrix();
//sendi
kiri
glPushMatrix();
glTranslatef(-2.6,0,0);
glColor3f(0.8,0.55,0.3);
glScalef(1,1,1);
glutSolidSphere(1,100,100);
glPopMatrix();
//Kepala
glPushMatrix();
glTranslatef(0,3.5,0);
glColor3f(0.8,0.6,0.3);
glScalef(1.5,1,1);
glutSolidCube(5);
glPopMatrix();
//mata
glPushMatrix();
glTranslatef(-1.5,3.5,0.55);
glColor3f(0,0,0);
glutSolidSphere(2,100,100);
glPopMatrix();
//mata
glPushMatrix();
glTranslatef(1.5,3.5,0.55);
glColor3f(0,0,0);
glutSolidSphere(2,100,100);
glPopMatrix();
//mulut
glPushMatrix();
glRotatef(-65,1,0,0);
glTranslatef(0,-1.1,2.8);
glScalef(0.3,0.3,0.3);
glColor3f(0,0,0);
glutSolidCone(3,3,3,3);
glPopMatrix();
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){
if
(key =='b') z+=5;
else
if (key == 'k')
z-=5;
else
if (key == 'x')
{
x1=1;
y1=0;
z1=0;
sudut+=10;
}
else
if (key == 'y')
{
y1=1;
x1=0;
z1=0;
sudut+=-10;
}
else
if (key == 'z')
{
y1=0;
x1=0;
z1=1;
sudut+=-10;
}
}
void mySpecialKeyboard(int key, int x, int y){
switch(key){
case
GLUT_KEY_UP:
z+=5;
break;
case
GLUT_KEY_DOWN:
z-=5;
break;
}
}
void init(){
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLdouble)
w/(GLdouble) h, 1.0,300.0);
glMatrixMode(GL_MODELVIEW);
}
void timer(int value){
glutPostRedisplay();
glutTimerFunc(50,timer,0);
}
void main (int argc, char
**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |
GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(w,h);
glutCreateWindow("Danbo");
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glutDisplayFunc(renderScene);
glutReshapeFunc(resize);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutTimerFunc(1,timer,0);
init();
glutMainLoop();
}
Hasilnya :
Selamat Mencoba...
0 komentar:
Posting Komentar