Membuat Objek 3D di GLUT (OpenGL Utility Toolkit)
Selain bisa membuat objek primitif di GLUT (OpenGL Utility Toolkit) yang bersifat 2D, ada juga fungsi-fungsi yang digunakan untuk membuat objek dalam mode 3D (tiga dimensi). Fungsi-fungsi untuk membuat objek 3D di GLUT adalah :
- glutSolidSphere, glutWireSphere
- glutSolidCube, glutWireCube
- glutSolidCone, glutWireCone
- glutSolidTorus, glutWireTorus
- glutSolidDodecahedron, glutWireDodecahedron
- glutSolidOctahedron, glutWireOctahedron
- glutSolidTetrahedron, glutWireTetrahedron
- glutSolidIcosahedron, glutWireIcosahedron
- glutSolidTeapot, glutWireTeapot
#include <stdlib.h>
#include <glut/glut.h>
int w = 480, h = 480, z = 0;
int x1 = 0, y1 = 0, z1 = 0, sudut = 0, size = 1;
void timer(int value) {
glutPostRedisplay();
glutTimerFunc(50,timer,0);
}
void myKeyboard(unsigned char key, int x, int y) {
if(key == 'a') z+= 5; //mendekat
else if (key == 'd') z-= 5; //menjauh
else if (key == 'x') { //rotasi sumbu x
x1 = 1;
y1 = 0;
z1 = 0;
sudut += 5;
} else if (key == 'y') { //rotasi sumbu y x1 = 0; y1 = 1; z1 = 0; sudut += 5; } else if (key == 'z') { //rotasi sumbu z x1 = 0; y1 = 0; z1 = 1; sudut += 5; } }
void mySpecialKeyboard(int key, int x, int y) { if(key == GLUT_KEY_UP) size += 1; else if (key == GLUT_KEY_DOWN) size -= 1; }
void init() { glClearColor(0.0,0.0,0.0,1.0); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30., (GLdouble)w/(GLdouble)h, 1., 300.); glMatrixMode(GL_MODELVIEW); }
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 renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0, 0, z); glRotatef(sudut, x1, y1, z1); glColor3f(1, 1, 0); glutWireTeapot(size); glutSwapBuffers(); }
void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100, 100); glutInitWindowSize(w, h); glutCreateWindow("GRAFKOM 8"); gluOrtho2D(-w/2, w/2, -h/2, h/2); glutDisplayFunc(renderScene); glutTimerFunc(50, timer, 0); glutKeyboardFunc(myKeyboard); glutSpecialFunc(mySpecialKeyboard); glutReshapeFunc(resize); init(); glutMainLoop(); }
Hasil
Komentar
Posting Komentar