Manual_Graphics.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../AngelScript/APITemplates.h"
  24. #include "../AngelScript/Manual_Graphics.h"
  25. #include "../Scene/Scene.h"
  26. #include "../Graphics/DebugRenderer.h"
  27. namespace Urho3D
  28. {
  29. // This function is called before ASRegisterGenerated()
  30. void ASRegisterManualFirst_Graphics(asIScriptEngine* engine)
  31. {
  32. // enum TextureUnit | File: ../Graphics/GraphicsDefs.h
  33. engine->RegisterEnum("TextureUnit");
  34. engine->RegisterEnumValue("TextureUnit", "TU_DIFFUSE", TU_DIFFUSE);
  35. engine->RegisterEnumValue("TextureUnit", "TU_ALBEDOBUFFER", TU_ALBEDOBUFFER);
  36. engine->RegisterEnumValue("TextureUnit", "TU_NORMAL", TU_NORMAL);
  37. engine->RegisterEnumValue("TextureUnit", "TU_NORMALBUFFER", TU_NORMALBUFFER);
  38. engine->RegisterEnumValue("TextureUnit", "TU_SPECULAR", TU_SPECULAR);
  39. engine->RegisterEnumValue("TextureUnit", "TU_EMISSIVE", TU_EMISSIVE);
  40. engine->RegisterEnumValue("TextureUnit", "TU_ENVIRONMENT", TU_ENVIRONMENT);
  41. engine->RegisterEnumValue("TextureUnit", "TU_LIGHTRAMP", TU_LIGHTRAMP);
  42. engine->RegisterEnumValue("TextureUnit", "TU_LIGHTSHAPE", TU_LIGHTSHAPE);
  43. engine->RegisterEnumValue("TextureUnit", "TU_SHADOWMAP", TU_SHADOWMAP);
  44. #ifdef DESKTOP_GRAPHICS
  45. engine->RegisterEnumValue("TextureUnit", "TU_VOLUMEMAP", TU_VOLUMEMAP);
  46. engine->RegisterEnumValue("TextureUnit", "TU_CUSTOM1", TU_CUSTOM1);
  47. engine->RegisterEnumValue("TextureUnit", "TU_CUSTOM2", TU_CUSTOM2);
  48. engine->RegisterEnumValue("TextureUnit", "TU_FACESELECT", TU_FACESELECT);
  49. engine->RegisterEnumValue("TextureUnit", "TU_INDIRECTION", TU_INDIRECTION);
  50. engine->RegisterEnumValue("TextureUnit", "TU_DEPTHBUFFER", TU_DEPTHBUFFER);
  51. engine->RegisterEnumValue("TextureUnit", "TU_LIGHTBUFFER", TU_LIGHTBUFFER);
  52. engine->RegisterEnumValue("TextureUnit", "TU_ZONE", TU_ZONE);
  53. #endif
  54. engine->RegisterEnumValue("TextureUnit", "MAX_MATERIAL_TEXTURE_UNITS", MAX_MATERIAL_TEXTURE_UNITS);
  55. engine->RegisterEnumValue("TextureUnit", "MAX_TEXTURE_UNITS", MAX_TEXTURE_UNITS);
  56. }
  57. // ========================================================================================
  58. static void StaticModelSetModel(Model* model, StaticModel* ptr)
  59. {
  60. // Check type here to allow operating on both AnimatedModel and StaticModel without calling the wrong function,
  61. // as AnimatedModel can be cast to StaticModel
  62. if (ptr->GetType() == AnimatedModel::GetTypeStatic())
  63. static_cast<AnimatedModel*>(ptr)->SetModel(model);
  64. else
  65. ptr->SetModel(model);
  66. }
  67. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  68. static Graphics* GetGraphics()
  69. {
  70. return GetScriptContext()->GetSubsystem<Graphics>();
  71. }
  72. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  73. static Renderer* GetRenderer()
  74. {
  75. return GetScriptContext()->GetSubsystem<Renderer>();
  76. }
  77. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  78. static DebugRenderer* GetDebugRenderer()
  79. {
  80. Scene* scene = GetScriptContextScene();
  81. if (scene)
  82. return scene->GetComponent<DebugRenderer>();
  83. else
  84. return nullptr;
  85. }
  86. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  87. static Octree* GetOctree()
  88. {
  89. Scene* scene = GetScriptContextScene();
  90. return scene ? scene->GetComponent<Octree>() : nullptr;
  91. }
  92. // This function is called after ASRegisterGenerated()
  93. void ASRegisterManualLast_Graphics(asIScriptEngine* engine)
  94. {
  95. engine->RegisterObjectMethod("StaticModel", "void SetModel(Model@+)", asFUNCTION(StaticModelSetModel), asCALL_CDECL_OBJLAST);
  96. engine->RegisterObjectMethod("StaticModel", "void set_model(Model@+)", asFUNCTION(StaticModelSetModel), asCALL_CDECL_OBJLAST);
  97. engine->RegisterObjectMethod("Skybox", "void SetModel(Model@+)", asFUNCTION(StaticModelSetModel), asCALL_CDECL_OBJLAST);
  98. engine->RegisterObjectMethod("Skybox", "void set_model(Model@+)", asFUNCTION(StaticModelSetModel), asCALL_CDECL_OBJLAST);
  99. engine->RegisterObjectMethod("StaticModelGroup", "void SetModel(Model@+)", asFUNCTION(StaticModelSetModel), asCALL_CDECL_OBJLAST);
  100. engine->RegisterObjectMethod("StaticModelGroup", "void set_model(Model@+)", asFUNCTION(StaticModelSetModel), asCALL_CDECL_OBJLAST);
  101. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  102. engine->RegisterGlobalFunction("Graphics@+ get_graphics()", asFUNCTION(GetGraphics), asCALL_CDECL);
  103. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  104. engine->RegisterGlobalFunction("Renderer@+ get_renderer()", asFUNCTION(GetRenderer), asCALL_CDECL);
  105. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  106. engine->RegisterGlobalFunction("DebugRenderer@+ get_debugRenderer()", asFUNCTION(GetDebugRenderer), asCALL_CDECL);
  107. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  108. engine->RegisterGlobalFunction("Octree@+ get_octree()", asFUNCTION(GetOctree), asCALL_CDECL);
  109. }
  110. // ========================================================================================
  111. // Vector<RenderTargetInfo> RenderPath::renderTargets_ | File: ../Graphics/RenderPath.h
  112. RenderTargetInfo* RenderPathGetRenderTarget(unsigned index, RenderPath* ptr)
  113. {
  114. if (index >= ptr->renderTargets_.Size())
  115. {
  116. asIScriptContext* context = asGetActiveContext();
  117. if (context)
  118. context->SetException("Index out of bounds");
  119. return nullptr;
  120. }
  121. else
  122. return &ptr->renderTargets_[index];
  123. }
  124. // Vector<RenderPathCommand> RenderPath::commands_ | File: ../Graphics/RenderPath.h
  125. RenderPathCommand* RenderPathGetCommand(unsigned index, RenderPath* ptr)
  126. {
  127. if (index >= ptr->commands_.Size())
  128. {
  129. asIScriptContext* context = asGetActiveContext();
  130. if (context)
  131. context->SetException("Index out of bounds");
  132. return nullptr;
  133. }
  134. else
  135. return &ptr->commands_[index];
  136. }
  137. // ========================================================================================
  138. // SharedPtr<Technique> TechniqueEntry::technique_ | File: ../Graphics/Material.h
  139. void TechniqueEntrySetTechnique(Technique* technique, TechniqueEntry* ptr)
  140. {
  141. ptr->technique_ = technique;
  142. }
  143. // SharedPtr<Technique> TechniqueEntry::technique_ | File: ../Graphics/Material.h
  144. Technique* TechniqueEntryGetTechnique(TechniqueEntry* ptr)
  145. {
  146. return ptr->technique_;
  147. }
  148. // ========================================================================================
  149. static TechniqueEntry noTechniqueEntry;
  150. // const TechniqueEntry& Material::GetTechniqueEntry(unsigned index) const | File: ../Graphics/Material.h
  151. const TechniqueEntry& MaterialGetTechniqueEntry(unsigned index, Material* ptr)
  152. {
  153. if (index >= ptr->GetNumTechniques())
  154. {
  155. asGetActiveContext()->SetException("Index out of bounds");
  156. return noTechniqueEntry;
  157. }
  158. return ptr->GetTechniqueEntry(index);
  159. }
  160. CScriptArray* MaterialGetShaderParameterNames(Material* material)
  161. {
  162. Vector<String> result;
  163. const HashMap<StringHash, MaterialShaderParameter>& parameters = material->GetShaderParameters();
  164. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  165. result.Push(i->second_.name_);
  166. Sort(result.Begin(), result.End());
  167. return VectorToArray<String>(result, "Array<String>");
  168. }
  169. // ========================================================================================
  170. // bool VertexBuffer::SetData(const void *data) | File: ../Graphics/VertexBuffer.h
  171. bool VertexBufferSetData(VectorBuffer& src, VertexBuffer* ptr)
  172. {
  173. // Make sure there is enough data
  174. if (ptr->GetVertexCount() && src.GetSize() >= ptr->GetVertexCount() * ptr->GetVertexSize())
  175. return ptr->SetData(&src.GetBuffer()[0]);
  176. else
  177. return false;
  178. }
  179. // bool VertexBuffer::SetDataRange(const void *data, unsigned start, unsigned count, bool discard=false) | File: ../Graphics/VertexBuffer.h
  180. bool VertexBufferSetDataRange(VectorBuffer& src, unsigned start, unsigned count, bool discard, VertexBuffer* ptr)
  181. {
  182. // Make sure there is enough data
  183. if (ptr->GetVertexCount() && src.GetSize() >= count * ptr->GetVertexSize())
  184. return ptr->SetDataRange(&src.GetBuffer()[0], start, count, discard);
  185. else
  186. return false;
  187. }
  188. VectorBuffer VertexBufferGetData(VertexBuffer* ptr)
  189. {
  190. VectorBuffer ret;
  191. void* data = ptr->Lock(0, ptr->GetVertexCount(), false);
  192. if (data)
  193. {
  194. ret.Write(data, ptr->GetVertexCount() * ptr->GetVertexSize());
  195. ret.Seek(0);
  196. ptr->Unlock();
  197. }
  198. return ret;
  199. }
  200. // ========================================================================================
  201. // bool IndexBuffer::SetData(const void *data) | File: ../Graphics/IndexBuffer.h
  202. bool IndexBufferSetData(VectorBuffer& src, IndexBuffer* ptr)
  203. {
  204. // Make sure there is enough data
  205. if (ptr->GetIndexCount() && src.GetSize() >= ptr->GetIndexCount() * ptr->GetIndexSize())
  206. return ptr->SetData(&src.GetBuffer()[0]);
  207. else
  208. return false;
  209. }
  210. // bool IndexBuffer::SetDataRange(const void *data, unsigned start, unsigned count, bool discard=false) | File: ../Graphics/IndexBuffer.h
  211. bool IndexBufferSetDataRange(VectorBuffer& src, unsigned start, unsigned count, bool discard, IndexBuffer* ptr)
  212. {
  213. // Make sure there is enough data
  214. if (ptr->GetIndexCount() && src.GetSize() >= count * ptr->GetIndexSize())
  215. return ptr->SetDataRange(&src.GetBuffer()[0], start, count, discard);
  216. else
  217. return false;
  218. }
  219. VectorBuffer IndexBufferGetData(IndexBuffer* ptr)
  220. {
  221. VectorBuffer ret;
  222. void* data = ptr->Lock(0, ptr->GetIndexCount(), false);
  223. if (data)
  224. {
  225. ret.Write(data, ptr->GetIndexCount() * ptr->GetIndexSize());
  226. ret.Seek(0);
  227. ptr->Unlock();
  228. }
  229. return ret;
  230. }
  231. // ========================================================================================
  232. // AnimationTriggerPoint* Animation::GetTrigger(unsigned index) | File: ../Graphics/Animation.h
  233. AnimationTriggerPoint* AnimationGetTrigger(unsigned index, Animation* ptr)
  234. {
  235. if (index >= ptr->GetNumTriggers())
  236. {
  237. asIScriptContext* context = asGetActiveContext();
  238. if (context)
  239. context->SetException("Index out of bounds");
  240. return nullptr;
  241. }
  242. else
  243. return ptr->GetTrigger(index);
  244. }
  245. // ========================================================================================
  246. // void AnimationState::SetBoneWeight(const String &name, float weight, bool recursive=false) | File: ../Graphics/AnimationState.h
  247. void AnimationStateSetBoneWeight(const String& name, float weight, AnimationState* ptr)
  248. {
  249. ptr->SetBoneWeight(name, weight);
  250. }
  251. // ========================================================================================
  252. // void AnimatedModel::SetModel(Model *model, bool createBones=true) | File: ../Graphics/AnimatedModel.h
  253. void AnimatedModelSetModel(Model* model, AnimatedModel* ptr)
  254. {
  255. ptr->SetModel(model);
  256. }
  257. const String& AnimatedModelGetMorphName(unsigned index, AnimatedModel* ptr)
  258. {
  259. const Vector<ModelMorph>& morphs = ptr->GetMorphs();
  260. return index < morphs.Size() ? morphs[index].name_ : String::EMPTY;
  261. }
  262. // ========================================================================================
  263. // const Vector<AnimationControl>& AnimationController::GetAnimations() const | File: ../Graphics/AnimationController.h
  264. unsigned AnimationControllerGetNumAnimations(AnimationController* controller)
  265. {
  266. return controller->GetAnimations().Size();
  267. }
  268. // const Vector<AnimationControl>& AnimationController::GetAnimations() const | File: ../Graphics/AnimationController.h
  269. const AnimationControl* AnimationControllerGetAnimation(unsigned index, AnimationController* controller)
  270. {
  271. const Vector<AnimationControl>& animations = controller->GetAnimations();
  272. return (index < animations.Size()) ? &animations[index] : nullptr;
  273. }
  274. // ========================================================================================
  275. // void Graphics::PrecacheShaders(Deserializer &source) | File: ../Graphics/Graphics.h
  276. void GraphicsPrecacheShaders(File* file, Graphics* ptr)
  277. {
  278. if (file)
  279. ptr->PrecacheShaders(*file);
  280. }
  281. // void Graphics::PrecacheShaders(Deserializer &source) | File: ../Graphics/Graphics.h
  282. void GraphicsPrecacheShadersVectorBuffer(VectorBuffer& buffer, Graphics* ptr)
  283. {
  284. ptr->PrecacheShaders(buffer);
  285. }
  286. // ========================================================================================
  287. // Drawable* RayQueryResult::drawable_ | File: ../Graphics/OctreeQuery.h
  288. Drawable* RayQueryResultGetDrawable(RayQueryResult* ptr)
  289. {
  290. return ptr->drawable_;
  291. }
  292. // Node* RayQueryResult::node_ | File: ../Graphics/OctreeQuery.h
  293. Node* RayQueryResultGetNode(RayQueryResult* ptr)
  294. {
  295. return ptr->node_;
  296. }
  297. // ========================================================================================
  298. // void Octree::Raycast(RayOctreeQuery &query) const | File: ../Graphics/Octree.h
  299. CScriptArray* OctreeRaycast(const Ray& ray, RayQueryLevel level, float maxDistance, unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  300. {
  301. PODVector<RayQueryResult> result;
  302. RayOctreeQuery query(result, ray, level, maxDistance, drawableFlags, viewMask);
  303. ptr->Raycast(query);
  304. return VectorToArray<RayQueryResult>(result, "Array<RayQueryResult>");
  305. }
  306. // void Octree::RaycastSingle(RayOctreeQuery &query) const | File: ../Graphics/Octree.h
  307. RayQueryResult OctreeRaycastSingle(const Ray& ray, RayQueryLevel level, float maxDistance, unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  308. {
  309. PODVector<RayQueryResult> result;
  310. RayOctreeQuery query(result, ray, level, maxDistance, drawableFlags, viewMask);
  311. ptr->RaycastSingle(query);
  312. if (!query.result_.Empty())
  313. return query.result_[0];
  314. else
  315. {
  316. RayQueryResult empty;
  317. empty.position_ = Vector3::ZERO;
  318. empty.normal_ = Vector3::ZERO;
  319. empty.distance_ = M_INFINITY;
  320. empty.subObject_ = 0;
  321. return empty;
  322. }
  323. }
  324. // void Octree::GetDrawables(OctreeQuery &query) const | File: ../Graphics/Octree.h
  325. CScriptArray* OctreeGetDrawablesPoint(const Vector3& point, unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  326. {
  327. PODVector<Drawable*> result;
  328. PointOctreeQuery query(result, point, drawableFlags, viewMask);
  329. ptr->GetDrawables(query);
  330. return VectorToHandleArray<Drawable>(result, "Array<Drawable@>");
  331. }
  332. // void Octree::GetDrawables(OctreeQuery &query) const | File: ../Graphics/Octree.h
  333. CScriptArray* OctreeGetDrawablesBox(const BoundingBox& box, unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  334. {
  335. PODVector<Drawable*> result;
  336. BoxOctreeQuery query(result, box, drawableFlags, viewMask);
  337. ptr->GetDrawables(query);
  338. return VectorToHandleArray<Drawable>(result, "Array<Drawable@>");
  339. }
  340. // void Octree::GetDrawables(OctreeQuery &query) const | File: ../Graphics/Octree.h
  341. CScriptArray* OctreeGetDrawablesFrustum(const Frustum& frustum, unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  342. {
  343. PODVector<Drawable*> result;
  344. FrustumOctreeQuery query(result, frustum, drawableFlags, viewMask);
  345. ptr->GetDrawables(query);
  346. return VectorToHandleArray<Drawable>(result, "Array<Drawable@>");
  347. }
  348. // void Octree::GetDrawables(OctreeQuery &query) const | File: ../Graphics/Octree.h
  349. CScriptArray* OctreeGetDrawablesSphere(const Sphere& sphere, unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  350. {
  351. PODVector<Drawable*> result;
  352. SphereOctreeQuery query(result, sphere, drawableFlags, viewMask);
  353. ptr->GetDrawables(query);
  354. return VectorToHandleArray<Drawable>(result, "Array<Drawable@>");
  355. }
  356. // void Octree::GetDrawables(OctreeQuery &query) const | File: ../Graphics/Octree.h
  357. CScriptArray* OctreeGetAllDrawables(unsigned char drawableFlags, unsigned viewMask, Octree* ptr)
  358. {
  359. PODVector<Drawable*> result;
  360. AllContentOctreeQuery query(result, drawableFlags, viewMask);
  361. ptr->GetDrawables(query);
  362. return VectorToHandleArray<Drawable>(result, "Array<Drawable@>");
  363. }
  364. // ========================================================================================
  365. // void Renderer::SetVSMShadowParameters(float minVariance, float lightBleedingReduction) | File: ../Graphics/Renderer.h
  366. void RendererSetVSMShadowParameters(const Vector2& parameters, Renderer* ptr)
  367. {
  368. ptr->SetVSMShadowParameters(parameters.x_, parameters.y_);
  369. }
  370. }