GL_TEXTURE_GEN and fragment shaders.

Started by
6 comments, last by V-man 15 years, 8 months ago
Hey all, I'm starting to use fragment shaders now... and I just have a few questions. Firstly, I think I've run into a problem when combining fragment shaders with texture generation. 1) Is texture generation done in the vertex stage or fragment stage? If it's done in the vertex stage (and I don't have any vertex shaders), does this mean that I can still access the post-generated texels? 2) If so, how? I tried texture2D(shadowTexture,gl_TexCoord[0].st), but that's not giving me expected results. (and yes, I have forwarded the valid texture unit ID to the shader.) 3) When I specify an alpha value for gl_FragColor.. will that alpha value be further used to blend the pixels at all? Or can I do all of that in the fragment shader, and then output whatever alpha value I want? The reason being, I want my fragment shader to output specific alpha values to the framebuffer directly, without altering any of the RGB values.. I need to use these values later, you see. That's all for now :p Thanks, Ben.
--------------------------Check out my free, top-down multiplayer shooter: Subvein
Advertisement
Ahh, needed to use shadowTexture,gl_TexCoord[1].st (I guess because my shadow casing was in texture unit 1.

So scrap the first 2 questions.. still curious about number 3 though :)
--------------------------Check out my free, top-down multiplayer shooter: Subvein
After the fragment shader stage, there is the blend stage, so this stage will use the alpha that you give to gl_FragColor.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
hmm.. and we don't have access to write shaders for this blend stage yet, do we?

Is there someway I can just bypass it? perhaps by setting the BlendFunc to GL_ONE?
--------------------------Check out my free, top-down multiplayer shooter: Subvein
Just disable blending glDisable(GL_BLEND)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Yeah, I tried that but it didn't work. Here's where I'm using the shader.

glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glUseProgramObjectARB(images.shadowProgram);
glDisable(GL_BLEND);
map.Draw();
glEnable(GL_BLEND);
glUseProgramObjectARB(0);

And here is shader itself:

uniform sampler2D baseTexture;
uniform sampler2D shadowTexture;
void main() {
vec4 baseColour = texture2D(baseTexture, gl_TexCoord[0].st);
float shadowIntensity = texture2D(shadowTexture, gl_TexCoord[1].st).a;
gl_FragColor = vec4(gl_Color.rgb*baseColour.rgb*(1.0-shadowIntensity/2.0), shadowIntensity);
}

I know that shadowIntensity in the shader is working correctly, because it's altering the RGB values of the fragment properly. Perhaps I can't assign alpha in the way I've done?

Eitherway, one would expect that after doing all of this, any pixels that the map is shown on should have an alpha value of shadowIntensity.. although they don't.. they all have an intensity of 0.
--------------------------Check out my free, top-down multiplayer shooter: Subvein
My bad.. after doing some more testing.. the alpha set by shadowIntensity WAS coming through.. the problem is that it's coming through in the wrong place. It isn't perpective correct for some reason.

It appears as if I were rendering on a flat 2D plane at z = 0, even though I'm not.

Which is weird, because for the same fragment.. the RGB values will be darkened by this shadowIntensity and then adjusted for perspective, while the alpha values will be set by the shadowIntensity.. but NOT adjusted for perspective.. Does this make any sense at all? Is perspective applied after the fragment shader?

Most confused.
--------------------------Check out my free, top-down multiplayer shooter: Subvein
Perspective correction is applied to the texture coordinates for each fragment processed so texture2D returns a perspective correct result.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement