Tuesday, 26 June 2012

3D locomotive

3 Dimension of Locomotive with solid construction

locomotive
============================

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <math.h>

// angle of rotation for the camera direction
float angle=0.0;
// actual vector representing the camera's direction
float lx=0.0f,lz=-0.1f;
// XZ position of the camera
float x=0.0f,z=5.0f;

static int spin = 0;

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 drawhead()
{
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0.1,-1);
glutSolidCube(2);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0.3,-1);
glutSolidCube(2);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,1,0);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,1,-1);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,1.5,-1);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,1.5,-1.5);
glutSolidCube(1);
glPopMatrix();
}

void topi()
{
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,2,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,2,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,2,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,2,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,2,-1.8);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,2,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,2,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,2,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,2,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,2,-1.4);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,2,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,2,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,2,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,2,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,2,-1);
glutSolidCube(0.5);
glPopMatrix();
///////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,2,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,2,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,2,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,2,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,2,-0.6);
glutSolidCube(0.5);
glPopMatrix();
/////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,2,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,2,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,2,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,2,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,2,-0.2);
glutSolidCube(0.5);
glPopMatrix();
}

void drawbody()
{
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,0);
glutSolidSphere(1,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,0.5);
glutSolidSphere(1,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,1);
glutSolidSphere(1,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,1.5);
glutSolidSphere(1,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,2);
glutSolidSphere(1,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,2.5);
glutSolidSphere(1,20,5);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,3);
glutSolidSphere(1,20,5);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,3.5);
glutSolidSphere(1,20,5);
glPopMatrix();
}

void bawah()
{
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,-1,-1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,-1,-1.8);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,-1,-1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,-1,-1.4);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,-1,-1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,-1,-1);
glutSolidCube(0.5);
glPopMatrix();
///////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,-1,-0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,-1,-0.6);
glutSolidCube(0.5);
glPopMatrix();
/////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-1,-1,-0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(1,-1,-0.2);
glutSolidCube(0.5);
glPopMatrix();

////////////////////////////
////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,1);
glutSolidCube(0.5);
glPopMatrix();
///////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();
/////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();

////////////////////////////
////////////////////////////

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,2.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,2.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,2.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,0.2);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,2.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,2.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,2.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,0.6);
glutSolidCube(0.5);
glPopMatrix();
////////////////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,3);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,3);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,3);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,1);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,1);
glutSolidCube(0.5);
glPopMatrix();
///////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,3.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,3.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,3.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,1.4);
glutSolidCube(0.5);
glPopMatrix();
/////////////////////////
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0,-1,3.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(0.5,-1,3.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
glTranslatef(-0.5,-1,3.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(-1,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3f(1,1,0);
//glTranslatef(1,-1,1.8);
glutSolidCube(0.5);
glPopMatrix();
}

void cerobong()
{
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0.7,2);
glutSolidSphere(0.3,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0.9,2);
glutSolidSphere(0.3,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,1.1,2);
glutSolidSphere(0.3,20,5);
glPopMatrix();

///////////////////////////////////

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0.7,3);
glutSolidSphere(0.3,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0.9,3);
glutSolidSphere(0.3,20,5);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,1.1,3);
glutSolidSphere(0.3,20,5);
glPopMatrix();

//glRotatef(angle,x,y,z);
//glScalef(x,y,z);
}

void roda()
{
//////////kanan/////////
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0.8,-1,-0.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0.8,-1,-1.4);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0.8,-1,0.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0.8,-1,2.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0.8,-1,3.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();

/////////////////kiri///////

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-0.8,-1,-0.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();

glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-0.8,-1,-1.4);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-0.8,-1,0.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-0.8,-1,2.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
glPopMatrix();


glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-0.8,-1,3.5);
glRotatef(120,1,1,1);
glScalef(0.5,0.3,0.2);
glutSolidTorus(0.3,1,20,20);
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,1,z,x+lx,1,z+lz,0,1,0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawhead();
topi();
drawbody();
bawah();
cerobong();
roda();
glFlush ();
glutSwapBuffers();
}

void processSpecialKeys(int key, int xx, int yy)
{
float fraction = 0.1f;
switch (key)
{
case GLUT_KEY_LEFT :
angle -= 0.1;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_RIGHT :
angle += 0.1;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_UP :
x += lx * fraction;
z += lz * fraction;
break;
case GLUT_KEY_DOWN :
x -= lx * fraction;
z -= lz * fraction;
break;
}
}

void processNormalKeys(unsigned char key, int x, int y)
{
if (key == 27) exit(0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,50);
glutInitWindowSize(1024,720);
glutCreateWindow("7609040003 - Muhammad Ridwan");

glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
init();
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

return 1;
}

thanks..

No comments:

Post a Comment