Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include <math.h>
- int window_x = 600;
- int window_y = 600;
- int mouse_mode = 0;
- #define NO_FORM 0
- #define ROTATE_FORM 1
- #define ZOOM_FORM 2
- int click_x, click_y;
- int release_x, release_y;
- float x_angle = 60.0;
- float y_angle = -10.0;
- float x_look_at;
- float y_look_at;
- float z_look_at;
- float rDist = 24.0;
- int ground_floor = 2;
- int column = 1;
- int clawPinch = 45;
- float topBend = 10;
- float midBend = 10;
- float bottomTurn = 10;
- int clawTwist = 0;
- int clawTwistDelta = 0;
- float topDelta = 0.4;
- float midDelta = 0.4;
- float bottomDelta = 0.4;
- float clawDelta = 1;
- bool clawExpand = true;
- bool bendForward = true;
- bool animate = false;
- float x_light_pos;
- float y_light_pos;
- float z_light_pos;
- float x_light_angle = 45;
- float y_light_angle = 45;
- bool rotateLight = false;
- float amb_light_val = .7;
- float dif_light_val = .9;
- // These three are solely for the creation of "block".
- GLfloat normal[6][3] = {
- {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
- {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0} };
- GLint sides[6][4] = {
- {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
- {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
- GLfloat vectors[8][3];
- void animate_claw ( void ) {
- //The claw only opens if we're moving down to pick up the object.
- if (clawPinch < 40 && topDelta > 0) {
- clawDelta = 1;
- }
- //The claw only closes if we're moving back to the original position.
- if (clawPinch > 60 && topDelta < 0) {
- clawDelta = -1;
- }
- //The claw does nothing after it has reached its max opening or closing.
- if((clawPinch > 60 && topDelta > 0) || (clawPinch < 40 && topDelta < 0)) {
- clawDelta = 0;
- }
- clawPinch += clawDelta;
- }
- void doNothing (int value) {
- }
- //Responsible for animating the entire object.
- void animate_whole( int value ) {
- //This portion is responsible for the bend in the upper portion of the object.
- //In other words, the first block.
- if(topBend >= 45) {
- topDelta = -.4;
- } else if(topBend <= 10) {
- topDelta = .4;
- }
- //This is responsible for the bend in the middle of the object, or the middle block.
- if(midBend >= 45) {
- midDelta = -.4;
- } else if(midBend <= 10) {
- midDelta = .4;
- }
- //This is responsible for the twist at the base of the object.
- if(bottomTurn >= 45) {
- bottomDelta = -.4;
- } else if(bottomTurn <= 10) {
- bottomDelta = .4;
- }
- topBend += topDelta;
- midBend += midDelta;
- bottomTurn += bottomDelta;
- //This animates the pinching motion of the claw.
- animate_claw();
- //This animates the twist of the claw, which twists twice, and ONLY when the claw is returning
- //to its normal position.
- if(clawTwist < 360 && topDelta < 0) {
- clawTwistDelta = 10;
- } else if (topDelta > 0) {
- clawTwist = 0;
- } else{
- clawTwistDelta = 0;
- }
- clawTwist += clawTwistDelta;
- glutPostRedisplay();
- //Animate only if the animation boolean is true.
- if(animate){
- glutTimerFunc(33, animate_whole, 0);
- }
- }
- //Unused
- void draw_cube(float r, float g, float b)
- {
- GLfloat mat_amb_diff[] = {r,g,b,1};
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
- glutSolidCube(2.0);
- }
- void draw_block(float r, float g, float b)
- {
- glDisable(GL_NORMALIZE);
- glDisable(GL_COLOR_MATERIAL);
- //Create an orangish shade for the material
- GLfloat mat_amb_diff[] = {r,g,b, 1};
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
- //Create a 6-sized cube, with defined normals.
- for (int i = 0; i < 6; i++) {
- glBegin(GL_QUADS);
- //This defines the vector for the normal
- glNormal3fv(&normal[i][0]);
- //These define the square that make up one side.
- glVertex3fv(&vectors[sides[i][0]][0]);
- glVertex3fv(&vectors[sides[i][1]][0]);
- glVertex3fv(&vectors[sides[i][2]][0]);
- glVertex3fv(&vectors[sides[i][3]][0]);
- glEnd();
- }
- //Re-enable normalize to be used by the floor.
- glEnable(GL_NORMALIZE);
- }
- //Not used.
- void draw_teapot(float r, float g, float b)
- {
- glDisable(GL_COLOR_MATERIAL);
- GLfloat mat_amb_diff[] = {.5, 0, .5, 1};
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
- glutSolidTeapot(1.0);
- }
- //Draws in the floor.
- void draw_floor(float r, float g, float b)
- {
- //glEnable(GL_NORMALIZE);
- //Needs to be a proper size.
- glScalef(100, .1, 100);
- glNormal3f(100,.1,100);
- GLfloat mat_amb_diff[] = {.1, .7, 0, 1};
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
- //Built from a cube.
- glutSolidCube(1.0);
- //glDisable(GL_NORMALIZE);
- }
- //This just rotates the light around the y axis.
- void animate_light( int value ) {
- x_light_angle += .1;
- glutPostRedisplay();
- if(rotateLight) {
- glutTimerFunc(33, animate_light ,0);
- }
- }
- void init_lights( void ) {
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- //glEnable(GL_LIGHT1);
- //Define the RGB and Alpha values for the ambient light.
- GLfloat light_ambient[] = {amb_light_val, amb_light_val, amb_light_val, 1.0};
- //Define the RGB and Alpha values for the diffuse light.
- GLfloat light_diffuse[] = {dif_light_val, dif_light_val, dif_light_val, 1.0};
- //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- //glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- //glShadeModel(GL_SMOOTH);
- }
- void reinit_lights(void) {
- GLfloat light_ambient[] = {amb_light_val, amb_light_val, amb_light_val, 1.0};
- //Define the RGB and Alpha values for the diffuse light.
- GLfloat light_diffuse[] = {dif_light_val, dif_light_val, dif_light_val, 1.0};
- //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glutPostRedisplay();
- }
- void display(void)
- {
- //Define the background color as gray.
- glClearColor(.5,.5,.5,1);
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //Set the perspective.
- gluPerspective(60, 1, .1, 200);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //Calculate the rotation from the center using the angles relative to
- // x-z (called 'x_angle') and y-x (called 'y_angle').
- x_look_at = rDist*(cos(x_angle)*cos(y_angle));
- y_look_at = rDist*sin(y_angle);
- z_look_at = rDist*(sin(x_angle)*cos(y_angle));
- //Use these new values to determine the vector for what you are looking at.
- // Obviously, the center is (0,0,0).
- gluLookAt(x_look_at,y_look_at,z_look_at,0,0,0,0,3,0);
- x_light_pos = 4*(cos(x_light_angle) * cos(y_light_angle));
- y_light_pos = 4*(sin(y_light_angle));
- z_light_pos = 4*(sin(x_light_angle) * cos(y_light_angle));
- GLfloat light_position[] = {x_light_pos, y_light_pos, z_light_pos , 0};
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glPushMatrix();
- //Create the floor.
- glCallList(ground_floor);
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,1,0);
- glDisable(GL_NORMALIZE);
- //Base block.
- //Set the rotation around y.
- glRotatef(bottomTurn, 0, 1, 0);
- glPushMatrix();
- //Thin it down a bit from the cube shape.
- glScalef(.5f,1,.5f);
- glNormal3f(.5f,0,.5f);
- glCallList(column);
- glScalef(2,1,2);
- //2nd block.
- glPushMatrix();
- glTranslatef(0,1,0);
- glRotatef(midBend,1,0,0);
- glTranslatef(0,1,0);
- //This this block even further to make it look nicer.
- glScalef(.25f,1,.25f);
- glNormal3f(.25f,0,.25f);
- glCallList(column);
- glScalef(4,1,4);
- //3rd block.
- glPushMatrix();
- glTranslatef(0,1,0);
- glRotatef(-(topBend*2),1,0,1);
- glTranslatef(0,1,0);
- glScalef(.25f,1,.25f);
- glNormal3f(.25f,0,.25f);
- glCallList(column);
- glScalef(4,1,4);
- //Deals with rotation of the claw.
- glRotatef(clawTwist, 0, 1, 0);
- //Base of the claw block. This is just the block at the very base of the claw.
- glPushMatrix();
- glTranslatef(0,1,0);
- glScalef(.5f,.5f,1);
- glNormal3f(.5f,0,.5f);
- glCallList(column);
- glPopMatrix();
- //left Claw
- glPushMatrix();
- //Rotates the claw to pinch in and out.
- glRotatef(clawPinch, 1, 0 , 0);
- glTranslatef(0,2,-.5f);
- glScalef(.5f,1,.5f);
- glNormal3f(.5f,0,.5f);
- glCallList(column);
- glScalef(2,1,2);
- glPushMatrix();
- glTranslatef(0,1,-1);
- glRotatef(-90, 1, 0 , 0);
- glScalef(.5f,1,.2f);
- glNormal3f(.5f,0,.2f);
- glCallList(column);
- glScalef(2,1,2);
- glPopMatrix();
- glPopMatrix();
- //Right Claw
- glPushMatrix();
- glRotatef((-1 * clawPinch), 1, 0 , 0);
- glTranslatef(0,2,.5f);
- glScalef(.5f,1,.5f);
- glNormal3f(.5f,0,.5f);
- glCallList(column);
- glScalef(2,1,2);
- glPushMatrix();
- glTranslatef(0,1,1);
- glRotatef(90, 1, 0 , 0);
- glNormal3f(.5f,0,.2f);
- glScalef(.5f,1,.2f);
- glCallList(column);
- glScalef(2,1,2);
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- glFlush();
- }
- void keyPress (unsigned char key, int x, int y)
- {
- switch(key) {
- // Pressing q - Quits the program
- case 'q': exit (1); break;
- //Pressing s - sets the program to run frame by frame, then runs it.
- case 's' : animate = false;
- glutTimerFunc(33, animate_whole, 0);
- break;
- //Pressing c - sets the program to run the animation continuously, then runs it.
- case 'c' : animate = true;
- glutTimerFunc(33, animate_whole, 0);
- break;
- //Pressing r - Resets all values to their original position.
- case 'r':
- {
- window_x = 600;
- window_y = 600;
- x_angle = 60.0;
- y_angle = -10.0;
- x_look_at = 0.0;
- y_look_at = 4.0;
- z_look_at = 20.0;
- rDist = 24.0;
- clawPinch = 45;
- topBend = 10;
- midBend = 10;
- bottomTurn = 10;
- clawTwist = 0;
- clawTwistDelta = 0;
- topDelta = 0.4;
- midDelta = 0.4;
- bottomDelta = 0.4;
- clawDelta = 1;
- clawExpand = true;
- bendForward = true;
- animate = false;
- x_light_angle = 45;
- y_light_angle = 45;
- rotateLight = false;
- amb_light_val = .7;
- dif_light_val = .9;
- reinit_lights();
- }
- break;
- case 'z':
- rotateLight = !rotateLight;
- if(rotateLight) {
- glutTimerFunc(33, animate_light, 0);
- } else {
- glutTimerFunc(33, doNothing, 0);
- }
- break;
- //Toggles between 10 different ambient light intensities
- case 'n':
- if(amb_light_val < 1.0) {
- amb_light_val += .1;
- } else {
- amb_light_val = .1;
- }
- reinit_lights();
- break;
- //toggles between 10 different diffuse light intensities
- case 'm':
- if(dif_light_val < 1.0) {
- dif_light_val += .1;
- } else {
- dif_light_val = .1;
- }
- reinit_lights();
- break;
- }
- glutPostRedisplay();
- }
- void mouse_click (int button, int state, int x, int y) {
- //If you're clicking
- if (state == GLUT_DOWN) {
- //Save the coordinates.
- click_x = x;
- click_y = y;
- //If you're holding down a left click:
- if (button == GLUT_LEFT_BUTTON) {
- //You're trying to rotate the camera around (0,0,0).
- mouse_mode = ROTATE_FORM;
- }
- //If the button you're holding down is the middle one:
- else if (button == GLUT_MIDDLE_BUTTON) {
- //You're trying to zoom in or out.
- mouse_mode = ZOOM_FORM;
- }
- } else if (state == GLUT_UP) {
- mouse_mode = NO_FORM;
- }
- }
- void mouse_move (int x, int y)
- {
- if (mouse_mode == ROTATE_FORM) {
- x_angle += (x - click_x)/60.0;
- if (x_angle > 180){
- x_angle -= 360;
- }
- else if (x_angle < -180) {
- x_angle += 360;
- }
- click_x = x;
- y_angle += (y - click_y)/60.0;
- if (y_angle > 180) {
- y_angle -= 360;
- }
- else if (y_angle <-180) {
- y_angle += 360;
- }
- click_y = y;
- } else if (mouse_mode == ZOOM_FORM){
- float old_size = rDist;
- rDist = rDist + ((y - click_y)/20.0);
- if (rDist < 0) {
- rDist = old_size;
- }
- click_y = y;
- }
- glutPostRedisplay();
- }
- //Up or down zooming
- void keyUpOrDown (int key, int x, int y) {
- //If you push down, increase the r-size by 4.
- if (GLUT_KEY_DOWN == key) {
- rDist += 4;
- //Otherwise, if you push up, decrease the r-size by 4.
- } else if(GLUT_KEY_UP == key) {
- if(rDist >= 4) {
- rDist -= 4;
- } else {
- rDist = 0.0;
- }
- }
- glutPostRedisplay();
- }
- //In case of window resizing:
- void handleResize(int w, int h) {
- if (w > h) {
- w = h;
- } else if (h > w) {
- h = w;
- }
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glutPostRedisplay();
- }
- void init(void) {
- vectors[0][0] = vectors[1][0] = vectors[2][0] = vectors[3][0] = -1;
- vectors[4][0] = vectors[5][0] = vectors[6][0] = vectors[7][0] = 1;
- vectors[0][1] = vectors[1][1] = vectors[4][1] = vectors[5][1] = -1;
- vectors[2][1] = vectors[3][1] = vectors[6][1] = vectors[7][1] = 1;
- vectors[0][2] = vectors[3][2] = vectors[4][2] = vectors[7][2] = 1;
- vectors[1][2] = vectors[2][2] = vectors[5][2] = vectors[6][2] = -1;
- glEnable(GL_NORMALIZE);
- glEnable(GL_DEPTH_TEST);
- glDisable(GL_COLOR_MATERIAL);
- //Create the Lists (one for the floor, one for the block).
- GLuint index = glGenLists(2);
- glNewList(1, GL_COMPILE);
- draw_block(.6, .2, 0);
- glEndList();
- glNewList(2, GL_COMPILE);
- draw_floor(.5, 0, .5);
- glEndList();
- init_lights();
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- int mode = GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH;
- glutInitDisplayMode(mode);
- glutInitWindowSize(600,600);
- glutCreateWindow("Lab 3 - Karl Racke");
- init();
- glutDisplayFunc(display);
- glutMouseFunc(mouse_click);
- glutMotionFunc(mouse_move);
- glutSpecialFunc(keyUpOrDown);
- glutReshapeFunc(handleResize);
- glutKeyboardFunc(keyPress);
- glClearColor(1.0,0.5,0.0,0.5);
- glColor3f(0.0,1.0,0.0);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment