kwin: Add GLTexture::matrix()

This method returns a matrix that transforms normalized or un-normalized
texture coordinates, taking the texture target and y-inversion flag into
account.
This commit is contained in:
Fredrik Höglund 2012-09-27 21:22:05 +02:00
parent 38678bb84f
commit 7a99b8c0ef
5 changed files with 54 additions and 1 deletions

View file

@ -376,6 +376,7 @@ bool EglTexture::loadTexture(const Pixmap &pix, const QSize &size, int depth)
checkGLError("load texture");
q->setYInverted(true);
m_size = size;
updateMatrix();
return true;
}

View file

@ -612,6 +612,9 @@ bool GlxTexture::loadTexture(const Pixmap& pix, const QSize& size, int depth)
#ifdef CHECK_GL_ERROR
checkGLError("TextureLoad0");
#endif
updateMatrix();
unbind();
return true;
}

View file

@ -92,6 +92,8 @@ GLTexture::GLTexture(int width, int height)
d->m_size = QSize(width, height);
d->m_canUseMipmaps = true;
d->updateMatrix();
glGenTextures(1, &d->m_texture);
bind();
#ifdef KWIN_HAVE_OPENGLES
@ -204,6 +206,8 @@ bool GLTexture::load(const QImage& image, GLenum target)
d->m_size = img.size();
d->m_yInverted = true;
d->updateMatrix();
img = d->convertToGLFormat(img);
if (isNull()) {
@ -526,6 +530,27 @@ QImage GLTexturePrivate::convertToGLFormat(const QImage& img) const
return res;
}
void GLTexturePrivate::updateMatrix()
{
m_matrix[NormalizedCoordinates].setToIdentity();
m_matrix[UnnormalizedCoordinates].setToIdentity();
#ifndef KWIN_HAVE_OPENGLES
if (m_target == GL_TEXTURE_RECTANGLE_ARB)
m_matrix[NormalizedCoordinates].scale(m_size.width(), m_size.height());
else
#endif
m_matrix[UnnormalizedCoordinates].scale(1.0 / m_size.width(), 1.0 / m_size.height());
if (!m_yInverted) {
m_matrix[NormalizedCoordinates].translate(0.0, 1.0);
m_matrix[NormalizedCoordinates].scale(1.0, -1.0);
m_matrix[UnnormalizedCoordinates].translate(0.0, m_size.height());
m_matrix[UnnormalizedCoordinates].scale(1.0, -1.0);
}
}
bool GLTexture::isYInverted() const
{
Q_D(const GLTexture);
@ -536,6 +561,7 @@ void GLTexture::setYInverted(bool inverted)
{
Q_D(GLTexture);
d->m_yInverted = inverted;
d->updateMatrix();
}
int GLTexture::width() const
@ -550,6 +576,12 @@ int GLTexture::height() const
return d->m_size.height();
}
QMatrix4x4 GLTexture::matrix(TextureCoordinateType type) const
{
Q_D(const GLTexture);
return d->m_matrix[type];
}
bool GLTexture::NPOTTextureSupported()
{
return GLTexturePrivate::sNPOTTextureSupported;

View file

@ -28,6 +28,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QRegion>
#include <QSharedPointer>
#include <QExplicitlySharedDataPointer>
#include <QtGui/QMatrix4x4>
class QImage;
class QPixmap;
@ -41,6 +42,11 @@ namespace KWin
class GLVertexBuffer;
class GLTexturePrivate;
enum TextureCoordinateType {
NormalizedCoordinates = 0,
UnnormalizedCoordinates
};
class KWIN_EXPORT GLTexture
{
public:
@ -67,6 +73,14 @@ public:
**/
void setYInverted(bool inverted);
/**
* Returns a matrix that transforms texture coordinates of the given type,
* taking the texture target and the y-inversion flag into account.
*
* @since 4.11
*/
QMatrix4x4 matrix(TextureCoordinateType type) const;
virtual bool load(const QImage& image, GLenum target = GL_TEXTURE_2D);
virtual bool load(const QPixmap& pixmap, GLenum target = GL_TEXTURE_2D);
virtual bool load(const QString& fileName);

View file

@ -48,13 +48,16 @@ public:
QImage convertToGLFormat(const QImage& img) const;
void updateMatrix();
GLuint m_texture;
GLenum m_target;
GLenum m_filter;
GLenum m_wrapMode;
QSize m_size;
QSizeF m_scale; // to un-normalize GL_TEXTURE_2D
bool m_yInverted; // texture has y inverted
QMatrix4x4 m_matrix[2];
bool m_yInverted; // texture is y-inverted
bool m_canUseMipmaps;
bool m_markedDirty;
bool m_filterChanged;