From 495d53b1dc0f3c32e0b76344775b9fc728e09fe5 Mon Sep 17 00:00:00 2001 From: weigao Date: Sun, 3 Aug 2014 18:36:56 +0800 Subject: [PATCH] make pick shader work when bars scroll Change-Id: I9a3e0e36b212ff49ad22ac0ff267d04476b4c307 --- chart2/opengl/pickingFragmentShader.glsl | 5 ++++- chart2/opengl/pickingVertexShader.glsl | 7 +++++- chart2/source/view/inc/GL3DRenderer.hxx | 4 +++- chart2/source/view/main/GL3DRenderer.cxx | 28 +++++++++++++++++++----- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/chart2/opengl/pickingFragmentShader.glsl b/chart2/opengl/pickingFragmentShader.glsl index 805789e731ed..e2fba0391442 100644 --- a/chart2/opengl/pickingFragmentShader.glsl +++ b/chart2/opengl/pickingFragmentShader.glsl @@ -6,11 +6,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - +uniform float minCoordX; +varying vec3 positionWorldspace; varying vec4 fragmentColor; void main() { + if (positionWorldspace.x <= minCoordX) + discard; gl_FragColor = fragmentColor; } diff --git a/chart2/opengl/pickingVertexShader.glsl b/chart2/opengl/pickingVertexShader.glsl index f32e3fd38a91..df71ae170f01 100644 --- a/chart2/opengl/pickingVertexShader.glsl +++ b/chart2/opengl/pickingVertexShader.glsl @@ -9,11 +9,16 @@ attribute vec3 vPosition; uniform mat4 MVP; +uniform mat4 M; uniform vec4 vColor; +uniform float minCoordX; +uniform float maxCoordX; varying vec4 fragmentColor; - +varying vec3 positionWorldspace; void main() { + positionWorldspace = (M * vec4(vPosition,1)).xyz; + positionWorldspace.x = clamp(positionWorldspace.x, minCoordX, maxCoordX); gl_Position = MVP * vec4(vPosition, 1); fragmentColor = vColor; } diff --git a/chart2/source/view/inc/GL3DRenderer.hxx b/chart2/source/view/inc/GL3DRenderer.hxx index 8b879a5cd8d4..5a8b6ece4c81 100644 --- a/chart2/source/view/inc/GL3DRenderer.hxx +++ b/chart2/source/view/inc/GL3DRenderer.hxx @@ -356,7 +356,9 @@ private: GLint m_2DVertexID; GLint m_2DColorID; GLint m_MatrixID; - + GLint m_ModelID; + GLint m_MinCoordXID; + GLint m_MaxCoordXID; PickingShaderResources(); ~PickingShaderResources(); diff --git a/chart2/source/view/main/GL3DRenderer.cxx b/chart2/source/view/main/GL3DRenderer.cxx index 51f3a1e5ea56..b09238d64f58 100644 --- a/chart2/source/view/main/GL3DRenderer.cxx +++ b/chart2/source/view/main/GL3DRenderer.cxx @@ -115,6 +115,7 @@ OpenGL3DRenderer::OpenGL3DRenderer(): , m_fMaxCoordX(0.0f) , m_fCurDistance(0.0f) , m_bUndrawFlag(false) + , m_ScrollMoveMatrix(glm::mat4(1.0)) { m_Polygon3DInfo.lineOnly = false; m_Polygon3DInfo.twoSidesLighting = false; @@ -315,6 +316,9 @@ OpenGL3DRenderer::PickingShaderResources::PickingShaderResources() , m_2DVertexID(0) , m_2DColorID(0) , m_MatrixID(0) + , m_ModelID(0) + , m_MinCoordXID(0) + , m_MaxCoordXID(0) { } @@ -329,6 +333,9 @@ void OpenGL3DRenderer::PickingShaderResources::LoadShaders() m_MatrixID = glGetUniformLocation(m_CommonProID, "MVP"); m_2DVertexID = glGetAttribLocation(m_CommonProID, "vPosition"); m_2DColorID = glGetUniformLocation(m_CommonProID, "vColor"); + m_ModelID = glGetUniformLocation(m_CommonProID, "M"); + m_MinCoordXID = glGetUniformLocation(m_CommonProID, "minCoordX"); + m_MaxCoordXID = glGetUniformLocation(m_CommonProID, "maxCoordX"); } void OpenGL3DRenderer::SetCameraInfo(const glm::vec3& pos, const glm::vec3& direction, const glm::vec3& up) @@ -884,6 +891,10 @@ void OpenGL3DRenderer::RenderPolygon3D(const Polygon3DInfo& polygon) if(mbPickingMode) { glUseProgram(maPickingResources.m_CommonProID); + float minCoordX = 0.0f; + float maxCoordX = m_fMinCoordX + m_fMaxCoordX; + glUniform1fv(maPickingResources.m_MinCoordXID, 1, &minCoordX); + glUniform1fv(maPickingResources.m_MaxCoordXID, 1, &maxCoordX); } else { @@ -1327,7 +1338,7 @@ void OpenGL3DRenderer::RenderExtrudeFlatSurface(const Extrude3DInfo& extrude3D, extrude3D.zTransform}; glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z)); glm::mat4 flatScale = glm::scale(glm::vec3(xyScale, xyScale, xyScale)); - m_Model = m_GlobalScaleMatrix * aTranslationMatrix * extrude3D.rotation * flatScale; + m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * aTranslationMatrix * extrude3D.rotation * flatScale; if(!mbPickingMode) { glm::mat3 normalMatrix(m_Model); @@ -1338,6 +1349,7 @@ void OpenGL3DRenderer::RenderExtrudeFlatSurface(const Extrude3DInfo& extrude3D, else { glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model; + glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]); glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]); glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]); } @@ -1371,7 +1383,7 @@ void OpenGL3DRenderer::RenderExtrudeBottomSurface(const Extrude3DInfo& extrude3D glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z)); m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale; } - m_Model = m_GlobalScaleMatrix * m_Model; + m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model; if(!mbPickingMode) { glm::mat3 normalMatrix(m_Model); @@ -1382,6 +1394,7 @@ void OpenGL3DRenderer::RenderExtrudeBottomSurface(const Extrude3DInfo& extrude3D else { glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model; + glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]); glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]); glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]); } @@ -1416,7 +1429,7 @@ void OpenGL3DRenderer::RenderExtrudeMiddleSurface(const Extrude3DInfo& extrude3D glm::mat4 reverseMatrix = glm::translate(glm::vec3(0.0, 0.0, -1.0)); m_Model = m_Model * reverseMatrix; } - m_Model = m_GlobalScaleMatrix * m_Model; + m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model; if(!mbPickingMode) { glm::mat3 normalMatrix(m_Model); @@ -1427,6 +1440,7 @@ void OpenGL3DRenderer::RenderExtrudeMiddleSurface(const Extrude3DInfo& extrude3D else { glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model; + glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]); glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]); glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]); } @@ -1461,7 +1475,7 @@ void OpenGL3DRenderer::RenderExtrudeTopSurface(const Extrude3DInfo& extrude3D) glm::mat4 aTranslationMatrix = glm::translate(glm::vec3(trans.x, trans.y, trans.z)); m_Model = aTranslationMatrix * extrude3D.rotation * topTrans * topScale * orgTrans; } - m_Model = m_GlobalScaleMatrix * m_Model; + m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model; if(!mbPickingMode) { glm::mat3 normalMatrix(m_Model); @@ -1472,6 +1486,7 @@ void OpenGL3DRenderer::RenderExtrudeTopSurface(const Extrude3DInfo& extrude3D) else { glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model; + glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]); glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]); glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]); } @@ -1491,7 +1506,7 @@ void OpenGL3DRenderer::RenderNonRoundedBar(const Extrude3DInfo& extrude3D) glm::mat4 reverseMatrix = glm::translate(glm::vec3(0.0, 0.0, -1.0)); m_Model = m_Model * reverseMatrix; } - m_Model = m_GlobalScaleMatrix * m_Model; + m_Model = m_ScrollMoveMatrix * m_GlobalScaleMatrix * m_Model; if(!mbPickingMode) { glm::mat3 normalMatrix(m_Model); @@ -1502,6 +1517,7 @@ void OpenGL3DRenderer::RenderNonRoundedBar(const Extrude3DInfo& extrude3D) else { glm::mat4 aMVP = m_3DProjection * m_3DView * m_Model; + glUniformMatrix4fv(maPickingResources.m_ModelID, 1, GL_FALSE, &m_Model[0][0]); glUniformMatrix4fv(maPickingResources.m_MatrixID, 1, GL_FALSE, &aMVP[0][0]); glUniform4fv(maPickingResources.m_2DColorID, 1, &extrude3D.id[0]); } @@ -1536,6 +1552,8 @@ void OpenGL3DRenderer::RenderExtrude3DObject() if(mbPickingMode) { glUseProgram(maPickingResources.m_CommonProID); + glUniform1fv(maPickingResources.m_MinCoordXID, 1, &m_fMinCoordX); + glUniform1fv(maPickingResources.m_MaxCoordXID, 1, &m_fMaxCoordX); } else {