#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
int c,d,xx,yy;
float w=480,h=480;
float sudut=0,sx=0,sy=0,sz=0,dalam=10,luar=25,sides=25,rings=25;
float ax=45,ay=45,az=45;
int x =0;
int ujung2;
int ujung,r=0;
int i;
void setMaterial()
{
//set properties of surfaces material
GLfloat mat_ambient[] = {1.0f,1.0f,1.0f,1.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi warna tertentu
GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}
void setMaterial2()
{
//set properties of surfaces material
GLfloat mat_ambient[] = {1.0f,0.7f,0.7f,1.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi warna tertentu
GLfloat mat_diffuse[] = {1.6f,0.6f,0.6f,1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}
void materiallensa()
{
//set properties of surfaces material
GLfloat mat_ambient[] = {0.0f,0.0f,1.0f,1.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi warna tertentu
GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[] = {50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}
void setLighting()
{
//set light sources
GLfloat lightIntensity[] = {0.7f,0.7f,0.7f,1.0f};//mensetting pencahayaan
GLfloat light_position[] = {2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void torus()
{
glColor3f(0,0,1);
glutSolidTorus(dalam,luar,sides,rings);// fungsi disamping adalah bentuk pendeklarasian fungsi “Torus”
}
void torus2()
{
glColor3f(0,0,1);
glutSolidTorus(dalam,13,sides,rings);// fungsi disamping adalah bentuk pendeklarasian fungsi “Torus”
}
void sphere()
{
// glColor3d(1,1,1);
glutSolidSphere(20, 25, 25);
}
void teapot()
{
glColor3d(1,0,0);
glutSolidTeapot(5);
}
void kerucut()
{
glColor3d(1,0,0);
glutSolidCone(dalam,luar,20,20);
}
void tabung()
{
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1,1,1);
glBegin(GL_POLYGON);
GLUquadricObj *obj = gluNewQuadric();
gluCylinder(obj, 20, 20, 20, 50, 50);
glEnd();
}
void tbl()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
GLUquadricObj *obj = gluNewQuadric();
gluCylinder(obj, 3, 3, 5, 50, 50);
glEnd();
}
void tblaraha()
{
glRotated(90,0,90,0);
glTranslated(-33,-5,-5);
glScaled(3,1.5,0.7);
glutSolidCube(10);
}
void tblarahb()
{
glRotated(90,0,90,0);
glTranslated(0,-3,0);
glScaled(4,0.5,0.2);
glutSolidCube(10);
}
void cube()
{
//menggambar kubus dan transformasi tarnslasi ke titik 0.5 0.5 0.5 dan skala 1 1 1
//glPushMatrix();
glTranslated(0.5,0.5,-15);//cube
glScaled(3.0,2.0,0.7);
glutSolidCube(30.0);
//glPopMatrix();
}
void layar()
{
//menggambar kubus dan transformasi tarnslasi ke titik 0.5 0.5 0.5 dan skala 1 1 1
//glPushMatrix();
glTranslated(0.5,0.5,-15);
glScaled(1.5,1.5,0.7);
glutSolidCube(10);
//glPopMatrix();
}
void display(){
setMaterial();
materiallensa();
setLighting();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-250);
glRotatef(sudut,sx,sy,sz);
glPushMatrix();
setMaterial();
glRotatef(sudut,ax,ay,az);
glTranslatef(0,0,-10);
tabung();
glTranslatef(-20,35,-5);
glRotated(90,90,0,0);
tbl();
tblaraha();
//glRotated(90,180,0,0);
glPushMatrix();
setMaterial();
tblarahb();
glPopMatrix();
glPopMatrix();
glPushMatrix();
setMaterial2();
glRotatef(sudut,ax,ay,az);
glTranslatef(c,d,0);
cube();
materiallensa();
glTranslatef(5,0,0);
layar();
//cube();
glPopMatrix();
glPushMatrix();
materiallensa();
glRotatef(sudut,ax,ay,az);
glTranslatef(0,0,0);
sphere();
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 init(){
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.,(GLdouble)w/(GLdouble)h,1.,300.);
glMatrixMode(GL_MODELVIEW);
}
void myKeyboard(unsigned char key, int x, int y){
if(key=='x'){
sx=1;
sy=0;
sz=0;
sudut+=10;
}
else if(key=='y'){
sx=0;
sy=1;
sz=0;
sudut+=10;
}
else if(key=='z'){
sx=0;
sy=0;
sz=1;
sudut+=10;
}
else if(key=='a'){
xx+=-4; yy+=0;
}
else if(key=='d'){
xx+=4; yy+=0;
}
else if(key=='w'){
xx+=0; yy+=4;
}
else if(key=='s'){
xx+=0; yy+=-4;
}
}
void mySpecialKeyboard(int key, int x, int y){
switch(key){
case GLUT_KEY_LEFT : c+=-4; d+=0; break;
case GLUT_KEY_RIGHT : c+=4; d+=0; break;
case GLUT_KEY_UP : c+=0; d+=4; break;
case GLUT_KEY_DOWN : c+=0; d+=-4; break;
}}
void update(int value){
glutPostRedisplay();
glutTimerFunc(50,update,0);
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(w,h);
glutCreateWindow("Objek 3D");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutReshapeFunc(resize);
init();
glutTimerFunc(50,update,0);
glutSpecialFunc(mySpecialKeyboard);
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return 0;
}