SFML community forums

General => Feature requests => Topic started by: Mars_999 on September 18, 2010, 06:47:29 pm

Title: Mipmapping for OpenGL texture creation?
Post by: Mars_999 on September 18, 2010, 06:47:29 pm
Hello,

Can we add in the ability to generate mipmapping.
Using

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

We can use the glGenerateMipmap() if everyone is on board for that vs. the above code...

I am thinking of something like this?

Code: [Select]

bool Image::CreateTexture()
{
 // Create the OpenGL texture
 if (!myTexture)
    {
       GLint PreviousTexture;
       GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &PreviousTexture));
 
        GLuint Texture = 0;
        GLCheck(glGenTextures(1, &Texture));
        GLCheck(glBindTexture(GL_TEXTURE_2D, Texture));
        GLCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, myTextureWidth, myTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
        GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
        GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST));
if(myIsMipmapped)
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myIsSmooth ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST));
else
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE));
        myTexture = static_cast<unsigned int>(Texture);
        GLCheck(glBindTexture(GL_TEXTURE_2D, PreviousTexture));
   }
}


Mipmapping would be on by default and you would need to add bool IsMipmapped() and SetMipmap(bool)

Please advise!

Thanks,
Dave
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on September 18, 2010, 11:02:32 pm
Mipmapping for 2D?? It wouldn't be used at all in SFML, and if you manage your textures externally... well, you manage them externally :)

If you're using sf::Image for your texture, you can still do it:
Code: [Select]
image.Bind();
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
Title: Mipmapping for OpenGL texture creation?
Post by: Mars_999 on September 19, 2010, 03:48:13 am
Yeah I am doing that already, I figured since SFML seems to be somewhat tied to OpenGL it would be nice to have that built in, so one has less code to deal with and would look cleaner sending a flag vs. calling more GL functions for each bind...

Anyway thanks.
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on September 19, 2010, 10:04:47 am
Quote
Yeah I am doing that already, I figured since SFML seems to be somewhat tied to OpenGL it would be nice to have that built in, so one has less code to deal with and would look cleaner sending a flag vs. calling more GL functions for each bind...

I understand your point of view, but SFML is not a general purpose OpenGL wrapper, it's not meant to make your OpenGL code nicer.
Title: Mipmapping for OpenGL texture creation?
Post by: Malcolm on April 06, 2011, 06:41:21 pm
+1 for this feature, it would be very useful!

Quote from: "Laurent"
Mipmapping for 2D?? It wouldn't be used at all in SFML,

Why not? Any image I load from disk has a fixed resolution obviously, but the sizes at which it will be drawn on screen may vary greatly (depending on the sprite's scales at which I blit it, and to some extent the user's screen resolution).

When loading a large image (e.g. 800x600) and blitting it to a smaller area (say 80x60) the quality will *greatly* benefit from mipmapping (as opposed to only bilinear filtering, which happens by default).

Quote
If you're using sf::Image for your texture, you can still do it:
Code: [Select]
image.Bind();
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

Not quite sure where/how to do this, could you elaborate please?

I'm doing this:
Code: [Select]
sf::Image myImage;
myImage.LoadFromFile("example.png");

Now what? Doing the code above didn't seem to make any difference.

Do I have to assign this image to a sprite in any special way, or do I have to Draw the sprite differently?
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 06, 2011, 07:31:54 pm
It's true that mipmaps could improve the 2D quality, I'll do some tests and add it if it's worth.
Title: Mipmapping for OpenGL texture creation?
Post by: Mars_999 on April 07, 2011, 04:23:31 am
;) I say add it, simple flag should suffice!

BTW what is the status of flipping the image for OpenGL textures? Meaning the image is not lower left but upper left? I am guessing this is setup for DX and should be for GL... Please inform me as I need to figure out what I am to do wait for a update or 2.0 or move on...
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 07, 2011, 07:55:49 am
Quote
I say add it, simple flag should suffice!

I'm even thinking about nothing at all in the public API (and removing SetSmooth as well): generating mipmaps + using NEAREST_MIPMAP_LINEAR should be ok for all situations (pixel perfect for scale 1, smoothed for scale < 1, no "big blur" for scale > 1).
That's why I was talking about tests ;)

Quote
BTW what is the status of flipping the image for OpenGL textures? Meaning the image is not lower left but upper left?

Well, I'm not convinced that there is a real problem with that. UV (0, 0) correctly maps to the pixel at (0, 0), not (0, height-1). So I'm not sure why people always complain about this stuff.
Title: Mipmapping for OpenGL texture creation?
Post by: Mars_999 on April 08, 2011, 01:30:03 am
I would use GL_LINEAR_MIPMAP_LINEAR IMO....

THe issue with the image isn't the 0,0 origin, but the pixels are not correct. The image itself needs to be flipped on the Y axis. I am not using your texture coordinates for anything in my engine, and I still have to flip the image either myself, or use a paint program to flip it or else my model mesh will not be mapped correctly! So I beg you please flip the pixels after the image data is loaded with SOIL with a flag parameter that is set to default your way, but can be changed to meet others needs...

Thanks!
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 08, 2011, 07:48:39 am
Quote
I would use GL_LINEAR_MIPMAP_LINEAR IMO....

Why?

Quote
THe issue with the image isn't the 0,0 origin, but the pixels are not correct. The image itself needs to be flipped on the Y axis.

That's what I meant: if the image is flipped on Y it means that bottom pixels are mapped to top texture coordinates.
But in all the tests that I did, it was not true. So instead of just saying "it doesn't work for me so please fix it", let's try to understand what's really wrong and find the real solution ;)

Quote
I am not using your texture coordinates for anything in my engine, and I still have to flip the image either myself

How do you create/load your mesh? Which file format?
Title: Mipmapping for OpenGL texture creation?
Post by: Mars_999 on April 09, 2011, 01:10:26 am
I use ASSIMP SDK to load my meshes...

http://assimp.sourceforge.net/

My not use Trilinear mipmapping?

Thanks!
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 09, 2011, 09:29:28 am
Quote
I use ASSIMP SDK to load my meshes...

http://assimp.sourceforge.net/

My not use Trilinear mipmapping?

Sorry, I don't understand. What do you mean?
Title: Mipmapping for OpenGL texture creation?
Post by: Mars_999 on April 10, 2011, 04:36:28 am
Quote from: "Laurent"
Quote
I use ASSIMP SDK to load my meshes...

http://assimp.sourceforge.net/

My not use Trilinear mipmapping?

Sorry, I don't understand. What do you mean?


Lost me?

ASSIMP is the SDK to load my mesh files.... link shows that

And the mipmapping filtering I was stating use the best Image Quality possible...

Thanks!
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 10, 2011, 09:56:48 am
Quote
Lost me?

Kind of. Sorry I had forgotten about the flipped texture thing, I thought your previous answer was all about mipmapping and I didn't understand what ASSIMP was doing here :D

Quote
And the mipmapping filtering I was stating use the best Image Quality possible...

Don't forget that in 2D we sometimes want 1:1 rendering (pixel perfect) compared to the original images. Using the "biggest" smooth filter available may not be the best option.
Title: Mipmapping for OpenGL texture creation?
Post by: etam on April 22, 2011, 11:02:09 pm
+1 :D

How is your testing? Any results?
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 22, 2011, 11:44:40 pm
Not yet, I'm working on something else.
Title: Mipmapping for OpenGL texture creation?
Post by: Laurent on April 25, 2011, 07:33:03 pm
I've done some tests today.

Here are my conclusions:
- Mipmapping doesn't help at all, it's even worse than bilinear filtering without mipmaps; I think it's just an optimization, it's not meant to provide better results
- I can't remove SetSmooth from sf::Image, hen scale is greater than 1 we really need both modes: when the sprite is aligned, smoothing off gives better results, but when a rotation is involved it's really ugly without smoothing.
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Cleroth on April 24, 2014, 10:16:39 pm
I beg to differ. I just implemented mipmaps in my game and have seen far superior quality (at least on zoomed out views, such as minimaps).
It's such a simple change that it makes me wonder why it's not implemented. It's definitely very useful if you want to zoom out or scale down sprites.

And here's some screenshots for comparison:

Original: http://i.imgur.com/t9il2S9.png
Without: http://i.imgur.com/G0bUX8t.png
With: http://i.imgur.com/aaTswQd.png

Without: http://i.imgur.com/v4zCEFP.png
With: http://i.imgur.com/3rjNeXz.png

Without: http://i.imgur.com/lnv3ZoE.png
With: http://i.imgur.com/RNv6MIk.png

Without: http://i.imgur.com/noyWMsk.png
With: http://i.imgur.com/n3hfS4Y.png

I'm sure some may argue that without mipmaps you get more 'detail'. But this detail is pretty... unrealistic, as was as looking competely absurd when you do a smooth zoom: https://www.youtube.com/watch?v=JP108IQsA1o&feature=youtu.be
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Ixrec on April 24, 2014, 10:38:16 pm
Maybe I'm missing something but to my eyes those screenshots seem to be supporting Laurent's view.  The ones with mipmapping look more like solid green and gray blobs (especially that last one) than repeated grass and stone textures.
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Cleroth on April 24, 2014, 10:39:31 pm
Look at the video and tell me that looks natural?
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Ixrec on April 24, 2014, 10:48:46 pm
Yeah, zooming is definitely a problem, but making that smooth definitely doesn't justify the poor quality when not zooming.  Unless the idea is to toggle mipmapping when the user starts and stops zooming.

Also, I know I've seen those strange zoom effects in quite a few professional games before, so maybe this problem is just too hard to solve in general.
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Cleroth on April 24, 2014, 10:51:21 pm
They look exactly the same when you're not zoomed. It's looks slightly smoother when only zoomed out a bit, of which looks better then is a matter of taste.
My point isn't that mipmaps are good everywhere. They're good in some occasions, and some will just prefer them (as I do, and other in this thread have).
I'd rather have a simple toggle than writing my own mipmap functionality with just 5 lines on every texture I want it in.
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Nexus on April 24, 2014, 11:06:46 pm
https://github.com/SFML/SFML/pull/498

Suddenly mipmapping seems to be very popular :)
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Sqasher on April 25, 2014, 12:55:23 pm
I did a little bit of research and found out that mipmaps are a good thing in general, but can look blurry when used, because they are build by averaging the pixels. Also for good results with non power of two textures or arbitrary scales, you must use polyphase filters.

Possible solutions for the blurriness are:
- Use premade mipmaps
- Use a sharpening algorithm like the Kaiser filter: Mipmapping, Part 1   (http://www.number-none.com/product/Mipmapping,%20Part%201/index.html)Mipmapping, Part 2 (http://www.number-none.com/product/Mipmapping,%20Part%202/index.html)
- Modify the mipmap bias (GL_TEXTURE_LOD_BIAS) http://www.opengl.org/registry/specs/EXT/texture_lod_bias.txt (http://www.opengl.org/registry/specs/EXT/texture_lod_bias.txt)
- Don't use mipmaps

Sources:
http://forums.tigsource.com/index.php?topic=26733 (http://forums.tigsource.com/index.php?topic=26733), http://developer.nvidia.com/content/non-power-two-mipmapping (http://developer.nvidia.com/content/non-power-two-mipmapping)
Title: Re: Mipmapping for OpenGL texture creation?
Post by: lolz123 on April 25, 2014, 05:34:48 pm
Mipmaps definitely help, even in 2D. I made a simulation called ALife (it's on these forums) where you can zoom in and out of the map. It looked absolutely terrible without mipmaps when zoomed out, but great with them.

Mipmaps apply whenever the image on the screen is scaled down from its actual resolution, something that also applies to 2D.

So yes, I am in favor of adding mipmaps to SFML  :D
Title: Re: Mipmapping for OpenGL texture creation?
Post by: wintertime on April 29, 2014, 11:23:14 pm
It would be nice if people took a deeper look, rather sooner than later, at not only that pull-request, but also to the other related commits in the further branch of my fork. With the current spin up in activity and the changes for mobile I would get sad when it bitrots, as I made an effort to have it nicely integrated with fitting API that enables use of all mipmap-modes and writing some help text.

Btw, adding on loading of premade mipmaps would be a good addition later on.
Title: Re: Mipmapping for OpenGL texture creation?
Post by: Nexus on April 29, 2014, 11:42:30 pm
We will certainly have a look at it, but at the moment the top priority is preparing the release of SFML 2.2 (and organizing the whole development/team)... So please have some patience :)
Title: Re: Mipmapping for OpenGL texture creation?
Post by: CheezeMaker on January 30, 2024, 04:02:36 am
Mipmapping doesn't help at all, it's even worse than bilinear filtering without mipmaps; I think it's just an optimization, it's not meant to provide better results
I understand that I reply to 12yo topic, but! Of course mipmaps is for better results. If you're showing texture that has bigger resolution than you're drawing on screen - you absolutely need mipmaps. If you're drawing 512x512 texture in 32x32 square, what do you expect your GPU to do? It will just sample 32x32 pixels from 512x512 image, basically "skipping" the rest of the pixels, creating a noisy mess. And if move it - it's even worse, because those choosen 32x32 will changed every frame. What you want - is to sample 32x32 pixels from 32x32 image. Which is what mipmaps do for you. Cases where you don't need mipmaps are EXTREMELY rare.
Title: Re: Mipmapping for OpenGL texture creation?
Post by: eXpl0it3r on January 30, 2024, 07:59:01 am
Not exactly sure what we gain from that necropost, but just in case you missed it, SFML can generate mipmaps: sf::Texture::generateMipmap() (https://www.sfml-dev.org/documentation/2.6.1/classsf_1_1Texture.php#a7779a75c0324b5faff77602f871710a9). ;)