UiSprite.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include <OgreHardwareBufferManager.h>
  2. #include <OgreMaterialManager.h>
  3. #include "core/Logger.h"
  4. #include "core/UiSprite.h"
  5. UiSprite::UiSprite(const Ogre::String& name):
  6. UiWidget(name)
  7. {
  8. Initialise();
  9. }
  10. UiSprite::UiSprite(const Ogre::String& name, const Ogre::String& path_name, UiWidget* parent):
  11. UiWidget(name, path_name, parent)
  12. {
  13. Initialise();
  14. }
  15. UiSprite::~UiSprite()
  16. {
  17. DestroyVertexBuffer();
  18. }
  19. void
  20. UiSprite::Initialise()
  21. {
  22. m_SceneManager = Ogre::Root::getSingleton().getSceneManager("Scene");
  23. m_RenderSystem = Ogre::Root::getSingletonPtr()->getRenderSystem();
  24. m_Material = Ogre::MaterialManager::getSingleton().create("UiMaterials." + m_PathName, "General");
  25. Ogre::Pass* pass = m_Material->getTechnique(0)->getPass(0);
  26. pass->setVertexColourTracking(Ogre::TVC_AMBIENT);
  27. pass->setCullingMode(Ogre::CULL_NONE);
  28. pass->setDepthCheckEnabled(true);
  29. pass->setDepthWriteEnabled(true);
  30. pass->setLightingEnabled(false);
  31. pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
  32. pass->setAlphaRejectFunction(Ogre::CMPF_GREATER);
  33. pass->setAlphaRejectValue(0);
  34. Ogre::TextureUnitState* tex = pass->createTextureUnitState();
  35. tex->setTextureName("system/blank.png");
  36. tex->setNumMipmaps(-1);
  37. tex->setTextureFiltering(Ogre::TFO_NONE);
  38. CreateVertexBuffer();
  39. }
  40. void
  41. UiSprite::Update()
  42. {
  43. UiWidget::Update();
  44. }
  45. void
  46. UiSprite::Render()
  47. {
  48. if(m_Visible == true)
  49. {
  50. if(m_RenderOp.vertexData->vertexCount != 0)
  51. {
  52. m_RenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
  53. m_RenderSystem->_setProjectionMatrix(Ogre::Matrix4::IDENTITY);
  54. m_RenderSystem->_setViewMatrix(Ogre::Matrix4::IDENTITY);
  55. m_SceneManager->_setPass(m_Material->getTechnique(0)->getPass(0), true, false);
  56. m_RenderSystem->setScissorTest(true, m_ScissorLeft, m_ScissorTop, m_ScissorRight, m_ScissorBottom);
  57. m_RenderSystem->_render(m_RenderOp);
  58. m_RenderSystem->setScissorTest(false);
  59. }
  60. }
  61. UiWidget::Render();
  62. }
  63. void
  64. UiSprite::UpdateTransformation()
  65. {
  66. UiWidget::UpdateTransformation();
  67. UpdateGeometry();
  68. }
  69. void
  70. UiSprite::SetImage(const Ogre::String& image)
  71. {
  72. Ogre::Pass* pass = m_Material->getTechnique(0)->getPass(0);
  73. Ogre::TextureUnitState* tex = pass->getTextureUnitState(0);
  74. tex->setTextureName(image);
  75. }
  76. void
  77. UiSprite::SetVertexShader(const Ogre::String& shader)
  78. {
  79. Ogre::Pass* pass = m_Material->getTechnique(0)->getPass(0);
  80. pass->setVertexProgram(shader, true);
  81. pass->getVertexProgram()->load();
  82. }
  83. void
  84. UiSprite::SetFragmentShader(const Ogre::String& shader)
  85. {
  86. Ogre::Pass* pass = m_Material->getTechnique(0)->getPass(0);
  87. pass->setFragmentProgram(shader, true);
  88. pass->getFragmentProgram()->load();
  89. }
  90. void
  91. UiSprite::UpdateGeometry()
  92. {
  93. float local_x1 = -m_FinalOrigin.x;
  94. float local_y1 = -m_FinalOrigin.y;
  95. float local_x2 = m_FinalSize.x + local_x1;
  96. float local_y2 = m_FinalSize.y + local_y1;
  97. float x = m_FinalTranslate.x;
  98. float y = m_FinalTranslate.y;
  99. int x1, y1, x2, y2, x3, y3, x4, y4;
  100. //LOG_ERROR(m_Name + ", rotation = " + Ogre::StringConverter::toString(rotation));
  101. //LOG_ERROR("local_x1 = " + Ogre::StringConverter::toString(local_x1) + ", local_y1 = " + Ogre::StringConverter::toString(local_y1));
  102. //LOG_ERROR("local_x2 = " + Ogre::StringConverter::toString(local_x2) + ", local_y2 = " + Ogre::StringConverter::toString(local_y2));
  103. if(m_FinalRotation != 0)
  104. {
  105. float cos = Ogre::Math::Cos(Ogre::Radian(Ogre::Degree(m_FinalRotation)));
  106. float sin = Ogre::Math::Sin(Ogre::Radian(Ogre::Degree(m_FinalRotation)));
  107. x1 = static_cast<int>(local_x1 * cos - local_y1 * sin + x);
  108. y1 = static_cast<int>(local_x1 * sin + local_y1 * cos + y);
  109. x2 = static_cast<int>(local_x2 * cos - local_y1 * sin + x);
  110. y2 = static_cast<int>(local_x2 * sin + local_y1 * cos + y);
  111. x3 = static_cast<int>(local_x2 * cos - local_y2 * sin + x);
  112. y3 = static_cast<int>(local_x2 * sin + local_y2 * cos + y);
  113. x4 = static_cast<int>(local_x1 * cos - local_y2 * sin + x);
  114. y4 = static_cast<int>(local_x1 * sin + local_y2 * cos + y);
  115. }
  116. else
  117. {
  118. x1 = static_cast<int>(local_x1 + x);
  119. y1 = static_cast<int>(local_y1 + y);
  120. x2 = static_cast<int>(local_x2 + x);
  121. y2 = static_cast<int>(local_y1 + y);
  122. x3 = static_cast<int>(local_x2 + x);
  123. y3 = static_cast<int>(local_y2 + y);
  124. x4 = static_cast<int>(local_x1 + x);
  125. y4 = static_cast<int>(local_y2 + y);
  126. }
  127. //LOG_ERROR("x1 = " + Ogre::StringConverter::toString(x1) + ", y1 = " + Ogre::StringConverter::toString(y1));
  128. //LOG_ERROR("x2 = " + Ogre::StringConverter::toString(x2) + ", y2 = " + Ogre::StringConverter::toString(y2));
  129. //LOG_ERROR("x3 = " + Ogre::StringConverter::toString(x3) + ", y3 = " + Ogre::StringConverter::toString(y3));
  130. //LOG_ERROR("x4 = " + Ogre::StringConverter::toString(x4) + ", y4 = " + Ogre::StringConverter::toString(y4));
  131. float new_x1 = (x1 / m_ScreenWidth) * 2 - 1;
  132. float new_y1 = -((y1 / m_ScreenHeight) * 2 - 1);
  133. float new_x2 = (x2 / m_ScreenWidth) * 2 - 1;
  134. float new_y2 = -((y2 / m_ScreenHeight) * 2 - 1);
  135. float new_x3 = (x3 / m_ScreenWidth) * 2 - 1;
  136. float new_y3 = -((y3 / m_ScreenHeight) * 2 - 1);
  137. float new_x4 = (x4 / m_ScreenWidth) * 2 - 1;
  138. float new_y4 = -((y4 / m_ScreenHeight) * 2 - 1);
  139. float* writeIterator = (float*) m_VertexBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL);
  140. // TODO: use WriteGlyph (should probably rename and move to utils)
  141. *writeIterator++ = new_x1;
  142. *writeIterator++ = new_y1;
  143. *writeIterator++ = m_FinalZ;
  144. *writeIterator++ = m_Colour1.r;
  145. *writeIterator++ = m_Colour1.g;
  146. *writeIterator++ = m_Colour1.b;
  147. *writeIterator++ = m_Colour1.a;
  148. *writeIterator++ = 0;
  149. *writeIterator++ = 0;
  150. *writeIterator++ = new_x2;
  151. *writeIterator++ = new_y2;
  152. *writeIterator++ = m_FinalZ;
  153. *writeIterator++ = m_Colour2.r;
  154. *writeIterator++ = m_Colour2.g;
  155. *writeIterator++ = m_Colour2.b;
  156. *writeIterator++ = m_Colour2.a;
  157. *writeIterator++ = 1;
  158. *writeIterator++ = 0;
  159. *writeIterator++ = new_x3;
  160. *writeIterator++ = new_y3;
  161. *writeIterator++ = m_FinalZ;
  162. *writeIterator++ = m_Colour3.r;
  163. *writeIterator++ = m_Colour3.g;
  164. *writeIterator++ = m_Colour3.b;
  165. *writeIterator++ = m_Colour3.a;
  166. *writeIterator++ = 1;
  167. *writeIterator++ = 1;
  168. *writeIterator++ = new_x1;
  169. *writeIterator++ = new_y1;
  170. *writeIterator++ = m_FinalZ;
  171. *writeIterator++ = m_Colour1.r;
  172. *writeIterator++ = m_Colour1.g;
  173. *writeIterator++ = m_Colour1.b;
  174. *writeIterator++ = m_Colour1.a;
  175. *writeIterator++ = 0;
  176. *writeIterator++ = 0;
  177. *writeIterator++ = new_x3;
  178. *writeIterator++ = new_y3;
  179. *writeIterator++ = m_FinalZ;
  180. *writeIterator++ = m_Colour3.r;
  181. *writeIterator++ = m_Colour3.g;
  182. *writeIterator++ = m_Colour3.b;
  183. *writeIterator++ = m_Colour3.a;
  184. *writeIterator++ = 1;
  185. *writeIterator++ = 1;
  186. *writeIterator++ = new_x4;
  187. *writeIterator++ = new_y4;
  188. *writeIterator++ = m_FinalZ;
  189. *writeIterator++ = m_Colour4.r;
  190. *writeIterator++ = m_Colour4.g;
  191. *writeIterator++ = m_Colour4.b;
  192. *writeIterator++ = m_Colour4.a;
  193. *writeIterator++ = 0;
  194. *writeIterator++ = 1;
  195. m_RenderOp.vertexData->vertexCount = 6;
  196. m_VertexBuffer->unlock();
  197. }
  198. void
  199. UiSprite::CreateVertexBuffer()
  200. {
  201. m_RenderOp.vertexData = new Ogre::VertexData;
  202. m_RenderOp.vertexData->vertexStart = 0;
  203. Ogre::VertexDeclaration* vDecl = m_RenderOp.vertexData->vertexDeclaration;
  204. size_t offset = 0;
  205. vDecl->addElement(0, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
  206. offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
  207. vDecl->addElement(0, offset, Ogre::VET_FLOAT4, Ogre::VES_DIFFUSE);
  208. offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT4);
  209. vDecl->addElement(0, offset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES);
  210. m_VertexBuffer = Ogre::HardwareBufferManager::getSingletonPtr()->createVertexBuffer(vDecl->getVertexSize(0), 6, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, false);
  211. m_RenderOp.vertexData->vertexBufferBinding->setBinding(0, m_VertexBuffer);
  212. m_RenderOp.operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST;
  213. m_RenderOp.useIndexes = false;
  214. }
  215. void
  216. UiSprite::DestroyVertexBuffer()
  217. {
  218. delete m_RenderOp.vertexData;
  219. m_RenderOp.vertexData = 0;
  220. m_VertexBuffer.setNull();
  221. }