Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <bevgrafmath2017.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5.  
  6. GLsizei winWidth = 800, winHeight = 600;
  7.  
  8. bool vetites = true;
  9.  
  10. float angle = 0, radius = 4, height = 0;
  11. float R = 1.5, r = 0.5, u, v, lambdaU = pi() / 8, lambdaV = pi() / 8;
  12. float s = 5;
  13. int torusCount = 0;
  14. GLint keyStates[256];
  15.  
  16. vec3 cube[8] = {
  17. { 0.5, -0.5, -0.5 },
  18. { 0.5 , -0.5, 0.5 },
  19. { -0.5 , -0.5, 0.5 },
  20. { -0.5 , -0.5 , -0.5 },
  21. { 0.5 ,0.5 ,-0.5 },
  22. { 0.5 , 0.5 , 0.5 },
  23. { -0.5 ,0.5 ,0.5 },
  24. { -0.5 ,0.5 ,-0.5 }
  25. };
  26.  
  27. vec3 torus[4];
  28.  
  29. vec3 camera;
  30. vec3 CO, Cz, Cx, Cy;
  31. vec3 up = { 0, 0, 1 }, origo = { 0, 0, 0 };
  32.  
  33. mat4 w2v, scaled, projection, K;
  34.  
  35. struct face {
  36. vec3 p[4];
  37. vec3 normal;
  38. };
  39.  
  40. face cubeFaces[6] = {};
  41. face torusFaces[500] = {};
  42.  
  43.  
  44. void keyPressed(unsigned char key, int x, int y) {
  45. keyStates[key] = 1;
  46. }
  47.  
  48. void keyUp(unsigned char key, int x, int y) {
  49. keyStates[key] = 0;
  50. }
  51.  
  52. void keyOperations() {
  53. if (keyStates['w'] && height < 10) { height += 0.01; }
  54. if (keyStates['s'] && height > -10) { height -= 0.01; }
  55.  
  56. if (keyStates['a']) { angle -= 0.01; }
  57. if (keyStates['d']) { angle += 0.01; }
  58.  
  59. if (keyStates['q'] && radius > 3) { radius -= 0.01; }
  60. if (keyStates['e'] && radius < 5) { radius += 0.01; }
  61.  
  62. if (keyStates['+']) { R -= 0.001; }
  63. if (keyStates['-']) { R += 0.001; }
  64.  
  65. if (keyStates['*']) { r -= 0.001; }
  66. if (keyStates['/']) { r += 0.001; }
  67.  
  68. if (keyStates['z']) { s += 0.01; }
  69. if (keyStates['c']) { s -= 0.01; }
  70.  
  71. if (keyStates[',']) { vetites = true; }
  72. if (keyStates['.']) { vetites = false; }
  73.  
  74.  
  75. glutPostRedisplay();
  76. }
  77.  
  78. void setNormalVectors() {
  79. for (int i = 0; i < 6; i++)
  80. cubeFaces[i].normal = cross(cubeFaces[i].p[1] - cubeFaces[i].p[0], cubeFaces[i].p[2] - cubeFaces[i].p[0]);
  81.  
  82. }
  83.  
  84. void setCam() {
  85. camera = { 0,0,0 };
  86. camera.x = radius*cos(angle);
  87. camera.y = radius*sin(angle);
  88. camera.z = height;
  89. CO = origo - camera;
  90. Cz = normalize(-CO);
  91. Cx = normalize(cross(up, Cz));
  92. Cy = normalize(cross(Cz, Cx));
  93. K = coordinateTransform(camera, Cx, Cy, Cz);
  94. if (vetites) {
  95. projection = perspective(s);
  96. }
  97. else
  98. {
  99. projection = ortho();
  100. }
  101.  
  102. }
  103.  
  104. void initCube() {
  105. cubeFaces[0].p[0] = cube[5];
  106. cubeFaces[0].p[1] = cube[6];
  107. cubeFaces[0].p[2] = cube[2];
  108. cubeFaces[0].p[3] = cube[1];
  109.  
  110. cubeFaces[1].p[0] = cube[6];
  111. cubeFaces[1].p[1] = cube[7];
  112. cubeFaces[1].p[2] = cube[3];
  113. cubeFaces[1].p[3] = cube[2];
  114.  
  115. cubeFaces[2].p[0] = cube[7];
  116. cubeFaces[2].p[1] = cube[4];
  117. cubeFaces[2].p[2] = cube[0];
  118. cubeFaces[2].p[3] = cube[3];
  119.  
  120. cubeFaces[3].p[0] = cube[4];
  121. cubeFaces[3].p[1] = cube[5];
  122. cubeFaces[3].p[2] = cube[1];
  123. cubeFaces[3].p[3] = cube[0];
  124.  
  125. cubeFaces[4].p[0] = cube[3];
  126. cubeFaces[4].p[1] = cube[2];
  127. cubeFaces[4].p[2] = cube[1];
  128. cubeFaces[4].p[3] = cube[0];
  129.  
  130. cubeFaces[5].p[0] = cube[7];
  131. cubeFaces[5].p[1] = cube[6];
  132. cubeFaces[5].p[2] = cube[5];
  133. cubeFaces[5].p[3] = cube[4];
  134.  
  135. setNormalVectors();
  136. }
  137.  
  138. void setTorus() {
  139. torusCount = 0;
  140. for (u = 0; u < 2.0 * pi(); u += lambdaU) {
  141. for (v = 0; v < 2.0 * pi(); v += lambdaV) {
  142. torus[0].x = (R + r * cos(u)) * cos(v);
  143. torus[0].y = (R + r * cos(u)) * sin(v);
  144. torus[0].z = r * sin(u);
  145.  
  146. torus[1].x = (R + r * cos(u)) * cos(v + lambdaV);
  147. torus[1].y = (R + r * cos(u)) * sin(v + lambdaV);
  148. torus[1].z = r * sin(u);
  149.  
  150. torus[2].x = (R + r * cos(u + lambdaU)) * cos(v + lambdaV);
  151. torus[2].y = (R + r * cos(u + lambdaU)) * sin(v + lambdaV);
  152. torus[2].z = r * sin(u + lambdaU);
  153.  
  154. torus[3].x = (R + r * cos(u + lambdaU)) * cos(v);
  155. torus[3].y = (R + r * cos(u + lambdaU)) * sin(v);
  156. torus[3].z = r * sin(u + lambdaU);
  157.  
  158. torusFaces[torusCount].p[0] = torus[0];
  159. torusFaces[torusCount].p[1] = torus[1];
  160. torusFaces[torusCount].p[2] = torus[2];
  161. torusFaces[torusCount].p[3] = torus[3];
  162.  
  163. torusCount++;
  164. }
  165. }
  166.  
  167. }
  168.  
  169. void initMatrices() {
  170.  
  171. vec2 windowSize = { 1,1 };
  172. vec2 windowPosition = { -1, -1 };
  173. vec2 viewportSize = { 150, 150 };
  174. vec2 viewportPosition = { 250, 150 };
  175. w2v = windowToViewport3(windowPosition, windowSize, viewportPosition, viewportSize);
  176.  
  177. }
  178.  
  179. void init() {
  180. glClearColor(1.0, 1.0, 1.0, 0.0);
  181. glMatrixMode(GL_PROJECTION);
  182. gluOrtho2D(0.0, winWidth, 0.0, winHeight);
  183. glShadeModel(GL_FLAT);
  184. glEnable(GL_POINT_SMOOTH);
  185. glPointSize(5.0);
  186. glLineWidth(1.0);
  187. initCube();
  188. initMatrices();
  189. }
  190.  
  191. void drawFaces() {
  192.  
  193. keyOperations();
  194. setCam();
  195. setTorus();
  196. mat4 M = w2v *projection *K;
  197.  
  198. for (int idx = 0; idx < 6; idx++) {
  199.  
  200. face f = cubeFaces[idx];
  201.  
  202. glColor3f(0, 0, 0);
  203.  
  204. glBegin(GL_LINE_LOOP);
  205. for (int i = 0; i < 4; i++) {
  206.  
  207. vec4 pointH = ihToH(f.p[i]);
  208. vec4 transformedPoint = M * pointH;
  209.  
  210. if (transformedPoint.w != 0) {
  211. vec3 result = hToIh(transformedPoint);
  212. if (result.z == 0) {
  213. glVertex2f(result.x, result.y);
  214. }
  215. }
  216.  
  217. }
  218. glEnd();
  219.  
  220. }
  221.  
  222. for (int idx = 0; idx < torusCount; idx++) {
  223.  
  224. face f = torusFaces[idx];
  225.  
  226. glBegin(GL_LINE_LOOP);
  227. for (int i = 0; i < 4; i++) {
  228.  
  229. vec4 pointH = ihToH(f.p[i]);
  230. vec4 transformedPoint = M * pointH;
  231.  
  232. if (transformedPoint.w != 0) {
  233. vec3 result = hToIh(transformedPoint);
  234. if (result.z == 0) {
  235. glVertex2f(result.x, result.y);
  236. }
  237. }
  238.  
  239. }
  240. glEnd();
  241.  
  242. }
  243. }
  244.  
  245. void display() {
  246. glClear(GL_COLOR_BUFFER_BIT);
  247.  
  248. glColor3f(0, 0, 0);
  249.  
  250. drawFaces();
  251.  
  252. glutSwapBuffers();
  253. }
  254.  
  255. int main(int argc, char** argv) {
  256. glutInit(&argc, argv);
  257. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  258. glutInitWindowSize(winWidth, winHeight);
  259. glutInitWindowPosition(100, 100);
  260. glutCreateWindow("cube");
  261. glutKeyboardFunc(keyPressed);
  262. glutKeyboardUpFunc(keyUp);
  263. init();
  264. glutDisplayFunc(display);
  265.  
  266. // glutTimerFunc(10, update, 0);
  267.  
  268.  
  269. glutMainLoop();
  270. return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement