Hi! I’m trying to convert world coordinates to window coordinates in my shader but I’ve not the same values than when I do this with the cpu.
When I convert coords with the cpu the light attenuation is doing well :
math::Vec3f center = getWindow().mapCoordsToPixel(el->getCenter() - el->getSize()*0.5f, view);
math::Vec3f RenderTarget::mapCoordsToPixel(const math::Vec3f& point, View& view) {
ViewportMatrix vpm;
vpm.setViewport(math::Vec3f(view.getViewport().getPosition().x, view.getViewport().getPosition().y, 0),
math::Vec3f(view.getViewport().getWidth(), view.getViewport().getHeight(), 1));
math::Vec3f coords = view.getViewMatrix().transform(point);
coords = view.getProjMatrix().project(coords);
if (coords.w == 0) {
coords.w = view.getSize().z * 0.5;
}
coords = coords.normalizeToVec3();
coords = vpm.toViewportCoordinates(coords);
return coords;
}
But when I want to convert coordinates with the shader they are wrong. I do the same maths but I’ve not the same values.
R"(#version 460
layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoords;
layout (location = 3) in vec3 normals;
layout (location = 6) in uint l;
layout (location = 8) in vec4 lightP;
layout (location = 9) in vec4 lightC;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 viewportMatrix;
uniform mat4 textureMatrix;
uniform vec3 resolution;
out vec2 fTexCoords;
out vec4 frontColor;
out uint layer;
out vec4 lightPos;
out vec4 lightColor;
void main() {
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.f);
fTexCoords = (textureMatrix * vec4(texCoords, 1.f, 1.f)).xy;
frontColor = color;
layer = l;
vec4 coords = lightP;
coords = projectionMatrix * viewMatrix * coords;
if (coords.w == 0)
coords.w = resolution.z * 0.5;
coords = coords / coords.w;
coords = viewportMatrix * coords;
coords.w = lightP.w;
lightPos = coords;
lightColor = lightC;
}
)";
The convert coodinates of the light with the cpu are 350,280,0.51 with is correct but with my shader the coordinates are differents and they are 400, 298, 0.51 which is wrong.
Why I don’t have different results in the cpu and in my shader with the same maths ?
That’s not normal.
PS : the only difference is I transpose the matrices before I pass them to my shader.