RenderWorld.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace crown
  30. {
  31. TextureId grass_texture;
  32. TextureId lightmap_texture;
  33. ShaderId default_vs;
  34. ShaderId default_fs;
  35. ShaderId texture_fs;
  36. GPUProgramId default_program;
  37. GPUProgramId texture_program;
  38. UniformId u_albedo_0;
  39. UniformId u_lightmap_0;
  40. UniformId u_brightness;
  41. static const char* default_vertex =
  42. "in vec4 a_position;"
  43. "in vec4 a_normal;"
  44. "in vec2 a_tex_coord0;"
  45. "in vec4 a_color;"
  46. "uniform mat4 u_model;"
  47. "uniform mat4 u_model_view_projection;"
  48. "varying out vec2 tex_coord0;"
  49. "varying out vec4 color;"
  50. "void main(void)"
  51. "{"
  52. " tex_coord0 = a_tex_coord0;"
  53. " color = a_color;"
  54. " gl_Position = u_model_view_projection * a_position;"
  55. " gl_FrontColor = vec4(vec3(1, 0, 0), 1.0);"
  56. "}";
  57. static const char* default_fragment =
  58. "void main(void)"
  59. "{"
  60. " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
  61. "}";
  62. static const char* texture_fragment =
  63. "in vec2 tex_coord0;"
  64. "in vec4 color;"
  65. "uniform sampler2D u_albedo_0;"
  66. "uniform sampler2D u_lightmap_0;"
  67. "uniform float u_brightness;"
  68. "void main(void)"
  69. "{"
  70. " gl_FragColor = texture(u_albedo_0, tex_coord0) * texture(u_lightmap_0, tex_coord0) * color * u_brightness;"
  71. "}";
  72. //-----------------------------------------------------------------------------
  73. RenderWorld::RenderWorld()
  74. : m_mesh(default_allocator())
  75. {
  76. Renderer* r = device()->renderer();
  77. default_vs = r->create_shader(ShaderType::VERTEX, default_vertex);
  78. default_fs = r->create_shader(ShaderType::FRAGMENT, default_fragment);
  79. texture_fs = r->create_shader(ShaderType::FRAGMENT, texture_fragment);
  80. u_albedo_0 = r->create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
  81. u_lightmap_0 = r->create_uniform("u_lightmap_0", UniformType::INTEGER_1, 1);
  82. u_brightness = r->create_uniform("u_brightness", UniformType::FLOAT_1, 1);
  83. default_program = r->create_gpu_program(default_vs, default_fs);
  84. texture_program = r->create_gpu_program(default_vs, texture_fs);
  85. create_mesh("monkey");
  86. }
  87. //-----------------------------------------------------------------------------
  88. RenderWorld::~RenderWorld()
  89. {
  90. Renderer* r = device()->renderer();
  91. r->destroy_shader(default_vs);
  92. r->destroy_shader(default_fs);
  93. r->destroy_shader(texture_fs);
  94. r->destroy_gpu_program(default_program);
  95. r->destroy_gpu_program(texture_program);
  96. r->destroy_uniform(u_albedo_0);
  97. r->destroy_uniform(u_lightmap_0);
  98. r->destroy_uniform(u_brightness);
  99. }
  100. //-----------------------------------------------------------------------------
  101. MeshId RenderWorld::create_mesh(const char* name, const Vector3& pos, const Quaternion& rot)
  102. {
  103. MeshResource* mr = (MeshResource*) device()->resource_manager()->lookup("mesh", name);
  104. MeshId mesh = allocate_mesh();
  105. m_mesh[mesh.index].create(mr, pos, rot);
  106. return mesh;
  107. }
  108. //-----------------------------------------------------------------------------
  109. void RenderWorld::destroy_mesh(MeshId /*id*/)
  110. {
  111. }
  112. //-----------------------------------------------------------------------------
  113. void RenderWorld::update(Camera& camera, float /*dt*/)
  114. {
  115. Renderer* r = device()->renderer();
  116. Matrix4x4 camera_view = camera.local_pose();
  117. camera_view.invert();
  118. r->set_layer_view(0, camera_view);
  119. r->set_layer_projection(0, camera.m_projection);
  120. r->set_layer_viewport(0, 0, 0, 1000, 625);
  121. r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  122. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
  123. r->commit(0);
  124. // Draw all meshes
  125. for (uint32_t m = 0; m < m_mesh.size(); m++)
  126. {
  127. const Mesh& mesh = m_mesh[m];
  128. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
  129. r->set_vertex_buffer(mesh.m_vbuffer);
  130. r->set_index_buffer(mesh.m_ibuffer);
  131. r->set_program(default_program);
  132. /* r->set_texture(0, u_albedo_0, grass_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
  133. r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);*/
  134. r->set_pose(mesh.m_local_pose);
  135. r->commit(0);
  136. }
  137. }
  138. //-----------------------------------------------------------------------------
  139. MeshId RenderWorld::allocate_mesh()
  140. {
  141. MeshId mesh = m_mesh_table.create();
  142. if (m_mesh.size() <= mesh.index)
  143. {
  144. m_mesh.resize(1);
  145. }
  146. return mesh;
  147. }
  148. //-----------------------------------------------------------------------------
  149. void RenderWorld::deallocate_mesh(MeshId /*id*/)
  150. {
  151. }
  152. } // namespace crown