Advertisement
Guest User

Untitled

a guest
Jun 6th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #define GLEW_STATIC
  5. #include <GL/glew.h>
  6. #include <GLFW/glfw3.h>
  7. #include <glm/glm.hpp>
  8. #include <glm/gtc/matrix_transform.hpp>
  9. #include <glm/gtc/type_ptr.hpp>
  10.  
  11. int screenWidth = 800;
  12. int screenHeight = 600;
  13. double deltaTime = 0.0;
  14. bool keys[GLFW_KEY_LAST];
  15. bool mouseGrabbed = false;
  16.  
  17. std::vector<glm::vec3> lines;
  18.  
  19. struct CAMERA
  20. {
  21. glm::vec3 position = glm::vec3(0.0f, 0.0f, 10.0);
  22. glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0);
  23. glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
  24. glm::vec3 direction = glm::normalize(position - front);
  25. glm::vec3 upVector = glm::vec3(0.0f, 1.0f, 0.0f);
  26. glm::vec3 right = glm::normalize(glm::cross(upVector, direction));
  27. glm::vec3 cameraUp = glm::cross(direction, right);
  28. }camera;
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. void MakeCoordinateNet(int netSize, float yPos, float cubeSize)
  36. {
  37. for (float x = -netSize; x <= netSize; x += cubeSize)
  38. {
  39. lines.push_back(glm::vec3(x, yPos, netSize));
  40. lines.push_back(glm::vec3(x, yPos, -netSize));
  41. }
  42. for (float x = -netSize; x <= netSize; x += cubeSize)
  43. {
  44. lines.push_back(glm::vec3(netSize, yPos, x));
  45. lines.push_back(glm::vec3(-netSize, yPos, x));
  46. }
  47. }
  48.  
  49. void CameraChanged()
  50. {
  51. std::cout << "Position: [" << camera.position.x << "," << camera.position.y << "," << camera.position.z << "]" << std::endl;
  52. std::cout << "rotation: [" << camera.rotation.x << "," << camera.rotation.y << "," << camera.rotation.z << "]" << std::endl;
  53. }
  54.  
  55. void glfw_mouse_pos_callback(GLFWwindow* window, double xPos, double yPos)
  56. {
  57. if (!mouseGrabbed)
  58. {
  59. return;
  60. }
  61.  
  62. glm::vec3 oldRotation = camera.rotation;
  63.  
  64. static double lastX = 0.0;
  65. static double lastY = 0.0;
  66. static float yaw = camera.rotation.x;
  67. static float pitch = camera.rotation.y;
  68. static bool firstMouse = true;
  69.  
  70. if (firstMouse)
  71. {
  72. lastX = xPos;
  73. lastY = yPos;
  74. firstMouse = false;
  75. }
  76.  
  77. float xOffset = xPos - lastX;
  78. float yOffset = lastY - yPos;
  79. lastX = xPos;
  80. lastY = yPos;
  81.  
  82. float sensitivity = 0.05f;
  83. xOffset *= sensitivity;
  84. yOffset *= sensitivity;
  85.  
  86. yaw += xOffset;
  87. pitch += yOffset;
  88.  
  89. if (pitch > 89.0f) pitch = 89.0f;
  90. if (pitch < -89.0f) pitch = -89.0f;
  91.  
  92. camera.rotation.x = yaw;
  93. camera.rotation.y = pitch;
  94.  
  95. glm::vec3 front;
  96. front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
  97. front.y = sin(glm::radians(pitch));
  98. front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
  99. camera.front = glm::normalize(front);
  100.  
  101. if (camera.rotation.x != oldRotation.x ||
  102. camera.rotation.y != oldRotation.y ||
  103. camera.rotation.z != oldRotation.z)
  104. {
  105. CameraChanged();
  106. }
  107. }
  108.  
  109. void key_callback(GLFWwindow* wnd, int key, int scancode, int action, int mode)
  110. {
  111. if (action == GLFW_PRESS)
  112. {
  113. switch (key)
  114. {
  115. case GLFW_KEY_ESCAPE:
  116. case GLFW_KEY_ENTER:
  117. if (mouseGrabbed)
  118. {
  119. glfwSetInputMode(wnd, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  120. glfwSetCursorPos(wnd, screenWidth / 2, screenHeight / 2);
  121. mouseGrabbed = false;
  122. std::cout << "Mouse is released" << std::endl;
  123. }
  124. else
  125. {
  126. glfwSetWindowShouldClose(wnd, true);
  127. }
  128. return;
  129. }
  130. keys[key] = true;
  131. }
  132. else if (action == GLFW_RELEASE)
  133. {
  134. keys[key] = false;
  135. }
  136. }
  137.  
  138. void callback_func_windowResize(GLFWwindow* wnd, int w, int h)
  139. {
  140. screenWidth = w;
  141. screenHeight = h;
  142. glViewport(0,0, w,h);
  143. std::cout << "Changed viewport to: " << w << "x" << h << std::endl;
  144. }
  145.  
  146. void callback_func_MouseClicked(GLFWwindow* wnd, int button, int action, int mods)
  147. {
  148. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
  149. {
  150. if (!mouseGrabbed)
  151. {
  152. glfwSetCursorPos(wnd, screenWidth / 2, screenHeight / 2);
  153. glfwSetInputMode(wnd, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  154. mouseGrabbed = true;
  155. std::cout << "Mouse is grabbed" << std::endl;
  156. }
  157. }
  158. }
  159.  
  160. void HandleMovement(float speed)
  161. {
  162. glm::vec3 oldPos = camera.position;
  163. float cameraSpeed = deltaTime * speed;
  164. if(keys[GLFW_KEY_W])
  165. camera.position += camera.front * cameraSpeed;
  166. if(keys[GLFW_KEY_S])
  167. camera.position -= camera.front * cameraSpeed;
  168. if(keys[GLFW_KEY_A])
  169. camera.position -= glm::normalize(glm::cross(camera.front, camera.cameraUp)) * cameraSpeed;
  170. if(keys[GLFW_KEY_D])
  171. camera.position += glm::normalize(glm::cross(camera.front, camera.cameraUp)) * cameraSpeed;
  172. if(keys[GLFW_KEY_C])
  173. camera.position.y -= cameraSpeed;
  174. if(keys[GLFW_KEY_SPACE])
  175. camera.position.y += cameraSpeed;
  176.  
  177. if (camera.position.x != oldPos.x || camera.position.y != oldPos.y || camera.position.z != oldPos.z)
  178. {
  179. CameraChanged();
  180. }
  181. }
  182.  
  183. int main()
  184. {
  185. if (!glfwInit())
  186. {
  187. std::cout << "glfwInit() failed!" << std::endl;
  188. return 1;
  189. }
  190.  
  191. GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight,
  192. (char*)"camera test", nullptr, nullptr);
  193. if (!window)
  194. {
  195. glfwTerminate();
  196. std::cout << "glfwCreateWindow() failed!" << std::endl;
  197. return 1;
  198. }
  199. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  200. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  201. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  202.  
  203. glfwSetWindowPos(window, 700, 300);
  204. glfwSetKeyCallback(window, key_callback);
  205. glfwSetMouseButtonCallback(window, callback_func_MouseClicked);
  206. glfwSetCursorPosCallback(window, glfw_mouse_pos_callback);
  207. glfwSetWindowSizeCallback(window, callback_func_windowResize);
  208.  
  209. glfwMakeContextCurrent(window);
  210.  
  211. glewExperimental = true;
  212. if (glewInit() != GLEW_OK)
  213. {
  214. glfwTerminate();
  215. std::cout << "glewInit() failed!" << std::endl;
  216. return 1;
  217. }
  218.  
  219. GLchar* vertexShaderSource = (GLchar*)
  220. "#version 330 core\n"
  221. "\n"
  222. "layout (location = 0) in vec3 position;\n"
  223. "\n"
  224. "uniform mat4 view;\n"
  225. "uniform mat4 projection;\n"
  226. "\n"
  227. "void main()\n"
  228. "{\n"
  229. " gl_Position = projection * view * vec4(position, 1.0f);\n"
  230. "}\n";
  231.  
  232. GLuint vertexShader;
  233. vertexShader = glCreateShader(GL_VERTEX_SHADER);
  234.  
  235. glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  236. glCompileShader(vertexShader);
  237.  
  238. GLint success;
  239. GLchar infoLog[512];
  240. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  241. if (!success)
  242. {
  243. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  244. std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
  245. glfwTerminate();
  246. return 1;
  247. }
  248.  
  249. GLchar* fragmentShaderSource = (GLchar*)
  250. "#version 330 core\n"
  251. "\n"
  252. "out vec4 color;\n"
  253. "\n"
  254. "void main()\n"
  255. "{\n"
  256. " color = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
  257. "}\n";
  258.  
  259. GLuint fragmentShader;
  260. fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  261. glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  262. glCompileShader(fragmentShader);
  263.  
  264. glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  265. if (!success)
  266. {
  267. glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  268. std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
  269. glDeleteShader(vertexShader);
  270. glfwTerminate();
  271. return 1;
  272. }
  273.  
  274. GLuint shaderProgram;
  275. shaderProgram = glCreateProgram();
  276.  
  277. glAttachShader(shaderProgram, vertexShader);
  278. glAttachShader(shaderProgram, fragmentShader);
  279. glLinkProgram(shaderProgram);
  280.  
  281. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  282. if (!success)
  283. {
  284. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  285. std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
  286. glDeleteShader(vertexShader);
  287. glDeleteShader(fragmentShader);
  288. glfwTerminate();
  289. return 1;
  290. }
  291.  
  292. glUseProgram(shaderProgram);
  293.  
  294. glDeleteShader(vertexShader);
  295. glDeleteShader(fragmentShader);
  296.  
  297. MakeCoordinateNet(10, -1.0f, 1.0f);
  298.  
  299. GLuint vbo;
  300. glGenBuffers(1, &vbo);
  301.  
  302. GLuint vao;
  303. glGenVertexArrays(1, &vao);
  304.  
  305. glBindVertexArray(vao);
  306. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  307. glBufferData(GL_ARRAY_BUFFER, lines.size() * sizeof(glm::vec3), &lines[0], GL_STATIC_DRAW);
  308. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (GLvoid*)0);
  309. glEnableVertexAttribArray(0);
  310. glBindVertexArray(0);
  311.  
  312.  
  313. while (!glfwWindowShouldClose(window))
  314. {
  315. glfwPollEvents();
  316.  
  317. deltaTime = glfwGetTime();
  318. glfwSetTime(0.0);
  319.  
  320. HandleMovement(10.0f);
  321.  
  322. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  323.  
  324. glBindVertexArray(vao);
  325.  
  326. glm::mat4 projectionMatrix = glm::perspective(45.0f, (float)screenWidth / (float)screenHeight,
  327. 0.1f, 1000.0f);
  328. GLint n = glGetUniformLocation(shaderProgram, "projection");
  329. glUniformMatrix4fv(n, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  330.  
  331. glm::mat4 viewMatrix = glm::lookAt(camera.position, camera.position + camera.front, camera.cameraUp);
  332. n = glGetUniformLocation(shaderProgram, "view");
  333. glUniformMatrix4fv(n, 1, GL_FALSE, glm::value_ptr(viewMatrix));
  334.  
  335. glDrawArrays(GL_LINES, 0, lines.size() * 2);
  336.  
  337. glBindVertexArray(0);
  338.  
  339. glfwSwapBuffers(window);
  340. }
  341.  
  342. glDeleteProgram(shaderProgram);
  343.  
  344. glfwTerminate();
  345.  
  346. return 0;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement