Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include "bevgrafmath2017.h"
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. //Fényforrás
  10. vec3 feny = { 1,0,0 };
  11.  
  12. float lepes = pi()/8, r=5, centrum=8;
  13.  
  14. struct FACE{
  15. vec3 t[4];
  16. float distance=0;
  17. vec3 normal, suly;
  18. bool triangle = false;
  19. };
  20.  
  21. vector<FACE> faces;
  22.  
  23. bool sorban1(FACE a, FACE b)
  24. {
  25. return a.distance > b.distance;
  26. }
  27.  
  28. GLsizei winWidth = 800, winHeight = 800;
  29.  
  30. vec3 points[16] = { {-3,-1,-3},{-3,0,-1},{-3,0,1},{-3,-1,3},
  31. {-1,0,-3},{-1,0,-1},{-1,0,1},{-1,0,3},
  32. {1,0,-3},{1,0,-1},{1,0,1},{1,0,3},
  33. {3,-1,-3},{3,0,-1},{3,0,1},{3,-1,3},
  34. };
  35.  
  36. mat4 w2v, projection;
  37. float alpha = 0;
  38. float beta = 0;
  39. float s;
  40. vec3 eye;
  41. vec3 target = {0.0,0.0,0.0};
  42. vec3 up = {0.0,1.0,0.0};
  43.  
  44. void initMatrices()
  45. {
  46. projection = perspective(centrum);
  47. vec2 windowSize = {4,4};
  48. vec2 windowPosition = {-2,2};
  49. vec2 viewportSize = {800, 800};
  50. vec2 viewportPosition = {0 , 800};
  51. w2v = windowToViewport3(windowPosition, windowSize, viewportPosition, viewportSize);
  52. }
  53.  
  54. void init()
  55. {
  56. glClearColor(1.0, 1.0, 1.0, 0.0);
  57. glMatrixMode(GL_PROJECTION);
  58. gluOrtho2D(0.0, winWidth, 0.0, winHeight);
  59. glShadeModel(GL_FLAT);
  60. glEnable(GL_POINT_SMOOTH);
  61. glLineWidth(3.0);
  62. glPointSize(10);
  63. initMatrices();
  64. }
  65.  
  66. float b(int id, float t)
  67. {
  68. switch(id)
  69. {
  70. case 0:{return -1*pow(t,3)+3*t*t-3*t+1;}break;
  71. case 1:{return 3*pow(t,3)-6*t*t+3*t;}break;
  72. case 2:{return -3*pow(t,3)+3*t*t;}break;
  73. case 3:{return pow(t,3);}break;
  74. }
  75. }
  76.  
  77.  
  78. void vaza(){
  79. glClear(GL_COLOR_BUFFER_BIT);
  80.  
  81. eye = { float(r * cos(beta)),alpha,float(r * sin(beta)) };
  82. glClear ( GL_COLOR_BUFFER_BIT );
  83. vec3 z = normalize(eye - target);
  84. vec3 x = normalize(cross(up, z));
  85. vec3 y = normalize(cross(z, x));
  86. mat4 kamera = coordinateTransform(eye,x,y,z);
  87.  
  88. float u, t;
  89. vec4 trans;
  90. vec4 homogen;
  91. FACE alap;
  92. std::vector<vec3> controllpoly;
  93.  
  94. vec3 temp=vec3{0,0,0};
  95.  
  96. for (s = 0; s<1; s += 0.1)
  97. {
  98. for (t = 0; t<1; t += 0.1)
  99. {
  100. for(int i=0;i<4;i++)
  101. {
  102. for(int j=0;j<4;j++)
  103. {
  104. temp+=points[i*4+j]*b(i,s)*b(j,t);
  105. }
  106. homogen = ihToH(temp);
  107. trans =kamera * homogen;
  108. controllpoly.push_back(hToIh(trans));
  109. }
  110. }
  111. /*alap.t[3] = { cos(u)*bx(t),by(t) ,-sin(u)*bx(t) };
  112. alap.t[2] = { cos(u)*bx(t+0.1),by(t + 0.1) ,-sin(u)*bx(t + 0.1) };
  113. alap.t[1] = { cos(u+lepes)*bx(t + 0.1),by(t + 0.1) ,-sin(u + lepes)*bx(t + 0.1) };
  114. alap.t[0] = { cos(u + lepes)*bx(t),by(t) ,-sin(u + lepes)*bx(t) };
  115. for (int i = 0; i < 4; i++)
  116. {
  117. homogen = ihToH(alap.t[i]);
  118. trans =kamera * homogen;
  119. alap.t[i] = hToIh(trans);
  120. }
  121. alap.normal = normalize(cross(alap.t[0] - alap.t[1], alap.t[0] - alap.t[3]));
  122. alap.suly = (alap.t[0] + alap.t[1] + alap.t[2] + alap.t[3]) / 4;
  123. alap.distance = dist(vec3(0, 0, centrum),alap.suly);
  124. faces.push_back(alap);
  125. }*/
  126. }
  127.  
  128. /*for (u = 0; u < 2 * pi(); u += lepes)
  129. {
  130. alap.t[0] = { 0,0,0 };
  131. alap.t[1] = { cos(u)*bx(0), by(0), -sin(u)*bx(0) };
  132. alap.t[2] = {cos(u + lepes)*bx(0), by(0), -sin(u + lepes)*bx(0)};
  133. alap.t[3] = { 0,0,0 };
  134. for (int i = 0; i < 4; i++)
  135. {
  136. homogen = ihToH(alap.t[i]);
  137. trans = kamera * homogen;
  138. alap.t[i] = hToIh(trans);
  139. }
  140. alap.normal = normalize(cross(alap.t[0] - alap.t[1], alap.t[0] - alap.t[2]));
  141. alap.suly = (alap.t[0] + alap.t[1] + alap.t[2] + alap.t[2]) / 4;
  142. alap.distance = dist(vec3(0, 0, centrum), alap.suly);
  143. alap.triangle = true;
  144. faces.push_back(alap);
  145. }
  146.  
  147. sort(faces.begin(), faces.end(), sorban1);
  148.  
  149. float shade;
  150. vec4 fenyk=transpose(inverse(kamera)) * ihToH(feny);
  151. vec3 fenyt = {fenyk.x, fenyk.y, fenyk.z};*/
  152. glColor3f(0, 0, 0);
  153. glBegin(GL_POINTS);
  154. for(int i=0;i<controllpoly.size();i++)
  155. {
  156. homogen = ihToH(controllpoly[i]);
  157. trans = w2v * projection * homogen;
  158. controllpoly[i] = hToIh(trans);
  159. glVertex2f(controllpoly[i].x,controllpoly[i].y);
  160. }
  161. glEnd();
  162. for (int i=0; i < faces.size(); i++)
  163. {
  164. alap = faces[i];
  165. glBegin(GL_POLYGON);
  166. if (alap.triangle)
  167. {
  168. for (int j = 0; j < 3; j++)
  169. {
  170. homogen = ihToH(alap.t[j]);
  171. trans = w2v * projection * homogen;
  172. alap.t[j] = hToIh(trans);
  173. glVertex2f(alap.t[j].x, alap.t[j].y);
  174. }
  175. }
  176. else
  177. {
  178. for (int j = 0; j < 4; j++)
  179. {
  180. homogen = ihToH(alap.t[j]);
  181. trans = w2v * projection * homogen;
  182. alap.t[j] = hToIh(trans);
  183. glVertex2f(alap.t[j].x, alap.t[j].y);
  184. }
  185. }
  186. glEnd();
  187.  
  188.  
  189.  
  190. glColor3f(0, 0, 0);
  191. glBegin(GL_LINE_LOOP);
  192. if (alap.triangle)
  193. {
  194. for (int j = 0; j < 3; j++)
  195. {
  196. glVertex2f(alap.t[j].x, alap.t[j].y);
  197. }
  198. glEnd();
  199. }
  200. else
  201. {
  202. for (int j = 0; j < 4; j++)
  203. {
  204. glVertex2f(alap.t[j].x, alap.t[j].y);
  205. }
  206. glEnd();
  207. }
  208. }
  209. faces.clear();
  210. }
  211.  
  212. void display() {
  213. glClear(GL_COLOR_BUFFER_BIT);
  214. glColor3f(0, 0, 0);
  215. vaza();
  216. glutSwapBuffers();
  217. }
  218.  
  219. //Billentyűparancsok
  220. void keyboard(unsigned char key, int x, int y)
  221. {
  222. switch (key) {
  223. //Kilépés
  224. case 27:
  225. exit(0);
  226. case 'w':
  227. alpha += 0.5;
  228. break;
  229. case 's':
  230. alpha -= 0.5;
  231. break;
  232. case 'a':
  233. beta -= 0.05;
  234. break;
  235. case 'd':
  236. beta += 0.05;
  237. break;
  238. case 'e':
  239. r += 0.5;
  240. break;
  241. case 'q':
  242. r -= 0.5;
  243. break;
  244. case 'r':
  245. centrum += 0.5;
  246. break;
  247. case 'f':
  248. centrum -= 0.5;
  249. break;
  250. }
  251. glutPostRedisplay();
  252. }
  253.  
  254. void update(int n)
  255. {
  256. glutPostRedisplay();
  257. glutTimerFunc(50, update, 0);
  258. }
  259.  
  260. int main(int argc, char** argv) {
  261. glutInit(&argc, argv);
  262. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  263. glutInitWindowSize(winWidth, winHeight);
  264. glutInitWindowPosition(100, 100);
  265. glutCreateWindow("Váza");
  266. init();
  267. glutDisplayFunc(display);
  268. glutKeyboardFunc(keyboard);
  269. glutTimerFunc(5, update, 0);
  270. glutMainLoop();
  271. return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement