Tuesday, 26 June 2012

3D vase

3 Dimension vase

vase
===============================

#include <GL/glut.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

const GLfloat light_ambient[]  = { 0, 0, 0, 1 };
const GLfloat light_diffuse[]  = { 1, 1, 1, 1 };
const GLfloat light_specular[] = { 1, 1, 1, 1 };
const GLfloat light_position[] = { 10, 10, 0, 1 };

const GLfloat mat_ambient[]    = { .7, .7, .7, 1 };
const GLfloat mat_diffuse[]    = { .8, .8, .8, 1 };
const GLfloat mat_specular[]   = { 1, 1, 1, 1 };
const GLfloat high_shininess[] = { 90 };

// angle of rotation for the camera direction
float angle=1.0;
// actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f;
// XZ position of the camera
float x=0,z=0,y=5;
GLUquadricObj* quad;
GLUquadric * quadz=gluNewQuadric();

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}

void vase()
{
     glPushMatrix();
     glRotatef(-90,1,0,0);
gluCylinder(quadz,0.1,0.2,0.1,50,50);
     glTranslatef(0,0,0.03);
gluCylinder(quadz,0.1,0,0.05,50,50);
glTranslatef(0,0,0.07);
gluCylinder(quadz,0.2,0.23,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.23,0.25,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.25,0.27,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.27,0.29,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.29,0.30,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.30,0.32,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.32,0.32,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.32,0.32,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.32,0.32,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.32,0.31,0.05,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.31,0.30,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.30,0.29,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.29,0.28,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.28,0.27,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.27,0.26,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.26,0.25,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.25,0.24,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.24,0.23,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.23,0.22,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.22,0.24,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.24,0.25,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.25,0.26,0.06,50,50);
glTranslatef(0,0,0.05);
gluCylinder(quadz,0.26,0.26,0.06,50,50);
     glPopMatrix();  
}

void changeSize(int w, int h)
{
if (h == 0) h = 1;

float ratio =  w * 1.0 / h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45,ratio,1,100);
glMatrixMode(GL_MODELVIEW);
}

void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( x, y, z, x+lx, 1.0f,  z+lz, 0.0f, 1.0f,  0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vase();
glFlush ();
glutSwapBuffers();
}


float mouse_klik,mouse_state,mouse_x,mouse_y,tamp_x,tamp_y,wheel=0.5,mx,my,rx,ry,l_x,l_y;

void klik(int button, int state,int x, int y)
{
     mouse_klik = button;
     mouse_state = state;
}

void pasif(int xx, int xy)
{
    if(mouse_klik==0&&mouse_state==0)
    {
        mouse_x=xx;
        mouse_y=xy;
        mx=(tamp_x-mouse_x)*0.005;
        my=(tamp_y-mouse_y)*0.005;
        angle+=mx;
        lx=sin(angle);
        lz=-cos(angle);
        y+=my;
        tamp_x=mouse_x;
        tamp_y=mouse_y;  
    }
    if(mouse_klik==2&&mouse_state==0)
    {
        mouse_x=x;
        mouse_y=y;
        mx=(tamp_x-mouse_x)*1.5;
        my=(tamp_y-mouse_y)*1.5;
        l_x=l_x+mx;
        l_y=l_y+my;
        tamp_x=mouse_x;
        tamp_y=mouse_y;  
    }
  
}

void pasif_v(int x, int y)
{
     tamp_x=x;
     tamp_y=y;    
}

void processNormalKeys(unsigned char key, int px, int py)
{
switch (key) {
case 'a' :
z-=lx*0.1;
x+=lz*0.1;
            break;
case 'd' :
z+=lx*0.1;
x-=lz*0.1;
            break;
case 'w' :
x += lx * 0.1;
z += lz * 0.1;
break;
case 's' :
x -= lx * 0.1;
z -= lz * 0.1;
break;
}
if (key == 27)
exit(0);
}

int main(int argc, char **argv) {

// init GLUT and create window

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(1024,720);
glutCreateWindow("7609040003 Muhammad Ridwan");
// register callbacks
glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    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, high_shininess);
  
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
init();
glutMouseFunc(klik);
    glutMotionFunc(pasif);
    glutPassiveMotionFunc(pasif_v);
glutKeyboardFunc(processNormalKeys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();

return 1;
}


thanks a lot... ^^8

1 comment:

  1. keep up the good work, you're excelent helped me much :)

    ReplyDelete