Gui.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "Gui.h"
  24. #include "Assert.h"
  25. #include "Renderer.h"
  26. #include "Vector3.h"
  27. #include "Vector2.h"
  28. #include "GuiResource.h"
  29. #include "RenderWorld.h"
  30. #include "RendererTypes.h"
  31. #include "Vector3.h"
  32. #include "Vector2.h"
  33. #include "Color4.h"
  34. #include "GuiRect.h"
  35. #include "GuiTriangle.h"
  36. #include "GuiImage.h"
  37. namespace crown
  38. {
  39. ShaderId gui_default_vs;
  40. ShaderId gui_default_fs;
  41. ShaderId gui_texture_fs;
  42. GPUProgramId gui_default_program;
  43. GPUProgramId gui_texture_program;
  44. UniformId gui_albedo_0;
  45. static const char* default_vertex =
  46. "uniform mat4 u_model;"
  47. "uniform mat4 u_model_view_projection;"
  48. "attribute vec4 a_position;"
  49. "attribute vec2 a_tex_coord0;"
  50. "attribute vec4 a_color;"
  51. "varying vec2 tex_coord0;"
  52. "varying 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. "varying vec4 color;"
  61. "void main(void)"
  62. "{"
  63. " gl_FragColor = color;"
  64. "}";
  65. static const char* texture_fragment =
  66. "varying vec2 tex_coord0;"
  67. "varying vec4 color;"
  68. "uniform sampler2D u_albedo_0;"
  69. "void main(void)"
  70. "{"
  71. " gl_FragColor = texture2D(u_albedo_0, tex_coord0);"
  72. "}";
  73. //-----------------------------------------------------------------------------
  74. Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
  75. : m_render_world(render_world)
  76. , m_resource(gr)
  77. , m_r(r)
  78. , m_rect_pool(default_allocator(), MAX_GUI_RECTS, sizeof(GuiRect), CE_ALIGNOF(GuiRect))
  79. , m_triangle_pool(default_allocator(), MAX_GUI_TRIANGLES, sizeof(GuiTriangle), CE_ALIGNOF(GuiTriangle))
  80. , m_image_pool(default_allocator(), MAX_GUI_IMAGES, sizeof(GuiImage), CE_ALIGNOF(GuiImage))
  81. {
  82. // orthographic projection
  83. Vector2 size = m_resource->gui_size();
  84. m_projection.build_projection_ortho_rh(0, size.x, size.y, 0, -0.01f, 100.0f);
  85. // pose
  86. Vector3 pos = m_resource->gui_position();
  87. m_pose.load_identity();
  88. m_pose.set_translation(pos);
  89. // FIXME FIXME FIXME -- Shaders init should not be here
  90. gui_default_vs = m_r.create_shader(ShaderType::VERTEX, default_vertex);
  91. gui_default_fs = m_r.create_shader(ShaderType::FRAGMENT, default_fragment);
  92. gui_texture_fs = m_r.create_shader(ShaderType::FRAGMENT, texture_fragment);
  93. gui_default_program = m_r.create_gpu_program(gui_default_vs, gui_default_fs);
  94. gui_texture_program = m_r.create_gpu_program(gui_default_vs, gui_texture_fs);
  95. gui_albedo_0 = m_r.create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
  96. // Gui's rects creation
  97. for (uint32_t i = 0; i < m_resource->num_rects(); i++)
  98. {
  99. GuiRectData data = m_resource->get_rect(i);
  100. Vector3 pos(data.position[0], data.position[1], data.position[2]);
  101. Vector2 size(data.size[0], data.size[1]);
  102. Color4 color(data.color[0], data.color[1], data.color[2], data.color[3]);
  103. create_rect(pos, size, color);
  104. }
  105. for (uint32_t i = 0; i < m_resource->num_triangles(); i++)
  106. {
  107. GuiTriangleData data = m_resource->get_triangle(i);
  108. Vector2 p1(data.points[0], data.points[1]);
  109. Vector2 p2(data.points[2], data.points[3]);
  110. Vector2 p3(data.points[4], data.points[5]);
  111. Color4 color(data.color[0], data.color[1], data.color[2], data.color[3]);
  112. create_triangle(p1, p2, p3, color);
  113. }
  114. for (uint32_t i = 0; i < m_resource->num_images(); i++)
  115. {
  116. GuiImageData data = m_resource->get_image(i);
  117. ResourceId mat = data.material;
  118. Vector3 pos(data.position[0], data.position[1], 0);
  119. Vector2 size(data.size[0], data.size[1]);
  120. create_image(mat, pos, size);
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. Gui::~Gui()
  125. {
  126. for (uint32_t i = 0; i < m_resource->num_rects(); i++)
  127. {
  128. CE_DELETE(m_rect_pool, m_rects[i]);
  129. }
  130. for (uint32_t i = 0; i < m_resource->num_triangles(); i++)
  131. {
  132. CE_DELETE(m_triangle_pool, m_triangles[i]);
  133. }
  134. for (uint32_t i = 0; i < m_resource->num_images(); i++)
  135. {
  136. CE_DELETE(m_image_pool, m_images[i]);
  137. }
  138. // FIXME FIXME FIXME -- Shaders destruction should not be here
  139. m_r.destroy_uniform(gui_albedo_0);
  140. m_r.destroy_gpu_program(gui_texture_program);
  141. m_r.destroy_gpu_program(gui_default_program);
  142. m_r.destroy_shader(gui_texture_fs);
  143. m_r.destroy_shader(gui_default_fs);
  144. m_r.destroy_shader(gui_default_vs);
  145. }
  146. //-----------------------------------------------------------------------------
  147. Vector2 Gui::resolution() const
  148. {
  149. return m_resource->gui_size();
  150. }
  151. //-----------------------------------------------------------------------------
  152. void Gui::move(const Vector3& pos)
  153. {
  154. m_pose.load_identity();
  155. m_pose.set_translation(pos);
  156. }
  157. //-----------------------------------------------------------------------------
  158. GuiRectId Gui::create_rect(const Vector3& pos, const Vector2& size, const Color4& color)
  159. {
  160. GuiRect* rect = CE_NEW(m_rect_pool, GuiRect)(m_r, pos, size, color);
  161. return m_rects.create(rect);
  162. }
  163. //-----------------------------------------------------------------------------
  164. void Gui::update_rect(GuiRectId id, const Vector3& pos, const Vector2& size, const Color4& color)
  165. {
  166. CE_ASSERT(m_rects.has(id), "GuiRect does not exists");
  167. GuiRect* rect = m_rects.lookup(id);
  168. rect->update(pos, size, color);
  169. }
  170. //-----------------------------------------------------------------------------
  171. void Gui::destroy_rect(GuiRectId id)
  172. {
  173. CE_ASSERT(m_rects.has(id), "GuiRect does not exist");
  174. GuiRect* rect = m_rects.lookup(id);
  175. CE_DELETE(m_rect_pool, rect);
  176. m_rects.destroy(id);
  177. }
  178. //-----------------------------------------------------------------------------
  179. GuiTriangleId Gui::create_triangle(const Vector2& p1, const Vector2& p2, const Vector2& p3, const Color4& color)
  180. {
  181. GuiTriangle* triangle = CE_NEW(m_triangle_pool, GuiTriangle)(m_r, p1, p2, p3, color);
  182. return m_triangles.create(triangle);
  183. }
  184. //-----------------------------------------------------------------------------
  185. void Gui::update_triangle(GuiTriangleId id, const Vector2& p1, const Vector2& p2, const Vector2& p3, const Color4& color)
  186. {
  187. CE_ASSERT(m_triangles.has(id), "GuiTriangle does not exists");
  188. GuiTriangle* triangle = m_triangles.lookup(id);
  189. triangle->update(p1, p2, p3, color);
  190. }
  191. //-----------------------------------------------------------------------------
  192. void Gui::destroy_triangle(GuiTriangleId id)
  193. {
  194. CE_ASSERT(m_triangles.has(id), "Guitriangle does not exist");
  195. GuiTriangle* triangle = m_triangles.lookup(id);
  196. CE_DELETE(m_triangle_pool, triangle);
  197. m_triangles.destroy(id);
  198. }
  199. //-----------------------------------------------------------------------------
  200. GuiImageId Gui::create_image(ResourceId material, const Vector3& pos, const Vector2& size)
  201. {
  202. GuiImage* image = CE_NEW(m_image_pool, GuiImage)(m_render_world, m_r, material, pos, size);
  203. return m_images.create(image);
  204. }
  205. //-----------------------------------------------------------------------------
  206. void Gui::update_image(GuiImageId id, const Vector3& pos, const Vector2& size)
  207. {
  208. CE_ASSERT(m_images.has(id), "GuiImage does not exists");
  209. GuiImage* image = m_images.lookup(id);
  210. image->update(pos, size);
  211. }
  212. //-----------------------------------------------------------------------------
  213. void Gui::destroy_image(GuiImageId id)
  214. {
  215. CE_ASSERT(m_images.has(id), "GuiImage does not exists");
  216. GuiImage* image = m_images.lookup(id);
  217. CE_DELETE(m_image_pool, image);
  218. m_images.destroy(id);
  219. }
  220. //-----------------------------------------------------------------------------
  221. void Gui::render()
  222. {
  223. Vector2 size = m_resource->gui_size();
  224. m_r.set_layer_view(1, Matrix4x4::IDENTITY);
  225. m_r.set_layer_projection(1, m_projection);
  226. m_r.set_layer_viewport(1, m_pose.translation().x, m_pose.translation().y, size.x, size.y);
  227. // Render all Rects
  228. for (uint32_t i = 0; i < m_rects.size(); i++)
  229. {
  230. m_r.set_program(gui_default_program);
  231. m_r.set_state(STATE_DEPTH_WRITE
  232. | STATE_COLOR_WRITE
  233. | STATE_ALPHA_WRITE
  234. | STATE_CULL_CW
  235. | STATE_PRIMITIVE_LINES
  236. | STATE_BLEND_EQUATION_ADD
  237. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  238. m_r.set_pose(m_pose);
  239. m_rects[i]->render();
  240. }
  241. // Render all Triangles
  242. for (uint32_t i = 0; i < m_triangles.size(); i++)
  243. {
  244. m_r.set_program(gui_default_program);
  245. m_r.set_state(STATE_DEPTH_WRITE
  246. | STATE_COLOR_WRITE
  247. | STATE_ALPHA_WRITE
  248. | STATE_CULL_CW
  249. | STATE_PRIMITIVE_LINES
  250. | STATE_BLEND_EQUATION_ADD
  251. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  252. m_r.set_pose(m_pose);
  253. m_triangles[i]->render();
  254. }
  255. for (uint32_t i = 0; i < m_images.size(); i++)
  256. {
  257. m_r.set_program(gui_texture_program);
  258. m_r.set_state(STATE_DEPTH_WRITE
  259. | STATE_COLOR_WRITE
  260. | STATE_ALPHA_WRITE
  261. | STATE_CULL_CW
  262. | STATE_PRIMITIVE_TRIANGLES
  263. | STATE_BLEND_EQUATION_ADD
  264. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  265. m_r.set_pose(m_pose);
  266. m_images[i]->render(gui_albedo_0);
  267. }
  268. }
  269. } // namespace crown