A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Renderer.cc 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "Renderer.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <SDL_opengles2.h>
  5. #include <swan-common/constants.h>
  6. #include <string.h>
  7. // std::endian was originally in type_traits, was moved to bit
  8. #include <type_traits>
  9. #include <bit>
  10. #include "shaders.h"
  11. #include "GlWrappers.h"
  12. #include "TileAtlas.h"
  13. #include "util.h"
  14. namespace Cygnet {
  15. struct SpriteProg: public GlProgram {
  16. template<typename... T>
  17. SpriteProg(const T &... shaders): GlProgram(shaders...) { init(); }
  18. ~SpriteProg() { deinit(); }
  19. GLint camera = uniformLoc("camera");
  20. GLint transform = uniformLoc("transform");
  21. GLint vertex = attribLoc("vertex");
  22. GLint texCoord = attribLoc("texCoord");
  23. GLint tex = uniformLoc("tex");
  24. GLuint vbo;
  25. static constexpr GLfloat vertexes[] = {
  26. -0.5f, 0.5f, // pos 0: top left
  27. 0.0f, 0.0f, // tex 0: top left
  28. -0.5f, -0.5f, // pos 1: bottom left
  29. 0.0f, 1.0f, // tex 1: bottom left
  30. 0.5f, -0.5f, // pos 2: bottom right
  31. 1.0f, 1.0f, // tex 2: bottom right
  32. 0.5f, -0.5f, // pos 2: bottom right
  33. 1.0f, 1.0f, // tex 2: bottom right
  34. 0.5f, 0.5f, // pos 3: top right
  35. 1.0f, 0.0f, // tex 3: top right
  36. -0.5f, 0.5f, // pos 0: top left
  37. 0.0f, 0.0f, // tex 0: top left
  38. };
  39. void enable() {
  40. glUseProgram(id());
  41. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  42. glVertexAttribPointer(vertex, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)0);
  43. glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)));
  44. glEnableVertexAttribArray(vertex);
  45. glEnableVertexAttribArray(texCoord);
  46. glCheck();
  47. }
  48. void disable() {
  49. glDisableVertexAttribArray(vertex);
  50. glDisableVertexAttribArray(texCoord);
  51. glCheck();
  52. }
  53. void init() {
  54. glGenBuffers(1, &vbo);
  55. glCheck();
  56. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  57. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
  58. glCheck();
  59. }
  60. void deinit() {
  61. glDeleteBuffers(1, &vbo);
  62. glCheck();
  63. }
  64. };
  65. struct ChunkProg: public GlProgram {
  66. template<typename... T>
  67. ChunkProg(const T &... shaders): GlProgram(shaders...) { init(); }
  68. ~ChunkProg() { deinit(); }
  69. GLint camera = uniformLoc("camera");
  70. GLint pos = uniformLoc("pos");
  71. GLint vertex = attribLoc("vertex");
  72. GLint tileAtlas = uniformLoc("tileAtlas");
  73. GLint tileAtlasSize = uniformLoc("tileAtlasSize");
  74. GLint tiles = uniformLoc("tiles");
  75. GLuint vbo;
  76. static constexpr float ch = (float)SwanCommon::CHUNK_HEIGHT;
  77. static constexpr float cw = (float)SwanCommon::CHUNK_WIDTH;
  78. static constexpr GLfloat vertexes[] = {
  79. 0.0f, 0.0f, // pos 0: top left
  80. 0.0f, -ch , // pos 1: bottom left
  81. cw, -ch, // pos 2: bottom right
  82. cw, -ch, // pos 2: bottom right
  83. cw, 0.0f, // pos 3: top right
  84. 0.0f, 0.0f, // pos 0: top left
  85. };
  86. void enable() {
  87. glUseProgram(id());
  88. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  89. glVertexAttribPointer(vertex, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
  90. glEnableVertexAttribArray(vertex);
  91. glCheck();
  92. glUniform1i(tileAtlas, 0);
  93. glUniform1i(tiles, 1);
  94. }
  95. void disable() {
  96. glDisableVertexAttribArray(vertex);
  97. glCheck();
  98. }
  99. void init() {
  100. glGenBuffers(1, &vbo);
  101. glCheck();
  102. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  103. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
  104. glCheck();
  105. }
  106. void deinit() {
  107. glDeleteBuffers(1, &vbo);
  108. glCheck();
  109. }
  110. };
  111. struct RendererState {
  112. GlVxShader spriteVx{Shaders::spriteVx};
  113. GlFrShader spriteFr{Shaders::spriteFr};
  114. GlVxShader chunkVx{Shaders::chunkVx};
  115. GlFrShader chunkFr{Shaders::chunkFr};
  116. SpriteProg spriteProg{spriteVx, spriteFr};
  117. ChunkProg chunkProg{chunkVx, chunkFr};
  118. TileAtlas atlas;
  119. GlTexture atlasTex;
  120. };
  121. Renderer::Renderer(): state_(std::make_unique<RendererState>()) {}
  122. Renderer::~Renderer() = default;
  123. void Renderer::draw(const RenderCamera &cam) {
  124. Mat3gf camMat;
  125. camMat.translate(cam.pos.scale(-1, 1) * cam.zoom); // TODO: Change something to make this -cam.pos
  126. camMat.scale({ cam.zoom, cam.zoom });
  127. auto &chunkProg = state_->chunkProg;
  128. chunkProg.enable();
  129. glUniform2f(chunkProg.tileAtlasSize,
  130. (float)(int)(state_->atlasTex.width() / SwanCommon::TILE_SIZE),
  131. (float)(int)(state_->atlasTex.height() / SwanCommon::TILE_SIZE));
  132. glUniformMatrix3fv(chunkProg.camera, 1, GL_TRUE, camMat.data());
  133. glCheck();
  134. glActiveTexture(GL_TEXTURE0);
  135. glBindTexture(GL_TEXTURE_2D, state_->atlasTex.id()); // Necessary?
  136. glCheck();
  137. glActiveTexture(GL_TEXTURE1);
  138. for (auto [pos, chunk]: draw_chunks_) {
  139. glUniform2f(chunkProg.pos, pos.x, pos.y);
  140. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  141. glDrawArrays(GL_TRIANGLES, 0, 6);
  142. glCheck();
  143. }
  144. draw_chunks_.clear();
  145. chunkProg.disable();
  146. /*
  147. state_->spriteProg.enable();
  148. state_->camera.translate(-0.00001, 0);
  149. glUniformMatrix3fv(state_->spriteProg.transform, 1, GL_TRUE, Mat3gf::IDENTITY.data());
  150. glUniformMatrix3fv(state_->spriteProg.camera, 1, GL_TRUE, state_->camera.data());
  151. glCheck();
  152. glActiveTexture(GL_TEXTURE0);
  153. glBindTexture(GL_TEXTURE_2D,state_->atlasTex.id()); // Necessary?
  154. glUniform1i(state_->spriteProg.tex, 0);
  155. glCheck();
  156. glDrawArrays(GL_TRIANGLES, 0, 6);
  157. glCheck();
  158. state_->spriteProg.disable();
  159. */
  160. }
  161. void Renderer::registerTileTexture(TileID tileId, const void *data, size_t len) {
  162. state_->atlas.addTile(tileId, data, len);
  163. }
  164. void Renderer::uploadTileTexture() {
  165. size_t w, h;
  166. const unsigned char *data = state_->atlas.getImage(&w, &h);
  167. state_->atlasTex.upload(w, h, (void *)data, GL_RGBA, GL_UNSIGNED_BYTE);
  168. std::cerr << "Uploaded image of size " << w << 'x' << h << '\n';
  169. }
  170. RenderChunk Renderer::createChunk(
  171. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]) {
  172. // TODO: Maybe don't do this here? Maybe instead store the buffer and
  173. // upload the texture in the draw method?
  174. // The current approach needs createChunk to be called on the graphics thread.
  175. RenderChunk chunk;
  176. glGenTextures(1, &chunk.tex);
  177. glCheck();
  178. glActiveTexture(GL_TEXTURE0);
  179. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  180. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  181. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  182. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  183. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  184. glCheck();
  185. static_assert(
  186. std::endian::native == std::endian::big ||
  187. std::endian::native == std::endian::little,
  188. "Expected either big or little endian");
  189. if constexpr (std::endian::native == std::endian::little) {
  190. glTexImage2D(
  191. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  192. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  193. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tiles);
  194. } else if constexpr (std::endian::native == std::endian::big) {
  195. uint8_t buf[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT * 2];
  196. for (size_t y = 0; y < SwanCommon::CHUNK_HEIGHT; ++y) {
  197. for (size_t x = 0; x < SwanCommon::CHUNK_WIDTH; ++x) {
  198. size_t dst = y * SwanCommon::CHUNK_WIDTH * 2 + x * 2;
  199. size_t src = y * SwanCommon::CHUNK_WIDTH + x;
  200. buf[dst + 0] = tiles[src] & 0xff;
  201. buf[dst + 1] = (tiles[src] & 0xff00) >> 8;
  202. }
  203. }
  204. glTexImage2D(
  205. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  206. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  207. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  208. }
  209. return chunk;
  210. }
  211. void Renderer::modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id) {
  212. glActiveTexture(GL_TEXTURE0);
  213. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  214. glCheck();
  215. static_assert(
  216. std::endian::native == std::endian::big ||
  217. std::endian::native == std::endian::little,
  218. "Expected either big or little endian");
  219. if constexpr (std::endian::native == std::endian::little) {
  220. glTexSubImage2D(
  221. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  222. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, &id);
  223. } else if constexpr (std::endian::native == std::endian::big) {
  224. uint8_t buf[] = { (uint8_t)(id & 0xff), (uint8_t)((id & 0xff00) >> 8) };
  225. glTexSubImage2D(
  226. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  227. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  228. }
  229. }
  230. void Renderer::destroyChunk(RenderChunk chunk) {
  231. glDeleteTextures(1, &chunk.tex);
  232. }
  233. }