Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.48 KB | None | 0 0
  1. //=============================================================================================
  2. // Szamitogepes grafika hazi feladat keret. Ervenyes 2018-tol.
  3. // A //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. // sorokon beluli reszben celszeru garazdalkodni, mert a tobbit ugyis toroljuk.
  5. // A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat, BOM kihuzando.
  6. // Tilos:
  7. // - mast "beincludolni", illetve mas konyvtarat hasznalni
  8. // - faljmuveleteket vegezni a printf-et kiveve
  9. // - Mashonnan atvett programresszleteket forrasmegjeloles nelkul felhasznalni es
  10. // - felesleges programsorokat a beadott programban hagyni!!!!!!!
  11. // - felesleges kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
  12. // ---------------------------------------------------------------------------------------------
  13. // A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
  14. // es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
  15. // a hazibeado portal ad egy osszefoglalot.
  16. // ---------------------------------------------------------------------------------------------
  17. // A feladatmegoldasokban csak olyan OpenGL fuggvenyek hasznalhatok, amelyek az oran a feladatkiadasig elhangzottak
  18. // A keretben nem szereplo GLUT fuggvenyek tiltottak.
  19. //
  20. // NYILATKOZAT
  21. // ---------------------------------------------------------------------------------------------
  22. // Nev : Lakatos Dániel
  23. // Neptun : PRT14L
  24. // ---------------------------------------------------------------------------------------------
  25. // ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
  26. // mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
  27. // A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
  28. // grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
  29. // informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
  30. // azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
  31. // a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
  32. // Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
  33. // negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
  34. //=============================================================================================
  35. #define _USE_MATH_DEFINES // Van M_PI
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39. #include <vector>
  40.  
  41. #if defined(__APPLE__)
  42. #include <GLUT/GLUT.h>
  43. #include <OpenGL/gl3.h>
  44. #include <OpenGL/glu.h>
  45. #else
  46. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  47. #include <windows.h>
  48. #endif
  49. #include <GL/glew.h> // must be downloaded
  50. #include <GL/freeglut.h> // must be downloaded unless you have an Apple
  51. #endif
  52.  
  53. const unsigned int windowWidth = 600, windowHeight = 600;
  54.  
  55. // OpenGL major and minor versions
  56. int majorVersion = 3, minorVersion = 3;
  57.  
  58. void getErrorInfo(unsigned int handle) {
  59. int logLen;
  60. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLen);
  61. if (logLen > 0) {
  62. char * log = new char[logLen];
  63. int written;
  64. glGetShaderInfoLog(handle, logLen, &written, log);
  65. printf("Shader log:\n%s", log);
  66. delete log;
  67. }
  68. }
  69.  
  70. // check if shader could be compiled
  71. void checkShader(unsigned int shader, const char * message) {
  72. int OK;
  73. glGetShaderiv(shader, GL_COMPILE_STATUS, &OK);
  74. if (!OK) { printf("%s!\n", message); getErrorInfo(shader); }
  75. }
  76.  
  77. // check if shader could be linked
  78. void checkLinking(unsigned int program) {
  79. int OK;
  80. glGetProgramiv(program, GL_LINK_STATUS, &OK);
  81. if (!OK) { printf("Failed to link shader program!\n"); getErrorInfo(program); }
  82. }
  83.  
  84. // vertex shader in GLSL
  85. const char * vertexSource = R"(
  86. #version 330
  87. precision highp float;
  88.  
  89. uniform mat4 MVP; // Model-View-Projection matrix in row-major format
  90.  
  91. layout(location = 0) in vec2 vertexPosition; // Attrib Array 0
  92. layout(location = 1) in vec3 vertexColor; // Attrib Array 1
  93. out vec3 color; // output attribute
  94.  
  95. void main() {
  96. color = vertexColor; // copy color from input to output
  97. gl_Position = vec4(vertexPosition.x, vertexPosition.y, 0, 1) * MVP; // transform to clipping space
  98. }
  99. )";
  100.  
  101. // fragment shader in GLSL
  102. const char * fragmentSource = R"(
  103. #version 330
  104. precision highp float;
  105.  
  106. in vec3 color; // variable input: interpolated color of vertex shader
  107. out vec4 fragmentColor; // output that goes to the raster memory as told by glBindFragDataLocation
  108.  
  109. void main() {
  110. fragmentColor = vec4(color, 1);
  111. }
  112. )";
  113.  
  114. // vertex shader in GLSL
  115. const char * vertexTSource = R"(
  116. #version 330
  117. precision highp float;
  118.  
  119. uniform mat4 MVP; // Model-View-Projection matrix in row-major format
  120.  
  121. layout(location = 0) in vec2 vertexPosition; // Attrib Array 0
  122. layout(location = 1) in vec2 vertexUV; // Attrib Array 1
  123. out vec2 texcoord; // output attribute
  124.  
  125. void main() {
  126. texcoord = vertexUV; // copy color from input to output
  127. gl_Position = vec4(vertexPosition.x, vertexPosition.y, 0, 1) * MVP; // transform to clipping space
  128. }
  129. )";
  130.  
  131. // fragment shader in GLSL
  132. const char * fragmentTSource = R"(
  133. #version 330
  134. uniform sampler2D samplerUnit;
  135.  
  136. in vec2 texcoord; // variable input: interpolated color of vertex shader
  137. out vec4 fragmentColor; // output that goes to the raster memory as told by glBindFragDataLocation
  138.  
  139. void main() {
  140. fragmentColor = texture(samplerUnit, vec2(texcoord.y, texcoord.x));
  141. }
  142. )";
  143.  
  144. // row-major matrix 4x4
  145. struct mat4 {
  146. float m[4][4];
  147. public:
  148. mat4() {}
  149. mat4(float m00, float m01, float m02, float m03,
  150. float m10, float m11, float m12, float m13,
  151. float m20, float m21, float m22, float m23,
  152. float m30, float m31, float m32, float m33) {
  153. m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
  154. m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
  155. m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
  156. m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
  157. }
  158. mat4 operator*(const mat4& right) {
  159. mat4 result;
  160. for (int i = 0; i < 4; i++) {
  161. for (int j = 0; j < 4; j++) {
  162. result.m[i][j] = 0;
  163. for (int k = 0; k < 4; k++) result.m[i][j] += m[i][k] * right.m[k][j];
  164. }
  165. }
  166. return result;
  167. }
  168. mat4 operator*(float f) {
  169. mat4 result;
  170. for (int i = 0; i < 4; i++) {
  171. for (int j = 0; j < 4; j++) {
  172. result.m[i][j] = m[i][j] * f;
  173. }
  174. }
  175. return result;
  176. }
  177. operator float*() { return &m[0][0]; }
  178. };
  179.  
  180. // 3D point in Cartesian coordinates or RGB color
  181. struct vec3 {
  182. float v[3];
  183. vec3(float x = 0, float y = 0, float z = 0) { v[0] = x; v[1] = y; v[2] = z; }
  184. float& operator[](int idx) {
  185. return v[idx];
  186. }
  187. const float& operator[](int idx) const {
  188. return v[idx];
  189. }
  190. void operator+=(const vec3& v) {
  191. this->v[0] += v[0];
  192. this->v[1] += v[1];
  193. this->v[2] += v[2];
  194. }
  195. vec3 operator*(const mat4& mat) {
  196. vec3 result;
  197. for (int j = 0; j < 3; j++) {
  198. result.v[j] = 0;
  199. for (int i = 0; i < 3; i++) result.v[j] += v[i] * mat.m[i][j];
  200. }
  201. return result;
  202. }
  203. vec3 operator+(const vec3& v) {
  204. return vec3(this->v[0] + v[0], this->v[1] + v[1], this->v[2] + v[2]);
  205. }
  206. vec3 operator-(const vec3& v) {
  207. return vec3(this->v[0] - v[0], this->v[1] - v[1], this->v[2] - v[2]);
  208. }
  209. vec3 operator*(const vec3& v) {
  210. return vec3(this->v[1] * v[2] - this->v[2] * v[1], this->v[2] * v[0] - this->v[0] * v[2], this->v[0] * v[1] - this->v[1] * v[0]);
  211. }
  212. vec3 operator*(float f) {
  213. return vec3(v[0] * f, v[1] * f, v[2] * f);
  214. }
  215. vec3 operator/(float f) {
  216. return vec3(v[0] / f, v[1] / f, v[2] / f);
  217. }
  218. };
  219.  
  220. float dot(const vec3& left, const vec3& right) {
  221. return left[0] * right[0] + left[1] * right[1] + left[2] * right[2];
  222. }
  223.  
  224. vec3 operator*(float f, const vec3& v) {
  225. return vec3(v[0] * f, v[1] * f, v[2] * f);
  226. }
  227. vec3 operator/(float f, const vec3& v) {
  228. return vec3(v[0] / f, v[1] / f, v[2] / f);
  229. }
  230.  
  231. // 3D point in homogeneous coordinates
  232. struct vec4 {
  233. float v[4];
  234. vec4(float x = 0, float y = 0, float z = 0, float w = 1) {
  235. v[0] = x; v[1] = y; v[2] = z; v[3] = w;
  236. }
  237. vec4(vec3 const& v, float w) {
  238. this->v[0] = v[0]; this->v[1] = v[1]; this->v[2] = v[2]; this->v[3] = w;
  239. }
  240. vec4 operator*(const mat4& mat) {
  241. vec4 result;
  242. for (int j = 0; j < 4; j++) {
  243. result.v[j] = 0;
  244. for (int i = 0; i < 4; i++) result.v[j] += v[i] * mat.m[i][j];
  245. }
  246. return result;
  247. }
  248. };
  249.  
  250. // 2D point in Cartesian coordinates
  251. struct vec2 {
  252. float v[2];
  253. vec2(float x = 0, float y = 0) { v[0] = x; v[1] = y; }
  254. float& operator[](int idx) {
  255. return v[idx];
  256. }
  257. const float& operator[](int idx) const {
  258. return v[idx];
  259. }
  260. float length() { return sqrtf(v[0] * v[0] + v[1] * v[1]); }
  261. void operator+=(const vec2& v) {
  262. this->v[0] += v[0];
  263. this->v[1] += v[1];
  264. }
  265. vec2 operator+(const vec2& v) {
  266. return vec2(this->v[0] + v[0], this->v[1] + v[1]);
  267. }
  268. vec2 operator-(const vec2& v) {
  269. return vec2(this->v[0] - v[0], this->v[1] - v[1]);
  270. }
  271. vec2 operator*(float f) {
  272. return vec2(v[0] * f, v[1] * f);
  273. }
  274. vec2 operator/(float f) {
  275. return vec2(v[0] / f, v[1] / f);
  276. }
  277. };
  278.  
  279. vec2 operator*(float f, const vec2& v) {
  280. return vec2(v[0] * f, v[1] * f);
  281. }
  282. vec2 operator/(float f, const vec2& v) {
  283. return vec2(v[0] / f, v[1] / f);
  284. }
  285.  
  286. struct Texture {
  287. unsigned int textureId;
  288. Texture(int height = 0, int width = 0, vec3* image = NULL) {
  289. glGenTextures(1, &textureId);
  290. glBindTexture(GL_TEXTURE_2D, textureId); // binding
  291. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, image); //Texture -> GPU
  292. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  293. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  294. }
  295. };
  296.  
  297. // 2D camera
  298. struct Camera {
  299. float wCx, wCy; // center in world coordinates
  300. float wWx, wWy; // width and height in world coordinates
  301. float n = -100;
  302. float f = 100;
  303. public:
  304. Camera() {
  305. Animate(0);
  306. }
  307.  
  308. mat4 V() { // view matrix: translates the center to the origin
  309. return mat4(1, 0, 0, 0,
  310. 0, 1, 0, 0,
  311. 0, 0, 1, 0,
  312. -wCx, -wCy, 0, 1);;
  313. }
  314.  
  315. mat4 P() { // projection matrix: scales it to be a square of edge length 2
  316. return mat4(2 / wWx, 0, 0, 0,
  317. 0, 2 / wWy, 0, 0,
  318. 0, 0, 1 / (n-f), 0,
  319. 0, 0, (f + n) / (f - n), 1);
  320. }
  321.  
  322. mat4 Vinv() { // inverse view matrix
  323. return mat4(1, 0, 0, 0,
  324. 0, 1, 0, 0,
  325. 0, 0, 1, 0,
  326. wCx, wCy, 0, 1);
  327. }
  328.  
  329. mat4 Pinv() { // inverse projection matrix
  330. return mat4(wWx / 2, 0, 0, 0,
  331. 0, wWy / 2, 0, 0,
  332. 0, 0, 1 / (n - f), 0,
  333. 0, 0, (f + n) / (f - n), 1);
  334. }
  335.  
  336. void Animate(float t) {
  337. wCx = 0; // 10 * cosf(t);
  338. wCy = 0;
  339. wWx = 20;
  340. wWy = 20;
  341. }
  342. };
  343.  
  344. // 2D camera
  345. Camera camera;
  346.  
  347. // handle of the shader program
  348. unsigned int shaderProgram;
  349. unsigned int shaderTProgram;
  350.  
  351. //////////// Drawing functions :: BEGIN ////////////
  352. struct LagrangeCurve {
  353. std::vector<vec2> cps; // control pts
  354. std::vector<float> ts; // knots
  355.  
  356. float L(int i, float t) {
  357. float Li = 1.0f;
  358. for (unsigned int j = 0; j < cps.size(); j++)
  359. if (j != i) Li *= (t - ts[j]) / (ts[i] - ts[j]);
  360. return Li;
  361. }
  362. public:
  363. void AddControlPoint(vec2 cp, float t) {
  364. cps.push_back(cp);
  365. ts.push_back(t);
  366. }
  367.  
  368. vec2 r(float t) {
  369. vec2 rr(0, 0);
  370. for (unsigned int i = 0; i < cps.size(); i++) {
  371. rr += cps[i] * L(i, t);
  372. }
  373. return rr;
  374. }
  375. };
  376.  
  377. void drawCircle(std::vector<vec2> &pts, std::vector<vec3> &clr, vec2 const& org = vec2(0, 0), vec3 const& color = vec3(0, 0, 0)) {
  378. vec2 prev;
  379. for (float i = 0.0f; i < M_PI * 2; i += 0.05f) {
  380. float x = cos(i) * 2;
  381. float y = sin(i) * 2;
  382. vec2 pos(x, y);
  383. if (i > 0) {
  384. pts.push_back(prev);
  385. pts.push_back(pos);
  386. pts.push_back(org);
  387. }
  388. prev = pos;
  389. }
  390. pts.push_back(prev);
  391. pts.push_back(pts[0]);
  392. pts.push_back(org);
  393.  
  394. for (unsigned int i = 0; i < pts.size(); ++i) {
  395. clr.push_back(color);
  396. }
  397. }
  398.  
  399. void drawLine(std::vector<vec2> &pts, std::vector<vec3> &clr, vec2 const& p1, vec2 const& p2, float str = 0.05f, vec3 const& color = vec3(0, 0, 0)) {
  400. float dst = sqrtf(powf((p2[0] - p1[0]), 2.0f) + powf((p2[1] - p1[1]), 2.0f));
  401. float sinA = (p1[1] - p2[1]) / dst;
  402. float cosA = (p1[0] - p2[0]) / dst;
  403.  
  404. float x_ = str / 2 * sinA;
  405. float y_ = str / 2 * cosA;
  406.  
  407. pts.push_back(vec2(p1[0] + x_, p1[1] - y_));
  408. pts.push_back(vec2(p1[0] - x_, p1[1] + y_));
  409. pts.push_back(vec2(p2[0] + x_, p2[1] - y_));
  410. pts.push_back(vec2(p1[0] - x_, p1[1] + y_));
  411. pts.push_back(vec2(p2[0] - x_, p2[1] + y_));
  412. pts.push_back(vec2(p2[0] + x_, p2[1] - y_));
  413.  
  414. for (int i = 0; i < 6; ++i) {
  415. clr.push_back(color);
  416. }
  417. }
  418. //////////// Drawing functions :: END ////////////
  419.  
  420. class Drawable {
  421. public:
  422. unsigned int vao, vbo[2];
  423. float sx, sy; // scaling
  424. float wTx, wTy; // translation
  425. float fiY; // rotationY
  426. float fiZ; // rotationZ
  427. mat4 Mscale;
  428. mat4 Mtranslate;
  429. mat4 MrotateY;
  430. mat4 MrotateZ;
  431. std::vector<vec2> pts;
  432. std::vector<vec3> clr;
  433. public:
  434. Drawable() { Animate(0); }
  435.  
  436. void Create() {
  437. glGenVertexArrays(1, &vao);
  438. glBindVertexArray(vao);
  439. glGenBuffers(2, &vbo[0]);
  440. UpdateM();
  441. }
  442.  
  443. void Draw() {
  444. mat4 MVPTransform = Mscale * Mtranslate * camera.V() * camera.P();
  445.  
  446. int location = glGetUniformLocation(shaderProgram, "MVP");
  447. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  448. else printf("uniform MVP cannot be set\n");
  449.  
  450. glBindVertexArray(vao);
  451. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  452. }
  453.  
  454. void Animate(float t) {
  455. sx = 0.2f;
  456. sy = 1;
  457. wTx = 0;
  458. wTy = 0;
  459. }
  460.  
  461. void UpdateM() {
  462. Mscale = mat4(sx, 0, 0, 0,
  463. 0, sy, 0, 0,
  464. 0, 0, 1, 0,
  465. 0, 0, 0, 1);
  466.  
  467. Mtranslate = mat4(1, 0, 0, 0,
  468. 0, 1, 0, 0,
  469. 0, 0, 1, 0,
  470. wTx, wTy, 0, 1);
  471.  
  472. MrotateY = mat4(1, 0, 0, 0,
  473. 0, 1, 0, 0,
  474. 0, 0, 1, 0,
  475. 0, 0, 0, 1);
  476.  
  477. MrotateZ = mat4(1, 0, 0, 0,
  478. 0, 1, 0, 0,
  479. 0, 0, 1, 0,
  480. 0, 0, 0, 1);
  481. }
  482. };
  483.  
  484. bool mousePressed = false;
  485. vec3 cursor;
  486.  
  487. const float D = 10.0f;
  488. const float DRAG = 10.0f;
  489. const float M = 1.0f;
  490. const vec3 G = vec3(0, 0, 10);
  491.  
  492. vec3 F(vec3 pos, vec3 vcty) {
  493. if (mousePressed) {
  494. return D * (cursor - pos) - DRAG * vcty;
  495. }
  496. return vec3(0, 0, 0);
  497. }
  498.  
  499. bool inSmallCircle(float x, float y) {
  500. float cx = 0.85f;
  501. float cy = 0.72f;
  502. float r = 0.07f;
  503. if (((x - cx)*(x - cx) + (y - cy)*(y - cy)) < ((r)*(r))) {
  504. return true;
  505. }
  506. return false;
  507. }
  508.  
  509. bool inBigCircle(float x, float y) {
  510. float cx = 0.85f;
  511. float cy = 0.72f;
  512. float r1 = 0.1f;
  513. float r2 = 0.17f;
  514. if (((x - cx)*(x - cx) + (y - cy)*(y - cy)) < ((r2)*(r2)) && ((x - cx)*(x - cx) + (y - cy)*(y - cy)) > ((r1)*(r1))) {
  515. return true;
  516. }
  517. return false;
  518. }
  519.  
  520. bool inParabole(float x, float y) {
  521. x -= 0.35f;
  522. if (y < (-6.7f*x*x + 0.8f*x + 0.5f)) {
  523. return true;
  524. }
  525. return false;
  526. }
  527.  
  528. bool inEllipse(float x, float y) {
  529. float cx = 0.9f;
  530. float cy = 0.35f;
  531. if ((powf(((x - cx)*cosf(10.0f) + (y - cy)*sinf(10.0f)), 2) / (powf(0.17f, 2)) + powf(((y - cy)*cosf(10.0f) - (x - cx)*sinf(10.0f)), 2) / (powf(0.14f, 2))) < 1) {
  532. return true;
  533. }
  534. return false;
  535. }
  536.  
  537. class Wing : public Drawable {
  538. public:
  539. vec3 pos;
  540. float fiYw;
  541. mat4 MrotateYw;
  542. std::vector<vec2> tex;
  543. Texture texture;
  544. public:
  545. Wing() { Animate(0); }
  546.  
  547. void Create() {
  548. Drawable::Create();
  549.  
  550. LagrangeCurve curve;
  551.  
  552. vec2 v0(0.0f, -1.1f);
  553. vec2 v1(1.93f, -2.43f);
  554. vec2 v2(2.56f, -1.09f);
  555. vec2 v3(2.6f, 0.16f);
  556. vec2 v4(3.43f, 1.83f);
  557. vec2 v5(2.6f, 2.53f);
  558. vec2 v6(0.0f, 1.43f);
  559.  
  560. curve.AddControlPoint(v0, 0.0f);
  561. curve.AddControlPoint(v1, 1 / 6.0f);
  562. curve.AddControlPoint(v2, 2 / 6.0f);
  563. curve.AddControlPoint(v3, 3 / 6.0f);
  564. curve.AddControlPoint(v4, 4 / 6.0f);
  565. curve.AddControlPoint(v5, 5 / 6.0f);
  566. curve.AddControlPoint(v6, 1.0f);
  567.  
  568. float shift = 2.728842f;
  569. float norm = 0.160170f;
  570.  
  571. vec2 prev;
  572. float step = 0.001f;
  573. for (float i = 0; i <= 1.0f; i += step) {
  574. vec2 pos = curve.r(i);
  575. if (i > 0) {
  576. pts.push_back(prev);
  577. pts.push_back(pos);
  578. pts.push_back(vec2(0.0f, 1.0f));
  579.  
  580. tex.push_back(vec2((prev[0] + shift) * norm, (prev[1] + shift) * norm));
  581. tex.push_back(vec2((pos[0] + shift) * norm, (pos[1] + shift) * norm));
  582. tex.push_back(vec2((vec2(0.0f + shift, 1.0f + shift)[0]) * norm, (vec2(0.0f + shift, 1.0f + shift)[1]) * norm));
  583. }
  584. prev = pos;
  585. }
  586.  
  587. pts.push_back(prev);
  588. pts.push_back(pts[0]);
  589. pts.push_back(vec2(0.0f, 1.0f));
  590.  
  591. tex.push_back(vec2((prev[0] + shift) * norm, (prev[1] + shift) * norm));
  592. tex.push_back(vec2((pts[0][0] + shift) * norm, (pts[0][1] + shift) * norm));
  593. tex.push_back(vec2((vec2(0.0f + shift, 1.0f + shift)[0]) * norm, (vec2(0.0f + shift, 1.0f + shift)[1]) * norm));
  594.  
  595. int w = 128;
  596. float s = 1.0f / w;
  597.  
  598. vec3* image = new vec3[w * w];
  599. for (int y = 0; y < w; y++) {
  600. float yp = (float)y * s;
  601. for (int x = 0; x < w; x++) {
  602. float xp = (float)x * s;
  603.  
  604. if (inSmallCircle(xp, yp)) {
  605. image[x * w + y] = vec3(0, 0, 0);
  606. }
  607. else if (inBigCircle(xp, yp)) {
  608. image[x * w + y] = vec3(183 / 255.0f, 219 / 255.0f, 1);
  609. }
  610. else if (inParabole(xp, yp)) {
  611. image[x * w + y] = vec3(240 / 255.0f, 78 / 255.0f, 0);
  612. }
  613. else if (inEllipse(xp, yp)) {
  614. image[x * w + y] = vec3(240 / 255.0f, 78 / 255.0f, 0);
  615. }
  616. else {
  617. image[x * w + y] = vec3(235 / 255.0f, 183 / 255.0f, 73 / 255.0f);
  618. }
  619. }
  620. }
  621.  
  622. texture = Texture(w, w, image);
  623. delete[] image;
  624.  
  625. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  626. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * pts.size(), &pts[0], GL_STATIC_DRAW);
  627. glEnableVertexAttribArray(0);
  628. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  629.  
  630. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  631. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * tex.size(), &tex[0], GL_STATIC_DRAW);
  632. glEnableVertexAttribArray(1);
  633. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  634. }
  635.  
  636. void Draw() {
  637. glBindVertexArray(vao);
  638. glUseProgram(shaderTProgram);
  639.  
  640. mat4 MVPTransform = MrotateYw * Mscale * MrotateY * MrotateZ * Mtranslate * camera.V() * camera.P();
  641.  
  642. int location = glGetUniformLocation(shaderTProgram, "MVP");
  643. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  644. else printf("uniform MVP cannot be set\n");
  645.  
  646. int sampler = 0;
  647. location = glGetUniformLocation(shaderTProgram, "samplerUnit");
  648. glUniform1i(location, sampler);
  649. glActiveTexture(GL_TEXTURE0 + sampler);
  650. glBindTexture(GL_TEXTURE_2D, texture.textureId);
  651.  
  652. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  653.  
  654. sx = -sx;
  655. UpdateM();
  656.  
  657. MVPTransform = MrotateYw * Mscale * MrotateY * MrotateZ * Mtranslate * camera.V() * camera.P();
  658.  
  659. location = glGetUniformLocation(shaderTProgram, "MVP");
  660. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  661. else printf("uniform MVP cannot be set\n");
  662.  
  663. sampler = 0;
  664. location = glGetUniformLocation(shaderTProgram, "samplerUnit");
  665. glUniform1i(location, sampler);
  666. glActiveTexture(GL_TEXTURE0 + sampler);
  667. glBindTexture(GL_TEXTURE_2D, texture.textureId);
  668.  
  669. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  670.  
  671. sx = -sx;
  672. UpdateM();
  673.  
  674. glUseProgram(shaderProgram);
  675. }
  676.  
  677. void Animate(float t) {
  678. sx = 0.8f;
  679. sy = 0.8f;
  680. wTx = pos[0];
  681. wTy = pos[1];
  682. fiYw = 0.8f * sin(4 * t);
  683. UpdateM();
  684. }
  685.  
  686. void UpdateM() {
  687. Mscale = mat4(sx, 0, 0, 0,
  688. 0, sy, 0, 0,
  689. 0, 0, 1, 0,
  690. 0, 0, 0, 1);
  691.  
  692. Mtranslate = mat4(1, 0, 0, 0,
  693. 0, 1, 0, 0,
  694. 0, 0, 1, 0,
  695. wTx, wTy, 0, 1);
  696.  
  697. MrotateYw = mat4(cosf(fiYw), 0, -sinf(fiYw), 0,
  698. 0, 1, 0, 0,
  699. sinf(fiYw), 0, cosf(fiYw), 0,
  700. 0, 0, 0, 1);
  701.  
  702. MrotateY = mat4(cosf(fiY), 0, -sinf(fiY), 0,
  703. 0, 1, 0, 0,
  704. sinf(fiY), 0, cosf(fiY), 0,
  705. 0, 0, 0, 1);
  706.  
  707. MrotateZ = mat4(cosf(fiZ), sinf(fiZ), 0, 0,
  708. -sinf(fiZ), cosf(fiZ), 0, 0,
  709. 0, 0, 1, 0,
  710. 0, 0, 0, 1);
  711. }
  712. };
  713.  
  714. class Butterfly : public Drawable {
  715. public:
  716. Wing wing;
  717. vec3 vcty;
  718. vec3 pos;
  719. public:
  720. Butterfly(float x = 0, float y = 0) {
  721. pos = vec3(x, y, 0);
  722. vcty = vec3(0, 0.001f, 0);
  723. wing.pos = pos;
  724. Animate(0);
  725. }
  726.  
  727. void Create() {
  728. wing.Create();
  729.  
  730. Drawable::Create();
  731.  
  732. drawCircle(pts, clr, vec2(0, 0), vec3(85/255.0f, 43/255.0f, 0));
  733.  
  734. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  735. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * pts.size(), &pts[0], GL_STATIC_DRAW);
  736. glEnableVertexAttribArray(0);
  737. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  738.  
  739. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  740. glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * clr.size(), &clr[0], GL_STATIC_DRAW);
  741. glEnableVertexAttribArray(1);
  742. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  743. }
  744.  
  745. void Draw() {
  746. wing.Draw();
  747.  
  748. mat4 MVPTransform = Mscale * MrotateY * MrotateZ * Mtranslate * camera.V() * camera.P();
  749.  
  750. int location = glGetUniformLocation(shaderProgram, "MVP");
  751. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  752. else printf("uniform MVP cannot be set\n");
  753.  
  754. glBindVertexArray(vao);
  755. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  756. }
  757.  
  758. void Animate(float t) {
  759. wing.Animate(t);
  760. sx = 0.2f;
  761. sy = 0.8f;
  762. wTx = pos[0];
  763. wTy = pos[1];
  764.  
  765. float norm = sqrtf(powf(vcty[0], 2) + powf(vcty[1], 2) + powf(vcty[2], 2));
  766. vec3 Fbut;
  767.  
  768. if (mousePressed) {
  769. Fbut = D * (cursor - pos) - M * G;
  770. }
  771. else {
  772. Fbut = - M * G;
  773. }
  774.  
  775. vec3 j;
  776. float rotj;
  777. if (norm == 0) {
  778. j = vec3(0, 0, 0);
  779. rotj = atan2f(j[1], j[0]);
  780. }
  781. else {
  782. j = vcty / norm;
  783. rotj = atan2f(j[1], j[0]) - (float)M_PI_2;
  784. }
  785.  
  786. vec3 tmp = j * Fbut;
  787. vec3 i = tmp / sqrtf(powf(tmp[0], 2) + powf(tmp[1], 2) + powf(tmp[2], 2));
  788.  
  789. vec3 k = i * j;
  790.  
  791. float rotk = atan2f(k[1], k[2]);
  792.  
  793. fiZ = rotj;
  794. fiY = rotk;
  795. wing.fiZ = rotj;
  796. wing.fiY = rotk;
  797. UpdateM();
  798. }
  799.  
  800. void UpdateM() {
  801. wing.UpdateM();
  802.  
  803. Mscale = mat4(sx, 0, 0, 0,
  804. 0, sy, 0, 0,
  805. 0, 0, 1, 0,
  806. 0, 0, 0, 1);
  807.  
  808. Mtranslate = mat4(1, 0, 0, 0,
  809. 0, 1, 0, 0,
  810. 0, 0, 1, 0,
  811. wTx, wTy, 0, 1);
  812.  
  813. MrotateZ = mat4(cosf(fiZ), sinf(fiZ), 0, 0,
  814. -sinf(fiZ), cosf(fiZ), 0, 0,
  815. 0, 0, 1, 0,
  816. 0, 0, 0, 1);
  817.  
  818. MrotateY = mat4(cosf(fiY), 0, -sinf(fiY), 0,
  819. 0, 1, 0, 0,
  820. sinf(fiY), 0, cosf(fiY), 0,
  821. 0, 0, 0, 1);
  822. }
  823. };
  824.  
  825. class Leaf : public Drawable {
  826. public:
  827. Leaf() { Animate(0); }
  828.  
  829. void Create(vec3 color) {
  830. Drawable::Create();
  831.  
  832. LagrangeCurve curve;
  833.  
  834. vec2 v0(-0.6f, 2.0f);
  835. vec2 v1(-1.0f, 4.0f);
  836. vec2 v2(-0.4f, 5.3f);
  837. vec2 v3(0.2f, 5.5f);
  838. vec2 v4(0.4f, 5.3f);
  839. vec2 v5(1.0f, 4.0f);
  840. vec2 v6(0.6f, 2.0f);
  841.  
  842. curve.AddControlPoint(v0, 0.0f);
  843. curve.AddControlPoint(v1, 1 / 6.0f);
  844. curve.AddControlPoint(v2, 2 / 6.0f);
  845. curve.AddControlPoint(v3, 3 / 6.0f);
  846. curve.AddControlPoint(v4, 4 / 6.0f);
  847. curve.AddControlPoint(v5, 5 / 6.0f);
  848. curve.AddControlPoint(v6, 1.0f);
  849.  
  850. vec2 prev;
  851. float step = 0.001f;
  852. for (float i = 0; i <= 1.0f; i += step) {
  853. vec2 pos = curve.r(i);
  854. if (i > 0) {
  855. pts.push_back(prev);
  856. pts.push_back(pos);
  857. pts.push_back(vec2(0, 0));
  858. }
  859. prev = pos;
  860. }
  861. pts.push_back(prev);
  862. pts.push_back(pts[0]);
  863. pts.push_back(vec2(0, 0));
  864.  
  865. for (unsigned int i = 0; i < pts.size(); ++i) {
  866. clr.push_back(color);
  867. }
  868.  
  869. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  870. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * pts.size(), &pts[0], GL_STATIC_DRAW);
  871. glEnableVertexAttribArray(0);
  872. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  873.  
  874. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  875. glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * clr.size(), &clr[0], GL_STATIC_DRAW);
  876. glEnableVertexAttribArray(1);
  877. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  878. }
  879.  
  880. void Animate(float t) {
  881. sx = 0.5;
  882. sy = 0.5;
  883. UpdateM();
  884. }
  885.  
  886. void UpdateM() {
  887. MrotateZ = mat4(cosf(fiZ), sinf(fiZ), 0, 0,
  888. -sinf(fiZ), cosf(fiZ), 0, 0,
  889. 0, 0, 1, 0,
  890. 0, 0, 0, 1);
  891. }
  892.  
  893. void Draw(int leaves) {
  894. mat4 MVPTransform;
  895.  
  896. float step = 2.0f * (float)M_PI / (float)leaves;
  897. fiZ = step;
  898. UpdateM();
  899.  
  900. for (int i = 0; i < leaves; ++i) {
  901. MVPTransform = MrotateZ * Mscale * Mtranslate * camera.V() * camera.P();
  902. int location = glGetUniformLocation(shaderProgram, "MVP");
  903. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  904. else printf("uniform MVP cannot be set\n");
  905.  
  906. glBindVertexArray(vao);
  907. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  908.  
  909. fiZ += step;
  910. UpdateM();
  911. }
  912. }
  913. };
  914.  
  915. class Flower : public Drawable {
  916. Leaf leaf;
  917. int leaves;
  918. vec3 color;
  919. public:
  920. Flower(float x, float y, int leaves, vec3 color = vec3(1, 1, 1)) :leaves(leaves), color(color) {
  921. wTx = x;
  922. wTy = y;
  923. leaf.wTx = x;
  924. leaf.wTy = y;
  925. Animate(0);
  926. }
  927.  
  928. void Create() {
  929. leaf.Create(color);
  930.  
  931. Drawable::Create();
  932.  
  933. drawCircle(pts, clr, vec2(0, 0), vec3(1, 1, 0));
  934.  
  935. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  936. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * pts.size(), &pts[0], GL_STATIC_DRAW);
  937. glEnableVertexAttribArray(0);
  938. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  939.  
  940. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  941. glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * clr.size(), &clr[0], GL_STATIC_DRAW);
  942. glEnableVertexAttribArray(1);
  943. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  944. }
  945.  
  946. void Animate(float t) {
  947. leaf.Animate(t);
  948. sx = 0.4f;
  949. sy = 0.4f;
  950. UpdateM();
  951. }
  952.  
  953. void Draw() {
  954. leaf.Draw(leaves);
  955.  
  956. mat4 MVPTransform = Mscale * Mtranslate * camera.V() * camera.P();
  957.  
  958. int location = glGetUniformLocation(shaderProgram, "MVP");
  959. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  960. else printf("uniform MVP cannot be set\n");
  961.  
  962. glBindVertexArray(vao);
  963. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  964. }
  965. };
  966.  
  967. // The virtual world
  968. Flower f1(-6, 6, 1, vec3(1, 128 / 255.0f, 128 / 255.0f));
  969. Flower f2(0, 6, 1, vec3(1, 1, 1));
  970. Flower f3(6, 6, 2, vec3(1, 0, 128 / 255.0f));
  971. Flower f4(-6, -6, 3, vec3(128 / 255.0f, 0, 0));
  972. Flower f5(0, -6, 5, vec3(1, 0, 0));
  973. Flower f6(6, -6, 8, vec3(0, 0, 1));
  974. Butterfly bfly;
  975.  
  976. // Initialization, create an OpenGL context
  977. void onInitialization() {
  978. glViewport(0, 0, windowWidth, windowHeight);
  979.  
  980. // Create objects by setting up their vertex data on the GPU
  981. f1.Create();
  982. f2.Create();
  983. f3.Create();
  984. f4.Create();
  985. f5.Create();
  986. f6.Create();
  987. bfly.Create();
  988.  
  989. // Create vertex shader from string
  990. unsigned int vertexTShader = glCreateShader(GL_VERTEX_SHADER);
  991. if (!vertexTShader) {
  992. printf("Error in vertex shader creation\n");
  993. exit(1);
  994. }
  995. glShaderSource(vertexTShader, 1, &vertexTSource, NULL);
  996. glCompileShader(vertexTShader);
  997. checkShader(vertexTShader, "Vertex shader error");
  998.  
  999. // Create fragment shader from string
  1000. unsigned int fragmentTShader = glCreateShader(GL_FRAGMENT_SHADER);
  1001. if (!fragmentTShader) {
  1002. printf("Error in fragment shader creation\n");
  1003. exit(1);
  1004. }
  1005. glShaderSource(fragmentTShader, 1, &fragmentTSource, NULL);
  1006. glCompileShader(fragmentTShader);
  1007. checkShader(fragmentTShader, "Fragment shader error");
  1008.  
  1009. // Attach shaders to a single program
  1010. shaderTProgram = glCreateProgram();
  1011. if (!shaderTProgram) {
  1012. printf("Error in shader program creation\n");
  1013. exit(1);
  1014. }
  1015. glAttachShader(shaderTProgram, vertexTShader);
  1016. glAttachShader(shaderTProgram, fragmentTShader);
  1017.  
  1018. // Connect the fragmentColor to the frame buffer memory
  1019. glBindFragDataLocation(shaderTProgram, 0, "fragmentColor"); // fragmentColor goes to the frame buffer memory
  1020.  
  1021. // program packaging
  1022. glLinkProgram(shaderTProgram);
  1023. checkLinking(shaderTProgram);
  1024.  
  1025. glUseProgram(shaderTProgram);
  1026.  
  1027. // Create vertex shader from string
  1028. unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  1029. if (!vertexShader) {
  1030. printf("Error in vertex shader creation\n");
  1031. exit(1);
  1032. }
  1033. glShaderSource(vertexShader, 1, &vertexSource, NULL);
  1034. glCompileShader(vertexShader);
  1035. checkShader(vertexShader, "Vertex shader error");
  1036.  
  1037. // Create fragment shader from string
  1038. unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  1039. if (!fragmentShader) {
  1040. printf("Error in fragment shader creation\n");
  1041. exit(1);
  1042. }
  1043. glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  1044. glCompileShader(fragmentShader);
  1045. checkShader(fragmentShader, "Fragment shader error");
  1046.  
  1047. // Attach shaders to a single program
  1048. shaderProgram = glCreateProgram();
  1049. if (!shaderProgram) {
  1050. printf("Error in shader program creation\n");
  1051. exit(1);
  1052. }
  1053. glAttachShader(shaderProgram, vertexShader);
  1054. glAttachShader(shaderProgram, fragmentShader);
  1055.  
  1056. // Connect the fragmentColor to the frame buffer memory
  1057. glBindFragDataLocation(shaderProgram, 0, "fragmentColor"); // fragmentColor goes to the frame buffer memory
  1058.  
  1059. // program packaging
  1060. glLinkProgram(shaderProgram);
  1061. checkLinking(shaderProgram);
  1062. // make this program run
  1063. glUseProgram(shaderProgram);
  1064. }
  1065.  
  1066. void onExit() {
  1067. glDeleteProgram(shaderProgram);
  1068. glDeleteProgram(shaderTProgram);
  1069. printf("exit");
  1070. }
  1071.  
  1072. // Window has become invalid: Redraw
  1073. void onDisplay() {
  1074. glClearColor(0, 180/255.0f, 0, 1.0f); // background color
  1075. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen
  1076.  
  1077. f1.Draw();
  1078. f2.Draw();
  1079. f3.Draw();
  1080. f4.Draw();
  1081. f5.Draw();
  1082. f6.Draw();
  1083. bfly.Draw();
  1084.  
  1085. glutSwapBuffers(); // exchange the two buffers
  1086. }
  1087.  
  1088. // Key of ASCII code pressed
  1089. void onKeyboard(unsigned char key, int pX, int pY) {
  1090. if (key == 'd') glutPostRedisplay(); // if d, invalidate display, i.e. redraw
  1091. }
  1092.  
  1093. // Key of ASCII code released
  1094. void onKeyboardUp(unsigned char key, int pX, int pY) {
  1095.  
  1096. }
  1097.  
  1098. // Mouse click event
  1099. void onMouse(int button, int state, int pX, int pY) {
  1100. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { // GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON and GLUT_DOWN / GLUT_UP
  1101. mousePressed = true;
  1102. float cX = 2.0f * pX / windowWidth - 1; // flip y axis
  1103. float cY = 1.0f - 2.0f * pY / windowHeight;
  1104. vec3 wVertex = vec3(cX, cY) * camera.Pinv() * camera.Vinv();
  1105. cursor = wVertex;
  1106. glutPostRedisplay(); // redraw
  1107. }
  1108.  
  1109. if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { // GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON and GLUT_DOWN / GLUT_UP
  1110. mousePressed = false;
  1111. glutPostRedisplay(); // redraw
  1112. }
  1113. }
  1114.  
  1115. // Move mouse with key pressed
  1116. void onMouseMotion(int pX, int pY) {
  1117. float cX = 2.0f * pX / windowWidth - 1; // flip y axis
  1118. float cY = 1.0f - 2.0f * pY / windowHeight;
  1119. vec3 wVertex = vec3(cX, cY) * camera.Pinv() * camera.Vinv();
  1120. cursor = wVertex;
  1121. glutPostRedisplay(); // redraw
  1122. }
  1123.  
  1124. // Idle event indicating that some time elapsed: do animation here
  1125. void onIdle() {
  1126. static float tend = 0;
  1127. const float dt = 0.01f; // dt is ”infinitesimal”
  1128. float tstart = tend;
  1129. tend = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  1130.  
  1131. bfly.Animate(tend);
  1132.  
  1133. for (float t = tstart; t < tend; t += dt) {
  1134. float Dt = fmin(dt, tend - t);
  1135. vec3 butAccel = F(bfly.pos, bfly.vcty) / M;
  1136. bfly.vcty += butAccel * Dt;
  1137. bfly.pos += bfly.vcty * Dt;
  1138. bfly.wing.pos += bfly.vcty * Dt;
  1139. }
  1140.  
  1141. glutPostRedisplay(); // redraw the scene
  1142. }
  1143.  
  1144. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1145. // Do not touch the code below this line
  1146.  
  1147. int main(int argc, char * argv[]) {
  1148. glutInit(&argc, argv);
  1149. #if !defined(__APPLE__)
  1150. glutInitContextVersion(majorVersion, minorVersion);
  1151. #endif
  1152. glutInitWindowSize(windowWidth, windowHeight); // Application window is initially of resolution 600x600
  1153. glutInitWindowPosition(100, 100); // Relative location of the application window
  1154. #if defined(__APPLE__)
  1155. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_3_CORE_PROFILE); // 8 bit R,G,B,A + double buffer + depth buffer
  1156. #else
  1157. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  1158. #endif
  1159. glutCreateWindow(argv[0]);
  1160.  
  1161. #if !defined(__APPLE__)
  1162. glewExperimental = true; // magic
  1163. glewInit();
  1164. #endif
  1165.  
  1166. printf("GL Vendor : %s\n", glGetString(GL_VENDOR));
  1167. printf("GL Renderer : %s\n", glGetString(GL_RENDERER));
  1168. printf("GL Version (string) : %s\n", glGetString(GL_VERSION));
  1169. glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
  1170. glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
  1171. printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
  1172. printf("GLSL Version : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
  1173.  
  1174. onInitialization();
  1175.  
  1176. glutDisplayFunc(onDisplay); // Register event handlers
  1177. glutMouseFunc(onMouse);
  1178. glutIdleFunc(onIdle);
  1179. glutKeyboardFunc(onKeyboard);
  1180. glutKeyboardUpFunc(onKeyboardUp);
  1181. glutMotionFunc(onMouseMotion);
  1182.  
  1183. glutMainLoop();
  1184. onExit();
  1185. return 1;
  1186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement