Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. #include <memory>
  2. #include <cstring>
  3.  
  4. #include <boost/filesystem.hpp>
  5. #include <boost/multi_array.hpp>
  6. #define GLEW_STATIC
  7. #include <GL/glew.h>
  8. #include <glm/glm.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10. #include <glm/gtc/type_ptr.hpp>
  11. #include <SDL2/SDL.h>
  12.  
  13. #include <pck/global.hpp>
  14. #include <pck/program.hpp>
  15. #include <pck/utils/stb_image.h>
  16. #include <pck/window.hpp>
  17.  
  18. namespace fs = boost::filesystem;
  19.  
  20. const int WIDTH = 800, HEIGHT = 600;
  21.  
  22. GLuint map[3][8] = {
  23. { 1, 2, 3, 4, 5, 6, 7, 8},
  24. { 9, 10, 11, 12, 13, 14, 15, 16},
  25. {17, 18, 19, 20, 21, 22, 23, 24}
  26. };
  27.  
  28.  
  29. int main()
  30. {
  31. // Wrapper around SDL2 window, also initializing OpenGL
  32. pck::Global::window.reset(new pck::Window("Test_gl", WIDTH, HEIGHT));
  33.  
  34. // Vertices buffer
  35. GLfloat vertices[] = {
  36. 0.0f, 1.0f,
  37. 1.0f, 0.0f,
  38. 0.0f, 0.0f,
  39.  
  40. 0.0f, 1.0f,
  41. 1.0f, 0.0f,
  42. 1.0f, 1.0f
  43. };
  44.  
  45. // Positions buffer
  46. glm::vec2 positions[24];
  47.  
  48. int index = 0;
  49. for(size_t j = 0; j < 3; ++j)
  50. {
  51. for(size_t i = 0; i < 8; ++i)
  52. {
  53. positions[index++] = glm::vec2(i, j);
  54. }
  55. }
  56.  
  57. // Tileset caracteristics
  58. glm::vec2 tile_size(16.0f, 16.0f);
  59. glm::vec2 tex_size(128.0f, 208.0f);
  60. glm::vec2 tex_tile_size(8.0f, 13.0f);
  61.  
  62. // Textures coords buffer;
  63.  
  64. // 24 tiles * 12 uvs
  65. GLfloat tex_coords[24 * 12];
  66.  
  67. index = 0;
  68. for(size_t j = 0; j < 3; ++j)
  69. {
  70. for(size_t i = 0; i < 8; ++i)
  71. {
  72. glm::vec2 uv(i / tile_size.x, j / tile_size.y);
  73.  
  74. glm::vec2 ul(uv.x * i , uv.y * j);
  75. glm::vec2 ur(uv.x * i + uv.x, uv.y * j);
  76. glm::vec2 dl(uv.x * i , uv.y * j + uv.y);
  77. glm::vec2 dr(uv.x * i + uv.x, uv.y * j + uv.y);
  78.  
  79. GLfloat uvs[] = {
  80. ul.x, ul.y,
  81. dl.x, dl.y,
  82. ur.x, ur.y,
  83.  
  84. dr.x, dr.y,
  85. ur.x, ur.y,
  86. dl.x, dl.y
  87. };
  88.  
  89. // Fill the tex coords buffer
  90. std::memcpy(&tex_coords[index * 12], &uvs[0], sizeof(GLfloat) * 12);
  91. index++;
  92. }
  93. }
  94.  
  95. // Texture declaration
  96. GLuint tex_ID;
  97. glGenTextures(1, &tex_ID);
  98.  
  99. int w(0), h(0), c(0);
  100. unsigned char* data = stbi_load("tileset.png", &w, &h, &c, STBI_rgb_alpha);
  101.  
  102. if(data == nullptr)
  103. {
  104. std::cout << "Failed to load texturen";
  105. stbi_image_free(data);
  106. return 1;
  107. }
  108.  
  109. glBindTexture(GL_TEXTURE_2D, tex_ID);
  110. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  111. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  112. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  114.  
  115. if(c == 3)
  116. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  117. else if(c == 4)
  118. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  119.  
  120. glGenerateMipmap(GL_TEXTURE_2D);
  121. glBindTexture(GL_TEXTURE_2D, 0);
  122.  
  123. stbi_image_free(data);
  124.  
  125. GLuint VAO;
  126. glGenVertexArrays(1, &VAO);
  127. glBindVertexArray(VAO);
  128.  
  129. GLuint vertex_VBO;
  130. glGenBuffers(1, &vertex_VBO);
  131.  
  132. glBindBuffer(GL_ARRAY_BUFFER, vertex_VBO);
  133. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  134. glEnableVertexAttribArray(0);
  135. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
  136. glBindBuffer(GL_ARRAY_BUFFER, 0);
  137. glVertexAttribDivisor(0, 0);
  138.  
  139. // Also set instance data
  140. GLuint positions_VBO;
  141. glGenBuffers(1, &positions_VBO);
  142.  
  143. glEnableVertexAttribArray(1);
  144. glBindBuffer(GL_ARRAY_BUFFER, positions_VBO);
  145. glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * 24, &positions[0], GL_STATIC_DRAW);
  146. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
  147. glBindBuffer(GL_ARRAY_BUFFER, 0);
  148. glVertexAttribDivisor(1, 1);
  149.  
  150. GLuint textures_VBO;
  151. glGenBuffers(1, &textures_VBO);
  152.  
  153. glEnableVertexAttribArray(2);
  154. glBindBuffer(GL_ARRAY_BUFFER, textures_VBO);
  155. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24 * 12, &tex_coords[0], GL_STATIC_DRAW);
  156. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
  157. glBindBuffer(GL_ARRAY_BUFFER, 0);
  158. glVertexAttribDivisor(2, 1);
  159.  
  160. glBindVertexArray(0);
  161.  
  162. pck::Global::zoom = 2;
  163. glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(pck::Global::width / pck::Global::zoom),
  164. static_cast<GLfloat>(pck::Global::height / pck::Global::zoom), 0.0f, -1.0f, 1.0f);
  165.  
  166. glm::mat4 model;
  167. model = glm::rotate(model, 0.0f, glm::vec3(0.0f, 0.0f, 1.0f));
  168. model = glm::scale(model, glm::vec3(16.0f, 16.0f, 1.0f));
  169.  
  170. // Shader for instanciated tiles
  171. pck::VertShader vs(std::string(R"(
  172. #version 330 core
  173. layout (location = 0) in vec2 position;
  174. layout (location = 1) in vec2 offset;
  175. layout (location = 2) in vec2 texture;
  176.  
  177. uniform mat4 model;
  178. uniform mat4 projection;
  179.  
  180. out vec2 vs_tex_coords;
  181.  
  182. void main()
  183. {
  184. gl_Position = projection * model * vec4(position + offset, 0.0f, 1.0f);
  185. vs_tex_coords = texture;
  186. }
  187. )"));
  188.  
  189. pck::FragShader fs(std::string(R"(
  190. #version 330 core
  191.  
  192. in vec2 vs_tex_coords;
  193.  
  194. out vec4 fs_color;
  195.  
  196. uniform sampler2D image;
  197.  
  198. void main()
  199. {
  200. fs_color = texture(image, vs_tex_coords);
  201. }
  202. )"));
  203.  
  204. std::shared_ptr<pck::Program> program(new pck::Program(vs, fs));
  205.  
  206. //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  207.  
  208. while(pck::Global::window->is_closed())
  209. {
  210. while(pck::Global::window->poll_event() != 0)
  211. {
  212. if(pck::Global::event->type == SDL_QUIT)
  213. {
  214. pck::Global::window->close();
  215. }
  216. else if(pck::Global::event->type == SDL_KEYDOWN)
  217. {
  218. if(pck::Global::event->key.keysym.sym == SDLK_ESCAPE)
  219. pck::Global::window->close();
  220. }
  221. }
  222.  
  223. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  224. glClear(GL_COLOR_BUFFER_BIT);
  225.  
  226. glBindTexture(GL_TEXTURE_2D, tex_ID);
  227.  
  228. program->use();
  229.  
  230. glUniformMatrix4fv(glGetUniformLocation(program->ID(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
  231. glUniformMatrix4fv(glGetUniformLocation(program->ID(), "model"), 1, GL_FALSE, glm::value_ptr(model));
  232.  
  233. glBindVertexArray(VAO);
  234. glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 24);
  235. glBindVertexArray(0);
  236.  
  237. pck::Global::window->update();
  238. }
  239.  
  240. return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement