diff options
author | Markus Mohrhard <markus.mohrhard@collabora.co.uk> | 2014-04-15 00:55:07 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@collabora.co.uk> | 2014-04-15 01:51:28 +0200 |
commit | cd56c7bcf375a2ec9b93496f37ab781887fc12f1 (patch) | |
tree | a36b65477a901a1d61f4caf588ff39bcaba8f7d5 | |
parent | ce9f9de1971285d41831fa5a5df7462889155afe (diff) |
move some common OpenGL methods to vcl
Change-Id: Ic96487afce64bfb0c1dfcc03c088e5d6e1b34ad3
-rwxr-xr-x | chart2/source/view/main/OpenGLRender.cxx | 141 | ||||
-rwxr-xr-x | chart2/source/view/main/OpenGLRender.hxx | 1 | ||||
-rw-r--r-- | include/vcl/opengl/OpenGLHelper.hxx | 27 | ||||
-rw-r--r-- | vcl/Library_vclopengl.mk | 1 | ||||
-rw-r--r-- | vcl/source/opengl/OpenGLHelper.cxx | 145 |
5 files changed, 179 insertions, 136 deletions
diff --git a/chart2/source/view/main/OpenGLRender.cxx b/chart2/source/view/main/OpenGLRender.cxx index 01331a04e15b..0ccb353004f9 100755 --- a/chart2/source/view/main/OpenGLRender.cxx +++ b/chart2/source/view/main/OpenGLRender.cxx @@ -35,9 +35,7 @@ #include <vcl/virdev.hxx> #include <vcl/dibtools.hxx> -#include <osl/file.hxx> -#include <rtl/bootstrap.hxx> -#include <config_folders.h> +#include <vcl/opengl/OpenGLHelper.hxx> #include <boost/scoped_array.hpp> @@ -96,133 +94,6 @@ static bool bGlewInit = false; namespace { -OUString getShaderFolder() -{ - OUString aUrl("$BRAND_BASE_DIR/" LIBO_ETC_FOLDER); - rtl::Bootstrap::expandMacros(aUrl); - - return aUrl + "/opengl/"; -} - -OUString maShaderFolder = getShaderFolder(); - -OString loadShader(const OUString& rFilename) -{ - OUString aFileURL = maShaderFolder + rFilename +".glsl"; - osl::File aFile(aFileURL); - if(aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None) - { - sal_uInt64 nSize = 0; - aFile.getSize(nSize); - char* content = new char[nSize+1]; - sal_uInt64 nBytesRead = 0; - aFile.read(content, nSize, nBytesRead); - if(nSize != nBytesRead) - assert(false); - - content[nSize] = 0; - return OString(content); - } - else - { - SAL_WARN("chart2.opengl", "could not load the file: " << aFileURL); - } - - return OString(); -} - -} - -GLint OpenGLRender::LoadShaders(const OUString& rVertexShaderName,const OUString& rFragmentShaderName) -{ - // Create the shaders - GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); - GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); - - GLint Result = GL_FALSE; - int InfoLogLength; - - // Compile Vertex Shader - OString aVertexShaderSource = loadShader(rVertexShaderName); - char const * VertexSourcePointer = aVertexShaderSource.getStr(); - glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); - glCompileShader(VertexShaderID); - - // Check Vertex Shader - glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); - if ( !Result ) - { - glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ) - { - std::vector<char> VertexShaderErrorMessage(InfoLogLength+1); - glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); - VertexShaderErrorMessage.push_back('\0'); - SAL_WARN("chart2.opengl", "vertex shader compile failed : " << &VertexShaderErrorMessage[0]); - } - else - SAL_WARN("chart2.opengl", "vertex shader compile failed without error log"); - - return 0; - } - - // Compile Fragment Shader - OString aFragmentShaderSource = loadShader(rFragmentShaderName); - char const * FragmentSourcePointer = aFragmentShaderSource.getStr(); - glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); - glCompileShader(FragmentShaderID); - - // Check Fragment Shader - glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); - if ( !Result ) - { - glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ) - { - std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1); - glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); - FragmentShaderErrorMessage.push_back('\0'); - SAL_WARN("chart2.opengl", "fragment shader compile failed : " << &FragmentShaderErrorMessage[0]); - } - else - SAL_WARN("chart2.opengl", "fragment shader compile failed without error log"); - - - return 0; - } - - // Link the program - GLint ProgramID = glCreateProgram(); - glAttachShader(ProgramID, VertexShaderID); - glAttachShader(ProgramID, FragmentShaderID); - glLinkProgram(ProgramID); - - // Check the program - glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); - if ( !Result ) - { - glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ) - { - std::vector<char> ProgramErrorMessage(InfoLogLength+1); - glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); - ProgramErrorMessage.push_back('\0'); - SAL_WARN("chart2.opengl", "Shader Program failed : " << &ProgramErrorMessage[0]); - } - else - SAL_WARN("chart2.opengl", "shader program link failed without error log"); - - return 0; - } - - glDeleteShader(VertexShaderID); - glDeleteShader(FragmentShaderID); - - return ProgramID; -} - -namespace { - GLfloat texCoords[] = { 0.0f, 0.0f, 1.0f, 0.0f, @@ -287,26 +158,26 @@ int OpenGLRender::InitOpenGL() CHECK_GL_ERROR(); - m_CommonProID = LoadShaders("commonVertexShader", "commonFragmentShader"); + m_CommonProID = OpenGLHelper::LoadShaders("commonVertexShader", "commonFragmentShader"); m_MatrixID = glGetUniformLocation(m_CommonProID, "MVP"); m_2DVertexID = glGetAttribLocation(m_CommonProID, "vPosition"); m_2DColorID = glGetUniformLocation(m_CommonProID, "vColor"); CHECK_GL_ERROR(); #if DEBUG_POSITIONING - m_DebugProID = LoadShaders("debugVertexShader", "debugFragmentShader"); + m_DebugProID = OpenGLHelper::LoadShaders("debugVertexShader", "debugFragmentShader"); m_DebugVertexID = glGetAttribLocation(m_DebugProID, "vPosition"); CHECK_GL_ERROR(); #endif - m_BackgroundProID = LoadShaders("backgroundVertexShader", "backgroundFragmentShader"); + m_BackgroundProID = OpenGLHelper::LoadShaders("backgroundVertexShader", "backgroundFragmentShader"); m_BackgroundMatrixID = glGetUniformLocation(m_BackgroundProID, "MVP"); m_BackgroundVertexID = glGetAttribLocation(m_BackgroundProID, "vPosition"); m_BackgroundColorID = glGetAttribLocation(m_BackgroundProID, "vColor"); CHECK_GL_ERROR(); - m_SymbolProID = LoadShaders("symbolVertexShader", "symbolFragmentShader"); + m_SymbolProID = OpenGLHelper::LoadShaders("symbolVertexShader", "symbolFragmentShader"); m_SymbolVertexID = glGetAttribLocation(m_SymbolProID, "vPosition"); m_SymbolMatrixID = glGetUniformLocation(m_SymbolProID, "MVP"); m_SymbolColorID = glGetUniformLocation(m_SymbolProID, "vColor"); @@ -314,7 +185,7 @@ int OpenGLRender::InitOpenGL() CHECK_GL_ERROR(); - m_TextProID = LoadShaders("textVertexShader", "textFragmentShader"); + m_TextProID = OpenGLHelper::LoadShaders("textVertexShader", "textFragmentShader"); m_TextMatrixID = glGetUniformLocation(m_TextProID, "MVP"); m_TextVertexID = glGetAttribLocation(m_TextProID, "vPosition"); m_TextTexCoordID = glGetAttribLocation(m_TextProID, "texCoord"); diff --git a/chart2/source/view/main/OpenGLRender.hxx b/chart2/source/view/main/OpenGLRender.hxx index d0121ad9b4cc..2f79e1c35fb0 100755 --- a/chart2/source/view/main/OpenGLRender.hxx +++ b/chart2/source/view/main/OpenGLRender.hxx @@ -108,7 +108,6 @@ public: void SetBackGroundColor(sal_uInt32 color1, sal_uInt32 color2, sal_uInt8 nAlpha); private: - GLint LoadShaders(const OUString& rVertexShaderName, const OUString& rFragmentShaderName); int CreateTextureObj(int width, int height); int CreateRenderObj(int width, int height); int CreateFrameBufferObj(); diff --git a/include/vcl/opengl/OpenGLHelper.hxx b/include/vcl/opengl/OpenGLHelper.hxx new file mode 100644 index 000000000000..77d1b2840c45 --- /dev/null +++ b/include/vcl/opengl/OpenGLHelper.hxx @@ -0,0 +1,27 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef VCL_OPENGLHELPERS_HXX +#define VCL_OPENGLHELPERS_HXX + +#include <GL/glew.h> +#include <vcl/vclopengl_dllapi.hxx> + +#include <rtl/ustring.hxx> + +class VCLOPENGL_DLLPUBLIC OpenGLHelper +{ +public: + static GLint LoadShaders(const OUString& rVertexShaderName, const OUString& rFragmentShaderName); + +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/Library_vclopengl.mk b/vcl/Library_vclopengl.mk index e14899baf3de..84310bb84811 100644 --- a/vcl/Library_vclopengl.mk +++ b/vcl/Library_vclopengl.mk @@ -39,6 +39,7 @@ $(eval $(call gb_Library_use_libraries,vclopengl,\ $(eval $(call gb_Library_add_exception_objects,vclopengl,\ vcl/source/opengl/OpenGLContext \ + vcl/source/opengl/OpenGLHelper \ )) ifeq ($(strip $(OS)),WNT) diff --git a/vcl/source/opengl/OpenGLHelper.cxx b/vcl/source/opengl/OpenGLHelper.cxx new file mode 100644 index 000000000000..75eefe7c1f3f --- /dev/null +++ b/vcl/source/opengl/OpenGLHelper.cxx @@ -0,0 +1,145 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <vcl/opengl/OpenGLHelper.hxx> + +#include <osl/file.hxx> +#include <rtl/bootstrap.hxx> +#include <config_folders.h> + +#include <vector> + +namespace { + +OUString getShaderFolder() +{ + OUString aUrl("$BRAND_BASE_DIR/" LIBO_ETC_FOLDER); + rtl::Bootstrap::expandMacros(aUrl); + + return aUrl + "/opengl/"; +} + +OUString maShaderFolder = getShaderFolder(); + +OString loadShader(const OUString& rFilename) +{ + OUString aFileURL = maShaderFolder + rFilename +".glsl"; + osl::File aFile(aFileURL); + if(aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None) + { + sal_uInt64 nSize = 0; + aFile.getSize(nSize); + char* content = new char[nSize+1]; + sal_uInt64 nBytesRead = 0; + aFile.read(content, nSize, nBytesRead); + if(nSize != nBytesRead) + assert(false); + + content[nSize] = 0; + return OString(content); + } + else + { + SAL_WARN("vcl.opengl", "could not load the file: " << aFileURL); + } + + return OString(); +} + +} + +GLint OpenGLHelper::LoadShaders(const OUString& rVertexShaderName,const OUString& rFragmentShaderName) +{ + // Create the shaders + GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); + GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); + + GLint Result = GL_FALSE; + int InfoLogLength; + + // Compile Vertex Shader + OString aVertexShaderSource = loadShader(rVertexShaderName); + char const * VertexSourcePointer = aVertexShaderSource.getStr(); + glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); + glCompileShader(VertexShaderID); + + // Check Vertex Shader + glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); + if ( !Result ) + { + glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ) + { + std::vector<char> VertexShaderErrorMessage(InfoLogLength+1); + glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); + VertexShaderErrorMessage.push_back('\0'); + SAL_WARN("vcl.opengl", "vertex shader compile failed : " << &VertexShaderErrorMessage[0]); + } + else + SAL_WARN("vcl.opengl", "vertex shader compile failed without error log"); + + return 0; + } + + // Compile Fragment Shader + OString aFragmentShaderSource = loadShader(rFragmentShaderName); + char const * FragmentSourcePointer = aFragmentShaderSource.getStr(); + glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); + glCompileShader(FragmentShaderID); + + // Check Fragment Shader + glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); + if ( !Result ) + { + glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ) + { + std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1); + glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); + FragmentShaderErrorMessage.push_back('\0'); + SAL_WARN("vcl.opengl", "fragment shader compile failed : " << &FragmentShaderErrorMessage[0]); + } + else + SAL_WARN("vcl.opengl", "fragment shader compile failed without error log"); + + + return 0; + } + + // Link the program + GLint ProgramID = glCreateProgram(); + glAttachShader(ProgramID, VertexShaderID); + glAttachShader(ProgramID, FragmentShaderID); + glLinkProgram(ProgramID); + + // Check the program + glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); + if ( !Result ) + { + glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ) + { + std::vector<char> ProgramErrorMessage(InfoLogLength+1); + glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); + ProgramErrorMessage.push_back('\0'); + SAL_WARN("vcl.opengl", "Shader Program failed : " << &ProgramErrorMessage[0]); + } + else + SAL_WARN("vcl.opengl", "shader program link failed without error log"); + + return 0; + } + + glDeleteShader(VertexShaderID); + glDeleteShader(FragmentShaderID); + + return ProgramID; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |