Sign in to follow this  
Darragh

GLSL- Mipmaps don't work

Recommended Posts

Darragh    308
Hi everyone, Unfortunately, i've been having problems recently using mipmapping in my game when I use GLSL shaders. The screenshot below illustrates what I am talking about: As you can see, everything bar the column by the wall is mipmapped. The players gun is also not mipmapped too since it uses the same GLSL program to render as the column, but it is too close to see the effects. Oddly, the walls and ceiling / floor remain unaffected by the mipmapping problem- even though they are also rendered using a GLSL shader (for fog). The main difference between the GLSL programs for model rendering and wall rendering is that the model rendering program contains both a vertex (for interpolating keyframes) and fragment program (for fog), the wall rendering program contains only a fragment program to do the fog effect. This leads me to the conclusion that the problem only occurs when using vertex shaders. Further evidence of this lies in another shader- for doing water effects. This shader has both a vertex and fragment program too, and again- no mipmapping occurs. So there must be some part of the fixed functionality pipeline which I am not replacing in my vertex programs.. I've looked at the GLSL spec but unfortunately I couldn't get anything from it which would help my problem. I am setting up my mipmapping correctly using the GLU routines for building mipmaps and then using the texture filter GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR throughout the program. The problem must lie within my vertex shaders.. Below is the both the vertex and fragment code for the model rendering shader: - VERTEX PROGRAM -
uniform   float interpolation;

void main ()
{
// SHADER FOR INTERPOLATING KEYFRAME ANIMATION
// gl_SecondaryColor is the vertex to be interpolated with

gl_Position     = gl_ProjectionMatrix*mix(gl_Vertex,gl_SecondaryColor,interpolation);
gl_FrontColor	= gl_Color;
gl_TexCoord[0]  = gl_MultiTexCoord0;

}

- FRAGMENT PROGRAM -

uniform sampler2D tex;


// FOG SETTINGS

const float red   	= 0.00;
const float green 	= 0.01;
const float blue  	= 0.03;
const float fogEnd     	= 7.2;

uniform int renderFog;

const vec4 fogColor	= vec4(red,green,blue,1.0);

// FRAGMENT FOG AMOUNT

float fog_amount;

void main() 
{

// FOG EQUATION:

if  ( renderFog == 1 )
    {

    fog_amount =  -log(gl_FragCoord.z)*exp(fogEnd);

    }
else
    {
    
    fog_amount = 1.0;

    }

// GET TEXTURE COLOR FROM TEXTURE

vec4 texColor = texture2D(tex,gl_TexCoord[0].xy);

// MULTIPLY TEX COLOR BY VERTEX COLOR
 
texColor = texColor*gl_Color; 

// BLENDING WITH FOG BY CALCULATED AMOUNT:


if  ( renderFog == 1 )
    {

    gl_FragColor = mix(fogColor,texColor,fog_amount);

    }
else
    {

    gl_FragColor.rgb = texColor.rgb;

    }

gl_FragColor.a 	 = gl_Color.a;

}


Any ideas as to how I might solve this problem ? My graphics card is an X800XT and I have fairly recent display drivers installed. Perhaps this is an ATI specific problem ? Thanks in advance for your help, Darragh

Share this post


Link to post
Share on other sites
MARS_999    1627
I am not a pro at GLSL but your gl_Position = mix() I never seen that before and only seen these

gl_Position = ftransform();

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

Share this post


Link to post
Share on other sites
zedzeek    528
Quote:
Unfortunately, i've been having problems recently using mipmapping in my game when I use GLSL shaders. The screenshot below illustrates what I am talking about:

i cant see anything wrong in the picture, describe the problem or draw a circle around it

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Umm, maybe that's shader aliasing? Bad vertexpos from the vertex shader => screwed up fog color. Also maybe the column doesn't like to be animated! Try disabling keyframe interpolation and see what you get for the column. If it looks fine then you need to correct your vertex shader/ send good values for static models.

Share this post


Link to post
Share on other sites
Darragh    308
Hi guys, thanks for replying.

Quote:
Original post by MARS_999
I am not a pro at GLSL but your gl_Position = mix() I never seen that before and only seen these

gl_Position = ftransform();

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;


The mix() function is used to perform linear mixing between two sets of data. I use it to perform the interpolation on my models animation. I know its confusing, but I use gl_Secondary color as the next vertex which is to be mixed with the current vertex (gl_Vertex).


Quote:
zedzeek
i cant see anything wrong in the picture, describe the problem or draw a circle around it


The problem is on the column model- the thing at which the gun is pointing. Its difficult to spot on a static picture ( the problem becomes obvious once you see it moving ) but if you look very closely you will notice the 'grainy' look that occurs on objects that are in the distance when no mipmapping is used. Look at the wall and the floor around the column, both are nicely mipmapped and smooth, whereas the column itself is very rough looking.

Quote:
Anonymous Poster
Umm, maybe that's shader aliasing? Bad vertexpos from the vertex shader => screwed up fog color.


Nope. The vertex position does not affect how the fog is generated. If you look at the fragment shader for the model rendering program you will see that I am generating the fog amount based on the fragments z-coordinate. Even if the fog was messed up, the underlying texture should still be smooth if it was mipmapped. I've tried to turn fog off to see what effect it would have on the problem but unfortunately it had none.

Quote:
Anonymous Poster
Also maybe the column doesn't like to be animated! Try disabling keyframe interpolation and see what you get for the column. If it looks fine then you need to correct your vertex shader/ send good values for static models.[/


Wouldn't be that either. Instead of sending no values to the vertex program ( I'm using VBOs so I must provide proper pointers anyways ) I simply send the same frame twice for static models. Its slightly wasteful I know and I'll create a separate rendering system for static models later- but at least there is no funny values being passed into the shader. Animated models also suffer from the same problem, along with my water surfaces.

Still no luck however in solving this thing. If anyone else has any suggestions / ideas then fire away..

Thanks,
Darragh


Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Also you don't use the modelview matrix. Is this also intentional?
Should it not be

gl_Position = gl_ModelViewProjectionMatrix*mix(gl_Vertex,gl_SecondaryColor,interpolation);

the fragcoord.z for a fragment of a triangle depends on the the three vertexpos of the triangle and the lod of the texture selected also depends on the vertexpos. Its really not clear from the image of the column, what are the artifacts and what's part of the texture. You should probably render them using fixed function see how it looks and then proceed to eliminate. And use some debug textures, which could easily show the problem.

Share this post


Link to post
Share on other sites
Darragh    308
Quote:
Original post by Anonymous Poster
Also you don't use the modelview matrix. Is this also intentional?
Should it not be

gl_Position = gl_ModelViewProjectionMatrix*mix(gl_Vertex,gl_SecondaryColor,interpolation);


Not really intentional per se, just a throwback from when I started this project. See, when I first began this I knew nothing of OpenGL, or indeed- game programming. I've since learned that using the Projection Matrix in this way is a BAD idea, but because the code still worked fine- i didn't feel the need to change it.

Quote:
the fragcoord.z for a fragment of a triangle depends on the the three vertexpos of the triangle


True. I supposed I wasn't 100% accurate in saying that the vertices didn't affect it.

Quote:
the lod of the texture selected also depends on the vertexpos.


Perhaps this has something do with the use of the projection matrix.. Though the vertex position ends up in the correct place, so it should work- in theory.

I'll try using the projection matrix ONLY to set up the perspective view, clip planes etc.. and use the modelview for world / model transformations instead. Maybe this may help the shader determine the correct LOD.

Quote:
Its really not clear from the image of the column, what are the artifacts and what's part of the texture.


As I said, its difficult to spot in a static image- but trust me, its there alright. When you see it moving you will notice the abrupt changes in pixel color that occur when you attempt to use a big texture in a small amount of screen space, without mipmapping enabled.

Quote:
You should probably render them using fixed function see how it looks and then proceed to eliminate. And use some debug textures, which could easily show the problem.


I think I will give that a shot. But first I'll try changing my use of the OpenGL matrices and see what difference it makes.

BTW, does anyone have any links that shows what equations OpenGL uses to determine mipmap LODS ? If I can't solve this problem then perhaps I could implement an LOD scheme manually in the fragment shader. Maybe I could get things working that way..

Thanks for your help,
Darragh

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