RenderWorld.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "ResourceManager.h"
  26. #include "Renderer.h"
  27. #include "Allocator.h"
  28. #include "Camera.h"
  29. #include "Resource.h"
  30. #include "Log.h"
  31. #include "SpriteResource.h"
  32. namespace crown
  33. {
  34. TextureId grass_texture;
  35. TextureId lightmap_texture;
  36. ShaderId default_vs;
  37. ShaderId default_fs;
  38. ShaderId texture_fs;
  39. GPUProgramId default_program;
  40. GPUProgramId texture_program;
  41. UniformId u_albedo_0;
  42. UniformId u_lightmap_0;
  43. UniformId u_brightness;
  44. static const char* default_vertex =
  45. "uniform mat4 u_model;"
  46. "uniform mat4 u_model_view_projection;"
  47. "in vec4 a_position;"
  48. "in vec4 a_normal;"
  49. "in vec2 a_tex_coord0;"
  50. "in vec4 a_color;"
  51. "varying out vec2 tex_coord0;"
  52. "varying out vec4 color;"
  53. "void main(void)"
  54. "{"
  55. " tex_coord0 = a_tex_coord0;"
  56. " color = a_color;"
  57. " gl_Position = u_model_view_projection * a_position;"
  58. "}";
  59. static const char* default_fragment =
  60. "void main(void)"
  61. "{"
  62. " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
  63. "}";
  64. static const char* texture_fragment =
  65. "in vec2 tex_coord0;"
  66. "in vec4 color;"
  67. "uniform sampler2D u_albedo_0;"
  68. "void main(void)"
  69. "{"
  70. " gl_FragColor = texture(u_albedo_0, tex_coord0);"
  71. "}";
  72. //-----------------------------------------------------------------------------
  73. RenderWorld::RenderWorld()
  74. : m_mesh_pool(default_allocator(), MAX_MESHES, sizeof(Mesh), CE_ALIGNOF(Mesh))
  75. , m_sprite_pool(default_allocator(), MAX_SPRITES, sizeof(Sprite), CE_ALIGNOF(Sprite))
  76. {
  77. Renderer* r = device()->renderer();
  78. default_vs = r->create_shader(ShaderType::VERTEX, default_vertex);
  79. default_fs = r->create_shader(ShaderType::FRAGMENT, default_fragment);
  80. texture_fs = r->create_shader(ShaderType::FRAGMENT, texture_fragment);
  81. u_albedo_0 = r->create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
  82. default_program = r->create_gpu_program(default_vs, default_fs);
  83. texture_program = r->create_gpu_program(default_vs, texture_fs);
  84. }
  85. //-----------------------------------------------------------------------------
  86. RenderWorld::~RenderWorld()
  87. {
  88. Renderer* r = device()->renderer();
  89. r->destroy_shader(default_vs);
  90. r->destroy_shader(default_fs);
  91. r->destroy_shader(texture_fs);
  92. r->destroy_gpu_program(default_program);
  93. r->destroy_gpu_program(texture_program);
  94. r->destroy_uniform(u_albedo_0);
  95. }
  96. //-----------------------------------------------------------------------------
  97. MeshId RenderWorld::create_mesh(ResourceId id, SceneGraph& sg, int32_t node)
  98. {
  99. MeshResource* mr = (MeshResource*) device()->resource_manager()->data(id);
  100. // Allocate memory for mesh
  101. Mesh* mesh = CE_NEW(m_mesh_pool, Mesh)(sg, node, mr);
  102. return m_mesh.create(mesh);
  103. }
  104. //-----------------------------------------------------------------------------
  105. void RenderWorld::destroy_mesh(MeshId id)
  106. {
  107. CE_ASSERT(m_mesh.has(id), "Mesh does not exist");
  108. Mesh* mesh = m_mesh.lookup(id);
  109. CE_DELETE(m_mesh_pool, mesh);
  110. m_mesh.destroy(id);
  111. }
  112. //-----------------------------------------------------------------------------
  113. Mesh* RenderWorld::lookup_mesh(MeshId mesh)
  114. {
  115. CE_ASSERT(m_mesh.has(mesh), "Mesh does not exits");
  116. return m_mesh.lookup(mesh);
  117. }
  118. //-----------------------------------------------------------------------------
  119. SpriteId RenderWorld::create_sprite(ResourceId id, SceneGraph& sg, int32_t node)
  120. {
  121. SpriteResource* sr = (SpriteResource*) device()->resource_manager()->data(id);
  122. // Allocate memory for sprite
  123. Sprite* sprite = CE_NEW(m_sprite_pool, Sprite)(sg, node, sr);
  124. return m_sprite.create(sprite);
  125. }
  126. //-----------------------------------------------------------------------------
  127. void RenderWorld::destroy_sprite(SpriteId id)
  128. {
  129. CE_ASSERT(m_sprite.has(id), "Sprite does not exist");
  130. Sprite* sprite = m_sprite.lookup(id);
  131. CE_DELETE(m_sprite_pool, sprite);
  132. m_sprite.destroy(id);
  133. }
  134. //-----------------------------------------------------------------------------
  135. Sprite* RenderWorld::lookup_sprite(SpriteId id)
  136. {
  137. CE_ASSERT(m_sprite.has(id), "Sprite does not exist");
  138. return m_sprite.lookup(id);
  139. }
  140. //-----------------------------------------------------------------------------
  141. void RenderWorld::update(const Matrix4x4& , const Matrix4x4& , uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  142. {
  143. static uint64_t frames = 0;
  144. Renderer* r = device()->renderer();
  145. Matrix4x4 projection;
  146. float ovest = -6;
  147. float east = 6;
  148. float north = 6 / 1.6;
  149. float south = -6 / 1.6;
  150. projection.build_projection_ortho_rh(ovest, east, south, north, 0.1, 100.0);
  151. Matrix4x4 view = Matrix4x4::IDENTITY;
  152. view.set_translation(Vector3(0, 0, 1));
  153. Matrix4x4 inv_view = view;
  154. inv_view.invert();
  155. r->set_layer_view(0, inv_view);
  156. r->set_layer_projection(0, projection);
  157. r->set_layer_viewport(0, x, y, width, height);
  158. r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  159. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
  160. r->commit(0);
  161. // Draw all meshes
  162. for (uint32_t m = 0; m < m_mesh.size(); m++)
  163. {
  164. const Mesh* mesh = m_mesh.m_objects[m];
  165. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
  166. r->set_vertex_buffer(mesh->m_vbuffer);
  167. r->set_index_buffer(mesh->m_ibuffer);
  168. r->set_program(default_program);
  169. /*r->set_texture(0, u_albedo_0, grass_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
  170. r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);*/
  171. r->set_pose(mesh->world_pose());
  172. r->commit(0);
  173. }
  174. for (uint32_t s = 0; s < m_sprite.size(); s++)
  175. {
  176. Sprite* sprite = m_sprite.m_objects[s];
  177. r->set_state(STATE_DEPTH_WRITE
  178. | STATE_COLOR_WRITE
  179. | STATE_ALPHA_WRITE
  180. | STATE_CULL_CW
  181. | STATE_BLEND_EQUATION_ADD
  182. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  183. r->set_vertex_buffer(sprite->m_vb);
  184. r->set_index_buffer(sprite->m_ib);
  185. r->set_program(texture_program);
  186. r->set_texture(0, u_albedo_0, sprite->m_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
  187. r->set_pose(sprite->world_pose());
  188. r->commit(0);
  189. }
  190. frames++;
  191. }
  192. } // namespace crown