CTextSceneNode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "CTextSceneNode.h"
  5. #include "ISceneManager.h"
  6. #include "IVideoDriver.h"
  7. #include "ICameraSceneNode.h"
  8. #include "IGUISpriteBank.h"
  9. #include "SMeshBuffer.h"
  10. #include "os.h"
  11. namespace irr
  12. {
  13. namespace scene
  14. {
  15. //! constructor
  16. CTextSceneNode::CTextSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
  17. gui::IGUIFont* font, scene::ISceneCollisionManager* coll,
  18. const core::vector3df& position, const wchar_t* text,
  19. video::SColor color)
  20. : ITextSceneNode(parent, mgr, id, position), Text(text), Color(color),
  21. Font(font), Coll(coll)
  22. {
  23. #ifdef _DEBUG
  24. setDebugName("CTextSceneNode");
  25. #endif
  26. if (Font)
  27. Font->grab();
  28. setAutomaticCulling(scene::EAC_OFF);
  29. }
  30. //! destructor
  31. CTextSceneNode::~CTextSceneNode()
  32. {
  33. if (Font)
  34. Font->drop();
  35. }
  36. void CTextSceneNode::OnRegisterSceneNode()
  37. {
  38. if (IsVisible)
  39. SceneManager->registerNodeForRendering(this, ESNRP_TRANSPARENT);
  40. ISceneNode::OnRegisterSceneNode();
  41. }
  42. //! renders the node.
  43. void CTextSceneNode::render()
  44. {
  45. if (!Font || !Coll)
  46. return;
  47. core::position2d<s32> pos = Coll->getScreenCoordinatesFrom3DPosition(getAbsolutePosition(),
  48. SceneManager->getActiveCamera());
  49. core::rect<s32> r(pos, core::dimension2d<s32>(1,1));
  50. Font->draw(Text.c_str(), r, Color, true, true);
  51. }
  52. //! returns the axis aligned bounding box of this node
  53. const core::aabbox3d<f32>& CTextSceneNode::getBoundingBox() const
  54. {
  55. return Box;
  56. }
  57. //! sets the text string
  58. void CTextSceneNode::setText(const wchar_t* text)
  59. {
  60. Text = text;
  61. }
  62. //! sets the color of the text
  63. void CTextSceneNode::setTextColor(video::SColor color)
  64. {
  65. Color = color;
  66. }
  67. //!--------------------------------- CBillboardTextSceneNode ----------------------------------------------
  68. //! constructor
  69. CBillboardTextSceneNode::CBillboardTextSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
  70. gui::IGUIFont* font,const wchar_t* text,
  71. const core::vector3df& position, const core::dimension2d<f32>& size,
  72. video::SColor colorTop,video::SColor shade_bottom )
  73. : IBillboardTextSceneNode(parent, mgr, id, position),
  74. Font(0), ColorTop(colorTop), ColorBottom(shade_bottom), Mesh(0)
  75. {
  76. #ifdef _DEBUG
  77. setDebugName("CBillboardTextSceneNode");
  78. #endif
  79. Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  80. Material.MaterialTypeParam = 1.f / 255.f;
  81. Material.BackfaceCulling = false;
  82. Material.Lighting = false;
  83. Material.ZBuffer = video::ECFN_LESSEQUAL;
  84. Material.ZWriteEnable = false;
  85. if (font)
  86. {
  87. // doesn't support other font types
  88. if (font->getType() == gui::EGFT_BITMAP)
  89. {
  90. Font = (gui::IGUIFontBitmap*)font;
  91. Font->grab();
  92. // mesh with one buffer per texture
  93. Mesh = new SMesh();
  94. for (u32 i=0; i<Font->getSpriteBank()->getTextureCount(); ++i)
  95. {
  96. SMeshBuffer *mb = new SMeshBuffer();
  97. mb->Material = Material;
  98. mb->Material.setTexture(0, Font->getSpriteBank()->getTexture(i));
  99. Mesh->addMeshBuffer(mb);
  100. mb->drop();
  101. }
  102. }
  103. else
  104. {
  105. os::Printer::log("Sorry, CBillboardTextSceneNode does not support this font type", ELL_INFORMATION);
  106. }
  107. }
  108. setText(text);
  109. setSize(size);
  110. setAutomaticCulling ( scene::EAC_BOX );
  111. }
  112. CBillboardTextSceneNode::~CBillboardTextSceneNode()
  113. {
  114. if (Font)
  115. Font->drop();
  116. if (Mesh)
  117. Mesh->drop();
  118. }
  119. //! sets the text string
  120. void CBillboardTextSceneNode::setText(const wchar_t* text)
  121. {
  122. if ( !Mesh )
  123. return;
  124. Text = text;
  125. Symbol.clear();
  126. // clear mesh
  127. for (u32 j=0; j < Mesh->getMeshBufferCount(); ++j)
  128. {
  129. ((SMeshBuffer*)Mesh->getMeshBuffer(j))->Indices.clear();
  130. ((SMeshBuffer*)Mesh->getMeshBuffer(j))->Vertices.clear();
  131. }
  132. if (!Font)
  133. return;
  134. const core::array< core::rect<s32> > &sourceRects = Font->getSpriteBank()->getPositions();
  135. const core::array< gui::SGUISprite > &sprites = Font->getSpriteBank()->getSprites();
  136. f32 dim[2];
  137. f32 tex[4];
  138. u32 i;
  139. for ( i = 0; i != Text.size (); ++i )
  140. {
  141. SSymbolInfo info;
  142. u32 spriteno = Font->getSpriteNoFromChar( &text[i] );
  143. u32 rectno = sprites[spriteno].Frames[0].rectNumber;
  144. u32 texno = sprites[spriteno].Frames[0].textureNumber;
  145. dim[0] = core::reciprocal ( (f32) Font->getSpriteBank()->getTexture(texno)->getSize().Width );
  146. dim[1] = core::reciprocal ( (f32) Font->getSpriteBank()->getTexture(texno)->getSize().Height );
  147. const core::rect<s32>& s = sourceRects[rectno];
  148. // add space for letter to buffer
  149. SMeshBuffer* buf = (SMeshBuffer*)Mesh->getMeshBuffer(texno);
  150. u32 firstInd = buf->Indices.size();
  151. u32 firstVert = buf->Vertices.size();
  152. buf->Indices.set_used(firstInd + 6);
  153. buf->Vertices.set_used(firstVert + 4);
  154. tex[0] = (s.LowerRightCorner.X * dim[0]) + 0.5f*dim[0]; // half pixel
  155. tex[1] = (s.LowerRightCorner.Y * dim[1]) + 0.5f*dim[1];
  156. tex[2] = (s.UpperLeftCorner.Y * dim[1]) - 0.5f*dim[1];
  157. tex[3] = (s.UpperLeftCorner.X * dim[0]) - 0.5f*dim[0];
  158. buf->Vertices[firstVert+0].TCoords.set(tex[0], tex[1]);
  159. buf->Vertices[firstVert+1].TCoords.set(tex[0], tex[2]);
  160. buf->Vertices[firstVert+2].TCoords.set(tex[3], tex[2]);
  161. buf->Vertices[firstVert+3].TCoords.set(tex[3], tex[1]);
  162. buf->Vertices[firstVert+0].Color = ColorBottom;
  163. buf->Vertices[firstVert+3].Color = ColorBottom;
  164. buf->Vertices[firstVert+1].Color = ColorTop;
  165. buf->Vertices[firstVert+2].Color = ColorTop;
  166. buf->Indices[firstInd+0] = (u16)firstVert+0;
  167. buf->Indices[firstInd+1] = (u16)firstVert+2;
  168. buf->Indices[firstInd+2] = (u16)firstVert+1;
  169. buf->Indices[firstInd+3] = (u16)firstVert+0;
  170. buf->Indices[firstInd+4] = (u16)firstVert+3;
  171. buf->Indices[firstInd+5] = (u16)firstVert+2;
  172. wchar_t *tp = 0;
  173. if (i>0)
  174. tp = &Text[i-1];
  175. info.Width = (f32)s.getWidth();
  176. info.bufNo = texno;
  177. info.Kerning = (f32)Font->getKerningWidth(&Text[i], tp);
  178. info.firstInd = firstInd;
  179. info.firstVert = firstVert;
  180. Symbol.push_back(info);
  181. }
  182. }
  183. //! pre render event
  184. void CBillboardTextSceneNode::OnAnimate(u32 timeMs)
  185. {
  186. ISceneNode::OnAnimate(timeMs);
  187. if (!IsVisible || !Font || !Mesh)
  188. return;
  189. ICameraSceneNode* camera = SceneManager->getActiveCamera();
  190. if (!camera)
  191. return;
  192. // get text width
  193. f32 textLength = 0.f;
  194. u32 i;
  195. for(i=0; i!=Symbol.size(); ++i)
  196. {
  197. SSymbolInfo &info = Symbol[i];
  198. textLength += info.Kerning + info.Width;
  199. }
  200. if (textLength<0.0f)
  201. textLength=1.0f;
  202. //const core::matrix4 &m = camera->getViewFrustum()->Matrices[ video::ETS_VIEW ];
  203. // make billboard look to camera
  204. core::vector3df pos = getAbsolutePosition();
  205. core::vector3df campos = camera->getAbsolutePosition();
  206. core::vector3df target = camera->getTarget();
  207. core::vector3df up = camera->getUpVector();
  208. core::vector3df view = target - campos;
  209. view.normalize();
  210. core::vector3df horizontal = up.crossProduct(view);
  211. if ( horizontal.getLength() == 0 )
  212. {
  213. horizontal.set(up.Y,up.X,up.Z);
  214. }
  215. horizontal.normalize();
  216. core::vector3df space = horizontal;
  217. horizontal *= 0.5f * Size.Width;
  218. core::vector3df vertical = horizontal.crossProduct(view);
  219. vertical.normalize();
  220. vertical *= 0.5f * Size.Height;
  221. view *= -1.0f;
  222. // center text
  223. pos += space * (Size.Width * -0.5f);
  224. for ( i = 0; i!= Symbol.size(); ++i )
  225. {
  226. SSymbolInfo &info = Symbol[i];
  227. f32 infw = info.Width / textLength;
  228. f32 infk = info.Kerning / textLength;
  229. f32 w = (Size.Width * infw * 0.5f);
  230. pos += space * w;
  231. SMeshBuffer* buf = (SMeshBuffer*)Mesh->getMeshBuffer(info.bufNo);
  232. buf->Vertices[info.firstVert+0].Normal = view;
  233. buf->Vertices[info.firstVert+1].Normal = view;
  234. buf->Vertices[info.firstVert+2].Normal = view;
  235. buf->Vertices[info.firstVert+3].Normal = view;
  236. buf->Vertices[info.firstVert+0].Pos = pos + (space * w) + vertical;
  237. buf->Vertices[info.firstVert+1].Pos = pos + (space * w) - vertical;
  238. buf->Vertices[info.firstVert+2].Pos = pos - (space * w) - vertical;
  239. buf->Vertices[info.firstVert+3].Pos = pos - (space * w) + vertical;
  240. pos += space * (Size.Width*infk + w);
  241. }
  242. // make bounding box
  243. for (i=0; i< Mesh->getMeshBufferCount() ; ++i)
  244. Mesh->getMeshBuffer(i)->recalculateBoundingBox();
  245. Mesh->recalculateBoundingBox();
  246. BBox = Mesh->getBoundingBox();
  247. core::matrix4 mat( getAbsoluteTransformation(), core::matrix4::EM4CONST_INVERSE );
  248. mat.transformBoxEx(BBox);
  249. }
  250. void CBillboardTextSceneNode::OnRegisterSceneNode()
  251. {
  252. SceneManager->registerNodeForRendering(this, ESNRP_TRANSPARENT);
  253. ISceneNode::OnRegisterSceneNode();
  254. }
  255. //! render
  256. void CBillboardTextSceneNode::render()
  257. {
  258. if ( !Mesh )
  259. return;
  260. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  261. // draw
  262. core::matrix4 mat;
  263. driver->setTransform(video::ETS_WORLD, mat);
  264. for (u32 i = 0; i < Mesh->getMeshBufferCount(); ++i)
  265. {
  266. driver->setMaterial(Mesh->getMeshBuffer(i)->getMaterial());
  267. driver->drawMeshBuffer(Mesh->getMeshBuffer(i));
  268. }
  269. if ( DebugDataVisible & scene::EDS_BBOX )
  270. {
  271. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  272. video::SMaterial m;
  273. m.Lighting = false;
  274. driver->setMaterial(m);
  275. driver->draw3DBox(BBox, video::SColor(0,208,195,152));
  276. }
  277. }
  278. //! returns the axis aligned bounding box of this node
  279. const core::aabbox3d<f32>& CBillboardTextSceneNode::getBoundingBox() const
  280. {
  281. return BBox;
  282. }
  283. //! sets the size of the billboard
  284. void CBillboardTextSceneNode::setSize(const core::dimension2d<f32>& size)
  285. {
  286. Size = size;
  287. if (Size.Width == 0.0f)
  288. Size.Width = 1.0f;
  289. if (Size.Height == 0.0f )
  290. Size.Height = 1.0f;
  291. //f32 avg = (size.Width + size.Height)/6;
  292. //BBox.MinEdge.set(-avg,-avg,-avg);
  293. //BBox.MaxEdge.set(avg,avg,avg);
  294. }
  295. video::SMaterial& CBillboardTextSceneNode::getMaterial(u32 i)
  296. {
  297. if (Mesh && Mesh->getMeshBufferCount() > i )
  298. return Mesh->getMeshBuffer(i)->getMaterial();
  299. else
  300. return Material;
  301. }
  302. //! returns amount of materials used by this scene node.
  303. u32 CBillboardTextSceneNode::getMaterialCount() const
  304. {
  305. if (Mesh)
  306. return Mesh->getMeshBufferCount();
  307. else
  308. return 0;
  309. }
  310. //! gets the size of the billboard
  311. const core::dimension2d<f32>& CBillboardTextSceneNode::getSize() const
  312. {
  313. return Size;
  314. }
  315. //! sets the color of the text
  316. void CBillboardTextSceneNode::setTextColor(video::SColor color)
  317. {
  318. Color = color;
  319. }
  320. //! Set the color of all vertices of the billboard
  321. //! \param overallColor: the color to set
  322. void CBillboardTextSceneNode::setColor(const video::SColor & overallColor)
  323. {
  324. if ( !Mesh )
  325. return;
  326. for ( u32 i = 0; i != Text.size (); ++i )
  327. {
  328. const SSymbolInfo &info = Symbol[i];
  329. SMeshBuffer* buf = (SMeshBuffer*)Mesh->getMeshBuffer(info.bufNo);
  330. buf->Vertices[info.firstVert+0].Color = overallColor;
  331. buf->Vertices[info.firstVert+1].Color = overallColor;
  332. buf->Vertices[info.firstVert+2].Color = overallColor;
  333. buf->Vertices[info.firstVert+3].Color = overallColor;
  334. }
  335. }
  336. //! Set the color of the top and bottom vertices of the billboard
  337. //! \param topColor: the color to set the top vertices
  338. //! \param bottomColor: the color to set the bottom vertices
  339. void CBillboardTextSceneNode::setColor(const video::SColor & topColor, const video::SColor & bottomColor)
  340. {
  341. if ( !Mesh )
  342. return;
  343. ColorBottom = bottomColor;
  344. ColorTop = topColor;
  345. for ( u32 i = 0; i != Text.size (); ++i )
  346. {
  347. const SSymbolInfo &info = Symbol[i];
  348. SMeshBuffer* buf = (SMeshBuffer*)Mesh->getMeshBuffer(info.bufNo);
  349. buf->Vertices[info.firstVert+0].Color = ColorBottom;
  350. buf->Vertices[info.firstVert+3].Color = ColorBottom;
  351. buf->Vertices[info.firstVert+1].Color = ColorTop;
  352. buf->Vertices[info.firstVert+2].Color = ColorTop;
  353. }
  354. }
  355. //! Gets the color of the top and bottom vertices of the billboard
  356. //! \param topColor: stores the color of the top vertices
  357. //! \param bottomColor: stores the color of the bottom vertices
  358. void CBillboardTextSceneNode::getColor(video::SColor & topColor, video::SColor & bottomColor) const
  359. {
  360. topColor = ColorTop;
  361. bottomColor = ColorBottom;
  362. }
  363. } // end namespace scene
  364. } // end namespace irr