20. OpenGL MipMap Generation
Mipmaps are a group of the same image, scaled to different sizes which are then selected according to how far away the viewer is.
Instead of resizing the texture to fit the object on the fly,
opengl will pick the mipmap that best fits the shape and textures
it with that. This fixes dodgy looking textures, and smooths
out the overall effect. Making a better looking image.
To do this all we need is this line in our LoadTexture function:
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
Instead of the line:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
The ‘data’ being the texture, the width and height being the width and height
of the texture file.
Then you just bind it like you would a normal texture.
But because we are now using mipmaps, we also want to change:
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
To
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
Simply so that we can get better looking textures.
And wasn’t that simple, we now have mipmaps 😀
If you have any questions, please email me at swiftless@gmail.com
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. |
#include <GL/gl.h> #include <GL/glut.h> #include <windows.h> #include <stdio.h> GLuint texture; //the array for our texture GLfloat angle = 0.0; GLuint LoadTexture( const char * filename, int width, int //The following code will read in our RAW file glGenTextures( 1, &texture ); //generate the texture with //here we are setting what textures to use and when. The MIN //The qualities are (in order from worst to best) //And if you go and use extensions, you can use Anisotropic //Here we are setting the parameter to repeat the texture //Generate the texture with mipmaps void FreeTexture( GLuint texture ) void square (void) { void display (void) { int main (int argc, char **argv) { texture = LoadTexture( “texture.raw”, 256, 256 ); //load our texture glutMainLoop (); FreeTexture( texture ); return 0; |
http://www.opengl.org/wiki/Common_Mistakes#gluBuild2DMipmaps
You should be using glGenerateMipmap(GL_TEXTURE_2D).
Never use gluBuild2DMipmaps
http://www.opengl.org/wiki/Common_Mistakes#gluBuild2DMipmaps
Right, so this line:
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
. . . will give undefined behavior.
Setting the mipmap flag in the mag filter will have undefined results. Using mipmapping for magnification doesn’t make sense.
Hi Geometrian,
You’re right, you can leave the mag filter as linear as this doesn’t make sense to set.
However it half makes sense in the case of magnification for effects such as bloom, where you take the smaller mipmap and apply it to a shape too large on purpose to get a blurry effect. I see this as magnification, albeit a skewed interpretation 😛
Cheers,
Swiftless
Magnification doesn’t work that way. Mipmapping is designed to work only in minification. This is why, according to the spec, no mipmapping is defined for mag filters. See (http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml).
While mipmap levels can be blown up for the purposes you describe, it can’t be done simply by setting the mag filter to a mipmap. You’d need to select the actual level when you draw it in the fullscreen pass.
Hi Geometrian,
I think we got our wires crossed, when you first mentioned magnification I was thinking as I described in my response (as an effect), I now understand where you are coming from (magnification as in the magnification level of the texture, not magnification as in the effect), and agree totally.
Cheers,
Swiftless