sprite.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "sprite.h"
  24. #include "vector3.h"
  25. #include "quaternion.h"
  26. #include "sprite_resource.h"
  27. #include "allocator.h"
  28. #include "scene_graph.h"
  29. #include "unit.h"
  30. #include "material.h"
  31. #include "render_world.h"
  32. #include "device.h"
  33. #include "material_manager.h"
  34. namespace crown
  35. {
  36. //-----------------------------------------------------------------------------
  37. Sprite::Sprite(RenderWorld& render_world, SceneGraph& sg, int32_t node, const SpriteResource* sr)
  38. : m_render_world(render_world)
  39. , m_scene_graph(sg)
  40. , m_node(node)
  41. , m_resource(sr)
  42. , m_frame(0)
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. Vector3 Sprite::local_position() const
  47. {
  48. return m_scene_graph.local_position(m_node);
  49. }
  50. //-----------------------------------------------------------------------------
  51. Quaternion Sprite::local_rotation() const
  52. {
  53. return m_scene_graph.local_rotation(m_node);
  54. }
  55. //-----------------------------------------------------------------------------
  56. Matrix4x4 Sprite::local_pose() const
  57. {
  58. return m_scene_graph.local_pose(m_node);
  59. }
  60. //-----------------------------------------------------------------------------
  61. Vector3 Sprite::world_position() const
  62. {
  63. return m_scene_graph.world_position(m_node);
  64. }
  65. //-----------------------------------------------------------------------------
  66. Quaternion Sprite::world_rotation() const
  67. {
  68. return m_scene_graph.world_rotation(m_node);
  69. }
  70. //-----------------------------------------------------------------------------
  71. Matrix4x4 Sprite::world_pose() const
  72. {
  73. return m_scene_graph.world_pose(m_node);
  74. }
  75. //-----------------------------------------------------------------------------
  76. void Sprite::set_local_position(Unit* unit, const Vector3& pos)
  77. {
  78. unit->set_local_position(m_node, pos);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void Sprite::set_local_rotation(Unit* unit, const Quaternion& rot)
  82. {
  83. unit->set_local_rotation(m_node, rot);
  84. }
  85. //-----------------------------------------------------------------------------
  86. void Sprite::set_local_pose(Unit* unit, const Matrix4x4& pose)
  87. {
  88. unit->set_local_pose(m_node, pose);
  89. }
  90. void Sprite::set_material(MaterialId id)
  91. {
  92. m_material = id;
  93. }
  94. //-----------------------------------------------------------------------------
  95. void Sprite::set_frame(uint32_t i)
  96. {
  97. m_frame = i;
  98. }
  99. //-----------------------------------------------------------------------------
  100. void Sprite::render()
  101. {
  102. if (m_material.id != INVALID_ID)
  103. material_manager::get()->lookup_material(m_material)->bind();
  104. bgfx::setState(BGFX_STATE_RGB_WRITE
  105. | BGFX_STATE_ALPHA_WRITE
  106. | BGFX_STATE_DEPTH_TEST_LEQUAL
  107. | BGFX_STATE_DEPTH_WRITE
  108. | BGFX_STATE_CULL_CW
  109. | BGFX_STATE_MSAA
  110. | BGFX_STATE_BLEND_ALPHA);
  111. bgfx::setVertexBuffer(m_resource->vb);
  112. bgfx::setIndexBuffer(m_resource->ib, m_frame * 6, 6);
  113. bgfx::setTransform(matrix4x4::to_float_ptr(world_pose()));
  114. bgfx::submit(0);
  115. }
  116. } // namespace crown