|
|
(15 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
| First, let's talk about texture types available.<br>
| | #REDIRECT [[Texture]] |
| <br>
| |
| GL has GL_TEXTURE_1D. You can ignore this and use GL_TEXTURE_2D instead.<br>
| |
| This has been part of GL since 1.0<br>
| |
| <br>
| |
| GL_TEXTURE_2D has width and height and usually the GPU stores this in memory in a format that is quick to access.<br>
| |
| For example, small blocks of the texture are stored in sequence so that cache memory works better.
| |
| This has been part of GL since 1.0<br>
| |
| <br>
| |
| GL_TEXTURE_3D has width and height and depth and usually the GPU stores this in memory in a format that is quick to access.<br>
| |
| Just like 2D, small blocks of the texture are stored in sequence so that cache memory works better but other techniques exist as well.<br>
| |
| This has been part of GL since 1.2<br>
| |
| <br>
| |
| GL_TEXTURE_CUBE_MAP has width and height and 6 faces. Kind of like 2D except it has 6 faces and texcoords work in a special way.<br>
| |
| This has been part of GL since 1.3<br>
| |
| <br>
| |
| GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_RECTANGLE_ARB are supported as extensions. For having non power
| |
| of 2 dimension 2D textures. Texcoords worked in a unusual way. From 0 to width for S. From 0 to height for the T.<br>
| |
| <br>
| |
| <br>
| |
| With GL 2.0, GL_TEXTURE_RECTANGLE becomes obsolete. You can make textures with any dimension, mipmap it, use any anisotropy supported,
| |
| use any texture wrap mode.<br>
| |
| <br>
| |
| <br>
| |
| How to create a texture?<br>
| |
| <br>
| |
| Create a single texture ID<br>
| |
| GLuint textureID;<br>
| |
| glGenTextures(1, &textureID);<br>
| |
| <br>
| |
| Now you should define your texture properties. Any call sequence will work since GL is a state machine.<br>
| |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, TextureWrapS);<br>
| |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, TextureWrapT);<br>
| |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagFilter);<br>
| |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter);<br>
| |
| <br>
| |
| Some people make the mistake of not calling glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinFilter); and
| |
| they get the default state GL_LINEAR_MIPMAP_NEAREST and they don't define the mipmaps, so the texture is considered
| |
| incomplete and you just get a white texture.<br>
| |
| <br>
| |
| If you will use mipmapping, you can either define them yourself by making many calls to glTexImage2D<br>
| |
| Since current GPUs can generate it automatically with a box filter technique, you can call<br>
| |
| Mipmapping is usually good and increases performance.<br>
| |
| <br>
| |
| glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
| |
| <br>
| |
| If you need anisotropy call<br>
| |
| glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, Anisotropy);<br>
| |
| <br>
| |
| Anisotropy is an extension. It can drag down performance greatly and make your results look better. Use as less as possible.<br>
| |
| <br>
| |
| Define the texture with<br>
| |
| glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, border, format, type, ptexels);<br>
| |
| <Br>
| |
| You need to make sure that your width and height are supported by the GPU.<br>
| |
| Very old GPUs don't support border texels.<br>
| |
| Make sure the format is supported by the GPU else the drivers will convert into a proper format.<br>
| |
| Make sure the internal format is supported by the GPU (example : GL_RGBA8) else the driver will convert the texture for you.<br>
| |
| You should also call glGetError to make you don't get an error message like running out of memory.<br>
| |
| <br>
| |
| The only thing left is calling glTexEnv but this isn't part of the texture state. It is part of the texture_environment, in other
| |
| words the texture unit.<br>
| |
| <br>
| |