Advertisement
Guest User

Untitled

a guest
May 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "stdio.h"
  3. #include "gl/glew.h"
  4. #include "gl/glut.h"
  5.  
  6. //DALTON BURTS
  7. //a robot whose head moves back and forth at 90 degree angles. I noticed that the speed is different on different computers. had to lower my speed from .5 to .01
  8.  
  9. float yLocation = 0.0f; //measure how far the y axis has rotated
  10. bool tooFar = false; //gauge distance head has moved
  11.  
  12.  
  13. void cubey(float size)
  14. {
  15. glBegin(GL_QUADS);
  16. //front face of the cube
  17. glVertex3f(size / 2, size / 2, size / 2);
  18. glVertex3f(-size / 2, size / 2, size / 2);
  19. glVertex3f(-size / 2, -size / 2, size / 2);
  20. glVertex3f(size / 2, -size / 2, size / 2);
  21. //left face
  22. glVertex3f(-size / 2, size / 2, size / 2);
  23. glVertex3f(-size / 2, size / 2, -size / 2);
  24. glVertex3f(-size / 2, -size / 2, -size / 2);
  25. glVertex3f(-size / 2, -size / 2, size / 2);
  26. //backface
  27. glVertex3f(size / 2, size / 2, -size / 2);
  28. glVertex3f(-size / 2, size / 2, -size / 2);
  29. glVertex3f(-size / 2, -size / 2, -size / 2);
  30. glVertex3f(size / 2, -size / 2, -size / 2);
  31. //right face
  32. glVertex3f(size / 2, size / 2, -size / 2);
  33. glVertex3f(size / 2, size / 2, size / 2);
  34. glVertex3f(size / 2, -size / 2, size / 2);
  35. glVertex3f(size / 2, -size / 2, -size / 2);
  36. //top
  37. glVertex3f(size / 2, size / 2, size / 2);
  38. glVertex3f(-size / 2, size / 2, size / 2);
  39. glVertex3f(-size / 2, size / 2, -size / 2);
  40. glVertex3f(size / 2, size / 2, -size / 2);
  41. //bottom
  42. glVertex3f(size / 2, -size / 2, size / 2);
  43. glVertex3f(-size / 2, -size / 2, size / 2);
  44. glVertex3f(-size / 2, -size / 2, -size / 2);
  45. glVertex3f(size / 2, -size / 2, -size / 2);
  46. glEnd();
  47. }
  48. void head()
  49. {
  50. //ROBOT HEAD
  51. glTranslatef(0, 2, -9);
  52.  
  53. if (tooFar == false) //if you haven't gone too far
  54. {
  55. yLocation += .01; //rotate at this speed
  56. glRotatef(yLocation, 0, 1, 0); //in a positive y direction
  57. if (yLocation >= 90) //if greater than 90 degrees turn around
  58. {
  59. tooFar = true;
  60. }
  61. }
  62.  
  63. if(tooFar == true)
  64. {
  65. yLocation -= .01; //subtracting rewinds the motion as long as the y axis isn't inverted
  66. glRotatef(yLocation, 0, 1, 0); //maintain proper y axis rotation
  67. if (yLocation <= -90) //if the float yLocation drops below -90 the bool triggers and resets the animation
  68. {
  69. tooFar = false;
  70. }
  71. }
  72.  
  73. glColor3f(0, 0, 0);
  74. cubey(1.0); //head base
  75. glTranslatef(.25, 0, .5);
  76. glColor3f(1, 0, 0);
  77. cubey(.1); //eye 1
  78. glTranslatef(-.5, 0, 0);
  79. glColor3f(1, 0, 0);
  80. cubey(.1); //eye 2
  81. //translate works in relation to last location***
  82.  
  83. }
  84. void torso()
  85. {
  86.  
  87. glTranslatef(.25 , -1.5, -.5);
  88. glColor3f(0, 0, 1);
  89. cubey(1.5);
  90.  
  91. glTranslatef(-1, .6, 0);
  92. glColor3f(0, 0, 0);
  93. cubey(.75);
  94. glTranslatef(1, 0, 0);
  95. cubey(.75);
  96. glTranslatef(1, 0, 0);
  97. cubey(.75);
  98. glTranslatef(-1, -1, 0);
  99. cubey(.75);
  100. glTranslatef(0, -.75, 0);
  101. cubey(.75);
  102. //
  103. glTranslatef(-.5, 0, 0);
  104. cubey(.75);
  105. glTranslatef(1, 0, 0);
  106. cubey(.75);
  107. glTranslatef(0, -.5, 0);
  108. cubey(.75);
  109. glTranslatef(-1, 0, 0);
  110. cubey(.75);
  111. glTranslatef(0,-.5,0);
  112. glColor3f(0, 0, 1);
  113. cubey(.8);
  114. glTranslatef(1, 0, 0);
  115. cubey(.8);
  116.  
  117.  
  118. }
  119.  
  120. void init()
  121. {
  122. glClearColor(1, 1, 1, 0);
  123. glMatrixMode(GL_PROJECTION);
  124. glLoadIdentity();
  125. //glOrtho(0, 400, 400, 1, -1, 1);
  126.  
  127. gluPerspective(45, 640 / 480, 1, 500);
  128. glMatrixMode(GL_MODELVIEW);
  129. glEnable(GL_DEPTH_TEST);
  130.  
  131. }
  132.  
  133. void draw()
  134. {
  135. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  136. glPushMatrix(); //outer push
  137.  
  138. glLoadIdentity(); //load identity matrix obj 1
  139.  
  140. glPushMatrix(); //inner push obj 1
  141. head();
  142. //glPopMatrix(); //inner pop obj 1
  143.  
  144.  
  145. //glLoadIdentity(); //doesn't change anything
  146.  
  147. //body disappears.
  148. //glPushMatrix(); //inner push obj 2
  149. torso();
  150. glPopMatrix(); //inner pop obj 2
  151.  
  152. glPopMatrix(); //outer pop
  153.  
  154. }
  155. int main(int argc, char *argv[])
  156. {
  157. SDL_Window *mainwindow;
  158. SDL_GLContext maincontext;
  159. SDL_Init(SDL_INIT_VIDEO);
  160. mainwindow = SDL_CreateWindow("Custom Robo",
  161. SDL_WINDOWPOS_CENTERED,
  162. SDL_WINDOWPOS_CENTERED,
  163. 400, 400,
  164. SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  165. maincontext = SDL_GL_CreateContext(mainwindow);
  166.  
  167. bool running = true;
  168. SDL_Event event;
  169. init();
  170. while (running)
  171. {
  172.  
  173. draw();
  174.  
  175.  
  176. while (SDL_PollEvent(&event))
  177. {
  178. switch (event.type)
  179. {
  180. case SDL_QUIT:
  181. running = false;
  182. break;
  183. case SDL_KEYDOWN:
  184. switch (event.key.keysym.sym)
  185. {
  186. case SDLK_h:
  187.  
  188. break;
  189.  
  190. case SDLK_q:
  191. SDL_Quit();
  192. running = false;
  193. break;
  194. }//close key switch
  195. }//close switch
  196. }//close inner while
  197. SDL_GL_SwapWindow(mainwindow);
  198. }//close outer while
  199. return 0;
  200. }//close main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement