Receiving Helpdesk

cube.obj with texture

by Destini O'Connell I Published 3 years ago Updated 3 years ago

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

Image
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. Each vertex can have, on top of its position, a couple of floats, U and V. These coordinates are used to access the texture, in the following way : Notice how the texture is distorted on …
See more on opengl-tutorial.org

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. …
See more on opengl-tutorial.org

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...
See more on opengl-tutorial.org

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.
See more on opengl-tutorial.org

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) :
See more on opengl-tutorial.org

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.
See more on opengl-tutorial.org

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)
See more on opengl-tutorial.org

Exercices

  1. 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.
  2. Experiment with the various DDS formats. Do they give different result ? Different compression ratios ?
  3. Try not to generate mipmaps in The Compressonator. What is the result ? Give 3 different wa…
  1. 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.
  2. Experiment with the various DDS formats. Do they give different result ? Different compression ratios ?
  3. Try not to generate mipmaps in The Compressonator. What is the result ? Give 3 different ways to fix this.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9