fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / view / main / GL3DRenderer.cxx
blob1b7f67acc2c1b6b7e4bf7dd6595cf4ea1453319e
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 #include <GL/glew.h>
12 #include "GL3DRenderer.hxx"
14 #include <vcl/opengl/OpenGLHelper.hxx>
15 #include <vcl/font.hxx>
16 #include <vcl/virdev.hxx>
18 #include <com/sun/star/awt/Size.hpp>
20 #include <StaticGeometry.h>
21 #include "glm/gtc/matrix_inverse.hpp"
22 #include <boost/checked_delete.hpp>
23 #include <boost/scoped_array.hpp>
25 #define DEBUG_FBO 0
27 using namespace com::sun::star;
29 namespace chart {
31 namespace opengl3D {
33 namespace {
35 const int CORNER_DIVION_Y = 20;
36 const int CORNER_DIVION_Z = 20;
38 GLfloat texCoords[] = {
39 1.0f, 0.0f,
40 1.0f, 0.96f,
41 0.0f, 0.96f,
42 0.0f, 0.0f
45 glm::vec4 getColorAsVector(sal_uInt32 nColor)
47 return glm::vec4(((nColor & 0x00FF0000) >> 16) / 255.0f,
48 ((nColor & 0x0000FF00) >> 8) / 255.0f,
49 (nColor & 0x000000FF) / 255.0f,
50 (0xFF - (nColor & 0xFF000000)/255.0));
55 TextureArrayInfo::TextureArrayInfo():
56 subTextureNum(0),
57 textureArrayWidth(0),
58 textureArrayHeight(0),
59 textureID(0)
63 OpenGL3DRenderer::OpenGL3DRenderer():
64 m_iWidth(0)
65 , m_iHeight(0)
66 , m_3DUBOBuffer(0)
67 , m_3DActualSizeLight(0)
68 , m_NormalBuffer(0)
69 , m_VertexBuffer(0)
70 , m_CubeVertexBuf(0)
71 , m_CubeElementBuf(0)
72 , m_CubeNormalBuf(0)
73 , m_BoundBox(0)
74 , m_BoundBoxNormal(0)
75 , m_TextTexCoordBuf(0)
76 , m_TextTexCoordBufBatch(0)
77 , m_RoundBarMesh()
78 , m_RenderVertexBuf(0)
79 , m_RenderTexCoordBuf(0)
80 , m_fViewAngle(30.0f)
81 , mbPickingMode(false)
82 , mnPickingFbo(0)
83 , mnPickingRboDepth(0)
84 , mnPickingRboColor(0)
85 , m_BatchModelMatrixBuf(0)
86 , m_BatchNormalMatrixBuf(0)
87 , m_BatchColorBuf(0)
88 , m_Batch3DUBOBuffer(0)
89 , m_Batch3DActualSizeLight(0)
90 , m_iLightNum(0)
91 , m_bHighLighting(false)
92 , m_uiSelectID(0)
93 , m_fScrollSpeed(0.0f)
94 , m_fScrollDistance(0.0f)
95 , m_fMinCoordX(0.0f)
96 , m_fMaxCoordX(0.0f)
97 , m_fCurDistance(0.0f)
98 , m_ScrollMoveMatrix(glm::mat4(1.0))
99 , m_bUndrawFlag(false)
100 , m_matDiff(glm::mat4(0.0))
102 m_Polygon3DInfo.lineOnly = false;
103 m_Polygon3DInfo.twoSidesLighting = false;
104 m_Polygon3DInfo.vertices = NULL;
105 m_Polygon3DInfo.normals = NULL;
106 m_Polygon3DInfo.lineWidth = 0.001f;
108 m_Extrude3DInfo.twoSidesLighting = false;
110 m_RoundBarMesh.iMeshSizes = 0;
113 OpenGL3DRenderer::~OpenGL3DRenderer()
115 ReleaseShapes();
116 // delete buffers
117 glDeleteBuffers(1, &m_CubeVertexBuf);
118 glDeleteBuffers(1, &m_CubeNormalBuf);
119 glDeleteBuffers(1, &m_CubeElementBuf);
120 glDeleteBuffers(1, &m_BoundBox);
121 glDeleteBuffers(1, &m_BoundBoxNormal);
122 glDeleteBuffers(1, &m_TextTexCoordBuf);
123 glDeleteBuffers(1, &m_RenderTexCoordBuf);
124 glDeleteBuffers(1, &m_RenderVertexBuf);
125 glDeleteBuffers(1, &m_3DUBOBuffer);
126 glDeleteBuffers(1, &m_VertexBuffer);
127 glDeleteBuffers(1, &m_NormalBuffer);
128 glDeleteBuffers(1, &m_Batch3DUBOBuffer);
129 glDeleteBuffers(1, &m_3DUBOBuffer);
130 glDeleteBuffers(1, &m_3DUBOBuffer);
131 glDeleteBuffers(1, &m_TextTexCoordBufBatch);
133 glDeleteFramebuffers(1, &mnPickingFbo);
134 glDeleteRenderbuffers(1, &mnPickingRboDepth);
135 glDeleteRenderbuffers(1, &mnPickingRboColor);
137 for (size_t i = 0; i < m_TextInfoBatch.texture.size(); i++)
139 glDeleteTextures(1, &m_TextInfoBatch.texture[i].textureID);
141 m_TextInfoBatch.texture.clear();
145 OpenGL3DRenderer::ShaderResources::ShaderResources()
146 : m_b330Support(false)
147 , m_bScrollFlag(false)
148 , m_3DProID(0)
149 , m_3DProjectionID(0)
150 , m_3DViewID(0)
151 , m_3DModelID(0)
152 , m_3DNormalMatrixID(0)
153 , m_3DVertexID(0)
154 , m_3DNormalID(0)
155 , m_3DMinCoordXID(0)
156 , m_3DMaxCoordXID(0)
157 , m_3DUndrawID(0)
158 , m_3DMaterialAmbientID(0)
159 , m_3DMaterialDiffuseID(0)
160 , m_3DMaterialSpecularID(0)
161 , m_3DMaterialColorID(0)
162 , m_3DMaterialTwoSidesID(0)
163 , m_3DMaterialShininessID(0)
164 , m_3DLightColorID(0)
165 , m_3DLightPosID(0)
166 , m_3DLightPowerID(0)
167 , m_3DLightNumID(0)
168 , m_3DLightAmbientID(0)
169 , m_TextProID(0)
170 , m_TextMatrixID(0)
171 , m_TextVertexID(0)
172 , m_TextTexCoordID(0)
173 , m_TextTexID(0)
174 , m_ScreenTextProID(0)
175 , m_ScreenTextVertexID(0)
176 , m_ScreenTextTexCoordID(0)
177 , m_ScreenTextTexID(0)
178 , m_ScreenTextColorID(0)
179 , m_CommonProID(0)
180 , m_2DVertexID(0)
181 , m_2DColorID(0)
182 , m_MatrixID(0)
183 , m_3DBatchProID(0)
184 , m_3DBatchProjectionID(0)
185 , m_3DBatchViewID(0)
186 , m_3DBatchModelID(0)
187 , m_3DBatchNormalMatrixID(0)
188 , m_3DBatchVertexID(0)
189 , m_3DBatchNormalID(0)
190 , m_3DBatchColorID(0)
191 , m_3DBatchTransMatrixID(0)
192 , m_3DBatchMinCoordXID(0)
193 , m_3DBatchMaxCoordXID(0)
194 , m_3DBatchUndrawID(0)
195 , mbTexBatchSupport(false)
196 , m_BatchTextProID(0)
197 , m_BatchTextMatrixID(0)
198 , m_BatchTextVertexID(0)
199 , m_BatchTextTexCoordID(0)
200 , m_BatchTextTexID(0)
204 OpenGL3DRenderer::ShaderResources::~ShaderResources()
206 glDeleteProgram(m_CommonProID);
207 glDeleteProgram(m_TextProID);
208 glDeleteProgram(m_ScreenTextProID);
209 glDeleteProgram(m_3DProID);
210 glDeleteProgram(m_3DBatchProID);
211 glDeleteProgram(m_BatchTextProID);
214 void OpenGL3DRenderer::CheckGLSLVersion()
216 maResources.m_b330Support = GLEW_VERSION_3_3;
219 void OpenGL3DRenderer::ShaderResources::LoadShaders()
221 CHECK_GL_ERROR();
222 if (m_b330Support)
224 m_3DProID = OpenGLHelper::LoadShaders("shape3DVertexShader", "shape3DFragmentShader");
225 m_3DProjectionID = glGetUniformLocation(m_3DProID, "P");
226 m_3DViewID = glGetUniformLocation(m_3DProID, "V");
227 m_3DModelID = glGetUniformLocation(m_3DProID, "M");
228 m_3DNormalMatrixID = glGetUniformLocation(m_3DProID, "normalMatrix");
229 m_3DVertexID = glGetAttribLocation(m_3DProID, "vertexPositionModelspace");
230 m_3DNormalID = glGetAttribLocation(m_3DProID, "vertexNormalModelspace");
231 CHECK_GL_ERROR();
232 if (m_bScrollFlag)
234 m_3DBatchProID = OpenGLHelper::LoadShaders("shape3DVertexShaderBatchScroll", "shape3DFragmentShaderBatchScroll");
235 m_3DBatchTransMatrixID = glGetUniformLocation(m_3DBatchProID, "transMatrix");
236 m_3DBatchMinCoordXID = glGetUniformLocation(m_3DBatchProID, "minCoordX");
237 m_3DBatchMaxCoordXID = glGetUniformLocation(m_3DBatchProID, "maxCoordX");
238 m_3DBatchUndrawID = glGetUniformLocation(m_3DBatchProID, "undraw");
240 else
241 m_3DBatchProID = OpenGLHelper::LoadShaders("shape3DVertexShaderBatch", "shape3DFragmentShaderBatch");
243 CHECK_GL_ERROR();
244 m_3DBatchProjectionID = glGetUniformLocation(m_3DBatchProID, "P");
245 m_3DBatchViewID = glGetUniformLocation(m_3DBatchProID, "V");
246 m_3DBatchModelID = glGetAttribLocation(m_3DBatchProID, "M");
247 m_3DBatchNormalMatrixID = glGetAttribLocation(m_3DBatchProID, "normalMatrix");
248 m_3DBatchVertexID = glGetAttribLocation(m_3DBatchProID, "vertexPositionModelspace");
249 m_3DBatchNormalID = glGetAttribLocation(m_3DBatchProID, "vertexNormalModelspace");
250 m_3DBatchColorID = glGetAttribLocation(m_3DBatchProID, "barColor");
251 #if !defined MACOSX
252 //check whether the texture array is support
253 mbTexBatchSupport = GLEW_EXT_texture_array;
254 #endif
255 CHECK_GL_ERROR();
256 if (mbTexBatchSupport)
258 m_BatchTextProID = OpenGLHelper::LoadShaders("textVertexShaderBatch", "textFragmentShaderBatch");
259 m_BatchTextMatrixID = glGetUniformLocation(m_BatchTextProID, "MVP");
260 m_BatchTextTexID = glGetUniformLocation(m_BatchTextProID, "texArray");
261 m_BatchTextVertexID = glGetAttribLocation(m_BatchTextProID, "vPosition");
262 m_BatchTextTexCoordID = glGetAttribLocation(m_BatchTextProID, "texCoord");
264 mbTexBatchSupport = m_BatchTextProID != 0;
265 CHECK_GL_ERROR();
267 else
269 CHECK_GL_ERROR();
270 //use 300
271 m_3DProID = OpenGLHelper::LoadShaders("shape3DVertexShaderV300", "shape3DFragmentShaderV300");
272 m_3DProjectionID = glGetUniformLocation(m_3DProID, "P");
273 m_3DViewID = glGetUniformLocation(m_3DProID, "V");
274 m_3DModelID = glGetUniformLocation(m_3DProID, "M");
275 m_3DNormalMatrixID = glGetUniformLocation(m_3DProID, "normalMatrix");
276 m_3DMaterialAmbientID = glGetUniformLocation(m_3DProID, "materialAmbient");
277 m_3DMaterialDiffuseID = glGetUniformLocation(m_3DProID, "materialDiffuse");
278 m_3DMaterialSpecularID = glGetUniformLocation(m_3DProID, "materialSpecular");
279 m_3DMaterialColorID = glGetUniformLocation(m_3DProID, "materialColor");
280 m_3DMaterialTwoSidesID = glGetUniformLocation(m_3DProID, "twoSidesLighting");
281 m_3DMaterialShininessID = glGetUniformLocation(m_3DProID, "materialShininess");
282 m_3DLightColorID = glGetUniformLocation(m_3DProID, "lightColor");
283 m_3DLightPosID = glGetUniformLocation(m_3DProID, "lightPosWorldspace");
284 m_3DLightPowerID = glGetUniformLocation(m_3DProID, "lightPower");
285 m_3DLightNumID = glGetUniformLocation(m_3DProID, "lightNum");
286 m_3DLightAmbientID = glGetUniformLocation(m_3DProID, "lightAmbient");
287 m_3DMinCoordXID = glGetUniformLocation(m_3DProID, "minCoordX");
288 m_3DMaxCoordXID = glGetUniformLocation(m_3DProID, "maxCoordX");
289 m_3DUndrawID = glGetUniformLocation(m_3DProID, "undraw");
290 m_3DVertexID = glGetAttribLocation(m_3DProID, "vertexPositionModelspace");
291 m_3DNormalID = glGetAttribLocation(m_3DProID, "vertexNormalModelspace");
293 CHECK_GL_ERROR();
294 if (!mbTexBatchSupport)
296 m_TextProID = OpenGLHelper::LoadShaders("textVertexShader", "textFragmentShader");
297 m_TextMatrixID = glGetUniformLocation(m_TextProID, "MVP");
298 m_TextVertexID = glGetAttribLocation(m_TextProID, "vPosition");
299 m_TextTexCoordID = glGetAttribLocation(m_TextProID, "texCoord");
300 m_TextTexID = glGetUniformLocation(m_TextProID, "TextTex");
302 CHECK_GL_ERROR();
304 m_ScreenTextProID = OpenGLHelper::LoadShaders("screenTextVertexShader", "screenTextFragmentShader");
305 m_ScreenTextVertexID = glGetAttribLocation(m_ScreenTextProID, "vPosition");
306 m_ScreenTextTexCoordID = glGetAttribLocation(m_ScreenTextProID, "texCoord");
307 m_ScreenTextTexID = glGetUniformLocation(m_ScreenTextProID, "TextTex");
308 m_ScreenTextColorID = glGetUniformLocation(m_ScreenTextProID, "textColor");
309 CHECK_GL_ERROR();
311 m_CommonProID = OpenGLHelper::LoadShaders("commonVertexShader", "commonFragmentShader");
312 m_MatrixID = glGetUniformLocation(m_CommonProID, "MVP");
313 m_2DColorID = glGetUniformLocation(m_CommonProID, "vColor");
314 m_2DVertexID = glGetAttribLocation(m_CommonProID, "vPosition");
316 CHECK_GL_ERROR();
319 OpenGL3DRenderer::PickingShaderResources::PickingShaderResources()
320 : m_CommonProID(0)
321 , m_2DVertexID(0)
322 , m_2DColorID(0)
323 , m_MatrixID(0)
324 , m_ModelID(0)
325 , m_MinCoordXID(0)
326 , m_MaxCoordXID(0)
330 OpenGL3DRenderer::PickingShaderResources::~PickingShaderResources()
332 glDeleteProgram(m_CommonProID);
335 void OpenGL3DRenderer::PickingShaderResources::LoadShaders()
337 m_CommonProID = OpenGLHelper::LoadShaders("pickingVertexShader", "pickingFragmentShader");
338 m_MatrixID = glGetUniformLocation(m_CommonProID, "MVP");
339 m_2DVertexID = glGetAttribLocation(m_CommonProID, "vPosition");
340 m_2DColorID = glGetUniformLocation(m_CommonProID, "vColor");
341 m_ModelID = glGetUniformLocation(m_CommonProID, "M");
342 m_MinCoordXID = glGetUniformLocation(m_CommonProID, "minCoordX");
343 m_MaxCoordXID = glGetUniformLocation(m_CommonProID, "maxCoordX");
346 void OpenGL3DRenderer::SetCameraInfo(const glm::vec3& pos, const glm::vec3& direction, const glm::vec3& up)
348 m_CameraInfo.cameraPos = pos;
349 m_CameraInfo.cameraOrg = direction;
350 m_CameraInfo.cameraUp = up;
353 void OpenGL3DRenderer::init()
355 CHECK_GL_ERROR();
356 glEnable(GL_CULL_FACE);
357 CHECK_GL_ERROR();
358 glCullFace(GL_BACK);
359 CHECK_GL_ERROR();
360 #if !defined(ANDROID) && !defined(IOS)
361 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
362 #endif
363 // Enable depth test
364 CHECK_GL_ERROR();
365 glEnable(GL_DEPTH_TEST);
366 // Accept fragment if it closer to the camera than the former one
367 CHECK_GL_ERROR();
368 glDepthFunc(GL_LESS);
369 CHECK_GL_ERROR();
370 glEnable(GL_LINE_SMOOTH);
371 CHECK_GL_ERROR();
372 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
373 CHECK_GL_ERROR();
374 glEnable(GL_BLEND);
375 CHECK_GL_ERROR();
376 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
377 CHECK_GL_ERROR();
379 glEnable(GL_MULTISAMPLE);
381 CHECK_GL_ERROR();
382 ClearBuffer();
383 CHECK_GL_ERROR();
385 glGenBuffers(1, &m_CubeVertexBuf);
386 glGenBuffers(1, &m_CubeNormalBuf);
387 glGenBuffers(1, &m_CubeElementBuf);
388 glGenBuffers(1, &m_VertexBuffer);
389 glGenBuffers(1, &m_NormalBuffer);
390 glGenBuffers(1, &m_BatchModelMatrixBuf);
391 glGenBuffers(1, &m_BatchNormalMatrixBuf);
392 glGenBuffers(1, &m_BatchColorBuf);
393 glGenBuffers(1, &m_TextTexCoordBufBatch);
394 glGenBuffers(1, &m_BoundBox);
395 glBindBuffer(GL_ARRAY_BUFFER, m_BoundBox);
396 glBufferData(GL_ARRAY_BUFFER, sizeof(boundBox), boundBox, GL_STATIC_DRAW);
397 glBindBuffer(GL_ARRAY_BUFFER, 0);
398 CHECK_GL_ERROR();
400 glGenBuffers(1, &m_BoundBoxNormal);
401 glBindBuffer(GL_ARRAY_BUFFER, m_BoundBoxNormal);
402 glBufferData(GL_ARRAY_BUFFER, sizeof(boundBoxNormal), boundBoxNormal, GL_STATIC_DRAW);
403 glBindBuffer(GL_ARRAY_BUFFER, 0);
404 CHECK_GL_ERROR();
406 m_fViewAngle = 30.0f;
407 m_3DProjection = glm::perspective(m_fViewAngle, (float)m_iWidth / (float)m_iHeight, 0.01f, 6000.0f);
409 CheckGLSLVersion();
410 CHECK_GL_ERROR();
411 maResources.LoadShaders();
412 maPickingResources.LoadShaders();
414 glGenBuffers(1, &m_TextTexCoordBuf);
415 glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBuf);
416 glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);
417 glBindBuffer(GL_ARRAY_BUFFER, 0);
419 glGenBuffers(1, &m_RenderTexCoordBuf);
420 glBindBuffer(GL_ARRAY_BUFFER, m_RenderTexCoordBuf);
421 glBufferData(GL_ARRAY_BUFFER, sizeof(coordReverseVertices), coordReverseVertices, GL_STATIC_DRAW);
422 glBindBuffer(GL_ARRAY_BUFFER, 0);
424 glGenBuffers(1, &m_RenderVertexBuf);
425 glBindBuffer(GL_ARRAY_BUFFER, m_RenderVertexBuf);
426 glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
427 glBindBuffer(GL_ARRAY_BUFFER, 0);
428 CHECK_GL_ERROR();
430 OpenGLHelper::createFramebuffer(m_iWidth, m_iHeight, mnPickingFbo, mnPickingRboDepth, mnPickingRboColor);
432 m_Extrude3DInfo.rounded = false;
433 CHECK_GL_ERROR();
434 if (maResources.m_b330Support)
436 Init3DUniformBlock();
437 InitBatch3DUniformBlock();
439 m_TextInfoBatch.batchNum = 512;
440 CHECK_GL_ERROR();
441 glViewport(0, 0, m_iWidth, m_iHeight);
442 Set3DSenceInfo(0xFFFFFF, true);
443 m_GlobalScaleMatrix = glm::scale(glm::vec3(0.01f, 0.01f, 0.01f));
446 void OpenGL3DRenderer::SetSize(const Size& rSize)
448 m_iWidth = rSize.Width();
449 m_iHeight = rSize.Height();
452 void OpenGL3DRenderer::AddVertexData(GLuint vertexBuf)
454 glBindBuffer(GL_ARRAY_BUFFER, vertexBuf);
455 glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(glm::vec3), &m_Vertices[0], GL_STATIC_DRAW);
456 CHECK_GL_ERROR();
457 glBindBuffer(GL_ARRAY_BUFFER, 0);
460 void OpenGL3DRenderer::AddNormalData(GLuint normalBuf)
462 glBindBuffer(GL_ARRAY_BUFFER, normalBuf);
463 glBufferData(GL_ARRAY_BUFFER, m_Normals.size() * sizeof(glm::vec3), &m_Normals[0], GL_STATIC_DRAW);
464 CHECK_GL_ERROR();
465 glBindBuffer(GL_ARRAY_BUFFER, 0);
468 void OpenGL3DRenderer::AddIndexData(GLuint indexBuf)
470 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuf);
471 glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned short), &m_Indices[0], GL_STATIC_DRAW);
472 CHECK_GL_ERROR();
473 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
476 bool OpenGL3DRenderer::GetSimilarVertexIndex(PackedVertex & packed,
477 std::map<PackedVertex,unsigned short> & VertexToOutIndex,
478 unsigned short & result
481 std::map<PackedVertex,unsigned short>::iterator it = VertexToOutIndex.find(packed);
482 if ( it == VertexToOutIndex.end() )
484 return false;
486 else
488 result = it->second;
489 return true;
493 void OpenGL3DRenderer::SetVertex(PackedVertex &packed,
494 std::map<PackedVertex,unsigned short> &VertexToOutIndex,
495 std::vector<glm::vec3> &vertex,
496 std::vector<glm::vec3> &normal,
497 std::vector<unsigned short> &indices)
499 unsigned short index;
500 bool found = GetSimilarVertexIndex(packed, VertexToOutIndex, index);
501 if ( found )
502 { // A similar vertex is already in the VBO, use it instead !
503 indices.push_back( index );
505 else
506 { // If not, it needs to be added in the output data.
507 vertex.push_back(packed.position);
508 normal.push_back(packed.normal);
509 size_t newindex = vertex.size() - 1;
510 indices.push_back( newindex );
511 VertexToOutIndex[ packed ] = newindex;
515 void OpenGL3DRenderer::CreateActualRoundedCube(float fRadius, int iSubDivY, int iSubDivZ, float width, float height, float depth)
517 float topThreshold = depth - 2 * fRadius;
518 float bottomThreshold = fRadius;
520 std::vector<glm::vec3> vertices;
521 std::vector<glm::vec3> normals;
522 GenerateRoundCornerBar(vertices, normals, fRadius, iSubDivY, iSubDivZ, width, height, depth);
523 std::map<PackedVertex,unsigned short> VertexToOutIndex;
524 glm::vec3 actualVerteices[3];
525 glm::vec3 actualNormals[3];
526 std::vector<unsigned short> indices[5];
527 glm::vec3 externSurNormal;
528 glm::mat4 corrctCoord = glm::translate(glm::vec3(width / 2.0f, height / 2.0f, depth / 2.0f - fRadius));
529 m_RoundBarMesh.topThreshold = topThreshold;
530 m_RoundBarMesh.bottomThreshold = bottomThreshold;
531 m_RoundBarMesh.iMeshStartIndices = m_Vertices.size();
532 for (int k = 0; k < 5; k++)
534 m_RoundBarMesh.iElementStartIndices[k] = indices[k].size();
536 for (size_t i = 0; i < vertices.size(); i += 3)
538 for (int k = 0; k < 3; k++)
540 actualVerteices[k] = glm::vec3(corrctCoord * glm::vec4(vertices[i + k], 1.0));
541 actualNormals[k] = normals[i + k];
543 float maxZ = std::max(std::max(actualVerteices[0].z, actualVerteices[1].z), actualVerteices[2].z);
544 float minZ = std::min(std::min(actualVerteices[0].z, actualVerteices[1].z), actualVerteices[2].z);
545 int surfaceIndex = (minZ >= topThreshold - 0.001) ? TOP_SURFACE : ((maxZ <= bottomThreshold + 0.001) ? BOTTOM_SURFACE : MIDDLE_SURFACE);
546 for (int k = 0; k < 3; k++)
549 PackedVertex packed = {actualVerteices[k], actualNormals[k]};
550 SetVertex(packed, VertexToOutIndex, m_Vertices, m_Normals, indices[surfaceIndex]);
553 //add extern
554 if ((surfaceIndex == TOP_SURFACE) || (surfaceIndex == BOTTOM_SURFACE))
556 actualVerteices[k].z = 0.0f;
557 externSurNormal = (surfaceIndex == TOP_SURFACE) ? glm::vec3(0.0, 0.0, 1.0) : glm::vec3(0.0, 0.0, -1.0);
558 int tmpSurfaceIndex = (surfaceIndex == TOP_SURFACE) ? FLAT_TOP_SURFACE : FLAT_BOTTOM_SURFACE;
559 PackedVertex packed = {actualVerteices[k], externSurNormal};
560 SetVertex(packed, VertexToOutIndex, m_Vertices, m_Normals, indices[tmpSurfaceIndex]);
565 //create index below
566 m_RoundBarMesh.iMeshSizes = m_Vertices.size() - m_RoundBarMesh.iMeshStartIndices;
567 for (int k = 0; k < 5; k++)
569 m_RoundBarMesh.iElementSizes[k] = indices[k].size() - m_RoundBarMesh.iElementStartIndices[k];
570 m_RoundBarMesh.iElementStartIndices[k] = m_Indices.size() * sizeof(unsigned short);
571 for (unsigned int IdxCnt = 0; IdxCnt < indices[k].size(); IdxCnt++)
573 m_Indices.push_back(indices[k][IdxCnt]);
575 indices[k].clear();
577 vertices.clear();
578 normals.clear();
579 VertexToOutIndex.clear();
582 int OpenGL3DRenderer::GenerateRoundCornerBar(std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &normals, float fRadius, int iSubDivY, int iSubDivZ, float width, float height, float depth)
584 //float fAddAngleY = 360.0f/float(iSubDivY), fAddAngleZ = 180.0f/float(iSubDivZ);
585 float fAddAngleY = 360.0f/float(iSubDivY);
586 float fAddAngleZ = 180.0f/float(iSubDivZ);
587 float fCurAngleY = 0.0f;
588 int iStepsY = 1;
590 const float PI = float(atan(1.0)*4.0);
591 int iFacesAdded = 0;
592 float xOffset[] = {(width / 2 - fRadius), -(width / 2 - fRadius), -(width / 2 - fRadius), (width / 2 - fRadius), (width / 2 - fRadius)};
593 float yOffset[] = {(height / 2 - fRadius), -(height / 2 - fRadius), (height / 2 - fRadius)};
594 float zOffset[] = {-(depth / 2 - fRadius), -(depth / 2 - fRadius), (depth / 2 - fRadius), (depth / 2 - fRadius), -(depth / 2 - fRadius)};
595 int iIndices[] = {0, 1, 2, 2, 3, 0};
596 while(iStepsY <= iSubDivY)
598 float fNextAngleY = fCurAngleY+fAddAngleY;
599 if ((fCurAngleY < 90.0f) && (fNextAngleY >= 90.0f))
601 fNextAngleY = 90.0f;
603 else if ((fCurAngleY < 180.0f) && (fNextAngleY >= 180.0f))
605 fNextAngleY = 180.0f;
607 else if ((fCurAngleY < 270.0f) && (fNextAngleY >= 270.0f))
609 fNextAngleY = 270.0f;
611 else if ((fCurAngleY < 360.0f) && (fNextAngleY >= 360.0f))
613 fNextAngleY = 360.0f;
615 float fSineY = sin(fCurAngleY/180.0f*PI), fCosY = cos(fCurAngleY/180.0f*PI);
616 float fNextSineY = sin(fNextAngleY/180.0f*PI), fNextCosY = cos(fNextAngleY/180.0f*PI);
617 glm::vec3 vDirY(fCosY, 0.0f, -fSineY), vNextDirY(fNextCosY, 0.0f, -fNextSineY);
618 float fCurAngleZ = 0.0f;
619 int iStepsZ = 1;
620 int xzIndex = 0;
621 if ((fCurAngleY >= 0) && (fCurAngleY < 90.0))
623 xzIndex = 0;
625 else if ((fCurAngleY >= 90) && (fCurAngleY < 180.0))
627 xzIndex = 1;
629 else if ((fCurAngleY >= 180) && (fCurAngleY < 270.0))
631 xzIndex = 2;
633 else if ((fCurAngleY >= 270) && (fCurAngleY < 360.0))
635 xzIndex = 3;
637 while(iStepsZ <= iSubDivZ)
639 int yIndex = 0;
640 float fNextAngleZ = fCurAngleZ+fAddAngleZ;
642 float fSineZ = sin(fCurAngleZ/180.0f*PI), fCosZ = cos(fCurAngleZ/180.0f*PI);
643 float fNextSineZ = sin(fNextAngleZ/180.0f*PI), fNextCosZ = cos(fNextAngleZ/180.0f*PI);
645 if ((fCurAngleZ < 90.0f) && (fNextAngleZ >= 90.0f))
647 fNextAngleZ = 90.0f;
650 if ((fCurAngleZ >= 0.0f) && (fCurAngleZ < 90.0f))
652 yIndex = 0;
654 else
656 yIndex = 1;
659 glm::vec3 vQuadPoints[] =
661 glm::vec3(vDirY.x*fSineZ*fRadius, fCosZ*fRadius, vDirY.z*fSineZ*fRadius),
662 glm::vec3(vDirY.x*fNextSineZ*fRadius, fNextCosZ*fRadius, vDirY.z*fNextSineZ*fRadius),
663 glm::vec3(vNextDirY.x*fNextSineZ*fRadius, fNextCosZ*fRadius, vNextDirY.z*fNextSineZ*fRadius),
664 glm::vec3(vNextDirY.x*fSineZ*fRadius, fCosZ*fRadius, vNextDirY.z*fSineZ*fRadius)
667 glm::vec3 vActualQuadPoints[] =
670 glm::vec3(vQuadPoints[0].x + xOffset[xzIndex], vQuadPoints[0].y + yOffset[yIndex], vQuadPoints[0].z + zOffset[xzIndex]),
671 glm::vec3(vQuadPoints[1].x + xOffset[xzIndex], vQuadPoints[1].y + yOffset[yIndex], vQuadPoints[1].z + zOffset[xzIndex]),
672 glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex]),
673 glm::vec3(vQuadPoints[3].x + xOffset[xzIndex], vQuadPoints[3].y + yOffset[yIndex], vQuadPoints[3].z + zOffset[xzIndex])
676 glm::vec3 vNormals[] =
678 glm::normalize(vQuadPoints[0]),
679 glm::normalize(vQuadPoints[1]),
680 glm::normalize(vQuadPoints[2]),
681 glm::normalize(vQuadPoints[3])
683 for (int i = 0; i < 6; i++)
685 int index = iIndices[i];
686 vertices.push_back(vActualQuadPoints[index]);
687 normals.push_back(vNormals[index]);
689 iFacesAdded += 2; // Keep count of added faces
690 if (((fCurAngleY < 90.0) && (fNextAngleY >= 90.0)) ||
691 ((fCurAngleY < 180.0) && (fNextAngleY >= 180.0)) ||
692 ((fCurAngleY < 270.0) && (fNextAngleY >= 270.0)) ||
693 ((fCurAngleY < 360.0) && (fNextAngleY >= 360.0)))
695 glm::vec3 vXZQuadNextPoints[] =
697 glm::vec3(vQuadPoints[3].x + xOffset[xzIndex], vQuadPoints[3].y + yOffset[yIndex], vQuadPoints[3].z + zOffset[xzIndex]),
698 glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex]),
699 glm::vec3(vQuadPoints[2].x + xOffset[xzIndex + 1], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex + 1]),
700 glm::vec3(vQuadPoints[3].x + xOffset[xzIndex + 1], vQuadPoints[3].y + yOffset[yIndex], vQuadPoints[3].z + zOffset[xzIndex + 1])
702 glm::vec3 vXZNextNormals[] =
704 glm::normalize(vQuadPoints[3]),
705 glm::normalize(vQuadPoints[2]),
706 glm::normalize(vQuadPoints[2]),
707 glm::normalize(vQuadPoints[3])
709 for (int i = 0; i < 6; i++)
711 int index = iIndices[i];
712 vertices.push_back(vXZQuadNextPoints[index]);
713 normals.push_back(vXZNextNormals[index]);
715 iFacesAdded += 2;
717 if ((fCurAngleZ < 90.0) && (fNextAngleZ >= 90.0))
719 glm::vec3 vYQuadNextPoints[] =
721 glm::vec3(vQuadPoints[1].x + xOffset[xzIndex], vQuadPoints[1].y + yOffset[yIndex], vQuadPoints[1].z + zOffset[xzIndex]),
722 glm::vec3(vQuadPoints[1].x + xOffset[xzIndex], vQuadPoints[1].y + yOffset[yIndex + 1], vQuadPoints[1].z + zOffset[xzIndex]),
723 glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex + 1], vQuadPoints[2].z + zOffset[xzIndex]),
724 glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex])
726 glm::vec3 vYNextNormals[] =
728 glm::normalize(vQuadPoints[1]),
729 glm::normalize(vQuadPoints[1]),
730 glm::normalize(vQuadPoints[2]),
731 glm::normalize(vQuadPoints[2])
733 for (int i = 0; i < 6; i++)
735 int index = iIndices[i];
736 vertices.push_back(vYQuadNextPoints[index]);
737 normals.push_back(vYNextNormals[index]);
739 iFacesAdded += 2;
741 iStepsZ++;
742 if ((iStepsZ > iSubDivZ) && (fCurAngleZ < 180.0f))
744 iStepsZ--;
746 fCurAngleZ = fNextAngleZ;
748 iStepsY++;
749 if ((iStepsY > iSubDivY) && (fCurAngleY < 360.0f))
751 iStepsY--;
753 fCurAngleY = fNextAngleY;
755 //draw the rectangle face
756 //top surface
757 glm::vec3 vTopPoints[] =
759 glm::vec3(xOffset[0], height / 2, zOffset[0]),
760 glm::vec3(xOffset[1], height / 2, zOffset[1]),
761 glm::vec3(xOffset[2], height / 2, zOffset[2]),
762 glm::vec3(xOffset[3], height / 2, zOffset[3])
764 glm::vec3 vTopNormal = glm::vec3(0.0f, 1.0f, 0.0f);
765 for (int i = 0; i < 6; i++)
767 int index = iIndices[i];
768 vertices.push_back(vTopPoints[index]);
769 normals.push_back(vTopNormal);
771 iFacesAdded += 2;
772 //bottom surface
773 glm::vec3 vBottomPoints[] =
775 glm::vec3(xOffset[3], -height / 2, zOffset[3]),
776 glm::vec3(xOffset[2], -height / 2, zOffset[2]),
777 glm::vec3(xOffset[1], -height / 2, zOffset[1]),
778 glm::vec3(xOffset[0], -height / 2, zOffset[0])
780 glm::vec3 vBottomNormal = glm::vec3(0.0f, -1.0f, 0.0f);
781 for (int i = 0; i < 6; i++)
783 int index = iIndices[i];
784 vertices.push_back(vBottomPoints[index]);
785 normals.push_back(vBottomNormal);
787 iFacesAdded += 2;
788 //left surface
789 glm::vec3 vLeftPoints[] =
791 glm::vec3(-width / 2, yOffset[0], zOffset[0]),
792 glm::vec3(-width / 2, yOffset[1], zOffset[1]),
793 glm::vec3(-width / 2, yOffset[1], zOffset[2]),
794 glm::vec3(-width / 2, yOffset[0], zOffset[3])
796 glm::vec3 vLeftNormal = glm::vec3(-1.0f, 0.0f, 0.0f);
797 for (int i = 0; i < 6; i++)
799 int index = iIndices[i];
800 vertices.push_back(vLeftPoints[index]);
801 normals.push_back(vLeftNormal);
803 //right surface
804 glm::vec3 vRightPoints[] =
806 glm::vec3(width / 2, yOffset[0], zOffset[3]),
807 glm::vec3(width / 2, yOffset[1], zOffset[2]),
808 glm::vec3(width / 2, yOffset[1], zOffset[1]),
809 glm::vec3(width / 2, yOffset[0], zOffset[0])
811 glm::vec3 vRightNormal = glm::vec3(1.0f, 0.0f, 0.0f);
812 for (int i = 0; i < 6; i++)
814 int index = iIndices[i];
815 vertices.push_back(vRightPoints[index]);
816 normals.push_back(vRightNormal);
818 iFacesAdded += 2;
819 //front surface
820 glm::vec3 vFrontPoints[] =
822 glm::vec3(xOffset[0], yOffset[0], depth / 2),
823 glm::vec3(xOffset[1], yOffset[0], depth / 2),
824 glm::vec3(xOffset[2], yOffset[1], depth / 2),
825 glm::vec3(xOffset[3], yOffset[1], depth / 2)
827 glm::vec3 vFrontNormal = glm::vec3(0.0f, 0.0f, 1.0f);
828 for (int i = 0; i < 6; i++)
830 int index = iIndices[i];
831 vertices.push_back(vFrontPoints[index]);
832 normals.push_back(vFrontNormal);
834 //back surface
835 glm::vec3 vBackPoints[] =
837 glm::vec3(xOffset[0], yOffset[1], -depth / 2),
838 glm::vec3(xOffset[1], yOffset[1], -depth / 2),
839 glm::vec3(xOffset[2], yOffset[0], -depth / 2),
840 glm::vec3(xOffset[3], yOffset[0], -depth / 2)
842 glm::vec3 vBackNormal = glm::vec3(0.0f, 0.0f, -1.0f);
843 for (int i = 0; i < 6; i++)
845 int index = iIndices[i];
846 vertices.push_back(vBackPoints[index]);
847 normals.push_back(vBackNormal);
849 iFacesAdded += 2;
850 return iFacesAdded;
853 void OpenGL3DRenderer::RenderLine3D(const Polygon3DInfo& polygon)
855 CHECK_GL_ERROR();
856 glUseProgram(maResources.m_CommonProID);
857 PosVecf3 trans = {0.0f, 0, 0.0};
858 PosVecf3 angle = {0.0f, 0.0f, 0.0f};
859 PosVecf3 scale = {1.0f, 1.0f, 1.0f};
860 MoveModelf(trans, angle, scale);
861 m_Model = m_GlobalScaleMatrix * m_Model;
862 m_3DMVP = m_3DProjection * m_3DView * m_Model;
864 for (size_t i = 0; i < polygon.verticesList.size(); i++)
866 //move the circle to the pos, and scale using the xScale and Y scale
867 Vertices3D *pointList = polygon.verticesList[i];
868 //if line only, using the common shader to render
870 //fill vertex buffer
871 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
872 glBufferData(GL_ARRAY_BUFFER, pointList->size() * sizeof(glm::vec3), &pointList[0][0], GL_STATIC_DRAW);
873 if(mbPickingMode)
874 glUniform4fv(maResources.m_2DColorID, 1, &polygon.id[0]);
875 else
876 glUniform4fv(maResources.m_2DColorID, 1, &polygon.polygonColor[0]);
877 glUniformMatrix4fv(maResources.m_MatrixID, 1, GL_FALSE, &m_3DMVP[0][0]);
878 CHECK_GL_ERROR();
880 // 1rst attribute buffer : vertices
881 glEnableVertexAttribArray(maResources.m_2DVertexID);
882 CHECK_GL_ERROR();
883 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
884 CHECK_GL_ERROR();
885 glVertexAttribPointer(maResources.m_2DVertexID, // attribute
886 3, // size
887 GL_FLOAT, // type
888 GL_FALSE, // normalized?
889 0, // stride
890 (void*)0 // array buffer offset
893 CHECK_GL_ERROR();
894 glLineWidth(polygon.lineWidth);
895 CHECK_GL_ERROR();
896 glDrawArrays(GL_LINE_STRIP, 0, pointList->size());
897 CHECK_GL_ERROR();
898 glDisableVertexAttribArray(maResources.m_2DVertexID);
899 CHECK_GL_ERROR();
900 glBindBuffer(GL_ARRAY_BUFFER, 0);
901 CHECK_GL_ERROR();
903 glUseProgram(0);
904 CHECK_GL_ERROR();
907 void OpenGL3DRenderer::RenderPolygon3D(const Polygon3DInfo& polygon)
909 CHECK_GL_ERROR();
910 size_t verticesNum = polygon.verticesList.size();
911 size_t normalsNum = polygon.normalsList.size();
912 //check whether the number of vertices and normals are equal
913 if (m_CameraInfo.cameraPos.z >= 0.0f)
914 glPolygonOffset(1.0, 1.0);
915 else
916 glPolygonOffset(-1.0, -1.0);
917 CHECK_GL_ERROR();
918 if (verticesNum != normalsNum)
920 return ;
922 glBindBuffer(GL_UNIFORM_BUFFER, 0);
923 CHECK_GL_ERROR();
924 if(mbPickingMode)
926 glUseProgram(maPickingResources.m_CommonProID);
927 float minCoordX = 0.0f;
928 float maxCoordX = m_fMinCoordX + m_fMaxCoordX;
929 glUniform1fv(maPickingResources.m_MinCoordXID, 1, &minCoordX);
930 glUniform1fv(maPickingResources.m_MaxCoordXID, 1, &maxCoordX);
932 else
934 glUseProgram(maResources.m_3DProID);
935 glUniformMatrix4fv(maResources.m_3DViewID, 1, GL_FALSE, &m_3DView[0][0]);
936 glUniformMatrix4fv(maResources.m_3DProjectionID, 1, GL_FALSE, &m_3DProjection[0][0]);
937 CHECK_GL_ERROR();
938 if (maResources.m_b330Support)
940 //update ubo
941 Update3DUniformBlock();
942 glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
943 glBufferSubData(GL_UNIFORM_BUFFER, m_3DActualSizeLight, sizeof(MaterialParameters), &polygon.material);
944 CHECK_GL_ERROR();
946 else
948 float minCoordX = 0.0f;
949 float maxCoordX = m_fMinCoordX + m_fMaxCoordX;
950 glUniform1fv(maResources.m_3DMinCoordXID, 1, &minCoordX);
951 glUniform1fv(maResources.m_3DMaxCoordXID, 1, &maxCoordX);
952 glUniform1i(maResources.m_3DUndrawID, m_bUndrawFlag);
953 CHECK_GL_ERROR();
954 //update light information
955 glUniform4fv(maResources.m_3DLightColorID, m_iLightNum, reinterpret_cast<GLfloat*>(m_LightColor));
956 glUniform4fv(maResources.m_3DLightPosID, m_iLightNum, reinterpret_cast<GLfloat*>(m_PositionWorldspace));
957 glUniform1fv(maResources.m_3DLightPowerID, m_iLightNum, m_fLightPower);
958 glUniform1i(maResources.m_3DLightNumID, m_iLightNum);
959 glUniform4fv(maResources.m_3DLightAmbientID, 1, &m_Ambient[0]);
960 CHECK_GL_ERROR();
961 //update meterial information
962 glUniform4fv(maResources.m_3DMaterialAmbientID, 1, &polygon.material.ambient[0]);
963 glUniform4fv(maResources.m_3DMaterialDiffuseID, 1, &polygon.material.diffuse[0]);
964 glUniform4fv(maResources.m_3DMaterialSpecularID, 1, &polygon.material.specular[0]);
965 glUniform4fv(maResources.m_3DMaterialColorID, 1, &polygon.material.materialColor[0]);
966 glUniform1i(maResources.m_3DMaterialTwoSidesID, polygon.material.twoSidesLighting);
967 glUniform1f(maResources.m_3DMaterialShininessID, polygon.material.shininess);
968 CHECK_GL_ERROR();
971 for (size_t i = 0; i < verticesNum; i++)
973 //move the circle to the pos, and scale using the xScale and Y scale
974 Vertices3D *pointList = polygon.verticesList[i];
975 Normals3D *normalList = polygon.normalsList[i];
976 PosVecf3 trans = {0.0f, 0.0f, 0.0};
977 PosVecf3 angle = {0.0f, 0.0f, 0.0f};
978 PosVecf3 scale = {1.0f, 1.0f, 1.0f};
979 MoveModelf(trans, angle, scale);
980 m_Model = m_GlobalScaleMatrix * m_Model;
981 glm::mat3 normalMatrix(m_Model);
982 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
984 //render to fbo
985 //fill vertex buffer
986 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
987 glBufferData(GL_ARRAY_BUFFER, pointList->size() * sizeof(glm::vec3), &pointList[0][0], GL_STATIC_DRAW);
988 CHECK_GL_ERROR();
990 if(!mbPickingMode)
992 //fill normal buffer
993 glBindBuffer(GL_ARRAY_BUFFER, m_NormalBuffer);
994 CHECK_GL_ERROR();
995 glBufferData(GL_ARRAY_BUFFER, normalList->size() * sizeof(glm::vec3), &normalList[0][0], GL_STATIC_DRAW);
996 CHECK_GL_ERROR();
997 glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
998 CHECK_GL_ERROR();
999 glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
1000 CHECK_GL_ERROR();
1002 else
1004 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
1005 glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
1006 glUniform4fv(maPickingResources.m_2DColorID, 1, &polygon.id[0]);
1008 CHECK_GL_ERROR();
1009 GLint maVertexID = mbPickingMode ? maPickingResources.m_2DVertexID : maResources.m_3DVertexID;
1010 // 1rst attribute buffer : vertices
1011 glEnableVertexAttribArray(maVertexID);
1012 CHECK_GL_ERROR();
1013 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
1014 CHECK_GL_ERROR();
1015 glVertexAttribPointer(maVertexID, // attribute
1016 3, // size
1017 GL_FLOAT, // type
1018 GL_FALSE, // normalized?
1019 0, // stride
1020 (void*)0 // array buffer offset
1022 CHECK_GL_ERROR();
1023 if(!mbPickingMode)
1025 // 2nd attribute buffer : normals
1026 glEnableVertexAttribArray(maResources.m_3DNormalID);
1027 CHECK_GL_ERROR();
1028 glBindBuffer(GL_ARRAY_BUFFER, m_NormalBuffer);
1029 CHECK_GL_ERROR();
1030 glVertexAttribPointer(maResources.m_3DNormalID, // attribute
1031 3, // size
1032 GL_FLOAT, // type
1033 GL_FALSE, // normalized?
1034 0, // stride
1035 (void*)0 // array buffer offset
1038 CHECK_GL_ERROR();
1039 glDrawArrays(GL_POLYGON, 0, pointList->size());
1040 CHECK_GL_ERROR();
1041 glDisableVertexAttribArray(maVertexID);
1042 CHECK_GL_ERROR();
1043 if(!mbPickingMode)
1044 glDisableVertexAttribArray(maResources.m_3DNormalID);
1046 CHECK_GL_ERROR();
1047 glBindBuffer(GL_ARRAY_BUFFER, 0);
1048 CHECK_GL_ERROR();
1050 glUseProgram(0);
1051 CHECK_GL_ERROR();
1054 namespace {
1056 template< typename T >
1057 struct DeletePointer
1059 void operator()(T* p)
1061 delete p;
1067 void OpenGL3DRenderer::ReleasePolygonShapes()
1069 for (size_t i = 0; i < m_Polygon3DInfoList.size(); i++)
1071 Polygon3DInfo &polygon = m_Polygon3DInfoList[i];
1072 std::for_each(polygon.verticesList.begin(),
1073 polygon.verticesList.end(), DeletePointer<Vertices3D>());
1074 std::for_each(polygon.normalsList.begin(),
1075 polygon.normalsList.end(), DeletePointer<Normals3D>());
1076 delete polygon.vertices;
1077 delete polygon.normals;
1079 m_Polygon3DInfoList.clear();
1082 void OpenGL3DRenderer::RenderPolygon3DObject()
1084 glDepthMask(GL_FALSE);
1085 CHECK_GL_ERROR();
1086 for (size_t i = 0; i < m_Polygon3DInfoList.size(); i++)
1088 Polygon3DInfo &polygon = m_Polygon3DInfoList[i];
1089 if (polygon.lineOnly || (!polygon.fillStyle))
1091 //just use the common shader is ok for lines
1092 RenderLine3D(polygon);
1093 CHECK_GL_ERROR();
1095 else
1097 RenderPolygon3D(polygon);
1100 glDepthMask(GL_TRUE);
1101 return;
1104 void OpenGL3DRenderer::Set3DSenceInfo(sal_uInt32 nColor, bool twoSidesLighting)
1106 m_Polygon3DInfo.material.twoSidesLighting = twoSidesLighting;
1107 if (maResources.m_b330Support)
1109 m_LightsInfo.ambient = getColorAsVector(nColor);
1110 m_LightsInfo.lightNum = 0;
1112 else
1114 m_iLightNum = 0;
1115 m_Ambient = getColorAsVector(nColor);;
1117 SetLightInfo(true, 0xFFFFFF, glm::vec4(-1.0, -1.0, 1.0, 0.0));
1118 SetLightInfo(true, 0xFFFFFF, glm::vec4(-1.0, 1.0, 1.0, 0.0));
1119 SetLightInfo(true, 0xFFFFFF, glm::vec4(1.0, -1.0, 1.0, 0.0));
1120 SetLightInfo(true, 0xFFFFFF, glm::vec4(1.0, 1.0, 1.0, 0.0));
1123 void OpenGL3DRenderer::SetLightInfo(bool lightOn, sal_uInt32 nColor, const glm::vec4& direction)
1125 if (lightOn)
1127 if (maResources.m_b330Support)
1129 if (m_LightsInfo.lightNum >= MAX_LIGHT_NUM)
1131 return;
1133 m_LightsInfo.light[m_LightsInfo.lightNum].lightColor = getColorAsVector(nColor);
1134 m_LightsInfo.light[m_LightsInfo.lightNum].positionWorldspace = direction;
1135 m_LightsInfo.light[m_LightsInfo.lightNum].lightPower = 0.3f;
1136 m_LightsInfo.lightNum++;
1138 else
1140 if (m_iLightNum >= MAX_LIGHT_NUM)
1142 return;
1144 m_LightColor[m_iLightNum] = getColorAsVector(nColor);
1145 m_PositionWorldspace[m_iLightNum] = direction;
1146 m_fLightPower[m_iLightNum] = 0.3f;
1147 m_iLightNum++;
1152 void OpenGL3DRenderer::AddShapePolygon3DObject(sal_uInt32 nColor, bool lineOnly, sal_uInt32 nLineColor,long fillStyle, sal_uInt32 specular, sal_uInt32 nUniqueId)
1154 m_Polygon3DInfo.polygonColor = getColorAsVector(nColor);
1155 m_Polygon3DInfo.id = getColorAsVector(nUniqueId);
1156 m_Polygon3DInfo.material.materialColor = m_Polygon3DInfo.polygonColor;//material color seems to be the same for all parts, so we use the polygon color
1157 //line or Polygon
1158 m_Polygon3DInfo.lineOnly = lineOnly;
1160 // if line only, use line color
1161 if (m_Polygon3DInfo.lineOnly)
1163 m_Polygon3DInfo.polygonColor = getColorAsVector(nLineColor);
1166 //fillStyle
1167 m_Polygon3DInfo.fillStyle= fillStyle;
1169 //material specular;
1170 m_Polygon3DInfo.material.specular = getColorAsVector(specular);
1172 m_Polygon3DInfo.material.diffuse = glm::vec4(1.0, 1.0, 1.0, 1.0);
1174 m_Polygon3DInfo.material.ambient = glm::vec4(0.2, 0.2, 0.2, 1.0);
1176 m_Polygon3DInfo.material.shininess = 1.0f;
1179 void OpenGL3DRenderer::EndAddShapePolygon3DObject()
1181 m_Polygon3DInfoList.push_back(m_Polygon3DInfo);
1182 m_Polygon3DInfo.normals = NULL;
1183 m_Polygon3DInfo.vertices = NULL;
1184 // TODO: moggi: memory leak???
1185 m_Polygon3DInfo.verticesList.clear();
1186 m_Polygon3DInfo.normalsList.clear();
1189 void OpenGL3DRenderer::AddPolygon3DObjectNormalPoint(float x, float y, float z)
1191 if (m_Polygon3DInfo.fillStyle)
1193 if (!m_Polygon3DInfo.normals)
1195 m_Polygon3DInfo.normals = new Normals3D;
1197 m_Polygon3DInfo.normals->push_back(glm::vec3(x, y, z));
1201 void OpenGL3DRenderer::EndAddPolygon3DObjectNormalPoint()
1203 m_Polygon3DInfo.normalsList.push_back(m_Polygon3DInfo.normals);
1204 m_Polygon3DInfo.normals = NULL;
1207 void OpenGL3DRenderer::AddPolygon3DObjectPoint(float x, float y, float z)
1209 if (!m_Polygon3DInfo.vertices)
1211 m_Polygon3DInfo.vertices = new Vertices3D;
1213 //float actualX = x - (float)m_iWidth / 2;
1214 //float actualY = y - (float)m_iHeight / 2;
1215 float actualX = x;
1216 float actualY = y;
1217 float actualZ = z;
1218 m_Polygon3DInfo.vertices->push_back(glm::vec3(actualX, actualY, actualZ));
1221 void OpenGL3DRenderer::EndAddPolygon3DObjectPoint()
1223 m_Polygon3DInfo.verticesList.push_back(m_Polygon3DInfo.vertices);
1224 m_Polygon3DInfo.vertices = NULL;
1227 void OpenGL3DRenderer::AddShape3DExtrudeObject(bool roundedCorner, sal_uInt32 nColor, sal_uInt32 specular, const glm::mat4& modelMatrix, sal_uInt32 nUniqueId)
1229 m_Extrude3DInfo.id = getColorAsVector(nUniqueId);
1230 m_Extrude3DInfo.orgID = nUniqueId;
1231 glm::vec4 tranform = modelMatrix * glm::vec4(0.0, 0.0, 0.0, 1.0);
1232 glm::vec4 DirX = modelMatrix * glm::vec4(1.0, 0.0, 0.0, 0.0);
1233 glm::vec4 DirY = modelMatrix * glm::vec4(0.0, 1.0, 0.0, 0.0);
1234 glm::vec4 DirZ = modelMatrix * glm::vec4(0.0, 0.0, 1.0, 0.0);
1235 m_Extrude3DInfo.xScale = glm::length(DirX);
1236 m_Extrude3DInfo.yScale = glm::length(DirY);
1237 m_Extrude3DInfo.zScale = glm::length(DirZ);
1238 glm::mat4 transformMatrixInverse = glm::inverse(glm::translate(glm::vec3(tranform)));
1239 glm::mat4 scaleMatrixInverse = glm::inverse(glm::scale(glm::vec3(m_Extrude3DInfo.xScale, m_Extrude3DInfo.yScale, m_Extrude3DInfo.zScale)));
1240 m_Extrude3DInfo.rotation = transformMatrixInverse * modelMatrix * scaleMatrixInverse;
1242 //color
1243 m_Extrude3DInfo.extrudeColor = getColorAsVector(nColor);
1244 m_Extrude3DInfo.material.materialColor = m_Extrude3DInfo.extrudeColor;//material color seems to be the same for all parts, so we use the polygon color
1246 //material specular;
1247 m_Extrude3DInfo.material.specular = getColorAsVector(specular);
1249 m_Extrude3DInfo.material.diffuse = glm::vec4(1.0, 1.0, 1.0, 1.0);
1251 m_Extrude3DInfo.material.ambient = glm::vec4(0.2, 0.2, 0.2, 1.0);
1253 m_Extrude3DInfo.material.shininess = 1.0f;
1254 m_Extrude3DInfo.xTransform = tranform.x;
1255 m_Extrude3DInfo.yTransform = tranform.y;
1256 m_Extrude3DInfo.zTransform = tranform.z;
1257 float width = 1.0f;
1258 float height = m_Extrude3DInfo.yScale / m_Extrude3DInfo.xScale;
1259 float radius = height > 0.2f ? 0.2f : height / 4.0f;
1260 float depth = 1 + 2 * radius;
1261 bool NORoundedCube = (radius > (width / 2)) || (radius > (height / 2)) || (radius > (depth / 2));
1262 if (!NORoundedCube && roundedCorner && (m_RoundBarMesh.iMeshSizes == 0))
1264 CreateActualRoundedCube(radius, CORNER_DIVION_Y, CORNER_DIVION_Z, width, height, depth);
1265 AddVertexData(m_CubeVertexBuf);
1266 AddNormalData(m_CubeNormalBuf);
1267 AddIndexData(m_CubeElementBuf);
1268 for (int j = 0; j < 5; j++)
1270 m_Extrude3DInfo.startIndex[j] = m_RoundBarMesh.iElementStartIndices[j];
1271 m_Extrude3DInfo.size[j] = m_RoundBarMesh.iElementSizes[j];
1273 m_Vertices.clear();
1274 m_Normals.clear();
1275 m_Indices.clear();
1276 m_Extrude3DInfo.rounded = true;
1278 m_Batchmaterial = m_Extrude3DInfo.material;
1281 void OpenGL3DRenderer::EndAddShape3DExtrudeObject()
1283 m_Extrude3DList.push_back(m_Extrude3DInfo);
1286 void OpenGL3DRenderer::Init3DUniformBlock()
1288 if(mbPickingMode)
1289 return;
1291 GLuint a3DLightBlockIndex = glGetUniformBlockIndex(maResources.m_3DProID, "GlobalLights");
1292 GLuint a3DMaterialBlockIndex = glGetUniformBlockIndex(maResources.m_3DProID, "GlobalMaterialParameters");
1294 if ((GL_INVALID_INDEX == a3DLightBlockIndex) || (GL_INVALID_INDEX == a3DMaterialBlockIndex))
1296 return;
1298 int nUniformBufferAlignSize = 1;
1299 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &nUniformBufferAlignSize);
1300 GLint nBlockDataSizeLight = 0, nBlockDataSizeMertrial = 0;
1301 glGetActiveUniformBlockiv(maResources.m_3DProID, a3DLightBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeLight);
1302 glGetActiveUniformBlockiv(maResources.m_3DProID, a3DMaterialBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeMertrial);
1303 CHECK_GL_ERROR();
1304 glGenBuffers(1, &m_3DUBOBuffer);
1305 glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
1306 CHECK_GL_ERROR();
1307 m_3DActualSizeLight = ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize;
1308 // cout << "nBlockDataSizeMertrial = " << nBlockDataSizeMertrial << ", nBlockDataSizeLight = " << nBlockDataSizeLight << ", m_3DActualSizeLight = " << m_3DActualSizeLight << endl;
1309 int dataSize = m_3DActualSizeLight + nBlockDataSizeMertrial;
1310 glBufferData(GL_UNIFORM_BUFFER, dataSize, NULL, GL_DYNAMIC_DRAW);
1311 glBindBufferRange(GL_UNIFORM_BUFFER, 0, m_3DUBOBuffer, 0, nBlockDataSizeLight);
1312 CHECK_GL_ERROR();
1313 glUniformBlockBinding(maResources.m_3DProID, a3DLightBlockIndex, 0);
1315 glBindBufferRange(GL_UNIFORM_BUFFER, 1, m_3DUBOBuffer, ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize, nBlockDataSizeMertrial);
1316 glUniformBlockBinding(maResources.m_3DProID, a3DMaterialBlockIndex, 1);
1317 //for the light source uniform, we must calc the offset of each element
1318 CHECK_GL_ERROR();
1319 glBindBuffer(GL_UNIFORM_BUFFER, 0);
1322 void OpenGL3DRenderer::InitBatch3DUniformBlock()
1324 if(mbPickingMode)
1325 return;
1327 GLuint a3DLightBlockIndex = glGetUniformBlockIndex(maResources.m_3DBatchProID, "GlobalLights");
1328 GLuint a3DMaterialBlockIndex = glGetUniformBlockIndex(maResources.m_3DBatchProID, "GlobalMaterialParameters");
1330 if ((GL_INVALID_INDEX == a3DLightBlockIndex) || (GL_INVALID_INDEX == a3DMaterialBlockIndex))
1332 return;
1334 int nUniformBufferAlignSize = 1;
1335 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &nUniformBufferAlignSize);
1336 GLint nBlockDataSizeLight = 0, nBlockDataSizeMertrial = 0;
1337 glGetActiveUniformBlockiv(maResources.m_3DBatchProID, a3DLightBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeLight);
1338 glGetActiveUniformBlockiv(maResources.m_3DBatchProID, a3DMaterialBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeMertrial);
1339 CHECK_GL_ERROR();
1340 glGenBuffers(1, &m_Batch3DUBOBuffer);
1341 glBindBuffer(GL_UNIFORM_BUFFER, m_Batch3DUBOBuffer);
1342 CHECK_GL_ERROR();
1343 m_Batch3DActualSizeLight = ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize;
1344 // cout << "nBlockDataSizeMertrial = " << nBlockDataSizeMertrial << ", nBlockDataSizeLight = " << nBlockDataSizeLight << ", m_3DActualSizeLight = " << m_3DActualSizeLight << endl;
1345 int dataSize = m_Batch3DActualSizeLight + nBlockDataSizeMertrial;
1346 glBufferData(GL_UNIFORM_BUFFER, dataSize, NULL, GL_DYNAMIC_DRAW);
1347 glBindBufferRange(GL_UNIFORM_BUFFER, 2, m_Batch3DUBOBuffer, 0, nBlockDataSizeLight);
1348 CHECK_GL_ERROR();
1349 glUniformBlockBinding(maResources.m_3DBatchProID, a3DLightBlockIndex, 2);
1351 glBindBufferRange(GL_UNIFORM_BUFFER, 3, m_Batch3DUBOBuffer, ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize, nBlockDataSizeMertrial);
1352 glUniformBlockBinding(maResources.m_3DBatchProID, a3DMaterialBlockIndex, 3);
1353 //for the light source uniform, we must calc the offset of each element
1354 CHECK_GL_ERROR();
1355 glBindBuffer(GL_UNIFORM_BUFFER, 0);
1358 void OpenGL3DRenderer::UpdateBatch3DUniformBlock()
1360 if(mbPickingMode)
1361 return;
1363 CHECK_GL_ERROR();
1364 glBindBuffer(GL_UNIFORM_BUFFER, m_Batch3DUBOBuffer);
1365 CHECK_GL_ERROR();
1366 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLint), &m_LightsInfo.lightNum);
1367 CHECK_GL_ERROR();
1368 //current std140 alignment: 16
1369 glBufferSubData(GL_UNIFORM_BUFFER, 16, sizeof(glm::vec4), &m_LightsInfo.ambient[0]);
1370 CHECK_GL_ERROR();
1371 //current std140 alignment: 16
1372 glBufferSubData(GL_UNIFORM_BUFFER, 32, sizeof(LightSource) * MAX_LIGHT_NUM, &m_LightsInfo.light);
1373 CHECK_GL_ERROR();
1374 glBindBuffer(GL_UNIFORM_BUFFER, 0);
1377 void OpenGL3DRenderer::Update3DUniformBlock()
1379 if(mbPickingMode)
1380 return;
1382 CHECK_GL_ERROR();
1383 glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
1384 CHECK_GL_ERROR();
1385 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLint), &m_LightsInfo.lightNum);
1386 CHECK_GL_ERROR();
1387 //current std140 alignment: 16
1388 glBufferSubData(GL_UNIFORM_BUFFER, 16, sizeof(glm::vec4), &m_LightsInfo.ambient[0]);
1389 CHECK_GL_ERROR();
1390 //current std140 alignment: 16
1391 glBufferSubData(GL_UNIFORM_BUFFER, 32, sizeof(LightSource) * MAX_LIGHT_NUM, &m_LightsInfo.light);
1392 CHECK_GL_ERROR();
1393 glBindBuffer(GL_UNIFORM_BUFFER, 0);
1396 void OpenGL3DRenderer::RenderExtrudeFlatSurface(const Extrude3DInfo& extrude3D, int surIndex)
1398 float xyScale = extrude3D.xScale;
1399 PosVecf3 trans = {extrude3D.xTransform,
1400 extrude3D.yTransform,
1401 extrude3D.zTransform};
1402 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
1403 glm::mat4 flatScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
1404 m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * aTranslationMatrix * extrude3D.rotation * flatScale;
1405 if(!mbPickingMode)
1407 glm::mat3 normalMatrix(m_Model);
1408 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
1409 glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
1410 glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
1412 else
1414 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
1415 glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
1416 glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
1417 glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
1420 glDrawElements(GL_TRIANGLES, extrude3D.size[surIndex], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[surIndex]));
1423 void OpenGL3DRenderer::RenderExtrudeBottomSurface(const Extrude3DInfo& extrude3D)
1425 float xyScale = extrude3D.xScale;
1426 float zScale = extrude3D.zScale;
1427 float actualZTrans = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
1428 PosVecf3 trans = {extrude3D.xTransform,
1429 extrude3D.yTransform,
1430 extrude3D.zTransform};
1431 //PosVecf3 angle = {0.0f, 0.0f, 0.0f};
1432 if (actualZTrans < 0.0f)
1434 // the height of rounded corner is higher than the cube than use the org scale matrix
1435 // yScale /= (float)(1 + BOTTOM_THRESHOLD);
1436 zScale /= (float)(m_RoundBarMesh.bottomThreshold);
1437 PosVecf3 scale = {xyScale, xyScale, zScale};
1438 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
1439 glm::mat4 aScaleMatrix = glm::scale(glm::vec3(scale.x, scale.y, scale.z));
1440 m_Model = aTranslationMatrix * extrude3D.rotation * aScaleMatrix;
1442 else
1444 glm::mat4 topTrans = glm::translate(glm::vec3(0.0, 0.0, actualZTrans));
1445 glm::mat4 topScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
1446 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
1447 m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale;
1449 m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
1450 if(!mbPickingMode)
1452 glm::mat3 normalMatrix(m_Model);
1453 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
1454 glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
1455 glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
1457 else
1459 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
1460 glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
1461 glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
1462 glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
1464 glDrawElements(GL_TRIANGLES, extrude3D.size[BOTTOM_SURFACE], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[BOTTOM_SURFACE]));
1467 void OpenGL3DRenderer::RenderExtrudeMiddleSurface(const Extrude3DInfo& extrude3D)
1469 float xyScale = extrude3D.xScale;
1470 float zScale = extrude3D.zScale;
1471 float actualZScale = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
1472 PosVecf3 trans = {extrude3D.xTransform,
1473 extrude3D.yTransform,
1474 extrude3D.zTransform};
1475 if (actualZScale < 0.0f)
1477 // the height of rounded corner is higher than the cube than use the org scale matrix
1478 // yScale /= (float)(1 + BOTTOM_THRESHOLD);
1479 // PosVecf3 scale = {xzScale, yScale, xzScale};
1480 // MoveModelf(trans, angle, scale);
1481 return ;
1483 else
1485 glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale,actualZScale));
1486 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
1487 m_Model = aTranslationMatrix * extrude3D.rotation * scale;
1490 if (extrude3D.reverse)
1492 glm::mat4 reverseMatrix = glm::translate(glm::vec3(0.0, 0.0, -1.0));
1493 m_Model = m_Model * reverseMatrix;
1495 m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
1496 if(!mbPickingMode)
1498 glm::mat3 normalMatrix(m_Model);
1499 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
1500 glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
1501 glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
1503 else
1505 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
1506 glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
1507 glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
1508 glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
1510 glDrawElements(GL_TRIANGLES, extrude3D.size[MIDDLE_SURFACE], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[MIDDLE_SURFACE]));
1513 void OpenGL3DRenderer::RenderExtrudeTopSurface(const Extrude3DInfo& extrude3D)
1515 float xyScale = extrude3D.xScale;
1516 float zScale = extrude3D.zScale;
1517 float actualZTrans = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
1518 PosVecf3 trans = {extrude3D.xTransform,
1519 extrude3D.yTransform,
1520 extrude3D.zTransform};
1521 if (actualZTrans < 0.0f)
1523 // the height of rounded corner is higher than the cube than use the org scale matrix
1524 //yScale /= (float)(1 + BOTTOM_THRESHOLD);
1525 zScale /= (float)(m_RoundBarMesh.bottomThreshold);
1526 glm::mat4 orgTrans = glm::translate(glm::vec3(0.0, 0.0, -1.0));
1527 glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale, zScale));
1528 //MoveModelf(trans, angle, scale);
1529 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
1530 m_Model = aTranslationMatrix * extrude3D.rotation * scale * orgTrans;
1532 else
1534 // use different matrices for different parts
1535 glm::mat4 orgTrans = glm::translate(glm::vec3(0.0, 0.0, -1.0));
1536 glm::mat4 topTrans = glm::translate(glm::vec3(0.0, 0.0, actualZTrans));
1537 glm::mat4 topScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
1538 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
1539 m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale * orgTrans;
1541 m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
1542 if(!mbPickingMode)
1544 glm::mat3 normalMatrix(m_Model);
1545 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
1546 glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
1547 glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
1549 else
1551 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
1552 glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
1553 glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
1554 glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
1556 glDrawElements(GL_TRIANGLES, extrude3D.size[TOP_SURFACE], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[TOP_SURFACE]));
1559 void OpenGL3DRenderer::RenderNonRoundedBar(const Extrude3DInfo& extrude3D)
1561 float xScale = extrude3D.xScale;
1562 float yScale = extrude3D.yScale;
1563 float zScale = extrude3D.zScale;
1564 glm::mat4 transformMatrix = glm::translate(glm::vec3(extrude3D.xTransform, extrude3D.yTransform, extrude3D.zTransform));
1565 glm::mat4 scaleMatrix = glm::scale(glm::vec3(xScale, yScale, zScale));
1566 m_Model = transformMatrix * extrude3D.rotation * scaleMatrix;
1567 if (extrude3D.reverse)
1569 glm::mat4 reverseMatrix = glm::translate(glm::vec3(0.0, 0.0, -1.0));
1570 m_Model = m_Model * reverseMatrix;
1572 m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
1573 if(!mbPickingMode)
1575 glm::mat3 normalMatrix(m_Model);
1576 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
1577 glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
1578 glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
1580 else
1582 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
1583 glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
1584 glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
1585 glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
1587 glDrawArrays(GL_TRIANGLES, 0, 36);
1590 void OpenGL3DRenderer::RenderExtrudeSurface(const Extrude3DInfo& extrude3D)
1592 RenderExtrudeMiddleSurface(extrude3D);
1593 // check reverse flag to decide whether to render the top middle
1594 if (extrude3D.reverse)
1596 RenderExtrudeBottomSurface(extrude3D);
1597 RenderExtrudeFlatSurface(extrude3D, FLAT_TOP_SURFACE);
1599 else
1601 RenderExtrudeTopSurface(extrude3D);
1602 RenderExtrudeFlatSurface(extrude3D, FLAT_BOTTOM_SURFACE);
1605 void OpenGL3DRenderer::ReleaseExtrude3DShapes()
1607 m_Extrude3DList.clear();
1610 void OpenGL3DRenderer::RenderExtrude3DObject()
1612 CHECK_GL_ERROR();
1613 glEnable(GL_DEPTH_TEST);
1614 glEnable(GL_CULL_FACE);
1615 glCullFace(GL_BACK);
1616 CHECK_GL_ERROR();
1617 if(mbPickingMode)
1619 glUseProgram(maPickingResources.m_CommonProID);
1620 glUniform1fv(maPickingResources.m_MinCoordXID, 1, &m_fMinCoordX);
1621 glUniform1fv(maPickingResources.m_MaxCoordXID, 1, &m_fMaxCoordX);
1623 else
1625 glUseProgram(maResources.m_3DProID);
1626 glUniformMatrix4fv(maResources.m_3DViewID, 1, GL_FALSE, &m_3DView[0][0]);
1627 glUniformMatrix4fv(maResources.m_3DProjectionID, 1, GL_FALSE, &m_3DProjection[0][0]);
1628 CHECK_GL_ERROR();
1629 if (maResources.m_b330Support)
1631 //update ubo
1632 Update3DUniformBlock();
1633 CHECK_GL_ERROR();
1635 else
1637 glUniform1fv(maResources.m_3DMinCoordXID, 1, &m_fMinCoordX);
1638 glUniform1fv(maResources.m_3DMaxCoordXID, 1, &m_fMaxCoordX);
1639 glUniform1i(maResources.m_3DUndrawID, m_bUndrawFlag);
1640 //update light information
1641 glUniform4fv(maResources.m_3DLightColorID, m_iLightNum, reinterpret_cast<GLfloat*>(m_LightColor));
1642 glUniform4fv(maResources.m_3DLightPosID, m_iLightNum, reinterpret_cast<GLfloat*>(m_PositionWorldspace));
1643 glUniform1fv(maResources.m_3DLightPowerID, m_iLightNum, m_fLightPower);
1644 glUniform1i(maResources.m_3DLightNumID, m_iLightNum);
1645 glUniform4fv(maResources.m_3DLightAmbientID, 1, &m_Ambient[0]);
1646 CHECK_GL_ERROR();
1649 size_t extrude3DNum = m_Extrude3DList.size();
1650 for (size_t i = 0; i < extrude3DNum; i++)
1652 Extrude3DInfo extrude3DInfo = m_Extrude3DList[i];
1653 GLuint vertexBuf = extrude3DInfo.rounded ? m_CubeVertexBuf : m_BoundBox;
1654 GLuint normalBuf = extrude3DInfo.rounded ? m_CubeNormalBuf : m_BoundBoxNormal;
1656 if(mbPickingMode)
1657 glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3DInfo.id[0]);
1658 // 1st attribute buffer : vertices
1660 GLint aVertexID = mbPickingMode ? maPickingResources.m_2DVertexID : maResources.m_3DVertexID;
1661 glEnableVertexAttribArray(aVertexID);
1662 glBindBuffer(GL_ARRAY_BUFFER, vertexBuf);
1663 glVertexAttribPointer(aVertexID, // attribute
1664 3, // size
1665 GL_FLOAT, // type
1666 GL_FALSE, // normalized?
1667 0, // stride
1668 (void*)0 // array buffer offset
1671 if(!mbPickingMode)
1673 // 2nd attribute buffer : normals
1674 glEnableVertexAttribArray(maResources.m_3DNormalID);
1675 glBindBuffer(GL_ARRAY_BUFFER, normalBuf);
1676 glVertexAttribPointer(maResources.m_3DNormalID, // attribute
1677 3, // size
1678 GL_FLOAT, // type
1679 GL_FALSE, // normalized?
1680 0, // stride
1681 (void*)0 // array buffer offset
1684 if(!mbPickingMode)
1686 if (maResources.m_b330Support)
1688 glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
1689 glBufferSubData(GL_UNIFORM_BUFFER, m_3DActualSizeLight, sizeof(MaterialParameters), &extrude3DInfo.material);
1690 CHECK_GL_ERROR();
1691 glBindBuffer(GL_UNIFORM_BUFFER, 0);
1693 else
1695 //update meterial information
1696 glUniform4fv(maResources.m_3DMaterialAmbientID, 1, &extrude3DInfo.material.ambient[0]);
1697 glUniform4fv(maResources.m_3DMaterialDiffuseID, 1, &extrude3DInfo.material.diffuse[0]);
1698 glUniform4fv(maResources.m_3DMaterialSpecularID, 1, &extrude3DInfo.material.specular[0]);
1699 glUniform4fv(maResources.m_3DMaterialColorID, 1, &extrude3DInfo.material.materialColor[0]);
1700 glUniform1i(maResources.m_3DMaterialTwoSidesID, extrude3DInfo.material.twoSidesLighting);
1701 glUniform1f(maResources.m_3DMaterialShininessID, extrude3DInfo.material.shininess);
1704 extrude3DInfo.reverse = 0;
1705 if (extrude3DInfo.rounded)
1707 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CubeElementBuf);
1708 RenderExtrudeSurface(extrude3DInfo);
1709 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1711 else
1713 RenderNonRoundedBar(extrude3DInfo);
1715 glDisableVertexAttribArray(aVertexID);
1716 if(!mbPickingMode)
1717 glDisableVertexAttribArray(maResources.m_3DNormalID);
1719 glUseProgram(0);
1720 glBindBuffer(GL_ARRAY_BUFFER, 0);
1721 glDisable(GL_CULL_FACE);
1724 void OpenGL3DRenderer::CreateScreenTextTexture(
1725 const boost::shared_array<sal_uInt8> &bitmapBuf,
1726 const ::Size& rSizePixels, const glm::vec2& vTopLeft,
1727 const glm::vec2& vBottomRight, const glm::vec3& vPos,
1728 const glm::vec4& vScreenTextColor, sal_uInt32 nUniqueId)
1730 long bmpWidth = rSizePixels.Width();
1731 long bmpHeight = rSizePixels.Height();
1733 TextInfo aTextInfo;
1734 aTextInfo.id = getColorAsVector(nUniqueId);
1735 aTextInfo.uniqueId = nUniqueId;
1736 aTextInfo.vertex[0] = vBottomRight.x;
1737 aTextInfo.vertex[1] = vBottomRight.y;
1738 aTextInfo.vertex[2] = 0;
1740 aTextInfo.vertex[3] = vBottomRight.x;
1741 aTextInfo.vertex[4] = vTopLeft.y;
1742 aTextInfo.vertex[5] = 0;
1744 aTextInfo.vertex[6] = vTopLeft.x;
1745 aTextInfo.vertex[7] = vTopLeft.y;
1746 aTextInfo.vertex[8] = 0;
1748 aTextInfo.vertex[9] = vTopLeft.x;
1749 aTextInfo.vertex[10] = vBottomRight.y;
1750 aTextInfo.vertex[11] = 0;
1751 aTextInfo.pos = vPos;
1752 aTextInfo.textColor = vScreenTextColor;
1754 CHECK_GL_ERROR();
1755 glGenTextures(1, &aTextInfo.texture);
1756 CHECK_GL_ERROR();
1757 glBindTexture(GL_TEXTURE_2D, aTextInfo.texture);
1758 CHECK_GL_ERROR();
1759 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1760 CHECK_GL_ERROR();
1761 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1762 CHECK_GL_ERROR();
1763 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1764 CHECK_GL_ERROR();
1765 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1766 CHECK_GL_ERROR();
1767 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmpWidth, bmpHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapBuf.get());
1768 CHECK_GL_ERROR();
1769 glBindTexture(GL_TEXTURE_2D, 0);
1770 CHECK_GL_ERROR();
1771 m_ScreenTextInfoList.push_back(aTextInfo);
1772 m_ScreenTexturelist.push_back(aTextInfo.texture);
1775 void OpenGL3DRenderer::CreateTextTextureSingle(const boost::shared_array<sal_uInt8> &bitmapBuf,
1776 const ::Size& rSizePixels,
1777 const glm::vec3& vTopLeft, const glm::vec3& vTopRight,
1778 const glm::vec3& vBottomRight, const glm::vec3& vBottomLeft,
1779 sal_uInt32 nUniqueId)
1781 long bmpWidth = rSizePixels.Width();
1782 long bmpHeight = rSizePixels.Height();
1784 TextInfo aTextInfo;
1785 aTextInfo.id = getColorAsVector(nUniqueId);
1786 aTextInfo.vertex[0] = vBottomRight.x;
1787 aTextInfo.vertex[1] = vBottomRight.y;
1788 aTextInfo.vertex[2] = vBottomRight.z;
1790 aTextInfo.vertex[3] = vTopRight.x;
1791 aTextInfo.vertex[4] = vTopRight.y;
1792 aTextInfo.vertex[5] = vTopRight.z;
1794 aTextInfo.vertex[9] = vBottomLeft.x;
1795 aTextInfo.vertex[10] = vBottomLeft.y;
1796 aTextInfo.vertex[11] = vBottomLeft.z;
1798 aTextInfo.vertex[6] = vTopLeft.x;
1799 aTextInfo.vertex[7] = vTopLeft.y;
1800 aTextInfo.vertex[8] = vTopLeft.z;
1802 CHECK_GL_ERROR();
1803 glGenTextures(1, &aTextInfo.texture);
1804 CHECK_GL_ERROR();
1805 glBindTexture(GL_TEXTURE_2D, aTextInfo.texture);
1806 CHECK_GL_ERROR();
1807 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1808 CHECK_GL_ERROR();
1809 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1810 CHECK_GL_ERROR();
1811 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmpWidth, bmpHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapBuf.get());
1812 CHECK_GL_ERROR();
1813 glBindTexture(GL_TEXTURE_2D, 0);
1814 CHECK_GL_ERROR();
1815 m_TextInfoList.push_back(aTextInfo);
1816 m_Texturelist.push_back(aTextInfo.texture);
1820 void OpenGL3DRenderer::CreateTextTextureBatch(const boost::shared_array<sal_uInt8> &bitmapBuf,
1821 const ::Size& rSizePixels,
1822 const glm::vec3& vTopLeft, const glm::vec3& vTopRight,
1823 const glm::vec3& vBottomRight, const glm::vec3& vBottomLeft,
1824 sal_uInt32 nUniqueId)
1826 long bmpWidth = rSizePixels.Width();
1827 long bmpHeight = rSizePixels.Height();
1828 glm::vec4 id = getColorAsVector(nUniqueId);
1829 m_TextInfoBatch.idList.push_back(id);
1830 m_TextInfoBatch.vertexList.push_back(glm::vec3(vBottomRight.x, vBottomRight.y, vBottomRight.z));
1831 m_TextInfoBatch.vertexList.push_back(glm::vec3(vTopRight.x, vTopRight.y, vTopRight.z));
1832 m_TextInfoBatch.vertexList.push_back(glm::vec3(vTopLeft.x, vTopLeft.y, vTopLeft.z));
1833 m_TextInfoBatch.vertexList.push_back(glm::vec3(vBottomLeft.x, vBottomLeft.y, vBottomLeft.z));
1834 //find the last vector, which size is small than default batch number;
1835 size_t index = 0;
1836 while ((m_TextInfoBatch.texture.size() > 0) &&
1837 (m_TextInfoBatch.texture[index].subTextureNum >= m_TextInfoBatch.batchNum) &&
1838 (index < m_TextInfoBatch.texture.size() - 1))
1840 index++;
1842 //if the sub texture number of the last texture array reach the largest, create a new textur array
1843 if ((m_TextInfoBatch.texture.size() == 0) ||
1844 (m_TextInfoBatch.texture[index].subTextureNum >= m_TextInfoBatch.batchNum))
1846 TextureArrayInfo textureArray;
1847 glGenTextures(1, &textureArray.textureID);
1848 CHECK_GL_ERROR();
1849 glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray.textureID);
1850 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1851 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1852 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1853 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1854 CHECK_GL_ERROR();
1855 textureArray.textureArrayWidth = bmpHeight * 8;
1856 textureArray.textureArrayHeight = bmpHeight;
1857 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGB, textureArray.textureArrayWidth, textureArray.textureArrayHeight,
1858 m_TextInfoBatch.batchNum, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
1859 CHECK_GL_ERROR();
1860 if (m_TextInfoBatch.texture.size() > 0)
1862 index++;
1864 m_TextInfoBatch.texture.push_back(textureArray);
1865 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
1867 glBindTexture(GL_TEXTURE_2D_ARRAY, m_TextInfoBatch.texture[index].textureID);
1868 CHECK_GL_ERROR();
1869 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, m_TextInfoBatch.texture[index].subTextureNum, bmpWidth, bmpHeight, 1, GL_RGB, GL_UNSIGNED_BYTE, bitmapBuf.get());
1870 CHECK_GL_ERROR();
1871 //calc texture coordinate
1872 m_TextInfoBatch.textureCoordList.push_back(glm::vec3((float)bmpWidth / (float)m_TextInfoBatch.texture[index].textureArrayWidth,
1874 m_TextInfoBatch.texture[index].subTextureNum));
1875 m_TextInfoBatch.textureCoordList.push_back(glm::vec3((float)bmpWidth / (float)m_TextInfoBatch.texture[index].textureArrayWidth,
1876 (float)bmpHeight/ (float)m_TextInfoBatch.texture[index].textureArrayHeight,
1877 m_TextInfoBatch.texture[index].subTextureNum));
1878 m_TextInfoBatch.textureCoordList.push_back(glm::vec3(0,
1879 (float)bmpHeight/ (float)m_TextInfoBatch.texture[index].textureArrayHeight,
1880 m_TextInfoBatch.texture[index].subTextureNum));
1881 m_TextInfoBatch.textureCoordList.push_back(glm::vec3(0,
1883 m_TextInfoBatch.texture[index].subTextureNum));
1884 m_TextInfoBatch.texture[index].subTextureNum++;
1885 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
1888 void OpenGL3DRenderer::CreateTextTexture(const boost::shared_array<sal_uInt8> &bitmapBuf,
1889 const ::Size& rSizePixels,
1890 const glm::vec3& vTopLeft, const glm::vec3& vTopRight,
1891 const glm::vec3& vBottomRight, const glm::vec3& vBottomLeft,
1892 sal_uInt32 nUniqueId)
1894 if (maResources.mbTexBatchSupport)
1896 CreateTextTextureBatch(bitmapBuf, rSizePixels, vTopLeft, vTopRight, vBottomRight, vBottomLeft, nUniqueId);
1898 else
1900 CreateTextTextureSingle(bitmapBuf, rSizePixels, vTopLeft, vTopRight, vBottomRight, vBottomLeft, nUniqueId);
1904 void OpenGL3DRenderer::ReleaseTextShapes()
1906 m_TextInfoList.clear();
1909 void OpenGL3DRenderer::ReleaseTextTexture()
1911 for (size_t i = 0; i < m_Texturelist.size(); i++)
1913 glDeleteTextures(1, &m_Texturelist[i]);
1915 m_Texturelist.clear();
1918 void OpenGL3DRenderer::ReleaseScreenTextShapes()
1920 m_ScreenTextInfoList.clear();
1923 void OpenGL3DRenderer::ReleaseScreenTextTexture()
1925 for (size_t i = 0; i < m_ScreenTexturelist.size(); i++)
1927 glDeleteTextures(1, &m_ScreenTexturelist[i]);
1929 m_ScreenTexturelist.clear();
1933 void OpenGL3DRenderer::RenderScreenTextShape()
1935 if (mbPickingMode)
1936 return;
1937 glUseProgram(maResources.m_ScreenTextProID);
1938 CHECK_GL_ERROR();
1939 for (size_t i = 0; i < m_ScreenTextInfoList.size(); i++)
1941 TextInfo textInfo = m_ScreenTextInfoList[i];
1942 //calc the position and check whether it can be displayed
1943 if (textInfo.uniqueId)
1945 glm::vec3 worldPos = glm::vec3(m_ScrollMoveMatrix * m_GlobalScaleMatrix * glm::vec4(textInfo.pos, 1));
1946 if ((worldPos.x < m_fMinCoordX) || (worldPos.x > m_fMaxCoordX))
1947 continue;
1948 glm::vec4 pos = m_3DProjection * m_3DView * glm::vec4(worldPos, 1);
1949 const float xTrans = pos.x / pos.w;
1950 const float yTrans = pos.y / pos.w;
1951 for (int j = 0; j < 12; j++)
1953 if (j % 3 == 0)
1955 textInfo.vertex[j] += xTrans;
1957 if (j % 3 == 1)
1959 textInfo.vertex[j] += yTrans;
1963 glUniform4fv(maResources.m_ScreenTextColorID, 1, &textInfo.textColor[0]);
1964 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
1965 CHECK_GL_ERROR();
1966 glBufferData(GL_ARRAY_BUFFER, sizeof(textInfo.vertex), textInfo.vertex, GL_STATIC_DRAW);
1967 CHECK_GL_ERROR();
1969 // 1rst attribute buffer : vertices
1970 glEnableVertexAttribArray(maResources.m_ScreenTextVertexID);
1971 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
1972 glVertexAttribPointer(
1973 maResources.m_ScreenTextVertexID,
1974 3, // size
1975 GL_FLOAT, // type
1976 GL_FALSE, // normalized?
1977 0, // stride
1978 (void*)0 // array buffer offset
1980 //tex coord
1981 CHECK_GL_ERROR();
1982 glEnableVertexAttribArray(maResources.m_ScreenTextTexCoordID);
1983 glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBuf);
1984 glVertexAttribPointer(
1985 maResources.m_ScreenTextTexCoordID,
1986 2, // size
1987 GL_FLOAT, // type
1988 GL_FALSE, // normalized?
1989 0, // stride
1990 (void*)0 // array buffer offset
1992 //texture
1993 CHECK_GL_ERROR();
1994 glBindTexture(GL_TEXTURE_2D, textInfo.texture);
1995 CHECK_GL_ERROR();
1996 glUniform1i(maResources.m_ScreenTextTexID, 0);
1997 CHECK_GL_ERROR();
1998 //TODO: moggi: get rid fo GL_QUADS
1999 glDrawArrays(GL_QUADS, 0, 4);
2000 CHECK_GL_ERROR();
2002 glDisableVertexAttribArray(maResources.m_ScreenTextTexCoordID);
2003 CHECK_GL_ERROR();
2004 glDisableVertexAttribArray(maResources.m_ScreenTextVertexID);
2005 CHECK_GL_ERROR();
2006 glBindTexture(GL_TEXTURE_2D, 0);
2007 glUseProgram(0);
2008 CHECK_GL_ERROR();
2010 void OpenGL3DRenderer::ReleaseTextShapesBatch()
2012 for (size_t i = 0; i < m_TextInfoBatch.texture.size(); i++)
2014 m_TextInfoBatch.texture[i].subTextureNum = 0;
2016 m_TextInfoBatch.vertexList.clear();
2017 m_TextInfoBatch.textureCoordList.clear();
2018 m_TextInfoBatch.idList.clear();
2021 void OpenGL3DRenderer::RenderTextShapeBatch()
2023 glm::mat4 aMVP = m_3DProjection * m_3DView * m_GlobalScaleMatrix;
2024 glUseProgram(maResources.m_BatchTextProID);
2025 CHECK_GL_ERROR();
2026 glUniformMatrix4fv(maResources.m_BatchTextMatrixID, 1, GL_FALSE, &aMVP[0][0]);
2027 glEnableVertexAttribArray(maResources.m_BatchTextVertexID);
2028 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
2029 glVertexAttribPointer(
2030 maResources.m_BatchTextVertexID,
2031 3, // size
2032 GL_FLOAT, // type
2033 GL_FALSE, // normalized?
2034 0, // stride
2035 (void*)0 // array buffer offset
2037 //tex coord
2038 CHECK_GL_ERROR();
2039 glEnableVertexAttribArray(maResources.m_BatchTextTexCoordID);
2040 glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBufBatch);
2041 CHECK_GL_ERROR();
2042 glVertexAttribPointer(
2043 maResources.m_BatchTextTexCoordID,
2044 3, // size
2045 GL_FLOAT, // type
2046 GL_FALSE, // normalized?
2047 0, // stride
2048 (void*)0 // array buffer offset
2050 //use texture array to get the vertex
2051 for (size_t i = 0; i < m_TextInfoBatch.texture.size(); i++)
2053 int vertexNum = m_TextInfoBatch.texture[i].subTextureNum;
2054 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
2055 CHECK_GL_ERROR();
2056 glBufferData(GL_ARRAY_BUFFER, 4 * vertexNum * sizeof(glm::vec3), &m_TextInfoBatch.vertexList[4 * i * m_TextInfoBatch.batchNum], GL_STATIC_DRAW);
2057 glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBufBatch);
2058 CHECK_GL_ERROR();
2059 glBufferData(GL_ARRAY_BUFFER, 4 * vertexNum * sizeof(glm::vec3), &m_TextInfoBatch.textureCoordList[4 * i * m_TextInfoBatch.batchNum], GL_STATIC_DRAW);
2060 glBindTexture(GL_TEXTURE_2D_ARRAY, m_TextInfoBatch.texture[i].textureID);
2061 CHECK_GL_ERROR();
2062 glUniform1i(maResources.m_BatchTextTexID, 0);
2063 CHECK_GL_ERROR();
2064 //TODO: moggi: get rid fo GL_QUADS
2065 glDrawArrays(GL_QUADS, 0, 4 * vertexNum);
2067 glDisableVertexAttribArray(maResources.m_BatchTextVertexID);
2068 CHECK_GL_ERROR();
2069 glDisableVertexAttribArray(maResources.m_BatchTextTexCoordID);
2070 CHECK_GL_ERROR();
2071 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
2072 CHECK_GL_ERROR();
2073 glUseProgram(0);
2075 void OpenGL3DRenderer::RenderTextShape()
2077 CHECK_GL_ERROR();
2078 for (size_t i = 0; i < m_TextInfoList.size(); i++)
2080 TextInfo &textInfo = m_TextInfoList[i];
2081 PosVecf3 trans = {0, 0, 0};
2082 PosVecf3 angle = {0.0f, 0.0f, 0.0f};
2083 PosVecf3 scale = {1.0, 1.0, 1.0f};
2084 MoveModelf(trans, angle, scale);
2085 m_Model = m_GlobalScaleMatrix * m_Model;
2086 glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
2087 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
2088 CHECK_GL_ERROR();
2089 glBufferData(GL_ARRAY_BUFFER, sizeof(textInfo.vertex), textInfo.vertex, GL_STATIC_DRAW);
2090 CHECK_GL_ERROR();
2091 glUseProgram(maResources.m_TextProID);
2093 CHECK_GL_ERROR();
2094 glUniformMatrix4fv(maResources.m_TextMatrixID, 1, GL_FALSE, &aMVP[0][0]);
2095 // 1rst attribute buffer : vertices
2096 glEnableVertexAttribArray(maResources.m_TextVertexID);
2097 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
2098 glVertexAttribPointer(
2099 maResources.m_TextVertexID,
2100 3, // size
2101 GL_FLOAT, // type
2102 GL_FALSE, // normalized?
2103 0, // stride
2104 (void*)0 // array buffer offset
2106 //tex coord
2107 CHECK_GL_ERROR();
2108 glEnableVertexAttribArray(maResources.m_TextTexCoordID);
2109 glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBuf);
2110 glVertexAttribPointer(
2111 maResources.m_TextTexCoordID,
2112 2, // size
2113 GL_FLOAT, // type
2114 GL_FALSE, // normalized?
2115 0, // stride
2116 (void*)0 // array buffer offset
2118 //texture
2119 CHECK_GL_ERROR();
2120 glBindTexture(GL_TEXTURE_2D, textInfo.texture);
2121 CHECK_GL_ERROR();
2122 glUniform1i(maResources.m_TextTexID, 0);
2123 CHECK_GL_ERROR();
2124 //TODO: moggi: get rid fo GL_QUADS
2125 glDrawArrays(GL_QUADS, 0, 4);
2126 CHECK_GL_ERROR();
2127 glDisableVertexAttribArray(maResources.m_TextTexCoordID);
2128 CHECK_GL_ERROR();
2129 glDisableVertexAttribArray(maResources.m_TextVertexID);
2130 CHECK_GL_ERROR();
2131 glBindTexture(GL_TEXTURE_2D, 0);
2132 glUseProgram(0);
2134 CHECK_GL_ERROR();
2137 void OpenGL3DRenderer::CreateSceneBoxView()
2139 m_CameraInfo.cameraPos = glm::vec3(m_GlobalScaleMatrix * glm::vec4(m_CameraInfo.cameraPos, 1.0));
2140 m_CameraInfo.cameraOrg = glm::vec3(m_GlobalScaleMatrix * glm::vec4(m_CameraInfo.cameraOrg, 1.0));
2141 m_3DView = glm::lookAt(m_CameraInfo.cameraPos,
2142 m_CameraInfo.cameraOrg,
2143 m_CameraInfo.cameraUp);
2144 m_3DView = m_3DView + m_matDiff;
2147 void OpenGL3DRenderer::AddMatrixDiff(const glm::mat4& aMat)
2149 m_matDiff = m_matDiff + aMat;
2152 void OpenGL3DRenderer::ResetMatrixDiff()
2154 m_matDiff = glm::mat4(0.0);
2157 void OpenGL3DRenderer::ClearBuffer()
2159 CHECK_GL_ERROR();
2160 glDisable(GL_DEPTH_TEST);
2161 CHECK_GL_ERROR();
2163 #if defined(IOS) || defined(ANDROID)
2164 glClearDepthf(1.0f);
2165 #else
2166 glClearDepth(1.0f);
2167 #endif
2168 CHECK_GL_ERROR();
2169 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2170 CHECK_GL_ERROR();
2173 * TODO: moggi: use a shader!!!
2174 glBegin (GL_QUADS);
2175 glColor3f (0.3f, 0.3f, 0.3f);
2176 glVertex3f (-1.0f, -1.0f, -1.0f);
2177 glVertex3f (1.0f, -1.0f, -1.0f);
2179 glColor3f (0.0f, 0.0f, 0.0f);
2180 glVertex3f (1.0f, 1.0f, -1.0f);
2181 glVertex3f (-1.0f, 1.0f, -1.0f);
2182 glEnd ();
2185 glEnable(GL_DEPTH_TEST);
2186 CHECK_GL_ERROR();
2189 void OpenGL3DRenderer::ProcessUnrenderedShape(bool bNewScene)
2191 glViewport(0, 0, m_iWidth, m_iHeight);
2192 CHECK_GL_ERROR();
2193 ClearBuffer();
2194 CHECK_GL_ERROR();
2195 CreateSceneBoxView();
2196 CHECK_GL_ERROR();
2197 CalcScrollMoveMatrix(bNewScene);
2198 //Polygon
2199 CHECK_GL_ERROR();
2200 RenderPolygon3DObject();
2201 //Shape3DExtrudeObject
2202 if(mbPickingMode)
2203 RenderExtrude3DObject();
2204 else
2206 if (maResources.m_b330Support)
2208 RenderBatchBars(bNewScene);
2210 else
2212 RenderExtrude3DObject();
2215 //render text
2216 if (maResources.mbTexBatchSupport)
2218 RenderTextShapeBatch();
2220 else
2222 RenderTextShape();
2224 // render screen text
2225 RenderScreenTextShape();
2226 #if DEBUG_FBO
2227 OUString aFileName = OUString("D://shaderout_") + OUString::number(m_iWidth) + "_" + OUString::number(m_iHeight) + ".png";
2228 OpenGLHelper::renderToFile(m_iWidth, m_iHeight, aFileName);
2229 #endif
2232 void OpenGL3DRenderer::MoveModelf(const PosVecf3& trans, const PosVecf3& angle, const PosVecf3& scale)
2234 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
2235 glm::mat4 aScaleMatrix = glm::scale(glm::vec3(scale.x, scale.y, scale.z));
2236 glm::mat4 aRotationMatrix = glm::eulerAngleYXZ(angle.y, angle.x, angle.z);
2237 m_Model = aTranslationMatrix * aRotationMatrix * aScaleMatrix;
2240 void OpenGL3DRenderer::SetPickingMode(bool bPickingMode)
2242 mbPickingMode = bPickingMode;
2243 if(mbPickingMode)
2245 glBindFramebuffer(GL_FRAMEBUFFER, mnPickingFbo);
2246 glDisable(GL_MULTISAMPLE);
2248 else
2250 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2251 glEnable(GL_MULTISAMPLE);
2255 sal_uInt32 OpenGL3DRenderer::GetPixelColorFromPoint(long nX, long nY)
2257 static sal_uInt32 nId = 0;
2258 OUString aFileName = "/home/moggi/work/picking_" + OUString::number(nId++) + ".png";
2259 OpenGLHelper::renderToFile(m_iWidth, m_iHeight, aFileName);
2260 boost::scoped_array<sal_uInt8> buf(new sal_uInt8[4]);
2261 glReadPixels(nX, m_iHeight-nY, 1, 1, GL_BGRA, GL_UNSIGNED_BYTE, buf.get());
2262 Color aColor(255-buf[3], buf[2], buf[1], buf[0]);
2263 return aColor.GetColor();
2266 void OpenGL3DRenderer::ReleaseBatchBarInfo()
2268 for (int i = 0; i < 3; i++)
2270 m_BarSurface[i].modelMatrixList.clear();
2271 m_BarSurface[i].normalMatrixList.clear();
2272 m_BarSurface[i].colorList.clear();
2273 m_BarSurface[i].mapId2Color.clear();
2277 void OpenGL3DRenderer::ReleaseShapes()
2279 ReleasePolygonShapes();
2280 ReleaseExtrude3DShapes();
2281 ReleaseTextShapes();
2282 //ReleaseScreenTextShapes();
2283 ReleaseBatchBarInfo();
2284 ReleaseTextShapesBatch();
2287 void OpenGL3DRenderer::GetBatchMiddleInfo(const Extrude3DInfo &extrude3D)
2289 float xyScale = extrude3D.xScale;
2290 float zScale = extrude3D.zScale;
2291 float actualZScale = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
2292 PosVecf3 trans = {extrude3D.xTransform,
2293 extrude3D.yTransform,
2294 extrude3D.zTransform};
2295 if (actualZScale < 0.0f)
2297 return ;
2299 else
2301 glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale,actualZScale));
2302 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
2303 m_Model = aTranslationMatrix * extrude3D.rotation * scale;
2306 m_Model = m_GlobalScaleMatrix * m_Model;
2307 glm::mat3 normalMatrix(m_Model);
2308 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
2309 m_BarSurface[MIDDLE_SURFACE].modelMatrixList.push_back(m_Model);
2310 m_BarSurface[MIDDLE_SURFACE].normalMatrixList.push_back(normalInverseTranspos);
2311 m_BarSurface[MIDDLE_SURFACE].colorList.push_back(extrude3D.material.materialColor);
2312 m_BarSurface[MIDDLE_SURFACE].mapId2Color[extrude3D.orgID] = m_BarSurface[MIDDLE_SURFACE].colorList.size() - 1;
2315 void OpenGL3DRenderer::GetBatchTopAndFlatInfo(const Extrude3DInfo &extrude3D)
2317 float xyScale = extrude3D.xScale;
2318 float zScale = extrude3D.zScale;
2319 float actualZTrans = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
2320 PosVecf3 trans = {extrude3D.xTransform,
2321 extrude3D.yTransform,
2322 extrude3D.zTransform};
2323 glm::mat4 orgTrans = glm::translate(glm::vec3(0.0, 0.0, -1.0));
2324 if (actualZTrans < 0.0f)
2326 // the height of rounded corner is higher than the cube than use the org scale matrix
2327 //yScale /= (float)(1 + BOTTOM_THRESHOLD);
2328 zScale /= (float)(m_RoundBarMesh.bottomThreshold);
2329 glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale, zScale));
2330 //MoveModelf(trans, angle, scale);
2331 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
2332 m_Model = aTranslationMatrix * extrude3D.rotation * scale * orgTrans;
2334 else
2336 // use different matrices for different parts
2337 glm::mat4 topTrans = glm::translate(glm::vec3(0.0, 0.0, actualZTrans));
2338 glm::mat4 topScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
2339 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
2340 m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale * orgTrans;
2343 m_Model = m_GlobalScaleMatrix * m_Model;
2344 glm::mat3 normalMatrix(m_Model);
2345 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
2346 m_BarSurface[TOP_SURFACE].modelMatrixList.push_back(m_Model);
2347 m_BarSurface[TOP_SURFACE].normalMatrixList.push_back(normalInverseTranspos);
2348 m_BarSurface[TOP_SURFACE].colorList.push_back(extrude3D.material.materialColor);
2349 m_BarSurface[TOP_SURFACE].mapId2Color[extrude3D.orgID] = m_BarSurface[TOP_SURFACE].colorList.size() - 1;
2351 glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
2352 glm::mat4 flatScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
2353 m_Model = aTranslationMatrix * extrude3D.rotation * flatScale;
2354 m_Model = m_GlobalScaleMatrix * m_Model;
2355 normalMatrix = glm::mat3(m_Model);
2356 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
2358 m_BarSurface[FLAT_BOTTOM_SURFACE].modelMatrixList.push_back(m_Model);
2359 m_BarSurface[FLAT_BOTTOM_SURFACE].normalMatrixList.push_back(normalInverseTranspos);
2360 m_BarSurface[FLAT_BOTTOM_SURFACE].colorList.push_back(extrude3D.material.materialColor);
2361 m_BarSurface[FLAT_BOTTOM_SURFACE].mapId2Color[extrude3D.orgID] = m_BarSurface[FLAT_BOTTOM_SURFACE].colorList.size() - 1;
2364 void OpenGL3DRenderer::GetBatchBarsInfo()
2366 for (size_t i = 0; i < m_Extrude3DList.size(); i++)
2368 Extrude3DInfo &extrude3DInfo = m_Extrude3DList[i];
2369 if (m_Extrude3DInfo.rounded)
2371 GetBatchTopAndFlatInfo(extrude3DInfo);
2372 GetBatchMiddleInfo(extrude3DInfo);
2374 else
2376 glm::mat4 transformMatrix = glm::translate(glm::vec3(extrude3DInfo.xTransform, extrude3DInfo.yTransform, extrude3DInfo.zTransform));
2377 glm::mat4 scaleMatrix = glm::scale(glm::vec3(extrude3DInfo.xScale, extrude3DInfo.yScale, extrude3DInfo.zScale));
2378 m_Model = transformMatrix * extrude3DInfo.rotation * scaleMatrix;
2379 m_Model = m_GlobalScaleMatrix * m_Model;
2380 glm::mat3 normalMatrix(m_Model);
2381 glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
2382 m_BarSurface[0].modelMatrixList.push_back(m_Model);
2383 m_BarSurface[0].normalMatrixList.push_back(normalInverseTranspos);
2384 m_BarSurface[0].colorList.push_back(extrude3DInfo.material.materialColor);
2385 m_BarSurface[0].mapId2Color[extrude3DInfo.orgID] = m_BarSurface[0].colorList.size() - 1;
2390 void OpenGL3DRenderer::SetHighLightBar(BatchBarInfo &barInfo)
2392 std::map<sal_uInt32, unsigned int> ::iterator it = barInfo.mapId2Color.find(m_uiSelectID);
2393 if (it != barInfo.mapId2Color.end())
2395 unsigned int idx = it->second;
2396 barInfo.selectBarColor = barInfo.colorList[idx];
2397 barInfo.colorList[idx] = glm::vec4(1.0, 1.0, 1.0, 1.0);
2401 void OpenGL3DRenderer::DisableHighLightBar(BatchBarInfo &barInfo)
2403 std::map<sal_uInt32, unsigned int> ::iterator it = barInfo.mapId2Color.find(m_uiSelectID);
2404 if (it != barInfo.mapId2Color.end())
2406 unsigned int idx = it->second;
2407 barInfo.colorList[idx] = barInfo.selectBarColor;
2411 void OpenGL3DRenderer::StartClick(sal_uInt32 &selectID)
2413 m_bHighLighting = true;
2414 m_uiSelectID = selectID;
2415 for (unsigned int i = 0; i < 3; i++)
2417 SetHighLightBar(m_BarSurface[i]);
2421 void OpenGL3DRenderer::EndClick()
2423 m_bHighLighting = false;
2424 for (unsigned int i = 0; i < 3; i++)
2426 DisableHighLightBar(m_BarSurface[i]);
2430 void OpenGL3DRenderer::SetScroll()
2432 maResources.m_bScrollFlag = true;
2435 void OpenGL3DRenderer::SetScrollSpeed(float scrollSpeed)
2437 m_fScrollSpeed = scrollSpeed;
2439 void OpenGL3DRenderer::SetScrollDistance(float scrollDistance)
2441 m_fScrollDistance = scrollDistance;
2444 void OpenGL3DRenderer::SetSceneEdge(float minCoordX, float maxCoordX)
2446 m_fMinCoordX = minCoordX * 0.01;
2447 m_fMaxCoordX = maxCoordX * 0.01;
2450 void OpenGL3DRenderer::CalcScrollMoveMatrix(bool bNewScene)
2452 if (!maResources.m_bScrollFlag)
2453 return;
2454 if (bNewScene)
2455 m_fCurDistance = -m_fScrollSpeed;
2456 m_fCurDistance += m_fCurDistance >= m_fScrollDistance ? 0.0f : m_fScrollSpeed;
2457 m_ScrollMoveMatrix = glm::translate(glm::vec3(-m_fCurDistance * 0.01, 0.0f, 0.0f));
2458 m_bUndrawFlag = m_fCurDistance >= m_fScrollDistance;
2461 glm::mat4 OpenGL3DRenderer::GetDiffOfTwoCameras(const glm::vec3& rBeginPos, const glm::vec3& rEndPos, const glm::vec3& rBeginDirection, const glm::vec3& rEndDirection)
2463 glm::mat4 aBegin = glm::lookAt(glm::vec3(m_GlobalScaleMatrix * glm::vec4(rBeginPos, 1.0)),
2464 glm::vec3(m_GlobalScaleMatrix * glm::vec4(rBeginDirection, 1.0)),
2465 glm::vec3(0, 0, 1));
2466 glm::mat4 aEnd = glm::lookAt(glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndPos, 1.0)),
2467 glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndDirection, 1.0)),
2468 glm::vec3(0, 0, 1));
2469 return aEnd - aBegin;
2472 glm::mat4 OpenGL3DRenderer::GetDiffOfTwoCameras(const glm::vec3& rEndPos, const glm::vec3& rEndDirection)
2474 glm::mat4 aEnd = glm::lookAt(glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndPos, 1.0)),
2475 glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndDirection, 1.0)),glm::vec3(0, 0, 1));
2476 return aEnd - m_3DView;
2479 glm::mat4 OpenGL3DRenderer::GetProjectionMatrix()
2481 return m_3DProjection;
2484 glm::mat4 OpenGL3DRenderer::GetViewMatrix()
2486 return m_3DView;
2489 glm::mat4 OpenGL3DRenderer::GetGlobalScaleMatrix()
2491 return m_GlobalScaleMatrix;
2494 void OpenGL3DRenderer::RenderBatchBars(bool bNewScene)
2496 if (m_BarSurface[0].modelMatrixList.empty())
2497 return;
2499 if(bNewScene)
2501 GetBatchBarsInfo();
2502 if (m_bHighLighting)
2504 for (unsigned int i = 0; i < 3; i++)
2506 SetHighLightBar(m_BarSurface[i]);
2510 glEnable(GL_DEPTH_TEST);
2511 glEnable(GL_CULL_FACE);
2512 glCullFace(GL_BACK);
2513 glPolygonOffset(0.0f, 0.0f);
2514 glUseProgram(maResources.m_3DBatchProID);
2515 UpdateBatch3DUniformBlock();
2516 glBindBuffer(GL_UNIFORM_BUFFER, m_Batch3DUBOBuffer);
2517 glBufferSubData(GL_UNIFORM_BUFFER, m_Batch3DActualSizeLight, sizeof(MaterialParameters), &m_Batchmaterial);
2518 CHECK_GL_ERROR();
2519 glBindBuffer(GL_UNIFORM_BUFFER, 0);
2520 if (maResources.m_bScrollFlag)
2522 glUniform1fv(maResources.m_3DBatchMinCoordXID, 1, &m_fMinCoordX);
2523 glUniform1fv(maResources.m_3DBatchMaxCoordXID, 1, &m_fMaxCoordX);
2524 glUniform1i(maResources.m_3DBatchUndrawID, m_bUndrawFlag);
2525 glUniformMatrix4fv(maResources.m_3DBatchTransMatrixID, 1, GL_FALSE, &m_ScrollMoveMatrix[0][0]);
2527 glUniformMatrix4fv(maResources.m_3DBatchViewID, 1, GL_FALSE, &m_3DView[0][0]);
2528 glUniformMatrix4fv(maResources.m_3DBatchProjectionID, 1, GL_FALSE, &m_3DProjection[0][0]);
2529 CHECK_GL_ERROR();
2530 GLuint vertexBuf = m_Extrude3DInfo.rounded ? m_CubeVertexBuf : m_BoundBox;
2531 GLuint normalBuf = m_Extrude3DInfo.rounded ? m_CubeNormalBuf : m_BoundBoxNormal;
2532 //vertex
2533 glEnableVertexAttribArray(maResources.m_3DBatchVertexID);
2534 glBindBuffer(GL_ARRAY_BUFFER, vertexBuf);
2535 glVertexAttribPointer(maResources.m_3DBatchVertexID, // attribute
2536 3, // size
2537 GL_FLOAT, // type
2538 GL_FALSE, // normalized?
2539 0, // stride
2540 (void*)0 // array buffer offset
2542 //normal
2543 glEnableVertexAttribArray(maResources.m_3DBatchNormalID);
2544 glBindBuffer(GL_ARRAY_BUFFER, normalBuf);
2545 glVertexAttribPointer(maResources.m_3DBatchNormalID, // attribute
2546 3, // size
2547 GL_FLOAT, // type
2548 GL_FALSE, // normalized?
2549 0, // stride
2550 (void*)0 // array buffer offset
2553 for (unsigned int i = 0; i < 4 ; i++)
2555 glEnableVertexAttribArray(maResources.m_3DBatchModelID + i);
2556 glBindBuffer(GL_ARRAY_BUFFER, m_BatchModelMatrixBuf);
2557 glVertexAttribPointer(maResources.m_3DBatchModelID + i, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * i * 4));
2558 glVertexAttribDivisor(maResources.m_3DBatchModelID + i, 1);
2561 for (unsigned int i = 0; i < 3 ; i++)
2563 glEnableVertexAttribArray(maResources.m_3DBatchNormalMatrixID + i);
2564 glBindBuffer(GL_ARRAY_BUFFER, m_BatchNormalMatrixBuf);
2565 glVertexAttribPointer(maResources.m_3DBatchNormalMatrixID + i, 3, GL_FLOAT, GL_FALSE, sizeof(glm::mat3), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * i * 3));
2566 glVertexAttribDivisor(maResources.m_3DBatchNormalMatrixID + i, 1);
2568 glEnableVertexAttribArray(maResources.m_3DBatchColorID);
2569 glBindBuffer(GL_ARRAY_BUFFER, m_BatchColorBuf);
2570 glVertexAttribPointer(maResources.m_3DBatchColorID , 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), 0);
2571 glVertexAttribDivisor(maResources.m_3DBatchColorID, 1);
2572 if (m_Extrude3DInfo.rounded)
2574 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CubeElementBuf);
2575 for (int i = 0; i < 2; i++)
2577 glBindBuffer(GL_ARRAY_BUFFER, m_BatchModelMatrixBuf);
2578 glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * m_BarSurface[i].modelMatrixList.size(), &m_BarSurface[i].modelMatrixList[0][0], GL_DYNAMIC_DRAW);
2579 glBindBuffer(GL_ARRAY_BUFFER, m_BatchNormalMatrixBuf);
2580 glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat3) * m_BarSurface[i].normalMatrixList.size(), &m_BarSurface[i].normalMatrixList[0][0], GL_DYNAMIC_DRAW);
2581 glBindBuffer(GL_ARRAY_BUFFER, m_BatchColorBuf);
2582 glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * m_BarSurface[i].colorList.size(), &m_BarSurface[i].colorList[0], GL_DYNAMIC_DRAW);
2583 glDrawElementsInstancedBaseVertex(GL_TRIANGLES,
2584 m_Extrude3DInfo.size[i],
2585 GL_UNSIGNED_SHORT,
2586 reinterpret_cast<GLvoid*>(m_Extrude3DInfo.startIndex[i]),
2587 m_BarSurface[i].modelMatrixList.size(),
2591 else
2593 glBindBuffer(GL_ARRAY_BUFFER, m_BatchModelMatrixBuf);
2594 glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * m_BarSurface[0].modelMatrixList.size(), &m_BarSurface[0].modelMatrixList[0][0], GL_DYNAMIC_DRAW);
2595 glBindBuffer(GL_ARRAY_BUFFER, m_BatchNormalMatrixBuf);
2596 glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat3) * m_BarSurface[0].normalMatrixList.size(), &m_BarSurface[0].normalMatrixList[0][0], GL_DYNAMIC_DRAW);
2597 glBindBuffer(GL_ARRAY_BUFFER, m_BatchColorBuf);
2598 glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * m_BarSurface[0].colorList.size(), &m_BarSurface[0].colorList[0], GL_DYNAMIC_DRAW);
2599 glDrawArraysInstanced(GL_TRIANGLES, 0, 36, m_BarSurface[0].modelMatrixList.size());
2601 glDisableVertexAttribArray(maResources.m_3DBatchVertexID);
2602 glDisableVertexAttribArray(maResources.m_3DBatchNormalID);
2603 glDisableVertexAttribArray(maResources.m_3DBatchColorID);
2604 glVertexAttribDivisor(maResources.m_3DBatchColorID, 0);
2605 for (unsigned int i = 0; i < 4 ; i++)
2607 glDisableVertexAttribArray(maResources.m_3DBatchModelID + i);
2608 glVertexAttribDivisor(maResources.m_3DBatchModelID + i, 0);
2610 for (unsigned int i = 0; i < 3 ; i++)
2612 glDisableVertexAttribArray(maResources.m_3DBatchNormalMatrixID + i);
2613 glVertexAttribDivisor(maResources.m_3DBatchNormalMatrixID + i, 0);
2615 glUseProgram(0);
2616 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
2617 glDisable(GL_CULL_FACE);
2621 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */