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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. camMat.translate(-cam.pos);
  122. if (cam.size.y > cam.size.x) {
  123. float ratio = (float)cam.size.y / (float)cam.size.x;
  124. winScale_ = {1/ratio, 1};
  125. camMat.scale({cam.zoom * ratio, -cam.zoom});
  126. } else {
  127. float ratio = (float)cam.size.x / (float)cam.size.y;
  128. winScale_ = {1, 1/ratio};
  129. camMat.scale({cam.zoom, -cam.zoom * ratio});
  130. }
  131. auto &chunkProg = state_->chunkProg;
  132. auto &spriteProg = state_->spriteProg;
  133. {
  134. chunkProg.enable();
  135. glUniformMatrix3fv(chunkProg.camera, 1, GL_TRUE, camMat.data());
  136. glCheck();
  137. glUniform2f(chunkProg.tileAtlasSize, state_->atlasTexSize.x, state_->atlasTexSize.y);
  138. glCheck();
  139. glActiveTexture(GL_TEXTURE0);
  140. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  141. glCheck();
  142. glActiveTexture(GL_TEXTURE1);
  143. for (auto [pos, chunk]: drawChunks_) {
  144. glUniform2f(chunkProg.pos, pos.x, pos.y);
  145. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  146. glDrawArrays(GL_TRIANGLES, 0, 6);
  147. glCheck();
  148. }
  149. drawChunks_.clear();
  150. chunkProg.disable();
  151. }
  152. {
  153. spriteProg.enable();
  154. glUniformMatrix3fv(spriteProg.camera, 1, GL_TRUE, camMat.data());
  155. glCheck();
  156. glActiveTexture(GL_TEXTURE0);
  157. for (auto [mat, frame, sprite]: drawSprites_) {
  158. glUniformMatrix3fv(spriteProg.transform, 1, GL_TRUE, mat.data());
  159. glUniform2f(spriteProg.frameSize, sprite.scale.x, sprite.scale.y);
  160. glUniform2f(spriteProg.frameInfo, sprite.frameCount, frame);
  161. glBindTexture(GL_TEXTURE_2D, sprite.tex);
  162. glDrawArrays(GL_TRIANGLES, 0, 6);
  163. glCheck();
  164. }
  165. drawSprites_.clear();
  166. spriteProg.disable();
  167. }
  168. }
  169. void Renderer::uploadTileAtlas(const void *data, int width, int height) {
  170. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  171. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  172. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  173. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  174. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  175. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  176. glCheck();
  177. state_->atlasTexSize = {
  178. (float)(int)(width / SwanCommon::TILE_SIZE),
  179. (float)(int)(height / SwanCommon::TILE_SIZE) };
  180. }
  181. void Renderer::modifyTile(TileID id, const void *data) {
  182. int w = (int)state_->atlasTexSize.x;
  183. int x = id % w;
  184. int y = id / w;
  185. glActiveTexture(GL_TEXTURE0);
  186. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  187. glTexSubImage2D(
  188. GL_TEXTURE_2D, 0, x * SwanCommon::TILE_SIZE, y * SwanCommon::TILE_SIZE,
  189. SwanCommon::TILE_SIZE, SwanCommon::TILE_SIZE,
  190. GL_RGBA, GL_UNSIGNED_BYTE, data);
  191. glCheck();
  192. }
  193. RenderChunk Renderer::createChunk(
  194. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]) {
  195. RenderChunk chunk;
  196. glGenTextures(1, &chunk.tex);
  197. glCheck();
  198. glActiveTexture(GL_TEXTURE0);
  199. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  204. glCheck();
  205. static_assert(
  206. std::endian::native == std::endian::big ||
  207. std::endian::native == std::endian::little,
  208. "Expected either big or little endian");
  209. if constexpr (std::endian::native == std::endian::little) {
  210. glTexImage2D(
  211. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  212. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  213. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tiles);
  214. } else if constexpr (std::endian::native == std::endian::big) {
  215. uint8_t buf[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT * 2];
  216. for (size_t y = 0; y < SwanCommon::CHUNK_HEIGHT; ++y) {
  217. for (size_t x = 0; x < SwanCommon::CHUNK_WIDTH; ++x) {
  218. size_t dst = y * SwanCommon::CHUNK_WIDTH * 2 + x * 2;
  219. size_t src = y * SwanCommon::CHUNK_WIDTH + x;
  220. buf[dst + 0] = tiles[src] & 0xff;
  221. buf[dst + 1] = (tiles[src] & 0xff00) >> 8;
  222. }
  223. }
  224. glTexImage2D(
  225. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  226. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  227. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  228. }
  229. glCheck();
  230. return chunk;
  231. }
  232. void Renderer::modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id) {
  233. glActiveTexture(GL_TEXTURE0);
  234. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  235. glCheck();
  236. static_assert(
  237. std::endian::native == std::endian::big ||
  238. std::endian::native == std::endian::little,
  239. "Expected either big or little endian");
  240. if constexpr (std::endian::native == std::endian::little) {
  241. glTexSubImage2D(
  242. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  243. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, &id);
  244. } else if constexpr (std::endian::native == std::endian::big) {
  245. uint8_t buf[] = { (uint8_t)(id & 0xff), (uint8_t)((id & 0xff00) >> 8) };
  246. glTexSubImage2D(
  247. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  248. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  249. }
  250. glCheck();
  251. }
  252. void Renderer::destroyChunk(RenderChunk chunk) {
  253. glDeleteTextures(1, &chunk.tex);
  254. glCheck();
  255. }
  256. RenderSprite Renderer::createSprite(void *data, int width, int height, int fh) {
  257. RenderSprite sprite;
  258. sprite.scale = {
  259. (float)width / SwanCommon::TILE_SIZE,
  260. (float)fh / SwanCommon::TILE_SIZE };
  261. sprite.frameCount = height / fh;
  262. glGenTextures(1, &sprite.tex);
  263. glCheck();
  264. glActiveTexture(GL_TEXTURE0);
  265. glBindTexture(GL_TEXTURE_2D, sprite.tex);
  266. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  267. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  268. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  269. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  270. glCheck();
  271. glTexImage2D(
  272. GL_TEXTURE_2D, 0, GL_RGBA, width, height,
  273. 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  274. glCheck();
  275. return sprite;
  276. }
  277. RenderSprite Renderer::createSprite(void *data, int width, int height) {
  278. return createSprite(data, width, height, height);
  279. }
  280. void Renderer::destroySprite(RenderSprite sprite) {
  281. glDeleteTextures(1, &sprite.tex);
  282. }
  283. }