Cara Objek 3D dengan OpenGL
1. Script untuk kerangka 3D
2. Contoh Penggunaan Script
#include "stdlib.h"
#include "glut.h"
int w=400, h=400, z=0;
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,1);
glLoadIdentity();
glTranslatef(0,0,-13);
glColor3f(0,1,0);
glutWireSphere(2,20,10);
glLoadIdentity();
glTranslatef(2,2,-13);
glColor3f(0,0,1);
glutWireSphere(4,20,10);
glLoadIdentity();
glTranslatef(-2,-2,-13);
glColor3f(1,0,1);
glutWireSphere(4,20,10);
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 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 2");
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glutDisplayFunc(renderScene);
glutReshapeFunc(resize);
glutTimerFunc(1,timer,0);
init();
glutMainLoop(); }
3. Output yang Dihasilkan
***Selamat Mencoba***