CGUIMeshViewer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CGUIMeshViewer.h"
  5. #ifdef _IRR_COMPILE_WITH_GUI_
  6. #include "IGUIEnvironment.h"
  7. #include "IVideoDriver.h"
  8. #include "IAnimatedMesh.h"
  9. #include "IMesh.h"
  10. #include "os.h"
  11. #include "IGUISkin.h"
  12. namespace irr
  13. {
  14. namespace gui
  15. {
  16. //! constructor
  17. CGUIMeshViewer::CGUIMeshViewer(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
  18. : IGUIMeshViewer(environment, parent, id, rectangle), Mesh(0)
  19. {
  20. #ifdef _DEBUG
  21. setDebugName("CGUIMeshViewer");
  22. #endif
  23. }
  24. //! destructor
  25. CGUIMeshViewer::~CGUIMeshViewer()
  26. {
  27. if (Mesh)
  28. Mesh->drop();
  29. }
  30. //! sets the mesh to be shown
  31. void CGUIMeshViewer::setMesh(scene::IAnimatedMesh* mesh)
  32. {
  33. if (mesh)
  34. mesh->grab();
  35. if (Mesh)
  36. Mesh->drop();
  37. Mesh = mesh;
  38. /* This might be used for proper transformation etc.
  39. core::vector3df center(0.0f,0.0f,0.0f);
  40. core::aabbox3d<f32> box;
  41. box = Mesh->getMesh(0)->getBoundingBox();
  42. center = (box.MaxEdge + box.MinEdge) / 2;
  43. */
  44. }
  45. //! Gets the displayed mesh
  46. scene::IAnimatedMesh* CGUIMeshViewer::getMesh() const
  47. {
  48. return Mesh;
  49. }
  50. //! sets the material
  51. void CGUIMeshViewer::setMaterial(const video::SMaterial& material)
  52. {
  53. Material = material;
  54. }
  55. //! gets the material
  56. const video::SMaterial& CGUIMeshViewer::getMaterial() const
  57. {
  58. return Material;
  59. }
  60. //! called if an event happened.
  61. bool CGUIMeshViewer::OnEvent(const SEvent& event)
  62. {
  63. return IGUIElement::OnEvent(event);
  64. }
  65. //! draws the element and its children
  66. void CGUIMeshViewer::draw()
  67. {
  68. if (!IsVisible)
  69. return;
  70. IGUISkin* skin = Environment->getSkin();
  71. video::IVideoDriver* driver = Environment->getVideoDriver();
  72. core::rect<s32> viewPort = AbsoluteRect;
  73. viewPort.LowerRightCorner.X -= 1;
  74. viewPort.LowerRightCorner.Y -= 1;
  75. viewPort.UpperLeftCorner.X += 1;
  76. viewPort.UpperLeftCorner.Y += 1;
  77. viewPort.clipAgainst(AbsoluteClippingRect);
  78. // draw the frame
  79. core::rect<s32> frameRect(AbsoluteRect);
  80. frameRect.LowerRightCorner.Y = frameRect.UpperLeftCorner.Y + 1;
  81. skin->draw2DRectangle(this, skin->getColor(EGDC_3D_SHADOW), frameRect, &AbsoluteClippingRect);
  82. frameRect.LowerRightCorner.Y = AbsoluteRect.LowerRightCorner.Y;
  83. frameRect.LowerRightCorner.X = frameRect.UpperLeftCorner.X + 1;
  84. skin->draw2DRectangle(this, skin->getColor(EGDC_3D_SHADOW), frameRect, &AbsoluteClippingRect);
  85. frameRect = AbsoluteRect;
  86. frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X - 1;
  87. skin->draw2DRectangle(this, skin->getColor(EGDC_3D_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
  88. frameRect = AbsoluteRect;
  89. frameRect.UpperLeftCorner.Y = AbsoluteRect.LowerRightCorner.Y - 1;
  90. skin->draw2DRectangle(this, skin->getColor(EGDC_3D_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
  91. // draw the mesh
  92. if (Mesh)
  93. {
  94. //TODO: if outside of screen, dont draw.
  95. // - why is the absolute clipping rect not already the screen?
  96. core::rect<s32> oldViewPort = driver->getViewPort();
  97. driver->setViewPort(viewPort);
  98. core::matrix4 mat;
  99. //CameraControl->calculateProjectionMatrix(mat);
  100. //driver->setTransform(video::TS_PROJECTION, mat);
  101. mat.makeIdentity();
  102. mat.setTranslation(core::vector3df(0,0,0));
  103. driver->setTransform(video::ETS_WORLD, mat);
  104. //CameraControl->calculateViewMatrix(mat);
  105. //driver->setTransform(video::TS_VIEW, mat);
  106. driver->setMaterial(Material);
  107. u32 frame = 0;
  108. if(Mesh->getFrameCount())
  109. frame = (os::Timer::getTime()/20)%Mesh->getFrameCount();
  110. const scene::IMesh* const m = Mesh->getMesh(frame);
  111. for (u32 i=0; i<m->getMeshBufferCount(); ++i)
  112. {
  113. scene::IMeshBuffer* mb = m->getMeshBuffer(i);
  114. driver->drawVertexPrimitiveList(mb->getVertices(),
  115. mb->getVertexCount(), mb->getIndices(),
  116. mb->getIndexCount()/ 3, mb->getVertexType(),
  117. scene::EPT_TRIANGLES, mb->getIndexType());
  118. }
  119. driver->setViewPort(oldViewPort);
  120. }
  121. IGUIElement::draw();
  122. }
  123. } // end namespace gui
  124. } // end namespace irr
  125. #endif // _IRR_COMPILE_WITH_GUI_