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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 RectProg: public GlProgram {
  62. template<typename... T>
  63. RectProg(const T &... shaders): GlProgram(shaders...) { init(); }
  64. ~RectProg() { deinit(); }
  65. GLint camera = uniformLoc("camera");
  66. GLint pos = uniformLoc("pos");
  67. GLint size = uniformLoc("size");
  68. GLint vertex = attribLoc("vertex");
  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. }
  85. void disable() {
  86. glDisableVertexAttribArray(vertex);
  87. glCheck();
  88. }
  89. void init() {
  90. glGenBuffers(1, &vbo);
  91. glCheck();
  92. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  93. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
  94. glCheck();
  95. }
  96. void deinit() {
  97. glDeleteBuffers(1, &vbo);
  98. glCheck();
  99. }
  100. };
  101. struct SpriteProg: public GlProgram {
  102. template<typename... T>
  103. SpriteProg(const T &... shaders): GlProgram(shaders...) { init(); }
  104. ~SpriteProg() { deinit(); }
  105. GLint camera = uniformLoc("camera");
  106. GLint transform = uniformLoc("transform");
  107. GLint frameSize = uniformLoc("frameSize");
  108. GLint frameInfo = uniformLoc("frameInfo");
  109. GLint vertex = attribLoc("vertex");
  110. GLint tex = uniformLoc("tex");
  111. GLuint vbo;
  112. static constexpr GLfloat vertexes[] = {
  113. 0.0f, 0.0f, // pos 0: top left
  114. 0.0f, 1.0f, // pos 1: bottom left
  115. 1.0f, 1.0f, // pos 2: bottom right
  116. 1.0f, 1.0f, // pos 2: bottom right
  117. 1.0f, 0.0f, // pos 3: top right
  118. 0.0f, 0.0f, // pos 0: top left
  119. };
  120. void enable() {
  121. glUseProgram(id());
  122. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  123. glVertexAttribPointer(vertex, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
  124. glEnableVertexAttribArray(vertex);
  125. glCheck();
  126. glUniform1i(tex, 0);
  127. }
  128. void disable() {
  129. glDisableVertexAttribArray(vertex);
  130. glCheck();
  131. }
  132. void init() {
  133. glGenBuffers(1, &vbo);
  134. glCheck();
  135. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  136. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
  137. glCheck();
  138. }
  139. void deinit() {
  140. glDeleteBuffers(1, &vbo);
  141. glCheck();
  142. }
  143. };
  144. struct RendererState {
  145. GlVxShader spriteVx{Shaders::spriteVx};
  146. GlFrShader spriteFr{Shaders::spriteFr};
  147. GlVxShader chunkVx{Shaders::chunkVx};
  148. GlFrShader chunkFr{Shaders::chunkFr};
  149. GlVxShader rectVx{Shaders::rectVx};
  150. GlFrShader rectFr{Shaders::rectFr};
  151. SpriteProg spriteProg{spriteVx, spriteFr};
  152. ChunkProg chunkProg{chunkVx, chunkFr};
  153. RectProg rectProg{rectVx, rectFr};
  154. GLuint atlasTex;
  155. SwanCommon::Vec2 atlasTexSize;
  156. };
  157. Renderer::Renderer(): state_(std::make_unique<RendererState>()) {
  158. glGenTextures(1, &state_->atlasTex);
  159. glCheck();
  160. }
  161. Renderer::~Renderer() = default;
  162. void Renderer::draw(const RenderCamera &cam) {
  163. Mat3gf camMat;
  164. camMat.translate(-cam.pos);
  165. if (cam.size.y > cam.size.x) {
  166. float ratio = (float)cam.size.y / (float)cam.size.x;
  167. winScale_ = {1/ratio, 1};
  168. camMat.scale({cam.zoom * ratio, -cam.zoom});
  169. } else {
  170. float ratio = (float)cam.size.x / (float)cam.size.y;
  171. winScale_ = {1, 1/ratio};
  172. camMat.scale({cam.zoom, -cam.zoom * ratio});
  173. }
  174. auto &chunkProg = state_->chunkProg;
  175. auto &spriteProg = state_->spriteProg;
  176. auto &rectProg = state_->rectProg;
  177. {
  178. chunkProg.enable();
  179. glUniformMatrix3fv(chunkProg.camera, 1, GL_TRUE, camMat.data());
  180. glCheck();
  181. glUniform2f(chunkProg.tileAtlasSize, state_->atlasTexSize.x, state_->atlasTexSize.y);
  182. glCheck();
  183. glActiveTexture(GL_TEXTURE0);
  184. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  185. glCheck();
  186. glActiveTexture(GL_TEXTURE1);
  187. for (auto [pos, chunk]: drawChunks_) {
  188. glUniform2f(chunkProg.pos, pos.x, pos.y);
  189. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  190. glDrawArrays(GL_TRIANGLES, 0, 6);
  191. glCheck();
  192. }
  193. drawChunks_.clear();
  194. chunkProg.disable();
  195. }
  196. {
  197. spriteProg.enable();
  198. glUniformMatrix3fv(spriteProg.camera, 1, GL_TRUE, camMat.data());
  199. glCheck();
  200. glActiveTexture(GL_TEXTURE0);
  201. for (auto [mat, frame, sprite]: drawSprites_) {
  202. glUniformMatrix3fv(spriteProg.transform, 1, GL_TRUE, mat.data());
  203. glUniform2f(spriteProg.frameSize, sprite.scale.x, sprite.scale.y);
  204. glUniform2f(spriteProg.frameInfo, sprite.frameCount, frame);
  205. glBindTexture(GL_TEXTURE_2D, sprite.tex);
  206. glDrawArrays(GL_TRIANGLES, 0, 6);
  207. glCheck();
  208. }
  209. drawSprites_.clear();
  210. spriteProg.disable();
  211. }
  212. {
  213. rectProg.enable();
  214. glUniformMatrix3fv(rectProg.camera, 1, GL_TRUE, camMat.data());
  215. glCheck();
  216. for (auto [pos, size]: drawRects_) {
  217. glUniform2f(rectProg.pos, pos.x, pos.y);
  218. glUniform2f(rectProg.size, size.x, size.y);
  219. glDrawArrays(GL_TRIANGLES, 0, 6);
  220. glCheck();
  221. }
  222. drawRects_.clear();
  223. rectProg.disable();
  224. }
  225. }
  226. void Renderer::uploadTileAtlas(const void *data, int width, int height) {
  227. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  228. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  229. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  230. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  231. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  233. glCheck();
  234. state_->atlasTexSize = {
  235. (float)(int)(width / SwanCommon::TILE_SIZE),
  236. (float)(int)(height / SwanCommon::TILE_SIZE) };
  237. }
  238. void Renderer::modifyTile(TileID id, const void *data) {
  239. int w = (int)state_->atlasTexSize.x;
  240. int x = id % w;
  241. int y = id / w;
  242. glActiveTexture(GL_TEXTURE0);
  243. glBindTexture(GL_TEXTURE_2D, state_->atlasTex);
  244. glTexSubImage2D(
  245. GL_TEXTURE_2D, 0, x * SwanCommon::TILE_SIZE, y * SwanCommon::TILE_SIZE,
  246. SwanCommon::TILE_SIZE, SwanCommon::TILE_SIZE,
  247. GL_RGBA, GL_UNSIGNED_BYTE, data);
  248. glCheck();
  249. }
  250. RenderChunk Renderer::createChunk(
  251. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]) {
  252. RenderChunk chunk;
  253. glGenTextures(1, &chunk.tex);
  254. glCheck();
  255. glActiveTexture(GL_TEXTURE0);
  256. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  257. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  258. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  259. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  260. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  261. glCheck();
  262. static_assert(
  263. std::endian::native == std::endian::big ||
  264. std::endian::native == std::endian::little,
  265. "Expected either big or little endian");
  266. if constexpr (std::endian::native == std::endian::little) {
  267. glTexImage2D(
  268. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  269. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  270. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, tiles);
  271. } else if constexpr (std::endian::native == std::endian::big) {
  272. uint8_t buf[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT * 2];
  273. for (size_t y = 0; y < SwanCommon::CHUNK_HEIGHT; ++y) {
  274. for (size_t x = 0; x < SwanCommon::CHUNK_WIDTH; ++x) {
  275. size_t dst = y * SwanCommon::CHUNK_WIDTH * 2 + x * 2;
  276. size_t src = y * SwanCommon::CHUNK_WIDTH + x;
  277. buf[dst + 0] = tiles[src] & 0xff;
  278. buf[dst + 1] = (tiles[src] & 0xff00) >> 8;
  279. }
  280. }
  281. glTexImage2D(
  282. GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
  283. SwanCommon::CHUNK_WIDTH, SwanCommon::CHUNK_HEIGHT,
  284. 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  285. }
  286. glCheck();
  287. return chunk;
  288. }
  289. void Renderer::modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id) {
  290. glActiveTexture(GL_TEXTURE0);
  291. glBindTexture(GL_TEXTURE_2D, chunk.tex);
  292. glCheck();
  293. static_assert(
  294. std::endian::native == std::endian::big ||
  295. std::endian::native == std::endian::little,
  296. "Expected either big or little endian");
  297. if constexpr (std::endian::native == std::endian::little) {
  298. glTexSubImage2D(
  299. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  300. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, &id);
  301. } else if constexpr (std::endian::native == std::endian::big) {
  302. uint8_t buf[] = { (uint8_t)(id & 0xff), (uint8_t)((id & 0xff00) >> 8) };
  303. glTexSubImage2D(
  304. GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
  305. GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
  306. }
  307. glCheck();
  308. }
  309. void Renderer::destroyChunk(RenderChunk chunk) {
  310. glDeleteTextures(1, &chunk.tex);
  311. glCheck();
  312. }
  313. RenderSprite Renderer::createSprite(void *data, int width, int height, int fh) {
  314. RenderSprite sprite;
  315. sprite.scale = {
  316. (float)width / SwanCommon::TILE_SIZE,
  317. (float)fh / SwanCommon::TILE_SIZE };
  318. sprite.frameCount = height / fh;
  319. glGenTextures(1, &sprite.tex);
  320. glCheck();
  321. glActiveTexture(GL_TEXTURE0);
  322. glBindTexture(GL_TEXTURE_2D, sprite.tex);
  323. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  324. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  325. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  326. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  327. glCheck();
  328. glTexImage2D(
  329. GL_TEXTURE_2D, 0, GL_RGBA, width, height,
  330. 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  331. glCheck();
  332. return sprite;
  333. }
  334. RenderSprite Renderer::createSprite(void *data, int width, int height) {
  335. return createSprite(data, width, height, height);
  336. }
  337. void Renderer::destroySprite(RenderSprite sprite) {
  338. glDeleteTextures(1, &sprite.tex);
  339. }
  340. }