Original Post
This is a follow up/alternate version of an issue i posted about a few days ago, but hear me out. I am using a noise function to map a certain "attribute" to parts of my world (lets say it's a height value). As my camera moves around in my world, i want to be able to sample that function, as a post processing effect, at specific intervals (or slices) inside the camera volume, since i am trying to create a volumetric effect. That would be easy if i could sample using the clip-space coordinates of each fragment, however i need to be able to sample using "world" coordinates. Source code for my attempt so far follows. I am using a little "trick" to separate the world matrix from the view matrix, in that i am storing both the Projection and Camera matrices in the GL_PROJECTION matrix stack and saving the GL_MODELVIEW stack for world transformations. I have also loaded the Projection and Eye matrices into GL_Texture_matrix[6] and [7] respectively to have access to them individually if i need them. In my application i render a quad in screenspace like this: And this is my vertex shader This does not work. I am able to sample right infront of the camera correctly, but if i try to sample further inside the "volume" i get "nothing"... I also tried it in another way, throwing up quads in screen-space at various Z-values. But then the conversion to world-space wasnt correct due to the division by w that happens during the projection transformation. Any ideas?
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(1.0,0.0);
glVertex3f(1.0,-1.0,-1.0);
glTexCoord2f(1.0,1.0);
glVertex3f(1.0,1.0,-1.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-1.0,1.0,-1.0);
glEnd();
uniform float samplingDepth;
varying vec3 worldCoordinates;
void main()
{
vec4 oVertex = gl_Vertex;
worldCoordinates = gl_TextureMatrixInverse[6] * gl_TextureMatrixInverse[7] *oVertex; // the vertex is in clip-space, so multiplying by the inverse of the projection and camera matrices brings it to world space
vec3 eye = gl_TextureMatrixInverse[6] * gl_TextureMatrixInverse[7] * vec4(0.0,0.0,0.0,1.0); //same thing for the camera position
vec3 lookAt = lookAt = worldCoordinates - eye;//create the eye->screen vector in world-space
worldCoordinates = worldCoordinates + lookAt * samplingDepth;//extrude along the vector
gl_Position = gl_ModelViewProjectionMatrix * oVertex;
}