Membuat Boneka salju dengan GLUt menggunakan interaksi keyborad
program:
hasilnya:
program:
#include<stdlib.h>
#include<glut.h>
int w=400, h=400, z=0;
int x1=0, y1=0, sudut=0, z1=0;
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,z);
glRotatef(sudut,x1,y1,z1);
glPushMatrix();
glTranslatef(0,-5,0);
glColor3f(1,1,1);
glutWireSphere(4,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,0);
glColor3f(1,1,1);
glutWireSphere(3,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(0,4,0);
glColor3f(1,1,1);
glutWireSphere(2,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(0,4,0);
glColor3f(1,0,0);
glutWireCone(1,3,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(1,4.5,2);
glColor3f(0,0,0);
glutWireSphere(0.1,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(-1,4.5,2);
glColor3f(0,0,0);
glutWireSphere(0.1,200,50);
glPopMatrix();
glPushMatrix();
glTranslatef(0,5,0);
glRotatef(-90,1,0,0);
glColor3f(0,0,1);
glutWireCone(2,4,200,50);
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 =='a')
z+=5;
else if (key == 'd') 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("Praktikum
8");
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glutDisplayFunc(renderScene);
glutReshapeFunc(resize);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeyboard);
glutTimerFunc(50,timer,0);
init();
glutMainLoop();}=
|
hasilnya: