GLSL & Mipmaps

Started by
10 comments, last by Deliverance 13 years, 2 months ago
I'm trying to access mipmaps with GLSL but i have no luck. After searching the internet i have found texture2DLod. I use it like so:


gl_FragColor = (1.0 - texture2DLod(heightMapID, uv, 5));

but this has the same result as putting any other parameter instead of 5. Isn't that the mipmap level? where 0 si the base level? What i am missing here?

P.S. I do generate mipmaps for the image and upload each mipmap with glTexImage2D. No errors occur during this setup(i check this with glGetError()).
Advertisement
[s]You should create the texture once with glTexImage2D, specifying the total number of mip-levels, and then use glTexSubImage2D to upload the different levels.[/s]
EDIT: Incorrect.

You should create the texture once with glTexImage2D, specifying the total number of mip-levels, and then use glTexSubImage2D to upload the different levels.


Thanks for the quick reply!

Ok, so i've done things like this:



glGenTextures(1, &texID);

glBindTexture(GL_TEXTURE_2D, texID);

int i;
for (i=0; i<image->getNumMipmaps(); i++)
{
if (i==0)
glTexImage2D(GL_TEXTURE_2D, image->getNumMipmaps(),
bytesPerPixel, width >> i,
height >> i, 0, bytesPerPixel==1 ? GL_LUMINANCE : (bytesPerPixel==3 ? GL_RGB : (bytesPerPixel==1 ? GL_LUMINANCE : GL_RGBA)),
GL_UNSIGNED_BYTE, image->getData(i));
else
glTexSubImage2D(GL_TEXTURE_2D, i, 0, 0, width >> i,
height >> i, bytesPerPixel==1 ? GL_LUMINANCE : (bytesPerPixel==3 ? GL_RGB : (bytesPerPixel==1 ? GL_LUMINANCE : GL_RGBA)),
GL_UNSIGNED_BYTE, image->getData(i));

printf("error: %d\n", glGetErrro());
}



But i receive GL_INVALID_ENUM after each iteration. Now i have cut off the code and boiled it down to just



gluBuild2DMipmaps(GL_TEXTURE_2D, bytesPerPixel, width, height, bytesPerPixel==1 ? GL_LUMINANCE : (bytesPerPixel==3 ? GL_RGB : (bytesPerPixel==1 ? GL_LUMINANCE : GL_RGBA)),
GL_UNSIGNED_BYTE, image->getData(0));


The errors are gone(also this can't be the final version, because i need my own mipmaps for pydamidal displacement mapping). Now i stiil can't access the mipmaps in the fragment shader. Whatever i plug into texture2DLod as the level parameter i get the same result. Here's the code:


gl_FragColor = texture2DLod(heightMapID, uv, 6.0);


What might be wrong?
I think I have given you false information, apologies, it's correct to use glTexImage2D for each mip-level.

As for your shader not working, do you enable mip-maps wih glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)?

gluBuild2DMipMaps calls glTexImage2D internally, so it should definitely correctly set up all the mip-levels.

Also, what GL/GLSL version are you using?
Try textureLod instead of texture2DLod if it's available.
texture2DLod is for the vertex shader according to glsl 1.10
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);
Ok, thanks for the replies!

My OpenGL version is 3.2 and GLSL version is 1.5.

I can compile a shader that uses texturelod but no visual differences occur withing the textured rectangle that i'm using for testing.

Also, here's my complete setup now:



glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, bytesPerPixel, width, height, bytesPerPixel==1 ? GL_LUMINANCE : (bytesPerPixel==3 ? GL_RGB : (bytesPerPixel==1 ? GL_LUMINANCE : GL_RGBA)),
GL_UNSIGNED_BYTE, image->getData(0));

printf("error=%d - %d\n", glGetError(), GL_INVALID_VALUE);
Is the texture in GLSL a sampler2D?
Also, what texture do you test with?
Try an RGBA image where you can clearly see the difference if another mip-level is used. GL_LUMINANCE is deprecated, but it should still work..
Yes, the texture is a sampler2D. I"m using a RGB texture which is in fact a grayscale image, every component of the three is equal to the same value. The output i'm expecting is a blurred/pixelated image , because i'm selecting a mipmap of 6, out out of 9 total.
Show your shader code. It works fine for me to use textureLod like that together with gluBuild2DMipmaps, as well as when using glGenerateMips after specifying mip-level 0 with glTexImage2d instead, at GL 3.3.
I tested the following minimal fragment shader:

#version 330 core

uniform sampler2D tex0;
in vec2 varTexCoord;
out vec4 outColor;

void main() {
outColor = textureLod(tex0, varTexCoord, 2.0f);
}

Show your shader code. It works fine for me to use textureLod like that together with gluBuild2DMipmaps, as well as when using glGenerateMips after specifying mip-level 0 with glTexImage2d instead, at GL 3.3.
I tested the following minimal fragment shader:

#version 330 core

uniform sampler2D tex0;
in vec2 varTexCoord;
out vec4 outColor;

void main() {
outColor = textureLod(tex0, varTexCoord, 2.0f);
}



Here it is:



varying vec2 texCoords;
uniform sampler2D heightMapID;

void main()
{
gl_FragColor = textureLod(heightMapID, texCoords, 6.0);
}

This topic is closed to new replies.

Advertisement