Sign in to follow this  

GLSL & Mipmaps

This topic is 2490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:


[code]gl_FragColor = (1.0 - texture2DLod(heightMapID, uv, 5));[/code]

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()).

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1298202554' post='4776637']
You should create the texture once with glTexImage2D, specifying the total number of mip-levels, and then use glTexSubImage2D to upload the different levels.
[/quote]

Thanks for the quick reply!

Ok, so i've done things like this:

[code]

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());
}
[/code]


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

[code]

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));
[/code]

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:

[code]
gl_FragColor = texture2DLod(heightMapID, uv, 6.0);
[/code]

What might be wrong?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

[code]

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);
[/code]

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:
[code]
#version 330 core

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

void main() {
outColor = textureLod(tex0, varTexCoord, 2.0f);
}
[/code]

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1298216851' post='4776694']
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:
[code]
#version 330 core

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

void main() {
outColor = textureLod(tex0, varTexCoord, 2.0f);
}
[/code]
[/quote]

Here it is:

[code]

varying vec2 texCoords;
uniform sampler2D heightMapID;

void main()
{
gl_FragColor = textureLod(heightMapID, texCoords, 6.0);
}
[/code]

Share this post


Link to post
Share on other sites
Okay, i finally nailed the little bastard! Thanks to all of you, who helped me through the debug process! dpadam's post started me thinking and i tried to visualize the mipmap levels(by assigning a different color to every mip level, that is i colored every pixel from each level different). I started the program and nothing happened(the mipmaps were not shown, only the base level). The error was mine, obviosuly. I had a function like this:

[code]

void createPDM(std::string fileName, Endeavour::Assets::Texture* texture)
{
Endeavour::Assets::Image* image = (Endeavour::Assets::Image*)
Endeavour::Engine::getResourceFactory()->create("Image");

image->load(fileName);
image->allocateMipmaps();

// create pyramidal height map
......
// done

// what?!!!!!

Endeavour::Assets::Image* texture = (Endeavour::Assets::Texture*)
Endeavour::Engine::getResourceFactory()->create("Texture");
// what?!!!


texture->createFromImage(image);

delete image;
}
[/code]

Well, problem solved , this code dissaparead:
[code]

Endeavour::Assets::Image* texture = (Endeavour::Assets::Texture*)
Endeavour::Engine::getResourceFactory()->create("Texture");
[/code]

And now everything is working as expected!

Thanks again!

Share this post


Link to post
Share on other sites

This topic is 2490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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