PDA

View Full Version : texture projection using GLSL



Dragon89
02-17-2007, 08:59 PM
how would I project a fullscreen texture onto a horizontal plane using GLSL?

PfhorSlayer
02-18-2007, 04:05 AM
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. :\

Dragon89
02-18-2007, 04:53 AM
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;

zeoverlord
02-18-2007, 10:18 AM
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.

Dragon89
02-18-2007, 10:29 AM
Originally posted by zeoverlord:
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. i didnt quite understand that part... is it wrong to use gl_ModelViewProjectionMatrix in my case?

Dragon89
02-18-2007, 11:08 AM
Originally posted by zeoverlord:
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. and also whats that texturematrix thing? is that necessary?

Dragon89
02-18-2007, 11:29 AM
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

zeoverlord
02-18-2007, 02:20 PM
Originally posted by Dragon89:

Originally posted by zeoverlord:
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. i didnt quite understand that part... is it wrong to use gl_ModelViewProjectionMatrix in my case? 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.

Dragon89
02-18-2007, 02:39 PM
thx alot.. it works great now...