Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.06 KB | None | 0 0
  1. // Lab6 - Texturi.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include"pch.h"
  5.  
  6. //#include "stdafx.h"
  7. #include <stdlib.h> // necesare pentru citirea shader-elor
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. #include <GL/glew.h>
  12.  
  13. #include <GLM.hpp>
  14. #include <gtc/matrix_transform.hpp>
  15. #include <gtc/type_ptr.hpp>
  16.  
  17. #include <glfw3.h>
  18.  
  19. #include <iostream>
  20.  
  21. #define STB_IMAGE_IMPLEMENTATION
  22. #include <stb_image.h>
  23. #pragma comment (lib, "glfw3dll.lib")
  24. #pragma comment (lib, "glew32.lib")
  25. #pragma comment (lib, "OpenGL32.lib")
  26.  
  27. // settings
  28. const unsigned int SCR_WIDTH = 800;
  29. const unsigned int SCR_HEIGHT = 600;
  30.  
  31. float mixValue = 0.5;
  32.  
  33. enum ECameraMovementType
  34. {
  35. UNKNOWN,
  36. FORWARD,
  37. BACKWARD,
  38. LEFT,
  39. RIGHT,
  40. UP,
  41. DOWN
  42. };
  43.  
  44. class Camera
  45. {
  46. private:
  47. // Default camera values
  48. const float zNEAR = 0.1f;
  49. const float zFAR = 500.f;
  50. const float YAW = -90.0f;
  51. const float PITCH = 0.0f;
  52. const float FOV = 45.0f;
  53. glm::vec3 startPosition;
  54.  
  55. public:
  56. Camera(const int width, const int height, const glm::vec3 &position)
  57. {
  58. startPosition = position;
  59. Set(width, height, position);
  60. }
  61.  
  62. void Set(const int width, const int height, const glm::vec3 &position)
  63. {
  64. this->isPerspective = true;
  65. this->yaw = YAW;
  66. this->pitch = PITCH;
  67.  
  68. this->FoVy = FOV;
  69. this->width = width;
  70. this->height = height;
  71. this->zNear = zNEAR;
  72. this->zFar = zFAR;
  73.  
  74. this->worldUp = glm::vec3(0, 1, 0);
  75. this->position = position;
  76.  
  77. lastX = width / 2.0f;
  78. lastY = height / 2.0f;
  79. bFirstMouseMove = true;
  80.  
  81. UpdateCameraVectors();
  82. }
  83.  
  84. void Reset(const int width, const int height)
  85. {
  86. Set(width, height, startPosition);
  87. }
  88.  
  89. void Reshape(int windowWidth, int windowHeight)
  90. {
  91. width = windowWidth;
  92. height = windowHeight;
  93.  
  94. // define the viewport transformation
  95. glViewport(0, 0, windowWidth, windowHeight);
  96. }
  97.  
  98. const glm::mat4 GetViewMatrix() const
  99. {
  100. // Returns the View Matrix
  101. return glm::lookAt(position, position + forward, up);
  102. }
  103.  
  104. const glm::mat4 GetProjectionMatrix() const
  105. {
  106. glm::mat4 Proj = glm::mat4(1);
  107. if (isPerspective) {
  108. float aspectRatio = ((float)(width)) / height;
  109. Proj = glm::perspective(glm::radians(FoVy), aspectRatio, zNear, zFar);
  110. }
  111. else {
  112. float scaleFactor = 2000.f;
  113. Proj = glm::ortho<float>(
  114. -width / scaleFactor, width / scaleFactor,
  115. -height / scaleFactor, height / scaleFactor, -zFar, zFar);
  116. }
  117. return Proj;
  118. }
  119.  
  120. void ProcessKeyboard(ECameraMovementType direction, float deltaTime)
  121. {
  122. float velocity = (float)(cameraSpeedFactor * deltaTime);
  123. switch (direction) {
  124. case ECameraMovementType::FORWARD:
  125. position += forward * velocity;
  126. break;
  127. case ECameraMovementType::BACKWARD:
  128. position -= forward * velocity;
  129. break;
  130. case ECameraMovementType::LEFT:
  131. position -= right * velocity;
  132. break;
  133. case ECameraMovementType::RIGHT:
  134. position += right * velocity;
  135. break;
  136. case ECameraMovementType::UP:
  137. position += up * velocity;
  138. break;
  139. case ECameraMovementType::DOWN:
  140. position -= up * velocity;
  141. break;
  142. }
  143. }
  144.  
  145. void MouseControl(float xPos, float yPos)
  146. {
  147. if (bFirstMouseMove) {
  148. lastX = xPos;
  149. lastY = yPos;
  150. bFirstMouseMove = false;
  151. }
  152.  
  153. float xChange = xPos - lastX;
  154. float yChange = lastY - yPos;
  155. lastX = xPos;
  156. lastY = yPos;
  157.  
  158. if (fabs(xChange) <= 1e-6 && fabs(yChange) <= 1e-6) {
  159. return;
  160. }
  161. xChange *= mouseSensitivity;
  162. yChange *= mouseSensitivity;
  163.  
  164. ProcessMouseMovement(xChange, yChange);
  165. }
  166.  
  167. void ProcessMouseScroll(float yOffset)
  168. {
  169. if (FoVy >= 1.0f && FoVy <= 90.0f) {
  170. FoVy -= yOffset;
  171. }
  172. if (FoVy <= 1.0f)
  173. FoVy = 1.0f;
  174. if (FoVy >= 90.0f)
  175. FoVy = 90.0f;
  176. }
  177.  
  178. private:
  179. void ProcessMouseMovement(float xOffset, float yOffset, bool constrainPitch = true)
  180. {
  181. yaw += xOffset;
  182. pitch += yOffset;
  183.  
  184. //std::cout << "yaw = " << yaw << std::endl;
  185. //std::cout << "pitch = " << pitch << std::endl;
  186.  
  187. // Avem grijã sã nu ne dãm peste cap
  188. if (constrainPitch) {
  189. if (pitch > 89.0f)
  190. pitch = 89.0f;
  191. if (pitch < -89.0f)
  192. pitch = -89.0f;
  193. }
  194.  
  195. // Se modificã vectorii camerei pe baza unghiurilor Euler
  196. UpdateCameraVectors();
  197. }
  198.  
  199. void UpdateCameraVectors()
  200. {
  201. // Calculate the new forward vector
  202. this->forward.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
  203. this->forward.y = sin(glm::radians(pitch));
  204. this->forward.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
  205. this->forward = glm::normalize(this->forward);
  206. // Also re-calculate the Right and Up vector
  207. right = glm::normalize(glm::cross(forward, worldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
  208. up = glm::normalize(glm::cross(right, forward));
  209. }
  210.  
  211. protected:
  212. const float cameraSpeedFactor = 2.5f;
  213. const float mouseSensitivity = 0.1f;
  214.  
  215. // Perspective properties
  216. float zNear;
  217. float zFar;
  218. float FoVy;
  219. int width;
  220. int height;
  221. bool isPerspective;
  222.  
  223. glm::vec3 position;
  224. glm::vec3 forward;
  225. glm::vec3 right;
  226. glm::vec3 up;
  227. glm::vec3 worldUp;
  228.  
  229. // Euler Angles
  230. float yaw;
  231. float pitch;
  232.  
  233. bool bFirstMouseMove = true;
  234. float lastX = 0.f, lastY = 0.f;
  235. };
  236.  
  237. GLuint VAO, VBO, EBO;
  238. unsigned int VertexShaderId, FragmentShaderId, ProgramId;
  239. GLuint ProjMatrixLocation, ViewMatrixLocation, WorldMatrixLocation;
  240. unsigned int texture1Location, texture2Location;
  241.  
  242.  
  243. Camera *pCamera = nullptr;
  244.  
  245. // Shader-ul de varfuri / Vertex shader (este privit ca un sir de caractere)
  246. const GLchar* VertexShader =
  247. {
  248. "#version 330\n"\
  249. "layout (location = 0) in vec3 aPos;\n"\
  250. "layout (location = 1) in vec3 aColor;\n"\
  251. "layout (location = 2) in vec2 aTexCoord;\n"\
  252. "out vec3 ourColor;\n"\
  253. "out vec2 TexCoord;\n"\
  254. "uniform mat4 ProjMatrix;\n"\
  255. "uniform mat4 ViewMatrix;\n"\
  256. "uniform mat4 WorldMatrix;\n"\
  257. "void main()\n"\
  258. "{\n"\
  259. "gl_Position = ProjMatrix * ViewMatrix * WorldMatrix * vec4(aPos, 1.0);\n"\
  260. "ourColor = aColor;\n"\
  261. "TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n"\
  262. "}\n"
  263. };
  264. // Shader-ul de fragment / Fragment shader (este privit ca un sir de caractere)
  265. const GLchar* FragmentShader =
  266. {
  267. "#version 330\n"\
  268. "out vec4 FragColor;\n"\
  269. "in vec3 ourColor;\n"\
  270. "in vec2 TexCoord;\n"\
  271. "uniform float mixValue;\n"\
  272. "uniform sampler2D texture1;\n"\
  273. "uniform sampler2D texture2;\n"\
  274. "void main()\n"\
  275. "{\n"\
  276. " FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), mixValue) * vec4(ourColor,0.1);\n"\
  277. "}\n"
  278. };
  279.  
  280.  
  281. void CreateVBO()
  282. {
  283. /*float vertices[] = {
  284. 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  285. 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  286. -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  287. -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
  288. };
  289. unsigned int indices[] = {
  290. 0, 1, 3,
  291. 1, 2, 3
  292. };*/
  293.  
  294. // indexurile cubului
  295. unsigned int indices[] = {
  296. 0,1,2,
  297. 0,2,3,
  298. 1,5,6,
  299. 1,6,2,
  300. 5,4,7,
  301. 5,7,6,
  302. 4,0,3,
  303. 4,3,7,
  304. 0,5,1,
  305. 0,4,5,
  306. 3,2,6,
  307. 3,6,7
  308. };
  309.  
  310. // varfurile cubului
  311. float vertices[] = {
  312. 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  313. 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  314. 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  315. 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  316. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  317. 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  318. 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  319. 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
  320. };
  321.  
  322. /*unsigned int indices[] = {
  323. 0,1,3,
  324. 1,2,3,
  325. 2,0,3,
  326. 0,1,2
  327. };
  328. float vertices[] = {
  329.  
  330. -0.8f, -0.8f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  331. 0.0f, -0.8f, 1.6f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  332. 0.8f, -0.8f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  333. 0.0f, 0.8f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
  334. };*/
  335.  
  336.  
  337. glGenVertexArrays(1, &VAO);
  338. glGenBuffers(1, &VBO);
  339. glGenBuffers(1, &EBO);
  340.  
  341. glBindVertexArray(VAO);
  342.  
  343. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  344. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  345.  
  346. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  347. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  348.  
  349. // position attribute
  350. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
  351. glEnableVertexAttribArray(0);
  352. // color attribute
  353. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
  354. glEnableVertexAttribArray(1);
  355. // texture coord attribute
  356. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
  357. glEnableVertexAttribArray(2);
  358. }
  359. void DestroyVBO()
  360. {
  361. glDeleteVertexArrays(1, &VAO);
  362. glDeleteBuffers(1, &VBO);
  363. glDeleteBuffers(1, &EBO);
  364. }
  365. void CreateShaders()
  366. {
  367. VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
  368. glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
  369. glCompileShader(VertexShaderId);
  370.  
  371. FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
  372. glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
  373. glCompileShader(FragmentShaderId);
  374.  
  375. ProgramId = glCreateProgram();
  376. glAttachShader(ProgramId, VertexShaderId);
  377. glAttachShader(ProgramId, FragmentShaderId);
  378. glLinkProgram(ProgramId);
  379.  
  380. GLint Success = 0;
  381. GLchar ErrorLog[1024] = { 0 };
  382.  
  383. glGetProgramiv(ProgramId, GL_LINK_STATUS, &Success);
  384. if (Success == 0) {
  385. glGetProgramInfoLog(ProgramId, sizeof(ErrorLog), NULL, ErrorLog);
  386. fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
  387. exit(1);
  388. }
  389.  
  390. glValidateProgram(ProgramId);
  391. glGetProgramiv(ProgramId, GL_VALIDATE_STATUS, &Success);
  392. if (!Success) {
  393. glGetProgramInfoLog(ProgramId, sizeof(ErrorLog), NULL, ErrorLog);
  394. fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
  395. exit(1);
  396. }
  397.  
  398. glUseProgram(ProgramId);
  399.  
  400. ProjMatrixLocation = glGetUniformLocation(ProgramId, "ProjMatrix");
  401. ViewMatrixLocation = glGetUniformLocation(ProgramId, "ViewMatrix");
  402. WorldMatrixLocation = glGetUniformLocation(ProgramId, "WorldMatrix");
  403.  
  404. glUniform1i(glGetUniformLocation(ProgramId, "texture1"), 0);
  405. glUniform1i(glGetUniformLocation(ProgramId, "texture2"), 1);
  406.  
  407. //glUniform1f(glGetUniformLocation(ProgramId, "mixValue"), 0.5f);
  408. glUniform1f(glGetUniformLocation(ProgramId, "mixValue"), mixValue);
  409. }
  410. void DestroyShaders()
  411. {
  412. glUseProgram(0);
  413.  
  414. glDetachShader(ProgramId, VertexShaderId);
  415. glDetachShader(ProgramId, FragmentShaderId);
  416.  
  417. glDeleteShader(FragmentShaderId);
  418. glDeleteShader(VertexShaderId);
  419.  
  420. glDeleteProgram(ProgramId);
  421. }
  422.  
  423. void CreateTextures(const std::string& strExePath)
  424. {
  425. // load and create a texture
  426. // -------------------------
  427. // texture 1
  428. // ---------
  429. glGenTextures(1, &texture1Location);
  430. glBindTexture(GL_TEXTURE_2D, texture1Location);
  431. // set the texture wrapping parameters
  432. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  433. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  434. // set texture filtering parameters
  435. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  436. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  437. // load image, create texture and generate mipmaps
  438. int width, height, nrChannels;
  439. stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
  440. unsigned char *data = stbi_load((strExePath + "\\stones.jpg").c_str(), &width, &height, &nrChannels, 0);
  441. if (data) {
  442. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  443. glGenerateMipmap(GL_TEXTURE_2D);
  444. }
  445. else {
  446. std::cout << "Failed to load texture" << std::endl;
  447. }
  448. stbi_image_free(data);
  449. // texture 2
  450. // ---------
  451. glGenTextures(1, &texture2Location);
  452. glBindTexture(GL_TEXTURE_2D, texture2Location);
  453. // set the texture wrapping parameters
  454. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  455. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  456. // set texture filtering parameters
  457. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  458. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  459. // load image, create texture and generate mipmaps
  460. data = stbi_load((strExePath + "\\Bricks.jpg").c_str(), &width, &height, &nrChannels, 0);
  461. if (data) {
  462. // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
  463. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  464. glGenerateMipmap(GL_TEXTURE_2D);
  465. }
  466. else {
  467. std::cout << "Failed to load texture" << std::endl;
  468. }
  469. stbi_image_free(data);
  470. }
  471. void Initialize(const std::string& strExePath)
  472. {
  473. glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // culoarea de fond a ecranului
  474. //glEnable(GL_CULL_FACE);
  475. glEnable(GL_DEPTH_TEST);
  476. glEnable(GL_COLOR_MATERIAL);
  477. glDisable(GL_LIGHTING);
  478.  
  479. //glFrontFace(GL_CCW);
  480. //glCullFace(GL_BACK);
  481.  
  482. CreateVBO();
  483. CreateShaders();
  484. CreateTextures(strExePath);
  485.  
  486. // Create camera
  487. pCamera = new Camera(SCR_WIDTH, SCR_HEIGHT, glm::vec3(0.5, 0.5, 10));
  488. }
  489.  
  490. void RenderCube()
  491. {
  492. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  493. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  494.  
  495. int indexArraySize;
  496. glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &indexArraySize);
  497. glDrawElements(GL_TRIANGLES, indexArraySize / sizeof(unsigned int), GL_UNSIGNED_INT, 0);
  498. }
  499.  
  500. void RenderFunction()
  501. {
  502. glm::vec3 cubePositions[] = {
  503. glm::vec3(0.0f, 0.0f, 0.0f),
  504. glm::vec3(-5.0f, 5.0f, 5.0f),
  505. glm::vec3(-5.0f, -5.0f, 5.0f),
  506. glm::vec3(5.0f, -5.0f, 5.0f),
  507. glm::vec3(5.0f, 5.0f, 5.0f),
  508. glm::vec3(-5.0f, 5.0f, -5.0f),
  509. glm::vec3(-5.0f, -5.0f, -5.0f),
  510. glm::vec3(5.0f, -5.0f, -5.0f),
  511. glm::vec3(5.0f, 5.0f, -5.0f),
  512. };
  513.  
  514. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  515.  
  516. glUseProgram(ProgramId);
  517.  
  518. glUniform1f(glGetUniformLocation(ProgramId, "mixValue"), mixValue);
  519.  
  520. // bind textures on corresponding texture units
  521. glActiveTexture(GL_TEXTURE0);
  522. glBindTexture(GL_TEXTURE_2D, texture1Location);
  523. glActiveTexture(GL_TEXTURE1);
  524. glBindTexture(GL_TEXTURE_2D, texture2Location);
  525.  
  526. glm::mat4 projection = pCamera->GetProjectionMatrix();
  527. glUniformMatrix4fv(ProjMatrixLocation, 1, GL_FALSE, glm::value_ptr(projection));
  528.  
  529. glm::mat4 view = pCamera->GetViewMatrix();
  530. glUniformMatrix4fv(ViewMatrixLocation, 1, GL_FALSE, glm::value_ptr(view));
  531.  
  532. /*glm::mat4 view;
  533. float radius = 10.0f;
  534. float camX = sin(glfwGetTime()) * radius;
  535. float camZ = cos(glfwGetTime()) * radius;
  536. view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  537. glUniformMatrix4fv(ViewMatrixLocation, 1, GL_FALSE, glm::value_ptr(view));*/
  538.  
  539. glBindVertexArray(VAO);
  540.  
  541. for (unsigned int i = 0; i < sizeof(cubePositions) / sizeof(cubePositions[0]); i++) {
  542. // calculate the model matrix for each object and pass it to shader before drawing
  543. glm::mat4 worldTransf = glm::translate(glm::mat4(1.0), cubePositions[i]);
  544. glUniformMatrix4fv(WorldMatrixLocation, 1, GL_FALSE, glm::value_ptr(worldTransf));
  545.  
  546. RenderCube();
  547. }
  548. }
  549.  
  550. void Cleanup()
  551. {
  552. DestroyShaders();
  553. DestroyVBO();
  554.  
  555. delete pCamera;
  556. }
  557.  
  558. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  559. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  560. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
  561. void processInput(GLFWwindow *window);
  562.  
  563. // timing
  564. double deltaTime = 0.0f; // time between current frame and last frame
  565. double lastFrame = 0.0f;
  566.  
  567. int main(int argc, char** argv)
  568. {
  569. std::string strFullExeFileName = argv[0];
  570. std::string strExePath;
  571. const size_t last_slash_idx = strFullExeFileName.rfind('\\');
  572. if (std::string::npos != last_slash_idx) {
  573. strExePath = strFullExeFileName.substr(0, last_slash_idx);
  574. }
  575.  
  576. // glfw: initialize and configure
  577. glfwInit();
  578. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  579. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  580. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  581.  
  582. // glfw window creation
  583. GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Lab 6", NULL, NULL);
  584. if (window == NULL) {
  585. std::cout << "Failed to create GLFW window" << std::endl;
  586. glfwTerminate();
  587. return -1;
  588. }
  589.  
  590. glfwMakeContextCurrent(window);
  591. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  592. glfwSetCursorPosCallback(window, mouse_callback);
  593. glfwSetScrollCallback(window, scroll_callback);
  594.  
  595. // tell GLFW to capture our mouse
  596. //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  597.  
  598. glewInit();
  599.  
  600. Initialize(strExePath);
  601.  
  602. // render loop
  603. while (!glfwWindowShouldClose(window)) {
  604. // per-frame time logic
  605. double currentFrame = glfwGetTime();
  606. deltaTime = currentFrame - lastFrame;
  607. lastFrame = currentFrame;
  608.  
  609. // input
  610. processInput(window);
  611.  
  612. // render
  613. RenderFunction();
  614.  
  615. // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  616. glfwSwapBuffers(window);
  617. glfwPollEvents();
  618. }
  619.  
  620. Cleanup();
  621.  
  622. // glfw: terminate, clearing all previously allocated GLFW resources
  623. glfwTerminate();
  624. return 0;
  625. }
  626.  
  627. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  628. void processInput(GLFWwindow *window)
  629. {
  630. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  631. glfwSetWindowShouldClose(window, true);
  632.  
  633. if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
  634. pCamera->ProcessKeyboard(FORWARD, (float)deltaTime);
  635. if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
  636. pCamera->ProcessKeyboard(BACKWARD, (float)deltaTime);
  637. if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
  638. pCamera->ProcessKeyboard(LEFT, (float)deltaTime);
  639. if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
  640. pCamera->ProcessKeyboard(RIGHT, (float)deltaTime);
  641. if (glfwGetKey(window, GLFW_KEY_PAGE_UP) == GLFW_PRESS)
  642. pCamera->ProcessKeyboard(UP, (float)deltaTime);
  643. if (glfwGetKey(window, GLFW_KEY_PAGE_DOWN) == GLFW_PRESS)
  644. pCamera->ProcessKeyboard(DOWN, (float)deltaTime);
  645.  
  646. if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
  647. int width, height;
  648. glfwGetWindowSize(window, &width, &height);
  649. pCamera->Reset(width, height);
  650.  
  651. }
  652. if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS) {
  653. if (mixValue < 1)
  654. {
  655. mixValue += 0.001;
  656. }
  657. }
  658. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
  659. if (mixValue > 0)
  660. {
  661. mixValue -= 0.001;
  662. }
  663. }
  664.  
  665. }
  666.  
  667. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
  668. // ---------------------------------------------------------------------------------------------
  669. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  670. {
  671. // make sure the viewport matches the new window dimensions; note that width and
  672. // height will be significantly larger than specified on retina displays.
  673. pCamera->Reshape(width, height);
  674. }
  675.  
  676. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  677. {
  678. pCamera->MouseControl((float)xpos, (float)ypos);
  679. }
  680.  
  681. void scroll_callback(GLFWwindow* window, double xoffset, double yOffset)
  682. {
  683. pCamera->ProcessMouseScroll((float)yOffset);
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement