PDA

View Full Version : GLSL Translation Issue



Wasabi
04-06-2011, 09:37 AM
I have the following GLSL vertex shader code:

#version 330

struct Matrix
{
vec4 row1;
vec4 row2;
vec4 row3;
vec4 row4;
};

uniform Matrix ModelviewProjection;
in vec3 in_Position;
in vec3 in_Color;
out vec3 ex_Color;

void main(void)
{
//gl_Position = vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
vec4 Position = vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
vec4 x,y,z,w;
x = ModelviewProjection.row1*Position;
y = ModelviewProjection.row2*Position;
z = ModelviewProjection.row3*Position;
gl_Position.x = x.x+x.y+x.z;
gl_Position.y = y.x+y.y+y.z;
gl_Position.z = z.x+z.y+z.z;
gl_Position.w = 1;
ex_Color = in_Color;
}

At first, the uniform is defined as an identity matrix, i.e. gl_Position=in_Position. If I "push" (using my own function) a rotation matrix onto the uniform, the image shown on screen is rotated. However, if I add any translation data, it is ignored.

That is, if I have the uniform at its default identity value and push the following matrix at the Modelview part:

[cos(30),-sin(30),0,1,
sin(30),cos(30),0,0,
0,0,1,0
0,0,0,1]
The end product of Projection*Modelview*Rotation=Rotation, since the others are identity matricies.

Now, if I update the uniform to its new value (=Rotation) and redraw the screen, the image appears rotated by 30 degrees, as it should, however, the x-translation is ignored. If instead the matrix is:


[1,0,0,1,
0,1,0,0,
0,0,1,0,
0,0,0,1]
The matrix might as well be an identity, since nothing happens.

Am I doing something wrong?

Alfonse Reinheart
04-06-2011, 10:10 AM
vec4 x,y,z,w;
x = ModelviewProjection.row1*Position;
y = ModelviewProjection.row2*Position;
z = ModelviewProjection.row3*Position;

Are you actually manually multiplying the matrix by the position? That's really unnecessary. Just let GLSL do it for you and use the * operator. mat4 * vec4 is a transformation of the vector by the matrix.

Also, your matrix is a 4D matrix. Your position should likewise be a 4D position (typically, W=1). And therefore you get a 4D output value.

Wasabi
04-06-2011, 10:38 AM
Ah, didn't know GLSL had a matrix-vector operator. Good to know.

As well, the position is 4D. It is imported as a 3D value (in_position), but all the work is done on "vec4 Position", which is defined by in_position and w=1.

enjoycrf
04-07-2011, 12:33 AM
thats deep, iono any maff