UiSprite.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <iostream>
  16. #include <OgreHardwareBufferManager.h>
  17. #include <OgreMaterialManager.h>
  18. #include <OgreTechnique.h>
  19. #include "common/Logger.h"
  20. #include "core/UiSprite.h"
  21. UiSprite::UiSprite(const Ogre::String& name): UiWidget(name){Initialise();}
  22. UiSprite::UiSprite(const Ogre::String& name, const Ogre::String& path_name, UiWidget* parent):
  23. UiWidget(name, path_name, parent)
  24. {Initialise();}
  25. UiSprite::~UiSprite(){DestroyVertexBuffer();}
  26. void UiSprite::Initialise(){
  27. scene_manager_ = Ogre::Root::getSingleton().getSceneManager("Scene");
  28. render_system_ = Ogre::Root::getSingletonPtr()->getRenderSystem();
  29. material_ = Ogre::MaterialManager::getSingleton().create("UiMaterials." + path_name_, "IMAGES");
  30. Ogre::Pass* pass = material_->getTechnique(0)->getPass(0);
  31. pass->setVertexColourTracking(Ogre::TVC_AMBIENT);
  32. pass->setCullingMode(Ogre::CULL_NONE);
  33. pass->setDepthCheckEnabled(true);
  34. pass->setDepthWriteEnabled(true);
  35. pass->setLightingEnabled(false);
  36. pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
  37. pass->setAlphaRejectFunction(Ogre::CMPF_GREATER);
  38. pass->setAlphaRejectValue(0);
  39. Ogre::TextureUnitState* tex = pass->createTextureUnitState();
  40. tex->setTextureName("system/blank.png");
  41. tex->setNumMipmaps(-1);
  42. tex->setTextureFiltering(Ogre::TFO_NONE);
  43. CreateVertexBuffer();
  44. }
  45. void UiSprite::Update(){UiWidget::Update();}
  46. void UiSprite::Render(){
  47. if(visible_ == true){
  48. if(render_operation_.vertexData->vertexCount != 0){
  49. render_system_->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
  50. render_system_->_setProjectionMatrix(Ogre::Matrix4::IDENTITY);
  51. render_system_->_setViewMatrix(Ogre::Matrix4::IDENTITY);
  52. scene_manager_->_setPass(material_->getTechnique(0)->getPass(0), true, false);
  53. render_system_->setScissorTest(
  54. true, scissor_left_, scissor_top_, scissor_right_, scissor_bottom_
  55. );
  56. render_system_->_render(render_operation_);
  57. render_system_->setScissorTest(false);
  58. }
  59. }
  60. UiWidget::Render();
  61. }
  62. void UiSprite::UpdateTransformation(){
  63. UiWidget::UpdateTransformation();
  64. UpdateGeometry();
  65. }
  66. void UiSprite::SetImage(const Ogre::String& image){
  67. Ogre::Pass* pass = material_->getTechnique(0)->getPass(0);
  68. Ogre::TextureUnitState* tex = pass->getTextureUnitState(0);
  69. tex->setTextureName(image);
  70. }
  71. void UiSprite::SetImage(const char* image){SetImage(Ogre::String(image));}
  72. void UiSprite::SetImageRegion(const float u1, const float v1, const float u2, const float v2){
  73. image_region_[0] = u1;
  74. image_region_[1] = v1;
  75. image_region_[2] = u2;
  76. image_region_[3] = v2;
  77. UpdateGeometry();
  78. }
  79. void UiSprite::SetAdditive(const bool additive){
  80. Ogre::Pass* pass = material_->getTechnique(0)->getPass(0);
  81. pass->setSceneBlending(additive ? Ogre::SBT_ADD : Ogre::SBT_TRANSPARENT_ALPHA);
  82. }
  83. void UiSprite::SetVertexShader(const Ogre::String& shader){
  84. Ogre::Pass* pass = material_->getTechnique(0)->getPass(0);
  85. pass->setVertexProgram(shader, true);
  86. pass->getVertexProgram()->load();
  87. }
  88. void UiSprite::SetFragmentShader(const Ogre::String& shader){
  89. Ogre::Pass* pass = material_->getTechnique(0)->getPass(0);
  90. pass->setFragmentProgram(shader, true);
  91. pass->getFragmentProgram()->load();
  92. }
  93. void UiSprite::UpdateGeometry(){
  94. float local_x1 = -final_origin_.x;
  95. float local_y1 = -final_origin_.y;
  96. float local_x2 = final_size_.x + local_x1;
  97. float local_y2 = final_size_.y + local_y1;
  98. float x = final_translate_.x;
  99. float y = final_translate_.y;
  100. int x1, y1, x2, y2, x3, y3, x4, y4;
  101. if(final_rotation_ != 0){
  102. float cos = Ogre::Math::Cos(Ogre::Radian(Ogre::Degree(final_rotation_)));
  103. float sin = Ogre::Math::Sin(Ogre::Radian(Ogre::Degree(final_rotation_)));
  104. x1 = static_cast<int>(local_x1 * cos - local_y1 * sin + x);
  105. y1 = static_cast<int>(local_x1 * sin + local_y1 * cos + y);
  106. x2 = static_cast<int>(local_x2 * cos - local_y1 * sin + x);
  107. y2 = static_cast<int>(local_x2 * sin + local_y1 * cos + y);
  108. x3 = static_cast<int>(local_x2 * cos - local_y2 * sin + x);
  109. y3 = static_cast<int>(local_x2 * sin + local_y2 * cos + y);
  110. x4 = static_cast<int>(local_x1 * cos - local_y2 * sin + x);
  111. y4 = static_cast<int>(local_x1 * sin + local_y2 * cos + y);
  112. }
  113. else{
  114. x1 = static_cast<int>(local_x1 + x);
  115. y1 = static_cast<int>(local_y1 + y);
  116. x2 = static_cast<int>(local_x2 + x);
  117. y2 = static_cast<int>(local_y1 + y);
  118. x3 = static_cast<int>(local_x2 + x);
  119. y3 = static_cast<int>(local_y2 + y);
  120. x4 = static_cast<int>(local_x1 + x);
  121. y4 = static_cast<int>(local_y2 + y);
  122. }
  123. float new_x1 = (x1 / screen_width_) * 2 - 1;
  124. float new_y1 = -((y1 / screen_height_) * 2 - 1);
  125. float new_x2 = (x2 / screen_width_) * 2 - 1;
  126. float new_y2 = -((y2 / screen_height_) * 2 - 1);
  127. float new_x3 = (x3 / screen_width_) * 2 - 1;
  128. float new_y3 = -((y3 / screen_height_) * 2 - 1);
  129. float new_x4 = (x4 / screen_width_) * 2 - 1;
  130. float new_y4 = -((y4 / screen_height_) * 2 - 1);
  131. float* write_iterator
  132. = static_cast<float*>(vertex_buffer_->lock(Ogre::HardwareBuffer::HBL_NORMAL));
  133. *write_iterator ++ = new_x1;
  134. *write_iterator ++ = new_y1;
  135. *write_iterator ++ = final_z_;
  136. *write_iterator ++ = colour_1_.r;
  137. *write_iterator ++ = colour_1_.g;
  138. *write_iterator ++ = colour_1_.b;
  139. *write_iterator ++ = colour_1_.a;
  140. *write_iterator ++ = image_region_[0];
  141. *write_iterator ++ = image_region_[1];
  142. *write_iterator ++ = new_x2;
  143. *write_iterator ++ = new_y2;
  144. *write_iterator ++ = final_z_;
  145. *write_iterator ++ = colour_2_.r;
  146. *write_iterator ++ = colour_2_.g;
  147. *write_iterator ++ = colour_2_.b;
  148. *write_iterator ++ = colour_2_.a;
  149. *write_iterator ++ = image_region_[2];
  150. *write_iterator ++ = image_region_[1];
  151. *write_iterator ++ = new_x3;
  152. *write_iterator ++ = new_y3;
  153. *write_iterator ++ = final_z_;
  154. *write_iterator ++ = colour_3_.r;
  155. *write_iterator ++ = colour_3_.g;
  156. *write_iterator ++ = colour_3_.b;
  157. *write_iterator ++ = colour_3_.a;
  158. *write_iterator ++ = image_region_[2];
  159. *write_iterator ++ = image_region_[3];
  160. *write_iterator ++ = new_x1;
  161. *write_iterator ++ = new_y1;
  162. *write_iterator ++ = final_z_;
  163. *write_iterator ++ = colour_1_.r;
  164. *write_iterator ++ = colour_1_.g;
  165. *write_iterator ++ = colour_1_.b;
  166. *write_iterator ++ = colour_1_.a;
  167. *write_iterator ++ = image_region_[0];
  168. *write_iterator ++ = image_region_[1];
  169. *write_iterator ++ = new_x3;
  170. *write_iterator ++ = new_y3;
  171. *write_iterator ++ = final_z_;
  172. *write_iterator ++ = colour_3_.r;
  173. *write_iterator ++ = colour_3_.g;
  174. *write_iterator ++ = colour_3_.b;
  175. *write_iterator ++ = colour_3_.a;
  176. *write_iterator ++ = image_region_[2];
  177. *write_iterator ++ = image_region_[3];
  178. *write_iterator ++ = new_x4;
  179. *write_iterator ++ = new_y4;
  180. *write_iterator ++ = final_z_;
  181. *write_iterator ++ = colour_4_.r;
  182. *write_iterator ++ = colour_4_.g;
  183. *write_iterator ++ = colour_4_.b;
  184. *write_iterator ++ = colour_4_.a;
  185. *write_iterator ++ = image_region_[0];
  186. *write_iterator ++ = image_region_[3];
  187. render_operation_.vertexData->vertexCount = 6;
  188. vertex_buffer_->unlock();
  189. }
  190. void UiSprite::CreateVertexBuffer(){
  191. render_operation_.vertexData = new Ogre::VertexData;
  192. render_operation_.vertexData->vertexStart = 0;
  193. Ogre::VertexDeclaration* vertex_declaration = render_operation_.vertexData->vertexDeclaration;
  194. size_t offset = 0;
  195. vertex_declaration->addElement(0, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
  196. offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
  197. vertex_declaration->addElement(0, offset, Ogre::VET_FLOAT4, Ogre::VES_DIFFUSE);
  198. offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT4);
  199. vertex_declaration->addElement(0, offset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES);
  200. vertex_buffer_ = Ogre::HardwareBufferManager::getSingletonPtr()->createVertexBuffer(
  201. vertex_declaration->getVertexSize(0), 6, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, false
  202. );
  203. render_operation_.vertexData->vertexBufferBinding->setBinding(0, vertex_buffer_);
  204. render_operation_.operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST;
  205. render_operation_.useIndexes = false;
  206. }
  207. void UiSprite::DestroyVertexBuffer(){
  208. delete render_operation_.vertexData;
  209. render_operation_.vertexData = 0;
  210. vertex_buffer_.reset();
  211. }