Gui.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "FontResource.h"
  35. #include "GuiRect.h"
  36. #include "GuiTriangle.h"
  37. #include "GuiImage.h"
  38. #include "GuiText.h"
  39. namespace crown
  40. {
  41. ShaderId gui_default_vs;
  42. ShaderId gui_default_fs;
  43. ShaderId gui_texture_fs;
  44. GPUProgramId gui_default_program;
  45. GPUProgramId gui_texture_program;
  46. UniformId gui_albedo_0;
  47. ShaderId font_vs;
  48. ShaderId font_fs;
  49. GPUProgramId font_program;
  50. UniformId font_uniform;
  51. static const char* default_vertex =
  52. "uniform mat4 u_model_view_projection;"
  53. "attribute vec4 a_position;"
  54. "attribute vec2 a_tex_coord0;"
  55. "attribute vec4 a_color;"
  56. "varying vec2 tex_coord0;"
  57. "varying vec4 color;"
  58. "void main(void)"
  59. "{"
  60. " tex_coord0 = a_tex_coord0;"
  61. " color = a_color;"
  62. " gl_Position = u_model_view_projection * a_position;"
  63. "}";
  64. static const char* default_fragment =
  65. "varying vec4 color;"
  66. "void main(void)"
  67. "{"
  68. " gl_FragColor = color;"
  69. "}";
  70. static const char* texture_fragment =
  71. "varying vec2 tex_coord0;"
  72. "varying vec4 color;"
  73. "uniform sampler2D u_albedo_0;"
  74. "void main(void)"
  75. "{"
  76. " gl_FragColor = texture2D(u_albedo_0, tex_coord0);"
  77. "}";
  78. static const char* sdf_vertex =
  79. "uniform mat4 u_model_view_projection;"
  80. "attribute vec4 a_position;"
  81. "attribute vec2 a_tex_coord0;"
  82. "attribute vec4 a_color;"
  83. "varying vec2 v_tex_coord;"
  84. "varying vec4 v_color;"
  85. "void main(void)"
  86. "{"
  87. " gl_Position = u_model_view_projection * a_position;"
  88. " v_tex_coord = a_tex_coord0;"
  89. " v_color = vec4(1, 1, 1, 1);"
  90. "}";
  91. static const char* sdf_fragment =
  92. "uniform sampler2D u_texture;"
  93. "varying vec4 v_color;"
  94. "varying vec2 v_tex_coord;"
  95. "const float smoothing = 1.0/16.0;"
  96. "void main() {"
  97. "float distance = texture2D(u_texture, v_tex_coord).a;"
  98. "float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);"
  99. "gl_FragColor = vec4(v_color.rgb, alpha);"
  100. "}";
  101. //-----------------------------------------------------------------------------
  102. Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
  103. : m_render_world(render_world)
  104. , m_resource(gr)
  105. , m_r(r)
  106. , m_rect_pool(default_allocator(), MAX_GUI_RECTS, sizeof(GuiRect), CE_ALIGNOF(GuiRect))
  107. , m_triangle_pool(default_allocator(), MAX_GUI_TRIANGLES, sizeof(GuiTriangle), CE_ALIGNOF(GuiTriangle))
  108. , m_image_pool(default_allocator(), MAX_GUI_IMAGES, sizeof(GuiImage), CE_ALIGNOF(GuiImage))
  109. , m_text_pool(default_allocator(), MAX_GUI_TEXTS, sizeof(GuiText), CE_ALIGNOF(GuiText))
  110. {
  111. // orthographic projection
  112. Vector2 size = m_resource->gui_size();
  113. m_projection.build_projection_ortho_rh(0, size.x, size.y, 0, -0.01f, 100.0f);
  114. // pose
  115. Vector3 pos = m_resource->gui_position();
  116. m_pose.load_identity();
  117. m_pose.set_translation(pos);
  118. // FIXME FIXME FIXME -- Shaders init should not be here
  119. gui_default_vs = m_r.create_shader(ShaderType::VERTEX, default_vertex);
  120. gui_default_fs = m_r.create_shader(ShaderType::FRAGMENT, default_fragment);
  121. gui_texture_fs = m_r.create_shader(ShaderType::FRAGMENT, texture_fragment);
  122. gui_default_program = m_r.create_gpu_program(gui_default_vs, gui_default_fs);
  123. gui_texture_program = m_r.create_gpu_program(gui_default_vs, gui_texture_fs);
  124. gui_albedo_0 = m_r.create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
  125. // FIXME FIXME FIXME -- Shaders init should not be here
  126. font_vs = m_r.create_shader(ShaderType::VERTEX, sdf_vertex);
  127. font_fs = m_r.create_shader(ShaderType::FRAGMENT, sdf_fragment);
  128. font_program = m_r.create_gpu_program(font_vs, font_fs);
  129. font_uniform = m_r.create_uniform("u_texture", UniformType::INTEGER_1, 1);
  130. // Gui's rects creation
  131. for (uint32_t i = 0; i < m_resource->num_rects(); i++)
  132. {
  133. GuiRectData data = m_resource->get_rect(i);
  134. Vector3 pos(data.position[0], data.position[1], data.position[2]);
  135. Vector2 size(data.size[0], data.size[1]);
  136. Color4 color(data.color[0], data.color[1], data.color[2], data.color[3]);
  137. create_rect(pos, size, color);
  138. }
  139. for (uint32_t i = 0; i < m_resource->num_triangles(); i++)
  140. {
  141. GuiTriangleData data = m_resource->get_triangle(i);
  142. Vector2 p1(data.points[0], data.points[1]);
  143. Vector2 p2(data.points[2], data.points[3]);
  144. Vector2 p3(data.points[4], data.points[5]);
  145. Color4 color(data.color[0], data.color[1], data.color[2], data.color[3]);
  146. create_triangle(p1, p2, p3, color);
  147. }
  148. for (uint32_t i = 0; i < m_resource->num_images(); i++)
  149. {
  150. GuiImageData data = m_resource->get_image(i);
  151. ResourceId mat = data.material;
  152. Vector3 pos(data.position[0], data.position[1], 0);
  153. Vector2 size(data.size[0], data.size[1]);
  154. create_image(mat, pos, size);
  155. }
  156. // Manage texts creation
  157. /* FontResource* res = (FontResource*) device()->resource_manager()->lookup("font", "fonts/veramobi");
  158. create_text("ciaO mAngOZOide", res, 30, Vector3(300, 400, 0));*/
  159. }
  160. //-----------------------------------------------------------------------------
  161. Gui::~Gui()
  162. {
  163. for (uint32_t i = 0; i < m_rects.size(); i++)
  164. {
  165. CE_DELETE(m_rect_pool, m_rects[i]);
  166. }
  167. for (uint32_t i = 0; i < m_triangles.size(); i++)
  168. {
  169. CE_DELETE(m_triangle_pool, m_triangles[i]);
  170. }
  171. for (uint32_t i = 0; i < m_images.size(); i++)
  172. {
  173. CE_DELETE(m_image_pool, m_images[i]);
  174. }
  175. for (uint32_t i = 0; i < m_texts.size(); i++)
  176. {
  177. CE_DELETE(m_text_pool, m_texts[i]);
  178. }
  179. // FIXME FIXME FIXME -- Shaders destruction should not be here
  180. m_r.destroy_uniform(gui_albedo_0);
  181. m_r.destroy_gpu_program(gui_texture_program);
  182. m_r.destroy_gpu_program(gui_default_program);
  183. m_r.destroy_shader(gui_texture_fs);
  184. m_r.destroy_shader(gui_default_fs);
  185. m_r.destroy_shader(gui_default_vs);
  186. // FIXME FIXME FIXME -- Shaders destruction should not be here
  187. m_r.destroy_uniform(font_uniform);
  188. m_r.destroy_gpu_program(font_program);
  189. m_r.destroy_shader(font_vs);
  190. m_r.destroy_shader(font_fs);
  191. }
  192. //-----------------------------------------------------------------------------
  193. Vector2 Gui::resolution() const
  194. {
  195. return m_resource->gui_size();
  196. }
  197. //-----------------------------------------------------------------------------
  198. void Gui::move(const Vector3& pos)
  199. {
  200. m_pose.load_identity();
  201. m_pose.set_translation(pos);
  202. }
  203. //-----------------------------------------------------------------------------
  204. GuiRectId Gui::create_rect(const Vector3& pos, const Vector2& size, const Color4& color)
  205. {
  206. GuiRect* rect = CE_NEW(m_rect_pool, GuiRect)(m_r, pos, size, color);
  207. return m_rects.create(rect);
  208. }
  209. //-----------------------------------------------------------------------------
  210. void Gui::update_rect(GuiRectId id, const Vector3& pos, const Vector2& size, const Color4& color)
  211. {
  212. CE_ASSERT(m_rects.has(id), "GuiRect does not exists");
  213. GuiRect* rect = m_rects.lookup(id);
  214. rect->update(pos, size, color);
  215. }
  216. //-----------------------------------------------------------------------------
  217. void Gui::destroy_rect(GuiRectId id)
  218. {
  219. CE_ASSERT(m_rects.has(id), "GuiRect does not exist");
  220. GuiRect* rect = m_rects.lookup(id);
  221. CE_DELETE(m_rect_pool, rect);
  222. m_rects.destroy(id);
  223. }
  224. //-----------------------------------------------------------------------------
  225. GuiTriangleId Gui::create_triangle(const Vector2& p1, const Vector2& p2, const Vector2& p3, const Color4& color)
  226. {
  227. GuiTriangle* triangle = CE_NEW(m_triangle_pool, GuiTriangle)(m_r, p1, p2, p3, color);
  228. return m_triangles.create(triangle);
  229. }
  230. //-----------------------------------------------------------------------------
  231. void Gui::update_triangle(GuiTriangleId id, const Vector2& p1, const Vector2& p2, const Vector2& p3, const Color4& color)
  232. {
  233. CE_ASSERT(m_triangles.has(id), "GuiTriangle does not exists");
  234. GuiTriangle* triangle = m_triangles.lookup(id);
  235. triangle->update(p1, p2, p3, color);
  236. }
  237. //-----------------------------------------------------------------------------
  238. void Gui::destroy_triangle(GuiTriangleId id)
  239. {
  240. CE_ASSERT(m_triangles.has(id), "GuiTriangle does not exist");
  241. GuiTriangle* triangle = m_triangles.lookup(id);
  242. CE_DELETE(m_triangle_pool, triangle);
  243. m_triangles.destroy(id);
  244. }
  245. //-----------------------------------------------------------------------------
  246. GuiImageId Gui::create_image(ResourceId material, const Vector3& pos, const Vector2& size)
  247. {
  248. GuiImage* image = CE_NEW(m_image_pool, GuiImage)(m_render_world, m_r, material, pos, size);
  249. return m_images.create(image);
  250. }
  251. //-----------------------------------------------------------------------------
  252. void Gui::update_image(GuiImageId id, const Vector3& pos, const Vector2& size)
  253. {
  254. CE_ASSERT(m_images.has(id), "GuiImage does not exists");
  255. GuiImage* image = m_images.lookup(id);
  256. image->update(pos, size);
  257. }
  258. //-----------------------------------------------------------------------------
  259. void Gui::destroy_image(GuiImageId id)
  260. {
  261. CE_ASSERT(m_images.has(id), "GuiImage does not exists");
  262. GuiImage* image = m_images.lookup(id);
  263. CE_DELETE(m_image_pool, image);
  264. m_images.destroy(id);
  265. }
  266. //-----------------------------------------------------------------------------
  267. GuiTextId Gui::create_text(const char* str, const FontResource* font, uint32_t font_size, const Vector3& pos)
  268. {
  269. GuiText* text = CE_NEW(m_text_pool, GuiText)(m_render_world, m_r, str, font, font_size, pos);
  270. return m_texts.create(text);
  271. }
  272. //-----------------------------------------------------------------------------
  273. void Gui::update_text(GuiTextId id, const char* str, uint32_t font_size, const Vector3& pos)
  274. {
  275. CE_ASSERT(m_texts.has(id), "GuiText does not exists");
  276. GuiText* text = m_texts.lookup(id);
  277. text->update(str, font_size, pos);
  278. }
  279. //-----------------------------------------------------------------------------
  280. void Gui::destroy_text(GuiTextId id)
  281. {
  282. CE_ASSERT(m_texts.has(id), "GuiText does not exists");
  283. GuiText* text = m_texts.lookup(id);
  284. CE_DELETE(m_text_pool, text);
  285. m_texts.destroy(id);
  286. }
  287. //-----------------------------------------------------------------------------
  288. void Gui::render()
  289. {
  290. Vector2 size = m_resource->gui_size();
  291. m_r.set_layer_view(1, Matrix4x4::IDENTITY);
  292. m_r.set_layer_projection(1, m_projection);
  293. m_r.set_layer_viewport(1, m_pose.translation().x, m_pose.translation().y, size.x, size.y);
  294. // Render all Rects
  295. for (uint32_t i = 0; i < m_rects.size(); i++)
  296. {
  297. m_r.set_program(gui_default_program);
  298. m_r.set_state(STATE_DEPTH_WRITE
  299. | STATE_COLOR_WRITE
  300. | STATE_ALPHA_WRITE
  301. | STATE_CULL_CW
  302. | STATE_PRIMITIVE_LINES
  303. | STATE_BLEND_EQUATION_ADD
  304. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  305. m_r.set_pose(m_pose);
  306. m_rects[i]->render();
  307. }
  308. // Render all Triangles
  309. for (uint32_t i = 0; i < m_triangles.size(); i++)
  310. {
  311. m_r.set_program(gui_default_program);
  312. m_r.set_state(STATE_DEPTH_WRITE
  313. | STATE_COLOR_WRITE
  314. | STATE_ALPHA_WRITE
  315. | STATE_CULL_CW
  316. | STATE_PRIMITIVE_LINES
  317. | STATE_BLEND_EQUATION_ADD
  318. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  319. m_r.set_pose(m_pose);
  320. m_triangles[i]->render();
  321. }
  322. // Render all Images
  323. for (uint32_t i = 0; i < m_images.size(); i++)
  324. {
  325. m_r.set_program(gui_texture_program);
  326. m_r.set_state(STATE_DEPTH_WRITE
  327. | STATE_COLOR_WRITE
  328. | STATE_ALPHA_WRITE
  329. | STATE_CULL_CW
  330. | STATE_PRIMITIVE_TRIANGLES
  331. | STATE_BLEND_EQUATION_ADD
  332. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  333. m_r.set_pose(m_pose);
  334. m_images[i]->render(gui_albedo_0);
  335. }
  336. // Render all Texts
  337. for (uint32_t i = 0; i < m_texts.size(); i++)
  338. {
  339. m_r.set_program(font_program);
  340. m_r.set_state(STATE_DEPTH_WRITE
  341. | STATE_COLOR_WRITE
  342. | STATE_ALPHA_WRITE
  343. | STATE_CULL_CW
  344. | STATE_PRIMITIVE_TRIANGLES
  345. | STATE_BLEND_EQUATION_ADD
  346. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  347. m_r.set_pose(m_pose);
  348. m_texts[i]->render(font_uniform);
  349. }
  350. }
  351. } // namespace crown