Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. // GLEW
  5. #define GLEW_STATIC
  6. #include <GL/glew.h>
  7.  
  8. // GLFW
  9. #include <GLFW/glfw3.h>
  10.  
  11. // GLM Mathematics
  12. #include <glm/glm.hpp>
  13. #include <glm/gtc/matrix_transform.hpp>
  14. #include <glm/gtc/type_ptr.hpp>
  15.  
  16. // Other includes
  17. #include "Shader.h"
  18.  
  19.  
  20. // Function prototypes
  21. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  22. void do_movement();
  23.  
  24. // Window dimensions
  25. const GLuint WIDTH = 800, HEIGHT = 600;
  26.  
  27. // Camera
  28. glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
  29. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
  30. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
  31. bool keys[1024];
  32.  
  33. // Deltatime
  34. GLfloat deltaTime = 0.0f; // Time between current frame and last frame
  35. GLfloat lastFrame = 0.0f; // Time of last frame
  36.  
  37. // The MAIN function, from here we start the application and run the game loop
  38. int main()
  39. {
  40. // Init GLFW
  41. glfwInit();
  42. // Set all the required options for GLFW
  43. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  44. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  45. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  46. glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  47.  
  48. // Create a GLFWwindow object that we can use for GLFW's functions
  49. GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
  50. glfwMakeContextCurrent(window);
  51.  
  52. // Set the required callback functions
  53. glfwSetKeyCallback(window, key_callback);
  54.  
  55. // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
  56. glewExperimental = GL_TRUE;
  57. // Initialize GLEW to setup the OpenGL Function pointers
  58. glewInit();
  59.  
  60. // Define the viewport dimensions
  61. glViewport(0, 0, WIDTH, HEIGHT);
  62.  
  63. //Enable depth test
  64. glEnable(GL_DEPTH_TEST);
  65.  
  66.  
  67. // Build and compile our shader program
  68. Shader ourShader("C:/Users/Noel/Documents/Visual Studio 2013/Projects/RoomSim/Vertexshader.vs", "C:/Users/Noel/Documents/Visual Studio 2013/Projects/RoomSim/FragShader.frag");
  69.  
  70.  
  71. // Set up vertex data (and buffer(s)) and attribute pointers
  72. GLfloat vertices[] = {
  73. -0.5f, -0.5f, -0.5f,
  74. 0.5f, -0.5f, -0.5f,
  75. 0.5f, 0.5f, -0.5f,
  76. 0.5f, 0.5f, -0.5f,
  77. -0.5f, 0.5f, -0.5f,
  78. -0.5f, -0.5f, -0.5f,
  79.  
  80. -0.5f, -0.5f, 0.5f,
  81. 0.5f, -0.5f, 0.5f,
  82. 0.5f, 0.5f, 0.5f,
  83. 0.5f, 0.5f, 0.5f,
  84. -0.5f, 0.5f, 0.5f,
  85. -0.5f, -0.5f, 0.5f,
  86.  
  87. -0.5f, 0.5f, 0.5f,
  88. -0.5f, 0.5f, -0.5f,
  89. -0.5f, -0.5f, -0.5f,
  90. -0.5f, -0.5f, -0.5f,
  91. -0.5f, -0.5f, 0.5f,
  92. -0.5f, 0.5f, 0.5f,
  93.  
  94. 0.5f, 0.5f, 0.5f,
  95. 0.5f, 0.5f, -0.5f,
  96. 0.5f, -0.5f, -0.5f,
  97. 0.5f, -0.5f, -0.5f,
  98. 0.5f, -0.5f, 0.5f,
  99. 0.5f, 0.5f, 0.5f,
  100.  
  101. -0.5f, -0.5f, -0.5f,
  102. 0.5f, -0.5f, -0.5f,
  103. 0.5f, -0.5f, 0.5f,
  104. 0.5f, -0.5f, 0.5f,
  105. -0.5f, -0.5f, 0.5f,
  106. -0.5f, -0.5f, -0.5f,
  107.  
  108. -0.5f, 0.5f, -0.5f,
  109. 0.5f, 0.5f, -0.5f,
  110. 0.5f, 0.5f, 0.5f,
  111. 0.5f, 0.5f, 0.5f,
  112. -0.5f, 0.5f, 0.5f,
  113. -0.5f, 0.5f, -0.5f,
  114. };
  115.  
  116. GLuint VBO, VAO;
  117. glGenVertexArrays(1, &VAO);
  118. glGenBuffers(1, &VBO);
  119.  
  120. // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
  121. glBindVertexArray(VAO);
  122.  
  123. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  124. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  125.  
  126. //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  127. //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  128.  
  129. // Position attribute
  130. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(GLfloat), (GLvoid*)0);
  131. glEnableVertexAttribArray(0);
  132. // Color attribute
  133. //glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  134. //glEnableVertexAttribArray(1);
  135.  
  136. glBindVertexArray(0); // Unbind VAO
  137.  
  138.  
  139. // Game loop
  140. while (!glfwWindowShouldClose(window))
  141. {
  142. // Calculate deltatime of current frame
  143. GLfloat currentFrame = glfwGetTime();
  144. deltaTime = currentFrame - lastFrame;
  145. lastFrame = currentFrame;
  146.  
  147. // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
  148. glfwPollEvents();
  149. do_movement();
  150.  
  151. // Render
  152. // Clear the colorbuffer
  153. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  154.  
  155. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  156.  
  157. //Transformations
  158. // Create transformations
  159. //glm::mat4 transform;
  160. //transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));
  161. //transform = glm::rotate(transform, (GLfloat)glfwGetTime() * 50.0f, glm::vec3(0.0f, 0.0f, 1.0f));
  162.  
  163. // Get matrix's uniform location and set matrix
  164.  
  165. //GLint transformLoc = glGetUniformLocation(ourShader.Program, "transform");
  166.  
  167. //glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
  168.  
  169. // Draw the triangle
  170. ourShader.Use();
  171.  
  172. // Create transformations
  173. glm::mat4 model;
  174. glm::mat4 view;
  175. glm::mat4 projection;
  176.  
  177. //Camera particulars for view
  178. glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
  179. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
  180. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
  181.  
  182. model = glm::rotate(model, 50.0f, glm::vec3(1.0f, 0.0f, 0.0f));
  183.  
  184.  
  185. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  186. projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
  187.  
  188. // Get their uniform location
  189. GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
  190. GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
  191. GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
  192. glBindVertexArray(VAO);
  193. // Pass them to the shaders
  194. glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  195. glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  196. glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
  197.  
  198.  
  199. glDrawArrays(GL_TRIANGLES, 0, 36);
  200. glBindVertexArray(0);
  201.  
  202. // Swap the screen buffers
  203. glfwSwapBuffers(window);
  204. }
  205. // Properly de-allocate all resources once they've outlived their purpose
  206. glDeleteVertexArrays(1, &VAO);
  207. glDeleteBuffers(1, &VBO);
  208.  
  209. // Terminate GLFW, clearing any resources allocated by GLFW.
  210. glfwTerminate();
  211. return 0;
  212. }
  213.  
  214. // Is called whenever a key is pressed/released via GLFW
  215. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  216. {
  217. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  218. glfwSetWindowShouldClose(window, GL_TRUE);
  219. if (key >= 0 && key < 1024)
  220. {
  221. if (action == GLFW_PRESS)
  222. keys[key] = true;
  223. else if (action == GLFW_RELEASE)
  224. keys[key] = false;
  225. }
  226. }
  227.  
  228. void do_movement()
  229. {
  230. GLfloat cameraSpeed = 5.0f * deltaTime;
  231. // Camera controls
  232.  
  233. if (keys[GLFW_KEY_W])
  234. cameraPos += cameraSpeed * cameraFront;
  235. if (keys[GLFW_KEY_S])
  236. cameraPos -= cameraSpeed * cameraFront;
  237. if (keys[GLFW_KEY_A])
  238. cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  239. if (keys[GLFW_KEY_D])
  240. cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  241. }
  242.  
  243. #version 330 core
  244. layout(location = 0) in vec3 position;
  245. layout(location = 1) in vec3 color;
  246.  
  247. out vec3 ourColor;
  248. uniform mat4 model;
  249. uniform mat4 view;
  250. uniform mat4 projection;
  251.  
  252. void main()
  253. {
  254. gl_Position = projection * view * model * vec4(position, 1.0f);
  255. ourColor = color;
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement