[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / internal / chipmunk-impl / chipmunk-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 #include <GLES3/gl3.h>
20 #include <chipmunk/chipmunk.h>
21 #include <dali/dali.h>
22
23 using Dali::Actor;
24 using Dali::CameraActor;
25 using Dali::Geometry;
26 using Dali::Renderer;
27 using Dali::Shader;
28 using Dali::TextureSet;
29
30 namespace Dali::Toolkit::Physics::Internal
31 {
32 class PhysicsAdaptor;
33
34 class PhysicsDebugRenderer
35 {
36 public:
37   // Creates and initializes a new renderer
38   static std::unique_ptr<PhysicsDebugRenderer> New(uint32_t width, uint32_t height, Dali::CameraActor camera, PhysicsAdaptor* adaptor);
39   /**
40    * Get the callback (for actor creation)
41    */
42
43   std::unique_ptr<Dali::RenderCallback>& GetCallback()
44   {
45     return mRenderCallback;
46   }
47
48   void UpdateWindowSize(Dali::Vector2 size);
49
50   /**
51    * Constructor.
52    * @param[in] width Width of the renderer - viewport
53    * @param[in] height Height of the renderer - viewport
54    */
55   PhysicsDebugRenderer(uint32_t width, uint32_t height, Dali::CameraActor camera, PhysicsAdaptor* adaptor);
56
57   /**
58    * Get the drawing options struct ( construct only )
59    */
60   const cpSpaceDebugDrawOptions& GetDebugDrawOptions()
61   {
62     return mDebugDrawOptions;
63   }
64
65   struct Vertex
66   {
67     Dali::Vector2 position;
68     Dali::Vector2 uvs;
69     float         radius;
70     Dali::Vector4 fillColor;
71     Dali::Vector4 outlineColor;
72   };
73
74 public: // Debug functions (Creates indices & verts)
75   void              DrawCircle(cpVect pos, cpFloat angle, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor);
76   void              DrawSegment(cpVect a, cpVect b, cpSpaceDebugColor color);
77   void              DrawFatSegment(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor);
78   void              DrawPolygon(int count, const cpVect* verts, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor);
79   void              DrawDot(cpFloat size, cpVect pos, cpSpaceDebugColor color);
80   cpSpaceDebugColor DrawColorForShape(cpShape* shape);
81
82 private:
83   bool OnRender(const Dali::RenderCallbackInput& input);
84   void Setup();
85   void PrepareShader();
86   void RenderLines(const Dali::RenderCallbackInput& input);
87
88   Vertex* PushVertices(uint32_t vertexCount, uint32_t indexCount, const uint16_t* indices);
89   Vertex  MakeVertex(cpVect pos, float u, float v, float r, Vector4 fill, Vector4 outline);
90
91 private:
92   CameraActor                           mCamera;
93   cpSpaceDebugDrawOptions               mDebugDrawOptions;
94   Renderer                              mDebugRenderer;
95   std::unique_ptr<Dali::RenderCallback> mRenderCallback;
96
97   enum class State
98   {
99     INIT,
100     RENDER
101   } mState{State::INIT};
102
103   std::vector<Vertex>   mVertices;
104   std::vector<uint16_t> mIndices;
105
106   Dali::Matrix    mModelViewMatrix;
107   Dali::Matrix    mViewMatrix;
108   Dali::Matrix    mProjectionMatrix;
109   int             mWidth;
110   int             mHeight;
111   PhysicsAdaptor& mAdaptor;
112
113   float  mPointLineScale{2.0f};
114   GLint  mPositionLocation;
115   GLint  mUvsLocation;
116   GLint  mRadiusLocation;
117   GLint  mFillColourLocation;
118   GLint  mOutlineColourLocation;
119   GLint  mProjectionLocation;
120   GLint  mModelViewLocation;
121   GLuint mIndexBufferId;
122   GLuint mVertexBufferId;
123   GLuint mProgramId;
124 };
125
126 } // namespace Dali::Toolkit::Physics::Internal