Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <bevgrafmath2017.h>
  3. #include <math.h>
  4.  
  5. float alpha = -1.4, radius = 4, m = 1; // kamera ertekei
  6. float R = 1.5, r = 0.5, u, v, lambdaU = pi() / 8, lambdaV = pi() / 8, rotateTorusValue; // torusz ertekei
  7. float projectionDist = 4; // centralis vetites tavolsag
  8. int torusCount = 0;
  9. GLsizei winWidth = 800, winHeight = 600;
  10. GLint keyStates[256];
  11. bool projectionValue = true;
  12.  
  13. vec3 cube[8] = {
  14. { 0.5, -0.5, -0.5 }, { 0.5 , -0.5, 0.5 }, { -0.5 , -0.5, 0.5 },
  15. { -0.5 , -0.5, -0.5 }, { 0.5, 0.5 , -0.5 }, { 0.5, 0.5, 0.5 },
  16. { -0.5, 0.5, 0.5 }, { -0.5, 0.5, -0.5 }
  17. };
  18. vec3 torus[4];
  19.  
  20. vec3 camera;
  21. vec3 CO, Cx, Cy, Cz;
  22. vec3 up = { 0, 0, 1 };
  23. vec3 origo = { 0, 0, 0 };
  24.  
  25. mat4 w2v, scaled, projection, K, rotateTorus;
  26.  
  27. struct face {
  28. vec3 p[4]; // 4 pontu lap
  29. vec3 normal; // normalvektor a laphozű
  30. vec3 sulypont;
  31. };
  32.  
  33. face cubeFaces[6] = {}; // 6 lapu a kocka
  34. face torusFaces[500] = {};
  35. face uniteFaces[500] = {};
  36.  
  37. vec3 calculateNormalVectors(face param) {
  38. return cross(param.p[1] - param.p[0], param.p[3] - param.p[0]);
  39. }
  40.  
  41. vec3 calculateSulypont(face param) {
  42. return (param.p[0] + param.p[1] + param.p[2] + param.p[3]) / 4;
  43. }
  44.  
  45. void setProjection() {
  46. if (projectionValue) {
  47. projection = ortho();
  48. }
  49. else {
  50. projection = perspective(projectionDist);
  51. }
  52. }
  53.  
  54. void initializeCamera() {
  55. camera = { 0, 0, 0 };
  56. camera.x = radius*cos(alpha);
  57. camera.y = radius*sin(alpha);
  58. camera.z = m;
  59. CO = origo - camera; // kamera pontjából az eredeti origóba mutató vektor
  60. Cz = normalize(-CO); // CO vektornak a negaltjat normalizalva => z tengely
  61. Cx = normalize(cross(up, Cz)); // up vektor es a z tengely vektorialis szorzata => x tengelyet
  62. Cy = normalize(cross(Cz, Cx)); // z es x tengely vektorialis szorzata => y tengely
  63. K = coordinateTransform(camera, Cx, Cy, Cz);
  64. }
  65.  
  66. void initPoints() {
  67. // kocka pontjainak beallitasa
  68. cubeFaces[0].p[0] = cube[5];
  69. cubeFaces[0].p[1] = cube[6];
  70. cubeFaces[0].p[2] = cube[2];
  71. cubeFaces[0].p[3] = cube[1];
  72.  
  73. cubeFaces[1].p[0] = cube[6];
  74. cubeFaces[1].p[1] = cube[7];
  75. cubeFaces[1].p[2] = cube[3];
  76. cubeFaces[1].p[3] = cube[2];
  77.  
  78. cubeFaces[2].p[0] = cube[7];
  79. cubeFaces[2].p[1] = cube[4];
  80. cubeFaces[2].p[2] = cube[0];
  81. cubeFaces[2].p[3] = cube[3];
  82.  
  83. cubeFaces[3].p[0] = cube[4];
  84. cubeFaces[3].p[1] = cube[5];
  85. cubeFaces[3].p[2] = cube[1];
  86. cubeFaces[3].p[3] = cube[0];
  87.  
  88. cubeFaces[4].p[0] = cube[3];
  89. cubeFaces[4].p[1] = cube[0];
  90. cubeFaces[4].p[2] = cube[1];
  91. cubeFaces[4].p[3] = cube[2];
  92.  
  93. cubeFaces[5].p[0] = cube[7];
  94. cubeFaces[5].p[1] = cube[6];
  95. cubeFaces[5].p[2] = cube[5];
  96. cubeFaces[5].p[3] = cube[4];
  97.  
  98. for (int i = 0; i < 6; i++) {
  99. cubeFaces[i].normal = calculateNormalVectors(cubeFaces[i]);
  100. }
  101.  
  102. // torusz pontjainak kiszamitasa
  103. torusCount = 0;
  104.  
  105. for (u = 0; u <= 2.0 * pi(); u += lambdaU) {
  106. for (v = 0; v <= 2.0 * pi(); v += lambdaV) {
  107. torus[0].x = (R + r * cos(u)) * cos(v);
  108. torus[0].y = (R + r * cos(u)) * sin(v);
  109. torus[0].z = r * sin(u);
  110.  
  111. torus[1].x = (R + r * cos(u)) * cos(v + lambdaV);
  112. torus[1].y = (R + r * cos(u)) * sin(v + lambdaV);
  113. torus[1].z = r * sin(u);
  114.  
  115. torus[2].x = (R + r * cos(u + lambdaU)) * cos(v + lambdaV);
  116. torus[2].y = (R + r * cos(u + lambdaU)) * sin(v + lambdaV);
  117. torus[2].z = r * sin(u + lambdaU);
  118.  
  119. torus[3].x = (R + r * cos(u + lambdaU)) * cos(v);
  120. torus[3].y = (R + r * cos(u + lambdaU)) * sin(v);
  121. torus[3].z = r * sin(u + lambdaU);
  122.  
  123. torusFaces[torusCount].p[0] = torus[0];
  124. torusFaces[torusCount].p[1] = torus[1];
  125. torusFaces[torusCount].p[2] = torus[2];
  126. torusFaces[torusCount].p[3] = torus[3];
  127.  
  128. torusFaces[torusCount].normal = calculateNormalVectors(torusFaces[torusCount]);
  129.  
  130. torusCount++;
  131. }
  132. printf("Toruscount: %d ", torusCount);
  133. }
  134.  
  135. }
  136.  
  137. void initMatrices() {
  138. vec2 windowSize = { 1, 1 };
  139. vec2 windowPosition = { -1, -1 };
  140. vec2 viewportSize = { 150, 150 };
  141. vec2 viewportPosition = { winWidth / 2 - viewportSize.x, winHeight / 2 - viewportSize.y};
  142. w2v = windowToViewport3(windowPosition, windowSize, viewportPosition, viewportSize);
  143. }
  144.  
  145. void keyPressed(unsigned char key, int x, int y) {
  146. keyStates[key] = 1;
  147. }
  148.  
  149. void keyUp(unsigned char key, int x, int y) {
  150. keyStates[key] = 0;
  151. }
  152.  
  153. void keyOperations() {
  154. if (keyStates['r']) { if (m < 5) m += 0.1; }
  155. if (keyStates['f']) { if (m > -5) m -= 0.1; }
  156.  
  157. if (keyStates['w']) { if (radius < 6) radius += 0.05; }
  158. if (keyStates['s']) { if (radius > 2) radius -= 0.05; }
  159.  
  160. if (keyStates['d']) { alpha += 0.05; }
  161. if (keyStates['g']) { alpha -= 0.05; }
  162.  
  163. if (keyStates['u']) { if (R < 2.1) R += 0.01; }
  164. if (keyStates['j']) { if (R > 1.4) R -= 0.01; }
  165.  
  166. if (keyStates['i']) { if (r < 0.7) r += 0.01; }
  167. if (keyStates['k']) { if (r > 0.2) r -= 0.01; }
  168.  
  169. if (keyStates['[']) { projectionValue = true; }
  170. if (keyStates[']']) { projectionValue = false; }
  171.  
  172. if (keyStates['e']) { if (projectionDist < 6) projectionDist += 0.1; }
  173. if (keyStates['t']) { if (projectionDist > 2) projectionDist -= 0.1; }
  174.  
  175. glutPostRedisplay();
  176. }
  177.  
  178. void calculateFaces() {
  179.  
  180. keyOperations();
  181. initializeCamera();
  182. initPoints();
  183. setProjection();
  184.  
  185. glColor3f(0, 0, 0);
  186.  
  187. mat4 M = w2v * projection;
  188.  
  189. for (int i = 0; i < 6; i++) {
  190. for (int j = 0; j < 4; j++) {
  191. vec4 pointH = ihToH(cubeFaces[i].p[j]);
  192. cubeFaces[i].p[j] = hToIh(K * pointH);
  193. }
  194.  
  195. cubeFaces[i].normal = calculateNormalVectors(cubeFaces[i]);
  196. cubeFaces[i].sulypont = calculateSulypont(cubeFaces[i]);
  197.  
  198. for (int j = 0; j < 4; j++) {
  199. vec4 pointH = ihToH(cubeFaces[i].p[j]);
  200. cubeFaces[i].p[j] = hToIh(M * pointH);
  201. }
  202. }
  203.  
  204. /*
  205. for (int i = 0; i < torusCount; i++) {
  206.  
  207. for (int j = 0; j < 4; j++) {
  208. vec4 pointH = ihToH(torusFaces[i].p[i]);
  209. vec4 transformedPoint = M * pointH;
  210. vec4 vectorH = ihToH(torusFaces[i].normal);
  211. vec4 transformedVector = transpose(M) * vectorH;
  212. torusFaces[i].normal = hToIh(transformedVector);
  213. }
  214. }
  215. */
  216.  
  217. for (int i = 0; i < torusCount; i++) {
  218. for (int j = 0; j < 4; j++) {
  219. vec4 pointH = ihToH(torusFaces[i].p[j]);
  220. torusFaces[i].p[j] = hToIh(K * rotateTorus * pointH);
  221. }
  222.  
  223. torusFaces[i].normal = calculateNormalVectors(torusFaces[i]);
  224. torusFaces[i].sulypont = calculateSulypont(torusFaces[i]);
  225.  
  226. for (int j = 0; j < 4; j++) {
  227. vec4 pointH = ihToH(torusFaces[i].p[j]);
  228. torusFaces[i].p[j] = hToIh(M * pointH);
  229. }
  230.  
  231. }
  232.  
  233. for (int i = 0; i < 6; i++) {
  234. uniteFaces[i] = cubeFaces[i];
  235. }
  236.  
  237. for (int i = 0; i < torusCount; i++) {
  238. uniteFaces[i+6] = torusFaces[i];
  239. }
  240. }
  241.  
  242. void drawFaces() {
  243.  
  244. for (int i = 0; i < torusCount + 6; i++) {
  245. glBegin(GL_LINE_LOOP);
  246. for (int j = 0; j < 4; j++) {
  247. glVertex2f(uniteFaces[i].p[j].x, uniteFaces[i].p[j].y);
  248. }
  249. glEnd();
  250. }
  251. }
  252.  
  253. void init() {
  254. glClearColor(1.0, 1.0, 1.0, 0.0);
  255. glMatrixMode(GL_PROJECTION);
  256. gluOrtho2D(0.0, winWidth, 0.0, winHeight);
  257. glShadeModel(GL_FLAT);
  258. glEnable(GL_POINT_SMOOTH);
  259. glPointSize(5.0);
  260. glLineWidth(1.5);
  261.  
  262. initPoints();
  263. initMatrices();
  264. }
  265.  
  266. void display() {
  267. glClear(GL_COLOR_BUFFER_BIT);
  268.  
  269. glColor3f(0, 0, 0);
  270.  
  271. drawFaces();
  272.  
  273. glutSwapBuffers();
  274. }
  275.  
  276. void update(int value) {
  277. rotateTorusValue += 0.01;
  278. rotateTorus = rotateZ(rotateTorusValue);
  279.  
  280. glutPostRedisplay();
  281.  
  282. glutTimerFunc(10, update, 0);
  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("Masodik vedesi feladat");
  291.  
  292. glutKeyboardFunc(keyPressed);
  293. glutKeyboardUpFunc(keyUp);
  294.  
  295. init();
  296. glutDisplayFunc(display);
  297.  
  298. glutTimerFunc(10, update, 0);
  299.  
  300. glutMainLoop();
  301. return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement