RenderWorld.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "RenderWorld.h"
  24. #include "Device.h"
  25. #include "Renderer.h"
  26. #include "Allocator.h"
  27. #include "Camera.h"
  28. #include "Resource.h"
  29. #include "Log.h"
  30. #include "SpriteResource.h"
  31. #include "Mesh.h"
  32. #include "Sprite.h"
  33. #include "Material.h"
  34. #include "Config.h"
  35. #include "Gui.h"
  36. #include "GuiResource.h"
  37. namespace crown
  38. {
  39. namespace render_world_globals
  40. {
  41. static const char* default_vertex =
  42. "precision mediump float;\n"
  43. "uniform mat4 u_model_view_projection;\n"
  44. "attribute vec4 a_position;\n"
  45. "attribute vec2 a_tex_coord0;\n"
  46. "attribute vec4 a_color;\n"
  47. "varying vec2 tex_coord0;\n"
  48. "varying vec4 color;\n"
  49. "void main(void)\n"
  50. "{\n"
  51. " tex_coord0 = a_tex_coord0;\n"
  52. " color = a_color;\n"
  53. " gl_Position = u_model_view_projection * a_position;\n"
  54. "}\n";
  55. static const char* default_fragment =
  56. "precision mediump float;\n"
  57. "varying vec4 color;\n"
  58. "void main(void)\n"
  59. "{\n"
  60. " gl_FragColor = color;\n"
  61. "}\n";
  62. static const char* texture_fragment =
  63. "precision mediump float;\n"
  64. "varying vec2 tex_coord0;\n"
  65. "varying vec4 color;\n"
  66. "uniform sampler2D u_albedo_0;\n"
  67. "void main(void)\n"
  68. "{\n"
  69. " gl_FragColor = texture2D(u_albedo_0, tex_coord0);\n"
  70. "}\n";
  71. static const char* sdf_vertex =
  72. "precision mediump float;\n"
  73. "uniform mat4 u_model_view_projection;\n"
  74. "attribute vec4 a_position;\n"
  75. "attribute vec2 a_tex_coord0;\n"
  76. "varying vec2 v_tex_coord;\n"
  77. "varying vec4 v_color;\n"
  78. "void main(void)\n"
  79. "{\n"
  80. " gl_Position = u_model_view_projection * a_position;\n"
  81. " v_tex_coord = a_tex_coord0;\n"
  82. " v_color = vec4(1, 1, 1, 1);\n"
  83. "}\n";
  84. static const char* sdf_fragment =
  85. "precision mediump float;\n"
  86. "uniform sampler2D u_texture;\n"
  87. "uniform vec4 u_color;\n"
  88. "varying vec2 v_tex_coord;\n"
  89. "const float smoothing = 1.0/16.0;\n"
  90. "void main() {\n"
  91. "float distance = texture2D(u_texture, v_tex_coord).a;\n"
  92. "float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);\n"
  93. "gl_FragColor = vec4(u_color.rgb, alpha);\n"
  94. "}\n";
  95. static const char* default_color_fragment =
  96. "precision mediump float;\n"
  97. "uniform vec4 u_color;\n"
  98. "void main(void)\n"
  99. "{\n"
  100. " gl_FragColor = u_color;\n"
  101. "}\n";
  102. ShaderId default_vs;
  103. ShaderId default_fs;
  104. ShaderId texture_fs;
  105. ShaderId font_vs;
  106. ShaderId font_fs;
  107. ShaderId color_fs;
  108. GPUProgramId texture_program;
  109. GPUProgramId def_program;
  110. GPUProgramId font_program;
  111. GPUProgramId color_program;
  112. UniformId u_albedo_0;
  113. UniformId u_font;
  114. UniformId u_color;
  115. uint32_t num_refs = 0;
  116. void init()
  117. {
  118. if (num_refs)
  119. return;
  120. num_refs++;
  121. Renderer* r = device()->renderer();
  122. u_font = r->create_uniform("u_font", UniformType::INTEGER_1, 1);
  123. u_albedo_0 = r->create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
  124. u_color = r->create_uniform("u_color", UniformType::FLOAT_4, 1);
  125. default_vs = r->create_shader(ShaderType::VERTEX, default_vertex);
  126. default_fs = r->create_shader(ShaderType::FRAGMENT, default_fragment);
  127. texture_fs = r->create_shader(ShaderType::FRAGMENT, texture_fragment);
  128. font_vs = r->create_shader(ShaderType::VERTEX, sdf_vertex);
  129. font_fs = r->create_shader(ShaderType::FRAGMENT, sdf_fragment);
  130. color_fs = r->create_shader(ShaderType::FRAGMENT, default_color_fragment);
  131. def_program = r->create_gpu_program(default_vs, default_fs);
  132. texture_program = r->create_gpu_program(default_vs, texture_fs);
  133. font_program = r->create_gpu_program(font_vs, font_fs);
  134. color_program = r->create_gpu_program(default_vs, color_fs);
  135. }
  136. void shutdown()
  137. {
  138. num_refs--;
  139. if (num_refs)
  140. return;
  141. Renderer* r = device()->renderer();
  142. r->destroy_uniform(u_albedo_0);
  143. r->destroy_uniform(u_font);
  144. r->destroy_uniform(u_color);
  145. r->destroy_gpu_program(texture_program);
  146. r->destroy_gpu_program(def_program);
  147. r->destroy_gpu_program(font_program);
  148. r->destroy_gpu_program(color_program);
  149. r->destroy_shader(default_vs);
  150. r->destroy_shader(default_fs);
  151. r->destroy_shader(texture_fs);
  152. r->destroy_shader(font_vs);
  153. r->destroy_shader(font_fs);
  154. r->destroy_shader(color_fs);
  155. }
  156. GPUProgramId default_program()
  157. {
  158. return def_program;
  159. }
  160. GPUProgramId default_texture_program()
  161. {
  162. return texture_program;
  163. }
  164. GPUProgramId default_font_program()
  165. {
  166. return font_program;
  167. }
  168. GPUProgramId default_color_program()
  169. {
  170. return color_program;
  171. }
  172. UniformId default_albedo_uniform()
  173. {
  174. return u_albedo_0;
  175. }
  176. UniformId default_font_uniform()
  177. {
  178. return u_font;
  179. }
  180. UniformId default_color_uniform()
  181. {
  182. return u_color;
  183. }
  184. };
  185. //-----------------------------------------------------------------------------
  186. RenderWorld::RenderWorld()
  187. : m_mesh_pool(default_allocator(), MAX_MESHES, sizeof(Mesh), CE_ALIGNOF(Mesh))
  188. , m_sprite_pool(default_allocator(), MAX_SPRITES, sizeof(Sprite), CE_ALIGNOF(Sprite))
  189. , m_material_pool(default_allocator(), MAX_MATERIALS, sizeof(Material), CE_ALIGNOF(Material))
  190. , m_gui_pool(default_allocator(), MAX_GUIS, sizeof(Gui), CE_ALIGNOF(Gui))
  191. {
  192. render_world_globals::init();
  193. }
  194. //-----------------------------------------------------------------------------
  195. RenderWorld::~RenderWorld()
  196. {
  197. render_world_globals::shutdown();
  198. }
  199. //-----------------------------------------------------------------------------
  200. MeshId RenderWorld::create_mesh(MeshResource* mr, SceneGraph& sg, int32_t node)
  201. {
  202. Mesh* mesh = CE_NEW(m_mesh_pool, Mesh)(sg, node, mr);
  203. return id_array::create(m_mesh, mesh);
  204. }
  205. //-----------------------------------------------------------------------------
  206. void RenderWorld::destroy_mesh(MeshId id)
  207. {
  208. Mesh* mesh = id_array::get(m_mesh, id);
  209. CE_DELETE(m_mesh_pool, mesh);
  210. id_array::destroy(m_mesh, id);
  211. }
  212. //-----------------------------------------------------------------------------
  213. Mesh* RenderWorld::get_mesh(MeshId mesh)
  214. {
  215. return id_array::get(m_mesh, mesh);
  216. }
  217. //-----------------------------------------------------------------------------
  218. SpriteId RenderWorld::create_sprite(SpriteResource* sr, SceneGraph& sg, int32_t node)
  219. {
  220. Sprite* sprite = CE_NEW(m_sprite_pool, Sprite)(*this, sg, node, sr);
  221. return id_array::create(m_sprite, sprite);
  222. }
  223. //-----------------------------------------------------------------------------
  224. void RenderWorld::destroy_sprite(SpriteId id)
  225. {
  226. CE_DELETE(m_sprite_pool, id_array::get(m_sprite, id));
  227. id_array::destroy(m_sprite, id);
  228. }
  229. //-----------------------------------------------------------------------------
  230. Sprite* RenderWorld::get_sprite(SpriteId id)
  231. {
  232. return id_array::get(m_sprite, id);
  233. }
  234. //-----------------------------------------------------------------------------
  235. MaterialId RenderWorld::create_material(MaterialResource* mr)
  236. {
  237. Material* mat = CE_NEW(m_material_pool, Material)(mr);
  238. return id_array::create(m_materials, mat);
  239. }
  240. //-----------------------------------------------------------------------------
  241. void RenderWorld::destroy_material(MaterialId id)
  242. {
  243. CE_DELETE(m_material_pool, id_array::get(m_materials, id));
  244. id_array::destroy(m_materials, id);
  245. }
  246. //-----------------------------------------------------------------------------
  247. Material* RenderWorld::get_material(MaterialId id)
  248. {
  249. return id_array::get(m_materials, id);
  250. }
  251. //-----------------------------------------------------------------------------
  252. GuiId RenderWorld::create_gui(uint16_t width, uint16_t height)
  253. {
  254. Gui* gui = CE_NEW(m_gui_pool, Gui)(*this, width, height);
  255. GuiId id = id_array::create(m_guis, gui);
  256. gui->set_id(id);
  257. return id;
  258. }
  259. //-----------------------------------------------------------------------------
  260. void RenderWorld::destroy_gui(GuiId id)
  261. {
  262. CE_DELETE(m_gui_pool, id_array::get(m_guis, id));
  263. id_array::destroy(m_guis, id);
  264. }
  265. //-----------------------------------------------------------------------------
  266. Gui* RenderWorld::get_gui(GuiId id)
  267. {
  268. return id_array::get(m_guis, id);
  269. }
  270. //-----------------------------------------------------------------------------
  271. void RenderWorld::update(const Matrix4x4& view, const Matrix4x4& projection, uint16_t x, uint16_t y, uint16_t width, uint16_t height, float dt)
  272. {
  273. Renderer* r = device()->renderer();
  274. Matrix4x4 inv_view = view;
  275. matrix4x4::invert(inv_view);
  276. r->set_layer_view(0, inv_view);
  277. r->set_layer_projection(0, projection);
  278. r->set_layer_viewport(0, x, y, width, height);
  279. r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4(0x353839FF), 1.0f);
  280. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
  281. r->commit(0);
  282. // Draw all meshes
  283. for (uint32_t m = 0; m < id_array::size(m_mesh); m++)
  284. {
  285. const Mesh* mesh = m_mesh.m_objects[m];
  286. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
  287. r->set_vertex_buffer(mesh->m_vbuffer);
  288. r->set_index_buffer(mesh->m_ibuffer);
  289. r->set_program(render_world_globals::default_program());
  290. // r->set_texture(0, render_world_globals::u_albedo_0, grass_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
  291. // r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);
  292. r->set_pose(mesh->world_pose());
  293. r->commit(0);
  294. }
  295. // Draw all sprites
  296. for (uint32_t s = 0; s < id_array::size(m_sprite); s++)
  297. {
  298. r->set_program(render_world_globals::default_texture_program());
  299. // m_sprite[s]->update(dt);
  300. m_sprite[s]->render(*r, render_world_globals::u_albedo_0, dt);
  301. }
  302. }
  303. } // namespace crown