1 #ifndef _UOGLCtrl_UOGLCtrl_h_
2 #define _UOGLCtrl_UOGLCtrl_h_
3 #include "Definition.h"
4 #include "Object3DProvider.h"
5 #include "Object3D.h"
6 #include "MagicCamera.h"
7
8 namespace Upp{
9
GetSurfaceCtrlDirectory()10 static const String GetSurfaceCtrlDirectory(){
11 return GetFileDirectory(__FILE__);
12 }
13
14 class SurfaceCtrl : public GLCtrl{
15 public:
16 SurfaceCtrl();
17 ~SurfaceCtrl();
18
19 Function <void()> WhenBegin;
20 Function <void()> WhenPaint;
21 Function <void()> WhenEnd;
22
23 //Starting function
24 void Init()noexcept;
25 void InitCamera()noexcept;
26
27 //Action on all objects vector
GetAllObjects()28 const Upp::Vector<Object3D>& GetAllObjects()const noexcept{return allObjects;}
29 Object3D& CreateObject()noexcept;
30 int FindObject(int ID)const noexcept;
31 Object3D& GetObject(int ID)noexcept;
32 void DeleteObject(int ID)noexcept; //Delete the object (update selected)
33 void DeleteAllObjects()noexcept; //Delete all object
34 void SetDefaultShader(Object3D& obj); //Set default to the object in arg
35 void DrawAllObjects()noexcept; //Draw all object
36
37 //Change selected object vector
GetSelectedObject()38 const Upp::Vector<int>& GetSelectedObject()const noexcept{return allSelected;} //return const vector representing all selected Object
39 void AddSelectedObject(int ID)noexcept;
40 void UpdateSelectedObjectViaMouse(Point& p, dword keyflags)noexcept; //Process work on selected object depending on keyflags and point
41 glm::vec3 GetCenterPoint()const noexcept; //Return center point between all selected item
42 void RemoveSelectedObject(int ID)noexcept;
43 void ClearSelectedObject()noexcept;
44
45 //Skybox Getter
GetSkybox()46 Skybox& GetSkybox()noexcept{return skybox;}
47
48 //Change Object selected
49 void MoveAllSelectedObjects(glm::vec3 move)noexcept;
50 void RotateAllSelectedObjects(glm::quat rotation)noexcept;
51 void DeleteAllSelectedObjects()noexcept;
52
53 //Camera getter
GetCamera()54 const MagicCamera& GetCamera()const noexcept{return camera;}
GetCamera()55 MagicCamera& GetCamera()noexcept{return camera;}
56
57 //Fast Mode
EnableFastMode()58 SurfaceCtrl& EnableFastMode()noexcept{fastMode = true;return *this;}
DisableFastMode()59 SurfaceCtrl& DisableFastMode()noexcept{fastMode = false;return *this;}
60 SurfaceCtrl& FastMode(bool b = true)noexcept{fastMode = b; return *this;}
IsfastModeEnable()61 bool IsfastModeEnable()const noexcept{return fastMode;}
62
63 //time option
StartTimer()64 SurfaceCtrl& StartTimer()noexcept{TimerStarted = true; start= std::chrono::high_resolution_clock::now();return *this;}
StopTimer()65 SurfaceCtrl& StopTimer()noexcept{TimerStarted = false;return *this;}
66 double GetEllapsedTime()noexcept;
67 double GetDeltaTime()noexcept;
68
69 //Axis option
EnableAxis()70 SurfaceCtrl& EnableAxis()noexcept{showAxis = true; return *this;}
DisableAxis()71 SurfaceCtrl& DisableAxis()noexcept{showAxis = false; return *this;}
72 SurfaceCtrl& ShowAxis(bool b = true)noexcept{showAxis = b; return *this;}
IsAxisEnable()73 bool IsAxisEnable()const noexcept{return showAxis;}
74
EnableDepthAxis()75 SurfaceCtrl& EnableDepthAxis()noexcept{depthAxis = true; return *this;}
DisableDepthAxis()76 SurfaceCtrl& DisableDepthAxis()noexcept{depthAxis = false; return *this;}
77 SurfaceCtrl& UseDepthAxis(bool b = true)noexcept{depthAxis = b; return *this;}
IsDepthAxisEnable()78 bool IsDepthAxisEnable()noexcept{return depthAxis;}
79
80 //Focus option
81 SurfaceCtrl& ShowCameraFocus(bool b = true)noexcept{showCameraFocus = b; return *this;}
IsCameraFocusShow()82 bool IsCameraFocusShow()const noexcept{return showCameraFocus;}
83 void ZoomToFit(); //Replace the camera to fit all object loaded in the screen
84 void ProcessZoom(Point p, int zdelta, float multiplier = 1.0f); // Zoom the camera out/in depending on multiplier
85 void ViewFromAxe(Point p, bool AxeX, bool AxeY, bool AxeZ); // Will set camera on axe selected axe
86
87 //Application event
Layout()88 virtual void Layout(){GLResize(GetSize().cx,GetSize().cy);}
89 virtual void GLPaint(); //paint function
90 virtual void GLResize(int w, int h); //Action on resize
91
92 //Input event
93 virtual bool Key(dword key,int count); //Action when key press
94
95 int buttonRotation = Ctrl::MIDDLE;
96 int buttonDrag = Ctrl::LEFT;
97 int buttonMenu = Ctrl::RIGHT;
98
99 String defaultFileName;
100 int jpgQuality = 90;
101 Image GetImage();
102 void SaveToClipboard();
103 void SaveToFile();
104 void OnTypeImage(FileSel *_fs);
105
106 virtual Image HandleEvent(int event, Point p, int zdelta, dword);
107
108 //Menu bar
109 Image MouseEvent(int event, Point p, int zdelta, dword keyflags);
110 void ContextMenu(Bar& bar,const Point& p);
111
112 private:
113 typedef SurfaceCtrl CLASSNAME;
114 bool loaded = false;
115 Object3DProvider objProvider;
116
117 Upp::Vector<Object3D> allObjects;
118 Upp::Vector<int> allSelected;
119
120 Object3D Axis;
121 Object3D CameraFocus;
122 Skybox skybox;
123
124 MagicCamera camera;
125
126 OpenGLProgram DrawMeshNoLight;
127 OpenGLProgram DrawMeshLight;
128 OpenGLProgram DrawMeshLine;
129 OpenGLProgram DrawMeshNormal;
130
131 bool showAxis = true;
132 bool depthAxis = false;
133 bool showCameraFocus = false;
134
135 float sizeW = 800.0f;
136 float sizeH = 600.0f;
137
138 bool TimerStarted = false;
139 std::chrono::time_point<std::chrono::high_resolution_clock> start,end; //High resolution clock
140 double DeltaTime=0.0f,LastTime=0.0f,lastFrame =0.0f,Timer=0.0f;
141 int bufferFrame =0,frameCount = 0; //used to calculate FPS
142 void ProcessTime()noexcept;
143
144 bool fastMode = false;
145
146 void InitShader(); //Load default shader
147 };
148 #include "Object3D.h"
149 }
150 #endif
151