tools/CppunitTest_tools_test.mk | 1 tools/qa/cppunit/test_rectangle.cxx | 60 ++++++++++++++++++ vcl/inc/opengl/program.hxx | 2 vcl/inc/openglgdiimpl.hxx | 2 vcl/opengl/blendedTextureVertexShader.glsl | 3 vcl/opengl/dumbVertexShader.glsl | 4 - vcl/opengl/gdiimpl.cxx | 82 ++++++++++++------------- vcl/opengl/program.cxx | 30 +++++++-- vcl/opengl/scale.cxx | 1 vcl/opengl/textureVertexShader.glsl | 3 vcl/opengl/transformedTextureVertexShader.glsl | 5 - 11 files changed, 143 insertions(+), 50 deletions(-)
New commits: commit 2e99e4e11d33679aed674eea0d6054d16d39d6df Author: Tomaž Vajngerl <[email protected]> Date: Wed Jul 8 18:43:32 2015 +0900 opengl: use MVP matrix in vertex shaders, pixel offsets ChangChange all vertex shaders to accept model, view, projection matrix to calculate the vertex position. So now we don't need to convert the coordinates to OpenGL coordinate space [-1.0, 1.0] anymore. Additionally make it possible to offset vertex coordinates so we can apply 0.5 px offset (to hit the pixel center) at some operations. Change-Id: I8e0a61d5fd4ab6aaa1c0c94439061725918577a0 diff --git a/vcl/inc/opengl/program.hxx b/vcl/inc/opengl/program.hxx index 3ac0ff4..5ca9aff 100644 --- a/vcl/inc/opengl/program.hxx +++ b/vcl/inc/opengl/program.hxx @@ -67,6 +67,8 @@ public: const basegfx::B2DPoint& rY ); void SetBlendMode( GLenum nSFactor, GLenum nDFactor ); + void ApplyMatrix(float fWidth, float fHeight, float fPixelOffset = 0.0f); + bool DrawTexture( OpenGLTexture& rTexture ); protected: diff --git a/vcl/inc/openglgdiimpl.hxx b/vcl/inc/openglgdiimpl.hxx index 444c9f2..b056382 100644 --- a/vcl/inc/openglgdiimpl.hxx +++ b/vcl/inc/openglgdiimpl.hxx @@ -72,6 +72,8 @@ protected: void ImplDrawLineAA( double nX1, double nY1, double nX2, double nY2, bool edge = false ); bool CheckOffscreenTexture(); + void ApplyProgramMatrices(float fPixelOffset = 0.0); + public: bool UseProgram( const OUString& rVertexShader, const OUString& rFragmentShader, const OString& preamble = "" ); bool UseSolid( SalColor nColor, sal_uInt8 nTransparency ); diff --git a/vcl/opengl/blendedTextureVertexShader.glsl b/vcl/opengl/blendedTextureVertexShader.glsl index bc8972c..3a9b827 100644 --- a/vcl/opengl/blendedTextureVertexShader.glsl +++ b/vcl/opengl/blendedTextureVertexShader.glsl @@ -12,9 +12,10 @@ attribute vec2 tex_coord_in; attribute vec2 alpha_coord_in; varying vec2 tex_coord; varying vec2 alpha_coord; +uniform mat4 mvp; void main() { - gl_Position = position; + gl_Position = mvp * position; tex_coord = tex_coord_in; alpha_coord = alpha_coord_in; } diff --git a/vcl/opengl/dumbVertexShader.glsl b/vcl/opengl/dumbVertexShader.glsl index 47061f6..deaa35b 100644 --- a/vcl/opengl/dumbVertexShader.glsl +++ b/vcl/opengl/dumbVertexShader.glsl @@ -8,8 +8,10 @@ */ attribute vec4 position; +uniform mat4 mvp; + void main() { - gl_Position = position; + gl_Position = mvp * position; } diff --git a/vcl/opengl/gdiimpl.cxx b/vcl/opengl/gdiimpl.cxx index e9ad376..97f1c14 100644 --- a/vcl/opengl/gdiimpl.cxx +++ b/vcl/opengl/gdiimpl.cxx @@ -36,9 +36,6 @@ #include <vector> -#define OPENGL_COORD_X(x) GLfloat((2.0 * double(x)) / GetWidth() - 1.0) -#define OPENGL_COORD_Y(y) GLfloat(1.0 - (2.0 * double(y)) / GetHeight()) - OpenGLSalGraphicsImpl::OpenGLSalGraphicsImpl(SalGraphics& rParent, SalGeometryProvider *pProvider) : mpContext(0) , mrParent(rParent) @@ -194,6 +191,11 @@ void OpenGLSalGraphicsImpl::PostDraw() CHECK_GL_ERROR(); } +void OpenGLSalGraphicsImpl::ApplyProgramMatrices(float fPixelOffset) +{ + mpProgram->ApplyMatrix(GetWidth(), GetHeight(), fPixelOffset); +} + void OpenGLSalGraphicsImpl::freeResources() { // TODO Delete shaders, programs and textures if not shared @@ -453,9 +455,10 @@ void OpenGLSalGraphicsImpl::DrawPoint( long nX, long nY ) { GLfloat pPoint[2]; - pPoint[0] = OPENGL_COORD_X(nX); - pPoint[1] = OPENGL_COORD_Y(nY); + pPoint[0] = GLfloat(nX); + pPoint[1] = GLfloat(nY); + ApplyProgramMatrices(0.5f); mpProgram->SetVertices( pPoint ); glDrawArrays( GL_POINTS, 0, 1 ); @@ -466,11 +469,12 @@ void OpenGLSalGraphicsImpl::DrawLine( double nX1, double nY1, double nX2, double { GLfloat pPoints[4]; - pPoints[0] = OPENGL_COORD_X(nX1); - pPoints[1] = OPENGL_COORD_Y(nY1); - pPoints[2] = OPENGL_COORD_X(nX2); - pPoints[3] = OPENGL_COORD_Y(nY2); + pPoints[0] = GLfloat(nX1); + pPoints[1] = GLfloat(nY1); + pPoints[2] = GLfloat(nX2); + pPoints[3] = GLfloat(nY2); + ApplyProgramMatrices(0.5f); mpProgram->SetVertices( pPoints ); glDrawArrays( GL_LINES, 0, 2 ); @@ -484,19 +488,12 @@ void OpenGLSalGraphicsImpl::DrawLineAA( double nX1, double nY1, double nX2, doub if( nX1 == nX2 || nY1 == nY2 ) { // Horizontal/vertical, no need for AA, both points have normal color. - GLfloat pPoints[4]; - - pPoints[0] = OPENGL_COORD_X(nX1); - pPoints[1] = OPENGL_COORD_Y(nY1); - pPoints[2] = OPENGL_COORD_X(nX2); - pPoints[3] = OPENGL_COORD_Y(nY2); - mpProgram->SetVertices( pPoints ); // Still set up for the trivial "gradients", because presumably UseSolidAA() has been called. GLfloat aTexCoord[4] = { 0, 1, 1, 1 }; mpProgram->SetTextureCoord( aTexCoord ); + DrawLine(nX1, nY1, nX2, nY2); - glDrawArrays( GL_LINES, 0, 2 ); return; } ImplDrawLineAA( nX1, nY1, nX2, nY2 ); @@ -623,16 +620,17 @@ void OpenGLSalGraphicsImpl::ImplDrawLineAA( double nX1, double nY1, double nX2, GLfloat vertices[]= { - OPENGL_COORD_X(x1-tx-Rx), OPENGL_COORD_Y(y1-ty-Ry), //fading edge1 - OPENGL_COORD_X(x2-tx-Rx), OPENGL_COORD_Y(y2-ty-Ry), - OPENGL_COORD_X(x1-tx), OPENGL_COORD_Y(y1-ty), //core - OPENGL_COORD_X(x2-tx), OPENGL_COORD_Y(y2-ty), - OPENGL_COORD_X(x1+tx), OPENGL_COORD_Y(y1+ty), - OPENGL_COORD_X(x2+tx), OPENGL_COORD_Y(y2+ty), - OPENGL_COORD_X(x1+tx+Rx), OPENGL_COORD_Y(y1+ty+Ry), //fading edge2 - OPENGL_COORD_X(x2+tx+Rx), OPENGL_COORD_Y(y2+ty+Ry) + GLfloat(x1-tx-Rx), GLfloat(y1-ty-Ry), //fading edge1 + GLfloat(x2-tx-Rx), GLfloat(y2-ty-Ry), + GLfloat(x1-tx), GLfloat(y1-ty), //core + GLfloat(x2-tx), GLfloat(y2-ty), + GLfloat(x1+tx), GLfloat(y1+ty), + GLfloat(x2+tx), GLfloat(y2+ty), + GLfloat(x1+tx+Rx), GLfloat(y1+ty+Ry), //fading edge2 + GLfloat(x2+tx+Rx), GLfloat(y2+ty+Ry) }; + ApplyProgramMatrices(0.0f); GLfloat aTexCoord[16] = { 0, 0, 1, 0, 2, 1, 3, 1, 4, 1, 5, 1, 6, 0, 7, 0 }; mpProgram->SetTextureCoord( aTexCoord ); mpProgram->SetVertices( vertices ); @@ -665,10 +663,11 @@ void OpenGLSalGraphicsImpl::DrawConvexPolygon( sal_uInt32 nPoints, const SalPoin for( i = 0, j = 0; i < nPoints; i++, j += 2 ) { - aVertices[j] = OPENGL_COORD_X(pPtAry[i].mnX); - aVertices[j+1] = OPENGL_COORD_Y(pPtAry[i].mnY); + aVertices[j] = GLfloat(pPtAry[i].mnX); + aVertices[j+1] = GLfloat(pPtAry[i].mnY); } + ApplyProgramMatrices(); mpProgram->SetVertices( &aVertices[0] ); glDrawArrays( GL_TRIANGLE_FAN, 0, nPoints ); @@ -707,10 +706,11 @@ void OpenGLSalGraphicsImpl::DrawConvexPolygon( const Polygon& rPolygon, bool blo for( i = 0, j = 0; i < nPoints; i++, j += 2 ) { const Point& rPt = rPolygon.GetPoint( i ); - aVertices[j] = OPENGL_COORD_X(rPt.X()); - aVertices[j+1] = OPENGL_COORD_Y(rPt.Y()); + aVertices[j] = GLfloat(rPt.X()); + aVertices[j+1] = GLfloat(rPt.Y()); } + ApplyProgramMatrices(); mpProgram->SetVertices( &aVertices[0] ); glDrawArrays( GL_TRIANGLE_FAN, 0, nPoints ); @@ -750,10 +750,11 @@ void OpenGLSalGraphicsImpl::DrawTrapezoid( const basegfx::B2DTrapezoid& trapezoi for( i = 0, j = 0; i < nPoints; i++, j += 2 ) { const basegfx::B2DPoint& rPt = rPolygon.getB2DPoint( i ); - aVertices[j] = OPENGL_COORD_X(rPt.getX()); - aVertices[j+1] = OPENGL_COORD_Y(rPt.getY()); + aVertices[j] = GLfloat(rPt.getX()); + aVertices[j+1] = GLfloat(rPt.getY()); } + ApplyProgramMatrices(); mpProgram->SetVertices( &aVertices[0] ); glDrawArrays( GL_TRIANGLE_FAN, 0, nPoints ); @@ -850,8 +851,8 @@ void OpenGLSalGraphicsImpl::DrawRegionBand( const RegionBand& rRegion ) return; #define ADD_VERTICE(pt) \ - aVertices.push_back(OPENGL_COORD_X(pt.X())); \ - aVertices.push_back(OPENGL_COORD_Y(pt.Y())); + aVertices.push_back(GLfloat(pt.X())); \ + aVertices.push_back(GLfloat(pt.Y())); for( size_t i = 0; i < aRects.size(); ++i ) { @@ -866,6 +867,7 @@ void OpenGLSalGraphicsImpl::DrawRegionBand( const RegionBand& rRegion ) } #undef ADD_VERTICE + ApplyProgramMatrices(); mpProgram->SetVertices( &aVertices[0] ); glDrawArrays( GL_TRIANGLES, 0, aVertices.size() / 2 ); @@ -968,6 +970,7 @@ void OpenGLSalGraphicsImpl::DrawTransformedTexture( } } + ApplyProgramMatrices(); mpProgram->SetUniform2f( "viewport", GetWidth(), GetHeight() ); mpProgram->SetTransform( "transform", rTexture, rNull, rX, rY ); rTexture.GetWholeCoord( aTexCoord ); @@ -1188,10 +1191,10 @@ void OpenGLSalGraphicsImpl::drawRect( long nX, long nY, long nWidth, long nHeigh if( UseSolid( mnLineColor ) ) { - GLfloat fX1 = OPENGL_COORD_X(nX); - GLfloat fY1 = OPENGL_COORD_Y(nY); - GLfloat fX2 = OPENGL_COORD_X(nX + nWidth - 1); - GLfloat fY2 = OPENGL_COORD_Y(nY + nHeight - 1); + GLfloat fX1(nX); + GLfloat fY1(nY); + GLfloat fX2(nX + nWidth - 1); + GLfloat fY2(nY + nHeight - 1); GLfloat pPoints[16]; @@ -1204,6 +1207,7 @@ void OpenGLSalGraphicsImpl::drawRect( long nX, long nY, long nWidth, long nHeigh pPoints[6] = fX1; pPoints[7] = fY2; + ApplyProgramMatrices(0.5f); mpProgram->SetVertices(pPoints); glDrawArrays(GL_LINE_LOOP, 0, 4); } @@ -1824,7 +1828,4 @@ void OpenGLSalGraphicsImpl::endPaint() CHECK_GL_ERROR(); } -#undef OPENGL_COORD_X -#undef OPENGL_COORD_Y - /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/opengl/program.cxx b/vcl/opengl/program.cxx index d40aa30..157243b 100644 --- a/vcl/opengl/program.cxx +++ b/vcl/opengl/program.cxx @@ -13,6 +13,7 @@ #include <glm/glm.hpp> #include <glm/gtc/type_ptr.hpp> +#include <glm/gtc/matrix_transform.hpp> OpenGLProgram::OpenGLProgram() : mnId( 0 ), @@ -238,6 +239,19 @@ void OpenGLProgram::SetTransform( glUniformMatrix4fv( nUniform, 1, GL_FALSE, glm::value_ptr( mMatrix ) ); } +void OpenGLProgram::ApplyMatrix(float fWidth, float fHeight, float fPixelOffset) +{ + OString sProjectionMatrix("mvp"); + GLuint nUniform = GetUniformLocation(sProjectionMatrix); + + glm::mat4 mMVP = glm::ortho(0.0f, fWidth, fHeight, 0.0f, 0.0f, 1.0f); + + if (fPixelOffset != 0.0f) + mMVP = glm::translate(mMVP, glm::vec3(fPixelOffset, fPixelOffset, 0.0f)); + + glUniformMatrix4fv(nUniform, 1, GL_FALSE, glm::value_ptr(mMVP)); +} + void OpenGLProgram::SetBlendMode( GLenum nSFactor, GLenum nDFactor ) { glEnable( GL_BLEND ); @@ -247,12 +261,20 @@ void OpenGLProgram::SetBlendMode( GLenum nSFactor, GLenum nDFactor ) bool OpenGLProgram::DrawTexture( OpenGLTexture& rTexture ) { - GLfloat aPosition[8] = { -1, -1, -1, 1, 1, 1, 1, -1 }; - GLfloat aTexCoord[8]; - - if( !rTexture ) + if (!rTexture) return false; + float fWidth = rTexture.GetWidth(); + float fHeight = rTexture.GetHeight(); + + float fMinX = 0.0f; + float fMaxX = fWidth; + float fMinY = 0.0f; + float fMaxY = fHeight; + + GLfloat aPosition[8] = { fMinX, fMaxY, fMinX, fMinY, fMaxX, fMinY, fMaxX, fMaxY }; + GLfloat aTexCoord[8]; + rTexture.GetWholeCoord( aTexCoord ); SetVertices( aPosition ); SetTextureCoord( aTexCoord ); diff --git a/vcl/opengl/scale.cxx b/vcl/opengl/scale.cxx index 852592b..7b86912 100644 --- a/vcl/opengl/scale.cxx +++ b/vcl/opengl/scale.cxx @@ -65,6 +65,7 @@ bool OpenGLSalBitmap::ImplScaleFilter( pProgram->SetTexture( "sampler", maTexture ); nOldFilter = maTexture.GetFilter(); maTexture.SetFilter( nFilter ); + pProgram->ApplyMatrix(mnWidth, mnHeight); pProgram->DrawTexture( maTexture ); maTexture.SetFilter( nOldFilter ); pProgram->Clean(); diff --git a/vcl/opengl/textureVertexShader.glsl b/vcl/opengl/textureVertexShader.glsl index 99d7f37..bb852f9 100644 --- a/vcl/opengl/textureVertexShader.glsl +++ b/vcl/opengl/textureVertexShader.glsl @@ -10,9 +10,10 @@ attribute vec4 position; attribute vec2 tex_coord_in; varying vec2 tex_coord; +uniform mat4 mvp; void main() { - gl_Position = position; + gl_Position = mvp * position; tex_coord = tex_coord_in; } diff --git a/vcl/opengl/transformedTextureVertexShader.glsl b/vcl/opengl/transformedTextureVertexShader.glsl index 51485a0..3a64fd0 100644 --- a/vcl/opengl/transformedTextureVertexShader.glsl +++ b/vcl/opengl/transformedTextureVertexShader.glsl @@ -11,12 +11,11 @@ attribute vec4 position; attribute vec2 tex_coord_in; uniform vec2 viewport; uniform mat4 transform; +uniform mat4 mvp; varying vec2 tex_coord; void main() { - vec4 pos = transform * position; - pos.x = (2.0 * pos.x) / viewport.x - 1.0; - pos.y = 1.0 - (2.0 * pos.y / viewport.y); + vec4 pos = mvp * transform * position; gl_Position = pos; tex_coord = tex_coord_in; } commit a1385d37e9ffc84b82e9ee11e505bba6273823dc Author: Tomaž Vajngerl <[email protected]> Date: Mon Jul 6 20:06:31 2015 +0900 opengl: adjust rect drawing - should be to width (height) - 1 Might be the cause of Rectangle implementation of GetWidth and GetHeight which is different to other programs. X11 backend compensates in the same way for this use case. Change-Id: Ibc3c1d6f442d616c64b602cccb601bfc33fd4baf diff --git a/vcl/opengl/gdiimpl.cxx b/vcl/opengl/gdiimpl.cxx index 012ba9d..e9ad376 100644 --- a/vcl/opengl/gdiimpl.cxx +++ b/vcl/opengl/gdiimpl.cxx @@ -1190,8 +1190,8 @@ void OpenGLSalGraphicsImpl::drawRect( long nX, long nY, long nWidth, long nHeigh { GLfloat fX1 = OPENGL_COORD_X(nX); GLfloat fY1 = OPENGL_COORD_Y(nY); - GLfloat fX2 = OPENGL_COORD_X(nX + nWidth); - GLfloat fY2 = OPENGL_COORD_Y(nY + nHeight); + GLfloat fX2 = OPENGL_COORD_X(nX + nWidth - 1); + GLfloat fY2 = OPENGL_COORD_Y(nY + nHeight - 1); GLfloat pPoints[16]; commit 9125549453e3038eddafc3c89bc830a5e40953b4 Author: Tomaž Vajngerl <[email protected]> Date: Mon Jul 6 18:11:49 2015 +0900 add some unit tests for tools::Rectangle Change-Id: Ic0323864455c248c926e69a0bb91d13adc963eca diff --git a/tools/CppunitTest_tools_test.mk b/tools/CppunitTest_tools_test.mk index 58baeb5..fd8b2b4 100644 --- a/tools/CppunitTest_tools_test.mk +++ b/tools/CppunitTest_tools_test.mk @@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \ tools/qa/cppunit/test_stream \ tools/qa/cppunit/test_urlobj \ tools/qa/cppunit/test_color \ + tools/qa/cppunit/test_rectangle \ )) $(eval $(call gb_CppunitTest_use_api,tools_test, \ diff --git a/tools/qa/cppunit/test_rectangle.cxx b/tools/qa/cppunit/test_rectangle.cxx new file mode 100644 index 0000000..29d04c8 --- /dev/null +++ b/tools/qa/cppunit/test_rectangle.cxx @@ -0,0 +1,60 @@ +/* -*- 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 <sal/types.h> +#include "cppunit/TestAssert.h" +#include "cppunit/TestFixture.h" +#include "cppunit/extensions/HelperMacros.h" +#include "cppunit/plugin/TestPlugIn.h" +#include <tools/gen.hxx> + +namespace +{ + +class Test: public CppUnit::TestFixture +{ +public: + void test_rectangle(); + + CPPUNIT_TEST_SUITE(Test); + CPPUNIT_TEST(test_rectangle); + CPPUNIT_TEST_SUITE_END(); +}; + +void Test::test_rectangle() +{ + { + Rectangle aRect(1,1,1,1); + + CPPUNIT_ASSERT_EQUAL(long(1), aRect.GetWidth()); + CPPUNIT_ASSERT_EQUAL(long(1), aRect.GetHeight()); + + CPPUNIT_ASSERT_EQUAL(long(0), aRect.getWidth()); + CPPUNIT_ASSERT_EQUAL(long(0), aRect.getHeight()); + } + + { + Rectangle aRect(Point(), Size(1,1)); + + CPPUNIT_ASSERT_EQUAL(long(0), aRect.Left()); + CPPUNIT_ASSERT_EQUAL(long(0), aRect.Top()); + CPPUNIT_ASSERT_EQUAL(long(0), aRect.Right()); + CPPUNIT_ASSERT_EQUAL(long(0), aRect.Bottom()); + + CPPUNIT_ASSERT_EQUAL(long(1), aRect.GetWidth()); + CPPUNIT_ASSERT_EQUAL(long(1), aRect.GetHeight()); + } +} + + +CPPUNIT_TEST_SUITE_REGISTRATION(Test); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ commit bbb8c66ed976a1eaddd92400eb31462bed182be4 Author: Tomaž Vajngerl <[email protected]> Date: Mon Jul 6 18:10:00 2015 +0900 opengl: don't forget to call swapBuffers when paint ends Change-Id: I05a2dc1c36d5f23ee43bb799f28fc2403743a4c1 diff --git a/vcl/opengl/gdiimpl.cxx b/vcl/opengl/gdiimpl.cxx index 125ac0e..012ba9d 100644 --- a/vcl/opengl/gdiimpl.cxx +++ b/vcl/opengl/gdiimpl.cxx @@ -1818,6 +1818,7 @@ void OpenGLSalGraphicsImpl::endPaint() mpContext->makeCurrent(); mpContext->AcquireDefaultFramebuffer(); glFlush(); + mpContext->swapBuffers(); } CHECK_GL_ERROR();
_______________________________________________ Libreoffice-commits mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
