1 #ifndef _SurfaceCtrl_GLDrawFunction_h_
2 #define _SurfaceCtrl_GLDrawFunction_h_
3 #include "Definition.h"
4 #include "Object3D.h"
5 
6 namespace Upp{
7 class Object3DProvider {
8 	private:
9 		bool begin = false;
10 		Upp::Vector<float> vertexData;
11 		Upp::Vector<float> normalData;
12 		Upp::Vector<float> colorData;
13 		GLenum DrawType;
14 
InsertFloatColor(int number,const Color & color,int alpha)15 		void InsertFloatColor(int number,const Color& color, int alpha){
16 			if(number > 0){
17 				for(int e = 0;e < number; e ++){
18 					colorData.Add(color.GetR()/255.0f);
19 					colorData.Add(color.GetG()/255.0f);
20 					colorData.Add(color.GetB()/255.0f);
21 					colorData.Add(alpha/255.0f);  //Not used YET
22 				}
23 			}
24 		}
25 
Point3DToVec3(const Point3D & p)26 		glm::vec3 Point3DToVec3(const Point3D& p){
27 			return glm::vec3(float(p.x), float(p.y), float(p.z));
28 		}
29 
30 	public:
Object3DProvider()31 		Object3DProvider(){}
Begin(GLenum drawType)32 		Object3DProvider& Begin(GLenum drawType){
33 			if(!begin){
34 				vertexData.Clear();
35 				normalData.Clear();
36 				colorData.Clear();
37 				DrawType = drawType;
38 				begin = true;
39 			}else
40 				LOG("Error Object3DProvider : Call Begin function without ending the last begin function !");
41 			return *this;
42 		}
43 		Object3DProvider& AddLine(float x0, float y0, float z0, float x1, float y1, float z1,const Color& color, int alpha= 255){
44 			if(begin){
45 				vertexData<<x0<<y0<<z0<<x1<<y1<<z1;
46 				InsertFloatColor(2,color,alpha);
47 			}
48 			return *this;
49 		}
50 		Object3DProvider& AddLine(const glm::vec3& p0, const glm::vec3& p1, const Color &color, int alpha = 255){
51 			return AddLine(p0.x, p0.y, p0.z, p1.x, p1.y, p1.z, color, alpha);
52 		}
53 		Object3DProvider& AddLine(const Segment3D& seg, const Color &color, int alpha = 255){
54 			return AddLine(float(seg.from.x), float(seg.from.y), float(seg.from.z), float(seg.to.x), float(seg.to.y), float(seg.to.z), color, alpha);
55 		}
56 		Object3DProvider& AddQuad(const glm::vec3& p0, const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3, const Color &color, int alpha= 255){
57 			if(begin){
58 				vertexData
59 				<<p0.x<<p0.y<<p0.z
60 				<<p1.x<<p1.y<<p1.z
61 				<<p2.x<<p2.y<<p2.z
62 				<<p3.x<<p3.y<<p3.z;
63 				InsertFloatColor(4,color,alpha);
64 			}
65 			return *this;
66 		}
67 		Object3DProvider& AddCuboid(const glm::vec3& p0, const glm::vec3& p1, const Color &color, int alpha= 255){
68 			if(begin){
69 				vertexData
70 				<<p0.x<< p0.y<< p0.z
71 				<<p0.x<< p0.y<< p1.z
72 				<<p0.x<< p1.y<< p1.z
73 				<<p0.x<< p1.y<< p0.z
74 				<<p0.x<< p0.y<< p0.z
75 				<<p1.x<< p0.y<< p0.z
76 				<<p1.x<< p0.y<< p1.z
77 				<<p1.x<< p1.y<< p1.z
78 				<<p1.x<< p1.y<< p0.z
79 				<<p1.x<< p0.y<< p0.z
80 				<<p1.x<< p0.y<< p1.z
81 				<<p0.x<< p0.y<< p1.z
82 				<<p0.x<< p1.y<< p1.z
83 				<<p1.x<< p1.y<< p1.z
84 				<<p1.x<< p1.y<< p0.z
85 				<<p0.x<< p1.y<< p0.z;
86 				InsertFloatColor(16,color,alpha);
87 			}
88 			return *this;
89 		}
90 		Object3DProvider& AddSurface(const Surface &surf, const Color &linCol, int alpha= 255){
91 			return AddSurface0(surf.nodes, surf.panels, linCol, alpha);
92 		}
93 		Object3DProvider& AddSegments(const Vector<Segment3D>& segs, const Color &color, int alpha= 255){
94 			for (int i = 0; i < segs.GetCount(); ++i)
95 				AddLine(segs[i], color, alpha);
96 			return *this;
97 		}
98 		Object3DProvider& AddArrow(float x0, float y0, float z0, float x1, float y1, float z1, const Color &color, int alpha= 255){
99 			if(begin){
100 				Segment3D seg(Point3D(x0, y0, z0), Point3D(x1, y1, z1));
101 				Vector3D vector = seg.Vector().Normalize();
102 				double len = seg.Length();
103 				double lenArr = 0.8*len;
104 				Point3D pointTri(x0, y0, z0);
105 				pointTri = pointTri + vector*lenArr;
106 				double zangle = atan2(vector.x, vector.y);
107 				double nangle = zangle + M_PI/2;
108 				Point3D parr1(pointTri.x + 0.1*len*sin(nangle), pointTri.y + 0.1*len*cos(nangle), pointTri.z);
109 				Point3D parr2(pointTri.x - 0.1*len*sin(nangle), pointTri.y - 0.1*len*cos(nangle), pointTri.z);
110 
111 				AddLine(Point3DToVec3(seg.from), Point3DToVec3(pointTri), color, alpha);
112 				AddLine(Point3DToVec3(seg.to),   Point3DToVec3(parr1),	  color, alpha);
113 				AddLine(Point3DToVec3(seg.to),   Point3DToVec3(parr2),    color, alpha);
114 				AddLine(Point3DToVec3(parr1),    Point3DToVec3(parr2),    color, alpha);
115 			}
116 			return *this;
117 		}
118 		Object3DProvider& AddArrow(const glm::vec3 &p0, const glm::vec3 &p1, const Color &color, int alpha= 255){
119 			return AddArrow(p0.x, p0.y, p0.z, p1.x, p1.y, p1.z, color);
120 		}
121 		Object3DProvider& AddAxis(float x, float y, float z, float len, int alpha= 255){
122 			AddArrow(x, y, z, x+len, y	 , z,     LtRed() , alpha);
123 			AddArrow(x, y, z, x	 , y+len , z,     LtGreen() , alpha);
124 			AddArrow(x, y, z, x	 , y	 , z+len, LtBlue() , alpha);
125 			return *this;
126 		}
127 		Object3DProvider& AddAxis(const glm::vec3 &p, float len , int alpha= 255){
128 			return AddAxis(p.x, p.y, p.z, len , alpha);
129 		}
130 
CreateAxis(float len)131 		Object3DProvider&  CreateAxis(float len){
132 			if(begin){
133 				//vertexData is a vector of float
134 				//Len is the length of my axis (20 000)
135 				vertexData << 0.0f << 0.0f << 0.0f
136 						   << len  << 0.0f << 0.0f
137 						   << 0.0f << 0.0f << 0.0f
138 						   << 0.0f << len  << 0.0f
139 						   << 0.0f << 0.0f << 0.0f
140 						   << 0.0f << 0.0f << len;
141 
142 			   InsertFloatColor(2,LtRed(), 255);
143 			   InsertFloatColor(2,LtGreen(), 255);
144 			   InsertFloatColor(2,LtBlue(), 255);
145 			}
146 			return *this;
147 		}
148 
149 		Object3DProvider& AddDoubleAxis(float x, float y, float z, float len, const Color &color , int alpha= 255){
150 			AddLine(x-len/2, y	  , z	   , x+len/2, y	     , z	  , color, alpha);
151 			AddLine(x		 , y-len/2, z	   , x		, y+len/2, z	  , color, alpha);
152 			AddLine(x		 , y	  , z-len/2, x		, y	     , z+len/2, color, alpha);
153 			return *this;
154 		}
155 		Object3DProvider& AddDoubleAxis(const glm::vec3 &p, float len, const Color &color , int alpha= 255){
156 			return AddDoubleAxis(p.x, p.y, p.z, len, color, alpha);
157 		}
158 		Object3DProvider& AddCube(float x, float y, float z, float side, const Color &color , int alpha= 255){
159 			return AddCuboid(glm::vec3(x-side/2., y-side/2., z-side/2.), glm::vec3(x+side/2., y+side/2., z+side/2.), color, alpha);
160 		}
161 		Object3DProvider& AddCube(const Point3D &p, float side, const Color &color, int alpha= 255){
162 			return AddCube(float(p.x), float(p.y), float(p.z), side, color, alpha);
163 		}
164 		Object3DProvider& AddMesh(const glm::vec3 &p0, const glm::vec3 &p1, const glm::vec3 &p2, const glm::vec3 &p3, const Color &linCol , int alpha= 255){
165 			AddLine(p0, p1, linCol, alpha);
166 			AddLine(p1, p2, linCol, alpha);
167 			AddLine(p2, p3, linCol, alpha);
168 			AddLine(p3, p0, linCol, alpha);
169 			return *this;
170 		}
171 		Object3DProvider& AddSurface0(const Vector<Point3D> &nodes, const Vector<Panel> &panels, const Color &linCol , int alpha= 255){
172 			for (int ip = 0; ip < panels.GetCount(); ++ip) {
173 				const Panel &panel = panels[ip];
174 				glm::vec3 p0 = Point3DToVec3(nodes[panel.id[0]]);
175 				glm::vec3 p1 = Point3DToVec3(nodes[panel.id[1]]);
176 				glm::vec3 p2 = Point3DToVec3(nodes[panel.id[2]]);
177 				glm::vec3 p3 = Point3DToVec3(nodes[panel.id[3]]);
178 				AddMesh(p0, p1, p2, p3, linCol, alpha);
179 			}
180 			return *this;
181 		}
End()182 		Object3D End(){
183 			normalData.Append(Vector<float>(vertexData.GetCount()));
184 			Object3D buffered;
185 			Mesh& m = buffered.CreateMesh();
186 
187 			m.GetVertices().Append(vertexData);
188 			m.GetNormals().Append(normalData);
189 			m.GetColors().Append(colorData);
190 
191 			buffered.Init();
192 			buffered.SetDrawType(DrawType);
193 			buffered.ShowLight(false);
194 			buffered.ShowMeshLine(false);
195 			buffered.ShowMeshNormal(false);
196 
197 			begin = false;
198 			return pick(buffered);
199 		}
200 };
201 }
202 #endif