texture projection using GLSL

how would I project a fullscreen texture onto a horizontal plane using GLSL?

Use the vertex shader to calculate the projected texture coordinates, and sample the texture using those coordinates in the fragment shader?

I’m not really sure what you’re asking, so unless you elaborate a bit you’re not going to get much else out of me. :\

im trying to render some wate rin a terrain scene… .ive rendered the underwater terrain to a FBO texture… i want to project this FBo texture onto my water plane…

i would appricieate some code how u would do that… cuzz thats what ive been tryin to do without success…

ive read that u need the invertretd viewmatrix and texture space and etc… etc… i was hoping there would be a simple way to do this…

shouldnt this give the texture coordinates?

vec4 ScreenCoords = (gl_ModelViewProjectionMatrix * gl_Vertex)*0.5+0.5;

I use something like this for my shadow map demo

gl_TexCoord[2] = gl_TextureMatrix[0]*gl_Vertex;
gl_TexCoord[2] = gl_TexCoord[2]/gl_TexCoord[2].w;
gl_TexCoord[2]=(gl_TexCoord[2]+ 1.0) * 0.5;

But in case you use the gl_ModelViewProjectionMatrix instead of putting the light projection matrix in the texture matrix, it should be equal to frontal projection.

Originally posted by zeoverlord:
[b] I use something like this for my shadow map demo

gl_TexCoord[2] = gl_TextureMatrix[0]*gl_Vertex;
gl_TexCoord[2] = gl_TexCoord[2]/gl_TexCoord[2].w;
gl_TexCoord[2]=(gl_TexCoord[2]+ 1.0) * 0.5;

But in case you use the gl_ModelViewProjectionMatrix instead of putting the light projection matrix in the texture matrix, it should be equal to frontal projection. [/b]
i didnt quite understand that part… is it wrong to use gl_ModelViewProjectionMatrix in my case?

Originally posted by zeoverlord:
[b] I use something like this for my shadow map demo

gl_TexCoord[2] = gl_TextureMatrix[0]*gl_Vertex;
gl_TexCoord[2] = gl_TexCoord[2]/gl_TexCoord[2].w;
gl_TexCoord[2]=(gl_TexCoord[2]+ 1.0) * 0.5;

But in case you use the gl_ModelViewProjectionMatrix instead of putting the light projection matrix in the texture matrix, it should be equal to frontal projection. [/b]
and also whats that texturematrix thing? is that necessary?

ok ive rewritten it after a tutorial… but i stil ldont get the right result… for some reason gl_TexCoord[1].w is always less than 0

		Matrix4x4f offset( 0.5f,	0,	0,	0,
								0,	0.5f,	0,	0, 
								0,		0,	0,	0,
								0.5f,0.5f, 0.0f, 1 );


		Matrix4x4f projection;
		Matrix4x4f view;

		glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)&projection.m_matrix);
		glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&view.m_matrix);

		Matrix4x4f Result = offset * projection * view;
		Result.transpose();

		glMatrixMode(GL_TEXTURE);
		glLoadMatrixf((GLfloat*) &Result.m_matrix);
		glMatrixMode(GL_MODELVIEW);

		Matrix4x4f inv_view = view.invertMatrix(view);

		Terrain->WaterShader.sendUniform("ViewInv", inv_view.m_matrix, false, 16);

// VERTEX SHADER

gl_Position = gl_ModelViewMatrix * gl_Vertex;	

vec4 pos_global = ViewInv * gl_Position;
gl_TexCoord[1] = gl_TextureMatrix[0] * pos_global;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;	

// FRAGMENT SHADER

vec3 RefractColor = vec3(1.0, 0.0, 0.0);
if (gl_TexCoord[1].w > 0.0)
	RefractColor = texture2DProj(RefractMap, gl_TexCoord[0]).rgb;

the matrix class is from here…

http://www.codesampler.com/source_users/oglu_projtexture.zip

Originally posted by Dragon89:
[b] [quote]Originally posted by zeoverlord:
[b] I use something like this for my shadow map demo

gl_TexCoord[2] = gl_TextureMatrix[0]*gl_Vertex;
gl_TexCoord[2] = gl_TexCoord[2]/gl_TexCoord[2].w;
gl_TexCoord[2]=(gl_TexCoord[2]+ 1.0) * 0.5;

But in case you use the gl_ModelViewProjectionMatrix instead of putting the light projection matrix in the texture matrix, it should be equal to frontal projection. [/b]
i didnt quite understand that part… is it wrong to use gl_ModelViewProjectionMatrix in my case? [/b][/QUOTE]No in my case i set the texture matrix, but since you only need frontal projection the texture matrix would be the the same as the gl_ModelViewProjectionMatrix

and since you don’t need the bias transformation this would be the code you need.

gl_TexCoord[2] = gl_ModelViewProjectionMatrix*gl_Vertex;
gl_TexCoord[2] = gl_TexCoord[2]/gl_TexCoord[2].w;

place the first line in the vertex shader and the other one in the fragment shader, i have tested it and it works without any other code.

thx alot… it works great now…