What are Mipmaps?
BlitzMax Forums/BlitzMax Beginners Area/What are Mipmaps?
| ||
What are mipmaps? How do you use them? Thanks |
| ||
http://en.wikipedia.org/wiki/Mipmap Essentially mipmaps are pre-generated versions of an image at varying quality levels. The purpose is to speed drawing of things when scaled down (by drawing smaller ones rather than scaling the whole thing every time) or improve detail when scaled up (things get blurry when you scale up, but if you have a higher resolution version ready to go that has more detail problem solved). I don't recall if bmax handles mipmaping internally or not. MiniB3D does for textures, where by when you load a texture it creates smaller versions of it for you, and feeds it all to OpenGL. OpenGL then has various levels it can reference, so if it doesn't need the full size texture it can work with a lower mipmap level version of it so it takes less time to process since there is less data to work through. |
| ||
I wouldn't describe them as quality levels. They are at different SIZES mainly, and a size is selected based on the angle of the triangle in 3D and its distance from the camera, so that there is less time spent fetching/caching texture pixels when the geometry is relatively far away or scaled down. It takes 1.33* as much memory to store but can be quite a bit faster than without. |
| ||
I don't have evidence to back this up right now, but the cost of sampling should be the same regardless of the texture being close or distant from the camera. Since everything is rasterized, when the texture's far away the rasterizer samples texels with a bigger spread as only a few pixels of the far-away texture make it to the screen, so no cost inflicted. I strongly believe mip-mapping is implemented exclusively to reduce aliasing at regions that are farther away from the view. If you use the same high-detail texture for everything you will see a lot of noise at distant regions (notice the background): ![]() Left side with mip-maps off, right side with them on (ignore the devil faces as their showcasing Magnifying filtering which never uses mip-mapping) - Image by MikhailV of Fastlibs.com There's no point in keeping textures of the same size if they're blurred, so every mip-level is half smaller (128, 64, 32, 16, 8, 4, 2, 1 if you start with a 128² texture). The way you use mip-levels is by uploading them to video memory at texture-creation time. You upload them with API calls. You can generate each level yourself (as Max2D does it, downscaling the original bitmap for each mip level), or let the hardware do this (there's an API call to generate mip levels automagically, although it's been deprecated AFAIK); most developers choose to make them themselves as you can choose any method to downscale your bitmap and this can influence the quality of the generated images. Creating and uploading mip-levels is half the work. At render-time you need to set the each texture's Minifying filter to make use of them. In OpenGL, this is done with the TexParameteri() function with the appropriate parameters. This is how MiniB3D does it when preparing textures for the entity about to be rendered: ' mipmapping texture flag If tex_flags&8<>0 'If texture has flag '8'... glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR) '...turn on linear mipmap sampling. Else glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR) 'else turn it off. EndIfExcerpt from MiniB3D's TMesh.BMX Update() function - by SimonH BTW, MiniB3D uses the same method as Max2D for creating textures and mipmaps: it does so manually. [/grain of salt] Last edited 2012 |
| ||
Hiya, You are right in that explanation :- Except youre combining texture filtering with your explanation of mip-maps. Mip-maps are nothing more than the same texture down scaled in powers of 2 to be used in relation with the distance from the viewers point. The blurring action, therefore better image quality, is a result of texture filtering which is combined with mip-maps. Mip maps were invented to increase speed of rendering on the really old graphics cards. You usually find that mip-maps and texture filtering go hand in hand nowadays and one isn't used without the other. Max2D creates the mip-map levels manually. That image is also using texture filtering to achieve the blurred, smoother textures. Just my 2 pennies worth to help understand that there is a difference between mip-maps and texture filtering. |
| ||
Hiya, You are right in that explanation :- Except youre combining texture filtering with your explanation of mip-maps. Mip-maps are nothing more than the same texture down scaled in powers of 2 to be used in relation with the distance from the viewers point. The blurring action, therefore better image quality, is a result of texture filtering which is combined with mip-maps. Mip maps were invented to increase speed of rendering on the really old graphics cards. You usually find that mip-maps and texture filtering go hand in hand nowadays and one isn't used without the other. Max2D creates the mip-map levels manually. That image is also using texture filtering to achieve the blurred, smoother textures. Just my 2 pennies worth to help understand that there is a difference between mip-maps and texture filtering. EDIT:- Here's a respectable explanation of the various filter modes available |
| ||
Hi col, I see what you mean. • Nearest-neighbour filtering: jagged, aliased look no matter the distance. • Linear filtering: smooth look. • Linear filtering + Mip-Maps: smooth look just as before, BUT faster sampling of distant textures at the cost of more memory being used (sampling speed increase more noticeable on older graphic cards, but still present in modern ones). Last edited 2012 |
| ||
You got it. There's usually several filtering options available nowadays too... bilinear, trilinear, anisotropic just to name a few of the common 'built-in' ones. Also remember pretty much any 'filter effect' you can dream up is available when combined with shaders. |
| ||
How to use? Call this before you load any images. I assume you want the usual masked and filtered flags too. AutoImageFlags(MASKEDIMAGE | FILTEREDIMAGE | MIPMAPPEDIMAGE) What it does? Filters images which are scaled DOWN, to make the smoother. Experiment with it on and off and see which you prefer. If you won't scale images down, it has no effect. You could apply mipmapped image only to the images you will scale down. Mipmapping an image will double the amount of video memory it requires. A small game on a modern pc shouldn't have any problem with that. But if you have any large eg 1024x1024 background images, consider not mipmapping them. |