Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / view / inc / GL3DRenderer.hxx
blob9dc860229aee84ea9b1daf79c539c09c821802af
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_CHART2_SOURCE_VIEW_INC_GL3DRENDERER_HXX
11 #define INCLUDED_CHART2_SOURCE_VIEW_INC_GL3DRENDERER_HXX
13 #include <glm/glm.hpp>
14 #include <glm/gtx/transform.hpp>
15 #include <glm/gtx/euler_angles.hpp>
16 #include <glm/gtx/quaternion.hpp>
18 #include <com/sun/star/awt/Point.hpp>
19 #include <boost/shared_array.hpp>
20 #include <tools/gen.hxx>
22 #include <vcl/bitmapex.hxx>
24 #include <vector>
25 #include <list>
26 #include <map>
28 namespace chart {
30 namespace opengl3D {
32 const auto maxLights = 8;
34 struct PosVecf3
36 float x;
37 float y;
38 float z;
41 typedef std::vector <glm::vec3> Vertices3D;
42 typedef std::vector <glm::vec3> Normals3D;
44 // fields must match definition in GLSL shader file
45 struct MaterialParameters
47 glm::vec4 ambient;
48 glm::vec4 diffuse;
49 glm::vec4 specular;
50 glm::vec4 materialColor;
52 bool twoSidesLighting;
53 float shininess;
54 float pad;
55 float pad1;
58 // fields must match definition in GLSL shader file
59 struct LightSource
61 glm::vec4 lightColor;
62 glm::vec4 positionWorldspace;
63 float lightPower;
64 float pad1;
65 float pad2;
66 float pad3;
69 struct GlobalLights
71 int lightNum;
72 glm::vec4 ambient;
73 LightSource light[maxLights];
76 struct Polygon3DInfo
78 bool lineOnly;
79 float lineWidth;
80 bool twoSidesLighting;
81 long fillStyle;
82 glm::vec4 polygonColor;
83 glm::vec4 id;
84 Vertices3D *vertices;
85 Normals3D *normals;
86 std::vector <Vertices3D *> verticesList;
87 std::vector <Normals3D *> normalsList;
88 MaterialParameters material;
91 struct Extrude3DInfo
93 bool rounded;
94 bool twoSidesLighting;
95 glm::vec4 extrudeColor;
96 glm::vec4 id;
97 sal_uInt32 orgID;
98 float xScale;
99 float yScale;
100 float zScale;
101 float xTransform;
102 float yTransform;
103 float zTransform;
104 glm::mat4 rotation;
105 MaterialParameters material;
106 int startIndex[5];
107 int size[5];
108 int reverse;
111 struct CameraInfo
113 glm::vec3 cameraPos;
114 glm::vec3 cameraOrg;
115 glm::vec3 cameraUp;
117 CameraInfo():
118 cameraUp(glm::vec3(0, 0, 1)) {}
121 struct RoundBarMesh
123 float topThreshold;
124 float bottomThreshold;
125 int iMeshStartIndices;
126 int iMeshSizes;
127 int iElementStartIndices[5];
128 int iElementSizes[5];
131 struct PackedVertex{
132 glm::vec3 position;
133 glm::vec3 normal;
134 bool operator<(const PackedVertex& that) const{
135 return memcmp(this, &that, sizeof(PackedVertex))>0;
139 struct TextInfo
141 glm::vec4 id;
142 sal_uInt32 uniqueId;
143 GLuint texture;
144 float vertex[12];
145 glm::vec3 pos;
146 glm::vec4 textColor;
149 struct TextureArrayInfo
151 size_t subTextureNum;
152 int textureArrayWidth;
153 int textureArrayHeight;
154 GLuint textureID;
156 TextureArrayInfo();
159 struct TextInfoBatch
161 size_t batchNum;
162 std::vector<glm::vec4> idList;
163 std::vector<TextureArrayInfo> texture;
164 std::vector<glm::vec3> vertexList;
165 std::vector<glm::vec3> textureCoordList;
168 struct BatchBarInfo
170 std::vector <glm::mat4> modelMatrixList;
171 std::vector <glm::mat3> normalMatrixList;
172 std::vector <glm::vec4> colorList;
173 std::map<sal_uInt32, unsigned int> mapId2Color;
174 glm::vec4 selectBarColor;
177 class OpenGL3DRenderer
179 public:
180 OpenGL3DRenderer();
181 ~OpenGL3DRenderer();
183 void init();
184 void Set3DSenceInfo(Color color, bool twoSidesLighting = true);
185 void SetLightInfo(bool lightOn, Color color, const glm::vec4& direction);
186 void AddShapePolygon3DObject(Color color, bool lineOnly, Color lineColor,
187 long fillStyle, sal_uInt32 specular, sal_uInt32 nUniqueId);
188 void EndAddShapePolygon3DObject();
189 void AddPolygon3DObjectNormalPoint(float x, float y, float z);
190 void EndAddPolygon3DObjectNormalPoint();
191 void AddPolygon3DObjectPoint(float x, float y, float z);
192 void EndAddPolygon3DObjectPoint();
193 void AddShape3DExtrudeObject(bool roundedCorner, Color color, sal_uInt32 specular, const glm::mat4& modelMatrix, sal_uInt32 nUniqueId);
194 void EndAddShape3DExtrudeObject();
195 void SetSize(const Size& rSize);
196 void SetCameraInfo(const glm::vec3& pos, const glm::vec3& direction, const glm::vec3& up);
197 void CreateTextTexture(const boost::shared_array<sal_uInt8> &bitmapBuf,
198 const ::Size& rSizePixels,
199 const glm::vec3& vTopLeft, const glm::vec3& vTopRight,
200 const glm::vec3& vBottomRight, const glm::vec3& vBottomLeft,
201 sal_uInt32 nUniqueId);
202 void CreateScreenTextTexture(const boost::shared_array<sal_uInt8> &bitmapBuf,
203 const ::Size& rSizePixels, const glm::vec2& vTopLeft,
204 const glm::vec2& vBottomRight, const glm::vec3& vPos, const glm::vec4& vScreenTextColor,
205 sal_uInt32 nUniqueId);
206 void ProcessUnrenderedShape(bool bNewScene);
208 void SetPickingMode(bool bPickingMode);
210 Color GetPixelColorFromPoint(long nX, long nY);
212 void ReleaseShapes();
213 void ReleaseScreenTextShapes();
214 void ReleaseTextTexture();
215 void ReleaseScreenTextTexture();
216 void StartClick(sal_uInt32 selectID);
217 void EndClick();
218 void SetScroll();
219 void SetScrollSpeed(float scrollSpeed);
220 void SetScrollDistance(float scrollDistance);
221 void SetSceneEdge(float minCoordX, float maxCoordX);
222 const glm::mat4& GetProjectionMatrix();
223 const glm::mat4& GetViewMatrix();
224 const glm::mat4& GetGlobalScaleMatrix();
225 glm::mat4 GetDiffOfTwoCameras(const glm::vec3& rBeginPos, const glm::vec3& rEndPos, const glm::vec3& rBeginDirection, const glm::vec3& rEndDirection);
226 glm::mat4 GetDiffOfTwoCameras(const glm::vec3& rEndPos, const glm::vec3& rEndDirection);
227 void AddMatrixDiff(const glm::mat4& aMat);
228 void ResetMatrixDiff();
229 private:
230 void MoveModelf( const PosVecf3& trans, const PosVecf3& angle, const PosVecf3& scale);
232 static void ClearBuffer();
233 void RenderPolygon3DObject();
234 void RenderLine3D(const Polygon3DInfo &polygon);
235 void RenderPolygon3D(const Polygon3DInfo &polygon);
236 void Init3DUniformBlock();
237 void Update3DUniformBlock();
238 void RenderExtrude3DObject();
239 //add for text
240 void RenderTextShape();
241 void RenderScreenTextShape();
242 void RenderExtrudeSurface(const Extrude3DInfo& extrude3D);
243 void RenderExtrudeTopSurface(const Extrude3DInfo& extrude3D);
244 void RenderExtrudeMiddleSurface(const Extrude3DInfo& extrude3D);
245 void RenderExtrudeBottomSurface(const Extrude3DInfo& extrude3D);
246 void RenderExtrudeFlatSurface(const Extrude3DInfo& extrude3D, int surIndex);
247 void AddVertexData(GLuint vertexBuf);
248 void AddNormalData(GLuint normalBuf);
249 void AddIndexData(GLuint indexBuf);
250 void RenderNonRoundedBar(const Extrude3DInfo& extrude3D);
251 static bool GetSimilarVertexIndex(PackedVertex const & packed,
252 std::map<PackedVertex,unsigned short> & VertexToOutIndex,
253 unsigned short & result
255 static void SetVertex(PackedVertex const &packed,
256 std::map<PackedVertex,unsigned short> &VertexToOutIndex,
257 std::vector<glm::vec3> &vertex,
258 std::vector<glm::vec3> &normal,
259 std::vector<unsigned short> &indeices);
260 void CreateActualRoundedCube(float fRadius, int iSubDivY, int iSubDivZ, float width, float height, float depth);
261 static int GenerateRoundCornerBar(std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &normals, float fRadius, int iSubDivY,
262 int iSubDivZ, float width, float height, float depth);
263 void CreateSceneBoxView();
265 void ReleasePolygonShapes();
266 void ReleaseExtrude3DShapes();
267 void ReleaseTextShapes();
268 void ReleaseBatchBarInfo();
269 void GetBatchBarsInfo();
270 void GetBatchTopAndFlatInfo(const Extrude3DInfo &extrude3D);
271 void GetBatchMiddleInfo(const Extrude3DInfo &extrude3D);
272 void InitBatch3DUniformBlock();
273 void UpdateBatch3DUniformBlock();
274 void RenderBatchBars(bool bNewScene);
275 void RenderTextShapeBatch();
276 void ReleaseTextShapesBatch();
277 void CreateTextTextureSingle(const boost::shared_array<sal_uInt8> &bitmapBuf,
278 const ::Size& rSizePixels,
279 const glm::vec3& vTopLeft, const glm::vec3& vTopRight,
280 const glm::vec3& vBottomRight, const glm::vec3& vBottomLeft,
281 sal_uInt32 nUniqueId);
282 void CreateTextTextureBatch(const boost::shared_array<sal_uInt8> &bitmapBuf,
283 const ::Size& rSizePixels,
284 const glm::vec3& vTopLeft, const glm::vec3& vTopRight,
285 const glm::vec3& vBottomRight, const glm::vec3& vBottomLeft,
286 sal_uInt32 nUniqueId);
287 void SetHighLightBar(BatchBarInfo &barInfo);
288 void DisableHighLightBar(BatchBarInfo &barInfo);
289 void CalcScrollMoveMatrix(bool bNewScene);
290 private:
292 struct ShaderResources
294 bool m_b330Support;
295 bool m_bScrollFlag;
296 // 3DProID
297 GLint m_3DProID;
298 GLint m_3DProjectionID;
299 GLint m_3DViewID;
300 GLint m_3DModelID;
301 GLint m_3DNormalMatrixID;
302 GLint m_3DVertexID;
303 GLint m_3DNormalID;
304 GLint m_3DMinCoordXID;
305 GLint m_3DMaxCoordXID;
306 GLint m_3DUndrawID;
307 //300 version;
308 GLint m_3DMaterialAmbientID;
309 GLint m_3DMaterialDiffuseID;
310 GLint m_3DMaterialSpecularID;
311 GLint m_3DMaterialColorID;
312 GLint m_3DMaterialTwoSidesID;
313 GLint m_3DMaterialShininessID;
314 GLint m_3DLightColorID;
315 GLint m_3DLightPosID;
316 GLint m_3DLightPowerID;
317 GLint m_3DLightNumID;
318 GLint m_3DLightAmbientID;
320 // TextProID
321 GLint m_TextProID;
322 GLint m_TextMatrixID;
323 GLint m_TextVertexID;
324 GLint m_TextTexCoordID;
325 GLint m_TextTexID;
327 // ScreenTextProID
328 GLint m_ScreenTextProID;
329 GLint m_ScreenTextVertexID;
330 GLint m_ScreenTextTexCoordID;
331 GLint m_ScreenTextTexID;
332 GLint m_ScreenTextColorID;
334 // CommonProID
335 GLint m_CommonProID;
336 GLint m_2DVertexID;
337 GLint m_2DColorID;
338 GLint m_MatrixID;
340 // Batch render
341 GLint m_3DBatchProID;
342 GLint m_3DBatchProjectionID;
343 GLint m_3DBatchViewID;
344 GLint m_3DBatchModelID;
345 GLint m_3DBatchNormalMatrixID;
346 GLint m_3DBatchVertexID;
347 GLint m_3DBatchNormalID;
348 GLint m_3DBatchColorID;
349 GLint m_3DBatchTransMatrixID;
350 GLint m_3DBatchMinCoordXID;
351 GLint m_3DBatchMaxCoordXID;
352 GLint m_3DBatchUndrawID;
354 //Batch render text
355 bool mbTexBatchSupport;
356 GLint m_BatchTextProID;
357 GLint m_BatchTextMatrixID;
358 GLint m_BatchTextVertexID;
359 GLint m_BatchTextTexCoordID;
360 GLint m_BatchTextTexID;
362 ShaderResources();
363 ~ShaderResources();
365 void LoadShaders();
368 struct PickingShaderResources
370 // CommonProID
371 GLint m_CommonProID;
372 GLint m_2DVertexID;
373 GLint m_2DColorID;
374 GLint m_MatrixID;
375 GLint m_ModelID;
376 GLint m_MinCoordXID;
377 GLint m_MaxCoordXID;
378 PickingShaderResources();
379 ~PickingShaderResources();
381 void LoadShaders();
384 ShaderResources maResources;
385 PickingShaderResources maPickingResources;
387 // Model matrix : an identity matrix (model will be at the origin
388 glm::mat4 m_Model;
390 sal_Int32 m_iWidth;
392 sal_Int32 m_iHeight;
394 GlobalLights m_LightsInfo;
396 CameraInfo m_CameraInfo;
398 Polygon3DInfo m_Polygon3DInfo;
400 std::vector <Polygon3DInfo> m_Polygon3DInfoList;
402 glm::mat4 m_3DProjection;
404 glm::mat4 m_3DView;
406 glm::mat4 m_3DMVP;
408 GLuint m_3DUBOBuffer;
410 GLint m_3DActualSizeLight;
412 GLuint m_NormalBuffer;
414 GLuint m_VertexBuffer;
416 Extrude3DInfo m_Extrude3DInfo;
418 std::vector <Extrude3DInfo> m_Extrude3DList;
420 GLuint m_CubeVertexBuf;
422 GLuint m_CubeElementBuf;
424 GLuint m_CubeNormalBuf;
426 GLuint m_BoundBox;
427 GLuint m_BoundBoxNormal;
428 // add for text
429 std::vector <TextInfo> m_TextInfoList;
430 std::vector <TextInfo> m_ScreenTextInfoList;
431 GLuint m_TextTexCoordBuf;
432 GLuint m_TextTexCoordBufBatch;
434 std::vector<glm::vec3> m_Vertices;
436 std::vector<glm::vec3> m_Normals;
438 std::vector<unsigned short> m_Indices;
440 RoundBarMesh m_RoundBarMesh;
442 GLuint m_RenderVertexBuf;
444 GLuint m_RenderTexCoordBuf;
446 float m_fViewAngle;
448 bool mbPickingMode;
450 GLuint mnPickingFbo;
451 GLuint mnPickingRboDepth;
452 GLuint mnPickingRboColor;
454 BatchBarInfo m_BarSurface[3];
455 GLuint m_BatchModelMatrixBuf;
456 GLuint m_BatchNormalMatrixBuf;
457 GLuint m_BatchColorBuf;
458 MaterialParameters m_Batchmaterial;
459 GLuint m_Batch3DUBOBuffer;
460 GLint m_Batch3DActualSizeLight;
462 glm::mat4 m_GlobalScaleMatrix;
463 TextInfoBatch m_TextInfoBatch;
464 //for 3.0 version
465 int m_iLightNum;
466 glm::vec4 m_Ambient;
467 glm::vec4 m_LightColor[maxLights];
468 glm::vec4 m_PositionWorldspace[maxLights];
469 float m_fLightPower[maxLights];
470 //for 3.0 end
471 std::vector<GLuint> m_Texturelist;
472 std::vector<GLuint> m_ScreenTexturelist;
473 bool m_bHighLighting;
474 sal_uInt32 m_uiSelectID;
475 float m_fScrollSpeed;
476 float m_fScrollDistance;
477 float m_fMinCoordX;
478 float m_fMaxCoordX;
479 float m_fCurDistance;
480 glm::mat4 m_ScrollMoveMatrix;
481 bool m_bUndrawFlag;
482 glm::mat4 m_matDiff;
489 #endif
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */