Mega Code Archive

 
Categories / Delphi / Graphic
 

OpenGL V Texture filtering

Title: OpenGL V: Texture filtering Question: in my own words (and please correct me if this is wrong), Texture filtering "simply" defines how the images look when they are near/far from the eye Answer: Texture filtering: in my own words (and please correct me if this is wrong), it "simply" defines how the images look when they are near/far from the eye, in previous articles I have already showed you but I haven't explained any more detail until now, if you look at the previous articles code you'll find lines like: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering in OpenGL that's texture filtering, let me explain a little bit here so far I have shown you the basics of OpenGL, if you haven't read those, please do so you can understand better what I'm talking about on this article OpenGL I: Hello World, Setup a Delphi OpenGL Application OpenGL II: Moving and rotating 2D shapes OpenGL III: Moving and rotating 3D shapes OpenGL IV: Texture mapping Also you can keep checking here for future articles on OpenGL in the last tutorial I just put Linear Filtering, they require quite a bit of processing resources, but they look very nice, in this article I will show you 2 more types of filtering, the first one is: GL_NEAREST: this is basically no filtering at all, therefore it looks crappy, you will see when you run the demo, however this type of filtering requires very few CPU resources (translated in speed), making it ideal for slow cards/computers with that said, we would rewrite the 2 lines above like this: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Nearest Filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Nearest Filtering the GL_TEXTURE_MIN_FILTER filter is used when the image is drawn smaller than the original (Minification) the GL_TEXTURE_MAG_FILTER filter is used when the image is drawn bigger than the original (Magnification) Note that you can combine 2 types of filtering, say you don't care how the image looks when is far, but look good when is near, you could use GL_NEAREST for the MIN_FILTER and GL_LINEAR for the MAG_FILTER With the second (new) type of filtering I will also show you a new way to create textures, Mipmapping, before this whenever you drawn an image very tiny on the screen, a lot of the detail is lost, when you use mipmapping in a texture OpenGL tries to build different sized high quality textures, then when you draw the texture to the screen, OpenGL will select the best looking textures from the ones it built and draw it instead of resizing the original image Also, another interesting thing to note is that with mipmapping there's no limitation about the "power of 2", 64,128,256 size of images they say (don't take my word for it) that you can use any bitmap image you want with any width and height, when you use mipmapping OpenGL will automatically size it to the proper width and height it would be something like this: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // mipmap Linear Filtering (there's also GL_LINEAR_MIPMAP_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering that's for the filtering, then the next instruction actually builds the mipmapped texture: gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture1^.sizeX, texture1^.sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture1^.data) instead of glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1^.sizeX, texture1^.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1^.data) and that's it, here's the whole code to load the textures with the 3 types of textures, the 2 new and the one I showed you in the last article // Load Bitmaps And Convert To Textures procedure LoadGLTextures; Procedure LoadATexture(Var Where:GLuint; Const FName:String; const Param, Param2:Cardinal; const MipMapped:Boolean=False); var texture1: PTAUX_RGBImageRec; Begin texture1 := auxDIBImageLoadA(PChar(FName)); //Load the actual image to memory if (Assigned(texture1)) then Begin glBindTexture(GL_TEXTURE_2D, Where); // Typical Texture Generation Using Data From The Bitmap glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,Param); // filtering for Magnification glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,Param2); // filtering for Minification //generate the texture if not (MipMapped) then glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1^.sizeX, texture1^.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1^.data) else gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture1^.sizeX, texture1^.sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture1^.data) End End; begin // Create Texture glGenTextures(3, texture[0]); // Create The Textures LoadATexture(texture[0], 'data\rb.bmp', GL_NEAREST, GL_NEAREST); LoadATexture(texture[1], 'data\rb.bmp', GL_LINEAR, GL_LINEAR); LoadATexture(texture[2], 'data\rb.bmp', GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, True); end; and here's a relation of the parameters you can use: GL_TEXTURE_MAG_FILTER == GL_NEAREST or GL_LINEAR GL_TEXTURE_MIN_FILTER == GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, or GL_LINEAR_MIPMAP_LINEAR you can download the code for the article here: And the GLAux unit and dll here: reference: OpenGL programming guide third edition keep up coding salu2 EberSys