Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. #define STB_IMAGE_IMPLEMENTATION
  2. // This #define is needed so that only parts of stb i use is compiled
  3.  
  4. #include <glad/glad.h>
  5. #include <GLFW/glfw3.h>
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/matrix_transform.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9. #include <vector>
  10. #include <iostream>
  11. #include "gl/shader_s.h"
  12. #include "stb_image.h"
  13.  
  14. void framebuffer_size_callback(GLFWwindow *window, int width, int height);
  15.  
  16. void processInput(GLFWwindow *window);
  17.  
  18. // settings
  19. const unsigned int SCR_WIDTH = 1280;
  20. const unsigned int SCR_HEIGHT = 720;
  21.  
  22. int main() {
  23.  
  24. glm::mat4 model;
  25. model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
  26.  
  27. glm::mat4 view;
  28. // note that we're translating the scene in the reverse direction of where we want to move
  29. view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
  30.  
  31. glm::mat4 projection;
  32. // projection = glm::perspective(glm::radians(45.0f), (float) (1280.0 / 720.0), 0.1f, 100.0f);
  33. projection = glm::ortho(0.0f, 1280.0f, 0.0f, 720.0f, -100.0f, 100.0f);
  34.  
  35. // glfw: initialize and configure
  36. // ------------------------------
  37. glfwInit();
  38. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  39. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  40. // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Don't allow depricated GL to be used
  41.  
  42. #ifdef __APPLE__
  43. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
  44. #endif
  45.  
  46. // glfw window creation
  47. // --------------------
  48. GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Tactics", NULL, NULL);
  49. if (window == NULL) {
  50. std::cout << "Failed to create GLFW window" << std::endl;
  51. glfwTerminate();
  52. return -1;
  53. }
  54. glfwMakeContextCurrent(window);
  55. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  56.  
  57. // glad: load all OpenGL function pointers
  58. // ---------------------------------------
  59. if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
  60. std::cout << "Failed to initialize GLAD" << std::endl;
  61. return -1;
  62. }
  63.  
  64. // build and compile our shader zprogram
  65. // ------------------------------------
  66. Shader ourShader("data/shaders/texture3d.vs", "data/shaders/texture3d.fs");
  67.  
  68. // For Ortho
  69. float vertices[] = {
  70. -80.5f, -80.5f, -80.5f, 0.0f, 0.0f,
  71. 80.5f, -80.5f, -80.5f, 1.0f, 0.0f,
  72. 80.5f, 80.5f, -80.5f, 1.0f, 1.0f,
  73. 80.5f, 80.5f, -80.5f, 1.0f, 1.0f,
  74. -80.5f, 80.5f, -80.5f, 0.0f, 1.0f,
  75. -80.5f, -80.5f, -80.5f, 0.0f, 0.0f,
  76.  
  77. -80.5f, -80.5f, 80.5f, 0.0f, 0.0f,
  78. 80.5f, -80.5f, 80.5f, 1.0f, 0.0f,
  79. 80.5f, 80.5f, 80.5f, 1.0f, 1.0f,
  80. 80.5f, 80.5f, 80.5f, 1.0f, 1.0f,
  81. -80.5f, 80.5f, 80.5f, 0.0f, 1.0f,
  82. -80.5f, -80.5f, 80.5f, 0.0f, 0.0f,
  83.  
  84. -80.5f, 80.5f, 80.5f, 1.0f, 0.0f,
  85. -80.5f, 80.5f, -80.5f, 1.0f, 1.0f,
  86. -80.5f, -80.5f, -80.5f, 0.0f, 1.0f,
  87. -80.5f, -80.5f, -80.5f, 0.0f, 1.0f,
  88. -80.5f, -80.5f, 80.5f, 0.0f, 0.0f,
  89. -80.5f, 80.5f, 80.5f, 1.0f, 0.0f,
  90.  
  91. 80.5f, 80.5f, 80.5f, 1.0f, 0.0f,
  92. 80.5f, 80.5f, -80.5f, 1.0f, 1.0f,
  93. 80.5f, -80.5f, -80.5f, 0.0f, 1.0f,
  94. 80.5f, -80.5f, -80.5f, 0.0f, 1.0f,
  95. 80.5f, -80.5f, 80.5f, 0.0f, 0.0f,
  96. 80.5f, 80.5f, 80.5f, 1.0f, 0.0f,
  97.  
  98. -80.5f, -80.5f, -80.5f, 0.0f, 1.0f,
  99. 80.5f, -80.5f, -80.5f, 1.0f, 1.0f,
  100. 80.5f, -80.5f, 80.5f, 1.0f, 0.0f,
  101. 80.5f, -80.5f, 80.5f, 1.0f, 0.0f,
  102. -80.5f, -80.5f, 80.5f, 0.0f, 0.0f,
  103. -80.5f, -80.5f, -80.5f, 0.0f, 1.0f,
  104.  
  105. -80.5f, 80.5f, -80.5f, 0.0f, 1.0f,
  106. 80.5f, 80.5f, -80.5f, 1.0f, 1.0f,
  107. 80.5f, 80.5f, 80.5f, 1.0f, 0.0f,
  108. 80.5f, 80.5f, 80.5f, 1.0f, 0.0f,
  109. -80.5f, 80.5f, 80.5f, 0.0f, 0.0f,
  110. -80.5f, 80.5f, -80.5f, 0.0f, 1.0f
  111. };
  112.  
  113. unsigned int indices[] = {
  114. 0, 1, 3, // first triangle
  115. 1, 2, 3 // second triangle
  116. };
  117. unsigned int VBO, VAO, EBO;
  118. glGenVertexArrays(1, &VAO);
  119. glGenBuffers(1, &VBO);
  120. glGenBuffers(1, &EBO);
  121.  
  122. glBindVertexArray(VAO);
  123.  
  124. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  125. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  126.  
  127. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  128. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  129.  
  130. // // position attribute
  131. // glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) 0);
  132. // glEnableVertexAttribArray(0);
  133. // // color attribute
  134. // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (3 * sizeof(float)));
  135. // glEnableVertexAttribArray(1);
  136. // // texture coord attribute
  137. // glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *) (6 * sizeof(float)));
  138. // glEnableVertexAttribArray(2);
  139.  
  140. // position attribute
  141. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 0);
  142. glEnableVertexAttribArray(0);
  143. // texture coord attribute
  144. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) (3 * sizeof(float)));
  145. glEnableVertexAttribArray(1);
  146.  
  147.  
  148. // load and create a texture
  149. // -------------------------
  150. unsigned int texture;
  151. glGenTextures(1, &texture);
  152. glBindTexture(GL_TEXTURE_2D,
  153. texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
  154. // set the texture wrapping parameters
  155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  156. GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
  157. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  158. // set texture filtering parameters
  159. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  160. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  161. // load image, create texture and generate mipmaps
  162. int width, height, nrChannels;
  163. stbi_set_flip_vertically_on_load(true); // GL expects 0,0 at bottom but texture puts it at top
  164. unsigned char *data = stbi_load("data/braid.jpg", &width, &height, &nrChannels,
  165. STBI_rgb_alpha); // this allows 32bit colors (RGBA) STBI_rgb only goes 24bit colors (RGB)
  166. if (data) {
  167. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  168. glGenerateMipmap(GL_TEXTURE_2D);
  169. } else {
  170. std::cout << "Failed to load texture" << std::endl;
  171. }
  172. stbi_image_free(data);
  173.  
  174.  
  175. // Cube positions
  176. glm::vec3 cubePositions[] = {
  177. glm::vec3(800.0f, 0.0f, 0.0f),
  178. glm::vec3(2.0f, 5.0f, -15.0f),
  179. glm::vec3(-1.5f, -2.2f, -2.5f),
  180. glm::vec3(-3.8f, -2.0f, -12.3f),
  181. glm::vec3(2.4f, -0.4f, -3.5f),
  182. glm::vec3(-1.7f, 3.0f, -7.5f),
  183. glm::vec3(1.3f, -2.0f, -2.5f),
  184. glm::vec3(1.5f, 2.0f, -2.5f),
  185. glm::vec3(1.5f, 0.2f, -1.5f),
  186. glm::vec3(-1.3f, 1.0f, -1.5f)
  187. };
  188.  
  189.  
  190. glEnable(GL_DEPTH_TEST);
  191.  
  192. // render loop
  193. // -----------
  194. while (!glfwWindowShouldClose(window)) {
  195. // input
  196. // -----
  197. processInput(window);
  198.  
  199. // render
  200. // ------
  201. glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  202. // glClear(GL_COLOR_BUFFER_BIT);
  203. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  204.  
  205.  
  206. // bind Texture
  207. glBindTexture(GL_TEXTURE_2D, texture);
  208.  
  209.  
  210. // Rotate model view over time
  211. // model = glm::rotate(model, (float)sin(glfwGetTime()) * glm::radians(2.0f), glm::vec3(0.5f, 1.0f, 0.0f));
  212.  
  213. // render container
  214. // int modelLoc = glGetUniformLocation(ourShader.ID, "model");
  215. // glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  216.  
  217. int viewLoc = glGetUniformLocation(ourShader.ID, "view");
  218. glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  219.  
  220. int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
  221. glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
  222.  
  223.  
  224. ourShader.use();
  225. glBindVertexArray(VAO);
  226. // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  227. // glDrawArrays(GL_TRIANGLES, 0, 36); // (6 faces * 2 triangles * 3 vertices each)
  228.  
  229. for (unsigned int i = 0; i < 10; i++) {
  230. glm::mat4 model;
  231. model = glm::translate(model, cubePositions[i]);
  232. float angle = 20.0f * i + 60 * sin(glfwGetTime());
  233. model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
  234. int modelLoc = glGetUniformLocation(ourShader.ID, "model");
  235. glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  236. glDrawArrays(GL_TRIANGLES, 0, 36);
  237. }
  238.  
  239. // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  240. // -------------------------------------------------------------------------------
  241. glfwSwapBuffers(window);
  242. glfwPollEvents();
  243. }
  244.  
  245. // optional: de-allocate all resources once they've outlived their purpose:
  246. // ------------------------------------------------------------------------
  247. glDeleteVertexArrays(1, &VAO);
  248. glDeleteBuffers(1, &VBO);
  249. glDeleteBuffers(1, &EBO);
  250.  
  251. // glfw: terminate, clearing all previously allocated GLFW resources.
  252. // ------------------------------------------------------------------
  253. glfwTerminate();
  254. return 0;
  255. }
  256.  
  257. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  258. // ---------------------------------------------------------------------------------------------------------
  259.  
  260. void processInput(GLFWwindow *window) {
  261. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  262. glfwSetWindowShouldClose(window, true);
  263. }
  264.  
  265. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
  266. // ---------------------------------------------------------------------------------------------
  267.  
  268. void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
  269. // make sure the viewport matches the new window dimensions; note that width and
  270. // height will be significantly larger than specified on retina displays.
  271. glViewport(0, 0, width, height);
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement