Kaydet (Commit) f6b9ba33 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl Kaydeden (comit) Caolán McNamara

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
(cherry picked from commit 2e99e4e1)
Reviewed-on: https://gerrit.libreoffice.org/16919Reviewed-by: 's avatarCaolán McNamara <caolanm@redhat.com>
Tested-by: 's avatarCaolán McNamara <caolanm@redhat.com>
üst 94281392
......@@ -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:
......
......@@ -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 );
......
......@@ -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;
}
......
......@@ -8,8 +8,10 @@
*/
attribute vec4 position;
uniform mat4 mvp;
void main() {
gl_Position = position;
gl_Position = mvp * position;
}
......
......@@ -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 );
......@@ -672,10 +670,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 );
......@@ -714,10 +713,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 );
......@@ -757,10 +757,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 );
......@@ -857,8 +858,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 )
{
......@@ -873,6 +874,7 @@ void OpenGLSalGraphicsImpl::DrawRegionBand( const RegionBand& rRegion )
}
#undef ADD_VERTICE
ApplyProgramMatrices();
mpProgram->SetVertices( &aVertices[0] );
glDrawArrays( GL_TRIANGLES, 0, aVertices.size() / 2 );
......@@ -975,6 +977,7 @@ void OpenGLSalGraphicsImpl::DrawTransformedTexture(
}
}
ApplyProgramMatrices();
mpProgram->SetUniform2f( "viewport", GetWidth(), GetHeight() );
mpProgram->SetTransform( "transform", rTexture, rNull, rX, rY );
rTexture.GetWholeCoord( aTexCoord );
......@@ -1195,10 +1198,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];
......@@ -1211,6 +1214,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);
}
......@@ -1831,7 +1835,4 @@ void OpenGLSalGraphicsImpl::endPaint()
CHECK_GL_ERROR();
}
#undef OPENGL_COORD_X
#undef OPENGL_COORD_Y
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -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 );
......
......@@ -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();
......
......@@ -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;
}
......
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment