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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 frameSize = uniformLoc("frameSize");
  68. GLint frameInfo = uniformLoc("frameInfo");
  69. GLint vertex = attribLoc("vertex");
  70. GLint tex = uniformLoc("tex");
  71. GLuint vbo;
  72. static constexpr GLfloat vertexes[] = {
  73. 0.0f, 0.0f, // pos 0: top left
  74. 0.0f, 1.0f, // pos 1: bottom left
  75. 1.0f, 1.0f, // pos 2: bottom right
  76. 1.0f, 1.0f, // pos 2: bottom right
  77. 1.0f, 0.0f, // pos 3: top right
  78. 0.0f, 0.0f, // pos 0: top left
  79. };
  80. void enable() {
  81. glUseProgram(id());
  82. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  83. glVertexAttribPointer(vertex, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
  84. glEnableVertexAttribArray(vertex);
  85. glCheck();
  86. glUniform1i(tex, 0);
  87. }
  88. void disable() {
  89. glDisableVertexAttribArray(vertex);
  90. glCheck();
  91. }
  92. void init() {
  93. glGenBuffers(1, &vbo);
  94. glCheck();
  95. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  96. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
  97. glCheck();
  98. }
  99. void deinit() {
  100. glDeleteBuffers(1, &vbo);
  101. glCheck();
  102. }
  103. };
  104. struct RendererState {
  105. GlVxShader spriteVx{Shaders::spriteVx};
  106. GlFrShader spriteFr{Shaders::spriteFr};
  107. GlVxShader chunkVx{Shaders::chunkVx};
  108. GlFrShader chunkFr{Shaders::chunkFr};
  109. SpriteProg spriteProg{spriteVx, spriteFr};
  110. ChunkProg chunkProg{chunkVx, chunkFr};
  111. GLuint atlasTex;
  112. SwanCommon::Vec2 atlasTexSize;
  113. };
  114. Renderer::Renderer(): state_(std::make_unique<RendererState>()) {
  115. glGenTextures(1, &state_->atlasTex);
  116. glCheck();
  117. }
  118. Renderer::~Renderer() = default;
  119. void Renderer::draw(const RenderCamera &cam) {
  120. Mat3gf camMat;
  121. // Make the matrix translate to { -camX, -camY }, fix up the aspect ratio,
  122. // flip the Y axis so that positive Y direction is down, and scale according to zoom.
  123. float ratio = (float)cam.size.y / (float)cam.size.x;
  124. camMat.translate(-cam.pos);
  125. camMat.scale({cam.zoom * ratio, -cam.zoom});
  126. auto &chunkProg = state_->chunkProg;
  127. auto &spriteProg = state_->spriteProg;
  128. {
  129. chunkProg.enable();
  130. glUniformMatrix3fv(chunkProg.camera, 1, GL_TRUE, camMat.data());
  131. glCheck();
  132. glUniform2f(chunkProg.tileAtlasSize, state_->atlasTexSize.x, state_->atlasTexSize.y);
  133. glCheck();
  134. glActiveTexture(GL_TEXTURE0);
  135. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  136. glCheck();
  137. glActiveTexture(GL_TEXTURE1);
  138. for (auto [pos, chunk]: drawChunks_) {
  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. drawChunks_.clear();
  145. chunkProg.disable();
  146. }
  147. {
  148. spriteProg.enable();
  149. glUniformMatrix3fv(spriteProg.camera, 1, GL_TRUE, camMat.data());
  150. glCheck();
  151. glActiveTexture(GL_TEXTURE0);
  152. for (auto [mat, frame, sprite]: drawSprites_) {
  153. glUniformMatrix3fv(spriteProg.transform, 1, GL_TRUE, mat.data());
  154. glUniform2f(spriteProg.frameSize, sprite.scale.x, sprite.scale.y);
  155. glUniform2f(spriteProg.frameInfo, sprite.frameCount, frame);
  156. glBindTexture(GL_TEXTURE_2D, sprite.tex);
  157. glDrawArrays(GL_TRIANGLES, 0, 6);
  158. glCheck();
  159. }
  160. drawSprites_.clear();
  161. spriteProg.disable();
  162. }
  163. }
  164. void Renderer::uploadTileAtlas(const void *data, int width, int height) {
  165. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  166. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  167. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  168. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  169. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  170. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  171. glCheck();
  172. state_->atlasTexSize = {
  173. (float)(int)(width / SwanCommon::TILE_SIZE),
  174. (float)(int)(height / SwanCommon::TILE_SIZE) };
  175. }
  176. void Renderer::modifyTile(TileID id, const void *data) {
  177. int w = (int)state_->atlasTexSize.x;
  178. int x = id % w;
  179. int y = id / w;
  180. glActiveTexture(GL_TEXTURE0);
  181. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  182. glTexSubImage2D(
  183. GL_TEXTURE_2D, 0, x * SwanCommon::TILE_SIZE, y * SwanCommon::TILE_SIZE,
  184. SwanCommon::TILE_SIZE, SwanCommon::TILE_SIZE,
  185. GL_RGBA, GL_UNSIGNED_BYTE, data);
  186. glCheck();
  187. }
  188. RenderChunk Renderer::createChunk(
  189. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]) {
  190. // TODO: Maybe don't do this here? Maybe instead store the buffer and
  191. // upload the texture in the draw method?
  192. // The current approach needs createChunk to be called on the graphics thread.
  193. RenderChunk chunk;
  194. glGenTextures(1, &chunk.tex);
  195. glCheck();
  196. glActiveTexture(GL_TEXTURE0);
  197. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  198. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  199. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  202. glCheck();
  203. static_assert(
  204. std::endian::native == std::endian::big ||
  205. std::endian::native == std::endian::little,
  206. "Expected either big or little endian");
  207. if constexpr (std::endian::native == std::endian::little) {
  208. glTexImage2D(
  209. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  210. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  211. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tiles);
  212. } else if constexpr (std::endian::native == std::endian::big) {
  213. uint8_t buf[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT * 2];
  214. for (size_t y = 0; y < SwanCommon::CHUNK_HEIGHT; ++y) {
  215. for (size_t x = 0; x < SwanCommon::CHUNK_WIDTH; ++x) {
  216. size_t dst = y * SwanCommon::CHUNK_WIDTH * 2 + x * 2;
  217. size_t src = y * SwanCommon::CHUNK_WIDTH + x;
  218. buf[dst + 0] = tiles[src] & 0xff;
  219. buf[dst + 1] = (tiles[src] & 0xff00) >> 8;
  220. }
  221. }
  222. glTexImage2D(
  223. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  224. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  225. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  226. }
  227. glCheck();
  228. return chunk;
  229. }
  230. void Renderer::modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id) {
  231. glActiveTexture(GL_TEXTURE0);
  232. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  233. glCheck();
  234. static_assert(
  235. std::endian::native == std::endian::big ||
  236. std::endian::native == std::endian::little,
  237. "Expected either big or little endian");
  238. if constexpr (std::endian::native == std::endian::little) {
  239. glTexSubImage2D(
  240. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  241. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, &id);
  242. } else if constexpr (std::endian::native == std::endian::big) {
  243. uint8_t buf[] = { (uint8_t)(id & 0xff), (uint8_t)((id & 0xff00) >> 8) };
  244. glTexSubImage2D(
  245. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  246. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  247. }
  248. glCheck();
  249. }
  250. void Renderer::destroyChunk(RenderChunk chunk) {
  251. glDeleteTextures(1, &chunk.tex);
  252. glCheck();
  253. }
  254. RenderSprite Renderer::createSprite(void *data, int width, int height, int fh) {
  255. RenderSprite sprite;
  256. sprite.scale = {
  257. (float)width / SwanCommon::TILE_SIZE,
  258. (float)fh / SwanCommon::TILE_SIZE };
  259. sprite.frameCount = height / fh;
  260. glGenTextures(1, &sprite.tex);
  261. glCheck();
  262. glActiveTexture(GL_TEXTURE0);
  263. glBindTexture(GL_TEXTURE_2D, sprite.tex);
  264. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  265. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  266. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  267. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  268. glCheck();
  269. glTexImage2D(
  270. GL_TEXTURE_2D, 0, GL_RGBA, width, height,
  271. 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  272. glCheck();
  273. return sprite;
  274. }
  275. RenderSprite Renderer::createSprite(void *data, int width, int height) {
  276. return createSprite(data, width, height, height);
  277. }
  278. void Renderer::destroySprite(RenderSprite sprite) {
  279. glDeleteTextures(1, &sprite.tex);
  280. }
  281. }