[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / internal / bullet-impl / bullet-physics-debug-renderer.h
1 #pragma once
2
3 /*
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 // External includes
20 #include <GLES3/gl3.h>
21 #include <LinearMath/btIDebugDraw.h>
22 #include <dali/dali.h>
23
24 using Dali::Actor;
25 using Dali::CameraActor;
26 using Dali::Geometry;
27 using Dali::Renderer;
28 using Dali::Shader;
29 using Dali::TextureSet;
30
31 namespace Dali::Toolkit::Physics::Internal
32 {
33 class PhysicsAdaptor;
34
35 class PhysicsDebugRenderer : public btIDebugDraw
36 {
37 public:
38   // Creates and initializes a new renderer
39   static std::unique_ptr<PhysicsDebugRenderer> New(uint32_t width, uint32_t height, Dali::CameraActor camera, PhysicsAdaptor* adaptor);
40
41   /**
42    * Get the callback (for actor creation)
43    */
44   std::unique_ptr<Dali::RenderCallback>& GetCallback()
45   {
46     return mRenderCallback;
47   }
48
49   void UpdateWindowSize(Dali::Vector2 size);
50
51   /**
52    * Constructor.
53    * @param[in] width Width of the renderer - viewport
54    * @param[in] height Height of the renderer - viewport
55    */
56   PhysicsDebugRenderer(uint32_t width, uint32_t height, Dali::CameraActor camera, PhysicsAdaptor* adaptor);
57
58 public: // Inherited from btIDebugDraw
59   // Assume this is called during FrameCallback (i.e. in update manager, rather than during render...)
60   // Generate stack of lines... render, then clear stack.
61   void drawLine(const btVector3& from, const btVector3& to, const btVector3& color) override;
62   void drawLine(const btVector3& from, const btVector3& to, const btVector3& fromColor, const btVector3& toColor) override;
63   void drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color) override;
64   void reportErrorWarning(const char* warningString) override;
65   void draw3dText(const btVector3& location, const char* textString) override;
66   void setDebugMode(int debugMode) override;
67   int  getDebugMode() const override;
68
69 private:
70   bool OnRender(const Dali::RenderCallbackInput& input);
71   void Setup();
72   void PrepareShader();
73   void RenderLines(const Dali::RenderCallbackInput& input);
74
75 private:
76   CameraActor                           mCamera;
77   Renderer                              mDebugRenderer;
78   std::unique_ptr<Dali::RenderCallback> mRenderCallback;
79
80   enum class State
81   {
82     INIT,
83     RENDER
84   } mState{State::INIT};
85
86   struct VertexLine
87   {
88     Dali::Vector3 position;
89     Dali::Vector3 color;
90   };
91   std::vector<VertexLine> mLines;
92
93   Dali::Matrix    mModelViewMatrix;
94   Dali::Matrix    mViewMatrix;
95   Dali::Matrix    mProjectionMatrix;
96   int             mWidth;
97   int             mHeight;
98   PhysicsAdaptor& mAdaptor;
99   GLint           mVertexLocation;
100   GLint           mVertexColourLocation;
101   GLint           mProjectionLocation;
102   GLint           mModelViewLocation;
103   GLuint          mBufferId;
104   GLuint          mProgramId;
105 };
106
107 } // namespace Dali::Toolkit::Physics::Internal