Advertisement
Guest User

Untitled

a guest
May 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.43 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // GLEW
  4. //#define GLEW_STATIC
  5. #include <GL/glew.h>
  6.  
  7. // GLFW
  8. #include <GLFW/glfw3.h>
  9.  
  10. #include <glm/glm.hpp>
  11. #include <glm/gtc/matrix_transform.hpp>
  12. #include <glm/gtc/type_ptr.hpp>
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <algorithm>
  16. #include "glm/ext.hpp"
  17.  
  18. # define M_PI 3.14159265358979323846
  19.  
  20. # define OPENGL_MODE 0
  21. # define CLOSE2GL_MODE 1
  22.  
  23. void FramebufferSizeCallback(GLFWwindow* window, int width, int height);
  24.  
  25. // Classe utilizada para representar um vetor de quatro posições (coordenadas homogêneas)
  26. class Vect4 {
  27. public:
  28. float x, y, z, w;
  29.  
  30. Vect4(float x1, float y1, float z1, float w1) { x = x1; y = y1; z = z1; w = w1; }
  31.  
  32. Vect4() { x = 0.0f; y = 0.0f; z = 0.0f; w = 0.0f; }
  33.  
  34. float length() { return sqrt(pow(x, 2.0) + pow(y, 2.0) + pow(z, 2.0)); }
  35.  
  36. Vect4 operator+(const Vect4& b) const {
  37. return Vect4(this->x + b.x, this->y + b.y, this->z + b.z, this->w + b.w);
  38. }
  39.  
  40. Vect4 operator-() const {
  41. return Vect4(-this->x, -this->y, -this->z, -this->w);
  42. }
  43.  
  44. Vect4 operator-(const Vect4& b) const {
  45. return Vect4(this->x - b.x, this->y - b.y, this->z - b.z, this->w - b.w);
  46. }
  47.  
  48. Vect4 operator*(const float& n) const {
  49. return Vect4(this->x * n, this->y * n, this->z * n, this->w * n);
  50. }
  51.  
  52. Vect4 operator/(const float& n) const {
  53. return Vect4(this->x / n, this->y / n, this->z / n, this->w / n);
  54. }
  55.  
  56. void print() const {
  57. printf("\n%f %f %f %f\n", x, y, z, w);
  58. }
  59.  
  60. };
  61.  
  62. inline Vect4 operator* (int n, const Vect4& v) {
  63. return v*n;
  64. }
  65.  
  66.  
  67. Vect4 normalize(Vect4 v) {
  68.  
  69. return v / (v.length());
  70. }
  71.  
  72. float dot(Vect4 a, Vect4 b) {
  73. return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
  74. }
  75.  
  76. Vect4 cross(Vect4 a, Vect4 b) {
  77. return Vect4((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x), 0.0f);
  78. }
  79.  
  80. // Classe utilizada para representar uma matriz 4x4 (coordenadas homogêneas)
  81. class Matr4 {
  82. public:
  83. Vect4 m[4];
  84.  
  85. Matr4(float x0, float y0, float z0, float w0,
  86. float x1, float y1, float z1, float w1,
  87. float x2, float y2, float z2, float w2,
  88. float x3, float y3, float z3, float w3) {
  89.  
  90. m[0] = Vect4(x0, y0, z0, w0);
  91. m[1] = Vect4(x1, y1, z1, w1);
  92. m[2] = Vect4(x2, y2, z2, w2);
  93. m[3] = Vect4(x3, y3, z3, w3);
  94. }
  95. Matr4() {
  96. m[0] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
  97. m[1] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
  98. m[2] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
  99. m[3] = Vect4(0.0f, 0.0f, 0.0f, 0.0f);
  100. }
  101.  
  102. Matr4 transpose() const {
  103. return Matr4(m[0].x, m[1].x, m[2].x, m[3].x,
  104. m[0].y, m[1].y, m[2].y, m[3].y,
  105. m[0].z, m[1].z, m[2].z, m[3].z,
  106. m[0].w, m[1].w, m[2].w, m[3].w);
  107.  
  108. }
  109.  
  110. Matr4 inverse() const {
  111. float inv[16];
  112.  
  113. inv[0] = m[1].y * m[2].z * m[3].w -
  114. m[1].y * m[2].w * m[3].z -
  115. m[2].y * m[1].z * m[3].w +
  116. m[2].y * m[1].w * m[3].z +
  117. m[3].y * m[1].z * m[2].w -
  118. m[3].y * m[1].w * m[2].z;
  119.  
  120. inv[4] = -m[1].x * m[2].z * m[3].w +
  121. m[1].x * m[2].w * m[3].z +
  122. m[2].x * m[1].z * m[3].w -
  123. m[2].x * m[1].w * m[3].z -
  124. m[3].x * m[1].z * m[2].w +
  125. m[3].x * m[1].w * m[2].z;
  126.  
  127. inv[8] = m[1].x * m[2].y * m[3].w -
  128. m[1].x * m[2].w * m[3].y -
  129. m[2].x * m[1].y * m[3].w +
  130. m[2].x * m[1].w * m[3].y +
  131. m[3].x * m[1].y * m[2].w -
  132. m[3].x * m[1].w * m[2].y;
  133.  
  134. inv[12] = -m[1].x * m[2].y * m[3].z +
  135. m[1].x * m[2].z * m[3].y +
  136. m[2].x * m[1].y * m[3].z -
  137. m[2].x * m[1].z * m[3].y -
  138. m[3].x * m[1].y * m[2].z +
  139. m[3].x * m[1].z * m[2].y;
  140.  
  141. inv[1] = -m[0].y * m[2].z * m[3].w +
  142. m[0].y * m[2].w * m[3].z +
  143. m[2].y * m[0].z * m[3].w -
  144. m[2].y * m[0].w * m[3].z -
  145. m[3].y * m[0].z * m[2].w +
  146. m[3].y * m[0].w * m[2].z;
  147.  
  148. inv[5] = m[0].x * m[2].z * m[3].w -
  149. m[0].x * m[2].w * m[3].z -
  150. m[2].x * m[0].z * m[3].w +
  151. m[2].x * m[0].w * m[3].z +
  152. m[3].x * m[0].z * m[2].w -
  153. m[3].x * m[0].w * m[2].z;
  154.  
  155. inv[9] = -m[0].x * m[2].y * m[3].w +
  156. m[0].x * m[2].w * m[3].y +
  157. m[2].x * m[0].y * m[3].w -
  158. m[2].x * m[0].w * m[3].y -
  159. m[3].x * m[0].y * m[2].w +
  160. m[3].x * m[0].w * m[2].y;
  161.  
  162. inv[13] = m[0].x * m[2].y * m[3].z -
  163. m[0].x * m[2].z * m[3].y -
  164. m[2].x * m[0].y * m[3].z +
  165. m[2].x * m[0].z * m[3].y +
  166. m[3].x * m[0].y * m[2].z -
  167. m[3].x * m[0].z * m[2].y;
  168.  
  169. inv[2] = m[0].y * m[1].z * m[3].w -
  170. m[0].y * m[1].w * m[3].z -
  171. m[1].y * m[0].z * m[3].w +
  172. m[1].y * m[0].w * m[3].z +
  173. m[3].y * m[0].z * m[1].w -
  174. m[3].y * m[0].w * m[1].z;
  175.  
  176. inv[6] = -m[0].x * m[1].z * m[3].w +
  177. m[0].x * m[1].w * m[3].z +
  178. m[1].x * m[0].z * m[3].w -
  179. m[1].x * m[0].w * m[3].z -
  180. m[3].x * m[0].z * m[1].w +
  181. m[3].x * m[0].w * m[1].z;
  182.  
  183. inv[10] = m[0].x * m[1].y * m[3].w -
  184. m[0].x * m[1].w * m[3].y -
  185. m[1].x * m[0].y * m[3].w +
  186. m[1].x * m[0].w * m[3].y +
  187. m[3].x * m[0].y * m[1].w -
  188. m[3].x * m[0].w * m[1].y;
  189.  
  190. inv[14] = -m[0].x * m[1].y * m[3].z +
  191. m[0].x * m[1].z * m[3].y +
  192. m[1].x * m[0].y * m[3].z -
  193. m[1].x * m[0].z * m[3].y -
  194. m[3].x * m[0].y * m[1].z +
  195. m[3].x * m[0].z * m[1].y;
  196.  
  197. inv[3] = -m[0].y * m[1].z * m[2].w +
  198. m[0].y * m[1].w * m[2].z +
  199. m[1].y * m[0].z * m[2].w -
  200. m[1].y * m[0].w * m[2].z -
  201. m[2].y * m[0].z * m[1].w +
  202. m[2].y * m[0].w * m[1].z;
  203.  
  204. inv[7] = m[0].x * m[1].z * m[2].w -
  205. m[0].x * m[1].w * m[2].z -
  206. m[1].x * m[0].z * m[2].w +
  207. m[1].x * m[0].w * m[2].z +
  208. m[2].x * m[0].z * m[1].w -
  209. m[2].x * m[0].w * m[1].z;
  210.  
  211. inv[11] = -m[0].x * m[1].y * m[2].w +
  212. m[0].x * m[1].w * m[2].y +
  213. m[1].x * m[0].y * m[2].w -
  214. m[1].x * m[0].w * m[2].y -
  215. m[2].x * m[0].y * m[1].w +
  216. m[2].x * m[0].w * m[1].y;
  217.  
  218. inv[15] = m[0].x * m[1].y * m[2].z -
  219. m[0].x * m[1].z * m[2].y -
  220. m[1].x * m[0].y * m[2].z +
  221. m[1].x * m[0].z * m[2].y +
  222. m[2].x * m[0].y * m[1].z -
  223. m[2].x * m[0].z * m[1].y;
  224.  
  225. float det = m[0].x * inv[0] + m[0].y * inv[4] + m[0].z * inv[8] + m[0].w * inv[12];
  226.  
  227. if (det == 0)
  228. return Matr4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
  229.  
  230. det = 1.0 / det;
  231.  
  232. return Matr4(inv[0]*det, inv[1]*det, inv[2]*det, inv[3]*det,
  233. inv[4]*det, inv[5]*det, inv[6]*det, inv[7]*det,
  234. inv[8]*det, inv[9]*det, inv[10]*det, inv[11]*det,
  235. inv[12]*det, inv[13]*det, inv[14]*det, inv[15]*det);
  236.  
  237.  
  238. }
  239.  
  240. void print() const {
  241.  
  242. printf("\n%f %f %f %f\n", m[0].x, m[0].y, m[0].z, m[0].w);
  243. printf("%f %f %f %f\n", m[1].x, m[1].y, m[1].z, m[1].w);
  244. printf("%f %f %f %f\n", m[2].x, m[2].y, m[2].z, m[2].w);
  245. printf("%f %f %f %f\n\n", m[3].x, m[3].y, m[3].z, m[3].w);
  246.  
  247. }
  248.  
  249. Vect4 operator*(const Vect4& v) const {
  250. return Vect4(
  251. (m[0].x * v.x) + (m[0].y * v.y) + (m[0].z * v.z) + (m[0].w * v.w),
  252. (m[1].x * v.x) + (m[1].y * v.y) + (m[1].z * v.z) + (m[1].w * v.w),
  253. (m[2].x * v.x) + (m[2].y * v.y) + (m[2].z * v.z) + (m[2].w * v.w),
  254. (m[3].x * v.x) + (m[3].y * v.y) + (m[3].z * v.z) + (m[3].w * v.w)
  255. );
  256. }
  257.  
  258. Matr4 operator-() const {
  259. return Matr4(
  260. (-m[0].x), (-m[0].y), (-m[0].z), (-m[0].w),
  261. (-m[1].x), (-m[1].y), (-m[1].z), (-m[1].w),
  262. (-m[2].x), (-m[2].y), (-m[2].z), (-m[2].w),
  263. (-m[3].x), (-m[3].y), (-m[3].z), (-m[3].w)
  264. );
  265. }
  266.  
  267.  
  268. Matr4 operator*(const Matr4& mat) const {
  269. Matr4 b = mat.transpose();
  270. return Matr4(
  271. dot(this->m[0], b.m[0]), dot(this->m[0], b.m[1]), dot(this->m[0], b.m[2]), dot(this->m[0], b.m[3]),
  272. dot(this->m[1], b.m[0]), dot(this->m[1], b.m[1]), dot(this->m[1], b.m[2]), dot(this->m[1], b.m[3]),
  273. dot(this->m[2], b.m[0]), dot(this->m[2], b.m[1]), dot(this->m[2], b.m[2]), dot(this->m[2], b.m[3]),
  274. dot(this->m[3], b.m[0]), dot(this->m[3], b.m[1]), dot(this->m[3], b.m[2]), dot(this->m[3], b.m[3])
  275. );
  276. }
  277.  
  278. glm::mat4 convertToGLM() const {
  279. return glm::mat4(m[0].x, m[1].x, m[2].x, m[3].x,
  280. m[0].y, m[1].y, m[2].y, m[3].y,
  281. m[0].z, m[1].z, m[2].z, m[3].z,
  282. m[0].w, m[1].w, m[2].w, m[3].w
  283. );
  284. }
  285. };
  286.  
  287. Matr4 identity() {
  288.  
  289. return Matr4(1.0f, 0.0f, 0.0f, 0.0f,
  290. 0.0f, 1.0f, 0.0f, 0.0f,
  291. 0.0f, 0.0f, 1.0f, 0.0f,
  292. 0.0f, 0.0f, 0.0f, 1.0f
  293. );
  294.  
  295. }
  296.  
  297. Matr4 lookat(Vect4 eye, Vect4 lookpoint, Vect4 up_vector) {
  298. Vect4 n = normalize(eye - lookpoint);
  299. Vect4 u = normalize(cross(up_vector, n));
  300. Vect4 v = normalize(cross(n, u));
  301.  
  302.  
  303.  
  304. return Matr4(u.x, u.y, u.z, dot(-eye, u),
  305. v.x, v.y, v.z, dot(-eye, v),
  306. n.x, n.y, n.z, dot(-eye, n),
  307. 0.0f, 0.0f, 0.0f, 1.0f
  308. );
  309. }
  310.  
  311. Matr4 perspective(float fov, float aspectratio, float n, float f) {
  312.  
  313. float t = fabs(n) * tan(fov / 2);
  314. float b = -t;
  315. float r = t * aspectratio;
  316. float l = -r;
  317.  
  318. Matr4 P((2.0*n) / (r - l), 0.0f, (r + l) / (r - l), 0.0f,
  319. 0.0f, (2.0*n) / (t - b), (t + b) / (t - b), 0.0f,
  320. 0.0f, 0.0f, -(f + n) / (f - n), -(2.0*f*n) / (f - n),
  321. 0.0f, 0.0f, -1.0f, 0.0f
  322. );
  323.  
  324. return (P);
  325. }
  326.  
  327. Matr4 viewport(float width, float height, float dw, float dh) {
  328. Matr4 V(width/2.0f, 0.0f, 0.0f, dw + width/2.0f,
  329. 0.0f,-height/2.0f, 0.0f, dh + height/2.0f,
  330. 0.0f, 0.0f, 1.0f, 0.0f,
  331. 0.0f, 0.0f, 0.0f, 1.0f);
  332.  
  333. return V;
  334. }
  335.  
  336.  
  337.  
  338. void processInput(GLFWwindow *window);
  339. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  340.  
  341. glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 10.0f);
  342. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
  343.  
  344. glm::vec3 cameraDefaultPos = glm::vec3(0.0f, 0.0f, 10.0f);
  345. glm::vec3 cameraDefaultFront = glm::vec3(0.0f, 0.0f, -1.0f);
  346.  
  347.  
  348. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
  349. glm::vec3 objCenter = glm::vec3(0.0f, 0.0f, 0.0f);
  350.  
  351. // timing
  352. float deltaTime = 0.0f; // time between current frame and last frame
  353. float lastFrame = 0.0f;
  354. float rotationx = 0.0f;
  355. float rotationy = 0.0f;
  356.  
  357.  
  358. float lastX = 320.0f;
  359. float lastY = 240.0f;
  360.  
  361. float pitch = 0.0f;
  362. float yaw = -90.0f;
  363.  
  364. int cw = 0;
  365. int cull = 1;
  366. int wireframe = 0;
  367.  
  368. int cameraType = 0;
  369.  
  370. GLint shadingmethod = 0;
  371.  
  372. int renderingmethod = OPENGL_MODE;
  373.  
  374. glm::vec4 colorm = glm::vec4(0.5f, 0.5f, 0.5f, 1.0f);
  375.  
  376. GLfloat nearplane = 1.0f;
  377. GLfloat farplane = 10000.0f;
  378.  
  379. float calcz = 0.0f;
  380. float calcdz = 0.0f;
  381.  
  382.  
  383. //float cullSum = 0.0f; //i`m so sorry
  384.  
  385. int mouseclicked = 0;
  386.  
  387. float aspectRatio = 640.0f / 480.0f;
  388.  
  389. int w_width = 640;
  390. int w_height = 480;
  391.  
  392. glm::vec4 light_dir = glm::vec4(2.0f, 2.0f, 2.0f, 1.0f);
  393.  
  394. // Função utilizada para calcular em que ponto a câmera precisa ficar para enquadrar o modelo. Recebe os vértices do modelo (points) e número de triângulos (NumTris).
  395. void calcCameraPos(GLfloat *points, int NumTris) {
  396.  
  397. int maxxset = 0;
  398. int minxset = 0;
  399. int maxyset = 0;
  400. int minyset = 0;
  401. int maxzset = 0;
  402. int minzset = 0;
  403.  
  404.  
  405. float minx = 0.0f;
  406. float maxx = 0.0f;
  407. float miny = 0.0f;
  408. float maxy = 0.0f;
  409. float minz = 0.0f;
  410. float maxz = 0.0f;
  411.  
  412.  
  413. int u = 0;
  414. while (u < NumTris * 3 * 3) {
  415. if (u % 3 == 0) {
  416. if (points[u] < minx || minxset == 0) {
  417. minxset = 1;
  418. minx = points[u];
  419. //printf("minx %d\n", u);
  420. }
  421. if (points[u] > maxx || maxxset == 0) {
  422. maxxset = 1;
  423. maxx = points[u];
  424. //printf("maxx %d\n", u);
  425. }
  426. }
  427. if (u % 3 == 1) {
  428. if (points[u] < miny || minyset == 0) {
  429. minyset = 1;
  430. miny = points[u];
  431. //printf("miny %d\n", u);
  432. }
  433. if (points[u] > maxy || maxyset == 0) {
  434. maxyset = 1;
  435. maxy = points[u];
  436. //printf("maxy %d\n", u);
  437. }
  438. }
  439. if (u % 3 == 2) {
  440. if (points[u] < minz || minzset == 0) {
  441. minzset = 1;
  442. minz = points[u];
  443. //printf("minz %d\n", u);
  444. }
  445. if (points[u] > maxz || maxzset == 0) {
  446. maxzset = 1;
  447. maxz = points[u];
  448. // printf("maxz %d\n", u);
  449. }
  450. }
  451. u++;
  452. }
  453.  
  454.  
  455. objCenter = glm::vec3(((maxx - minx) / 2.0f) + minx, ((maxy - miny) / 2.0f) + miny, ((maxz - minz) / 2.0f) + minz);
  456.  
  457. float camtanz;
  458. if ((maxx - minx / 2.0f) >= (maxy - miny / 2.0f))
  459. camtanz = glm::tan(glm::radians(30.0f))*(maxx - minx / 2.0f)*1.5f;
  460. else
  461. camtanz = glm::tan(glm::radians(30.0f))*(maxy - miny / 2.0f)*1.5f;
  462.  
  463. cameraPos = glm::vec3(((maxx - minx) / 2.0f) + minx, (maxy - miny) / 2.0f + miny, camtanz + 0.5f);
  464. cameraDefaultPos = cameraPos;
  465.  
  466. calcz = camtanz + 0.5f;
  467. calcdz = calcz;
  468.  
  469.  
  470. }
  471.  
  472. // Função que inicializa o Vertex Shader da parte OpenGL.
  473. GLuint initVSShaderOGL() {
  474.  
  475. const char* vertex_shader =
  476. "#version 460\n"
  477. "in vec3 position;"
  478. "in vec3 pnormal;"
  479. "in int colorindex;"
  480. "uniform mat4 ModelView;"
  481. "uniform mat4 ModelViewIT;"
  482. "uniform mat4 Proj;"
  483. "uniform vec4 colorm;"
  484. "uniform vec4 light_pos;"
  485. "uniform vec3 camera_position;"
  486. "uniform int shading_method;"
  487. "out vec4 color0;"
  488. "out vec4 mod_position;"
  489. "out vec4 mod_normal;"
  490. "void main() {"
  491. " vec4 view_position = ModelView * vec4(position, 1.0f);"
  492. " vec4 l = normalize(light_pos - view_position);"
  493. " color0 = colorm;"
  494. " mod_position = vec4(position, 1.0f);"
  495. " mod_normal = normalize(ModelViewIT * vec4(pnormal, 0.0f));"
  496. " vec4 v = normalize(- view_position);"
  497. " vec4 h = normalize(v + l);"
  498. " if (shading_method == 0)"
  499. " {color0 = vec4(0.1f, 0.1f, 0.1f, 1.0f) + 0.3f * colorm * max(0.0f, dot(mod_normal, l));}"
  500. " if (shading_method == 1)"
  501. " {color0 = vec4(0.1f, 0.1f, 0.1f, 1.0f) + 0.3f * colorm * (max(0.0f, dot(mod_normal, l)) + vec4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6 * max(0.0f, pow((dot(h, mod_normal)), 10)));}"
  502. " mod_normal = vec4(pnormal, 0.0f);"
  503. " gl_Position = Proj * view_position;"
  504. "}";
  505.  
  506. GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  507. glShaderSource(vs, 1, &vertex_shader, NULL);
  508. glCompileShader(vs);
  509.  
  510. GLint isCompiled = 0;
  511. glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
  512. if (isCompiled == GL_FALSE)
  513. {
  514. GLint maxLength = 0;
  515. glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
  516.  
  517. // The maxLength includes the NULL character
  518. char *errorLog = (char *) malloc(sizeof(char) * maxLength);
  519. glGetShaderInfoLog(vs, maxLength, &maxLength, errorLog);
  520.  
  521. printf("%s", errorLog);
  522. // Provide the infolog in whatever manor you deem best.
  523. // Exit with failure.
  524. glDeleteShader(vs); // Don't leak the shader.
  525. exit(1);
  526. }
  527. return vs;
  528. }
  529.  
  530. // Função que inicializa o Fragment Shader da parte OpenGL.
  531. GLuint initFSShaderOGL() {
  532. const char* fragment_shader =
  533. "#version 460\n"
  534. "in vec4 color0;"
  535. "in vec4 mod_position;"
  536. "in vec4 mod_normal;"
  537. "uniform vec4 colorm;"
  538. "uniform mat4 ModelView;"
  539. "uniform mat4 Proj;"
  540. "uniform int shading_method;"
  541. "uniform vec4 light_pos;"
  542. "uniform vec3 camera_position;"
  543. "out vec4 frag_color;"
  544. "void main() {"
  545. // " frag_color = vec4(0.5f, 0.0f, 0.5f, 1.0f);"
  546. " frag_color = color0;"
  547. " vec4 v = mod_position - vec4(camera_position, 1.0f);"
  548. " vec4 l = light_pos - (ModelView * mod_position);"
  549. " vec4 h = normalize (v + l) ;"
  550. " frag_color = color0;"
  551. " if (shading_method == 2)"
  552. // " frag_color = vec4(0.1f, 0.1f, 0.1f, 1.0f) + colorm * ( 0.3 *max(0.0f, dot(normalize(mod_normal), normalize(l))) + 0.6 * max(0.0f, pow((dot(h, normalize(mod_normal))), 10)));"
  553. " frag_color = vec4(1.0f, 0.25f, 0.4f, 1.0f);"
  554. "}";
  555.  
  556.  
  557. GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  558. glShaderSource(fs, 1, &fragment_shader, NULL);
  559. glCompileShader(fs);
  560.  
  561.  
  562. GLint isCompiled = 0;
  563. glGetShaderiv(fs, GL_COMPILE_STATUS, &isCompiled);
  564. if (isCompiled == GL_FALSE)
  565. {
  566. GLint maxLength = 0;
  567. glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
  568.  
  569. // The maxLength includes the NULL character
  570. char *errorLog = (char *)malloc(sizeof(char) * maxLength);
  571. glGetShaderInfoLog(fs, maxLength, &maxLength, errorLog);
  572.  
  573.  
  574. printf("%s", errorLog);
  575. // Provide the infolog in whatever manor you deem best.
  576. // Exit with failure.
  577. glDeleteShader(fs); // Don't leak the shader.
  578. exit(1);
  579. }
  580.  
  581. return fs;
  582. }
  583.  
  584. // Função que inicializa o Vertex Shader da parte Close2GL.
  585. GLuint initVSShaderC2GL() {
  586.  
  587. const char* vertex_shader =
  588. "#version 460\n"
  589. "in vec3 position;"
  590. "in vec2 texcoord;"
  591. "uniform vec4 colorm;"
  592. "uniform sampler2D tex;"
  593. "out vec4 color0;"
  594. "out vec2 tex_coord;"
  595. "void main() {"
  596. " gl_Position = vec4(position, 1.0f);"
  597. " color0 = colorm;"
  598. " tex_coord = texcoord;"
  599. "}";
  600.  
  601. GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  602. glShaderSource(vs, 1, &vertex_shader, NULL);
  603. glCompileShader(vs);
  604.  
  605. GLint isCompiled = 0;
  606. glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
  607. if (isCompiled == GL_FALSE)
  608. {
  609. GLint maxLength = 0;
  610. glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
  611.  
  612. // The maxLength includes the NULL character
  613. char *errorLog = (char *)malloc(sizeof(char) * maxLength);
  614. glGetShaderInfoLog(vs, maxLength, &maxLength, errorLog);
  615.  
  616. printf("%s", errorLog);
  617. // Provide the infolog in whatever manor you deem best.
  618. // Exit with failure.
  619. glDeleteShader(vs); // Don't leak the shader.
  620. exit(1);
  621. }
  622. return vs;
  623. }
  624.  
  625. // Função que inicializa o Fragment Shader da parte Close2GL.
  626. GLuint initFSShaderC2GL() {
  627. const char* fragment_shader =
  628. "#version 460\n"
  629. "in vec2 tex_coord;"
  630. "uniform vec4 colorm;"
  631. "uniform sampler2D tex;"
  632. "out vec4 frag_color;"
  633. "void main() {"
  634. //" frag_color = colorm;"
  635. " frag_color = texture(tex, tex_coord);"
  636. "}";
  637.  
  638. GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  639. glShaderSource(fs, 1, &fragment_shader, NULL);
  640. glCompileShader(fs);
  641.  
  642.  
  643. GLint isCompiled = 0;
  644. glGetShaderiv(fs, GL_COMPILE_STATUS, &isCompiled);
  645. if (isCompiled == GL_FALSE)
  646. {
  647. GLint maxLength = 0;
  648. glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
  649.  
  650. // The maxLength includes the NULL character
  651. char *errorLog = (char *)malloc(sizeof(char) * maxLength);
  652. glGetShaderInfoLog(fs, maxLength, &maxLength, errorLog);
  653.  
  654.  
  655. printf("%s", errorLog);
  656. // Provide the infolog in whatever manor you deem best.
  657. // Exit with failure.
  658. glDeleteShader(fs); // Don't leak the shader.
  659. exit(1);
  660. }
  661.  
  662. return fs;
  663. }
  664.  
  665.  
  666. int main(int argc, char *argv[]) {
  667. // start GL context and O/S window using the GLFW helper library
  668. //if (argc <= 1)
  669. // exit(1)
  670.  
  671. FILE *fp3;
  672. fp3 = fopen("show.txt", "w+");
  673. fwrite("oi", 1, 2, fp3);
  674. fclose(fp3);
  675.  
  676. if (!glfwInit()) {
  677. fprintf(stderr, "ERROR: could not start GLFW3\n");
  678. return 1;
  679. }
  680.  
  681. GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
  682. if (!window) {
  683. fprintf(stderr, "ERROR: could not open window with GLFW3\n");
  684. glfwTerminate();
  685. return 1;
  686. }
  687. glfwMakeContextCurrent(window);
  688.  
  689. // start GLEW extension handler
  690. glewExperimental = GL_TRUE;
  691. glewInit();
  692.  
  693. // get version info
  694. const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
  695. const GLubyte* version = glGetString(GL_VERSION); // version as a string
  696. printf("Renderer: %s\n", renderer);
  697. printf("OpenGL version supported %s\n", version);
  698.  
  699. // tell GL to only draw onto a pixel if the shape is closer to the viewer
  700. glEnable(GL_DEPTH_TEST); // enable depth-testing
  701. glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
  702.  
  703. glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
  704. glfwSetCursorPosCallback(window, mouse_callback);
  705.  
  706.  
  707. // inicializa os shaders
  708. GLuint vs = initVSShaderOGL();
  709. GLuint fs = initFSShaderOGL();
  710. GLuint c2vs = initVSShaderC2GL();
  711. GLuint c2fs = initFSShaderC2GL();
  712.  
  713. GLuint shader_program = glCreateProgram();
  714. glAttachShader(shader_program, fs);
  715. glAttachShader(shader_program, vs);
  716.  
  717. glBindAttribLocation(shader_program, 0, "position");
  718. glBindAttribLocation(shader_program, 1, "pnormal");
  719. glBindAttribLocation(shader_program, 2, "colorindex");
  720.  
  721. glLinkProgram(shader_program);
  722. glValidateProgram(shader_program);
  723.  
  724. GLuint c2gl_shader_program = glCreateProgram();
  725. glAttachShader(c2gl_shader_program, c2fs);
  726. glAttachShader(c2gl_shader_program, c2vs);
  727.  
  728. glBindAttribLocation(c2gl_shader_program, 0, "position");
  729. glBindAttribLocation(c2gl_shader_program, 1, "texcoord");
  730.  
  731. glLinkProgram(c2gl_shader_program);
  732. glValidateProgram(c2gl_shader_program);
  733.  
  734.  
  735. // parsing do arquivo
  736. char ch;
  737. int NumTris;
  738. int material_count;
  739. FILE *fp;
  740. if (argc <= 1)
  741. fp = fopen("cow_up.in", "r");
  742. else
  743. {
  744. fp = fopen(argv[1], "r"); if (argc >= 3 && argv[2][0] == 't') cw = 1;
  745. }
  746.  
  747. if (fp == NULL) { printf("ERROR: unable to open TriObj !\n"); exit(1); }
  748.  
  749. fscanf(fp, "%c", &ch);
  750.  
  751. while (ch != '\n') // skip the first line – object’s name
  752. fscanf(fp, "%c", &ch);
  753.  
  754. fscanf(fp, "# triangles = %d\n", &NumTris); // read # of triangles
  755. fscanf(fp, "Material count = %d\n", &material_count); // read material count
  756.  
  757.  
  758. glm::vec3 *ambient = (glm::vec3 *) malloc (sizeof(glm::vec3) * material_count);
  759. glm::vec3 *diffuse = (glm::vec3 *) malloc(sizeof(glm::vec3) * material_count);
  760. glm::vec3 *specular = (glm::vec3 *) malloc(sizeof(glm::vec3) * material_count);
  761. glm::vec3 *shine = (glm::vec3 *) malloc(sizeof(glm::vec3) * material_count);
  762.  
  763.  
  764. for (int i = 0; i<material_count; i++) {
  765. fscanf(fp, "ambient color %f %f %f\n", &(ambient[i].x), &(ambient[i].y), &(ambient[i].z));
  766. fscanf(fp, "diffuse color %f %f %f\n", &(diffuse[i].x), &(diffuse[i].y), &(diffuse[i].z));
  767. fscanf(fp, "specular color %f %f %f\n", &(specular[i].x), &(specular[i].y), &(specular[i].z));
  768. fscanf(fp, "material shine %f\n", &(shine[i]));
  769. }
  770.  
  771. fscanf(fp, "%c", &ch);
  772. while (ch != '\n') // skip documentation line
  773. fscanf(fp, "%c", &ch);
  774.  
  775. GLfloat *points = (GLfloat *) malloc(sizeof(GLfloat) * NumTris * 3 * 3);
  776. GLfloat *pnormals = (GLfloat *) malloc(sizeof(GLfloat) * NumTris * 4 * 3);
  777. GLfloat *fnormals = (GLfloat *) malloc(sizeof(GLfloat) * NumTris * 3);
  778. GLuint *indices = (GLuint *) malloc(sizeof(GLuint) * NumTris * 3);
  779. GLuint *color_index = (GLuint *) malloc(sizeof(GLuint) * NumTris * 3);
  780.  
  781. for (int i = 0; i<NumTris; i++) // read triangles
  782. {
  783. fscanf(fp, "v0 %f %f %f %f %f %f %d\n",
  784. &(points[9 * i]), &(points[9 * i + 1]), &(points[9 * i + 2]),
  785. &(pnormals[9 * i]), &(pnormals[9 * i + 1]), &(pnormals[9 * i + 2]),
  786. &(color_index[3 * i]));
  787. fscanf(fp, "v1 %f %f %f %f %f %f %d\n",
  788. &(points[9 * i + 3]), &(points[9 * i + 4]), &(points[9 * i + 5]),
  789. &(pnormals[9 * i + 3]), &(pnormals[9 * i + 4]), &(pnormals[9 * i + 5]),
  790. &(color_index[3 * i + 1]));
  791. fscanf(fp, "v2 %f %f %f %f %f %f %d\n",
  792. &(points[9 * i + 6]), &(points[9 * i + 7]), &(points[9 * i + 8]),
  793. &(pnormals[9 * i + 6]), &(pnormals[9 * i + 7]), &(pnormals[9 * i + 8]),
  794. &(color_index[3 * i + 2]));
  795. fscanf(fp, "face normal %f %f %f\n", &(fnormals[3 * i]), &(fnormals[3 * i + 1]), &(fnormals[3 * i + 2]));
  796.  
  797. // /*
  798. if (cw == 0) {
  799. indices[3 * i] = 3 * i;
  800. indices[3 * i + 1] = 3 * i + 1;
  801. indices[3 * i + 2] = 3 * i + 2;
  802. }
  803. else {
  804. indices[3 * i] = 3 * i + 2;
  805. indices[3 * i + 1] = 3 * i + 1;
  806. indices[3 * i + 2] = 3 * i;
  807. }
  808. // */
  809. }
  810. fclose(fp);
  811.  
  812.  
  813. // passa os vértices para a GPU (OpenGL)
  814. calcCameraPos(points, NumTris);
  815.  
  816. GLuint vbo[3];
  817. glGenBuffers(3, vbo);
  818.  
  819. GLuint EBO;
  820. glGenBuffers(1, &EBO);
  821.  
  822. GLuint c2vbo;
  823. glGenBuffers(1, &c2vbo);
  824.  
  825. GLuint c2EBO;
  826. glGenBuffers(1, &c2EBO);
  827.  
  828. GLuint c2TBO;
  829. glGenBuffers(1, &c2TBO);
  830.  
  831. glActiveTexture(GL_TEXTURE1);
  832.  
  833. GLuint c2Tex;
  834. glGenTextures(1, &c2Tex);
  835.  
  836. GLuint vao = 0;
  837. GLuint c2vao = 0;
  838. glGenVertexArrays(1, &vao);
  839. glGenVertexArrays(1, &c2vao);
  840. glBindVertexArray(vao);
  841.  
  842. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  843. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * NumTris * 3 * 3, points, GL_STATIC_DRAW);
  844.  
  845. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  846. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * NumTris * 3, indices, GL_STATIC_DRAW);
  847.  
  848. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  849. glEnableVertexAttribArray(0);
  850.  
  851. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  852. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * NumTris * 4 * 3, pnormals, GL_STATIC_DRAW);
  853. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
  854. glEnableVertexAttribArray(1);
  855.  
  856. glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
  857. glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint) * NumTris * 3, color_index, GL_STATIC_DRAW);
  858. glVertexAttribPointer(2, 1, GL_UNSIGNED_INT, GL_FALSE, 0, 0);
  859. glEnableVertexAttribArray(2);
  860.  
  861.  
  862. glUseProgram(shader_program);
  863.  
  864. Matr4 model = identity(); // Sempre será identidade, o que muda é a câmera
  865.  
  866. char windowTitle[32];
  867.  
  868. float *new_points = (float *) malloc (sizeof(float) * NumTris * 3 * 3);
  869. GLuint *new_indexes = (GLuint *) malloc (sizeof(GLuint) * NumTris * 3 * 3);
  870.  
  871. while (!glfwWindowShouldClose(window)) {
  872. // wipe the drawing surface clear
  873.  
  874. // Calcula o FPS para mostrar no title
  875. float currentFrame = glfwGetTime();
  876. deltaTime = currentFrame - lastFrame;
  877. lastFrame = currentFrame;
  878.  
  879.  
  880. // liga/desliga o modo wireframe
  881. if (wireframe == 1)
  882. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  883. if (wireframe == 0)
  884. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  885.  
  886.  
  887. // liga/desliga o culling (OpenGL)
  888. if (cull == 1 && renderingmethod == OPENGL_MODE)
  889. glEnable(GL_CULL_FACE);
  890. else
  891. glDisable(GL_CULL_FACE);
  892.  
  893.  
  894. // escolhe o modo de renderização (OpenGl ou Close2Gl)
  895. if (renderingmethod == OPENGL_MODE)
  896. glUseProgram(shader_program);
  897. else
  898. glUseProgram(c2gl_shader_program);
  899.  
  900. // Atualiza o title da janela (FPS)
  901. snprintf(windowTitle, sizeof(windowTitle), "FPS: %f\n", 1.0f / deltaTime);
  902. glfwSetWindowTitle(window, windowTitle);
  903.  
  904. // Processa os inputs
  905. processInput(window);
  906.  
  907.  
  908. // Limpa o buffer de cor e o z-buffer
  909. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  910.  
  911.  
  912. // Calcula a projection matrix
  913. Matr4 proj = perspective(M_PI / 3.0f, aspectRatio, nearplane, farplane);
  914.  
  915.  
  916. // Calcula a matriz view dependendo do tipo de câmera (look-at ou free camera)
  917. Matr4 view;
  918. if (cameraType == 0) {
  919. view = lookat(Vect4(sin(rotationx) * (calcz + 2.0f), 0.0f, cos(rotationx)*(calcz + 2.0f), 0.0f) + Vect4(objCenter.x, objCenter.y, objCenter.z, 1.0f), Vect4(objCenter.x, objCenter.y, objCenter.z, 1.0f), Vect4(cameraUp.x, cameraUp.y, cameraUp.z, 0.0f));
  920. }
  921. else {
  922. view = lookat(Vect4(cameraPos.x, cameraPos.y, cameraPos.z, 1.0f), Vect4(cameraPos.x + cameraFront.x, cameraPos.y + cameraFront.y, cameraPos.z + cameraFront.z, 1.0f), Vect4(cameraUp.x, cameraUp.y, cameraUp.z, 0.0f));
  923. }
  924.  
  925. // Model View Proj calculada
  926. Matr4 ModelView = (view * model);
  927. Matr4 ModelViewIT = (ModelView.inverse()).transpose();
  928. Matr4 ModelViewProj = proj * ModelView;
  929.  
  930. glm::vec3 trueCameraPosition;
  931. if (cameraType == 0) {
  932. trueCameraPosition = glm::vec3(glm::sin(rotationx) * (calcz + 2.0f), 0.0f, glm::cos(rotationx)*(calcz + 2.0f)) + objCenter;
  933. }
  934. else {
  935. trueCameraPosition = cameraPos;
  936. }
  937.  
  938. int trueTriCount = 0;
  939.  
  940. // Passa os vértices para a etapa de rasterização (Close2Gl)
  941. if (renderingmethod == CLOSE2GL_MODE) {
  942. int i = 0;
  943.  
  944. float* vertex_colors = (float *) malloc (sizeof(float) * NumTris * 3 * 5);
  945.  
  946. while (i < NumTris * 3 * 3) {
  947.  
  948. Vect4 a(points[i], points[i + 1], points[i + 2], 1.0f);
  949. Vect4 b(points[i + 3], points[i + 4], points[i + 5], 1.0f);
  950. Vect4 c(points[i + 6], points[i + 7], points[i + 8], 1.0f);
  951.  
  952. Vect4 na(pnormals[i], pnormals[i + 1], pnormals[i + 2], 1.0f);
  953. Vect4 nb(pnormals[i + 3], pnormals[i + 4], pnormals[i + 5], 1.0f);
  954. Vect4 nc(pnormals[i + 6], pnormals[i + 7], pnormals[i + 8], 1.0f);
  955.  
  956. Vect4 newCA = ModelView * a;
  957. Vect4 newCB = ModelView * b;
  958. Vect4 newCC = ModelView * c;
  959.  
  960. Vect4 newNA = ModelViewIT * na;
  961. Vect4 newNB = ModelViewIT * nb;
  962. Vect4 newNC = ModelViewIT * nc;
  963.  
  964. Vect4 la(newCA.x - light_dir.x, newCA.y - light_dir.y, newCA.z - light_dir.z, 0.0f);
  965. Vect4 lb(newCB.x - light_dir.x, newCB.y - light_dir.y, newCB.z - light_dir.z, 0.0f);
  966. Vect4 lc(newCC.x - light_dir.x, newCC.y - light_dir.y, newCC.z - light_dir.z, 0.0f);
  967.  
  968. Vect4 camera_position(trueCameraPosition.x, trueCameraPosition.y, trueCameraPosition.z, 1.0f);
  969.  
  970. Vect4 va = newCA - camera_position;
  971. Vect4 vb = newCB - camera_position;
  972. Vect4 vc = newCC - camera_position;
  973.  
  974. Vect4 Ha = normalize(va + la);
  975. Vect4 Hb = normalize(vb + lb);
  976. Vect4 Hc = normalize(vc + lc);
  977.  
  978.  
  979.  
  980. Vect4 newA = proj * newCA;
  981.  
  982. Vect4 newB = proj * newCB;
  983.  
  984. Vect4 newC = proj * newCC;
  985.  
  986. // Checa o w para não projetar negativos ou dividir por 0
  987. if (newA.w > 0) {
  988. if (newB.w > 0) {
  989. if (newC.w > 0) {
  990. float AW = newA.w;
  991. float BW = newB.w;
  992. float CW = newC.w;
  993. newA = newA / newA.w;
  994. newB = newB / newB.w;
  995. newC = newC / newC.w;
  996.  
  997.  
  998. // Testa o culling de acordo com o OpenGL Red Book
  999. float cullSum = 0.0f;
  1000. cullSum += (newA.x + 1) * (newB.y + 1) - (newB.x + 1) * (newA.y + 1);
  1001. cullSum += (newB.x + 1) * (newC.y + 1) - (newC.x + 1) * (newB.y + 1);
  1002. cullSum += (newC.x + 1) * (newA.y + 1) - (newA.x + 1) * (newC.y + 1);
  1003. cullSum = cullSum / 2.0f;
  1004.  
  1005. if ((cull == 0 || cullSum < 0)
  1006. && ((newA.x < 1.0f) && (newA.x > -1.0f) && (newA.y < 1.0f) && (newA.y > -1.0f) && (newA.z < 1.0f) && (newA.z > -1.0f))
  1007. && ((newB.x < 1.0f) && (newB.x > -1.0f) && (newB.y < 1.0f) && (newB.y > -1.0f) && (newB.z < 1.0f) && (newB.z > -1.0f))
  1008. && ((newC.x < 1.0f) && (newC.x > -1.0f) && (newC.y < 1.0f) && (newC.y > -1.0f) && (newC.z < 1.0f) && (newC.z > -1.0f))) {
  1009. new_points[trueTriCount * 9] = newA.x;
  1010. new_points[trueTriCount * 9 + 1] = newA.y;
  1011. new_points[trueTriCount * 9 + 2] = newA.z;
  1012. new_points[trueTriCount * 9 + 3] = newB.x;
  1013. new_points[trueTriCount * 9 + 4] = newB.y;
  1014. new_points[trueTriCount * 9 + 5] = newB.z;
  1015. new_points[trueTriCount * 9 + 6] = newC.x;
  1016. new_points[trueTriCount * 9 + 7] = newC.y;
  1017. new_points[trueTriCount * 9 + 8] = newC.z;
  1018.  
  1019. new_indexes[trueTriCount * 9] = indices[i];
  1020. new_indexes[trueTriCount * 9 + 1] = indices[i + 1];
  1021. new_indexes[trueTriCount * 9 + 2] = indices[i + 2];
  1022. new_indexes[trueTriCount * 9 + 3] = indices[i + 3];
  1023. new_indexes[trueTriCount * 9 + 4] = indices[i + 4];
  1024. new_indexes[trueTriCount * 9 + 5] = indices[i + 5];
  1025. new_indexes[trueTriCount * 9 + 6] = indices[i + 6];
  1026. new_indexes[trueTriCount * 9 + 7] = indices[i + 7];
  1027. new_indexes[trueTriCount * 9 + 8] = indices[i + 8];
  1028.  
  1029. Vect4 colorA = Vect4(0.1f, 0.1f, 0.1f, 1.0f)
  1030. + Vect4(colorm.x, colorm.y, colorm.z, colorm.w) * (0.3f * std::max(0.0f, dot(normalize(newNA), normalize(la))))
  1031. + Vect4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6f * std::max(0.0f, pow((dot(Ha, normalize(newNA))), 10));
  1032. Vect4 colorB = Vect4(0.1f, 0.1f, 0.1f, 1.0f)
  1033. + Vect4(colorm.x, colorm.y, colorm.z, colorm.w) * (0.3f * std::max(0.0f, dot(normalize(newNB), normalize(lb))))
  1034. + Vect4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6f * std::max(0.0f, pow((dot(Hb, normalize(newNB))), 10));
  1035. Vect4 colorC = Vect4(0.1f, 0.1f, 0.1f, 1.0f)
  1036. + Vect4(colorm.x, colorm.y, colorm.z, colorm.w) * (0.3f * std::max(0.0f, dot(normalize(newNC), normalize(lc))))
  1037. + Vect4(1.0f, 1.0f, 1.0f, 1.0f) * 0.6f * std::max(0.0f, pow((dot(Hc, normalize(newNC))), 10));
  1038.  
  1039. vertex_colors[trueTriCount * 15] = colorA.x / AW;
  1040. vertex_colors[trueTriCount * 15 + 1] = colorA.y / AW;
  1041. vertex_colors[trueTriCount * 15 + 2] = colorA.z / AW;
  1042. vertex_colors[trueTriCount * 15 + 3] = colorA.w / AW;
  1043. vertex_colors[trueTriCount * 15 + 4] = 1.0f / AW;
  1044. vertex_colors[trueTriCount * 15 + 5] = colorB.x / BW;
  1045. vertex_colors[trueTriCount * 15 + 6] = colorB.y / BW;
  1046. vertex_colors[trueTriCount * 15 + 7] = colorB.z / BW;
  1047. vertex_colors[trueTriCount * 15 + 8] = colorB.w / BW;
  1048. vertex_colors[trueTriCount * 15 + 9] = 1.0f / BW;
  1049. vertex_colors[trueTriCount * 15 + 10] = colorC.x / CW;
  1050. vertex_colors[trueTriCount * 15 + 11] = colorC.y / CW;
  1051. vertex_colors[trueTriCount * 15 + 12] = colorC.z / CW;
  1052. vertex_colors[trueTriCount * 15 + 13] = colorC.w / CW;
  1053. vertex_colors[trueTriCount * 15 + 14] = 1.0f / CW;
  1054.  
  1055.  
  1056. trueTriCount++;
  1057. }
  1058. }
  1059. }
  1060. }
  1061.  
  1062. i += 9;
  1063. }
  1064.  
  1065. float vertex_data[] = { -1.0f, -1.0f, 0.5f, 1.0f, -1.0f, 0.5f, 1.0f, 1.0f, 0.5f,
  1066. 1.0f, 1.0f, 0.5f, -1.0f, 1.0f, -0.5f, -1.0f, -1.0f, 0.5f };
  1067.  
  1068. int vertex_index[] = { 0, 1, 2, 3, 4, 5};
  1069.  
  1070. float texture_coords[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  1071. 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f };
  1072.  
  1073. glBindVertexArray(c2vao);
  1074. /*
  1075. glBindBuffer(GL_ARRAY_BUFFER, c2vbo);
  1076. glBufferData(GL_ARRAY_BUFFER, trueTriCount * 3 * 3 * sizeof(float), new_points, GL_STATIC_DRAW);
  1077.  
  1078. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, c2EBO);
  1079. glBufferData(GL_ELEMENT_ARRAY_BUFFER, trueTriCount * 3 * sizeof(int), indices, GL_STATIC_DRAW);
  1080.  
  1081. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  1082.  
  1083. glEnableVertexAttribArray(0);
  1084. */
  1085.  
  1086. // glDisable(GL_CULL_FACE);
  1087.  
  1088. glBindBuffer(GL_ARRAY_BUFFER, c2vbo);
  1089. glBufferData(GL_ARRAY_BUFFER, 3*3*2*sizeof(float), vertex_data, GL_STATIC_DRAW);
  1090.  
  1091. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, c2EBO);
  1092. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * 3 * sizeof(int), vertex_index, GL_STATIC_DRAW);
  1093.  
  1094. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  1095.  
  1096. glEnableVertexAttribArray(0);
  1097.  
  1098. glBindBuffer(GL_ARRAY_BUFFER, c2TBO);
  1099. glBufferData(GL_ARRAY_BUFFER, 2 * 3 * 2 * sizeof(float), texture_coords, GL_STATIC_DRAW);
  1100.  
  1101. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
  1102.  
  1103. glEnableVertexAttribArray(1);
  1104.  
  1105. float *color_buffer = (float *) calloc (w_width * w_height * 4, sizeof(float));
  1106. float *depth_buffer = (float *) calloc (w_width * w_height, sizeof(float));
  1107.  
  1108. Matr4 v = viewport(w_width, w_height, 0.0f, 0.0f);
  1109.  
  1110.  
  1111. for (int t = 0; t < trueTriCount; t++) {
  1112. Vect4 d (new_points[t * 9], new_points[t * 9 + 1], new_points[t * 9 + 2], 1.0f);
  1113. Vect4 e (new_points[t * 9 + 3], new_points[t * 9 + 4], new_points[t * 9 + 5], 1.0f);
  1114. Vect4 f (new_points[t * 9 + 6], new_points[t * 9 + 7], new_points[t * 9 + 8], 1.0f);
  1115.  
  1116. int a_ix = 0;
  1117. int b_ix = 1;
  1118. int c_ix = 2;
  1119.  
  1120. Vect4 a = v * d;
  1121. Vect4 b = v * e;
  1122. Vect4 c = v * f;
  1123.  
  1124.  
  1125. if (a.y > b.y) {
  1126. Vect4 aux = a;
  1127. a = b;
  1128. b = aux;
  1129. a_ix += b_ix;
  1130. b_ix = a_ix - b_ix;
  1131. a_ix -= b_ix;
  1132. }
  1133. if (a.y > c.y) {
  1134. Vect4 aux = a;
  1135. a = c;
  1136. c = aux;
  1137. a_ix += c_ix;
  1138. c_ix = a_ix - c_ix;
  1139. a_ix -= c_ix;
  1140. }
  1141. if (b.y > c.y) {
  1142. Vect4 aux = b;
  1143. b = c;
  1144. c = aux;
  1145. b_ix += c_ix;
  1146. c_ix = b_ix - c_ix;
  1147. b_ix -= c_ix;
  1148. }
  1149.  
  1150.  
  1151.  
  1152. if ((a.y == b.y) || (b.y == c.y) || (a.y == c.y)) {
  1153. if (a.y == b.y && b.y != c.y) {
  1154. int ii1 = round(a.x);
  1155. int ij = (int)(a.y);
  1156. int ii2 = round(b.x);
  1157.  
  1158. if (ii1 > ii2) {
  1159. ii1 = ii2 + ii1;
  1160. ii2 = ii1 - ii2;
  1161. ii1 = ii1 - ii2;
  1162. }
  1163.  
  1164. for (int i = ii1; i <= ii2; i++) {
  1165. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij)] = 1.0f;
  1166. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij) + 1] = 0.0f;
  1167. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij) + 2] = 0.0f;
  1168. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - ij) + 3] = 1.0f;
  1169. }
  1170.  
  1171. float dx = c.x - a.x;
  1172. float dy = c.y - a.y;
  1173. float dz = c.z - a.z;
  1174.  
  1175. float ix = dx / dy;
  1176. float iz = dz / dy;
  1177.  
  1178. float dx2 = c.x - b.x;
  1179. float dy2 = c.y - b.y;
  1180. float dz2 = c.z - b.z;
  1181.  
  1182. float ix2 = dx2 / dy2;
  1183. float iz2 = dz2 / dy2;
  1184.  
  1185. for (int cy = 1; cy < dy2; cy++) {
  1186. int i1 = round(a.x + ix * cy);
  1187. int j = (int)(a.y + cy);
  1188. int i2 = round(a.x + ix2 * cy);
  1189.  
  1190. if (i1 > i2) {
  1191. i1 = i2 + i1;
  1192. i2 = i1 - i2;
  1193. i1 = i1 - i2;
  1194. }
  1195.  
  1196. for (int i = i1; i <= i2; i++) {
  1197. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = 1.0f;
  1198. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = 0.0f;
  1199. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = 0.0f;
  1200. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = 1.0f;
  1201. }
  1202.  
  1203. }
  1204.  
  1205. }
  1206.  
  1207. else if (b.y == c.y && a.y != c.y) {
  1208. int i = round(a.x);
  1209. int j = (int) a.y;
  1210.  
  1211.  
  1212. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = 1.0f;
  1213. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = 0.0f;
  1214. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = 0.0f;
  1215. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = 1.0f;
  1216.  
  1217. float dx = b.x - a.x;
  1218. float dy = b.y - a.y;
  1219. float dz = b.z - a.z;
  1220.  
  1221. float ix = dx / dy;
  1222. float iz = dz / dy;
  1223.  
  1224. float dx2 = c.x - a.x;
  1225. float dy2 = c.y - a.y;
  1226. float dz2 = c.z - a.z;
  1227.  
  1228. float ix2 = dx2 / dy2;
  1229. float iz2 = dz2 / dy2;
  1230.  
  1231. for (int cy = 1; cy < dy2; cy++) {
  1232. int i1 = round(a.x + ix * cy);
  1233. int j = (int)(a.y + cy);
  1234. int i2 = round(a.x + ix2 * cy);
  1235.  
  1236. if (i1 > i2) {
  1237. i1 = i2 + i1;
  1238. i2 = i1 - i2;
  1239. i1 = i1 - i2;
  1240. }
  1241.  
  1242. for (int i = i1; i <= i2; i++) {
  1243. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = 1.0f;
  1244. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = 0.0f;
  1245. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = 0.0f;
  1246. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = 1.0f;
  1247. }
  1248.  
  1249. }
  1250. }
  1251.  
  1252. }
  1253. else {
  1254. float dx = b.x - a.x;
  1255. float dy = b.y - a.y;
  1256. float dz = b.z - a.z;
  1257.  
  1258. float sq_d = pow(dx, 2) + pow(dy, 2);
  1259.  
  1260. float ix = dx / dy;
  1261. float iz = dz / dy;
  1262.  
  1263. float dx2 = c.x - a.x;
  1264. float dy2 = c.y - a.y;
  1265. float dz2 = c.z - a.z;
  1266.  
  1267. float sq_d2 = pow(dx2, 2) + pow(dy2, 2);
  1268.  
  1269. float ix2 = dx2 / dy2;
  1270. float iz2 = dz2 / dy2;
  1271.  
  1272. float x0 = a.x;
  1273.  
  1274. int offset = 0;
  1275.  
  1276. int line_change = 0;
  1277.  
  1278. Vect4 colora(vertex_colors[t * 15], vertex_colors[t * 15 + 1], vertex_colors[t * 15 + 2], vertex_colors[t * 15 + 3]);
  1279. float aw = vertex_colors[t*15 + 4];
  1280. Vect4 colorb(vertex_colors[t * 15 + 5], vertex_colors[t * 15 + 6], vertex_colors[t * 15 + 7], vertex_colors[t * 15 + 8]);
  1281. float bw = vertex_colors[t * 15 + 9];
  1282. Vect4 colorc(vertex_colors[t * 15 + 10], vertex_colors[t * 15 + 11], vertex_colors[t * 15 + 12], vertex_colors[t * 15 + 13]);
  1283. float cw = vertex_colors[t * 15 + 14];
  1284.  
  1285. for (int cy = 0; cy < dy2; cy++) {
  1286. if ((line_change == 0) && (cy >= dy)) {
  1287. offset = cy;
  1288.  
  1289. dx = c.x - b.x;
  1290. dy = c.y - b.y;
  1291. dz = c.z - b.z;
  1292. sq_d = pow(dx, 2) + pow(dy, 2);
  1293. ix = dx / dy;
  1294. iz = dz / dy;
  1295.  
  1296. x0 = b.x;
  1297.  
  1298. line_change = 1;
  1299. }
  1300.  
  1301. float f1 = x0 + ix * (cy - offset);
  1302. float f2 = a.x + ix2 * cy;
  1303. int i1 = round(f1);
  1304. int j = (int)(a.y + cy);
  1305. int i2 = round(f2);
  1306.  
  1307. float alpha1 = sqrt((pow(f1 - x0, 2) + pow(cy - offset, 2)) / sq_d);
  1308. float alpha2 = sqrt((pow(f2 - a.x, 2) + pow(cy, 2)) / sq_d2);
  1309.  
  1310. Vect4 colorleft = (1 - line_change) * (colora * alpha1 + colorb * (1-alpha1)) + (line_change) * (colorb * alpha1 + colorc * (1-alpha1));
  1311. Vect4 colorright = colora * alpha2 + colorc * (1-alpha2);
  1312.  
  1313. float wleft = (1 - line_change) * (aw * alpha1 + bw * (1 - alpha1)) + (line_change)* (bw * alpha1 + cw * (1 - alpha1));
  1314. float wright = aw * alpha2 + cw * (1 - alpha2);
  1315.  
  1316. if (i1 > i2) {
  1317. i1 = i2 + i1;
  1318. i2 = i1 - i2;
  1319. i1 = i1 - i2;
  1320.  
  1321. colorleft = colorleft + colorright;
  1322. colorright = colorleft - colorright;
  1323. colorleft = colorleft - colorright;
  1324.  
  1325. wleft = wright + wleft;
  1326. wright = wleft - wright;
  1327. wleft = wleft - wright;
  1328.  
  1329. }
  1330.  
  1331. for (int i = i1; i <= i2; i++) {
  1332. float thirdalpha;
  1333. if (i2 == i1 && i == i1)
  1334. thirdalpha = 0.0f;
  1335. else
  1336. thirdalpha = (i - i1) / (i2 - i1);
  1337. float finalw = wleft * thirdalpha + wright * (1 - thirdalpha);
  1338. Vect4 finalcolor = colorleft * thirdalpha + colorright * (1 - thirdalpha);
  1339. finalcolor = finalcolor / finalw;
  1340. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j)] = finalcolor.x;
  1341. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 1] = finalcolor.y;
  1342. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 2] = finalcolor.z;
  1343. color_buffer[i * 4 + (w_width * 4) * (w_height - 1 - j) + 3] = finalcolor.w;
  1344. }
  1345. //printf("1 %d\n", t);
  1346.  
  1347. }
  1348.  
  1349. }
  1350.  
  1351. }
  1352.  
  1353. glBindTexture(GL_TEXTURE_2D, c2Tex);
  1354. glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA4, w_width, w_height);
  1355. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w_width, w_height, GL_RGBA, GL_FLOAT, color_buffer);
  1356. free(color_buffer);
  1357. free(depth_buffer);
  1358. free(vertex_colors);
  1359. int texLoc = glGetUniformLocation(c2gl_shader_program, "tex");
  1360. glUniform1i(texLoc, 1);
  1361.  
  1362. }
  1363.  
  1364. // Passa as uniformes para o método Open GL
  1365. if (renderingmethod == OPENGL_MODE) {
  1366. int matLoc = glGetUniformLocation(shader_program, "ModelView");
  1367. glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr((ModelView).convertToGLM()));
  1368.  
  1369. matLoc = glGetUniformLocation(shader_program, "Proj");
  1370. glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr((proj).convertToGLM()));
  1371.  
  1372. matLoc = glGetUniformLocation(shader_program, "ModelViewIT");
  1373. glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr((ModelViewIT).convertToGLM()));
  1374.  
  1375. int colLoc = glGetUniformLocation(shader_program, "colorm");
  1376. glUniform4fv(colLoc, 1, glm::value_ptr(colorm));
  1377.  
  1378. int smeLoc = glGetUniformLocation(shader_program, "shading_method");
  1379. glUniform1i(smeLoc, shadingmethod);
  1380.  
  1381. int camLoc = glGetUniformLocation(shader_program, "camera_position");
  1382. glUniform3fv(camLoc, 1, glm::value_ptr(trueCameraPosition));
  1383.  
  1384. int ligLoc = glGetUniformLocation(shader_program, "light_pos");
  1385. glUniform4fv(ligLoc, 1, glm::value_ptr(light_dir));
  1386. }
  1387. else {
  1388. int colLoc = glGetUniformLocation(c2gl_shader_program, "colorm");
  1389. glUniform4fv(colLoc, 1, glm::value_ptr(colorm));
  1390. }
  1391.  
  1392.  
  1393. if (renderingmethod == OPENGL_MODE) {
  1394. glBindVertexArray(vao);
  1395. glDrawElements(GL_TRIANGLES, NumTris * 3, GL_UNSIGNED_INT, 0);
  1396. }
  1397. else {
  1398. glBindVertexArray(c2vao);
  1399. //glDrawElements(GL_TRIANGLES, trueTriCount * 4, GL_UNSIGNED_INT, 0);
  1400. glDrawElements(GL_TRIANGLES, 2*3, GL_UNSIGNED_INT, 0);
  1401. }
  1402. glfwPollEvents();
  1403. glfwSwapBuffers(window);
  1404. }
  1405. // close GL context and any other GLFW resources
  1406. glfwTerminate();
  1407.  
  1408. return 0;
  1409. }
  1410.  
  1411.  
  1412. void processInput(GLFWwindow *window)
  1413. {
  1414. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
  1415. glfwSetWindowShouldClose(window, true);
  1416. }
  1417.  
  1418. float cameraSpeed = 50 * deltaTime;
  1419. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  1420. if (cameraType == 0)
  1421. calcz -= cameraSpeed;
  1422. else
  1423. cameraPos += cameraSpeed * cameraFront;
  1424. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  1425. if (cameraType == 0)
  1426. calcz += cameraSpeed;
  1427. else
  1428. cameraPos -= cameraSpeed * cameraFront;
  1429.  
  1430. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  1431. if (cameraType == 0)
  1432. rotationx -= cameraSpeed;
  1433. else
  1434. cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  1435. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  1436. if (cameraType == 0)
  1437. rotationx += cameraSpeed;
  1438. else
  1439. cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  1440.  
  1441. if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
  1442. if (cameraType == 1)
  1443. cameraPos += cameraUp * cameraSpeed;
  1444.  
  1445. if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
  1446. if (cameraType == 1)
  1447. cameraPos -= cameraUp * cameraSpeed;
  1448.  
  1449. if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
  1450. {
  1451. nearplane -= 400.0f * deltaTime; printf("Nearplane: %f\n", nearplane);
  1452. }
  1453.  
  1454. if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
  1455. {
  1456. nearplane += 400.0f * deltaTime; printf("Nearplane: %f\n", nearplane);
  1457. }
  1458.  
  1459. if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
  1460. {
  1461. farplane -= 400.0f * deltaTime; printf("Farplane: %f\n", farplane);
  1462. }
  1463.  
  1464. if (glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS)
  1465. {
  1466. farplane += 400.0f * deltaTime; printf("Farplane: %f\n", farplane);
  1467. }
  1468.  
  1469. if (glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS)
  1470. {
  1471. colorm.x -= 0.01f; if (colorm.x < 0.0f) colorm.x = 0.0f;
  1472. }
  1473.  
  1474. if (glfwGetKey(window, GLFW_KEY_KP_5) == GLFW_PRESS)
  1475. {
  1476. colorm.y -= 0.01f; if (colorm.y < 0.0f) colorm.y = 0.0f;
  1477. }
  1478.  
  1479. if (glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS)
  1480. {
  1481. colorm.z -= 0.01f; if (colorm.z < 0.0f) colorm.z = 0.0f;
  1482. }
  1483.  
  1484. if (glfwGetKey(window, GLFW_KEY_KP_7) == GLFW_PRESS)
  1485. {
  1486. colorm.x += 0.01f; if (colorm.x > 1.0f) colorm.x = 1.0f;
  1487. }
  1488.  
  1489. if (glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS)
  1490. {
  1491. colorm.y += 0.01f; if (colorm.y > 1.0f) colorm.y = 1.0f;
  1492. }
  1493.  
  1494. if (glfwGetKey(window, GLFW_KEY_KP_9) == GLFW_PRESS)
  1495. {
  1496. colorm.z += 0.01f; if (colorm.z > 1.0f) colorm.z = 1.0f;
  1497. }
  1498.  
  1499. if (glfwGetKey(window, GLFW_KEY_KP_1) == GLFW_PRESS)
  1500. printf("Color: %f %f %f\n", colorm.x, colorm.y, colorm.z);
  1501.  
  1502. if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
  1503. cameraType = 1 - cameraType;
  1504.  
  1505. if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
  1506. shadingmethod = 0;
  1507. if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
  1508. shadingmethod = 1;
  1509. if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
  1510. shadingmethod = 2;
  1511. if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS)
  1512. renderingmethod = OPENGL_MODE;
  1513.  
  1514. if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS)
  1515. renderingmethod = CLOSE2GL_MODE;
  1516.  
  1517. if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS)
  1518. cull = 0;
  1519.  
  1520. if (glfwGetKey(window, GLFW_KEY_7) == GLFW_PRESS)
  1521. cull = 1;
  1522.  
  1523. if (glfwGetKey(window, GLFW_KEY_8) == GLFW_PRESS)
  1524. wireframe = 0;
  1525.  
  1526. if (glfwGetKey(window, GLFW_KEY_9) == GLFW_PRESS)
  1527. wireframe = 1;
  1528.  
  1529.  
  1530. if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
  1531. {
  1532. cameraPos = cameraDefaultPos;
  1533. cameraFront = cameraDefaultFront;
  1534. calcz = calcdz;
  1535. yaw = -90.0f;
  1536. pitch = 0.0f;
  1537. rotationx = 0.0f;
  1538. lastX = 320.0f;
  1539. lastY = 240.0f;
  1540. }
  1541.  
  1542. }
  1543.  
  1544.  
  1545. void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
  1546.  
  1547. if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != GLFW_PRESS)
  1548. return;
  1549.  
  1550. float xoffset = xpos - lastX;
  1551. float yoffset = lastY - ypos; // reversed since y-coordinates range from bottom to top
  1552. lastX = xpos;
  1553. lastY = ypos;
  1554.  
  1555. float sensitivity = 0.5f;
  1556. xoffset *= sensitivity;
  1557. yoffset *= sensitivity;
  1558.  
  1559. yaw += xoffset;
  1560. pitch += yoffset;
  1561.  
  1562. if (pitch > 89.0f)
  1563. pitch = 89.0f;
  1564. else if (pitch < -89.0f)
  1565. pitch = -89.0f;
  1566.  
  1567. glm::vec3 front;
  1568. front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
  1569. front.y = sin(glm::radians(pitch));
  1570. front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
  1571. cameraFront = glm::normalize(front);
  1572. }
  1573.  
  1574. void FramebufferSizeCallback(GLFWwindow* window, int width, int height) {
  1575. glViewport(0, 0, width, height);
  1576. aspectRatio = (float)width / height;
  1577. w_width = width;
  1578. w_height = height;
  1579. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement