LCOV - code coverage report
Current view: top level - chart2/source/view/main - GL3DRenderer.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 1568 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 88 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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             :  */
       9             : 
      10             : #include <GL/glew.h>
      11             : 
      12             : #include "GL3DRenderer.hxx"
      13             : 
      14             : #include <vcl/opengl/OpenGLHelper.hxx>
      15             : #include <vcl/font.hxx>
      16             : #include <vcl/virdev.hxx>
      17             : 
      18             : #include <com/sun/star/awt/Size.hpp>
      19             : 
      20             : #include <StaticGeometry.h>
      21             : #include "glm/gtc/matrix_inverse.hpp"
      22             : #include <boost/checked_delete.hpp>
      23             : #include <boost/scoped_array.hpp>
      24             : 
      25             : #define DEBUG_FBO 0
      26             : 
      27             : using namespace com::sun::star;
      28             : 
      29             : namespace chart {
      30             : 
      31             : namespace opengl3D {
      32             : 
      33             : namespace {
      34             : 
      35             : const int CORNER_DIVION_Y = 20;
      36             : const int CORNER_DIVION_Z = 20;
      37             : 
      38             : GLfloat texCoords[] = {
      39             :     1.0f, 0.0f,
      40             :     1.0f, 0.96f,
      41             :     0.0f, 0.96f,
      42             :     0.0f, 0.0f
      43             : };
      44             : 
      45           0 : glm::vec4 getColorAsVector(sal_uInt32 nColor)
      46             : {
      47           0 :     return glm::vec4(((nColor & 0x00FF0000) >> 16) / 255.0f,
      48           0 :             ((nColor & 0x0000FF00) >> 8) / 255.0f,
      49           0 :             (nColor & 0x000000FF) / 255.0f,
      50           0 :             (0xFF - (nColor & 0xFF000000)/255.0));
      51             : }
      52             : 
      53             : }
      54             : 
      55           0 : TextureArrayInfo::TextureArrayInfo():
      56             :     subTextureNum(0),
      57             :     textureArrayWidth(0),
      58             :     textureArrayHeight(0),
      59           0 :     textureID(0)
      60             : {
      61           0 : }
      62             : 
      63           0 : 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           0 :     , m_matDiff(glm::mat4(0.0))
     101             : {
     102           0 :     m_Polygon3DInfo.lineOnly = false;
     103           0 :     m_Polygon3DInfo.twoSidesLighting = false;
     104           0 :     m_Polygon3DInfo.vertices = NULL;
     105           0 :     m_Polygon3DInfo.normals = NULL;
     106           0 :     m_Polygon3DInfo.lineWidth = 0.001f;
     107             : 
     108           0 :     m_Extrude3DInfo.twoSidesLighting = false;
     109             : 
     110           0 :     m_RoundBarMesh.iMeshSizes = 0;
     111           0 : }
     112             : 
     113           0 : OpenGL3DRenderer::~OpenGL3DRenderer()
     114             : {
     115           0 :     ReleaseShapes();
     116             :     // delete buffers
     117           0 :     glDeleteBuffers(1, &m_CubeVertexBuf);
     118           0 :     glDeleteBuffers(1, &m_CubeNormalBuf);
     119           0 :     glDeleteBuffers(1, &m_CubeElementBuf);
     120           0 :     glDeleteBuffers(1, &m_BoundBox);
     121           0 :     glDeleteBuffers(1, &m_BoundBoxNormal);
     122           0 :     glDeleteBuffers(1, &m_TextTexCoordBuf);
     123           0 :     glDeleteBuffers(1, &m_RenderTexCoordBuf);
     124           0 :     glDeleteBuffers(1, &m_RenderVertexBuf);
     125           0 :     glDeleteBuffers(1, &m_3DUBOBuffer);
     126           0 :     glDeleteBuffers(1, &m_VertexBuffer);
     127           0 :     glDeleteBuffers(1, &m_NormalBuffer);
     128           0 :     glDeleteBuffers(1, &m_Batch3DUBOBuffer);
     129           0 :     glDeleteBuffers(1, &m_3DUBOBuffer);
     130           0 :     glDeleteBuffers(1, &m_3DUBOBuffer);
     131           0 :     glDeleteBuffers(1, &m_TextTexCoordBufBatch);
     132             : 
     133           0 :     glDeleteFramebuffers(1, &mnPickingFbo);
     134           0 :     glDeleteRenderbuffers(1, &mnPickingRboDepth);
     135           0 :     glDeleteRenderbuffers(1, &mnPickingRboColor);
     136             : 
     137           0 :     for (size_t i = 0; i < m_TextInfoBatch.texture.size(); i++)
     138             :     {
     139           0 :         glDeleteTextures(1, &m_TextInfoBatch.texture[i].textureID);
     140             :     }
     141           0 :     m_TextInfoBatch.texture.clear();
     142             : 
     143           0 : }
     144             : 
     145           0 : 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           0 :     , m_BatchTextTexID(0)
     201             : {
     202           0 : }
     203             : 
     204           0 : OpenGL3DRenderer::ShaderResources::~ShaderResources()
     205             : {
     206           0 :     glDeleteProgram(m_CommonProID);
     207           0 :     glDeleteProgram(m_TextProID);
     208           0 :     glDeleteProgram(m_ScreenTextProID);
     209           0 :     glDeleteProgram(m_3DProID);
     210           0 :     glDeleteProgram(m_3DBatchProID);
     211           0 :     glDeleteProgram(m_BatchTextProID);
     212           0 : }
     213             : 
     214           0 : void OpenGL3DRenderer::CheckGLSLVersion()
     215             : {
     216           0 :     maResources.m_b330Support = GLEW_VERSION_3_3;
     217           0 : }
     218             : 
     219           0 : void OpenGL3DRenderer::ShaderResources::LoadShaders()
     220             : {
     221           0 :     CHECK_GL_ERROR();
     222           0 :     if (m_b330Support)
     223             :     {
     224           0 :         m_3DProID = OpenGLHelper::LoadShaders("shape3DVertexShader", "shape3DFragmentShader");
     225           0 :         m_3DProjectionID = glGetUniformLocation(m_3DProID, "P");
     226           0 :         m_3DViewID = glGetUniformLocation(m_3DProID, "V");
     227           0 :         m_3DModelID = glGetUniformLocation(m_3DProID, "M");
     228           0 :         m_3DNormalMatrixID = glGetUniformLocation(m_3DProID, "normalMatrix");
     229           0 :         m_3DVertexID = glGetAttribLocation(m_3DProID, "vertexPositionModelspace");
     230           0 :         m_3DNormalID = glGetAttribLocation(m_3DProID, "vertexNormalModelspace");
     231           0 :         CHECK_GL_ERROR();
     232           0 :         if (m_bScrollFlag)
     233             :         {
     234           0 :             m_3DBatchProID = OpenGLHelper::LoadShaders("shape3DVertexShaderBatchScroll", "shape3DFragmentShaderBatchScroll");
     235           0 :             m_3DBatchTransMatrixID = glGetUniformLocation(m_3DBatchProID, "transMatrix");
     236           0 :             m_3DBatchMinCoordXID = glGetUniformLocation(m_3DBatchProID, "minCoordX");
     237           0 :             m_3DBatchMaxCoordXID = glGetUniformLocation(m_3DBatchProID, "maxCoordX");
     238           0 :             m_3DBatchUndrawID = glGetUniformLocation(m_3DBatchProID, "undraw");
     239             :         }
     240             :         else
     241           0 :             m_3DBatchProID = OpenGLHelper::LoadShaders("shape3DVertexShaderBatch", "shape3DFragmentShaderBatch");
     242             : 
     243           0 :         CHECK_GL_ERROR();
     244           0 :         m_3DBatchProjectionID = glGetUniformLocation(m_3DBatchProID, "P");
     245           0 :         m_3DBatchViewID = glGetUniformLocation(m_3DBatchProID, "V");
     246           0 :         m_3DBatchModelID = glGetAttribLocation(m_3DBatchProID, "M");
     247           0 :         m_3DBatchNormalMatrixID = glGetAttribLocation(m_3DBatchProID, "normalMatrix");
     248           0 :         m_3DBatchVertexID = glGetAttribLocation(m_3DBatchProID, "vertexPositionModelspace");
     249           0 :         m_3DBatchNormalID = glGetAttribLocation(m_3DBatchProID, "vertexNormalModelspace");
     250           0 :         m_3DBatchColorID = glGetAttribLocation(m_3DBatchProID, "barColor");
     251             : #if !defined MACOSX
     252             :         //check whether the texture array is support
     253           0 :         mbTexBatchSupport = GLEW_EXT_texture_array;
     254             : #endif
     255           0 :         CHECK_GL_ERROR();
     256           0 :         if (mbTexBatchSupport)
     257             :         {
     258           0 :             m_BatchTextProID = OpenGLHelper::LoadShaders("textVertexShaderBatch", "textFragmentShaderBatch");
     259           0 :             m_BatchTextMatrixID = glGetUniformLocation(m_BatchTextProID, "MVP");
     260           0 :             m_BatchTextTexID = glGetUniformLocation(m_BatchTextProID, "texArray");
     261           0 :             m_BatchTextVertexID = glGetAttribLocation(m_BatchTextProID, "vPosition");
     262           0 :             m_BatchTextTexCoordID = glGetAttribLocation(m_BatchTextProID, "texCoord");
     263             :         }
     264           0 :         mbTexBatchSupport = m_BatchTextProID != 0;
     265           0 :         CHECK_GL_ERROR();
     266             :     }
     267             :     else
     268             :     {
     269           0 :         CHECK_GL_ERROR();
     270             :         //use 300
     271           0 :         m_3DProID = OpenGLHelper::LoadShaders("shape3DVertexShaderV300", "shape3DFragmentShaderV300");
     272           0 :         m_3DProjectionID = glGetUniformLocation(m_3DProID, "P");
     273           0 :         m_3DViewID = glGetUniformLocation(m_3DProID, "V");
     274           0 :         m_3DModelID = glGetUniformLocation(m_3DProID, "M");
     275           0 :         m_3DNormalMatrixID = glGetUniformLocation(m_3DProID, "normalMatrix");
     276           0 :         m_3DMaterialAmbientID = glGetUniformLocation(m_3DProID, "materialAmbient");
     277           0 :         m_3DMaterialDiffuseID = glGetUniformLocation(m_3DProID, "materialDiffuse");
     278           0 :         m_3DMaterialSpecularID = glGetUniformLocation(m_3DProID, "materialSpecular");
     279           0 :         m_3DMaterialColorID = glGetUniformLocation(m_3DProID, "materialColor");
     280           0 :         m_3DMaterialTwoSidesID = glGetUniformLocation(m_3DProID, "twoSidesLighting");
     281           0 :         m_3DMaterialShininessID = glGetUniformLocation(m_3DProID, "materialShininess");
     282           0 :         m_3DLightColorID = glGetUniformLocation(m_3DProID, "lightColor");
     283           0 :         m_3DLightPosID = glGetUniformLocation(m_3DProID, "lightPosWorldspace");
     284           0 :         m_3DLightPowerID = glGetUniformLocation(m_3DProID, "lightPower");
     285           0 :         m_3DLightNumID = glGetUniformLocation(m_3DProID, "lightNum");
     286           0 :         m_3DLightAmbientID = glGetUniformLocation(m_3DProID, "lightAmbient");
     287           0 :         m_3DMinCoordXID = glGetUniformLocation(m_3DProID, "minCoordX");
     288           0 :         m_3DMaxCoordXID = glGetUniformLocation(m_3DProID, "maxCoordX");
     289           0 :         m_3DUndrawID = glGetUniformLocation(m_3DProID, "undraw");
     290           0 :         m_3DVertexID = glGetAttribLocation(m_3DProID, "vertexPositionModelspace");
     291           0 :         m_3DNormalID = glGetAttribLocation(m_3DProID, "vertexNormalModelspace");
     292             :     }
     293           0 :     CHECK_GL_ERROR();
     294           0 :     if (!mbTexBatchSupport)
     295             :     {
     296           0 :         m_TextProID = OpenGLHelper::LoadShaders("textVertexShader", "textFragmentShader");
     297           0 :         m_TextMatrixID = glGetUniformLocation(m_TextProID, "MVP");
     298           0 :         m_TextVertexID = glGetAttribLocation(m_TextProID, "vPosition");
     299           0 :         m_TextTexCoordID = glGetAttribLocation(m_TextProID, "texCoord");
     300           0 :         m_TextTexID = glGetUniformLocation(m_TextProID, "TextTex");
     301             :     }
     302           0 :     CHECK_GL_ERROR();
     303             : 
     304           0 :     m_ScreenTextProID = OpenGLHelper::LoadShaders("screenTextVertexShader", "screenTextFragmentShader");
     305           0 :     m_ScreenTextVertexID = glGetAttribLocation(m_ScreenTextProID, "vPosition");
     306           0 :     m_ScreenTextTexCoordID = glGetAttribLocation(m_ScreenTextProID, "texCoord");
     307           0 :     m_ScreenTextTexID = glGetUniformLocation(m_ScreenTextProID, "TextTex");
     308           0 :     m_ScreenTextColorID = glGetUniformLocation(m_ScreenTextProID, "textColor");
     309           0 :     CHECK_GL_ERROR();
     310             : 
     311           0 :     m_CommonProID = OpenGLHelper::LoadShaders("commonVertexShader", "commonFragmentShader");
     312           0 :     m_MatrixID = glGetUniformLocation(m_CommonProID, "MVP");
     313           0 :     m_2DColorID = glGetUniformLocation(m_CommonProID, "vColor");
     314           0 :     m_2DVertexID = glGetAttribLocation(m_CommonProID, "vPosition");
     315             : 
     316           0 :     CHECK_GL_ERROR();
     317           0 : }
     318             : 
     319           0 : 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           0 :     , m_MaxCoordXID(0)
     327             : {
     328           0 : }
     329             : 
     330           0 : OpenGL3DRenderer::PickingShaderResources::~PickingShaderResources()
     331             : {
     332           0 :     glDeleteProgram(m_CommonProID);
     333           0 : }
     334             : 
     335           0 : void OpenGL3DRenderer::PickingShaderResources::LoadShaders()
     336             : {
     337           0 :     m_CommonProID = OpenGLHelper::LoadShaders("pickingVertexShader", "pickingFragmentShader");
     338           0 :     m_MatrixID = glGetUniformLocation(m_CommonProID, "MVP");
     339           0 :     m_2DVertexID = glGetAttribLocation(m_CommonProID, "vPosition");
     340           0 :     m_2DColorID = glGetUniformLocation(m_CommonProID, "vColor");
     341           0 :     m_ModelID = glGetUniformLocation(m_CommonProID, "M");
     342           0 :     m_MinCoordXID = glGetUniformLocation(m_CommonProID, "minCoordX");
     343           0 :     m_MaxCoordXID = glGetUniformLocation(m_CommonProID, "maxCoordX");
     344           0 : }
     345             : 
     346           0 : void OpenGL3DRenderer::SetCameraInfo(const glm::vec3& pos, const glm::vec3& direction, const glm::vec3& up)
     347             : {
     348           0 :     m_CameraInfo.cameraPos = pos;
     349           0 :     m_CameraInfo.cameraOrg = direction;
     350           0 :     m_CameraInfo.cameraUp = up;
     351           0 : }
     352             : 
     353           0 : void OpenGL3DRenderer::init()
     354             : {
     355           0 :     CHECK_GL_ERROR();
     356           0 :     glEnable(GL_CULL_FACE);
     357           0 :     CHECK_GL_ERROR();
     358           0 :     glCullFace(GL_BACK);
     359           0 :     CHECK_GL_ERROR();
     360             : #if !defined(ANDROID) && !defined(IOS)
     361           0 :     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
     362             : #endif
     363             :     // Enable depth test
     364           0 :     CHECK_GL_ERROR();
     365           0 :     glEnable(GL_DEPTH_TEST);
     366             :     // Accept fragment if it closer to the camera than the former one
     367           0 :     CHECK_GL_ERROR();
     368           0 :     glDepthFunc(GL_LESS);
     369           0 :     CHECK_GL_ERROR();
     370           0 :     glEnable(GL_LINE_SMOOTH);
     371           0 :     CHECK_GL_ERROR();
     372           0 :     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
     373           0 :     CHECK_GL_ERROR();
     374           0 :     glEnable(GL_BLEND);
     375           0 :     CHECK_GL_ERROR();
     376           0 :     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     377           0 :     CHECK_GL_ERROR();
     378             : 
     379           0 :     glEnable(GL_MULTISAMPLE);
     380             : 
     381           0 :     CHECK_GL_ERROR();
     382           0 :     ClearBuffer();
     383           0 :     CHECK_GL_ERROR();
     384             : 
     385           0 :     glGenBuffers(1, &m_CubeVertexBuf);
     386           0 :     glGenBuffers(1, &m_CubeNormalBuf);
     387           0 :     glGenBuffers(1, &m_CubeElementBuf);
     388           0 :     glGenBuffers(1, &m_VertexBuffer);
     389           0 :     glGenBuffers(1, &m_NormalBuffer);
     390           0 :     glGenBuffers(1, &m_BatchModelMatrixBuf);
     391           0 :     glGenBuffers(1, &m_BatchNormalMatrixBuf);
     392           0 :     glGenBuffers(1, &m_BatchColorBuf);
     393           0 :     glGenBuffers(1, &m_TextTexCoordBufBatch);
     394           0 :     glGenBuffers(1, &m_BoundBox);
     395           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_BoundBox);
     396           0 :     glBufferData(GL_ARRAY_BUFFER, sizeof(boundBox), boundBox, GL_STATIC_DRAW);
     397           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     398           0 :     CHECK_GL_ERROR();
     399             : 
     400           0 :     glGenBuffers(1, &m_BoundBoxNormal);
     401           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_BoundBoxNormal);
     402           0 :     glBufferData(GL_ARRAY_BUFFER, sizeof(boundBoxNormal), boundBoxNormal, GL_STATIC_DRAW);
     403           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     404           0 :     CHECK_GL_ERROR();
     405             : 
     406           0 :     m_fViewAngle = 30.0f;
     407           0 :     m_3DProjection = glm::perspective(m_fViewAngle, (float)m_iWidth / (float)m_iHeight, 0.01f, 6000.0f);
     408             : 
     409           0 :     CheckGLSLVersion();
     410           0 :     CHECK_GL_ERROR();
     411           0 :     maResources.LoadShaders();
     412           0 :     maPickingResources.LoadShaders();
     413             : 
     414           0 :     glGenBuffers(1, &m_TextTexCoordBuf);
     415           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBuf);
     416           0 :     glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);
     417           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     418             : 
     419           0 :     glGenBuffers(1, &m_RenderTexCoordBuf);
     420           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_RenderTexCoordBuf);
     421           0 :     glBufferData(GL_ARRAY_BUFFER, sizeof(coordReverseVertices), coordReverseVertices, GL_STATIC_DRAW);
     422           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     423             : 
     424           0 :     glGenBuffers(1, &m_RenderVertexBuf);
     425           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_RenderVertexBuf);
     426           0 :     glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
     427           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     428           0 :     CHECK_GL_ERROR();
     429             : 
     430           0 :     OpenGLHelper::createFramebuffer(m_iWidth, m_iHeight, mnPickingFbo, mnPickingRboDepth, mnPickingRboColor);
     431             : 
     432           0 :     m_Extrude3DInfo.rounded = false;
     433           0 :     CHECK_GL_ERROR();
     434           0 :     if (maResources.m_b330Support)
     435             :     {
     436           0 :         Init3DUniformBlock();
     437           0 :         InitBatch3DUniformBlock();
     438             :     }
     439           0 :     m_TextInfoBatch.batchNum = 512;
     440           0 :     CHECK_GL_ERROR();
     441           0 :     glViewport(0, 0, m_iWidth, m_iHeight);
     442           0 :     Set3DSenceInfo(0xFFFFFF, true);
     443           0 :     m_GlobalScaleMatrix = glm::scale(glm::vec3(0.01f, 0.01f, 0.01f));
     444           0 : }
     445             : 
     446           0 : void OpenGL3DRenderer::SetSize(const Size& rSize)
     447             : {
     448           0 :     m_iWidth = rSize.Width();
     449           0 :     m_iHeight = rSize.Height();
     450           0 : }
     451             : 
     452           0 : void OpenGL3DRenderer::AddVertexData(GLuint vertexBuf)
     453             : {
     454           0 :     glBindBuffer(GL_ARRAY_BUFFER, vertexBuf);
     455           0 :     glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(glm::vec3), &m_Vertices[0], GL_STATIC_DRAW);
     456           0 :     CHECK_GL_ERROR();
     457           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     458           0 : }
     459             : 
     460           0 : void OpenGL3DRenderer::AddNormalData(GLuint normalBuf)
     461             : {
     462           0 :     glBindBuffer(GL_ARRAY_BUFFER, normalBuf);
     463           0 :     glBufferData(GL_ARRAY_BUFFER, m_Normals.size() * sizeof(glm::vec3), &m_Normals[0], GL_STATIC_DRAW);
     464           0 :     CHECK_GL_ERROR();
     465           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
     466           0 : }
     467             : 
     468           0 : void OpenGL3DRenderer::AddIndexData(GLuint indexBuf)
     469             : {
     470           0 :     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuf);
     471           0 :     glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned short), &m_Indices[0], GL_STATIC_DRAW);
     472           0 :     CHECK_GL_ERROR();
     473           0 :     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
     474           0 : }
     475             : 
     476           0 : bool OpenGL3DRenderer::GetSimilarVertexIndex(PackedVertex & packed,
     477             :     std::map<PackedVertex,unsigned short> & VertexToOutIndex,
     478             :     unsigned short & result
     479             :     )
     480             : {
     481           0 :     std::map<PackedVertex,unsigned short>::iterator it = VertexToOutIndex.find(packed);
     482           0 :     if ( it == VertexToOutIndex.end() )
     483             :     {
     484           0 :         return false;
     485             :     }
     486             :     else
     487             :     {
     488           0 :         result = it->second;
     489           0 :         return true;
     490             :     }
     491             : }
     492             : 
     493           0 : 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)
     498             : {
     499             :     unsigned short index;
     500           0 :     bool found = GetSimilarVertexIndex(packed, VertexToOutIndex, index);
     501           0 :     if ( found )
     502             :     { // A similar vertex is already in the VBO, use it instead !
     503           0 :         indices.push_back( index );
     504             :     }
     505             :     else
     506             :     { // If not, it needs to be added in the output data.
     507           0 :         vertex.push_back(packed.position);
     508           0 :         normal.push_back(packed.normal);
     509           0 :         size_t newindex = vertex.size() - 1;
     510           0 :         indices.push_back( newindex );
     511           0 :         VertexToOutIndex[ packed ] = newindex;
     512             :     }
     513           0 : }
     514             : 
     515           0 : void OpenGL3DRenderer::CreateActualRoundedCube(float fRadius, int iSubDivY, int iSubDivZ, float width, float height, float depth)
     516             : {
     517           0 :     float topThreshold = depth - 2 * fRadius;
     518           0 :     float bottomThreshold = fRadius;
     519             : 
     520           0 :     std::vector<glm::vec3> vertices;
     521           0 :     std::vector<glm::vec3> normals;
     522           0 :     GenerateRoundCornerBar(vertices, normals, fRadius, iSubDivY, iSubDivZ, width, height, depth);
     523           0 :     std::map<PackedVertex,unsigned short> VertexToOutIndex;
     524           0 :     glm::vec3 actualVerteices[3];
     525           0 :     glm::vec3 actualNormals[3];
     526           0 :     std::vector<unsigned short> indices[5];
     527           0 :     glm::vec3 externSurNormal;
     528           0 :     glm::mat4 corrctCoord = glm::translate(glm::vec3(width / 2.0f, height / 2.0f, depth / 2.0f - fRadius));
     529           0 :     m_RoundBarMesh.topThreshold = topThreshold;
     530           0 :     m_RoundBarMesh.bottomThreshold = bottomThreshold;
     531           0 :     m_RoundBarMesh.iMeshStartIndices = m_Vertices.size();
     532           0 :     for (int k = 0; k < 5; k++)
     533             :     {
     534           0 :         m_RoundBarMesh.iElementStartIndices[k] = indices[k].size();
     535             :     }
     536           0 :     for (size_t i = 0; i < vertices.size(); i += 3)
     537             :     {
     538           0 :         for (int k = 0; k < 3; k++)
     539             :         {
     540           0 :             actualVerteices[k] = glm::vec3(corrctCoord * glm::vec4(vertices[i + k], 1.0));
     541           0 :             actualNormals[k] = normals[i + k];
     542             :         }
     543           0 :         float maxZ = std::max(std::max(actualVerteices[0].z, actualVerteices[1].z), actualVerteices[2].z);
     544           0 :         float minZ = std::min(std::min(actualVerteices[0].z, actualVerteices[1].z), actualVerteices[2].z);
     545           0 :         int surfaceIndex = (minZ >= topThreshold - 0.001) ? TOP_SURFACE : ((maxZ <= bottomThreshold + 0.001) ? BOTTOM_SURFACE : MIDDLE_SURFACE);
     546           0 :         for (int k = 0; k < 3; k++)
     547             :         {
     548             :             {
     549           0 :                 PackedVertex packed = {actualVerteices[k], actualNormals[k]};
     550           0 :                 SetVertex(packed, VertexToOutIndex, m_Vertices, m_Normals, indices[surfaceIndex]);
     551             :             }
     552             : 
     553             :             //add extern
     554           0 :             if ((surfaceIndex == TOP_SURFACE) || (surfaceIndex == BOTTOM_SURFACE))
     555             :             {
     556           0 :                 actualVerteices[k].z = 0.0f;
     557           0 :                 externSurNormal = (surfaceIndex == TOP_SURFACE) ? glm::vec3(0.0, 0.0, 1.0) : glm::vec3(0.0, 0.0, -1.0);
     558           0 :                 int tmpSurfaceIndex = (surfaceIndex == TOP_SURFACE) ? FLAT_TOP_SURFACE : FLAT_BOTTOM_SURFACE;
     559           0 :                 PackedVertex packed = {actualVerteices[k], externSurNormal};
     560           0 :                 SetVertex(packed, VertexToOutIndex, m_Vertices, m_Normals, indices[tmpSurfaceIndex]);
     561             :             }
     562             :         }
     563             : 
     564             :     }
     565             :     //create index below
     566           0 :     m_RoundBarMesh.iMeshSizes = m_Vertices.size() - m_RoundBarMesh.iMeshStartIndices;
     567           0 :     for (int k = 0; k < 5; k++)
     568             :     {
     569           0 :         m_RoundBarMesh.iElementSizes[k] = indices[k].size() - m_RoundBarMesh.iElementStartIndices[k];
     570           0 :         m_RoundBarMesh.iElementStartIndices[k] = m_Indices.size() * sizeof(unsigned short);
     571           0 :         for (size_t IdxCnt = 0; IdxCnt < indices[k].size(); IdxCnt++)
     572             :         {
     573           0 :             m_Indices.push_back(indices[k][IdxCnt]);
     574             :         }
     575           0 :         indices[k].clear();
     576             :     }
     577           0 :     vertices.clear();
     578           0 :     normals.clear();
     579           0 :     VertexToOutIndex.clear();
     580           0 : }
     581             : 
     582           0 : 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)
     583             : {
     584             :     //float fAddAngleY = 360.0f/float(iSubDivY), fAddAngleZ = 180.0f/float(iSubDivZ);
     585           0 :     float fAddAngleY = 360.0f/float(iSubDivY);
     586           0 :     float fAddAngleZ = 180.0f/float(iSubDivZ);
     587           0 :     float fCurAngleY = 0.0f;
     588           0 :     int iStepsY = 1;
     589             : 
     590           0 :     const float PI = float(atan(1.0)*4.0);
     591           0 :     int iFacesAdded = 0;
     592           0 :     float xOffset[] = {(width / 2 - fRadius), -(width / 2 - fRadius), -(width / 2 - fRadius), (width / 2 - fRadius), (width / 2 - fRadius)};
     593           0 :     float yOffset[] = {(height / 2 - fRadius), -(height / 2 - fRadius), (height / 2 - fRadius)};
     594           0 :     float zOffset[] = {-(depth / 2 - fRadius), -(depth / 2 - fRadius), (depth / 2 - fRadius), (depth / 2 - fRadius), -(depth / 2 - fRadius)};
     595           0 :     int iIndices[] = {0, 1, 2, 2, 3, 0};
     596           0 :     while(iStepsY <= iSubDivY)
     597             :     {
     598           0 :         float fNextAngleY = fCurAngleY+fAddAngleY;
     599           0 :         if ((fCurAngleY < 90.0f) && (fNextAngleY >= 90.0f))
     600             :         {
     601           0 :             fNextAngleY = 90.0f;
     602             :         }
     603           0 :         else if ((fCurAngleY < 180.0f) && (fNextAngleY >= 180.0f))
     604             :         {
     605           0 :             fNextAngleY = 180.0f;
     606             :         }
     607           0 :         else if ((fCurAngleY < 270.0f) && (fNextAngleY >= 270.0f))
     608             :         {
     609           0 :             fNextAngleY = 270.0f;
     610             :         }
     611           0 :         else if ((fCurAngleY < 360.0f) && (fNextAngleY >= 360.0f))
     612             :         {
     613           0 :             fNextAngleY = 360.0f;
     614             :         }
     615           0 :         float fSineY = sin(fCurAngleY/180.0f*PI), fCosY = cos(fCurAngleY/180.0f*PI);
     616           0 :         float fNextSineY = sin(fNextAngleY/180.0f*PI), fNextCosY = cos(fNextAngleY/180.0f*PI);
     617           0 :         glm::vec3 vDirY(fCosY, 0.0f, -fSineY), vNextDirY(fNextCosY, 0.0f, -fNextSineY);
     618           0 :         float fCurAngleZ = 0.0f;
     619           0 :         int iStepsZ = 1;
     620           0 :         int xzIndex = 0;
     621           0 :         if ((fCurAngleY >= 0) && (fCurAngleY < 90.0))
     622             :         {
     623           0 :             xzIndex = 0;
     624             :         }
     625           0 :         else if ((fCurAngleY >= 90) && (fCurAngleY < 180.0))
     626             :         {
     627           0 :             xzIndex = 1;
     628             :         }
     629           0 :         else if ((fCurAngleY >= 180) && (fCurAngleY < 270.0))
     630             :         {
     631           0 :             xzIndex = 2;
     632             :         }
     633           0 :         else if ((fCurAngleY >= 270) && (fCurAngleY < 360.0))
     634             :         {
     635           0 :             xzIndex = 3;
     636             :         }
     637           0 :         while(iStepsZ <= iSubDivZ)
     638             :         {
     639           0 :             int yIndex = 0;
     640           0 :             float fNextAngleZ = fCurAngleZ+fAddAngleZ;
     641             : 
     642           0 :             float fSineZ = sin(fCurAngleZ/180.0f*PI), fCosZ = cos(fCurAngleZ/180.0f*PI);
     643           0 :             float fNextSineZ = sin(fNextAngleZ/180.0f*PI), fNextCosZ = cos(fNextAngleZ/180.0f*PI);
     644             : 
     645           0 :             if ((fCurAngleZ < 90.0f) && (fNextAngleZ >= 90.0f))
     646             :             {
     647           0 :                 fNextAngleZ = 90.0f;
     648             :             }
     649             : 
     650           0 :             if ((fCurAngleZ >= 0.0f) && (fCurAngleZ < 90.0f))
     651             :             {
     652           0 :                 yIndex = 0;
     653             :             }
     654             :             else
     655             :             {
     656           0 :                 yIndex = 1;
     657             :             }
     658             : 
     659             :             glm::vec3 vQuadPoints[] =
     660             :             {
     661           0 :                 glm::vec3(vDirY.x*fSineZ*fRadius, fCosZ*fRadius, vDirY.z*fSineZ*fRadius),
     662           0 :                 glm::vec3(vDirY.x*fNextSineZ*fRadius, fNextCosZ*fRadius, vDirY.z*fNextSineZ*fRadius),
     663           0 :                 glm::vec3(vNextDirY.x*fNextSineZ*fRadius, fNextCosZ*fRadius, vNextDirY.z*fNextSineZ*fRadius),
     664           0 :                 glm::vec3(vNextDirY.x*fSineZ*fRadius, fCosZ*fRadius, vNextDirY.z*fSineZ*fRadius)
     665           0 :             };
     666             : 
     667             :             glm::vec3 vActualQuadPoints[] =
     668             :             {
     669             : 
     670           0 :                 glm::vec3(vQuadPoints[0].x + xOffset[xzIndex], vQuadPoints[0].y + yOffset[yIndex], vQuadPoints[0].z + zOffset[xzIndex]),
     671           0 :                 glm::vec3(vQuadPoints[1].x + xOffset[xzIndex], vQuadPoints[1].y + yOffset[yIndex], vQuadPoints[1].z + zOffset[xzIndex]),
     672           0 :                 glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex]),
     673           0 :                 glm::vec3(vQuadPoints[3].x + xOffset[xzIndex], vQuadPoints[3].y + yOffset[yIndex], vQuadPoints[3].z + zOffset[xzIndex])
     674           0 :             };
     675             : 
     676             :             glm::vec3 vNormals[] =
     677             :             {
     678             :                 glm::normalize(vQuadPoints[0]),
     679             :                 glm::normalize(vQuadPoints[1]),
     680             :                 glm::normalize(vQuadPoints[2]),
     681             :                 glm::normalize(vQuadPoints[3])
     682           0 :             };
     683           0 :             for (int i = 0; i < 6; i++)
     684             :             {
     685           0 :                 int index = iIndices[i];
     686           0 :                 vertices.push_back(vActualQuadPoints[index]);
     687           0 :                 normals.push_back(vNormals[index]);
     688             :             }
     689           0 :             iFacesAdded += 2; // Keep count of added faces
     690           0 :             if (((fCurAngleY < 90.0) && (fNextAngleY >= 90.0)) ||
     691           0 :                 ((fCurAngleY < 180.0) && (fNextAngleY >= 180.0)) ||
     692           0 :                 ((fCurAngleY < 270.0) && (fNextAngleY >= 270.0)) ||
     693           0 :                 ((fCurAngleY < 360.0) && (fNextAngleY >= 360.0)))
     694             :             {
     695             :                 glm::vec3 vXZQuadNextPoints[] =
     696             :                 {
     697           0 :                     glm::vec3(vQuadPoints[3].x + xOffset[xzIndex], vQuadPoints[3].y + yOffset[yIndex], vQuadPoints[3].z + zOffset[xzIndex]),
     698           0 :                     glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex]),
     699           0 :                     glm::vec3(vQuadPoints[2].x + xOffset[xzIndex + 1], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex + 1]),
     700           0 :                     glm::vec3(vQuadPoints[3].x + xOffset[xzIndex + 1], vQuadPoints[3].y + yOffset[yIndex], vQuadPoints[3].z + zOffset[xzIndex + 1])
     701           0 :                 };
     702             :                 glm::vec3 vXZNextNormals[] =
     703             :                 {
     704             :                     glm::normalize(vQuadPoints[3]),
     705             :                     glm::normalize(vQuadPoints[2]),
     706             :                     glm::normalize(vQuadPoints[2]),
     707             :                     glm::normalize(vQuadPoints[3])
     708           0 :                 };
     709           0 :                 for (int i = 0; i < 6; i++)
     710             :                 {
     711           0 :                     int index = iIndices[i];
     712           0 :                     vertices.push_back(vXZQuadNextPoints[index]);
     713           0 :                     normals.push_back(vXZNextNormals[index]);
     714             :                 }
     715           0 :                 iFacesAdded += 2;
     716             :             }
     717           0 :             if ((fCurAngleZ < 90.0) && (fNextAngleZ >= 90.0))
     718             :             {
     719             :                 glm::vec3 vYQuadNextPoints[] =
     720             :                 {
     721           0 :                     glm::vec3(vQuadPoints[1].x + xOffset[xzIndex], vQuadPoints[1].y + yOffset[yIndex], vQuadPoints[1].z + zOffset[xzIndex]),
     722           0 :                     glm::vec3(vQuadPoints[1].x + xOffset[xzIndex], vQuadPoints[1].y + yOffset[yIndex + 1], vQuadPoints[1].z + zOffset[xzIndex]),
     723           0 :                     glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex + 1], vQuadPoints[2].z + zOffset[xzIndex]),
     724           0 :                     glm::vec3(vQuadPoints[2].x + xOffset[xzIndex], vQuadPoints[2].y + yOffset[yIndex], vQuadPoints[2].z + zOffset[xzIndex])
     725           0 :                 };
     726             :                 glm::vec3 vYNextNormals[] =
     727             :                 {
     728             :                     glm::normalize(vQuadPoints[1]),
     729             :                     glm::normalize(vQuadPoints[1]),
     730             :                     glm::normalize(vQuadPoints[2]),
     731             :                     glm::normalize(vQuadPoints[2])
     732           0 :                 };
     733           0 :                 for (int i = 0; i < 6; i++)
     734             :                 {
     735           0 :                     int index = iIndices[i];
     736           0 :                     vertices.push_back(vYQuadNextPoints[index]);
     737           0 :                     normals.push_back(vYNextNormals[index]);
     738             :                 }
     739           0 :                 iFacesAdded += 2;
     740             :             }
     741           0 :             iStepsZ++;
     742           0 :             if ((iStepsZ > iSubDivZ) && (fCurAngleZ < 180.0f))
     743             :             {
     744           0 :                 iStepsZ--;
     745             :             }
     746           0 :             fCurAngleZ = fNextAngleZ;
     747             :         }
     748           0 :         iStepsY++;
     749           0 :         if ((iStepsY > iSubDivY) && (fCurAngleY < 360.0f))
     750             :         {
     751           0 :             iStepsY--;
     752             :         }
     753           0 :         fCurAngleY = fNextAngleY;
     754             :     }
     755             :     //draw the rectangle face
     756             :     //top surface
     757             :     glm::vec3 vTopPoints[] =
     758             :     {
     759           0 :         glm::vec3(xOffset[0], height / 2, zOffset[0]),
     760           0 :         glm::vec3(xOffset[1], height / 2, zOffset[1]),
     761           0 :         glm::vec3(xOffset[2], height / 2, zOffset[2]),
     762           0 :         glm::vec3(xOffset[3], height / 2, zOffset[3])
     763           0 :     };
     764           0 :     glm::vec3 vTopNormal = glm::vec3(0.0f, 1.0f, 0.0f);
     765           0 :     for (int i = 0; i < 6; i++)
     766             :     {
     767           0 :         int index = iIndices[i];
     768           0 :         vertices.push_back(vTopPoints[index]);
     769           0 :         normals.push_back(vTopNormal);
     770             :     }
     771           0 :     iFacesAdded += 2;
     772             :     //bottom surface
     773             :     glm::vec3 vBottomPoints[] =
     774             :     {
     775           0 :         glm::vec3(xOffset[3], -height / 2, zOffset[3]),
     776           0 :         glm::vec3(xOffset[2], -height / 2, zOffset[2]),
     777           0 :         glm::vec3(xOffset[1], -height / 2, zOffset[1]),
     778           0 :         glm::vec3(xOffset[0], -height / 2, zOffset[0])
     779           0 :     };
     780           0 :     glm::vec3 vBottomNormal = glm::vec3(0.0f, -1.0f, 0.0f);
     781           0 :     for (int i = 0; i < 6; i++)
     782             :     {
     783           0 :         int index = iIndices[i];
     784           0 :         vertices.push_back(vBottomPoints[index]);
     785           0 :         normals.push_back(vBottomNormal);
     786             :     }
     787           0 :     iFacesAdded += 2;
     788             :     //left surface
     789             :     glm::vec3 vLeftPoints[] =
     790             :     {
     791           0 :         glm::vec3(-width / 2, yOffset[0], zOffset[0]),
     792           0 :         glm::vec3(-width / 2, yOffset[1], zOffset[1]),
     793           0 :         glm::vec3(-width / 2, yOffset[1], zOffset[2]),
     794           0 :         glm::vec3(-width / 2, yOffset[0], zOffset[3])
     795           0 :     };
     796           0 :     glm::vec3 vLeftNormal = glm::vec3(-1.0f, 0.0f, 0.0f);
     797           0 :     for (int i = 0; i < 6; i++)
     798             :     {
     799           0 :         int index = iIndices[i];
     800           0 :         vertices.push_back(vLeftPoints[index]);
     801           0 :         normals.push_back(vLeftNormal);
     802             :     }
     803             :     //right surface
     804             :     glm::vec3 vRightPoints[] =
     805             :     {
     806           0 :         glm::vec3(width / 2, yOffset[0], zOffset[3]),
     807           0 :         glm::vec3(width / 2, yOffset[1], zOffset[2]),
     808           0 :         glm::vec3(width / 2, yOffset[1], zOffset[1]),
     809           0 :         glm::vec3(width / 2, yOffset[0], zOffset[0])
     810           0 :     };
     811           0 :     glm::vec3 vRightNormal = glm::vec3(1.0f, 0.0f, 0.0f);
     812           0 :     for (int i = 0; i < 6; i++)
     813             :     {
     814           0 :         int index = iIndices[i];
     815           0 :         vertices.push_back(vRightPoints[index]);
     816           0 :         normals.push_back(vRightNormal);
     817             :     }
     818           0 :     iFacesAdded += 2;
     819             :     //front surface
     820             :     glm::vec3 vFrontPoints[] =
     821             :     {
     822           0 :         glm::vec3(xOffset[0], yOffset[0], depth / 2),
     823           0 :         glm::vec3(xOffset[1], yOffset[0], depth / 2),
     824           0 :         glm::vec3(xOffset[2], yOffset[1], depth / 2),
     825           0 :         glm::vec3(xOffset[3], yOffset[1], depth / 2)
     826           0 :     };
     827           0 :     glm::vec3 vFrontNormal = glm::vec3(0.0f, 0.0f, 1.0f);
     828           0 :     for (int i = 0; i < 6; i++)
     829             :     {
     830           0 :         int index = iIndices[i];
     831           0 :         vertices.push_back(vFrontPoints[index]);
     832           0 :         normals.push_back(vFrontNormal);
     833             :     }
     834             :     //back surface
     835             :     glm::vec3 vBackPoints[] =
     836             :     {
     837           0 :         glm::vec3(xOffset[0], yOffset[1], -depth / 2),
     838           0 :         glm::vec3(xOffset[1], yOffset[1], -depth / 2),
     839           0 :         glm::vec3(xOffset[2], yOffset[0], -depth / 2),
     840           0 :         glm::vec3(xOffset[3], yOffset[0], -depth / 2)
     841           0 :     };
     842           0 :     glm::vec3 vBackNormal = glm::vec3(0.0f, 0.0f, -1.0f);
     843           0 :     for (int i = 0; i < 6; i++)
     844             :     {
     845           0 :         int index = iIndices[i];
     846           0 :         vertices.push_back(vBackPoints[index]);
     847           0 :         normals.push_back(vBackNormal);
     848             :     }
     849           0 :     iFacesAdded += 2;
     850           0 :     return iFacesAdded;
     851             : }
     852             : 
     853           0 : void OpenGL3DRenderer::RenderLine3D(const Polygon3DInfo& polygon)
     854             : {
     855           0 :     CHECK_GL_ERROR();
     856           0 :     glUseProgram(maResources.m_CommonProID);
     857           0 :     PosVecf3 trans = {0.0f, 0, 0.0};
     858           0 :     PosVecf3 angle = {0.0f, 0.0f, 0.0f};
     859           0 :     PosVecf3 scale = {1.0f, 1.0f, 1.0f};
     860           0 :     MoveModelf(trans, angle, scale);
     861           0 :     m_Model = m_GlobalScaleMatrix * m_Model;
     862           0 :     m_3DMVP = m_3DProjection * m_3DView * m_Model;
     863             : 
     864           0 :     for (size_t i = 0; i < polygon.verticesList.size(); i++)
     865             :     {
     866             :         //move the circle to the pos, and scale using the xScale and Y scale
     867           0 :         Vertices3D *pointList = polygon.verticesList[i];
     868             :         //if line only, using the common shader to render
     869             : 
     870             :         //fill vertex buffer
     871           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
     872           0 :         glBufferData(GL_ARRAY_BUFFER, pointList->size() * sizeof(glm::vec3), &pointList[0][0], GL_STATIC_DRAW);
     873           0 :         if(mbPickingMode)
     874           0 :             glUniform4fv(maResources.m_2DColorID, 1, &polygon.id[0]);
     875             :         else
     876           0 :             glUniform4fv(maResources.m_2DColorID, 1, &polygon.polygonColor[0]);
     877           0 :         glUniformMatrix4fv(maResources.m_MatrixID, 1, GL_FALSE, &m_3DMVP[0][0]);
     878           0 :         CHECK_GL_ERROR();
     879             : 
     880             :         // 1rst attribute buffer : vertices
     881           0 :         glEnableVertexAttribArray(maResources.m_2DVertexID);
     882           0 :         CHECK_GL_ERROR();
     883           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
     884           0 :         CHECK_GL_ERROR();
     885             :         glVertexAttribPointer(maResources.m_2DVertexID, // attribute
     886             :                                 3,                  // size
     887             :                                 GL_FLOAT,           // type
     888             :                                 GL_FALSE,           // normalized?
     889             :                                 0,                  // stride
     890             :                                 nullptr            // array buffer offset
     891           0 :                                 );
     892             : 
     893           0 :         CHECK_GL_ERROR();
     894           0 :         glLineWidth(polygon.lineWidth);
     895           0 :         CHECK_GL_ERROR();
     896           0 :         glDrawArrays(GL_LINE_STRIP, 0, pointList->size());
     897           0 :         CHECK_GL_ERROR();
     898           0 :         glDisableVertexAttribArray(maResources.m_2DVertexID);
     899           0 :         CHECK_GL_ERROR();
     900           0 :         glBindBuffer(GL_ARRAY_BUFFER, 0);
     901           0 :         CHECK_GL_ERROR();
     902             :     }
     903           0 :     glUseProgram(0);
     904           0 :     CHECK_GL_ERROR();
     905           0 : }
     906             : 
     907           0 : void OpenGL3DRenderer::RenderPolygon3D(const Polygon3DInfo& polygon)
     908             : {
     909           0 :     CHECK_GL_ERROR();
     910           0 :     size_t verticesNum = polygon.verticesList.size();
     911           0 :     size_t normalsNum = polygon.normalsList.size();
     912             :     //check whether the number of vertices and normals are equal
     913           0 :     if (m_CameraInfo.cameraPos.z >= 0.0f)
     914           0 :         glPolygonOffset(1.0, 1.0);
     915             :     else
     916           0 :         glPolygonOffset(-1.0, -1.0);
     917           0 :     CHECK_GL_ERROR();
     918           0 :     if (verticesNum != normalsNum)
     919             :     {
     920           0 :         return ;
     921             :     }
     922           0 :     glBindBuffer(GL_UNIFORM_BUFFER, 0);
     923           0 :     CHECK_GL_ERROR();
     924           0 :     if(mbPickingMode)
     925             :     {
     926           0 :         glUseProgram(maPickingResources.m_CommonProID);
     927           0 :         float minCoordX = 0.0f;
     928           0 :         float maxCoordX = m_fMinCoordX + m_fMaxCoordX;
     929           0 :         glUniform1fv(maPickingResources.m_MinCoordXID, 1, &minCoordX);
     930           0 :         glUniform1fv(maPickingResources.m_MaxCoordXID, 1, &maxCoordX);
     931             :     }
     932             :     else
     933             :     {
     934           0 :         glUseProgram(maResources.m_3DProID);
     935           0 :         glUniformMatrix4fv(maResources.m_3DViewID, 1, GL_FALSE, &m_3DView[0][0]);
     936           0 :         glUniformMatrix4fv(maResources.m_3DProjectionID, 1, GL_FALSE, &m_3DProjection[0][0]);
     937           0 :         CHECK_GL_ERROR();
     938           0 :         if (maResources.m_b330Support)
     939             :         {
     940             :             //update ubo
     941           0 :             Update3DUniformBlock();
     942           0 :             glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
     943           0 :             glBufferSubData(GL_UNIFORM_BUFFER, m_3DActualSizeLight, sizeof(MaterialParameters), &polygon.material);
     944           0 :             CHECK_GL_ERROR();
     945             :         }
     946             :         else
     947             :         {
     948           0 :             float minCoordX = 0.0f;
     949           0 :             float maxCoordX = m_fMinCoordX + m_fMaxCoordX;
     950           0 :             glUniform1fv(maResources.m_3DMinCoordXID, 1, &minCoordX);
     951           0 :             glUniform1fv(maResources.m_3DMaxCoordXID, 1, &maxCoordX);
     952           0 :             glUniform1i(maResources.m_3DUndrawID, m_bUndrawFlag);
     953           0 :             CHECK_GL_ERROR();
     954             :             //update light information
     955           0 :             glUniform4fv(maResources.m_3DLightColorID, m_iLightNum, reinterpret_cast<GLfloat*>(m_LightColor));
     956           0 :             glUniform4fv(maResources.m_3DLightPosID, m_iLightNum, reinterpret_cast<GLfloat*>(m_PositionWorldspace));
     957           0 :             glUniform1fv(maResources.m_3DLightPowerID, m_iLightNum, m_fLightPower);
     958           0 :             glUniform1i(maResources.m_3DLightNumID, m_iLightNum);
     959           0 :             glUniform4fv(maResources.m_3DLightAmbientID, 1, &m_Ambient[0]);
     960           0 :             CHECK_GL_ERROR();
     961             :             //update meterial information
     962           0 :             glUniform4fv(maResources.m_3DMaterialAmbientID, 1, &polygon.material.ambient[0]);
     963           0 :             glUniform4fv(maResources.m_3DMaterialDiffuseID, 1, &polygon.material.diffuse[0]);
     964           0 :             glUniform4fv(maResources.m_3DMaterialSpecularID, 1, &polygon.material.specular[0]);
     965           0 :             glUniform4fv(maResources.m_3DMaterialColorID, 1, &polygon.material.materialColor[0]);
     966           0 :             glUniform1i(maResources.m_3DMaterialTwoSidesID, polygon.material.twoSidesLighting);
     967           0 :             glUniform1f(maResources.m_3DMaterialShininessID, polygon.material.shininess);
     968           0 :             CHECK_GL_ERROR();
     969             :         }
     970             :     }
     971           0 :     for (size_t i = 0; i < verticesNum; i++)
     972             :     {
     973             :         //move the circle to the pos, and scale using the xScale and Y scale
     974           0 :         Vertices3D *pointList = polygon.verticesList[i];
     975           0 :         Normals3D *normalList = polygon.normalsList[i];
     976           0 :         PosVecf3 trans = {0.0f, 0.0f, 0.0};
     977           0 :         PosVecf3 angle = {0.0f, 0.0f, 0.0f};
     978           0 :         PosVecf3 scale = {1.0f, 1.0f, 1.0f};
     979           0 :         MoveModelf(trans, angle, scale);
     980           0 :         m_Model = m_GlobalScaleMatrix * m_Model;
     981           0 :         glm::mat3 normalMatrix(m_Model);
     982           0 :         glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
     983             : 
     984             :         //render to fbo
     985             :         //fill vertex buffer
     986           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
     987           0 :         glBufferData(GL_ARRAY_BUFFER, pointList->size() * sizeof(glm::vec3), &pointList[0][0], GL_STATIC_DRAW);
     988           0 :         CHECK_GL_ERROR();
     989             : 
     990           0 :         if(!mbPickingMode)
     991             :         {
     992             :             //fill normal buffer
     993           0 :             glBindBuffer(GL_ARRAY_BUFFER, m_NormalBuffer);
     994           0 :             CHECK_GL_ERROR();
     995           0 :             glBufferData(GL_ARRAY_BUFFER, normalList->size() * sizeof(glm::vec3), &normalList[0][0], GL_STATIC_DRAW);
     996           0 :             CHECK_GL_ERROR();
     997           0 :             glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
     998           0 :             CHECK_GL_ERROR();
     999           0 :             glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
    1000           0 :             CHECK_GL_ERROR();
    1001             :         }
    1002             :         else
    1003             :         {
    1004           0 :             glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    1005           0 :             glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
    1006           0 :             glUniform4fv(maPickingResources.m_2DColorID, 1, &polygon.id[0]);
    1007             :         }
    1008           0 :         CHECK_GL_ERROR();
    1009           0 :         GLint maVertexID = mbPickingMode ? maPickingResources.m_2DVertexID : maResources.m_3DVertexID;
    1010             :         // 1rst attribute buffer : vertices
    1011           0 :         glEnableVertexAttribArray(maVertexID);
    1012           0 :         CHECK_GL_ERROR();
    1013           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    1014           0 :         CHECK_GL_ERROR();
    1015             :         glVertexAttribPointer(maVertexID, // attribute
    1016             :                                 3,                  // size
    1017             :                                 GL_FLOAT,           // type
    1018             :                                 GL_FALSE,           // normalized?
    1019             :                                 0,                  // stride
    1020             :                                 nullptr            // array buffer offset
    1021           0 :                                 );
    1022           0 :         CHECK_GL_ERROR();
    1023           0 :         if(!mbPickingMode)
    1024             :         {
    1025             :             // 2nd attribute buffer : normals
    1026           0 :             glEnableVertexAttribArray(maResources.m_3DNormalID);
    1027           0 :             CHECK_GL_ERROR();
    1028           0 :             glBindBuffer(GL_ARRAY_BUFFER, m_NormalBuffer);
    1029           0 :             CHECK_GL_ERROR();
    1030             :             glVertexAttribPointer(maResources.m_3DNormalID, // attribute
    1031             :                     3,                  // size
    1032             :                     GL_FLOAT,           // type
    1033             :                     GL_FALSE,           // normalized?
    1034             :                     0,                  // stride
    1035             :                     nullptr            // array buffer offset
    1036           0 :                     );
    1037             :         }
    1038           0 :         CHECK_GL_ERROR();
    1039           0 :         glDrawArrays(GL_POLYGON, 0, pointList->size());
    1040           0 :         CHECK_GL_ERROR();
    1041           0 :         glDisableVertexAttribArray(maVertexID);
    1042           0 :         CHECK_GL_ERROR();
    1043           0 :         if(!mbPickingMode)
    1044           0 :             glDisableVertexAttribArray(maResources.m_3DNormalID);
    1045             : 
    1046           0 :         CHECK_GL_ERROR();
    1047           0 :         glBindBuffer(GL_ARRAY_BUFFER, 0);
    1048           0 :         CHECK_GL_ERROR();
    1049             :     }
    1050           0 :     glUseProgram(0);
    1051           0 :     CHECK_GL_ERROR();
    1052             : }
    1053             : 
    1054             : namespace {
    1055             : 
    1056             : template< typename T >
    1057             : struct DeletePointer
    1058             : {
    1059           0 :     void operator()(T* p)
    1060             :     {
    1061           0 :         delete p;
    1062           0 :     }
    1063             : };
    1064             : 
    1065             : }
    1066             : 
    1067           0 : void OpenGL3DRenderer::ReleasePolygonShapes()
    1068             : {
    1069           0 :     for (size_t i = 0; i < m_Polygon3DInfoList.size(); i++)
    1070             :     {
    1071           0 :         Polygon3DInfo &polygon = m_Polygon3DInfoList[i];
    1072             :         std::for_each(polygon.verticesList.begin(),
    1073           0 :                 polygon.verticesList.end(), DeletePointer<Vertices3D>());
    1074             :         std::for_each(polygon.normalsList.begin(),
    1075           0 :                 polygon.normalsList.end(), DeletePointer<Normals3D>());
    1076           0 :         delete polygon.vertices;
    1077           0 :         delete polygon.normals;
    1078             :     }
    1079           0 :     m_Polygon3DInfoList.clear();
    1080           0 : }
    1081             : 
    1082           0 : void OpenGL3DRenderer::RenderPolygon3DObject()
    1083             : {
    1084           0 :     glDepthMask(GL_FALSE);
    1085           0 :     CHECK_GL_ERROR();
    1086           0 :     for (size_t i = 0; i < m_Polygon3DInfoList.size(); i++)
    1087             :     {
    1088           0 :         Polygon3DInfo &polygon = m_Polygon3DInfoList[i];
    1089           0 :         if (polygon.lineOnly || (!polygon.fillStyle))
    1090             :         {
    1091             :             //just use the common shader is ok for lines
    1092           0 :             RenderLine3D(polygon);
    1093           0 :             CHECK_GL_ERROR();
    1094             :         }
    1095             :         else
    1096             :         {
    1097           0 :             RenderPolygon3D(polygon);
    1098             :         }
    1099             :     }
    1100           0 :     glDepthMask(GL_TRUE);
    1101           0 :     return;
    1102             : }
    1103             : 
    1104           0 : void OpenGL3DRenderer::Set3DSenceInfo(sal_uInt32 nColor, bool twoSidesLighting)
    1105             : {
    1106           0 :     m_Polygon3DInfo.material.twoSidesLighting = twoSidesLighting;
    1107           0 :     if (maResources.m_b330Support)
    1108             :     {
    1109           0 :         m_LightsInfo.ambient = getColorAsVector(nColor);
    1110           0 :         m_LightsInfo.lightNum = 0;
    1111             :     }
    1112             :     else
    1113             :     {
    1114           0 :         m_iLightNum = 0;
    1115           0 :         m_Ambient = getColorAsVector(nColor);;
    1116             :     }
    1117           0 :     SetLightInfo(true, 0xFFFFFF, glm::vec4(-1.0, -1.0, 1.0, 0.0));
    1118           0 :     SetLightInfo(true, 0xFFFFFF, glm::vec4(-1.0, 1.0, 1.0, 0.0));
    1119           0 :     SetLightInfo(true, 0xFFFFFF, glm::vec4(1.0, -1.0, 1.0, 0.0));
    1120           0 :     SetLightInfo(true, 0xFFFFFF, glm::vec4(1.0, 1.0, 1.0, 0.0));
    1121           0 : }
    1122             : 
    1123           0 : void OpenGL3DRenderer::SetLightInfo(bool lightOn, sal_uInt32 nColor, const glm::vec4& direction)
    1124             : {
    1125           0 :     if (lightOn)
    1126             :     {
    1127           0 :         if (maResources.m_b330Support)
    1128             :         {
    1129           0 :             if (m_LightsInfo.lightNum >= MAX_LIGHT_NUM)
    1130             :             {
    1131           0 :                 return;
    1132             :             }
    1133           0 :             m_LightsInfo.light[m_LightsInfo.lightNum].lightColor = getColorAsVector(nColor);
    1134           0 :             m_LightsInfo.light[m_LightsInfo.lightNum].positionWorldspace = direction;
    1135           0 :             m_LightsInfo.light[m_LightsInfo.lightNum].lightPower = 0.3f;
    1136           0 :             m_LightsInfo.lightNum++;
    1137             :         }
    1138             :         else
    1139             :         {
    1140           0 :             if (m_iLightNum >= MAX_LIGHT_NUM)
    1141             :             {
    1142           0 :                 return;
    1143             :             }
    1144           0 :             m_LightColor[m_iLightNum] = getColorAsVector(nColor);
    1145           0 :             m_PositionWorldspace[m_iLightNum] = direction;
    1146           0 :             m_fLightPower[m_iLightNum] = 0.3f;
    1147           0 :             m_iLightNum++;
    1148             :         }
    1149             :     }
    1150             : }
    1151             : 
    1152           0 : void OpenGL3DRenderer::AddShapePolygon3DObject(sal_uInt32 nColor, bool lineOnly, sal_uInt32 nLineColor,long fillStyle, sal_uInt32 specular, sal_uInt32 nUniqueId)
    1153             : {
    1154           0 :     m_Polygon3DInfo.polygonColor = getColorAsVector(nColor);
    1155           0 :     m_Polygon3DInfo.id = getColorAsVector(nUniqueId);
    1156           0 :     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           0 :     m_Polygon3DInfo.lineOnly = lineOnly;
    1159             : 
    1160             :     // if line only, use line color
    1161           0 :     if (m_Polygon3DInfo.lineOnly)
    1162             :     {
    1163           0 :         m_Polygon3DInfo.polygonColor = getColorAsVector(nLineColor);
    1164             :     }
    1165             : 
    1166             :     //fillStyle
    1167           0 :     m_Polygon3DInfo.fillStyle= fillStyle;
    1168             : 
    1169             :     //material specular;
    1170           0 :     m_Polygon3DInfo.material.specular = getColorAsVector(specular);
    1171             : 
    1172           0 :     m_Polygon3DInfo.material.diffuse = glm::vec4(1.0, 1.0, 1.0, 1.0);
    1173             : 
    1174           0 :     m_Polygon3DInfo.material.ambient = glm::vec4(0.2, 0.2, 0.2, 1.0);
    1175             : 
    1176           0 :     m_Polygon3DInfo.material.shininess = 1.0f;
    1177           0 : }
    1178             : 
    1179           0 : void OpenGL3DRenderer::EndAddShapePolygon3DObject()
    1180             : {
    1181           0 :     m_Polygon3DInfoList.push_back(m_Polygon3DInfo);
    1182           0 :     m_Polygon3DInfo.normals = NULL;
    1183           0 :     m_Polygon3DInfo.vertices = NULL;
    1184             :     // TODO: moggi: memory leak???
    1185           0 :     m_Polygon3DInfo.verticesList.clear();
    1186           0 :     m_Polygon3DInfo.normalsList.clear();
    1187           0 : }
    1188             : 
    1189           0 : void OpenGL3DRenderer::AddPolygon3DObjectNormalPoint(float x, float y, float z)
    1190             : {
    1191           0 :     if (m_Polygon3DInfo.fillStyle)
    1192             :     {
    1193           0 :         if (!m_Polygon3DInfo.normals)
    1194             :         {
    1195           0 :             m_Polygon3DInfo.normals = new Normals3D;
    1196             :         }
    1197           0 :         m_Polygon3DInfo.normals->push_back(glm::vec3(x, y, z));
    1198             :     }
    1199           0 : }
    1200             : 
    1201           0 : void OpenGL3DRenderer::EndAddPolygon3DObjectNormalPoint()
    1202             : {
    1203           0 :     m_Polygon3DInfo.normalsList.push_back(m_Polygon3DInfo.normals);
    1204           0 :     m_Polygon3DInfo.normals = NULL;
    1205           0 : }
    1206             : 
    1207           0 : void OpenGL3DRenderer::AddPolygon3DObjectPoint(float x, float y, float z)
    1208             : {
    1209           0 :     if (!m_Polygon3DInfo.vertices)
    1210             :     {
    1211           0 :         m_Polygon3DInfo.vertices = new Vertices3D;
    1212             :     }
    1213             :     //float actualX = x - (float)m_iWidth / 2;
    1214             :     //float actualY = y  - (float)m_iHeight / 2;
    1215           0 :     float actualX = x;
    1216           0 :     float actualY = y;
    1217           0 :     float actualZ = z;
    1218           0 :     m_Polygon3DInfo.vertices->push_back(glm::vec3(actualX, actualY, actualZ));
    1219           0 : }
    1220             : 
    1221           0 : void OpenGL3DRenderer::EndAddPolygon3DObjectPoint()
    1222             : {
    1223           0 :     m_Polygon3DInfo.verticesList.push_back(m_Polygon3DInfo.vertices);
    1224           0 :     m_Polygon3DInfo.vertices = NULL;
    1225           0 : }
    1226             : 
    1227           0 : void OpenGL3DRenderer::AddShape3DExtrudeObject(bool roundedCorner, sal_uInt32 nColor, sal_uInt32 specular, const glm::mat4& modelMatrix, sal_uInt32 nUniqueId)
    1228             : {
    1229           0 :     m_Extrude3DInfo.id = getColorAsVector(nUniqueId);
    1230           0 :     m_Extrude3DInfo.orgID = nUniqueId;
    1231           0 :     glm::vec4 tranform = modelMatrix * glm::vec4(0.0, 0.0, 0.0, 1.0);
    1232           0 :     glm::vec4 DirX = modelMatrix * glm::vec4(1.0, 0.0, 0.0, 0.0);
    1233           0 :     glm::vec4 DirY = modelMatrix * glm::vec4(0.0, 1.0, 0.0, 0.0);
    1234           0 :     glm::vec4 DirZ = modelMatrix * glm::vec4(0.0, 0.0, 1.0, 0.0);
    1235           0 :     m_Extrude3DInfo.xScale = glm::length(DirX);
    1236           0 :     m_Extrude3DInfo.yScale = glm::length(DirY);
    1237           0 :     m_Extrude3DInfo.zScale = glm::length(DirZ);
    1238           0 :     glm::mat4 transformMatrixInverse = glm::inverse(glm::translate(glm::vec3(tranform)));
    1239           0 :     glm::mat4 scaleMatrixInverse = glm::inverse(glm::scale(glm::vec3(m_Extrude3DInfo.xScale, m_Extrude3DInfo.yScale, m_Extrude3DInfo.zScale)));
    1240           0 :     m_Extrude3DInfo.rotation = transformMatrixInverse * modelMatrix * scaleMatrixInverse;
    1241             : 
    1242             :     //color
    1243           0 :     m_Extrude3DInfo.extrudeColor = getColorAsVector(nColor);
    1244           0 :     m_Extrude3DInfo.material.materialColor = m_Extrude3DInfo.extrudeColor;//material color seems to be the same for all parts, so we use the polygon color
    1245             : 
    1246             :     //material specular;
    1247           0 :     m_Extrude3DInfo.material.specular = getColorAsVector(specular);
    1248             : 
    1249           0 :     m_Extrude3DInfo.material.diffuse = glm::vec4(1.0, 1.0, 1.0, 1.0);
    1250             : 
    1251           0 :     m_Extrude3DInfo.material.ambient = glm::vec4(0.2, 0.2, 0.2, 1.0);
    1252             : 
    1253           0 :     m_Extrude3DInfo.material.shininess = 1.0f;
    1254           0 :     m_Extrude3DInfo.xTransform = tranform.x;
    1255           0 :     m_Extrude3DInfo.yTransform = tranform.y;
    1256           0 :     m_Extrude3DInfo.zTransform = tranform.z;
    1257           0 :     float width = 1.0f;
    1258           0 :     float height = m_Extrude3DInfo.yScale / m_Extrude3DInfo.xScale;
    1259           0 :     float radius = height > 0.2f ? 0.2f : height / 4.0f;
    1260           0 :     float depth = 1 + 2 * radius;
    1261           0 :     bool NORoundedCube = (radius > (width / 2)) || (radius > (height / 2)) || (radius > (depth / 2));
    1262           0 :     if (!NORoundedCube && roundedCorner && (m_RoundBarMesh.iMeshSizes == 0))
    1263             :     {
    1264           0 :         CreateActualRoundedCube(radius, CORNER_DIVION_Y, CORNER_DIVION_Z, width, height, depth);
    1265           0 :         AddVertexData(m_CubeVertexBuf);
    1266           0 :         AddNormalData(m_CubeNormalBuf);
    1267           0 :         AddIndexData(m_CubeElementBuf);
    1268           0 :         for (int j = 0; j < 5; j++)
    1269             :         {
    1270           0 :             m_Extrude3DInfo.startIndex[j] = m_RoundBarMesh.iElementStartIndices[j];
    1271           0 :             m_Extrude3DInfo.size[j] = m_RoundBarMesh.iElementSizes[j];
    1272             :         }
    1273           0 :         m_Vertices.clear();
    1274           0 :         m_Normals.clear();
    1275           0 :         m_Indices.clear();
    1276           0 :         m_Extrude3DInfo.rounded = true;
    1277             :     }
    1278           0 :     m_Batchmaterial = m_Extrude3DInfo.material;
    1279           0 : }
    1280             : 
    1281           0 : void OpenGL3DRenderer::EndAddShape3DExtrudeObject()
    1282             : {
    1283           0 :     m_Extrude3DList.push_back(m_Extrude3DInfo);
    1284           0 : }
    1285             : 
    1286           0 : void OpenGL3DRenderer::Init3DUniformBlock()
    1287             : {
    1288           0 :     if(mbPickingMode)
    1289           0 :         return;
    1290             : 
    1291           0 :     GLuint a3DLightBlockIndex = glGetUniformBlockIndex(maResources.m_3DProID, "GlobalLights");
    1292           0 :     GLuint a3DMaterialBlockIndex = glGetUniformBlockIndex(maResources.m_3DProID, "GlobalMaterialParameters");
    1293             : 
    1294           0 :     if ((GL_INVALID_INDEX == a3DLightBlockIndex) || (GL_INVALID_INDEX == a3DMaterialBlockIndex))
    1295             :     {
    1296           0 :         return;
    1297             :     }
    1298           0 :     int nUniformBufferAlignSize = 1;
    1299           0 :     glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &nUniformBufferAlignSize);
    1300           0 :     GLint nBlockDataSizeLight = 0, nBlockDataSizeMertrial = 0;
    1301           0 :     glGetActiveUniformBlockiv(maResources.m_3DProID, a3DLightBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeLight);
    1302           0 :     glGetActiveUniformBlockiv(maResources.m_3DProID, a3DMaterialBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeMertrial);
    1303           0 :     CHECK_GL_ERROR();
    1304           0 :     glGenBuffers(1, &m_3DUBOBuffer);
    1305           0 :     glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
    1306           0 :     CHECK_GL_ERROR();
    1307           0 :     m_3DActualSizeLight = ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize;
    1308             : //    cout << "nBlockDataSizeMertrial = " << nBlockDataSizeMertrial << ", nBlockDataSizeLight = " << nBlockDataSizeLight << ", m_3DActualSizeLight = " << m_3DActualSizeLight << endl;
    1309           0 :     int dataSize = m_3DActualSizeLight + nBlockDataSizeMertrial;
    1310           0 :     glBufferData(GL_UNIFORM_BUFFER, dataSize, NULL, GL_DYNAMIC_DRAW);
    1311           0 :     glBindBufferRange(GL_UNIFORM_BUFFER, 0, m_3DUBOBuffer, 0, nBlockDataSizeLight);
    1312           0 :     CHECK_GL_ERROR();
    1313           0 :     glUniformBlockBinding(maResources.m_3DProID, a3DLightBlockIndex, 0);
    1314             : 
    1315           0 :     glBindBufferRange(GL_UNIFORM_BUFFER, 1, m_3DUBOBuffer, ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize, nBlockDataSizeMertrial);
    1316           0 :     glUniformBlockBinding(maResources.m_3DProID, a3DMaterialBlockIndex, 1);
    1317             :     //for the light source uniform, we must calc the offset of each element
    1318           0 :     CHECK_GL_ERROR();
    1319           0 :     glBindBuffer(GL_UNIFORM_BUFFER, 0);
    1320             : }
    1321             : 
    1322           0 : void OpenGL3DRenderer::InitBatch3DUniformBlock()
    1323             : {
    1324           0 :     if(mbPickingMode)
    1325           0 :         return;
    1326             : 
    1327           0 :     GLuint a3DLightBlockIndex = glGetUniformBlockIndex(maResources.m_3DBatchProID, "GlobalLights");
    1328           0 :     GLuint a3DMaterialBlockIndex = glGetUniformBlockIndex(maResources.m_3DBatchProID, "GlobalMaterialParameters");
    1329             : 
    1330           0 :     if ((GL_INVALID_INDEX == a3DLightBlockIndex) || (GL_INVALID_INDEX == a3DMaterialBlockIndex))
    1331             :     {
    1332           0 :         return;
    1333             :     }
    1334           0 :     int nUniformBufferAlignSize = 1;
    1335           0 :     glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &nUniformBufferAlignSize);
    1336           0 :     GLint nBlockDataSizeLight = 0, nBlockDataSizeMertrial = 0;
    1337           0 :     glGetActiveUniformBlockiv(maResources.m_3DBatchProID, a3DLightBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeLight);
    1338           0 :     glGetActiveUniformBlockiv(maResources.m_3DBatchProID, a3DMaterialBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSizeMertrial);
    1339           0 :     CHECK_GL_ERROR();
    1340           0 :     glGenBuffers(1, &m_Batch3DUBOBuffer);
    1341           0 :     glBindBuffer(GL_UNIFORM_BUFFER, m_Batch3DUBOBuffer);
    1342           0 :     CHECK_GL_ERROR();
    1343           0 :     m_Batch3DActualSizeLight = ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize;
    1344             : //    cout << "nBlockDataSizeMertrial = " << nBlockDataSizeMertrial << ", nBlockDataSizeLight = " << nBlockDataSizeLight << ", m_3DActualSizeLight = " << m_3DActualSizeLight << endl;
    1345           0 :     int dataSize = m_Batch3DActualSizeLight + nBlockDataSizeMertrial;
    1346           0 :     glBufferData(GL_UNIFORM_BUFFER, dataSize, NULL, GL_DYNAMIC_DRAW);
    1347           0 :     glBindBufferRange(GL_UNIFORM_BUFFER, 2, m_Batch3DUBOBuffer, 0, nBlockDataSizeLight);
    1348           0 :     CHECK_GL_ERROR();
    1349           0 :     glUniformBlockBinding(maResources.m_3DBatchProID, a3DLightBlockIndex, 2);
    1350             : 
    1351           0 :     glBindBufferRange(GL_UNIFORM_BUFFER, 3, m_Batch3DUBOBuffer, ((nBlockDataSizeLight / nUniformBufferAlignSize) + std::min(nBlockDataSizeLight % nUniformBufferAlignSize, 1)) * nUniformBufferAlignSize, nBlockDataSizeMertrial);
    1352           0 :     glUniformBlockBinding(maResources.m_3DBatchProID, a3DMaterialBlockIndex, 3);
    1353             :     //for the light source uniform, we must calc the offset of each element
    1354           0 :     CHECK_GL_ERROR();
    1355           0 :     glBindBuffer(GL_UNIFORM_BUFFER, 0);
    1356             : }
    1357             : 
    1358           0 : void OpenGL3DRenderer::UpdateBatch3DUniformBlock()
    1359             : {
    1360           0 :     if(mbPickingMode)
    1361           0 :         return;
    1362             : 
    1363           0 :     CHECK_GL_ERROR();
    1364           0 :     glBindBuffer(GL_UNIFORM_BUFFER, m_Batch3DUBOBuffer);
    1365           0 :     CHECK_GL_ERROR();
    1366           0 :     glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLint), &m_LightsInfo.lightNum);
    1367           0 :     CHECK_GL_ERROR();
    1368             :     //current std140 alignment: 16
    1369           0 :     glBufferSubData(GL_UNIFORM_BUFFER, 16, sizeof(glm::vec4), &m_LightsInfo.ambient[0]);
    1370           0 :     CHECK_GL_ERROR();
    1371             :     //current std140 alignment: 16
    1372           0 :     glBufferSubData(GL_UNIFORM_BUFFER, 32, sizeof(LightSource) * MAX_LIGHT_NUM, &m_LightsInfo.light);
    1373           0 :     CHECK_GL_ERROR();
    1374           0 :     glBindBuffer(GL_UNIFORM_BUFFER, 0);
    1375             : }
    1376             : 
    1377           0 : void OpenGL3DRenderer::Update3DUniformBlock()
    1378             : {
    1379           0 :     if(mbPickingMode)
    1380           0 :         return;
    1381             : 
    1382           0 :     CHECK_GL_ERROR();
    1383           0 :     glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
    1384           0 :     CHECK_GL_ERROR();
    1385           0 :     glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLint), &m_LightsInfo.lightNum);
    1386           0 :     CHECK_GL_ERROR();
    1387             :     //current std140 alignment: 16
    1388           0 :     glBufferSubData(GL_UNIFORM_BUFFER, 16, sizeof(glm::vec4), &m_LightsInfo.ambient[0]);
    1389           0 :     CHECK_GL_ERROR();
    1390             :     //current std140 alignment: 16
    1391           0 :     glBufferSubData(GL_UNIFORM_BUFFER, 32, sizeof(LightSource) * MAX_LIGHT_NUM, &m_LightsInfo.light);
    1392           0 :     CHECK_GL_ERROR();
    1393           0 :     glBindBuffer(GL_UNIFORM_BUFFER, 0);
    1394             : }
    1395             : 
    1396           0 : void OpenGL3DRenderer::RenderExtrudeFlatSurface(const Extrude3DInfo& extrude3D, int surIndex)
    1397             : {
    1398           0 :     float xyScale = extrude3D.xScale;
    1399             :     PosVecf3 trans = {extrude3D.xTransform,
    1400             :                       extrude3D.yTransform,
    1401           0 :                       extrude3D.zTransform};
    1402           0 :     glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    1403           0 :     glm::mat4 flatScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
    1404           0 :     m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * aTranslationMatrix * extrude3D.rotation * flatScale;
    1405           0 :     if(!mbPickingMode)
    1406             :     {
    1407           0 :         glm::mat3 normalMatrix(m_Model);
    1408           0 :         glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    1409           0 :         glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
    1410           0 :         glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
    1411             :     }
    1412             :     else
    1413             :     {
    1414           0 :         glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    1415           0 :         glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
    1416           0 :         glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
    1417           0 :         glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
    1418             :     }
    1419             : 
    1420           0 :     glDrawElements(GL_TRIANGLES, extrude3D.size[surIndex], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[surIndex]));
    1421           0 : }
    1422             : 
    1423           0 : void OpenGL3DRenderer::RenderExtrudeBottomSurface(const Extrude3DInfo& extrude3D)
    1424             : {
    1425           0 :     float xyScale = extrude3D.xScale;
    1426           0 :     float zScale = extrude3D.zScale;
    1427           0 :     float actualZTrans = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
    1428             :     PosVecf3 trans = {extrude3D.xTransform,
    1429             :                       extrude3D.yTransform,
    1430           0 :                       extrude3D.zTransform};
    1431             :     //PosVecf3 angle = {0.0f, 0.0f, 0.0f};
    1432           0 :     if (actualZTrans < 0.0f)
    1433             :     {
    1434             :         // the height of rounded corner is higher than the cube than use the org scale matrix
    1435             :    //     yScale /= (float)(1 + BOTTOM_THRESHOLD);
    1436           0 :         zScale /= (float)(m_RoundBarMesh.bottomThreshold);
    1437           0 :         PosVecf3 scale = {xyScale, xyScale, zScale};
    1438           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    1439           0 :         glm::mat4 aScaleMatrix = glm::scale(glm::vec3(scale.x, scale.y, scale.z));
    1440           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * aScaleMatrix;
    1441             :     }
    1442             :     else
    1443             :     {
    1444           0 :         glm::mat4 topTrans = glm::translate(glm::vec3(0.0, 0.0, actualZTrans));
    1445           0 :         glm::mat4 topScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
    1446           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    1447           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale;
    1448             :     }
    1449           0 :     m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
    1450           0 :     if(!mbPickingMode)
    1451             :     {
    1452           0 :         glm::mat3 normalMatrix(m_Model);
    1453           0 :         glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    1454           0 :         glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
    1455           0 :         glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
    1456             :     }
    1457             :     else
    1458             :     {
    1459           0 :         glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    1460           0 :         glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
    1461           0 :         glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
    1462           0 :         glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
    1463             :     }
    1464           0 :     glDrawElements(GL_TRIANGLES, extrude3D.size[BOTTOM_SURFACE], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[BOTTOM_SURFACE]));
    1465           0 : }
    1466             : 
    1467           0 : void OpenGL3DRenderer::RenderExtrudeMiddleSurface(const Extrude3DInfo& extrude3D)
    1468             : {
    1469           0 :     float xyScale = extrude3D.xScale;
    1470           0 :     float zScale = extrude3D.zScale;
    1471           0 :     float actualZScale = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
    1472             :     PosVecf3 trans = {extrude3D.xTransform,
    1473             :                       extrude3D.yTransform,
    1474           0 :                       extrude3D.zTransform};
    1475           0 :     if (actualZScale < 0.0f)
    1476             :     {
    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           0 :           return ;
    1482             :     }
    1483             :     else
    1484             :     {
    1485           0 :         glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale,actualZScale));
    1486           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    1487           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * scale;
    1488             :     }
    1489             : 
    1490           0 :     if (extrude3D.reverse)
    1491             :     {
    1492           0 :         glm::mat4 reverseMatrix = glm::translate(glm::vec3(0.0, 0.0, -1.0));
    1493           0 :         m_Model = m_Model * reverseMatrix;
    1494             :     }
    1495           0 :     m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
    1496           0 :     if(!mbPickingMode)
    1497             :     {
    1498           0 :         glm::mat3 normalMatrix(m_Model);
    1499           0 :         glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    1500           0 :         glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
    1501           0 :         glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
    1502             :     }
    1503             :     else
    1504             :     {
    1505           0 :         glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    1506           0 :         glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
    1507           0 :         glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
    1508           0 :         glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
    1509             :     }
    1510           0 :     glDrawElements(GL_TRIANGLES, extrude3D.size[MIDDLE_SURFACE], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[MIDDLE_SURFACE]));
    1511             : }
    1512             : 
    1513           0 : void OpenGL3DRenderer::RenderExtrudeTopSurface(const Extrude3DInfo& extrude3D)
    1514             : {
    1515           0 :     float xyScale = extrude3D.xScale;
    1516           0 :     float zScale = extrude3D.zScale;
    1517           0 :     float actualZTrans = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
    1518             :     PosVecf3 trans = {extrude3D.xTransform,
    1519             :                       extrude3D.yTransform,
    1520           0 :                       extrude3D.zTransform};
    1521           0 :     if (actualZTrans < 0.0f)
    1522             :     {
    1523             :         // the height of rounded corner is higher than the cube than use the org scale matrix
    1524             :         //yScale /= (float)(1 + BOTTOM_THRESHOLD);
    1525           0 :         zScale /= (float)(m_RoundBarMesh.bottomThreshold);
    1526           0 :         glm::mat4 orgTrans = glm::translate(glm::vec3(0.0, 0.0, -1.0));
    1527           0 :         glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale, zScale));
    1528             :         //MoveModelf(trans, angle, scale);
    1529           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    1530           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * scale * orgTrans;
    1531             :     }
    1532             :     else
    1533             :     {
    1534             :         // use different matrices for different parts
    1535           0 :         glm::mat4 orgTrans = glm::translate(glm::vec3(0.0, 0.0, -1.0));
    1536           0 :         glm::mat4 topTrans = glm::translate(glm::vec3(0.0, 0.0, actualZTrans));
    1537           0 :         glm::mat4 topScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
    1538           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    1539           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale * orgTrans;
    1540             :     }
    1541           0 :     m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
    1542           0 :     if(!mbPickingMode)
    1543             :     {
    1544           0 :         glm::mat3 normalMatrix(m_Model);
    1545           0 :         glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    1546           0 :         glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
    1547           0 :         glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
    1548             :     }
    1549             :     else
    1550             :     {
    1551           0 :         glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    1552           0 :         glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
    1553           0 :         glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
    1554           0 :         glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
    1555             :     }
    1556           0 :     glDrawElements(GL_TRIANGLES, extrude3D.size[TOP_SURFACE], GL_UNSIGNED_SHORT, reinterpret_cast<GLvoid*>(extrude3D.startIndex[TOP_SURFACE]));
    1557           0 : }
    1558             : 
    1559           0 : void OpenGL3DRenderer::RenderNonRoundedBar(const Extrude3DInfo& extrude3D)
    1560             : {
    1561           0 :     float xScale = extrude3D.xScale;
    1562           0 :     float yScale = extrude3D.yScale;
    1563           0 :     float zScale = extrude3D.zScale;
    1564           0 :     glm::mat4 transformMatrix = glm::translate(glm::vec3(extrude3D.xTransform, extrude3D.yTransform, extrude3D.zTransform));
    1565           0 :     glm::mat4 scaleMatrix = glm::scale(glm::vec3(xScale, yScale, zScale));
    1566           0 :     m_Model = transformMatrix * extrude3D.rotation * scaleMatrix;
    1567           0 :     if (extrude3D.reverse)
    1568             :     {
    1569           0 :         glm::mat4 reverseMatrix = glm::translate(glm::vec3(0.0, 0.0, -1.0));
    1570           0 :         m_Model = m_Model * reverseMatrix;
    1571             :     }
    1572           0 :     m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model;
    1573           0 :     if(!mbPickingMode)
    1574             :     {
    1575           0 :         glm::mat3 normalMatrix(m_Model);
    1576           0 :         glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    1577           0 :         glUniformMatrix4fv(maResources.m_3DModelID, 1, GL_FALSE, &m_Model[0][0]);
    1578           0 :         glUniformMatrix3fv(maResources.m_3DNormalMatrixID, 1, GL_FALSE, &normalInverseTranspos[0][0]);
    1579             :     }
    1580             :     else
    1581             :     {
    1582           0 :         glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    1583           0 :         glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]);
    1584           0 :         glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]);
    1585           0 :         glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]);
    1586             :     }
    1587           0 :     glDrawArrays(GL_TRIANGLES, 0, 36);
    1588           0 : }
    1589             : 
    1590           0 : void OpenGL3DRenderer::RenderExtrudeSurface(const Extrude3DInfo& extrude3D)
    1591             : {
    1592           0 :     RenderExtrudeMiddleSurface(extrude3D);
    1593             :     // check reverse flag to decide whether to render the top middle
    1594           0 :     if (extrude3D.reverse)
    1595             :     {
    1596           0 :         RenderExtrudeBottomSurface(extrude3D);
    1597           0 :         RenderExtrudeFlatSurface(extrude3D, FLAT_TOP_SURFACE);
    1598             :     }
    1599             :     else
    1600             :     {
    1601           0 :         RenderExtrudeTopSurface(extrude3D);
    1602           0 :         RenderExtrudeFlatSurface(extrude3D, FLAT_BOTTOM_SURFACE);
    1603             :     }
    1604           0 : }
    1605           0 : void OpenGL3DRenderer::ReleaseExtrude3DShapes()
    1606             : {
    1607           0 :     m_Extrude3DList.clear();
    1608           0 : }
    1609             : 
    1610           0 : void OpenGL3DRenderer::RenderExtrude3DObject()
    1611             : {
    1612           0 :     CHECK_GL_ERROR();
    1613           0 :     glEnable(GL_DEPTH_TEST);
    1614           0 :     glEnable(GL_CULL_FACE);
    1615           0 :     glCullFace(GL_BACK);
    1616           0 :     CHECK_GL_ERROR();
    1617           0 :     if(mbPickingMode)
    1618             :     {
    1619           0 :         glUseProgram(maPickingResources.m_CommonProID);
    1620           0 :         glUniform1fv(maPickingResources.m_MinCoordXID, 1, &m_fMinCoordX);
    1621           0 :         glUniform1fv(maPickingResources.m_MaxCoordXID, 1, &m_fMaxCoordX);
    1622             :     }
    1623             :     else
    1624             :     {
    1625           0 :         glUseProgram(maResources.m_3DProID);
    1626           0 :         glUniformMatrix4fv(maResources.m_3DViewID, 1, GL_FALSE, &m_3DView[0][0]);
    1627           0 :         glUniformMatrix4fv(maResources.m_3DProjectionID, 1, GL_FALSE, &m_3DProjection[0][0]);
    1628           0 :         CHECK_GL_ERROR();
    1629           0 :         if (maResources.m_b330Support)
    1630             :         {
    1631             :             //update ubo
    1632           0 :             Update3DUniformBlock();
    1633           0 :             CHECK_GL_ERROR();
    1634             :         }
    1635             :         else
    1636             :         {
    1637           0 :             glUniform1fv(maResources.m_3DMinCoordXID, 1, &m_fMinCoordX);
    1638           0 :             glUniform1fv(maResources.m_3DMaxCoordXID, 1, &m_fMaxCoordX);
    1639           0 :             glUniform1i(maResources.m_3DUndrawID, m_bUndrawFlag);
    1640             :             //update light information
    1641           0 :             glUniform4fv(maResources.m_3DLightColorID, m_iLightNum, reinterpret_cast<GLfloat*>(m_LightColor));
    1642           0 :             glUniform4fv(maResources.m_3DLightPosID, m_iLightNum, reinterpret_cast<GLfloat*>(m_PositionWorldspace));
    1643           0 :             glUniform1fv(maResources.m_3DLightPowerID, m_iLightNum, m_fLightPower);
    1644           0 :             glUniform1i(maResources.m_3DLightNumID, m_iLightNum);
    1645           0 :             glUniform4fv(maResources.m_3DLightAmbientID, 1, &m_Ambient[0]);
    1646           0 :             CHECK_GL_ERROR();
    1647             :         }
    1648             :     }
    1649           0 :     size_t extrude3DNum = m_Extrude3DList.size();
    1650           0 :     for (size_t i = 0; i < extrude3DNum; i++)
    1651             :     {
    1652           0 :         Extrude3DInfo extrude3DInfo = m_Extrude3DList[i];
    1653           0 :         GLuint vertexBuf = extrude3DInfo.rounded ? m_CubeVertexBuf : m_BoundBox;
    1654           0 :         GLuint normalBuf = extrude3DInfo.rounded ? m_CubeNormalBuf : m_BoundBoxNormal;
    1655             : 
    1656           0 :         if(mbPickingMode)
    1657           0 :             glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3DInfo.id[0]);
    1658             :         // 1st attribute buffer : vertices
    1659             : 
    1660           0 :         GLint aVertexID = mbPickingMode ? maPickingResources.m_2DVertexID : maResources.m_3DVertexID;
    1661           0 :         glEnableVertexAttribArray(aVertexID);
    1662           0 :         glBindBuffer(GL_ARRAY_BUFFER, vertexBuf);
    1663             :         glVertexAttribPointer(aVertexID, // attribute
    1664             :                                 3,                  // size
    1665             :                                 GL_FLOAT,           // type
    1666             :                                 GL_FALSE,           // normalized?
    1667             :                                 0,                  // stride
    1668             :                                 nullptr            // array buffer offset
    1669           0 :                                 );
    1670             : 
    1671           0 :         if(!mbPickingMode)
    1672             :         {
    1673             :             // 2nd attribute buffer : normals
    1674           0 :             glEnableVertexAttribArray(maResources.m_3DNormalID);
    1675           0 :             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             :                     nullptr            // array buffer offset
    1682           0 :                     );
    1683             :         }
    1684           0 :         if(!mbPickingMode)
    1685             :         {
    1686           0 :             if (maResources.m_b330Support)
    1687             :             {
    1688           0 :                 glBindBuffer(GL_UNIFORM_BUFFER, m_3DUBOBuffer);
    1689           0 :                 glBufferSubData(GL_UNIFORM_BUFFER, m_3DActualSizeLight, sizeof(MaterialParameters), &extrude3DInfo.material);
    1690           0 :                 CHECK_GL_ERROR();
    1691           0 :                 glBindBuffer(GL_UNIFORM_BUFFER, 0);
    1692             :             }
    1693             :             else
    1694             :             {
    1695             :                 //update meterial information
    1696           0 :                 glUniform4fv(maResources.m_3DMaterialAmbientID, 1, &extrude3DInfo.material.ambient[0]);
    1697           0 :                 glUniform4fv(maResources.m_3DMaterialDiffuseID, 1, &extrude3DInfo.material.diffuse[0]);
    1698           0 :                 glUniform4fv(maResources.m_3DMaterialSpecularID, 1, &extrude3DInfo.material.specular[0]);
    1699           0 :                 glUniform4fv(maResources.m_3DMaterialColorID, 1, &extrude3DInfo.material.materialColor[0]);
    1700           0 :                 glUniform1i(maResources.m_3DMaterialTwoSidesID, extrude3DInfo.material.twoSidesLighting);
    1701           0 :                 glUniform1f(maResources.m_3DMaterialShininessID, extrude3DInfo.material.shininess);
    1702             :             }
    1703             :         }
    1704           0 :         extrude3DInfo.reverse = 0;
    1705           0 :         if (extrude3DInfo.rounded)
    1706             :         {
    1707           0 :             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CubeElementBuf);
    1708           0 :             RenderExtrudeSurface(extrude3DInfo);
    1709           0 :             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    1710             :         }
    1711             :         else
    1712             :         {
    1713           0 :             RenderNonRoundedBar(extrude3DInfo);
    1714             :         }
    1715           0 :         glDisableVertexAttribArray(aVertexID);
    1716           0 :         if(!mbPickingMode)
    1717           0 :             glDisableVertexAttribArray(maResources.m_3DNormalID);
    1718             :     }
    1719           0 :     glUseProgram(0);
    1720           0 :     glBindBuffer(GL_ARRAY_BUFFER, 0);
    1721           0 :     glDisable(GL_CULL_FACE);
    1722           0 : }
    1723             : 
    1724           0 : 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)
    1729             : {
    1730           0 :     long bmpWidth = rSizePixels.Width();
    1731           0 :     long bmpHeight = rSizePixels.Height();
    1732             : 
    1733           0 :     TextInfo aTextInfo;
    1734           0 :     aTextInfo.id = getColorAsVector(nUniqueId);
    1735           0 :     aTextInfo.uniqueId = nUniqueId;
    1736           0 :     aTextInfo.vertex[0] = vBottomRight.x;
    1737           0 :     aTextInfo.vertex[1] = vBottomRight.y;
    1738           0 :     aTextInfo.vertex[2] = 0;
    1739             : 
    1740           0 :     aTextInfo.vertex[3] = vBottomRight.x;
    1741           0 :     aTextInfo.vertex[4] = vTopLeft.y;
    1742           0 :     aTextInfo.vertex[5] = 0;
    1743             : 
    1744           0 :     aTextInfo.vertex[6] = vTopLeft.x;
    1745           0 :     aTextInfo.vertex[7] = vTopLeft.y;
    1746           0 :     aTextInfo.vertex[8] = 0;
    1747             : 
    1748           0 :     aTextInfo.vertex[9] = vTopLeft.x;
    1749           0 :     aTextInfo.vertex[10] = vBottomRight.y;
    1750           0 :     aTextInfo.vertex[11] = 0;
    1751           0 :     aTextInfo.pos = vPos;
    1752           0 :     aTextInfo.textColor = vScreenTextColor;
    1753             : 
    1754           0 :     CHECK_GL_ERROR();
    1755           0 :     glGenTextures(1, &aTextInfo.texture);
    1756           0 :     CHECK_GL_ERROR();
    1757           0 :     glBindTexture(GL_TEXTURE_2D, aTextInfo.texture);
    1758           0 :     CHECK_GL_ERROR();
    1759           0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    1760           0 :     CHECK_GL_ERROR();
    1761           0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    1762           0 :     CHECK_GL_ERROR();
    1763           0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1764           0 :     CHECK_GL_ERROR();
    1765           0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1766           0 :     CHECK_GL_ERROR();
    1767           0 :     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmpWidth, bmpHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapBuf.get());
    1768           0 :     CHECK_GL_ERROR();
    1769           0 :     glBindTexture(GL_TEXTURE_2D, 0);
    1770           0 :     CHECK_GL_ERROR();
    1771           0 :     m_ScreenTextInfoList.push_back(aTextInfo);
    1772           0 :     m_ScreenTexturelist.push_back(aTextInfo.texture);
    1773           0 : }
    1774             : 
    1775           0 : 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)
    1780             : {
    1781           0 :     long bmpWidth = rSizePixels.Width();
    1782           0 :     long bmpHeight = rSizePixels.Height();
    1783             : 
    1784           0 :     TextInfo aTextInfo;
    1785           0 :     aTextInfo.id = getColorAsVector(nUniqueId);
    1786           0 :     aTextInfo.vertex[0] = vBottomRight.x;
    1787           0 :     aTextInfo.vertex[1] = vBottomRight.y;
    1788           0 :     aTextInfo.vertex[2] = vBottomRight.z;
    1789             : 
    1790           0 :     aTextInfo.vertex[3] = vTopRight.x;
    1791           0 :     aTextInfo.vertex[4] = vTopRight.y;
    1792           0 :     aTextInfo.vertex[5] = vTopRight.z;
    1793             : 
    1794           0 :     aTextInfo.vertex[9] = vBottomLeft.x;
    1795           0 :     aTextInfo.vertex[10] = vBottomLeft.y;
    1796           0 :     aTextInfo.vertex[11] = vBottomLeft.z;
    1797             : 
    1798           0 :     aTextInfo.vertex[6] = vTopLeft.x;
    1799           0 :     aTextInfo.vertex[7] = vTopLeft.y;
    1800           0 :     aTextInfo.vertex[8] = vTopLeft.z;
    1801             : 
    1802           0 :     CHECK_GL_ERROR();
    1803           0 :     glGenTextures(1, &aTextInfo.texture);
    1804           0 :     CHECK_GL_ERROR();
    1805           0 :     glBindTexture(GL_TEXTURE_2D, aTextInfo.texture);
    1806           0 :     CHECK_GL_ERROR();
    1807           0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1808           0 :     CHECK_GL_ERROR();
    1809           0 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1810           0 :     CHECK_GL_ERROR();
    1811           0 :     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmpWidth, bmpHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapBuf.get());
    1812           0 :     CHECK_GL_ERROR();
    1813           0 :     glBindTexture(GL_TEXTURE_2D, 0);
    1814           0 :     CHECK_GL_ERROR();
    1815           0 :     m_TextInfoList.push_back(aTextInfo);
    1816           0 :     m_Texturelist.push_back(aTextInfo.texture);
    1817             : 
    1818           0 : }
    1819             : 
    1820           0 : 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)
    1825             : {
    1826           0 :     long bmpWidth = rSizePixels.Width();
    1827           0 :     long bmpHeight = rSizePixels.Height();
    1828           0 :     glm::vec4 id = getColorAsVector(nUniqueId);
    1829           0 :     m_TextInfoBatch.idList.push_back(id);
    1830           0 :     m_TextInfoBatch.vertexList.push_back(glm::vec3(vBottomRight.x, vBottomRight.y, vBottomRight.z));
    1831           0 :     m_TextInfoBatch.vertexList.push_back(glm::vec3(vTopRight.x, vTopRight.y, vTopRight.z));
    1832           0 :     m_TextInfoBatch.vertexList.push_back(glm::vec3(vTopLeft.x, vTopLeft.y, vTopLeft.z));
    1833           0 :     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           0 :     size_t index = 0;
    1836           0 :     while ((m_TextInfoBatch.texture.size() > 0) &&
    1837           0 :            (m_TextInfoBatch.texture[index].subTextureNum >= m_TextInfoBatch.batchNum) &&
    1838           0 :            (index < m_TextInfoBatch.texture.size() - 1))
    1839             :     {
    1840           0 :         index++;
    1841             :     }
    1842             :     //if the sub texture number of the last texture array reach the largest, create a new textur array
    1843           0 :     if ((m_TextInfoBatch.texture.size() == 0) ||
    1844           0 :         (m_TextInfoBatch.texture[index].subTextureNum >= m_TextInfoBatch.batchNum))
    1845             :     {
    1846           0 :         TextureArrayInfo textureArray;
    1847           0 :         glGenTextures(1, &textureArray.textureID);
    1848           0 :         CHECK_GL_ERROR();
    1849           0 :         glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray.textureID);
    1850           0 :         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    1851           0 :         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    1852           0 :         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1853           0 :         glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1854           0 :         CHECK_GL_ERROR();
    1855           0 :         textureArray.textureArrayWidth = bmpHeight * 8;
    1856           0 :         textureArray.textureArrayHeight = bmpHeight;
    1857             :         glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGB, textureArray.textureArrayWidth, textureArray.textureArrayHeight,
    1858           0 :                      m_TextInfoBatch.batchNum, 0, GL_RGB,  GL_UNSIGNED_BYTE, NULL);
    1859           0 :         CHECK_GL_ERROR();
    1860           0 :         if (m_TextInfoBatch.texture.size() > 0)
    1861             :         {
    1862           0 :             index++;
    1863             :         }
    1864           0 :         m_TextInfoBatch.texture.push_back(textureArray);
    1865           0 :         glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
    1866             :     }
    1867           0 :     glBindTexture(GL_TEXTURE_2D_ARRAY, m_TextInfoBatch.texture[index].textureID);
    1868           0 :     CHECK_GL_ERROR();
    1869           0 :     glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, m_TextInfoBatch.texture[index].subTextureNum, bmpWidth, bmpHeight, 1, GL_RGB, GL_UNSIGNED_BYTE, bitmapBuf.get());
    1870           0 :     CHECK_GL_ERROR();
    1871             :         //calc texture coordinate
    1872           0 :     m_TextInfoBatch.textureCoordList.push_back(glm::vec3((float)bmpWidth / (float)m_TextInfoBatch.texture[index].textureArrayWidth,
    1873             :                                                          0,
    1874           0 :                                                          m_TextInfoBatch.texture[index].subTextureNum));
    1875           0 :     m_TextInfoBatch.textureCoordList.push_back(glm::vec3((float)bmpWidth / (float)m_TextInfoBatch.texture[index].textureArrayWidth,
    1876           0 :                                                          (float)bmpHeight/ (float)m_TextInfoBatch.texture[index].textureArrayHeight,
    1877           0 :                                                          m_TextInfoBatch.texture[index].subTextureNum));
    1878             :     m_TextInfoBatch.textureCoordList.push_back(glm::vec3(0,
    1879           0 :                                                          (float)bmpHeight/ (float)m_TextInfoBatch.texture[index].textureArrayHeight,
    1880           0 :                                                          m_TextInfoBatch.texture[index].subTextureNum));
    1881             :     m_TextInfoBatch.textureCoordList.push_back(glm::vec3(0,
    1882             :                                                          0,
    1883           0 :                                                          m_TextInfoBatch.texture[index].subTextureNum));
    1884           0 :     m_TextInfoBatch.texture[index].subTextureNum++;
    1885           0 :     glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
    1886           0 : }
    1887             : 
    1888           0 : 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)
    1893             : {
    1894           0 :     if (maResources.mbTexBatchSupport)
    1895             :     {
    1896           0 :         CreateTextTextureBatch(bitmapBuf, rSizePixels, vTopLeft, vTopRight, vBottomRight, vBottomLeft, nUniqueId);
    1897             :     }
    1898             :     else
    1899             :     {
    1900           0 :         CreateTextTextureSingle(bitmapBuf, rSizePixels, vTopLeft, vTopRight, vBottomRight, vBottomLeft, nUniqueId);
    1901             :     }
    1902           0 : }
    1903             : 
    1904           0 : void OpenGL3DRenderer::ReleaseTextShapes()
    1905             : {
    1906           0 :     m_TextInfoList.clear();
    1907           0 : }
    1908             : 
    1909           0 : void OpenGL3DRenderer::ReleaseTextTexture()
    1910             : {
    1911           0 :     for (size_t i = 0; i < m_Texturelist.size(); i++)
    1912             :     {
    1913           0 :         glDeleteTextures(1, &m_Texturelist[i]);
    1914             :     }
    1915           0 :     m_Texturelist.clear();
    1916           0 : }
    1917             : 
    1918           0 : void OpenGL3DRenderer::ReleaseScreenTextShapes()
    1919             : {
    1920           0 :     m_ScreenTextInfoList.clear();
    1921           0 : }
    1922             : 
    1923           0 : void OpenGL3DRenderer::ReleaseScreenTextTexture()
    1924             : {
    1925           0 :     for (size_t i = 0; i < m_ScreenTexturelist.size(); i++)
    1926             :     {
    1927           0 :         glDeleteTextures(1, &m_ScreenTexturelist[i]);
    1928             :     }
    1929           0 :     m_ScreenTexturelist.clear();
    1930           0 : }
    1931             : 
    1932             : 
    1933           0 : void OpenGL3DRenderer::RenderScreenTextShape()
    1934             : {
    1935           0 :     if (mbPickingMode)
    1936           0 :         return;
    1937           0 :     glUseProgram(maResources.m_ScreenTextProID);
    1938           0 :     CHECK_GL_ERROR();
    1939           0 :     for (size_t i = 0; i < m_ScreenTextInfoList.size(); i++)
    1940             :     {
    1941           0 :         TextInfo textInfo = m_ScreenTextInfoList[i];
    1942             :         //calc the position and check whether it can be displayed
    1943           0 :         if (textInfo.uniqueId)
    1944             :         {
    1945           0 :             glm::vec3 worldPos = glm::vec3(m_ScrollMoveMatrix * m_GlobalScaleMatrix * glm::vec4(textInfo.pos, 1));
    1946           0 :             if ((worldPos.x < m_fMinCoordX) || (worldPos.x > m_fMaxCoordX))
    1947           0 :                 continue;
    1948           0 :             glm::vec4 pos = m_3DProjection * m_3DView * glm::vec4(worldPos, 1);
    1949           0 :             const float xTrans = pos.x / pos.w;
    1950           0 :             const float yTrans = pos.y / pos.w;
    1951           0 :             for (int j = 0; j < 12; j++)
    1952             :             {
    1953           0 :                 if (j % 3 == 0)
    1954             :                 {
    1955           0 :                     textInfo.vertex[j] += xTrans;
    1956             :                 }
    1957           0 :                 if (j % 3 == 1)
    1958             :                 {
    1959           0 :                     textInfo.vertex[j] += yTrans;
    1960             :                 }
    1961             :             }
    1962             :         }
    1963           0 :         glUniform4fv(maResources.m_ScreenTextColorID, 1, &textInfo.textColor[0]);
    1964           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    1965           0 :         CHECK_GL_ERROR();
    1966           0 :         glBufferData(GL_ARRAY_BUFFER, sizeof(textInfo.vertex), textInfo.vertex, GL_STATIC_DRAW);
    1967           0 :         CHECK_GL_ERROR();
    1968             : 
    1969             :         // 1rst attribute buffer : vertices
    1970           0 :         glEnableVertexAttribArray(maResources.m_ScreenTextVertexID);
    1971           0 :         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             :             nullptr            // array buffer offset
    1979           0 :             );
    1980             :         //tex coord
    1981           0 :         CHECK_GL_ERROR();
    1982           0 :         glEnableVertexAttribArray(maResources.m_ScreenTextTexCoordID);
    1983           0 :         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             :             nullptr            // array buffer offset
    1991           0 :             );
    1992             :         //texture
    1993           0 :         CHECK_GL_ERROR();
    1994           0 :         glBindTexture(GL_TEXTURE_2D, textInfo.texture);
    1995           0 :         CHECK_GL_ERROR();
    1996           0 :         glUniform1i(maResources.m_ScreenTextTexID, 0);
    1997           0 :         CHECK_GL_ERROR();
    1998             :         //TODO: moggi: get rid fo GL_QUADS
    1999           0 :         glDrawArrays(GL_QUADS, 0, 4);
    2000           0 :         CHECK_GL_ERROR();
    2001             :     }
    2002           0 :     glDisableVertexAttribArray(maResources.m_ScreenTextTexCoordID);
    2003           0 :     CHECK_GL_ERROR();
    2004           0 :     glDisableVertexAttribArray(maResources.m_ScreenTextVertexID);
    2005           0 :     CHECK_GL_ERROR();
    2006           0 :     glBindTexture(GL_TEXTURE_2D, 0);
    2007           0 :     glUseProgram(0);
    2008           0 :     CHECK_GL_ERROR();
    2009             : }
    2010           0 : void OpenGL3DRenderer::ReleaseTextShapesBatch()
    2011             : {
    2012           0 :     for (size_t i = 0; i < m_TextInfoBatch.texture.size(); i++)
    2013             :     {
    2014           0 :         m_TextInfoBatch.texture[i].subTextureNum = 0;
    2015             :     }
    2016           0 :     m_TextInfoBatch.vertexList.clear();
    2017           0 :     m_TextInfoBatch.textureCoordList.clear();
    2018           0 :     m_TextInfoBatch.idList.clear();
    2019           0 : }
    2020             : 
    2021           0 : void OpenGL3DRenderer::RenderTextShapeBatch()
    2022             : {
    2023           0 :     glm::mat4 aMVP = m_3DProjection * m_3DView * m_GlobalScaleMatrix;
    2024           0 :     glUseProgram(maResources.m_BatchTextProID);
    2025           0 :     CHECK_GL_ERROR();
    2026           0 :     glUniformMatrix4fv(maResources.m_BatchTextMatrixID, 1, GL_FALSE, &aMVP[0][0]);
    2027           0 :     glEnableVertexAttribArray(maResources.m_BatchTextVertexID);
    2028           0 :     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             :         nullptr            // array buffer offset
    2036           0 :         );
    2037             :     //tex coord
    2038           0 :     CHECK_GL_ERROR();
    2039           0 :     glEnableVertexAttribArray(maResources.m_BatchTextTexCoordID);
    2040           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBufBatch);
    2041           0 :     CHECK_GL_ERROR();
    2042             :     glVertexAttribPointer(
    2043             :         maResources.m_BatchTextTexCoordID,
    2044             :         3,                  // size
    2045             :         GL_FLOAT,           // type
    2046             :         GL_FALSE,           // normalized?
    2047             :         0,                  // stride
    2048             :         nullptr            // array buffer offset
    2049           0 :         );
    2050             :     //use texture array to get the vertex
    2051           0 :     for (size_t i = 0; i < m_TextInfoBatch.texture.size(); i++)
    2052             :     {
    2053           0 :         int vertexNum = m_TextInfoBatch.texture[i].subTextureNum;
    2054           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    2055           0 :         CHECK_GL_ERROR();
    2056           0 :         glBufferData(GL_ARRAY_BUFFER, 4 * vertexNum * sizeof(glm::vec3), &m_TextInfoBatch.vertexList[4 * i * m_TextInfoBatch.batchNum], GL_STATIC_DRAW);
    2057           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_TextTexCoordBufBatch);
    2058           0 :         CHECK_GL_ERROR();
    2059           0 :         glBufferData(GL_ARRAY_BUFFER, 4 * vertexNum * sizeof(glm::vec3), &m_TextInfoBatch.textureCoordList[4 * i * m_TextInfoBatch.batchNum], GL_STATIC_DRAW);
    2060           0 :         glBindTexture(GL_TEXTURE_2D_ARRAY, m_TextInfoBatch.texture[i].textureID);
    2061           0 :         CHECK_GL_ERROR();
    2062           0 :         glUniform1i(maResources.m_BatchTextTexID, 0);
    2063           0 :         CHECK_GL_ERROR();
    2064             :         //TODO: moggi: get rid fo GL_QUADS
    2065           0 :         glDrawArrays(GL_QUADS, 0, 4 * vertexNum);
    2066             :     }
    2067           0 :     glDisableVertexAttribArray(maResources.m_BatchTextVertexID);
    2068           0 :     CHECK_GL_ERROR();
    2069           0 :     glDisableVertexAttribArray(maResources.m_BatchTextTexCoordID);
    2070           0 :     CHECK_GL_ERROR();
    2071           0 :     glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
    2072           0 :     CHECK_GL_ERROR();
    2073           0 :     glUseProgram(0);
    2074           0 : }
    2075           0 : void OpenGL3DRenderer::RenderTextShape()
    2076             : {
    2077           0 :     CHECK_GL_ERROR();
    2078           0 :     for (size_t i = 0; i < m_TextInfoList.size(); i++)
    2079             :     {
    2080           0 :         TextInfo &textInfo = m_TextInfoList[i];
    2081           0 :         PosVecf3 trans = {0, 0, 0};
    2082           0 :         PosVecf3 angle = {0.0f, 0.0f, 0.0f};
    2083           0 :         PosVecf3 scale = {1.0, 1.0, 1.0f};
    2084           0 :         MoveModelf(trans, angle, scale);
    2085           0 :         m_Model = m_GlobalScaleMatrix * m_Model;
    2086           0 :         glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model;
    2087           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    2088           0 :         CHECK_GL_ERROR();
    2089           0 :         glBufferData(GL_ARRAY_BUFFER, sizeof(textInfo.vertex), textInfo.vertex, GL_STATIC_DRAW);
    2090           0 :         CHECK_GL_ERROR();
    2091           0 :         glUseProgram(maResources.m_TextProID);
    2092             : 
    2093           0 :         CHECK_GL_ERROR();
    2094           0 :         glUniformMatrix4fv(maResources.m_TextMatrixID, 1, GL_FALSE, &aMVP[0][0]);
    2095             :         // 1rst attribute buffer : vertices
    2096           0 :         glEnableVertexAttribArray(maResources.m_TextVertexID);
    2097           0 :         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             :             nullptr            // array buffer offset
    2105           0 :             );
    2106             :         //tex coord
    2107           0 :         CHECK_GL_ERROR();
    2108           0 :         glEnableVertexAttribArray(maResources.m_TextTexCoordID);
    2109           0 :         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             :             nullptr            // array buffer offset
    2117           0 :             );
    2118             :         //texture
    2119           0 :         CHECK_GL_ERROR();
    2120           0 :         glBindTexture(GL_TEXTURE_2D, textInfo.texture);
    2121           0 :         CHECK_GL_ERROR();
    2122           0 :         glUniform1i(maResources.m_TextTexID, 0);
    2123           0 :         CHECK_GL_ERROR();
    2124             :         //TODO: moggi: get rid fo GL_QUADS
    2125           0 :         glDrawArrays(GL_QUADS, 0, 4);
    2126           0 :         CHECK_GL_ERROR();
    2127           0 :         glDisableVertexAttribArray(maResources.m_TextTexCoordID);
    2128           0 :         CHECK_GL_ERROR();
    2129           0 :         glDisableVertexAttribArray(maResources.m_TextVertexID);
    2130           0 :         CHECK_GL_ERROR();
    2131           0 :         glBindTexture(GL_TEXTURE_2D, 0);
    2132           0 :         glUseProgram(0);
    2133             :     }
    2134           0 :     CHECK_GL_ERROR();
    2135           0 : }
    2136             : 
    2137           0 : void OpenGL3DRenderer::CreateSceneBoxView()
    2138             : {
    2139           0 :     m_CameraInfo.cameraPos = glm::vec3(m_GlobalScaleMatrix * glm::vec4(m_CameraInfo.cameraPos, 1.0));
    2140           0 :     m_CameraInfo.cameraOrg = glm::vec3(m_GlobalScaleMatrix * glm::vec4(m_CameraInfo.cameraOrg, 1.0));
    2141           0 :     m_3DView = glm::lookAt(m_CameraInfo.cameraPos,
    2142             :                m_CameraInfo.cameraOrg,
    2143           0 :                m_CameraInfo.cameraUp);
    2144           0 :     m_3DView = m_3DView + m_matDiff;
    2145           0 : }
    2146             : 
    2147           0 : void OpenGL3DRenderer::AddMatrixDiff(const glm::mat4& aMat)
    2148             : {
    2149           0 :     m_matDiff = m_matDiff + aMat;
    2150           0 : }
    2151             : 
    2152           0 : void OpenGL3DRenderer::ResetMatrixDiff()
    2153             : {
    2154           0 :     m_matDiff = glm::mat4(0.0);
    2155           0 : }
    2156             : 
    2157           0 : void OpenGL3DRenderer::ClearBuffer()
    2158             : {
    2159           0 :     CHECK_GL_ERROR();
    2160           0 :     glDisable(GL_DEPTH_TEST);
    2161           0 :     CHECK_GL_ERROR();
    2162             : 
    2163             : #if defined(IOS) || defined(ANDROID)
    2164             :     glClearDepthf(1.0f);
    2165             : #else
    2166           0 :     glClearDepth(1.0f);
    2167             : #endif
    2168           0 :     CHECK_GL_ERROR();
    2169           0 :     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    2170           0 :     CHECK_GL_ERROR();
    2171             : 
    2172             :     /*
    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);
    2178             : 
    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 ();
    2183             :     */
    2184             : 
    2185           0 :     glEnable(GL_DEPTH_TEST);
    2186           0 :     CHECK_GL_ERROR();
    2187           0 : }
    2188             : 
    2189           0 : void OpenGL3DRenderer::ProcessUnrenderedShape(bool bNewScene)
    2190             : {
    2191           0 :     glViewport(0, 0, m_iWidth, m_iHeight);
    2192           0 :     CHECK_GL_ERROR();
    2193           0 :     ClearBuffer();
    2194           0 :     CHECK_GL_ERROR();
    2195           0 :     CreateSceneBoxView();
    2196           0 :     CHECK_GL_ERROR();
    2197           0 :     CalcScrollMoveMatrix(bNewScene);
    2198             :     //Polygon
    2199           0 :     CHECK_GL_ERROR();
    2200           0 :     RenderPolygon3DObject();
    2201             :     //Shape3DExtrudeObject
    2202           0 :     if(mbPickingMode)
    2203           0 :         RenderExtrude3DObject();
    2204             :     else
    2205             :     {
    2206           0 :         if (maResources.m_b330Support)
    2207             :         {
    2208           0 :             RenderBatchBars(bNewScene);
    2209             :         }
    2210             :         else
    2211             :         {
    2212           0 :             RenderExtrude3DObject();
    2213             :         }
    2214             :     }
    2215             :     //render text
    2216           0 :     if (maResources.mbTexBatchSupport)
    2217             :     {
    2218           0 :         RenderTextShapeBatch();
    2219             :     }
    2220             :     else
    2221             :     {
    2222           0 :         RenderTextShape();
    2223             :     }
    2224             :     // render screen text
    2225           0 :     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
    2230           0 : }
    2231             : 
    2232           0 : void OpenGL3DRenderer::MoveModelf(const PosVecf3& trans, const PosVecf3& angle, const PosVecf3& scale)
    2233             : {
    2234           0 :     glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    2235           0 :     glm::mat4 aScaleMatrix = glm::scale(glm::vec3(scale.x, scale.y, scale.z));
    2236           0 :     glm::mat4 aRotationMatrix = glm::eulerAngleYXZ(angle.y, angle.x, angle.z);
    2237           0 :     m_Model = aTranslationMatrix * aRotationMatrix * aScaleMatrix;
    2238           0 : }
    2239             : 
    2240           0 : void OpenGL3DRenderer::SetPickingMode(bool bPickingMode)
    2241             : {
    2242           0 :     mbPickingMode = bPickingMode;
    2243           0 :     if(mbPickingMode)
    2244             :     {
    2245           0 :         glBindFramebuffer(GL_FRAMEBUFFER, mnPickingFbo);
    2246           0 :         glDisable(GL_MULTISAMPLE);
    2247             :     }
    2248             :     else
    2249             :     {
    2250           0 :         glBindFramebuffer(GL_FRAMEBUFFER, 0);
    2251           0 :         glEnable(GL_MULTISAMPLE);
    2252             :     }
    2253           0 : }
    2254             : 
    2255           0 : sal_uInt32 OpenGL3DRenderer::GetPixelColorFromPoint(long nX, long nY)
    2256             : {
    2257             :     static sal_uInt32 nId = 0;
    2258           0 :     OUString aFileName = "/home/moggi/work/picking_" + OUString::number(nId++) + ".png";
    2259           0 :     OpenGLHelper::renderToFile(m_iWidth, m_iHeight, aFileName);
    2260           0 :     boost::scoped_array<sal_uInt8> buf(new sal_uInt8[4]);
    2261           0 :     glReadPixels(nX, m_iHeight-nY, 1, 1, GL_BGRA, GL_UNSIGNED_BYTE, buf.get());
    2262           0 :     Color aColor(255-buf[3], buf[2], buf[1], buf[0]);
    2263           0 :     return aColor.GetColor();
    2264             : }
    2265             : 
    2266           0 : void OpenGL3DRenderer::ReleaseBatchBarInfo()
    2267             : {
    2268           0 :     for (int i = 0; i < 3; i++)
    2269             :     {
    2270           0 :         m_BarSurface[i].modelMatrixList.clear();
    2271           0 :         m_BarSurface[i].normalMatrixList.clear();
    2272           0 :         m_BarSurface[i].colorList.clear();
    2273           0 :         m_BarSurface[i].mapId2Color.clear();
    2274             :     }
    2275           0 : }
    2276             : 
    2277           0 : void OpenGL3DRenderer::ReleaseShapes()
    2278             : {
    2279           0 :     ReleasePolygonShapes();
    2280           0 :     ReleaseExtrude3DShapes();
    2281           0 :     ReleaseTextShapes();
    2282             :     //ReleaseScreenTextShapes();
    2283           0 :     ReleaseBatchBarInfo();
    2284           0 :     ReleaseTextShapesBatch();
    2285           0 : }
    2286             : 
    2287           0 : void OpenGL3DRenderer::GetBatchMiddleInfo(const Extrude3DInfo &extrude3D)
    2288             : {
    2289           0 :     float xyScale = extrude3D.xScale;
    2290           0 :     float zScale = extrude3D.zScale;
    2291           0 :     float actualZScale = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
    2292             :     PosVecf3 trans = {extrude3D.xTransform,
    2293             :                       extrude3D.yTransform,
    2294           0 :                       extrude3D.zTransform};
    2295           0 :     if (actualZScale < 0.0f)
    2296             :     {
    2297           0 :           return ;
    2298             :     }
    2299             :     else
    2300             :     {
    2301           0 :         glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale,actualZScale));
    2302           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    2303           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * scale;
    2304             :     }
    2305             : 
    2306           0 :     m_Model = m_GlobalScaleMatrix * m_Model;
    2307           0 :     glm::mat3 normalMatrix(m_Model);
    2308           0 :     glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    2309           0 :     m_BarSurface[MIDDLE_SURFACE].modelMatrixList.push_back(m_Model);
    2310           0 :     m_BarSurface[MIDDLE_SURFACE].normalMatrixList.push_back(normalInverseTranspos);
    2311           0 :     m_BarSurface[MIDDLE_SURFACE].colorList.push_back(extrude3D.material.materialColor);
    2312           0 :     m_BarSurface[MIDDLE_SURFACE].mapId2Color[extrude3D.orgID] = m_BarSurface[MIDDLE_SURFACE].colorList.size() - 1;
    2313             : }
    2314             : 
    2315           0 : void OpenGL3DRenderer::GetBatchTopAndFlatInfo(const Extrude3DInfo &extrude3D)
    2316             : {
    2317           0 :     float xyScale = extrude3D.xScale;
    2318           0 :     float zScale = extrude3D.zScale;
    2319           0 :     float actualZTrans = zScale - m_RoundBarMesh.bottomThreshold * xyScale;
    2320             :     PosVecf3 trans = {extrude3D.xTransform,
    2321             :                       extrude3D.yTransform,
    2322           0 :                       extrude3D.zTransform};
    2323           0 :     glm::mat4 orgTrans = glm::translate(glm::vec3(0.0, 0.0, -1.0));
    2324           0 :     if (actualZTrans < 0.0f)
    2325             :     {
    2326             :         // the height of rounded corner is higher than the cube than use the org scale matrix
    2327             :         //yScale /= (float)(1 + BOTTOM_THRESHOLD);
    2328           0 :         zScale /= (float)(m_RoundBarMesh.bottomThreshold);
    2329           0 :         glm::mat4 scale = glm::scale(glm::vec3(xyScale, xyScale, zScale));
    2330             :         //MoveModelf(trans, angle, scale);
    2331           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    2332           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * scale * orgTrans;
    2333             :     }
    2334             :     else
    2335             :     {
    2336             :         // use different matrices for different parts
    2337           0 :         glm::mat4 topTrans = glm::translate(glm::vec3(0.0, 0.0, actualZTrans));
    2338           0 :         glm::mat4 topScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
    2339           0 :         glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    2340           0 :         m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale * orgTrans;
    2341             :     }
    2342             : 
    2343           0 :     m_Model = m_GlobalScaleMatrix * m_Model;
    2344           0 :     glm::mat3 normalMatrix(m_Model);
    2345           0 :     glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    2346           0 :     m_BarSurface[TOP_SURFACE].modelMatrixList.push_back(m_Model);
    2347           0 :     m_BarSurface[TOP_SURFACE].normalMatrixList.push_back(normalInverseTranspos);
    2348           0 :     m_BarSurface[TOP_SURFACE].colorList.push_back(extrude3D.material.materialColor);
    2349           0 :     m_BarSurface[TOP_SURFACE].mapId2Color[extrude3D.orgID] = m_BarSurface[TOP_SURFACE].colorList.size() - 1;
    2350             : 
    2351           0 :     glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z));
    2352           0 :     glm::mat4 flatScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale));
    2353           0 :     m_Model = aTranslationMatrix * extrude3D.rotation * flatScale;
    2354           0 :     m_Model = m_GlobalScaleMatrix * m_Model;
    2355           0 :     normalMatrix = glm::mat3(m_Model);
    2356           0 :     normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    2357             : 
    2358           0 :     m_BarSurface[FLAT_BOTTOM_SURFACE].modelMatrixList.push_back(m_Model);
    2359           0 :     m_BarSurface[FLAT_BOTTOM_SURFACE].normalMatrixList.push_back(normalInverseTranspos);
    2360           0 :     m_BarSurface[FLAT_BOTTOM_SURFACE].colorList.push_back(extrude3D.material.materialColor);
    2361           0 :     m_BarSurface[FLAT_BOTTOM_SURFACE].mapId2Color[extrude3D.orgID] = m_BarSurface[FLAT_BOTTOM_SURFACE].colorList.size() - 1;
    2362           0 : }
    2363             : 
    2364           0 : void OpenGL3DRenderer::GetBatchBarsInfo()
    2365             : {
    2366           0 :     for (size_t i = 0; i < m_Extrude3DList.size(); i++)
    2367             :     {
    2368           0 :         Extrude3DInfo &extrude3DInfo = m_Extrude3DList[i];
    2369           0 :         if (m_Extrude3DInfo.rounded)
    2370             :         {
    2371           0 :             GetBatchTopAndFlatInfo(extrude3DInfo);
    2372           0 :             GetBatchMiddleInfo(extrude3DInfo);
    2373             :         }
    2374             :         else
    2375             :         {
    2376           0 :             glm::mat4 transformMatrix = glm::translate(glm::vec3(extrude3DInfo.xTransform, extrude3DInfo.yTransform, extrude3DInfo.zTransform));
    2377           0 :             glm::mat4 scaleMatrix = glm::scale(glm::vec3(extrude3DInfo.xScale, extrude3DInfo.yScale, extrude3DInfo.zScale));
    2378           0 :             m_Model = transformMatrix * extrude3DInfo.rotation * scaleMatrix;
    2379           0 :             m_Model = m_GlobalScaleMatrix * m_Model;
    2380           0 :             glm::mat3 normalMatrix(m_Model);
    2381           0 :             glm::mat3 normalInverseTranspos = glm::inverseTranspose(normalMatrix);
    2382           0 :             m_BarSurface[0].modelMatrixList.push_back(m_Model);
    2383           0 :             m_BarSurface[0].normalMatrixList.push_back(normalInverseTranspos);
    2384           0 :             m_BarSurface[0].colorList.push_back(extrude3DInfo.material.materialColor);
    2385           0 :             m_BarSurface[0].mapId2Color[extrude3DInfo.orgID] = m_BarSurface[0].colorList.size() - 1;
    2386             :         }
    2387             :     }
    2388           0 : }
    2389             : 
    2390           0 : void OpenGL3DRenderer::SetHighLightBar(BatchBarInfo &barInfo)
    2391             : {
    2392           0 :     std::map<sal_uInt32, unsigned int> ::iterator it = barInfo.mapId2Color.find(m_uiSelectID);
    2393           0 :     if (it != barInfo.mapId2Color.end())
    2394             :     {
    2395           0 :         unsigned int idx = it->second;
    2396           0 :         barInfo.selectBarColor = barInfo.colorList[idx];
    2397           0 :         barInfo.colorList[idx] = glm::vec4(1.0, 1.0, 1.0, 1.0);
    2398             :     }
    2399           0 : }
    2400             : 
    2401           0 : void OpenGL3DRenderer::DisableHighLightBar(BatchBarInfo &barInfo)
    2402             : {
    2403           0 :     std::map<sal_uInt32, unsigned int> ::iterator it = barInfo.mapId2Color.find(m_uiSelectID);
    2404           0 :     if (it != barInfo.mapId2Color.end())
    2405             :     {
    2406           0 :         unsigned int idx = it->second;
    2407           0 :         barInfo.colorList[idx] = barInfo.selectBarColor;
    2408             :     }
    2409           0 : }
    2410             : 
    2411           0 : void OpenGL3DRenderer::StartClick(sal_uInt32 &selectID)
    2412             : {
    2413           0 :     m_bHighLighting = true;
    2414           0 :     m_uiSelectID = selectID;
    2415           0 :     for (unsigned int i = 0; i < 3; i++)
    2416             :     {
    2417           0 :         SetHighLightBar(m_BarSurface[i]);
    2418             :     }
    2419           0 : }
    2420             : 
    2421           0 : void OpenGL3DRenderer::EndClick()
    2422             : {
    2423           0 :     m_bHighLighting = false;
    2424           0 :     for (unsigned int i = 0; i < 3; i++)
    2425             :     {
    2426           0 :         DisableHighLightBar(m_BarSurface[i]);
    2427             :     }
    2428           0 : }
    2429             : 
    2430           0 : void OpenGL3DRenderer::SetScroll()
    2431             : {
    2432           0 :     maResources.m_bScrollFlag = true;
    2433           0 : }
    2434             : 
    2435           0 : void OpenGL3DRenderer::SetScrollSpeed(float scrollSpeed)
    2436             : {
    2437           0 :     m_fScrollSpeed = scrollSpeed;
    2438           0 : }
    2439           0 : void OpenGL3DRenderer::SetScrollDistance(float scrollDistance)
    2440             : {
    2441           0 :     m_fScrollDistance = scrollDistance;
    2442           0 : }
    2443             : 
    2444           0 : void OpenGL3DRenderer::SetSceneEdge(float minCoordX, float maxCoordX)
    2445             : {
    2446           0 :     m_fMinCoordX = minCoordX * 0.01;
    2447           0 :     m_fMaxCoordX = maxCoordX * 0.01;
    2448           0 : }
    2449             : 
    2450           0 : void OpenGL3DRenderer::CalcScrollMoveMatrix(bool bNewScene)
    2451             : {
    2452           0 :     if (!maResources.m_bScrollFlag)
    2453           0 :         return;
    2454           0 :     if (bNewScene)
    2455           0 :         m_fCurDistance = -m_fScrollSpeed;
    2456           0 :     m_fCurDistance += m_fCurDistance >= m_fScrollDistance ? 0.0f : m_fScrollSpeed;
    2457           0 :     m_ScrollMoveMatrix = glm::translate(glm::vec3(-m_fCurDistance * 0.01, 0.0f, 0.0f));
    2458           0 :     m_bUndrawFlag = m_fCurDistance >= m_fScrollDistance;
    2459             : }
    2460             : 
    2461           0 : glm::mat4 OpenGL3DRenderer::GetDiffOfTwoCameras(const glm::vec3& rBeginPos, const glm::vec3& rEndPos, const glm::vec3& rBeginDirection, const glm::vec3& rEndDirection)
    2462             : {
    2463           0 :     glm::mat4 aBegin = glm::lookAt(glm::vec3(m_GlobalScaleMatrix * glm::vec4(rBeginPos, 1.0)),
    2464           0 :               glm::vec3(m_GlobalScaleMatrix * glm::vec4(rBeginDirection, 1.0)),
    2465           0 :               glm::vec3(0, 0, 1));
    2466           0 :     glm::mat4 aEnd = glm::lookAt(glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndPos, 1.0)),
    2467           0 :               glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndDirection, 1.0)),
    2468           0 :               glm::vec3(0, 0, 1));
    2469           0 :     return aEnd - aBegin;
    2470             : }
    2471             : 
    2472           0 : glm::mat4 OpenGL3DRenderer::GetDiffOfTwoCameras(const glm::vec3& rEndPos, const glm::vec3& rEndDirection)
    2473             : {
    2474           0 :     glm::mat4 aEnd = glm::lookAt(glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndPos, 1.0)),
    2475           0 :                      glm::vec3(m_GlobalScaleMatrix * glm::vec4(rEndDirection, 1.0)),glm::vec3(0, 0, 1));
    2476           0 :     return aEnd - m_3DView;
    2477             : }
    2478             : 
    2479           0 : glm::mat4 OpenGL3DRenderer::GetProjectionMatrix()
    2480             : {
    2481           0 :     return m_3DProjection;
    2482             : }
    2483             : 
    2484           0 : glm::mat4 OpenGL3DRenderer::GetViewMatrix()
    2485             : {
    2486           0 :     return m_3DView;
    2487             : }
    2488             : 
    2489           0 : glm::mat4 OpenGL3DRenderer::GetGlobalScaleMatrix()
    2490             : {
    2491           0 :     return m_GlobalScaleMatrix;
    2492             : }
    2493             : 
    2494           0 : void OpenGL3DRenderer::RenderBatchBars(bool bNewScene)
    2495             : {
    2496           0 :     if (m_BarSurface[0].modelMatrixList.empty())
    2497           0 :         return;
    2498             : 
    2499           0 :     if(bNewScene)
    2500             :     {
    2501           0 :         GetBatchBarsInfo();
    2502           0 :         if (m_bHighLighting)
    2503             :         {
    2504           0 :             for (unsigned int i = 0; i < 3; i++)
    2505             :             {
    2506           0 :                 SetHighLightBar(m_BarSurface[i]);
    2507             :             }
    2508             :         }
    2509             :     }
    2510           0 :     glEnable(GL_DEPTH_TEST);
    2511           0 :     glEnable(GL_CULL_FACE);
    2512           0 :     glCullFace(GL_BACK);
    2513           0 :     glPolygonOffset(0.0f, 0.0f);
    2514           0 :     glUseProgram(maResources.m_3DBatchProID);
    2515           0 :     UpdateBatch3DUniformBlock();
    2516           0 :     glBindBuffer(GL_UNIFORM_BUFFER, m_Batch3DUBOBuffer);
    2517           0 :     glBufferSubData(GL_UNIFORM_BUFFER, m_Batch3DActualSizeLight, sizeof(MaterialParameters), &m_Batchmaterial);
    2518           0 :     CHECK_GL_ERROR();
    2519           0 :     glBindBuffer(GL_UNIFORM_BUFFER, 0);
    2520           0 :     if (maResources.m_bScrollFlag)
    2521             :     {
    2522           0 :         glUniform1fv(maResources.m_3DBatchMinCoordXID, 1, &m_fMinCoordX);
    2523           0 :         glUniform1fv(maResources.m_3DBatchMaxCoordXID, 1, &m_fMaxCoordX);
    2524           0 :         glUniform1i(maResources.m_3DBatchUndrawID, m_bUndrawFlag);
    2525           0 :         glUniformMatrix4fv(maResources.m_3DBatchTransMatrixID, 1, GL_FALSE, &m_ScrollMoveMatrix[0][0]);
    2526             :     }
    2527           0 :     glUniformMatrix4fv(maResources.m_3DBatchViewID, 1, GL_FALSE, &m_3DView[0][0]);
    2528           0 :     glUniformMatrix4fv(maResources.m_3DBatchProjectionID, 1, GL_FALSE, &m_3DProjection[0][0]);
    2529           0 :     CHECK_GL_ERROR();
    2530           0 :     GLuint vertexBuf = m_Extrude3DInfo.rounded ? m_CubeVertexBuf : m_BoundBox;
    2531           0 :     GLuint normalBuf = m_Extrude3DInfo.rounded ? m_CubeNormalBuf : m_BoundBoxNormal;
    2532             :     //vertex
    2533           0 :     glEnableVertexAttribArray(maResources.m_3DBatchVertexID);
    2534           0 :     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             :                           nullptr            // array buffer offset
    2541           0 :                           );
    2542             :     //normal
    2543           0 :     glEnableVertexAttribArray(maResources.m_3DBatchNormalID);
    2544           0 :     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             :                             nullptr            // array buffer offset
    2551           0 :                             );
    2552             : 
    2553           0 :     for (unsigned int i = 0; i < 4 ; i++)
    2554             :     {
    2555           0 :         glEnableVertexAttribArray(maResources.m_3DBatchModelID + i);
    2556           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_BatchModelMatrixBuf);
    2557           0 :         glVertexAttribPointer(maResources.m_3DBatchModelID + i, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * i * 4));
    2558           0 :         glVertexAttribDivisor(maResources.m_3DBatchModelID + i, 1);
    2559             :     }
    2560             : 
    2561           0 :     for (unsigned int i = 0; i < 3 ; i++)
    2562             :     {
    2563           0 :         glEnableVertexAttribArray(maResources.m_3DBatchNormalMatrixID + i);
    2564           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_BatchNormalMatrixBuf);
    2565           0 :         glVertexAttribPointer(maResources.m_3DBatchNormalMatrixID + i, 3, GL_FLOAT, GL_FALSE, sizeof(glm::mat3), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * i * 3));
    2566           0 :         glVertexAttribDivisor(maResources.m_3DBatchNormalMatrixID + i, 1);
    2567             :     }
    2568           0 :     glEnableVertexAttribArray(maResources.m_3DBatchColorID);
    2569           0 :     glBindBuffer(GL_ARRAY_BUFFER, m_BatchColorBuf);
    2570           0 :     glVertexAttribPointer(maResources.m_3DBatchColorID , 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), 0);
    2571           0 :     glVertexAttribDivisor(maResources.m_3DBatchColorID, 1);
    2572           0 :     if (m_Extrude3DInfo.rounded)
    2573             :     {
    2574           0 :         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CubeElementBuf);
    2575           0 :         for (int i = 0; i < 2; i++)
    2576             :         {
    2577           0 :             glBindBuffer(GL_ARRAY_BUFFER, m_BatchModelMatrixBuf);
    2578           0 :             glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * m_BarSurface[i].modelMatrixList.size(), &m_BarSurface[i].modelMatrixList[0][0], GL_DYNAMIC_DRAW);
    2579           0 :             glBindBuffer(GL_ARRAY_BUFFER, m_BatchNormalMatrixBuf);
    2580           0 :             glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat3) * m_BarSurface[i].normalMatrixList.size(), &m_BarSurface[i].normalMatrixList[0][0], GL_DYNAMIC_DRAW);
    2581           0 :             glBindBuffer(GL_ARRAY_BUFFER, m_BatchColorBuf);
    2582           0 :             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           0 :                                               reinterpret_cast<GLvoid*>(m_Extrude3DInfo.startIndex[i]),
    2587           0 :                                               m_BarSurface[i].modelMatrixList.size(),
    2588           0 :                                               0);
    2589             :         }
    2590             :     }
    2591             :     else
    2592             :     {
    2593           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_BatchModelMatrixBuf);
    2594           0 :         glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * m_BarSurface[0].modelMatrixList.size(), &m_BarSurface[0].modelMatrixList[0][0], GL_DYNAMIC_DRAW);
    2595           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_BatchNormalMatrixBuf);
    2596           0 :         glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat3) * m_BarSurface[0].normalMatrixList.size(), &m_BarSurface[0].normalMatrixList[0][0], GL_DYNAMIC_DRAW);
    2597           0 :         glBindBuffer(GL_ARRAY_BUFFER, m_BatchColorBuf);
    2598           0 :         glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * m_BarSurface[0].colorList.size(), &m_BarSurface[0].colorList[0], GL_DYNAMIC_DRAW);
    2599           0 :         glDrawArraysInstanced(GL_TRIANGLES, 0, 36, m_BarSurface[0].modelMatrixList.size());
    2600             :     }
    2601           0 :     glDisableVertexAttribArray(maResources.m_3DBatchVertexID);
    2602           0 :     glDisableVertexAttribArray(maResources.m_3DBatchNormalID);
    2603           0 :     glDisableVertexAttribArray(maResources.m_3DBatchColorID);
    2604           0 :     glVertexAttribDivisor(maResources.m_3DBatchColorID, 0);
    2605           0 :     for (unsigned int i = 0; i < 4 ; i++)
    2606             :     {
    2607           0 :         glDisableVertexAttribArray(maResources.m_3DBatchModelID + i);
    2608           0 :         glVertexAttribDivisor(maResources.m_3DBatchModelID + i, 0);
    2609             :     }
    2610           0 :     for (unsigned int i = 0; i < 3 ; i++)
    2611             :     {
    2612           0 :         glDisableVertexAttribArray(maResources.m_3DBatchNormalMatrixID + i);
    2613           0 :         glVertexAttribDivisor(maResources.m_3DBatchNormalMatrixID + i, 0);
    2614             :     }
    2615           0 :     glUseProgram(0);
    2616           0 :     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    2617           0 :     glDisable(GL_CULL_FACE);
    2618             : }
    2619             : }
    2620             : }
    2621             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11