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

Generated by: LCOV version 1.10