Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WINDOWS
- #ifdef WINDOWS
- #include <Windows.h>
- #include <gl/GL.h>
- #include <gl/GLU.h>
- #include <gl/glut.h>
- #include <iostream>
- #endif
- #ifdef LINUX
- #include <GL/glut.h>
- #endif
- #ifdef MAC
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #include <GLUT/glut.h>
- #endif
- using namespace std;
- bool done = false;
- float wRot;
- float pRot;
- float sRot;
- float cRot, cRot2;
- float wTran;
- float pTran;
- float p2Tran;
- float sTran;
- float cTran;
- void drawCrank()
- {
- // back horizontal
- glPushMatrix();
- glRotated(90,0,1,0);
- glScaled(0.08, 0.02, 0.02);
- glTranslated(.5 , 0, 0 + cTran);
- //glRotated(cRot,1,0,0);
- glutSolidCube(1.0);
- glPopMatrix();
- // back vertical
- glPushMatrix();
- glRotated(90,0,1,0);
- glScaled(0.02, 0.16, 0.02);
- glTranslated(3.5 , .5, 0+ cTran);
- //glRotated(cRot,1,0,0);
- glutSolidCube(1.0);
- glPopMatrix();
- //front horizontal
- glPushMatrix();
- glRotated(90, 0, 1, 0);
- glScaled(0.08, 0.02, 0.02);
- glTranslated(-.5 , 0, 0+ cTran);
- //glRotated(cRot,0,0,0);
- glutSolidCube(1.0);
- glPopMatrix();
- // front vertical
- glPushMatrix();
- glRotated(90,0,1,0);
- //glRotated(cRot2,1,0,0);
- glScaled(0.02, 0.16, 0.02);
- glTranslated(-3.5 , -.5, 0 + cTran);
- //glRotated(cRot,1,0,0);
- glutSolidCube(1.0);
- glPopMatrix();
- }
- void drawOneSpoke()
- {
- glPushMatrix();
- glRotated(sRot, 0, 0, 1);
- glScaled(.4,0.005,0.005);
- glutSolidCube(1.0);
- glPopMatrix();
- }
- void drawSpokes()
- {
- glPushMatrix();
- glTranslated(sTran, 0, 0);
- drawOneSpoke();
- glRotated(45,0,0,1);
- drawOneSpoke();
- glRotated(45,0,0,1);
- drawOneSpoke();
- glRotated(45,0,0,1);
- drawOneSpoke();
- glPopMatrix();
- }
- void drawWheel()
- {
- glPushMatrix();
- glScaled(0.1, 0.1, 0.1);
- glTranslated(wTran, 0, 0);
- glRotated(wRot, 0, 0, 1);
- glutSolidTorus(.3, 2.0, 25, 25);
- glPopMatrix();
- }
- void DisplaySolid()
- {
- if (done == false)
- {
- wTran = 0;
- sTran = 0;
- pTran = 0;
- p2Tran = -1.5;
- cTran = 0;
- cRot2 = 0;
- }
- // set properties of the surface material
- GLfloat mat_ambient[]={0.7f, 0.7f, 0.7f, 1.0f}; // gray
- 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);
- // set the light source properties
- GLfloat lightIntensity[] = {0.7f, 0.7f, 0.7f, 1.0f};
- GLfloat light_position[] = {2.0f, 6.0f, 3.0f, 0.0f};
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);
- // set the world window and the camera
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- double winHt = 1.0; // half-height of the window
- //glOrtho(-winHt*64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);
- glOrtho(-.45, winHt/2, -winHt/2, winHt/2, 0.1, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //gluLookAt(2.3, 1.3, 2, 0, 0.25, 0, 0.0, 1.0, 0.0);
- gluLookAt(.5, 0.7, 0.6, 0, 0, 0, 0.0, 1.0, 0.0);
- // start drawing
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // pedal
- glPushMatrix();
- glRotated(90, 0,1,0);
- glScaled(0.08, 0.04, 0.04);
- glTranslated(1.5, 4, 0 + pTran);
- glutSolidCube(1.0);
- glPopMatrix();
- glPushMatrix();
- glRotated(90, 0,1,0);
- glScaled(0.08, 0.04, 0.04);
- glTranslated(-1.5, -4, 0 + pTran);
- glutSolidCube(1.0);
- glPopMatrix();
- //wheel
- drawWheel();
- drawSpokes();
- drawCrank();
- glFlush();
- glutSwapBuffers();
- }
- void animate(int frame)
- {
- if (frame < 30)
- {
- wTran += .1;
- sTran += .01;
- pTran += .25;
- cTran += .5;
- sRot -= 5;
- wRot -= 5;
- cRot -= 1;
- cRot2 -= 10;
- }
- glutPostRedisplay();
- glutTimerFunc(33, animate, 1);
- }
- void Keyboard (unsigned char key, int x, int y)
- {
- // if ESC is pressed, exit
- if (key == 'a' || key == 'A')
- {
- done = true;
- cout << "a pressed" << endl;
- glutTimerFunc(33, animate, 1);
- }
- if (key == 27)
- exit(0);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(640, 480);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Shaded example - 3D scene");
- glutDisplayFunc(DisplaySolid);
- glutKeyboardFunc(Keyboard);
- glEnable(GL_LIGHTING); // enable the light source
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST); // for hidden surface removal
- glEnable(GL_NORMALIZE); // normalize vectors for proper shading
- glClearColor(0.1f, 0.1f, 0.1f, 0.0f); //background is light gray
- glViewport(0, 0, 640, 480);
- glutMainLoop();
- }
Add Comment
Please, Sign In to add comment