Cara membuat boneka salju 3D di OpenGL
Berikut merupakan script lengkap membuat boneka salju 3D :
- #include <stdlib.h>
- #include <glut.h>
- int w=480, h=480, z=0;
- int x1=0, y1=0, sudut=0, z1=0;
- void bawah ()
- {
- glColor3ub(255, 255, 255);
- glutWireSphere(5,100,100);
- }
- void tengah()
- {
- glColor3ub(255, 255, 255);
- glutWireSphere(4,100,100);
- }
- void atas()
- {
- glColor3ub(255, 255, 255);
- glutWireSphere(3,100,100);
- }
- void matakanan()
- {
- glColor3ub(0, 0, 0);
- glutWireSphere(0.5,100,100);
- }
- void matakiri()
- {
- glColor3ub(0, 0, 0);
- glutWireSphere(0.5,100,100);
- }
- void topi()
- {
- glColor3ub(0, 55, 252);
- glutWireCone(3,5,100,100);
- }
- void renderScene(void){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glPushMatrix();
- glTranslatef(0,-10,z);
- glRotatef(sudut,x1,y1,z1);
- bawah();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,-5,z);
- glRotatef(sudut,x1,y1,z1);
- tengah();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,0,z);
- glRotatef(sudut,x1,y1,z1);
- atas();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(1.5,0.5,z);
- glRotatef(sudut,x1,y1,z1);
- matakanan();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(-1.5,0.5,z);
- glRotatef(sudut,x1,y1,z1);
- matakiri();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,2,z);
- glRotatef(280,100,y1,z1);
- topi();
- 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;
- }
- 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("Manusia Salju");
- gluOrtho2D(-w/2,w/2,-h/2,h/2);
- glutDisplayFunc(renderScene);
- glutReshapeFunc(resize);
- glutKeyboardFunc(myKeyboard);
- glutTimerFunc(1,timer,0);
- init();
- glutMainLoop();
- }
Komentar
Posting Komentar