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; |