• 9
• 16
• 15
• 12
• 9

# Projective space in GLSL shader

This topic is 3976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm rendering a landscape, and was hoping the shader code given below would output a smooth gradient from RGB 0,0,0 to RGB 1,1,1 for the landscape, with white to the right of the screen and black to the left of the screen. When I rotate the camera, the gradient should follow the rotation. However what I get is a sharp line somewhere in the middle of the screen, with black to the left and white to the right. When I tried to scale the float x by dividing it by arbitrary values such as 1,000 or 10,000, I get a gradient, but the gradient seems to be different for the far away parts of the landscape (so for instance RGB 10,10,10 is closer to the middle of the screen in the upper part, than in the lower part, where it is further to the left), unless I divide it by projPos.w. I have no idea why the initial code doesn't work. Vertex shader:
varying vec4 projPos;

main() {
gl_Position = ftransform();
projPos = ftransform();
}


varying vec4 projPos;

main() {
float x = (projPos.x + 1.0f) / 2.0f;
gl_FragColor = vec4(x, x, x, 1.0f);
}


Here's how I thought when writing this: in projective space, all coordinates should be in the interval [-1..1]. So if I multiply my position by the modelviewproj matrix, I should get coordinates which should be in this interval. More specifically, the x coordinate in projPos should be exactly the same coordinate the vertex gets on the screen, but in the interval [-1..1]. For my gradient, I need the coordinates to be in the interval [0..1]. So I take the initial coordinates [-1..1] and add 1, to get [0..2], then I divide by 2, to get [0..1]. Is this correct? [Edited by - all_names_taken on May 6, 2007 3:54:21 AM]