sprite.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "sprite.h"
  6. #include "vector3.h"
  7. #include "quaternion.h"
  8. #include "sprite_resource.h"
  9. #include "allocator.h"
  10. #include "scene_graph.h"
  11. #include "unit.h"
  12. #include "material.h"
  13. #include "render_world.h"
  14. #include "device.h"
  15. #include "material_manager.h"
  16. namespace crown
  17. {
  18. Sprite::Sprite(RenderWorld& render_world, SceneGraph& sg, int32_t node, const SpriteResource* sr)
  19. : m_render_world(render_world)
  20. , m_scene_graph(sg)
  21. , m_node(node)
  22. , m_resource(sr)
  23. , m_frame(0)
  24. , _depth(0)
  25. {
  26. }
  27. Vector3 Sprite::local_position() const
  28. {
  29. return m_scene_graph.local_position(m_node);
  30. }
  31. Quaternion Sprite::local_rotation() const
  32. {
  33. return m_scene_graph.local_rotation(m_node);
  34. }
  35. Matrix4x4 Sprite::local_pose() const
  36. {
  37. return m_scene_graph.local_pose(m_node);
  38. }
  39. Vector3 Sprite::world_position() const
  40. {
  41. return m_scene_graph.world_position(m_node);
  42. }
  43. Quaternion Sprite::world_rotation() const
  44. {
  45. return m_scene_graph.world_rotation(m_node);
  46. }
  47. Matrix4x4 Sprite::world_pose() const
  48. {
  49. return m_scene_graph.world_pose(m_node);
  50. }
  51. void Sprite::set_local_position(Unit* unit, const Vector3& pos)
  52. {
  53. unit->set_local_position(m_node, pos);
  54. }
  55. void Sprite::set_local_rotation(Unit* unit, const Quaternion& rot)
  56. {
  57. unit->set_local_rotation(m_node, rot);
  58. }
  59. void Sprite::set_local_pose(Unit* unit, const Matrix4x4& pose)
  60. {
  61. unit->set_local_pose(m_node, pose);
  62. }
  63. void Sprite::set_material(MaterialId id)
  64. {
  65. m_material = id;
  66. }
  67. void Sprite::set_frame(uint32_t i)
  68. {
  69. m_frame = i;
  70. }
  71. void Sprite::set_depth(int32_t z)
  72. {
  73. _depth = z;
  74. }
  75. void Sprite::render()
  76. {
  77. if (m_material.id != INVALID_ID)
  78. material_manager::get()->lookup_material(m_material)->bind();
  79. bgfx::setState(BGFX_STATE_RGB_WRITE
  80. | BGFX_STATE_ALPHA_WRITE
  81. | BGFX_STATE_DEPTH_TEST_LEQUAL
  82. | BGFX_STATE_DEPTH_WRITE
  83. | BGFX_STATE_CULL_CW
  84. | BGFX_STATE_MSAA
  85. | BGFX_STATE_BLEND_ALPHA);
  86. bgfx::setVertexBuffer(m_resource->vb);
  87. bgfx::setIndexBuffer(m_resource->ib, m_frame * 6, 6);
  88. bgfx::setTransform(matrix4x4::to_float_ptr(world_pose()));
  89. bgfx::submit(0, _depth);
  90. }
  91. } // namespace crown