Advertisement
Guest User

Untitled

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