Advertisement
Guest User

Untitled

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