Lolilolight

03-30-2015, 01:54 AM

Hi!

In modern opengl we have to pass each matrix to a shader, I use a texture matrix for passing from [0,size] to [0,1] for uv coordinates.

It works fine except I can't flip a texture, when I try to invert the y coordinates with this matrix :

math::Matrix4f Texture::getTextureMatrix() const {

math::Matrix4f matrix(1.f, 0.f, 0.f, 0.f,

0.f, 1.f, 0.f, 0.f,

0.f, 0.f, 1.f, 0.f,

0.f, 0.f, 0.f, 1.f);

matrix.m11 = 1.f / m_actualSize.x;

matrix.m22 = 1.f / m_actualSize.y;

if (m_pixelsFlipped)

{

matrix.m22 = -matrix.m22;

}

return matrix;

}

Here is how I do in the vertex shader :

#version 330 core

layout (location = 0) in vec4 position;

layout (location = 1) in vec4 color;

layout (location = 2) in vec2 uv;

layout (location = 3) in vec3 normals;

uniform mat4 p;

uniform mat4 v;

uniform mat4 t;

uniform float haveTexture;

out vec4 f_color;

out vec2 texCoords;

void main() {

gl_Position = p * v * position;

texCoords = (haveTexture == 1) ? (t * vec4(uv.xy, 0, 0)).xy : vec2(0, 0);

f_color = color;

}

If I don't invert the v coordinate, the texture is well displayed but if I invert the v coordinates, the texture is not displayed. :/

In modern opengl we have to pass each matrix to a shader, I use a texture matrix for passing from [0,size] to [0,1] for uv coordinates.

It works fine except I can't flip a texture, when I try to invert the y coordinates with this matrix :

math::Matrix4f Texture::getTextureMatrix() const {

math::Matrix4f matrix(1.f, 0.f, 0.f, 0.f,

0.f, 1.f, 0.f, 0.f,

0.f, 0.f, 1.f, 0.f,

0.f, 0.f, 0.f, 1.f);

matrix.m11 = 1.f / m_actualSize.x;

matrix.m22 = 1.f / m_actualSize.y;

if (m_pixelsFlipped)

{

matrix.m22 = -matrix.m22;

}

return matrix;

}

Here is how I do in the vertex shader :

#version 330 core

layout (location = 0) in vec4 position;

layout (location = 1) in vec4 color;

layout (location = 2) in vec2 uv;

layout (location = 3) in vec3 normals;

uniform mat4 p;

uniform mat4 v;

uniform mat4 t;

uniform float haveTexture;

out vec4 f_color;

out vec2 texCoords;

void main() {

gl_Position = p * v * position;

texCoords = (haveTexture == 1) ? (t * vec4(uv.xy, 0, 0)).xy : vec2(0, 0);

f_color = color;

}

If I don't invert the v coordinate, the texture is well displayed but if I invert the v coordinates, the texture is not displayed. :/