About UV coordinates
When texturing a mesh, you need a way to tell to OpenGL which part of the image has to be used for each triangle. This is done with UV coordinates.
Loading .BMP images yourself
Knowing the BMP file format is not crucial : plenty of libraries can load BMP files for you. But it’s very simple and can help you understand how things work under the hood. So we’ll write a BMP file loader from scratch, so that you know how it works, and never use it again.
Using the texture in OpenGL
We’ll have a look at the fragment shader first. Most of it is straightforward :
What is filtering and mipmapping, and how to use them
As you can see in the screenshot above, the texture quality is not that great. This is because in loadBMP_custom, we wrote :
How to load texture with GLFW
Our loadBMP_custom function is great because we made it ourselves, but using a dedicated library is better. GLFW2 can do that too (but only for TGA files, and this feature has been removed in GLFW3, that we now use) :
Compressed Textures
At this point, you’re probably wondering how to load JPEG files instead of TGA.
Exercices
The DDS loader is implemented in the source code, but not the texture coordinate modification. Change the code at the appropriate place to display the cube correctly.
About UV Coordinates
Loading .bmp Images Yourself
- Knowing the BMP file format is not crucial : plenty of libraries can load BMP files for you. But it’s very simple and can help you understand how things work under the hood. So we’ll write a BMP file loader from scratch, so that you know how it works, and never use it again. Here is the declaration of the loading function : so it’s used like this : Let’s see how to read a BMP file, then. …
Using The Texture in OpenGL
- We’ll have a look at the fragment shader first. Most of it is straightforward : Three things : 1. The fragment shader needs UV coordinates. Seems fair. 2. It also needs a “sampler2D” in order to know which texture to access (you can access several texture in the same shader) 3. Finally, accessing a texture is done with texture(), which gives back a...
What Is Filtering and Mipmapping, and How to Use Them
- As you can see in the screenshot above, the texture quality is not that great. This is because in loadBMP_custom, we wrote : This means that in our fragment shader, texture() takes the texel that is at the (U,V) coordinates, and continues happily. There are several things we can do to improve this.
How to Load Texture with GLFW
- Our loadBMP_custom function is great because we made it ourselves, but using a dedicated library is better. GLFW2 can do that too (but only for TGA files, and this feature has been removed in GLFW3, that we now use) :
Compressed Textures
- At this point, you’re probably wondering how to load JPEG files instead of TGA. Short answer : don’t. GPUs can’t understand JPEG. So you’ll compress your original image in JPEG, and decompress it so that the GPU can understand it. You’re back to raw images, but you lost image quality while compressing to JPEG. There’s a better option.
Conclusion
- You just learnt to create, load and use textures in OpenGL. In general, you should only use compressed textures, since they are smaller to store, almost instantaneous to load, and faster to use; the main drawback it that you have to convert your images through The Compressonator (or any similar tool)
Exercices
- The DDS loader is implemented in the source code, but not the texture coordinate modification. Change the code at the appropriate place to display the cube correctly.
- Experiment with the various DDS formats. Do they give different result ? Different compression ratios ?
- Try not to generate mipmaps in The Compressonator. What is the result ? Give 3 different wa…
- The DDS loader is implemented in the source code, but not the texture coordinate modification. Change the code at the appropriate place to display the cube correctly.
- Experiment with the various DDS formats. Do they give different result ? Different compression ratios ?
- Try not to generate mipmaps in The Compressonator. What is the result ? Give 3 different ways to fix this.