Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 54 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1.  
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <GL/glut.h>
  6. #include <GL/gl.h>
  7. #include <math.h>
  8.  
  9.  
  10. int window_x = 600;
  11. int window_y = 600;
  12.  
  13. int mouse_mode = 0;
  14.  
  15. #define NO_FORM  0
  16. #define ROTATE_FORM  1
  17. #define ZOOM_FORM 2
  18.  
  19. int click_x, click_y;
  20. int release_x, release_y;
  21. float x_angle = 60.0;
  22. float y_angle = -10.0;
  23.  
  24. float x_look_at;
  25. float y_look_at;
  26. float z_look_at;
  27.  
  28. float rDist = 24.0;
  29.  
  30. int ground_floor = 2;
  31. int column = 1;
  32.  
  33.  
  34.  int clawPinch = 45;
  35.  float topBend = 10;
  36.  float midBend = 10;
  37.  float bottomTurn = 10;
  38.  
  39.  int clawTwist = 0;
  40.  int clawTwistDelta = 0;
  41.  float topDelta = 0.4;
  42.  float midDelta = 0.4;
  43.  float bottomDelta = 0.4;
  44.  float clawDelta = 1;
  45.  
  46.  bool clawExpand = true;
  47.  bool bendForward = true;
  48.  
  49.  bool animate = false;
  50.  
  51.  float x_light_pos;
  52.  float y_light_pos;
  53.  float z_light_pos;
  54.  
  55.  float x_light_angle = 45;
  56.  float y_light_angle = 45;
  57.  
  58.  bool rotateLight = false;
  59.  
  60.  float amb_light_val = .7;
  61.  float dif_light_val = .9;
  62.  
  63.  
  64.  
  65.  // These three are solely for the creation of "block".
  66.  
  67. GLfloat normal[6][3] = {  
  68.   {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  69.   {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0} };
  70.  
  71. GLint sides[6][4] = {  
  72.   {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  73.   {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
  74.  
  75. GLfloat vectors[8][3];  
  76.  
  77.  
  78.  
  79. void animate_claw ( void ) {
  80.  
  81.     //The claw only opens if we're moving down to pick up the object.
  82.     if (clawPinch < 40 && topDelta > 0) {
  83.             clawDelta = 1;
  84.    
  85.     }
  86.  
  87.     //The claw only closes if we're moving back to the original position.
  88.     if (clawPinch > 60 && topDelta < 0) {
  89.         clawDelta = -1;
  90.     }
  91.  
  92.    
  93.     //The claw does nothing after it has reached its max opening or closing.
  94.     if((clawPinch > 60 && topDelta > 0) || (clawPinch < 40 && topDelta < 0)) {
  95.         clawDelta = 0;
  96.     }
  97.    
  98.    
  99.     clawPinch += clawDelta;
  100.  
  101.  
  102. }
  103.  
  104. void doNothing (int value) {
  105.  
  106. }
  107.  
  108. //Responsible for animating the entire object.
  109. void animate_whole( int value ) {
  110.     //This portion is responsible for the bend in the upper portion of the object.
  111.     //In other words, the first block.
  112.     if(topBend >= 45) {
  113.         topDelta = -.4;
  114.     } else if(topBend <= 10) {
  115.         topDelta = .4;
  116.     }
  117.  
  118.     //This is responsible for the bend in the middle of the object, or the middle block.
  119.     if(midBend >= 45) {
  120.         midDelta = -.4;
  121.     } else if(midBend <= 10) {
  122.         midDelta = .4;
  123.     }
  124.  
  125.     //This is responsible for the twist at the base of the object.
  126.     if(bottomTurn >= 45) {
  127.         bottomDelta = -.4;
  128.     } else if(bottomTurn <= 10) {
  129.         bottomDelta = .4;
  130.     }
  131.  
  132.     topBend += topDelta;
  133.     midBend += midDelta;
  134.     bottomTurn += bottomDelta;
  135.  
  136.     //This animates the pinching motion of the claw.
  137.     animate_claw();
  138.  
  139.  
  140.     //This animates the twist of the claw, which twists twice, and ONLY when the claw is returning
  141.     //to its normal position.
  142.     if(clawTwist < 360 && topDelta < 0) {
  143.         clawTwistDelta = 10;
  144.     } else if (topDelta > 0) {
  145.         clawTwist = 0;
  146.     } else{
  147.         clawTwistDelta = 0;
  148.     }
  149.  
  150.    
  151.     clawTwist += clawTwistDelta;
  152.  
  153.     glutPostRedisplay();
  154.  
  155.     //Animate only if the animation boolean is true.
  156.     if(animate){
  157.         glutTimerFunc(33, animate_whole, 0);
  158.     }
  159.  
  160. }
  161.  
  162. //Unused
  163. void draw_cube(float r, float g, float b)
  164. {
  165.     GLfloat mat_amb_diff[] = {r,g,b,1};  
  166.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);  
  167.     glutSolidCube(2.0);  
  168. }
  169.  
  170. void draw_block(float r, float g, float b)
  171. {
  172.  
  173.     glDisable(GL_NORMALIZE);
  174.     glDisable(GL_COLOR_MATERIAL);
  175.  
  176.     //Create an orangish shade for the material
  177.     GLfloat mat_amb_diff[] = {r,g,b, 1};  
  178.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
  179.  
  180.     //Create a 6-sized cube, with defined normals.
  181.     for (int i = 0; i < 6; i++) {
  182.     glBegin(GL_QUADS);
  183.         //This defines the vector for the normal
  184.         glNormal3fv(&normal[i][0]);
  185.         //These define the square that make up one side.
  186.         glVertex3fv(&vectors[sides[i][0]][0]);
  187.         glVertex3fv(&vectors[sides[i][1]][0]);
  188.         glVertex3fv(&vectors[sides[i][2]][0]);
  189.         glVertex3fv(&vectors[sides[i][3]][0]);
  190.     glEnd();
  191.   }
  192.  
  193.     //Re-enable normalize to be used by the floor.
  194.   glEnable(GL_NORMALIZE);
  195.  
  196. }
  197.  
  198. //Not used.
  199. void draw_teapot(float r, float g, float b)
  200. {
  201.     glDisable(GL_COLOR_MATERIAL);
  202.  
  203.     GLfloat mat_amb_diff[] = {.5, 0, .5, 1};  
  204.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
  205.    
  206.     glutSolidTeapot(1.0);  
  207. }
  208.  
  209. //Draws in the floor.
  210. void draw_floor(float r, float g, float b)
  211. {
  212.     //glEnable(GL_NORMALIZE);
  213.     //Needs to be a proper size.
  214.     glScalef(100, .1, 100);
  215.     glNormal3f(100,.1,100);
  216.     GLfloat mat_amb_diff[] = {.1, .7, 0, 1};  
  217.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
  218.     //Built from a cube.
  219.     glutSolidCube(1.0);
  220.     //glDisable(GL_NORMALIZE);
  221.   }
  222.  
  223. //This just rotates the light around the y axis.
  224. void animate_light( int value ) {
  225.     x_light_angle += .1;
  226.     glutPostRedisplay();
  227.     if(rotateLight) {
  228.         glutTimerFunc(33, animate_light ,0);
  229.     }
  230. }
  231.  
  232. void init_lights( void ) {
  233.  
  234.     glEnable(GL_LIGHTING);
  235.     glEnable(GL_LIGHT0);
  236.    
  237.    
  238.    
  239.     //glEnable(GL_LIGHT1);
  240.  
  241.  
  242.  
  243.     //Define the RGB and Alpha values for the ambient light.
  244.     GLfloat light_ambient[] = {amb_light_val, amb_light_val, amb_light_val, 1.0};
  245.  
  246.     //Define the RGB and Alpha values for the diffuse light.
  247.     GLfloat  light_diffuse[] = {dif_light_val, dif_light_val, dif_light_val, 1.0};
  248.  
  249.    
  250.     //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
  251.    
  252.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  253.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  254.     //glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  255.     //glShadeModel(GL_SMOOTH);
  256.  
  257.    
  258.  
  259. }
  260.  
  261. void reinit_lights(void) {
  262.  
  263.     GLfloat light_ambient[] = {amb_light_val, amb_light_val, amb_light_val, 1.0};
  264.  
  265.     //Define the RGB and Alpha values for the diffuse light.
  266.     GLfloat  light_diffuse[] = {dif_light_val, dif_light_val, dif_light_val, 1.0};
  267.  
  268.    
  269.     //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient);
  270.    
  271.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  272.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  273.  
  274.     glutPostRedisplay();
  275.  
  276. }
  277.  
  278. void display(void)
  279. {
  280.  
  281.     //Define the background color as gray.
  282.     glClearColor(.5,.5,.5,1);
  283.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  284.  
  285.     glMatrixMode(GL_PROJECTION);
  286.     glLoadIdentity();
  287.  
  288.     //Set the perspective.
  289.     gluPerspective(60, 1, .1, 200);
  290.  
  291.  
  292.     glMatrixMode(GL_MODELVIEW);
  293.  
  294.     glLoadIdentity();
  295.  
  296.     //Calculate the rotation from the center using the angles relative to
  297.     // x-z (called 'x_angle') and y-x (called 'y_angle').
  298.     x_look_at = rDist*(cos(x_angle)*cos(y_angle));
  299.     y_look_at = rDist*sin(y_angle);
  300.     z_look_at = rDist*(sin(x_angle)*cos(y_angle));
  301.  
  302.  
  303.     //Use these new values to determine the vector for what you are looking at.
  304.     // Obviously, the center is (0,0,0).
  305.     gluLookAt(x_look_at,y_look_at,z_look_at,0,0,0,0,3,0);
  306.  
  307.     x_light_pos = 4*(cos(x_light_angle) * cos(y_light_angle));
  308.     y_light_pos = 4*(sin(y_light_angle));
  309.     z_light_pos = 4*(sin(x_light_angle) * cos(y_light_angle));
  310.  
  311.  
  312.     GLfloat light_position[] = {x_light_pos, y_light_pos, z_light_pos , 0};
  313.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  314.  
  315.  
  316.  
  317.  
  318.   glPushMatrix();
  319.  
  320.   //Create the floor.
  321.   glCallList(ground_floor);
  322.   glPopMatrix();
  323.   glPushMatrix();
  324.   glTranslatef(0,1,0);
  325.   glDisable(GL_NORMALIZE);
  326.   //Base block.
  327.   //Set the rotation around y.
  328.   glRotatef(bottomTurn, 0, 1, 0);
  329.   glPushMatrix();
  330.   //Thin it down a bit from the cube shape.
  331.   glScalef(.5f,1,.5f);
  332.   glNormal3f(.5f,0,.5f);
  333.   glCallList(column);
  334.   glScalef(2,1,2);
  335.  
  336.   //2nd block.
  337.   glPushMatrix();
  338.   glTranslatef(0,1,0);
  339.   glRotatef(midBend,1,0,0);
  340.   glTranslatef(0,1,0);
  341.   //This this block even further to make it look nicer.
  342.   glScalef(.25f,1,.25f);
  343.   glNormal3f(.25f,0,.25f);
  344.   glCallList(column);
  345.   glScalef(4,1,4);
  346.  
  347.  
  348.   //3rd block.
  349.   glPushMatrix();
  350.   glTranslatef(0,1,0);
  351.   glRotatef(-(topBend*2),1,0,1);
  352.   glTranslatef(0,1,0);
  353.    glScalef(.25f,1,.25f);
  354.    glNormal3f(.25f,0,.25f);
  355.   glCallList(column);
  356.   glScalef(4,1,4);
  357.  
  358.   //Deals with rotation of the claw.
  359.   glRotatef(clawTwist, 0, 1, 0);
  360.  
  361.   //Base of the claw block. This is just the block at the very base of the claw.
  362.   glPushMatrix();
  363.    glTranslatef(0,1,0);
  364.    glScalef(.5f,.5f,1);
  365.     glNormal3f(.5f,0,.5f);
  366.    glCallList(column);
  367.   glPopMatrix();
  368.  
  369.   //left Claw
  370.   glPushMatrix();
  371.   //Rotates the claw to pinch in and out.
  372.   glRotatef(clawPinch, 1, 0 , 0);
  373.   glTranslatef(0,2,-.5f);
  374.   glScalef(.5f,1,.5f);
  375.   glNormal3f(.5f,0,.5f);
  376.   glCallList(column);
  377.   glScalef(2,1,2);
  378.   glPushMatrix();
  379.   glTranslatef(0,1,-1);
  380.   glRotatef(-90, 1, 0 , 0);
  381.   glScalef(.5f,1,.2f);
  382.   glNormal3f(.5f,0,.2f);
  383.   glCallList(column);
  384.   glScalef(2,1,2);
  385.   glPopMatrix();
  386.   glPopMatrix();
  387.  
  388.  
  389.   //Right Claw
  390.   glPushMatrix();
  391.   glRotatef((-1 * clawPinch), 1, 0 , 0);
  392.   glTranslatef(0,2,.5f);
  393.   glScalef(.5f,1,.5f);
  394.   glNormal3f(.5f,0,.5f);
  395.   glCallList(column);
  396.   glScalef(2,1,2);
  397.   glPushMatrix();
  398.   glTranslatef(0,1,1);
  399.   glRotatef(90, 1, 0 , 0);
  400.   glNormal3f(.5f,0,.2f);
  401.   glScalef(.5f,1,.2f);
  402.   glCallList(column);
  403.   glScalef(2,1,2);
  404.  
  405.   glPopMatrix();
  406.   glPopMatrix();
  407.   glPopMatrix();
  408.   glPopMatrix();
  409.   glPopMatrix();
  410.   glPopMatrix();
  411.  
  412.    
  413.     glutSwapBuffers();
  414.     glFlush();
  415. }
  416.  
  417.  
  418. void keyPress (unsigned char key, int x, int y)
  419. {
  420.         switch(key) {
  421.  
  422.         // Pressing q - Quits the program
  423.         case 'q': exit (1); break;
  424.  
  425.         //Pressing s - sets the program to run frame by frame, then runs it.
  426.         case 's' : animate = false;
  427.             glutTimerFunc(33, animate_whole, 0);
  428.             break;
  429.  
  430.         //Pressing c - sets the program to run the animation continuously, then runs it.
  431.         case 'c' : animate = true;
  432.             glutTimerFunc(33, animate_whole, 0);
  433.             break;
  434.  
  435.             //Pressing r - Resets all values to their original position.
  436.         case 'r':
  437.             {
  438.  
  439.             window_x = 600;
  440.             window_y = 600;
  441.             x_angle = 60.0;
  442.             y_angle = -10.0;
  443.  
  444.             x_look_at = 0.0;
  445.             y_look_at = 4.0;
  446.             z_look_at = 20.0;
  447.             rDist = 24.0;
  448.  
  449.  
  450.             clawPinch = 45;
  451.             topBend = 10;
  452.             midBend = 10;
  453.             bottomTurn = 10;
  454.  
  455.             clawTwist = 0;
  456.             clawTwistDelta = 0;
  457.             topDelta = 0.4;
  458.             midDelta = 0.4;
  459.             bottomDelta = 0.4;
  460.             clawDelta = 1;
  461.  
  462.             clawExpand = true;
  463.             bendForward = true;
  464.  
  465.             animate = false;
  466.  
  467.             x_light_angle = 45;
  468.             y_light_angle = 45;
  469.  
  470.             rotateLight = false;
  471.  
  472.             amb_light_val = .7;
  473.             dif_light_val = .9;
  474.  
  475.             reinit_lights();
  476.  
  477.                
  478.             }
  479.             break;
  480.  
  481.         case 'z':
  482.  
  483.             rotateLight = !rotateLight;
  484.  
  485.             if(rotateLight) {
  486.                 glutTimerFunc(33, animate_light, 0);
  487.             } else {
  488.                 glutTimerFunc(33, doNothing, 0);
  489.             }
  490.            
  491.             break;
  492.  
  493.             //Toggles between 10 different ambient light intensities
  494.         case 'n':
  495.            
  496.             if(amb_light_val < 1.0) {
  497.                 amb_light_val += .1;
  498.             } else {
  499.                 amb_light_val = .1;
  500.                 }
  501.  
  502.             reinit_lights();
  503.            
  504.             break;
  505.  
  506.             //toggles between 10 different diffuse light intensities
  507.         case 'm':
  508.             if(dif_light_val < 1.0) {
  509.                 dif_light_val += .1;
  510.             } else {
  511.                 dif_light_val = .1;
  512.             }
  513.  
  514.             reinit_lights();
  515.            
  516.            
  517.             break;
  518.  
  519.            
  520.            
  521.  
  522.         }
  523.           glutPostRedisplay();
  524. }
  525.  
  526.  
  527. void mouse_click (int button, int state, int x, int y) {
  528.  
  529.      //If you're clicking
  530.     if (state == GLUT_DOWN) {
  531.         //Save the coordinates.
  532.         click_x = x;
  533.         click_y = y;
  534.  
  535.         //If you're holding down a left click:
  536.  
  537.         if (button == GLUT_LEFT_BUTTON) {
  538.             //You're trying to rotate the camera around (0,0,0).
  539.             mouse_mode = ROTATE_FORM;
  540.         }
  541.         //If the button you're holding down is the middle one:
  542.         else if (button == GLUT_MIDDLE_BUTTON) {
  543.             //You're trying to zoom in or out.
  544.             mouse_mode = ZOOM_FORM;
  545.         }
  546.     } else if (state == GLUT_UP) {
  547.       mouse_mode = NO_FORM;
  548.     }
  549.  
  550. }
  551.  
  552.  
  553. void mouse_move (int x, int y)
  554. {
  555.  
  556.  
  557.     if (mouse_mode == ROTATE_FORM) {
  558.  
  559.         x_angle += (x - click_x)/60.0;
  560.  
  561.         if (x_angle > 180){
  562.               x_angle -= 360;
  563.         }
  564.         else if (x_angle < -180) {
  565.             x_angle += 360;
  566.          }
  567.  
  568.         click_x = x;
  569.        
  570.         y_angle += (y - click_y)/60.0;
  571.         if (y_angle > 180) {
  572.             y_angle -= 360;
  573.         }
  574.         else if (y_angle <-180) {
  575.             y_angle += 360;
  576.         }
  577.         click_y = y;
  578.  
  579.  
  580.         } else if (mouse_mode == ZOOM_FORM){
  581.  
  582.         float old_size = rDist;
  583.         rDist = rDist + ((y - click_y)/20.0);
  584.         if (rDist < 0) {
  585.             rDist = old_size;
  586.         }
  587.         click_y = y;
  588.     }
  589.  
  590.     glutPostRedisplay();
  591.  
  592. }
  593.  
  594. //Up or down zooming
  595. void keyUpOrDown (int key, int x, int y) {  
  596.  
  597.     //If you push down, increase the r-size by 4.
  598.     if (GLUT_KEY_DOWN == key) {
  599.      
  600.         rDist += 4;
  601.  
  602.         //Otherwise, if you push up, decrease the r-size by 4.
  603.     } else if(GLUT_KEY_UP == key) {
  604.         if(rDist >= 4) {
  605.             rDist -= 4;
  606.         } else {
  607.             rDist = 0.0;
  608.         }
  609.  
  610.     }
  611.  
  612.  
  613.   glutPostRedisplay();
  614.  
  615. }
  616.  
  617.  
  618. //In case of window resizing:
  619.  
  620. void handleResize(int w, int h) {
  621.    
  622.     if (w > h) {
  623.  
  624.         w = h;
  625.  
  626.     } else if (h > w) {
  627.         h = w;
  628.     }
  629.    
  630.     glViewport(0, 0, w, h);
  631.    
  632.     glMatrixMode(GL_PROJECTION);
  633.  
  634.     glutPostRedisplay();
  635.    
  636. }
  637.  
  638. void init(void)  {
  639.     vectors[0][0] = vectors[1][0] = vectors[2][0] = vectors[3][0] = -1;
  640.   vectors[4][0] = vectors[5][0] = vectors[6][0] = vectors[7][0] = 1;
  641.   vectors[0][1] = vectors[1][1] = vectors[4][1] = vectors[5][1] = -1;
  642.   vectors[2][1] = vectors[3][1] = vectors[6][1] = vectors[7][1] = 1;
  643.   vectors[0][2] = vectors[3][2] = vectors[4][2] = vectors[7][2] = 1;
  644.   vectors[1][2] = vectors[2][2] = vectors[5][2] = vectors[6][2] = -1;
  645.  
  646.     glEnable(GL_NORMALIZE);
  647.     glEnable(GL_DEPTH_TEST);
  648.    
  649.     glDisable(GL_COLOR_MATERIAL);
  650.  
  651.     //Create the Lists (one for the floor, one for the block).
  652.     GLuint index = glGenLists(2);
  653.     glNewList(1, GL_COMPILE);
  654.     draw_block(.6, .2, 0);
  655.     glEndList();
  656.  
  657.     glNewList(2, GL_COMPILE);
  658.     draw_floor(.5, 0, .5);
  659.     glEndList();
  660.  
  661.     init_lights();
  662.  
  663. }
  664.  
  665. int main(int argc, char** argv)
  666. {
  667.     glutInit(&argc, argv);
  668.  
  669.     int mode = GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH;
  670.  
  671.     glutInitDisplayMode(mode);
  672.     glutInitWindowSize(600,600);
  673.     glutCreateWindow("Lab 3 - Karl Racke");  
  674.  
  675.     init();
  676.  
  677.    
  678.     glutDisplayFunc(display);          
  679.     glutMouseFunc(mouse_click);
  680.     glutMotionFunc(mouse_move);
  681.     glutSpecialFunc(keyUpOrDown);
  682.     glutReshapeFunc(handleResize);
  683.     glutKeyboardFunc(keyPress);
  684.  
  685.  
  686.  
  687.     glClearColor(1.0,0.5,0.0,0.5);      
  688.     glColor3f(0.0,1.0,0.0);            
  689.    
  690.     glutMainLoop();
  691.     return EXIT_SUCCESS;
  692.    
  693. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top