RenderWorld.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. TextureId grass_texture;
  40. TextureId lightmap_texture;
  41. ShaderId default_vs;
  42. ShaderId default_fs;
  43. ShaderId texture_fs;
  44. GPUProgramId default_program;
  45. GPUProgramId texture_program;
  46. UniformId u_albedo_0;
  47. UniformId u_lightmap_0;
  48. UniformId u_brightness;
  49. static const char* default_vertex =
  50. "uniform mat4 u_model;"
  51. "uniform mat4 u_model_view_projection;"
  52. "attribute vec4 a_position;"
  53. "attribute vec2 a_tex_coord0;"
  54. "attribute vec4 a_color;"
  55. "varying vec2 tex_coord0;"
  56. "varying vec4 color;"
  57. "void main(void)"
  58. "{"
  59. " tex_coord0 = a_tex_coord0;"
  60. " color = a_color;"
  61. " gl_Position = u_model_view_projection * a_position;"
  62. "}";
  63. static const char* default_fragment =
  64. "void main(void)"
  65. "{"
  66. " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
  67. "}";
  68. static const char* texture_fragment =
  69. "varying vec2 tex_coord0;"
  70. "varying vec4 color;"
  71. "uniform sampler2D u_albedo_0;"
  72. "void main(void)"
  73. "{"
  74. " gl_FragColor = texture2D(u_albedo_0, tex_coord0);"
  75. "}";
  76. //-----------------------------------------------------------------------------
  77. RenderWorld::RenderWorld()
  78. : m_mesh_pool(default_allocator(), MAX_MESHES, sizeof(Mesh), CE_ALIGNOF(Mesh))
  79. , m_sprite_pool(default_allocator(), MAX_SPRITES, sizeof(Sprite), CE_ALIGNOF(Sprite))
  80. , m_material_pool(default_allocator(), MAX_MATERIALS, sizeof(Material), CE_ALIGNOF(Material))
  81. , m_gui_pool(default_allocator(), MAX_GUIS, sizeof(Gui), CE_ALIGNOF(Gui))
  82. {
  83. Renderer* r = device()->renderer();
  84. default_vs = r->create_shader(ShaderType::VERTEX, default_vertex);
  85. default_fs = r->create_shader(ShaderType::FRAGMENT, default_fragment);
  86. texture_fs = r->create_shader(ShaderType::FRAGMENT, texture_fragment);
  87. u_albedo_0 = r->create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
  88. default_program = r->create_gpu_program(default_vs, default_fs);
  89. texture_program = r->create_gpu_program(default_vs, texture_fs);
  90. }
  91. //-----------------------------------------------------------------------------
  92. RenderWorld::~RenderWorld()
  93. {
  94. Renderer* r = device()->renderer();
  95. r->destroy_shader(default_vs);
  96. r->destroy_shader(default_fs);
  97. r->destroy_shader(texture_fs);
  98. r->destroy_gpu_program(default_program);
  99. r->destroy_gpu_program(texture_program);
  100. r->destroy_uniform(u_albedo_0);
  101. }
  102. //-----------------------------------------------------------------------------
  103. MeshId RenderWorld::create_mesh(MeshResource* mr, SceneGraph& sg, int32_t node)
  104. {
  105. Log::i("create mesh omfg");
  106. Mesh* mesh = CE_NEW(m_mesh_pool, Mesh)(sg, node, mr);
  107. return m_mesh.create(mesh);
  108. }
  109. //-----------------------------------------------------------------------------
  110. void RenderWorld::destroy_mesh(MeshId id)
  111. {
  112. CE_ASSERT(m_mesh.has(id), "Mesh does not exist");
  113. Mesh* mesh = m_mesh.lookup(id);
  114. CE_DELETE(m_mesh_pool, mesh);
  115. m_mesh.destroy(id);
  116. }
  117. //-----------------------------------------------------------------------------
  118. Mesh* RenderWorld::lookup_mesh(MeshId mesh)
  119. {
  120. CE_ASSERT(m_mesh.has(mesh), "Mesh does not exits");
  121. return m_mesh.lookup(mesh);
  122. }
  123. //-----------------------------------------------------------------------------
  124. SpriteId RenderWorld::create_sprite(SpriteResource* sr, SceneGraph& sg, int32_t node)
  125. {
  126. Sprite* sprite = CE_NEW(m_sprite_pool, Sprite)(*this, sg, node, sr);
  127. return m_sprite.create(sprite);
  128. }
  129. //-----------------------------------------------------------------------------
  130. void RenderWorld::destroy_sprite(SpriteId id)
  131. {
  132. CE_ASSERT(m_sprite.has(id), "Sprite does not exist");
  133. Sprite* sprite = m_sprite.lookup(id);
  134. CE_DELETE(m_sprite_pool, sprite);
  135. m_sprite.destroy(id);
  136. }
  137. //-----------------------------------------------------------------------------
  138. Sprite* RenderWorld::lookup_sprite(SpriteId id)
  139. {
  140. CE_ASSERT(m_sprite.has(id), "Sprite does not exist");
  141. return m_sprite.lookup(id);
  142. }
  143. //-----------------------------------------------------------------------------
  144. MaterialId RenderWorld::create_material(MaterialResource* mr)
  145. {
  146. Material* mat = CE_NEW(m_material_pool, Material)(mr);
  147. return m_materials.create(mat);
  148. }
  149. //-----------------------------------------------------------------------------
  150. void RenderWorld::destroy_material(MaterialId id)
  151. {
  152. CE_DELETE(m_material_pool, m_materials.lookup(id));
  153. m_materials.destroy(id);
  154. }
  155. //-----------------------------------------------------------------------------
  156. Material* RenderWorld::lookup_material(MaterialId id)
  157. {
  158. return m_materials.lookup(id);
  159. }
  160. //-----------------------------------------------------------------------------
  161. GuiId RenderWorld::create_gui(GuiResource* gr)
  162. {
  163. Renderer* r = device()->renderer();
  164. Gui* gui = CE_NEW(m_gui_pool, Gui)(*this, gr, *r);
  165. return m_guis.create(gui);
  166. }
  167. //-----------------------------------------------------------------------------
  168. void RenderWorld::destroy_gui(GuiId id)
  169. {
  170. CE_ASSERT(m_guis.has(id), "Gui does not exist");
  171. CE_DELETE(m_gui_pool, m_guis.lookup(id));
  172. m_guis.destroy(id);
  173. }
  174. //-----------------------------------------------------------------------------
  175. Gui* RenderWorld::lookup_gui(GuiId id)
  176. {
  177. CE_ASSERT(m_guis.has(id), "Gui does not exist");
  178. return m_guis.lookup(id);
  179. }
  180. //-----------------------------------------------------------------------------
  181. void RenderWorld::update(const Matrix4x4& view, const Matrix4x4& projection, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  182. {
  183. Renderer* r = device()->renderer();
  184. Matrix4x4 inv_view = view;
  185. inv_view.invert();
  186. r->set_layer_view(0, inv_view);
  187. r->set_layer_projection(0, projection);
  188. r->set_layer_viewport(0, x, y, width, height);
  189. r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  190. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
  191. r->commit(0);
  192. // Draw all meshes
  193. for (uint32_t m = 0; m < m_mesh.size(); m++)
  194. {
  195. const Mesh* mesh = m_mesh.m_objects[m];
  196. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
  197. r->set_vertex_buffer(mesh->m_vbuffer);
  198. r->set_index_buffer(mesh->m_ibuffer);
  199. r->set_program(default_program);
  200. // r->set_texture(0, u_albedo_0, grass_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
  201. // r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);
  202. r->set_pose(mesh->world_pose());
  203. r->commit(0);
  204. }
  205. // Draw all sprites
  206. for (uint32_t s = 0; s < m_sprite.size(); s++)
  207. {
  208. r->set_program(texture_program);
  209. m_sprite[s]->render(*r, u_albedo_0);
  210. }
  211. // Draw all guis
  212. for (uint32_t g = 0; g < m_guis.size(); g++)
  213. {
  214. m_guis[g]->render();
  215. }
  216. }
  217. } // namespace crown