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 8.4KB

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