sprite.cpp 3.0 KB

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