Gui.cpp 13 KB

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