summaryrefslogtreecommitdiff
path: root/chart2/source/view/inc/GL3DRenderer.hxx
blob: af14537b4caf5a963d96702dc3f09321c66e9c37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef CHART2_GL3DRENDERER_HXX
#define CHART2_GL3DRENDERER_HXX

#include "glm/glm.hpp"
#include "glm/gtx/transform.hpp"
#include "glm/gtx/euler_angles.hpp"
#include "glm/gtx/quaternion.hpp"

#include <com/sun/star/awt/Point.hpp>
#include <tools/gen.hxx>

#include <vcl/opengl/IOpenGLRenderer.hxx>

#include <vector>
#include <list>
#include <map>

#define MAX_LIGHT_NUM 8

namespace chart {

namespace opengl3D {

struct PosVecf3
{
    float x;
    float y;
    float z;
};

typedef std::vector <glm::vec3> Vertices3D;
typedef std::vector <glm::vec2> UVs3D;
typedef std::vector <glm::vec3> Normals3D;

typedef struct MaterialParameters
{
    glm::vec4 ambient;
    glm::vec4 diffuse;
    glm::vec4 specular;
    glm::vec4 materialColor;

    int twoSidesLighting;
    float shininess;
    float pad;
    float pad1;
}Material;

typedef struct LightSource
{
    glm::vec4   lightColor;
    glm::vec4   positionWorldspace;
    float  lightPower;
    float  pad1;
    float  pad2;
    float  pad3;
}LightSource;

typedef struct GlobalLights
{
    int lightNum;
    glm::vec4 ambient;
    LightSource light[MAX_LIGHT_NUM];
}Lights;

typedef struct Polygon3DInfo
{
    short lineOnly;
    float lineWidth;
    int twoSidesLighting;
    long fillStyle;
    glm::vec4 polygonColor;
    Vertices3D *vertices;
    UVs3D *uvs;
    Normals3D *normals;
    std::list <Vertices3D *> verticesList;
    std::list <UVs3D *> uvsList;
    std::list <Normals3D *> normalsList;
    Material material;
}Polygon3DInfo;

typedef struct Extrude3DInfo
{
    int pickingFlg;
    short lineOnly;
    float lineWidth;
    int twoSidesLighting;
    glm::vec4 extrudeColor;
    long fillStyle;
    float xRange[2];
    float yRange[2];
    float xTransform;
    float yTransform;
    float zTransform;
    Material material;
    int startIndex[5];
    int size[5];
    int reverse;
}Extrude3DInfo;


typedef struct CameraInfo
{
    glm::vec3 cameraPos;
    glm::vec3 cameraOrg;
    glm::vec3 cameraUp;
}CameraInfo;

typedef struct CoordinateAxisInfo
{
    int pickingFlg;
    int reverse;
    glm::vec4 color;
    PosVecf3 trans;
    PosVecf3 scale;
}CoordinateAxisInfo;

typedef struct RoundBarMesh
{
    float topThreshold;
    float bottomThreshold;
    int iMeshStartIndices;
    int iMeshSizes;
    int iElementStartIndices[5];
    int iElementSizes[5];
}RoundBarMesh;

struct PackedVertex{
    glm::vec3 position;
    glm::vec3 normal;
    bool operator<(const PackedVertex that) const{
        return memcmp((void*)this, (void*)&that, sizeof(PackedVertex))>0;
    };
};

class OpenGL3DRenderer : public IOpenGLInfoProvider
{
public:
    OpenGL3DRenderer();

    void LoadShaders();
    void init();

    void Set3DSenceInfo(glm::vec3 cameraUp,glm::mat4 D3DTrasform,bool twoSidesLighting,sal_Int32 color);
    void SetLightInfo(bool lightOn,sal_Int32 color,glm::vec4 direction);
    void AddShapePolygon3DObject(sal_Int32 color,bool lineOnly,sal_Int32 lineColor,long fillStyle,sal_Int32 specular);
    void EndAddShapePolygon3DObject();
    void AddPolygon3DObjectNormalPoint(float x, float y, float z);
    void EndAddPolygon3DObjectNormalPoint();
    void AddPolygon3DObjectPoint(float x, float y, float z);
    void EndAddPolygon3DObjectPoint();
    void AddShape3DExtrudeObject(sal_Int32 color,sal_Int32 specular,float xTransform,float yTransform,float zTransform);
    void EndAddShape3DExtrudeObject();
    void AddExtrude3DObjectPoint(float x, float y, float z);
    void EndAddExtrude3DObjectPoint();
    double GetTime();
    void SetFPS(float fps);
    void SetClickPos(Point aMPos);
    int RenderClickPos(Point aMPos);

private:
    int MoveModelf(PosVecf3 trans, PosVecf3 angle, PosVecf3 scale);

    void GetFreq();
    int RenderPolygon3DObject();
    int RenderLine3D(Polygon3DInfo &polygon);
    int RenderPolygon3D(Polygon3DInfo &polygon);
    int Init3DUniformBlock();
    int Update3DUniformBlock();
    int RenderExtrude3DObject();
    int RenderFPS(float fps);
    int RenderText(::rtl::OUString string, com::sun::star::awt::Point aPos);
    int RenderExtrudeSurface(Extrude3DInfo extrude3D);
    int RenderExtrudeTopSurface(Extrude3DInfo extrude3D);
    int RenderExtrudeMiddleSurface(Extrude3DInfo extrude3D);
    int RenderExtrudeBottomSurface(Extrude3DInfo extrude3D);
    int RenderExtrudeFlatSurface(Extrude3DInfo extrude3D, int surIndex);
    int ProcessUnrenderedShape();
    glm::vec4 GetColorByIndex(int index);
    int GetIndexByColor(int r, int g, int b);
    int ProcessPickingBox();
    int ProcessExtrude3DPickingBox();
    void RenderCoordinateAxis();
    int AddVertexData(GLuint vertexBuf);
    int AddNormalData(GLuint normalBuf);
    int AddIndexData(GLuint indexBuf);
    bool GetSimilarVertexIndex(PackedVertex & packed,
        std::map<PackedVertex,unsigned short> & VertexToOutIndex,
        unsigned short & result
        );
    void SetVertex(PackedVertex &packed,
        std::map<PackedVertex,unsigned short> &VertexToOutIndex,
        std::vector<glm::vec3> &vertex,
        std::vector<glm::vec3> &normal,
        std::vector<unsigned short> &indeices);
    void CreateActualRoundedCube(float fRadius, int iSubDivY, int iSubDivZ, float width, float height, float depth);
    int GenerateRoundCornerBar(std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &normals, float fRadius, int iSubDivY,
                               int iSubDivZ, float width, float height, float depth);
private:
    // Projection matrix : default 45 degree Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    glm::mat4 m_Projection;
    // Camera matrix
    glm::mat4 m_View;
    // Model matrix : an identity matrix (model will be at the origin
    glm::mat4 m_Model;
    // Our ModelViewProjection : multiplication of our 3 matrices
    glm::mat4 m_MVP;

    double m_dFreq;

    int m_iWidth;

    int m_iHeight;

    float m_fZmax;

    Lights m_LightsInfo;

    CameraInfo m_CameraInfo;

    Polygon3DInfo m_Polygon3DInfo;

    std::list <Polygon3DInfo> m_Polygon3DInfoList;

    glm::mat4 m_D3DTrasform;

    glm::mat4 m_3DProjection;

    glm::mat4 m_3DView;

    glm::mat4 m_3DViewBack;

    glm::mat4 m_3DMVP;

    glm::mat4 m_TranslationMatrix;

    GLint m_3DProID;

    GLuint m_3DProjectionID;

    GLuint m_3DViewID;

    GLuint m_3DModelID;

    GLuint m_3DVertexID;

    GLuint m_3DNormalID;

    GLuint m_3DNormalMatrixID;

    GLuint m_3DUBOBuffer;

    GLint m_3DLightBlockIndex;

    GLint m_3DMaterialBlockIndex;

    GLint m_3DActualSizeLight;

    GLuint m_NormalBuffer;

    GLuint m_VertexBuffer;

    GLint m_MatrixID;

    GLuint m_FboID[2];

    Extrude3DInfo m_Extrude3DInfo;

    std::vector <Extrude3DInfo> m_Extrude3DList;

    GLuint m_CubeVertexBuf;

    GLuint m_CubeElementBuf;

    GLuint m_CubeNormalBuf;

    bool m_bCameraUpdated;

    float m_fFPS;

    Point m_aMPos;

    GLuint m_BoundBox;

    GLuint m_CoordinateBuf;

    int m_uiSelectFrameCounter;

    CoordinateAxisInfo m_coordinateAxisinfo;

    std::vector<glm::vec3> m_Vertices;

    std::vector<glm::vec3> m_Normals;

    std::vector<unsigned short> m_Indeices;

    RoundBarMesh m_RoundBarMesh;

    GLint m_CommonProID;
    GLint m_2DVertexID;
    GLint m_2DColorID;

    //TODO: moggi: kill the following parts
    // don't add anything below or I will remove it
    size_t m_iPointNum;
};

}

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */