Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.50 KB | None | 0 0
  1. //=============================================================================================
  2. // Szamitogepes grafika hazi feladat keret. Ervenyes 2017-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.
  6. // Tilos:
  7. // - mast "beincludolni", illetve mas konyvtarat hasznalni
  8. // - faljmuveleteket vegezni a printf-et kivéve
  9. // - new operatort hivni a lefoglalt adat korrekt felszabaditasa nelkul
  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/GLUT fuggvenyek hasznalhatok, amelyek az oran a feladatkiadasig elhangzottak
  18. //
  19. // NYILATKOZAT
  20. // ---------------------------------------------------------------------------------------------
  21. // Nev :
  22. // Neptun :
  23. // ---------------------------------------------------------------------------------------------
  24. // ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
  25. // mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
  26. // A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
  27. // grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
  28. // informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
  29. // azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
  30. // a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
  31. // Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
  32. // negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
  33. //=============================================================================================
  34.  
  35. #define _USE_MATH_DEFINES
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39.  
  40. #include <vector>
  41.  
  42. #if defined(__APPLE__)
  43. #include <GLUT/GLUT.h>
  44. #include <OpenGL/gl3.h>
  45. #include <OpenGL/glu.h>
  46. #else
  47. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  48. #include <windows.h>
  49. #endif
  50. #include <GL/glew.h> // must be downloaded
  51. #include <GL/freeglut.h> // must be downloaded unless you have an Apple
  52. #endif
  53.  
  54.  
  55. const unsigned int windowWidth = 600, windowHeight = 600;
  56.  
  57. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. // You are supposed to modify the code from here...
  59.  
  60. // OpenGL major and minor versions
  61. int majorVersion = 3, minorVersion = 3;
  62.  
  63. void getErrorInfo(unsigned int handle) {
  64. int logLen;
  65. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLen);
  66. if (logLen > 0) {
  67. char * log = new char[logLen];
  68. int written;
  69. glGetShaderInfoLog(handle, logLen, &written, log);
  70. printf("Shader log:\n%s", log);
  71. delete log;
  72. }
  73. }
  74.  
  75. // check if shader could be compiled
  76. void checkShader(unsigned int shader, char * message) {
  77. int OK;
  78. glGetShaderiv(shader, GL_COMPILE_STATUS, &OK);
  79. if (!OK) {
  80. printf("%s!\n", message);
  81. getErrorInfo(shader);
  82. }
  83. }
  84.  
  85. // check if shader could be linked
  86. void checkLinking(unsigned int program) {
  87. int OK;
  88. glGetProgramiv(program, GL_LINK_STATUS, &OK);
  89. if (!OK) {
  90. printf("Failed to link shader program!\n");
  91. getErrorInfo(program);
  92. }
  93. }
  94.  
  95. // vertex shader in GLSL
  96. const char * vertexSource = R"(
  97. #version 330
  98. precision highp float;
  99.  
  100. uniform mat4 MVP; // Model-View-Projection matrix in row-major format
  101.  
  102. layout(location = 0) in vec2 vertexPosition; // Attrib Array 0
  103. layout(location = 1) in vec3 vertexColor; // Attrib Array 1
  104. out vec3 color; // output attribute
  105.  
  106. void main() {
  107. color = vertexColor; // copy color from input to output
  108. gl_Position = vec4(vertexPosition.x, vertexPosition.y, 0, 1) * MVP; // transform to clipping space
  109. }
  110. )";
  111.  
  112. // fragment shader in GLSL
  113. const char * fragmentSource = R"(
  114. #version 330
  115. precision highp float;
  116.  
  117. in vec3 color; // variable input: interpolated color of vertex shader
  118. out vec4 fragmentColor; // output that goes to the raster memory as told by glBindFragDataLocation
  119.  
  120. void main() {
  121. fragmentColor = vec4(color, 1); // extend RGB to RGBA
  122. }
  123. )";
  124.  
  125. // row-major matrix 4x4
  126. struct mat4 {
  127. float m[4][4];
  128. public:
  129. mat4() {}
  130. mat4(float m00, float m01, float m02, float m03,
  131. float m10, float m11, float m12, float m13,
  132. float m20, float m21, float m22, float m23,
  133. float m30, float m31, float m32, float m33) {
  134. m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
  135. m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
  136. m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
  137. m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
  138. }
  139.  
  140. mat4 operator*(const mat4& right) {
  141. mat4 result;
  142. for (int i = 0; i < 4; i++) {
  143. for (int j = 0; j < 4; j++) {
  144. result.m[i][j] = 0;
  145. for (int k = 0; k < 4; k++) result.m[i][j] += m[i][k] * right.m[k][j];
  146. }
  147. }
  148. return result;
  149. }
  150. operator float*() { return &m[0][0]; }
  151. };
  152.  
  153.  
  154. // 3D point in homogeneous coordinates
  155. struct vec4 {
  156. float v[4];
  157.  
  158. vec4(float x = 0, float y = 0, float z = 0, float w = 1) {
  159. v[0] = x; v[1] = y; v[2] = z; v[3] = w;
  160. }
  161.  
  162. vec4 operator*(const mat4& mat) {
  163. vec4 result;
  164. for (int j = 0; j < 4; j++) {
  165. result.v[j] = 0;
  166. for (int i = 0; i < 4; i++) result.v[j] += v[i] * mat.m[i][j];
  167. }
  168. return result;
  169. }
  170. //HOZZÁADOTT!!!
  171. vec4 operator*(const float& f) {
  172. vec4 result(0, 0, 0, 0);
  173. for (int i = 0; i < 4; i++) {
  174. result.v[i] += v[i] * f;
  175. }
  176. return result;
  177. }
  178. //HOZZÁADOTT!!!
  179. vec4 operator+(vec4 vi) {
  180. vec4 result(0, 0, 0, 0);
  181. for (int i = 0; i < 4; i++) {
  182. result.v[i] += v[i] + vi.v[i];
  183. }
  184. return result;
  185. }
  186. //HOZZÁADOTT
  187. vec4 operator+=(vec4 vi) {
  188. return (*this + vi);
  189. }
  190. };
  191.  
  192. // 2D camera
  193. struct Camera {
  194. float wCx, wCy; // center in world coordinates
  195. float wWx, wWy; // width and height in world coordinates
  196. public:
  197. Camera() {
  198. Animate(0);
  199. }
  200.  
  201. mat4 V() { // view matrix: translates the center to the origin
  202. return mat4(1, 0, 0, 0,
  203. 0, 1, 0, 0,
  204. 0, 0, 1, 0,
  205. -wCx, -wCy, 0, 1);
  206. }
  207.  
  208. mat4 P() { // projection matrix: scales it to be a square of edge length 2
  209. return mat4(2/wWx, 0, 0, 0,
  210. 0, 2/wWy, 0, 0,
  211. 0, 0, 1, 0,
  212. 0, 0, 0, 1);
  213. }
  214.  
  215. mat4 Vinv() { // inverse view matrix
  216. return mat4(1, 0, 0, 0,
  217. 0, 1, 0, 0,
  218. 0, 0, 1, 0,
  219. wCx, wCy, 0, 1);
  220. }
  221.  
  222. mat4 Pinv() { // inverse projection matrix
  223. return mat4(wWx/2, 0, 0, 0,
  224. 0, wWy/2, 0, 0,
  225. 0, 0, 1, 0,
  226. 0, 0, 0, 1);
  227. }
  228.  
  229. void Animate(float t) {
  230. wCx = 0; // 10 * cosf(t);
  231. wCy = 0;
  232. wWx = 20;
  233. wWy = 20;
  234. }
  235. };
  236.  
  237. // 2D camera
  238. Camera camera;
  239.  
  240. // handle of the shader program
  241. unsigned int shaderProgram;
  242.  
  243.  
  244. //HOZZÁADOTT!!!
  245. struct LagrangeCurve {
  246. std::vector<vec4> cps; // control points
  247. std::vector<float> ts; // parameter (knot) values
  248.  
  249. float L(int i, float t) {
  250. float Li = 1.0f;
  251. for(unsigned int j = 0; j < cps.size(); j++)
  252. if (j != i) {
  253. Li *= (t - ts[j]) / (ts[i] - ts[j]);
  254. }
  255. return Li;
  256. }
  257. public:
  258. void AddControlPoint(vec4 cp) {
  259. float ti = cp.v[0];
  260. cps.push_back(cp);
  261. ts.push_back(ti);
  262. }
  263.  
  264. vec4 r(float t) {
  265. vec4 rr(0, 0, 0);
  266. for(unsigned int i = 0; i < cps.size(); i++)
  267. rr = rr + (cps[i] * L(i,t));
  268. return rr;
  269. }
  270. };
  271.  
  272. //HOZZÁADOTT
  273. struct BezierCurve {
  274. std::vector<vec4> cps;
  275. float B(int i, float t) {
  276. int n = cps.size() - 1;
  277. float choose = 1;
  278. for (int j = 1; j <= i; j++)
  279. choose *= (float)(n - j + 1) / j;
  280. return choose * pow(t, i) * pow(1 - t, n - i);
  281. }
  282. public:
  283. void AddControlPoint(vec4 cp) {
  284. cps.push_back(cp);
  285. }
  286. vec4 r(float t) {
  287. vec4 rr(0, 0, 0);
  288. for (int i = 0; i < cps.size(); i++)
  289. rr = rr + (cps[i] * B(i, t));
  290. }
  291. };
  292.  
  293. class Triangle {
  294. unsigned int vao; // vertex array object id
  295. float sx, sy; // scaling
  296. float wTx, wTy; // translation
  297. public:
  298. Triangle() {
  299. Animate(0);
  300. }
  301.  
  302. void Create() {
  303. glGenVertexArrays(1, &vao); // create 1 vertex array object
  304. glBindVertexArray(vao); // make it active
  305.  
  306. unsigned int vbo[2]; // vertex buffer objects
  307. glGenBuffers(2, &vbo[0]); // Generate 2 vertex buffer objects
  308.  
  309. // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  310. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  311. static float vertexCoords[] = { -8, -8, -6, 10, 8, -2 }; // vertex data on the CPU
  312. glBufferData(GL_ARRAY_BUFFER, // copy to the GPU
  313. sizeof(vertexCoords), // number of the vbo in bytes
  314. vertexCoords, // address of the data array on the CPU
  315. GL_STATIC_DRAW); // copy to that part of the memory which is not modified
  316. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  317. glEnableVertexAttribArray(0);
  318. // Data organization of Attribute Array 0
  319. glVertexAttribPointer(0, // Attribute Array 0
  320. 2, GL_FLOAT, // components/attribute, component type
  321. GL_FALSE, // not in fixed point format, do not normalized
  322. 0, NULL); // stride and offset: it is tightly packed
  323.  
  324. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  325. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  326. static float vertexColors[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; // vertex data on the CPU
  327. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_STATIC_DRAW); // copy to the GPU
  328.  
  329. // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  330. glEnableVertexAttribArray(1); // Vertex position
  331. // Data organization of Attribute Array 1
  332. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  333. }
  334.  
  335. void Animate(float t) {
  336. sx = 0; // sinf(t);
  337. sy = 0; // cosf(t);
  338. wTx = 0; // 4 * cosf(t / 2);
  339. wTy = 0; // 4 * sinf(t / 2);
  340. }
  341.  
  342. void Draw() {
  343. mat4 Mscale(sx, 0, 0, 0,
  344. 0, sy, 0, 0,
  345. 0, 0, 0, 0,
  346. 0, 0, 0, 1); // model matrix
  347.  
  348. mat4 Mtranslate(1, 0, 0, 0,
  349. 0, 1, 0, 0,
  350. 0, 0, 0, 0,
  351. wTx, wTy, 0, 1); // model matrix
  352.  
  353. mat4 MVPTransform = Mscale * Mtranslate * camera.V() * camera.P();
  354.  
  355. // set GPU uniform matrix variable MVP with the content of CPU variable MVPTransform
  356. int location = glGetUniformLocation(shaderProgram, "MVP");
  357. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform); // set uniform variable MVP to the MVPTransform
  358. else printf("uniform MVP cannot be set\n");
  359.  
  360. glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  361. glDrawArrays(GL_TRIANGLES, 0, 3); // draw a single triangle with vertices defined in vao
  362. }
  363. };
  364.  
  365. class LineStrip {
  366. GLuint vao, vbo; // vertex array object, vertex buffer object
  367. //float vertexData[100]; // interleaved data of coordinates and colors
  368. int nVertices; // number of vertices
  369. LagrangeCurve lc;
  370. std::vector<float> rArray;
  371. public:
  372. LineStrip() {
  373. nVertices = 0;
  374. }
  375. void Create() {
  376. glGenVertexArrays(1, &vao);
  377. glBindVertexArray(vao);
  378.  
  379. glGenBuffers(1, &vbo); // Generate 1 vertex buffer object
  380. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  381. // Enable the vertex attribute arrays
  382. glEnableVertexAttribArray(0); // attribute array 0
  383. glEnableVertexAttribArray(1); // attribute array 1
  384. // Map attribute array 0 to the vertex data of the interleaved vbo
  385. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(0)); // attribute array, components/attribute, component type, normalize?, stride, offset
  386. // Map attribute array 1 to the color data of the interleaved vbo
  387. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(2 * sizeof(float)));
  388. }
  389.  
  390. void AddPoint(float cX, float cY) {
  391. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  392. if (nVertices >= 20) return;
  393.  
  394. vec4 wVertex = vec4(cX, cY, 0, 1) * camera.Pinv() * camera.Vinv();
  395. // fill interleaved data
  396. /*vertexData[5 * nVertices] = wVertex.v[0];
  397. vertexData[5 * nVertices + 1] = wVertex.v[1];
  398. vertexData[5 * nVertices + 2] = 1; // red
  399. vertexData[5 * nVertices + 3] = 0; // green
  400. vertexData[5 * nVertices + 4] = 0; // blue
  401. */
  402. lc.AddControlPoint(wVertex);
  403.  
  404. rArray.clear();
  405. float fx = (lc.ts.front());
  406. unsigned int n = 0;
  407. if (nVertices >= 1) {
  408. while ((n+1) < (lc.cps.size())) {
  409. if ((lc.ts[n]) < (lc.ts[n + 1])) {
  410. while (fx < (lc.ts[n + 1])) {
  411. rArray.push_back(lc.r(fx).v[0]);
  412. rArray.push_back(lc.r(fx).v[1]);
  413. rArray.push_back(1);
  414. rArray.push_back(1);
  415. rArray.push_back(1);
  416. fx += 0.1f;
  417. }
  418. }
  419. else if ((lc.ts[n]) > (lc.ts[n + 1])) {
  420. while (fx > lc.ts[n + 1]) {
  421. rArray.push_back(lc.r(fx).v[0]);
  422. rArray.push_back(lc.r(fx).v[1]);
  423. rArray.push_back(1);
  424. rArray.push_back(1);
  425. rArray.push_back(1);
  426. fx -= 0.1f;
  427. }
  428. }
  429. rArray.push_back(lc.r(fx).v[0]);
  430. rArray.push_back(lc.r(fx).v[1]);
  431. rArray.push_back(1);
  432. rArray.push_back(1);
  433. rArray.push_back(1);
  434. n++;
  435. }
  436. glBufferData(GL_ARRAY_BUFFER, rArray.size() * sizeof(float), &rArray.front(), GL_DYNAMIC_DRAW);
  437. }
  438. nVertices++;
  439. }
  440.  
  441. void Draw() {
  442. if (nVertices > 0) {
  443.  
  444.  
  445. mat4 VPTransform = camera.V() * camera.P();
  446.  
  447. int location = glGetUniformLocation(shaderProgram, "MVP");
  448. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, VPTransform);
  449. else printf("uniform MVP cannot be set\n");
  450.  
  451. glBindVertexArray(vao);
  452. glDrawArrays(GL_LINE_STRIP, 0, rArray.size()/5);
  453. }
  454. }
  455. };
  456.  
  457. // The virtual world: collection of two objects
  458. Triangle triangle;
  459. LineStrip lineStrip;
  460.  
  461. // Initialization, create an OpenGL context
  462. void onInitialization() {
  463. glViewport(0, 0, windowWidth, windowHeight);
  464.  
  465. // Create objects by setting up their vertex data on the GPU
  466. lineStrip.Create();
  467. triangle.Create();
  468.  
  469. //Create the map (HOZZÁADOTT!!!)
  470. //Bezier Curves for the surface
  471. BezierCurve c1;
  472. BezierCurve c2;
  473. BezierCurve c3;
  474. BezierCurve c4;
  475. //Vector for the points of the surface (HOZZÁADOTT!!!)
  476. std::vector<vec4> pArray;
  477. //Control points
  478. vec4 c1_1(-6, 6, 0, 1); vec4 c1_2(-2, 6, 2, 1);
  479. vec4 c1_3(2, 6, 1, 1); vec4 c1_4(6, 6, 0, 1);
  480.  
  481. vec4 c2_1(-6, 2, 1, 1); vec4 c2_2(-2, 2, 3, 1);
  482. vec4 c2_3(2, 2, 2, 1); vec4 c2_4(6, 2, 1, 1);
  483.  
  484. vec4 c3_1(-6, -2, -1, 1); vec4 c3_2(-2, -2, 1, 1);
  485. vec4 c3_3(2, -2, 0, 1); vec4 c3_4(6, -2, -1, 1);
  486.  
  487. vec4 c4_1(-6, -6, 0, 1); vec4 c4_2(-2, -6, 0, 1);
  488. vec4 c4_3(2, -6, 0, 1); vec4 c4_4(6, -6, 0, 1);
  489.  
  490. c1.AddControlPoint(c1_1); c1.AddControlPoint(c1_2);
  491. c1.AddControlPoint(c1_3); c1.AddControlPoint(c1_4);
  492.  
  493. c2.AddControlPoint(c2_1); c2.AddControlPoint(c2_2);
  494. c2.AddControlPoint(c2_3); c2.AddControlPoint(c2_4);
  495.  
  496. c3.AddControlPoint(c3_1); c3.AddControlPoint(c3_2);
  497. c3.AddControlPoint(c3_3); c3.AddControlPoint(c3_4);
  498.  
  499. c4.AddControlPoint(c4_1); c4.AddControlPoint(c4_2);
  500. c4.AddControlPoint(c4_3); c4.AddControlPoint(c4_4);
  501.  
  502. //Loop
  503. int i = 0;
  504. int j = 1;
  505. int density = 21;
  506. float squarelength = c1_1.v[0] + c1_4.v[0];
  507. float t = c1_1.v[0];
  508. float r;
  509.  
  510. while (i != density) {
  511. r = c1_1.v[1];
  512. pArray.push_back(c1.r(t));
  513. pArray.push_back(c2.r(t));
  514. pArray.push_back(c3.r(t));
  515. pArray.push_back(c4.r(t));
  516. while (j != density) {
  517. pArray.push_back();
  518. r -= (float)squarelength / (density-1);
  519. j++;
  520. }
  521. t += (float)squarelength/(density -1);
  522. i++;
  523. }
  524.  
  525. // Create vertex shader from string
  526. unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  527. if (!vertexShader) {
  528. printf("Error in vertex shader creation\n");
  529. exit(1);
  530. }
  531. glShaderSource(vertexShader, 1, &vertexSource, NULL);
  532. glCompileShader(vertexShader);
  533. checkShader(vertexShader, "Vertex shader error");
  534.  
  535. // Create fragment shader from string
  536. unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  537. if (!fragmentShader) {
  538. printf("Error in fragment shader creation\n");
  539. exit(1);
  540. }
  541. glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  542. glCompileShader(fragmentShader);
  543. checkShader(fragmentShader, "Fragment shader error");
  544.  
  545. // Attach shaders to a single program
  546. shaderProgram = glCreateProgram();
  547. if (!shaderProgram) {
  548. printf("Error in shader program creation\n");
  549. exit(1);
  550. }
  551. glAttachShader(shaderProgram, vertexShader);
  552. glAttachShader(shaderProgram, fragmentShader);
  553.  
  554. // Connect the fragmentColor to the frame buffer memory
  555. glBindFragDataLocation(shaderProgram, 0, "fragmentColor"); // fragmentColor goes to the frame buffer memory
  556.  
  557. // program packaging
  558. glLinkProgram(shaderProgram);
  559. checkLinking(shaderProgram);
  560. // make this program run
  561. glUseProgram(shaderProgram);
  562. }
  563.  
  564. void onExit() {
  565. glDeleteProgram(shaderProgram);
  566. printf("exit");
  567. }
  568.  
  569. // Window has become invalid: Redraw
  570. void onDisplay() {
  571. glClearColor(0, 0.3, 0.6, 0); // background color
  572. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen
  573.  
  574. triangle.Draw();
  575. lineStrip.Draw();
  576. glutSwapBuffers(); // exchange the two buffers
  577. }
  578.  
  579. // Key of ASCII code pressed
  580. void onKeyboard(unsigned char key, int pX, int pY) {
  581. if (key == 'd') glutPostRedisplay(); // if d, invalidate display, i.e. redraw
  582. }
  583.  
  584. // Key of ASCII code released
  585. void onKeyboardUp(unsigned char key, int pX, int pY) {
  586.  
  587. }
  588.  
  589. // Mouse click event
  590. void onMouse(int button, int state, int pX, int pY) {
  591. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { // GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON and GLUT_DOWN / GLUT_UP
  592. float cX = 2.0f * pX / windowWidth - 1; // flip y axis
  593. float cY = 1.0f - 2.0f * pY / windowHeight;
  594. lineStrip.AddPoint(cX, cY);
  595. glutPostRedisplay(); // redraw
  596. }
  597. }
  598.  
  599. // Move mouse with key pressed
  600. void onMouseMotion(int pX, int pY) {
  601. }
  602.  
  603. // Idle event indicating that some time elapsed: do animation here
  604. void onIdle() {
  605. long time = glutGet(GLUT_ELAPSED_TIME); // elapsed time since the start of the program
  606. float sec = time / 1000.0f; // convert msec to sec
  607. camera.Animate(sec); // animate the camera
  608. triangle.Animate(sec); // animate the triangle object
  609. glutPostRedisplay(); // redraw the scene
  610. }
  611.  
  612. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  613. // Do not touch the code below this line
  614.  
  615. int main(int argc, char * argv[]) {
  616. glutInit(&argc, argv);
  617. #if !defined(__APPLE__)
  618. glutInitContextVersion(majorVersion, minorVersion);
  619. #endif
  620. glutInitWindowSize(windowWidth, windowHeight); // Application window is initially of resolution 600x600
  621. glutInitWindowPosition(100, 100); // Relative location of the application window
  622. #if defined(__APPLE__)
  623. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_3_CORE_PROFILE); // 8 bit R,G,B,A + double buffer + depth buffer
  624. #else
  625. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  626. #endif
  627. glutCreateWindow(argv[0]);
  628.  
  629. #if !defined(__APPLE__)
  630. glewExperimental = true; // magic
  631. glewInit();
  632. #endif
  633.  
  634. printf("GL Vendor : %s\n", glGetString(GL_VENDOR));
  635. printf("GL Renderer : %s\n", glGetString(GL_RENDERER));
  636. printf("GL Version (string) : %s\n", glGetString(GL_VERSION));
  637. glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
  638. glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
  639. printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
  640. printf("GLSL Version : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
  641.  
  642. onInitialization();
  643.  
  644. glutDisplayFunc(onDisplay); // Register event handlers
  645. glutMouseFunc(onMouse);
  646. glutIdleFunc(onIdle);
  647. glutKeyboardFunc(onKeyboard);
  648. glutKeyboardUpFunc(onKeyboardUp);
  649. glutMotionFunc(onMouseMotion);
  650.  
  651. glutMainLoop();
  652. onExit();
  653. return 1;
  654. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement