Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.60 KB | None | 0 0
  1. //=============================================================================================
  2. // Triangle with smooth color and interactive polyline
  3. //=============================================================================================
  4. #define _USE_MATH_DEFINES
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. #include <vector>
  10.  
  11. #if defined(__APPLE__)
  12. #include <GLUT/GLUT.h>
  13. #include <OpenGL/gl3.h>
  14. #include <OpenGL/glu.h>
  15. #else
  16. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  17. #include <windows.h>
  18. #endif
  19. #include <GL/glew.h> // must be downloaded
  20. #include <GL/freeglut.h> // must be downloaded unless you have an Apple
  21. #endif
  22.  
  23. const unsigned int windowWidth = 600, windowHeight = 600;
  24.  
  25. // OpenGL major and minor versions
  26. int majorVersion = 3, minorVersion = 3;
  27.  
  28. void getErrorInfo(unsigned int handle) {
  29. int logLen;
  30. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLen);
  31. if (logLen > 0) {
  32. char * log = new char[logLen];
  33. int written;
  34. glGetShaderInfoLog(handle, logLen, &written, log);
  35. printf("Shader log:\n%s", log);
  36. delete log;
  37. }
  38. }
  39.  
  40. // check if shader could be compiled
  41. void checkShader(unsigned int shader, const char * message) {
  42. int OK;
  43. glGetShaderiv(shader, GL_COMPILE_STATUS, &OK);
  44. if (!OK) { printf("%s!\n", message); getErrorInfo(shader); }
  45. }
  46.  
  47. void gl_log_call(const char* stmt, const char* file, int line) {
  48. if (GLenum err = glGetError()) {
  49. printf("OpenGL error: %d in file '%s', at line %s, statement '%d'",
  50. int(err), file, line, stmt);
  51. }
  52. }
  53.  
  54. #define GLCall(x) \
  55. do { \
  56. while (glGetError() != GL_NO_ERROR); \
  57. x; \
  58. gl_log_call(#x, __FILE__, __LINE__); \
  59. } while (0)
  60.  
  61. // check if shader could be linked
  62. void checkLinking(unsigned int program) {
  63. int OK;
  64. glGetProgramiv(program, GL_LINK_STATUS, &OK);
  65. if (!OK) { printf("Failed to link shader program!\n"); getErrorInfo(program); }
  66. }
  67.  
  68. // vertex shader in GLSL
  69. const char * vertexSource = R"(
  70. #version 330
  71. precision highp float;
  72.  
  73. uniform mat4 MVP; // Model-View-Projection matrix in row-major format
  74.  
  75. layout(location = 0) in vec2 vertexPosition; // Attrib Array 0
  76. layout(location = 1) in vec3 vertexColor; // Attrib Array 1
  77. out vec3 color; // output attribute
  78.  
  79. void main() {
  80. color = vertexColor; // copy color from input to output
  81. gl_Position = vec4(vertexPosition.x, vertexPosition.y, 0, 1) * MVP; // transform to clipping space
  82. }
  83. )";
  84.  
  85. // fragment shader in GLSL
  86. const char * fragmentSource = R"(
  87. #version 330
  88. precision highp float;
  89.  
  90. in vec3 color; // variable input: interpolated color of vertex shader
  91. out vec4 fragmentColor; // output that goes to the raster memory as told by glBindFragDataLocation
  92.  
  93. void main() {
  94. fragmentColor = vec4(color, 1); // extend RGB to RGBA
  95. }
  96. )";
  97.  
  98. // row-major matrix 4x4
  99. struct mat4 {
  100. float m[4][4];
  101. public:
  102. mat4() {}
  103. mat4(float m00, float m01, float m02, float m03,
  104. float m10, float m11, float m12, float m13,
  105. float m20, float m21, float m22, float m23,
  106. float m30, float m31, float m32, float m33) {
  107. m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
  108. m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
  109. m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
  110. m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
  111. }
  112.  
  113. mat4 operator*(const mat4& right) {
  114. mat4 result;
  115. for (int i = 0; i < 4; i++) {
  116. for (int j = 0; j < 4; j++) {
  117. result.m[i][j] = 0;
  118. for (int k = 0; k < 4; k++) result.m[i][j] += m[i][k] * right.m[k][j];
  119. }
  120. }
  121. return result;
  122. }
  123.  
  124.  
  125. operator float*() { return &m[0][0]; }
  126.  
  127. };
  128.  
  129. // 3D point in homogeneous coordinates
  130.  
  131.  
  132. struct vec4 {
  133. float x, y, z, w;
  134. vec4(float _x = 0, float _y = 0, float _z = 0, float _w = 1) { x = _x; y = _y; z = _z; w = _w; }
  135. vec4 operator*(const mat4& mat) {
  136. return vec4(x * mat.m[0][0] + y * mat.m[1][0] + z * mat.m[2][0] + w * mat.m[3][0],
  137. x * mat.m[0][1] + y * mat.m[1][1] + z * mat.m[2][1] + w * mat.m[3][1],
  138. x * mat.m[0][2] + y * mat.m[1][2] + z * mat.m[2][2] + w * mat.m[3][2],
  139. x * mat.m[0][3] + y * mat.m[1][3] + z * mat.m[2][3] + w * mat.m[3][3]);
  140. }
  141. vec4 operator* (float f) {
  142. vec4 ret = *this;
  143. ret.x *= f;
  144. ret.y *= f;
  145. ret.z *= f;
  146. ret.w *= f;
  147. return ret;
  148. }
  149. vec4 const& operator+= (vec4 vec) {
  150. x += vec.x;
  151. y += vec.y;
  152. z += vec.z;
  153. w += vec.w;
  154. return *this;
  155. }
  156. vec4 operator-(vec4 const& right) {
  157. vec4 res(x- right.x, y - right.y, z - right.y);
  158. return res;
  159. }
  160. vec4 operator+(vec4 const& right) {
  161. vec4 res(x + right.x, y + right.y, z + right.y);
  162. return res;
  163. }
  164.  
  165. };
  166. struct vec3 {
  167. float v[3];
  168.  
  169. vec3(float x = 0, float y = 0, float z = 0)
  170. { v[0] = x; v[1] = y; v[2] = z; }
  171. vec3(const vec4& hom) { v[0] = hom.x / hom.w; v[1] = hom.y / hom.w; v[2] = hom.z / hom.w; }
  172.  
  173. vec3 operator*(float s) {
  174. vec3 res(v[0] * s, v[1] * s, v[2] * s);
  175. return res;
  176. }
  177.  
  178. };
  179.  
  180. // 2D point in Cartesian coordinates
  181. struct vec2 {
  182. float v[2];
  183.  
  184. vec2(float x = 0, float y = 0) { v[0] = x; v[1] = y; }
  185. vec2(const vec4& hom) { v[0] = hom.x / hom.w; v[1] = hom.y / hom.w; }
  186.  
  187. vec2 operator-(vec2& right) {
  188. vec2 res(v[0] - right.v[0], v[1] - right.v[1]);
  189. return res;
  190. }
  191.  
  192.  
  193. float Length() { return sqrtf(v[0] * v[0] + v[1] * v[1]); }
  194. };
  195.  
  196. float dot(const vec4& left, const vec4& right) {
  197. return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
  198. }
  199.  
  200. // 2D camera
  201. struct Camera {
  202. float wCx, wCy; // center in world coordinates
  203. float wWx, wWy; // width and height in world coordinates
  204. float n = -500;
  205. float p = 500;
  206. public:
  207. Camera() {
  208. Animate(0);
  209. }
  210.  
  211. mat4 V() { // view matrix: translates the center to the origin
  212. return
  213. mat4(1, 0, 0, 0,
  214. 0, 1, 0, 0,
  215. 0, 0, 1, 0,
  216. 0, 0, 0, 1);//-wCx, -wCy, 0, 1);
  217. }
  218.  
  219. mat4 P() { // projection matrix: scales it to be a square of edge length 2
  220. return mat4(2 / wWx, 0, 0, 0,
  221. 0, 2 / wWy, 0, 0,
  222. 0, 0, 1/(n-p), 0,
  223. 0, 0, (p+n)/(p-n), 1);
  224. }
  225.  
  226. mat4 Vinv() { // inverse view matrix
  227. return mat4(1, 0, 0, 0,
  228. 0, 1, 0, 0,
  229. 0, 0, 1, 0,
  230. wCx, wCy, 0, 1);
  231. }
  232.  
  233. mat4 Pinv() { // inverse projection matrix
  234. return mat4(wWx / 2, 0, 0, 0,
  235. 0, wWy / 2, 0, 0,
  236. 0, 0, 1, 0,
  237. 0, 0, 0, 1);
  238. }
  239.  
  240. void Animate(float t) {
  241. wCx = 0; // 10 * cosf(t);
  242. wCy = 0;
  243. wWx = 20;
  244. wWy = 20;
  245. }
  246. };
  247. struct Texture {
  248. unsigned int textureId;
  249. int width, height;
  250. vec3 *image;
  251. Texture(int w, int h, vec3* i) {
  252. width = w;
  253. height = h;
  254. image = i;
  255. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, image); //Texture -> GPU
  256. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  257. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  258. glGenTextures(1, &textureId);
  259. glBindTexture(GL_TEXTURE_2D, textureId); // binding
  260. }
  261. };
  262.  
  263. // 2D camera
  264. Camera camera;
  265.  
  266. // handle of the shader program
  267. unsigned int shaderProgram;
  268.  
  269. class Triangle {
  270. unsigned int vao; // vertex array object id
  271. float sx, sy; // scaling
  272. float wTx, wTy; // translation
  273. float phi;
  274.  
  275. public:
  276. Triangle() { Animate(0); }
  277.  
  278. void Create() {
  279. glGenVertexArrays(1, &vao); // create 1 vertex array object
  280. glBindVertexArray(vao); // make it active
  281.  
  282. unsigned int vbo[2]; // vertex buffer objects
  283. glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
  284.  
  285. // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  286. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  287. float vertexCoords[] = { -8, -8, -6, 10, 8, -2 }; // vertex data on the CPU
  288. glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
  289. sizeof(vertexCoords), // number of the vbo in bytes
  290. vertexCoords, // address of the data array on the CPU
  291. GL_STATIC_DRAW); // copy to that part of the memory which is not modified
  292. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  293. glEnableVertexAttribArray(0);
  294. // Data organization of Attribute Array 0
  295. glVertexAttribPointer(0, // Attribute Array 0
  296. 2, GL_FLOAT, // components/attribute, component type
  297. GL_FALSE, // not in fixed point format, do not normalized
  298. 0, NULL); // stride and offset: it is tightly packed
  299.  
  300. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  301. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  302. float vertexColors[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; // vertex data on the CPU
  303. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW); // copy to the GPU
  304.  
  305. // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  306. glEnableVertexAttribArray(1); // Vertex position
  307. // Data organization of Attribute Array 1
  308. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  309. }
  310.  
  311. void Animate(float t) {
  312. sx = 1; // sinf(t);
  313. sy = 1; // cosf(t);
  314. wTx = 1; // 4 * cosf(t / 2);
  315. wTy = 0; // 4 * sinf(t / 2);
  316. phi = t;
  317. }
  318.  
  319. void Draw() {
  320. mat4 Mscale(sx, 0, 0, 0,
  321. 0, sy, 0, 0,
  322. 0, 0, 0, 0,
  323. 0, 0, 0, 1); // model matrix
  324.  
  325. mat4 Mtranslate(1, 0, 0, 0,
  326. 0, 1, 0, 0,
  327. 0, 0, 0, 0,
  328. wTx, wTy, 0, 1); // model matrix
  329.  
  330. mat4 Mrotate(cosf(phi), sinf(phi), 0, 0,
  331. -sinf(phi), cosf(phi), 0, 0,
  332. 0, 0, 1, 0,
  333. 0, 0, 0, 1); // model matrix
  334.  
  335. //mat4 Mshift(1, 0, 0, 0,
  336. // 0, 1, 0, 0,
  337. // X, Y, 0, 0,
  338. // 0, 0, 0, 1);
  339.  
  340. mat4 MVPTransform = Mscale * Mtranslate * Mrotate * camera.V() * camera.P();
  341.  
  342. // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
  343. int location = glGetUniformLocation(shaderProgram, "MVP");
  344. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
  345. else printf("uniform MVP cannot be set\n");
  346.  
  347. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  348. glDrawArrays(GL_TRIANGLES, 0, 3); // draw a single triangle with vertices defined in vao
  349. }
  350. };
  351.  
  352. //##########################################################################################################
  353.  
  354. class Circle {
  355. unsigned int vao; // vertex array object id
  356. float sx, sy; // scaling
  357. float wTx, wTy; // translation
  358. float density = 0.1; //kör pontjainak sűrűsége
  359. int dotCount;
  360. vec4 origo;
  361. float size;
  362. float rot;
  363. vec4 speed;
  364. vec4 movement;
  365.  
  366. public:
  367. Circle(float siz = 4, vec4 o = (0, 0)) {
  368. size = siz;
  369. origo = o;
  370. Animate(0, 0);
  371. }
  372.  
  373. vec4 getOrigo() {
  374. return origo;
  375. }
  376.  
  377. void setOrigo(vec4 o) {
  378. origo = o;
  379. }
  380.  
  381. void Create() {
  382. glGenVertexArrays(1, &vao); // create 1 vertex array object
  383. glBindVertexArray(vao); // make it active
  384.  
  385. unsigned int vbo[2]; // vertex buffer objects
  386. glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
  387.  
  388. // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  389. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  390.  
  391.  
  392. std::vector<vec4> points;
  393. vec4 pre(0, 0);
  394. for (float i = 0; i <= 2*M_PI; i=i+density) {
  395. vec4 point(origo);
  396. point.x += cosf(i)*size;
  397. point.y += sinf(i)*size;
  398.  
  399. if (i > 0) {
  400. points.push_back(point);
  401. points.push_back(origo);
  402. points.push_back(pre);
  403. }
  404. pre = point;
  405. }
  406. points.push_back(pre);
  407. points.push_back(origo);
  408. points.push_back(points[0]);
  409.  
  410. dotCount = points.size();
  411. for (int i = 0; i < points.size(); i++) {
  412. //printf("%f %f\n", points[i].x, points[i].y);
  413. }
  414.  
  415. //printf("\n%d ", dotCount);
  416.  
  417. glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
  418. points.size()*sizeof(vec4), // number of the vbo in bytes
  419. &points[0], // address of the data array on the CPU
  420. GL_STATIC_DRAW); // copy to that part of the memory which is not modified
  421. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  422. glEnableVertexAttribArray(0);
  423. // Data organization of Attribute Array 0
  424. glVertexAttribPointer(0, // Attribute Array 0
  425. 4, GL_FLOAT, // components/attribute, component type vec4
  426. GL_FALSE, // not in fixed point format, do not normalized
  427. 0, NULL); // stride and offset: it is tightly packed
  428.  
  429. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  430. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  431. std::vector<vec4> colors;
  432. vec4 color(0, 0, 0);
  433. for (int i = 0; i < dotCount; i++) {
  434. colors.push_back(color);
  435. } // vertex data on the CPU
  436. glBufferData(GL_ARRAY_BUFFER, sizeof(vec4)*colors.size(), &colors[0], GL_STATIC_DRAW); // copy to the GPU
  437.  
  438. // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  439. glEnableVertexAttribArray(1); // Vertex position
  440. // Data organization of Attribute Array 1
  441. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  442. }
  443.  
  444. void Animate(float t, vec4 sp) {
  445. sx = 1; //sinf(t);
  446. sy = 1; // cosf(t);
  447. wTx = 0; // 4 * cosf(t / 2);
  448. wTy = 0; // 4 * sinf(t / 2
  449. speed = sp;
  450. }
  451.  
  452. void Draw(float rate){
  453. rot = atan2f(speed.x, speed.y);
  454.  
  455. mat4 Mscale(rate, 0, 0, 0,
  456. 0, sy, 0, 0,
  457. 0, 0, 0, 0,
  458. 0, 0, 0, 1); // model matrix
  459.  
  460. mat4 Mtranslate(1, 0, 0, 0,
  461. 0, 1, 0, 0,
  462. 0, 0, 0, 0,
  463. wTx, wTy, 0, 1); // model matrix
  464.  
  465. mat4 MrotateY(
  466. cosf(atan2f(speed.x, speed.y)), 0, sinf(atan2f(speed.x, speed.y)), 0,
  467. 0, 1, 0, 0,
  468. -sinf(atan2f(speed.x, speed.y)), 0, cosf(atan2f(speed.x, speed.y)), 0,
  469. 0, 0, 0, 1);
  470.  
  471. mat4 MrotateZ(
  472. cosf(rot), -sin(rot), 0, 0,
  473. sinf(rot), cosf(rot), 0, 0,
  474. 0, 0, 1, 0,
  475. 0, 0, 0, 1);
  476.  
  477. mat4 MVPTransform = Mscale * Mtranslate * MrotateZ * camera.V() * camera.P();
  478.  
  479. // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
  480. int location = glGetUniformLocation(shaderProgram, "MVP");
  481. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
  482. else printf("uniform MVP cannot be set\n");
  483.  
  484. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  485. glDrawArrays(GL_TRIANGLES, 0, dotCount); // draw a single triangle with vertices defined in vao dotCount szamu haromszog,
  486.  
  487. }
  488. };
  489.  
  490. //##########################################################################################################
  491.  
  492. vec4 mouse_pos;
  493.  
  494. class Wing {
  495.  
  496. unsigned int vao; // vertex array object id
  497. float sx, sy; // scaling
  498. float wTx, wTy; // translation
  499. int dotCount;
  500. float phi;
  501. float rot;
  502. vec4 speed;
  503. vec4 movement;
  504.  
  505.  
  506. std::vector<vec4> cps; // control pts
  507.  
  508. float B(int i, float t) {
  509. int n = cps.size() - 1; // n deg polynomial = n+1 pts!
  510. float choose = 1;
  511. for (int j = 1; j <= i; j++) choose *= (float)(n - j + 1) / j;
  512. return choose * pow(t, i) * pow(1 - t, n - i);
  513. }
  514. public:
  515. void Add(vec4 cp) { cps.push_back(cp); }
  516.  
  517.  
  518.  
  519. vec4 r(float t) {
  520. vec4 rr(0, 0, 0);
  521. for (int i = 0; i < cps.size(); i++) {
  522. rr += cps[i] * B(i, t);
  523. }
  524. return rr;
  525. }
  526.  
  527.  
  528. void Create() {
  529. glGenVertexArrays(1, &vao); // create 1 vertex array object
  530. glBindVertexArray(vao); // make it active
  531.  
  532. unsigned int vbo[2]; // vertex buffer objects
  533. glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
  534.  
  535. // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  536. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  537.  
  538. vec4 root(0.0, 0.0);
  539. vec4 point(root);
  540. Add(point); //Add(point);
  541. Add(point += vec4(1.3, 1.4)); //Add(vec4(1.8, 4.2));
  542. Add(point += vec4(1.2, -1.4)); //Add(vec4(3.0, 2.8));
  543. Add(point += vec4(1.2, -1.4)); //Add(vec4(4.2, 1.4));
  544. Add(point += vec4(1.4, -1.4)); //Add(vec4(5.6, 0.0));
  545. Add(point += vec4(-1.2, -0.4)); //Add(vec4(4.4, -1.6));
  546. Add(point += vec4(-3.0, -0.4)); //Add(vec4(1.4, -0.8));
  547. Add(point += vec4(0.5, -0.4)); //Add(vec4(1.9, -1.2));
  548. Add(point += vec4(1.2, -1.4)); //Add(vec4(3.1, -2.6));
  549. Add(point += vec4(-1.0, -1.4)); //Add(vec4(2.1, -4.0));
  550. Add(point += vec4(-0.2, -1.2)); //Add(vec4(1.9, -5.2));
  551. //Add(vec4(2.8, -4.2));
  552. Add(point += vec4(2.4, 1.4)); //Add(vec4(4.3, -3.8));
  553. Add(point += vec4(-1.2, -0.4)); //Add(vec4(3.1, -4.2));
  554. Add(point += vec4(-0.6, -0.4)); //Add(vec4(2.5, -4.6));
  555. Add(point += vec4(-1.3, -0.4)); //Add(vec4(1.2, -5.0));
  556. Add(point += vec4(0.4, -0, 4)); // Add(vec4(1.6, -6.4));
  557. Add(point += vec4(0.4, 0.6)); //Add(vec4(2.0, -5.8));
  558. Add(point += vec4(-2.0, -0.8)); //Add(vec4(0, -6.6));
  559. Add(root);
  560.  
  561.  
  562.  
  563. //float width = 0.05;
  564. std::vector<vec4> line;
  565. std::vector<vec4> tex;
  566. vec4 pre(0, 0);
  567. float max = 0;
  568. float min = 0;
  569.  
  570. for (float i = 0; i < 1; i = i + 0.01) {
  571. //vec4 temp(i, i);
  572. vec4 temp(r(i));
  573. if (r(i).y > max)
  574. max = r(i).y;
  575. if (r(i).y < min)
  576. min = r(i).y;
  577. //float cosA = (temp.x - pre.x) / sqrt((temp.y - pre.y)*(temp.y - pre.y) + (temp.x - pre.x)*(temp.x - pre.x));
  578. //float sinA = (temp.y - pre.y) / sqrt((temp.y - pre.y)*(temp.y - pre.y) + (temp.x - pre.x)*(temp.x - pre.x));
  579.  
  580. if (i > 0) {
  581. line.push_back(r(0));
  582. line.push_back(pre);
  583. line.push_back(temp);
  584.  
  585. /*vec4 dot1, dot2, dot3, dot4;
  586. dot1.y = temp.y + (width / 2) * cosA;
  587. dot1.x = temp.x - (width / 2) * sinA;
  588. dot2.y = temp.y - (width / 2) * cosA;
  589. dot2.x = temp.x + (width / 2) * sinA;
  590. dot3.y = pre.y - (width / 2) * cosA;
  591. dot3.x = pre.x + (width / 2) * sinA;
  592. dot4.y = pre.y + (width / 2) * cosA;
  593. dot4.x = pre.x - (width / 2) * sinA;
  594. line.push_back(dot1);
  595. line.push_back(dot2);
  596. line.push_back(dot3);
  597. line.push_back(dot1);
  598. line.push_back(dot3);
  599. line.push_back(dot4);*/
  600. }
  601. pre = temp;
  602. }
  603.  
  604. int width = 128;
  605. int height = 128;
  606. vec3 * image = new vec3[width * height];
  607. for (int y = 0; y < height; y++) {
  608. for (int x = 0; x < width; x++) {
  609. float luminance = ((x / 16) % 2) ^ ((y / 16) % 2);
  610. int temp = y * width;
  611. image[temp + x] = vec3(luminance, luminance, luminance);
  612. }
  613. }
  614.  
  615. Texture texture(width, height, image);
  616.  
  617. for (int i = 0; i < line.size(); i++) {
  618. tex.push_back(vec4(line[i].x / ((0-min) + max), (line[i].y + (0-min)) / ((0-min) + max)));
  619. }
  620. //for (int i = 0; i < line.size(); i++) {
  621. // printf("%f %f\n", tex[i].x, tex[i].y);
  622. //}
  623.  
  624. /*
  625. int sampler = 0;
  626. int location = glGetUniformLocation(shaderProg, "samplerUnit");
  627. glUniform1i(location, sampler);
  628. glActiveTexture(GL_TEXTURE0 + sampler);
  629. glBindTexture(GL_TEXTURE_2D, texture.textureId);
  630.  
  631. glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, tex.size());
  632. */
  633.  
  634. dotCount = line.size();
  635.  
  636. //float vertexCoords[] = { -8, -8, -6, 10, 8, -2 }; // vertex data on the CPU
  637. GLCall(glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
  638. line.size() * sizeof(vec4), // number of the vbo in bytes
  639. &line[0], // address of the data array on the CPU
  640. GL_STATIC_DRAW)); // copy to that part of the memory which is not modified
  641. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  642. glEnableVertexAttribArray(0);
  643. // Data organization of Attribute Array 0
  644. glVertexAttribPointer(0, // Attribute Array 0
  645. 4, GL_FLOAT, // components/attribute, component type vec4
  646. GL_FALSE, // not in fixed point format, do not normalized
  647. 0, NULL); // stride and offset: it is tightly packed
  648.  
  649. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  650. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  651.  
  652. std::vector<vec4> colors;
  653. vec4 color(0.6, 0.3, 0.4);
  654. for (int i = 0; i < dotCount; i++) {
  655. colors.push_back(color);
  656. } // vertex data on the CPU
  657. GLCall(glBufferData(GL_ARRAY_BUFFER, sizeof(vec4)*colors.size(), &colors[0], GL_STATIC_DRAW)); // copy to the GPU
  658.  
  659. // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  660. glEnableVertexAttribArray(1); // Vertex position
  661. // Data organization of Attribute Array 1
  662. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  663. }
  664.  
  665. // NOTE: FORCE APPLY
  666.  
  667. void Animate(float t, float dt) {
  668. sx = 1; //sinf(2*t);
  669. sy = 1; // cosf(t);
  670. wTx = 0.2; // 4 * cosf(t / 2);
  671. wTy = 3; // 4 * sinf(t / 2);
  672. phi = sin(3*t);
  673.  
  674. // Body movement
  675. }
  676.  
  677. void Draw() {
  678. rot = atan2f(speed.x, speed.y);
  679.  
  680.  
  681. mat4 Mscale(sx, 0, 0, 0,
  682. 0, sy, 0, 0,
  683. 0, 0, 0, 0,
  684. 0, 0, 0, 1); // model matrix
  685.  
  686. mat4 Mtranslate(1, 0, 0, 0,
  687. 0, 1, 0, 0,
  688. 0, 0, 0, 0,
  689. wTx, wTy, 0, 1); // model matrix
  690.  
  691. mat4 Mtranslate2(1, 0, 0, 0,
  692. 0, 1, 0, 0,
  693. 0, 0, 0, 0,
  694. speed.x, speed.y, 0, 1); // model matrix
  695.  
  696. mat4 MrotateY(
  697. cosf(phi), 0, sinf(phi), 0,
  698. 0, 1, 0, 0,
  699. -sinf(phi), 0, cosf(phi), 0,
  700. 0, 0, 0, 1);
  701.  
  702. mat4 MrotateZ(
  703. cosf(rot), -sinf(rot), 0, 0,
  704. sinf(rot), cosf(rot), 0, 0,
  705. 0, 0, 1, 0,
  706. 0, 0, 0, 1);
  707.  
  708. mat4 Mmirror(-1, 0, 0, 0,
  709. 0, 1, 0, 0,
  710. 0, 0, 1, 0,
  711. 0, 0, 0, 1); // model matrix
  712.  
  713. mat4 MVPTransform = Mscale * Mtranslate * MrotateY * MrotateZ*Mtranslate2 * camera.V() * camera.P();
  714. mat4 MVPTransform2 = Mscale * Mtranslate * Mmirror * MrotateY * MrotateZ *Mtranslate2* camera.V() * camera.P();
  715.  
  716. // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
  717. int location = glGetUniformLocation(shaderProgram, "MVP");
  718. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
  719. else printf("uniform MVP cannot be set\n");
  720.  
  721. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  722. GLCall(glDrawArrays(GL_TRIANGLES, 0, dotCount)); // draw a single triangle with vertices defined in vao dotCount szamu haromszog,
  723.  
  724. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform2); // set uniform variable MVP to the MVPTransform
  725. else printf("uniform MVP cannot be set\n");
  726.  
  727. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  728. GLCall(glDrawArrays(GL_TRIANGLES, 0, dotCount)); // draw a single triangle with vertices defined in vao dotCount szamu haromszog,
  729.  
  730. }
  731. };
  732.  
  733. class Butterfly {
  734.  
  735. unsigned int vao; // vertex array object id
  736. float sx, sy; // scaling
  737. float wTx, wTy; // translation
  738. float phi;
  739. Wing wing;
  740. Circle circle;
  741. vec4 speed;
  742. vec4 accel;
  743. float weight;
  744.  
  745. vec4 springForce;
  746. float windResistance;
  747. float SpringConstant;
  748.  
  749.  
  750. public:
  751.  
  752. Butterfly() {
  753. weight = 1;
  754. springForce = vec4(0, 0);
  755. SpringConstant = 20;
  756. windResistance = 0.0005;
  757. speed = vec4(0, 0);
  758. accel = vec4(0, 0);
  759. circle = Circle(3.5);
  760. Animate(0);
  761. }
  762.  
  763. float getWindResistance() {
  764. return windResistance;
  765. }
  766.  
  767. void setAccel(vec4 acc) {
  768. accel = acc;
  769. }
  770.  
  771. vec4 getAccel() {
  772. return accel;
  773. }
  774.  
  775. float getWeight() {
  776. return weight;
  777. }
  778.  
  779. float getSpringConstant() {
  780. return SpringConstant;
  781. }
  782.  
  783. vec4 getSpringForce() {
  784. return springForce;
  785. }
  786. void setSpringForce(vec4 force) {
  787. springForce = force;
  788. }
  789.  
  790. vec4 getSpeed() {
  791. return speed;
  792. }
  793.  
  794. void setSpeed(vec4 sp) {
  795. speed = sp;
  796. printf("%f %f\n", speed.x, speed.y);
  797. }
  798.  
  799. vec4 getPosition() {
  800. return circle.getOrigo();
  801. }
  802.  
  803. void Create() {
  804. circle.Create();
  805. wing.Create();
  806. }
  807.  
  808. void Animate(float t) {
  809. circle.Animate(t, speed);
  810. wing.Animate(t, speed);
  811. }
  812.  
  813. void Draw() {
  814. circle.Draw(0.15);
  815. wing.Draw();
  816. }
  817. };
  818.  
  819. bool isGPUProcedural = false;
  820.  
  821.  
  822.  
  823. class Leaf {
  824. unsigned int vao; // vertex array object id
  825. float sx, sy; // scaling
  826. float wTx, wTy; // translation
  827. float density = 0.1; //kör pontjainak sűrűsége
  828. int dotCount;
  829. vec4 origo;
  830. float phi;
  831. vec4 circleOrigo;
  832.  
  833.  
  834. public:
  835. Leaf(float angle, vec4 circleo){
  836. phi = angle;
  837. circleOrigo = circleo;
  838. origo = vec4(0, 1);
  839. Animate(0);
  840. }
  841.  
  842. void Create() {
  843. glGenVertexArrays(1, &vao); // create 1 vertex array object
  844. glBindVertexArray(vao); // make it active
  845.  
  846. unsigned int vbo[2]; // vertex buffer objects
  847. glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
  848.  
  849. // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  850. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  851.  
  852.  
  853. std::vector<vec4> points;
  854. vec4 pre(origo);
  855. for (float i = 0; i <= 2 * M_PI; i = i + density) {
  856. vec4 point(origo);
  857. point.x += cosf(i);
  858. point.y += sinf(i);
  859.  
  860. if (i > 0) {
  861. points.push_back(point);
  862. points.push_back(origo);
  863. points.push_back(pre);
  864. }
  865. pre = point;
  866. }
  867. points.push_back(pre);
  868. points.push_back(origo);
  869. points.push_back(points[0]);
  870.  
  871. dotCount = points.size();
  872.  
  873. glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
  874. points.size() * sizeof(vec4), // number of the vbo in bytes
  875. &points[0], // address of the data array on the CPU
  876. GL_STATIC_DRAW); // copy to that part of the memory which is not modified
  877. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  878. glEnableVertexAttribArray(0);
  879. // Data organization of Attribute Array 0
  880. glVertexAttribPointer(0, // Attribute Array 0
  881. 4, GL_FLOAT, // components/attribute, component type vec4
  882. GL_FALSE, // not in fixed point format, do not normalized
  883. 0, NULL); // stride and offset: it is tightly packed
  884.  
  885. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  886. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  887. std::vector<vec4> colors;
  888. vec4 color(1, 1, 0);
  889. for (int i = 0; i < dotCount; i++) {
  890. colors.push_back(color);
  891. } // vertex data on the CPU
  892. glBufferData(GL_ARRAY_BUFFER, sizeof(vec4)*colors.size(), &colors[0], GL_STATIC_DRAW); // copy to the GPU
  893.  
  894. // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  895. glEnableVertexAttribArray(1); // Vertex position
  896. // Data organization of Attribute Array 1
  897. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  898. }
  899.  
  900. void Animate(float t) {
  901. sx = 0.25; //sinf(t);
  902. sy = 1; // cosf(t);
  903. wTx = 0; // 4 * cosf(t / 2);
  904. wTy = 0; // 4 * sinf(t / 2);
  905. }
  906.  
  907. void Draw() {
  908. mat4 Mscale(sx, 0, 0, 0,
  909. 0, sy, 0, 0,
  910. 0, 0, 0, 0,
  911. 0, 0, 0, 1); // model matrix
  912.  
  913. mat4 Mtranslate(1, 0, 0, 0,
  914. 0, 1, 0, 0,
  915. 0, 0, 0, 0,
  916. wTx, wTy, 0, 1); // model matrix
  917.  
  918.  
  919. mat4 Mrotate(cosf(phi), sinf(phi), 0, 0,
  920. -sinf(phi), cosf(phi), 0, 0,
  921. 0, 0, 1, 0,
  922. circleOrigo.x, circleOrigo.y, 0, 1); // model matrix
  923.  
  924.  
  925. mat4 MVPTransform = Mscale * Mtranslate * Mrotate * camera.V() * camera.P();
  926.  
  927. // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
  928. int location = glGetUniformLocation(shaderProgram, "MVP");
  929. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
  930. else printf("uniform MVP cannot be set\n");
  931.  
  932. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  933. glDrawArrays(GL_TRIANGLES, 0, dotCount); // draw a single triangle with vertices defined in vao dotCount szamu haromszog,
  934.  
  935. }
  936. };
  937.  
  938.  
  939. class Flower {
  940. unsigned int vao; // vertex array object id
  941. float sx, sy; // scaling
  942. float wTx, wTy; // translation
  943. vec4 origo;
  944. std::vector<Leaf> leafs;
  945. int petalCount;
  946. Circle circle;
  947.  
  948.  
  949. public:
  950. Flower(vec4 o, int pc) {
  951. origo = o;
  952. Circle c(1, o);
  953. circle = c;
  954. petalCount = pc;
  955. printf("circle origo: %f %f\n", origo.x, origo.y);
  956. for (int i = 0; i < petalCount; i++) {
  957. vec4 temp(1, 1);
  958. temp.y += sinf(i * 2 * M_PI / petalCount);
  959. temp.x += cosf(i * 2 * M_PI / petalCount);
  960. Leaf leaf(i * 2 * M_PI / petalCount, origo);
  961. leafs.push_back(leaf);
  962. //printf("petal origo: %f %f angle: %f\n", temp.x, temp.y, i * 2 * M_PI / petalCount);
  963.  
  964. }
  965. Animate(0);
  966. }
  967.  
  968. void Create() {
  969. for (int i = 0; i < petalCount; i++) {
  970. leafs[i].Create();
  971. }
  972. circle.Create();
  973.  
  974. }
  975.  
  976. void Animate(float t) {
  977. for (int i = 0; i < petalCount; i++) {
  978. leafs[i].Animate(t);
  979. }
  980. circle.Animate(t, 0);
  981. }
  982.  
  983. void Draw() {
  984. for (int i = 0; i < petalCount; i++) {
  985. leafs[i].Draw();
  986. }
  987. circle.Draw(1);
  988. }
  989. };
  990.  
  991.  
  992. class LineStrip {
  993. GLuint vao, vbo; // vertex array object, vertex buffer object
  994. float vertexData[100]; // interleaved data of coordinates and colors
  995. int nVertices; // number of vertices
  996. public:
  997. LineStrip() {
  998. nVertices = 0;
  999. }
  1000. void Create() {
  1001. glGenVertexArrays(1, &vao);
  1002. glBindVertexArray(vao);
  1003.  
  1004. glGenBuffers(1, &vbo); // Generate 1 vertex buffer object
  1005. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  1006. // Enable the vertex attribute arrays
  1007. glEnableVertexAttribArray(0); // attribute array 0
  1008. glEnableVertexAttribArray(1); // attribute array 1
  1009. // Map attribute array 0 to the vertex data of the interleaved vbo
  1010. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(0)); // attribute array, components/attribute, component type, normalize?, stride, offset
  1011. // Map attribute array 1 to the color data of the interleaved vbo
  1012. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(2 * sizeof(float)));
  1013. }
  1014.  
  1015. void AddPoint(float cX, float cY) {
  1016. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  1017. if (nVertices >= 20) return;
  1018.  
  1019. vec4 wVertex = vec4(cX, cY, 0, 1) * camera.Pinv() * camera.Vinv();
  1020. // fill interleaved data
  1021. vertexData[5 * nVertices] = wVertex.x;
  1022. vertexData[5 * nVertices + 1] = wVertex.y;
  1023. vertexData[5 * nVertices + 2] = 1; // red
  1024. vertexData[5 * nVertices + 3] = 1; // green
  1025. vertexData[5 * nVertices + 4] = 0; // blue
  1026. nVertices++;
  1027. // copy data to the GPU
  1028. glBufferData(GL_ARRAY_BUFFER, nVertices * 5 * sizeof(float), vertexData, GL_DYNAMIC_DRAW);
  1029. }
  1030.  
  1031. void Draw() {
  1032. if (nVertices > 0) {
  1033. mat4 VPTransform = camera.V() * camera.P();
  1034.  
  1035. int location = glGetUniformLocation(shaderProgram, "MVP");
  1036. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, VPTransform);
  1037. else printf("uniform MVP cannot be set\n");
  1038.  
  1039. glBindVertexArray(vao);
  1040. glDrawArrays(GL_LINE_STRIP, 0, nVertices);
  1041. }
  1042. }
  1043. };
  1044.  
  1045. // The virtual world: collection of two objects
  1046. Triangle triangle;
  1047. LineStrip lineStrip;
  1048. Circle circle(4, vec4(6, 0));
  1049. Wing wing, wing1;
  1050. //TexturedQuad quad;
  1051.  
  1052. Flower flower1(vec4(8, 8), 30);
  1053. Flower flower2(vec4(4, 6), 10);
  1054. Flower flower3(vec4(3, 1), 12);
  1055. Flower flower4(vec4(-3, -2), 3);
  1056. Butterfly butt;
  1057.  
  1058. // Initialization, create an OpenGL context
  1059. void onInitialization() {
  1060. glViewport(0, 0, windowWidth, windowHeight);
  1061.  
  1062. // Create objects by setting up their vertex data on the GPU
  1063.  
  1064. flower1.Create();
  1065. flower2.Create();
  1066. flower3.Create();
  1067. flower4.Create();
  1068. butt.Create();
  1069. //quad.Create();
  1070.  
  1071. // Create vertex shader from string
  1072. unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  1073. if (!vertexShader) {
  1074. printf("Error in vertex shader creation\n");
  1075. exit(1);
  1076. }
  1077. glShaderSource(vertexShader, 1, &vertexSource, NULL);
  1078. glCompileShader(vertexShader);
  1079. checkShader(vertexShader, "Vertex shader error");
  1080.  
  1081. // Create fragment shader from string
  1082. unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  1083. if (!fragmentShader) {
  1084. printf("Error in fragment shader creation\n");
  1085. exit(1);
  1086. }
  1087. glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  1088. glCompileShader(fragmentShader);
  1089. checkShader(fragmentShader, "Fragment shader error");
  1090.  
  1091. // Attach shaders to a single program
  1092. shaderProgram = glCreateProgram();
  1093. if (!shaderProgram) {
  1094. printf("Error in shader program creation\n");
  1095. exit(1);
  1096. }
  1097. glAttachShader(shaderProgram, vertexShader);
  1098. glAttachShader(shaderProgram, fragmentShader);
  1099.  
  1100. // Connect the fragmentColor to the frame buffer memory
  1101. glBindFragDataLocation(shaderProgram, 0, "fragmentColor"); // fragmentColor goes to the frame buffer memory
  1102.  
  1103. // program packaging
  1104. glLinkProgram(shaderProgram);
  1105. checkLinking(shaderProgram);
  1106. // make this program run
  1107. glUseProgram(shaderProgram);
  1108. }
  1109.  
  1110. void onExit() {
  1111. glDeleteProgram(shaderProgram);
  1112. printf("exit");
  1113. }
  1114.  
  1115. // Window has become invalid: Redraw
  1116. void onDisplay() {
  1117. glClearColor(0.02, 1, 0.01, 1.0); // background color
  1118. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen
  1119.  
  1120.  
  1121. flower1.Draw();
  1122. //flower2.Draw();
  1123. //flower3.Draw();
  1124. //flower4.Draw();
  1125. butt.Draw();
  1126. //quad.Draw();
  1127. glutSwapBuffers(); // exchange the two buffers
  1128. }
  1129.  
  1130. // Key of ASCII code pressed
  1131. void onKeyboard(unsigned char key, int pX, int pY) {
  1132. if (key == 'd') glutPostRedisplay(); // if d, invalidate display, i.e. redraw
  1133. if (key == ' ') {
  1134. isGPUProcedural = !isGPUProcedural;
  1135. glutPostRedisplay(); // if d, invalidate display, i.e. redraw
  1136. }
  1137. }
  1138.  
  1139. // Key of ASCII code released
  1140. void onKeyboardUp(unsigned char key, int pX, int pY) {
  1141.  
  1142. }
  1143.  
  1144.  
  1145. bool mouseLeftPressed = false;
  1146. bool mouseRightPressed = false;
  1147.  
  1148.  
  1149. // Move mouse with key pressed
  1150. void onMouseMotion(int pX, int pY) {
  1151. float cX = 2.0f * pX / windowWidth - 1; // flip y axis
  1152. float cY = 1.0f - 2.0f * pY / windowHeight;
  1153. if (mouseLeftPressed) { // GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON and GLUT_DOWN / GLUT_UP
  1154.  
  1155.  
  1156.  
  1157. }
  1158. if (mouseRightPressed) { // GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON and GLUT_DOWN / GLUT_UP
  1159.  
  1160. }
  1161.  
  1162. glutPostRedisplay(); // redraw
  1163. }
  1164.  
  1165. // Mouse click event
  1166. void onMouse(int button, int state, int pX, int pY) {
  1167. float cX = 2.0f * pX / windowWidth - 1; // flip y axis
  1168. float cY = 1.0f - 2.0f * pY / windowHeight;
  1169. if (button == GLUT_LEFT_BUTTON) {
  1170.  
  1171.  
  1172. if (state == GLUT_DOWN){
  1173. cX = 2.0f * pX / windowWidth - 1; // flip y axis
  1174. cY = 1.0f - 2.0f * pY / windowHeight;
  1175. mouseLeftPressed = true;
  1176. mouse_pos = vec4(cX, cY);
  1177.  
  1178. }
  1179. else{
  1180. mouseLeftPressed = false;
  1181. //butt.setAccel(vec4(0, 0));}
  1182. }
  1183. if (button == GLUT_RIGHT_BUTTON) {
  1184. if (state == GLUT_DOWN) mouseRightPressed = true;
  1185. else mouseRightPressed = false;
  1186. }
  1187.  
  1188. onMouseMotion(pX, pY);
  1189. }
  1190.  
  1191. // Idle event indicating that some time elapsed: do animation here
  1192. float timeSinceUpdate;
  1193. void onIdle() {
  1194. long time = glutGet(GLUT_ELAPSED_TIME); // elapsed time since the start of the program
  1195. float sec = time / 1000.0f; // convert msec to sec
  1196. camera.Animate(sec); // animate the camera
  1197.  
  1198. flower1.Animate(sec);
  1199. flower2.Animate(sec);
  1200. flower3.Animate(sec);
  1201. flower4.Animate(sec);
  1202.  
  1203. //butt.Animate(sec);
  1204. //butt.setSpeed(( butt.getSpeed() + butt.getAccel() * (sec-timeSinceUpdate)) * (1 - butt.getWindResistance()));
  1205. timeSinceUpdate = sec;
  1206. glutPostRedisplay(); // redraw the scene
  1207. }
  1208.  
  1209. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1210. // Do not touch the code below this line
  1211.  
  1212. int main(int argc, char * argv[]) {
  1213. glutInit(&argc, argv);
  1214. #if !defined(__APPLE__)
  1215. glutInitContextVersion(majorVersion, minorVersion);
  1216. #endif
  1217. glutInitWindowSize(windowWidth, windowHeight); // Application window is initially of resolution 600x600
  1218. glutInitWindowPosition(100, 100); // Relative location of the application window
  1219. #if defined(__APPLE__)
  1220. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_3_CORE_PROFILE); // 8 bit R,G,B,A + double buffer + depth buffer
  1221. #else
  1222. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  1223. #endif
  1224. glutCreateWindow(argv[0]);
  1225.  
  1226. #if !defined(__APPLE__)
  1227. glewExperimental = true; // magic
  1228. glewInit();
  1229. #endif
  1230.  
  1231. printf("GL Vendor : %s\n", glGetString(GL_VENDOR));
  1232. printf("GL Renderer : %s\n", glGetString(GL_RENDERER));
  1233. printf("GL Version (string) : %s\n", glGetString(GL_VERSION));
  1234. glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
  1235. glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
  1236. printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
  1237. printf("GLSL Version : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
  1238.  
  1239. onInitialization();
  1240.  
  1241. glutDisplayFunc(onDisplay); // Register event handlers
  1242. glutMouseFunc(onMouse);
  1243. glutIdleFunc(onIdle);
  1244. glutKeyboardFunc(onKeyboard);
  1245. glutKeyboardUpFunc(onKeyboardUp);
  1246. glutMotionFunc(onMouseMotion);
  1247.  
  1248. glutMainLoop();
  1249. onExit();
  1250. return 1;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement