Sign in to follow this  
Black Knight

glsl projective shader

Recommended Posts

Im trying to use projective texturing with glsl but I have a problem. Here is the screenshot screenshot As you can see there are black parts in the water they are over the edges of the water mesh. I draw the water with vertex arrays and use triangle strips. Here are the shaders. //Vertex shader varying vec4 ProjTexCoord; void main() { mat4 MR; gl_Position = ftransform(); gl_TexCoord[0] = gl_MultiTexCoord0; MR=mat4(0.5,0.0,0.0,0.0, 0.0,0.5,0.0,0.0, 0.0,0.0,0.5,0.0, 0.5,0.5,0.5,1.0); ProjTexCoord=(MR*gl_ModelViewProjectionMatrix)*gl_Vertex; } //fragment shader uniform sampler2D tex0; uniform sampler2D tex1; varying vec4 ProjTexCoord; void main() { vec4 color; vec4 colProj; color = texture2D(tex0, gl_TexCoord[0].xy); colProj = texture2DProj(tex1, ProjTexCoord); color = color * colProj; gl_FragColor = color; }

Share this post


Link to post
Share on other sites
This is how i draw the water surface.
The RTT texture is in unit 0 and it works this way.

glPushAttrib(GL_ENABLE_BIT);
glVertexPointer(3,GL_FLOAT,0,m_pVertices);

//glClientActiveTextureARB(GL_TEXTURE0_ARB);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glTexCoordPointer(2,GL_FLOAT,0,m_pTexCoords);

glClientActiveTextureARB(GL_TEXTURE1_ARB);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2,GL_FLOAT,0,m_pTexCoords);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

//If we are over the water surface draw the water with projective texturing
if(pCamera->getPosition().y>m_vPosition.y){

if(m_bDrawReflection){
glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
}
glDisable(GL_LIGHTING);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D,m_iTextureName[(int)m_fCurrentTexture]);

if(m_bDrawReflection){
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,m_iRTextureName);
}

GLfloat remapMatrix[] = {0.5,0.0,0.0,0.0,
0.0,0.5,0.0,0.0,
0.0,0.0,0.5,0.0,
0.5,0.5,0.5,1.0};

glActiveTextureARB(GL_TEXTURE0_ARB);
float Mproj[16],Mview[16];
glGetFloatv(GL_MODELVIEW_MATRIX,Mview);
glGetFloatv(GL_PROJECTION_MATRIX,Mproj);
glMatrixMode(GL_TEXTURE);
{
glLoadMatrixf(remapMatrix);
glMultMatrixf(Mproj);
glMultMatrixf(Mview);
}
glMatrixMode(GL_MODELVIEW);

int count = m_iSize<<1;
for(int i=0;i<m_iSize-1;i++)
glDrawElements(GL_TRIANGLE_STRIP,count,GL_UNSIGNED_INT,&m_pIndices[(i*(m_iSize))<<1]);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glActiveTextureARB(GL_TEXTURE0_ARB);


I active the shader before i draw the water :

m_PerPixel.use(ST_TRUE);
//set shader tex
m_PerPixel.setInt(m_PerPixel.getVariable("tex0"),1);
m_PerPixel.setInt(m_PerPixel.getVariable("tex1"),0);
if(m_pWater)
//m_pWater->draw(pCamera);
m_pWater->drawShaders(pCamera);
m_PerPixel.use(ST_FALSE);


drawShaders is the function above.

And these are the working shaders

varying vec4 ProjTexCoord;
void main()
{
gl_Position = ftransform();
gl_TexCoord[1] = gl_MultiTexCoord1;
ProjTexCoord = gl_TextureMatrix[0] * gl_Vertex;
gl_FrontColor = vec4(1.0,1.0,1.0,1.0);
}


uniform sampler2D tex0;
uniform sampler2D tex1;
varying vec4 ProjTexCoord;
const vec4 ambient = vec4(0.13), boost = vec4(1.06);
void main()
{
vec4 color = texture2D(tex0, gl_TexCoord[1].st);
vec4 reflectionValue = texture2DProj(tex1, ProjTexCoord);


gl_FragColor = boost * color * reflectionValue + ambient;

}


But if I change the reflection from texture unit 0 to 1 and do all changes.I get the Image in screenshot.Black lines over the water surface other than that everything looks normal

[Edited by - phantom on November 6, 2005 7:34:10 PM]

Share this post


Link to post
Share on other sites
Thnx for the formatting :P
Hmmm does calling texture2d() affect the other texture2dProj() in the fragment shader??I guess it shouldn't but yesterday when I commented it out the output was different.Maybe it was just my dead eyes I dunno...

Share this post


Link to post
Share on other sites
Ok not it works in texture unit 1 too but I had to remove my varying variable for that and use the gl_TexCoord[1] like this


varying vec4 ProjTexCoord;
void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_TextureMatrix[1] * gl_Vertex;
//ProjTexCoord = gl_TextureMatrix[1] * gl_Vertex;;


gl_FrontColor = vec4(1.0,1.0,1.0,1.0);
}

uniform sampler2D base;
uniform sampler2D reflection;
varying vec4 ProjTexCoord;
const vec4 ambient = vec4(0.13), boost = vec4(1.06);
void main()
{
vec4 color = vec4(texture2D(base, gl_TexCoord[0].st));
vec4 reflectionValue = vec4(texture2DProj(reflection, gl_TexCoord[1]));

gl_FragColor = boost * reflectionValue + ambient;

}




Instead of gl_TexCoord[1] if I use ProjTexCoord it has those black artifacts in the screen shot O_O

Share this post


Link to post
Share on other sites
Hi Again :|
I want to disturb the reflections in the fragment shader by using another texture.
Here are the shaders :

varying vec4 ProjTexCoord;
void main()
{
gl_Position = ftransform();

gl_TexCoord[1] = gl_TextureMatrix[1] * gl_Vertex;
ProjTexCoord = gl_TextureMatrix[1] * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;


gl_FrontColor = gl_Color;
}

varying vec4 ProjTexCoord;
uniform sampler2D base;
uniform sampler2D reflection;

const vec4 ambient = vec4(0.13), boost = vec4(1.06);
void main()
{
vec4 color = vec4(texture2D(base, gl_TexCoord[0].st));


vec4 dist = vec4(0.0,0.0,0.0,0.0);
dist.x = color.x * 20.0 + gl_TexCoord[1].s;
dist.y = gl_TexCoord[1].t;
dist.z = color.z * 20.0 +gl_TexCoord[1].r;
dist.w = gl_TexCoord[1].q;

//vec4 reflectionValue = vec4(texture2DProj(reflection, gl_TexCoord[1]));
vec4 reflectionValue = vec4(texture2DProj(reflection, dist));

gl_FragColor = boost * reflectionValue + ambient;

gl_FragColor.a = gl_Color.a;
}



And here is how it looks like :(
Screen Shot

If a remove the *20.0 then the black artifacts go away but the reflections is very calm.Im sure color is between 0.0-1.0.But if I add too much to dist.x the reflections get black artifacts.
Any help?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this