Texture tutorial OpenGL

Tutorial 5 : A Textured Cube


In this tutorial, you will learn :

  • What are UV coordinates
  • How to load textures yourself
  • How to use them in OpenGL
  • What is filtering and mipmapping, and how to use them
  • How to load texture more robustly with GLFW
  • What is the alpha channel

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 has, 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 the triangle.

Knowing the BMP file format is not crucial : plenty of libraries can do it 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 :

GLuint loadBMP_custom(const char * imagepath);

so it’s used like this :

GLuint image = loadBMP_custom("./my_texture.bmp");

Let’s see how to read a BMP file, then.

First, we’ll need some data. These variable will be set when reading the file.

// Data read from the header of the BMP file unsigned char header[54]; // Each BMP file begins by a 54-bytes header unsigned int dataPos; // Position in the file where the actual data begins unsigned int width, height; unsigned int imageSize; // = width*height*3 // Actual RGB data unsigned char * data;

We now have to actually open the file

// Open the file FILE * file = fopen(imagepath, "rb"); if (!file) {printf("Image could not be opened\n"); return 0;}

The first thing in the file is a 54-bytes header. It contains information such as “Is this file really a BMP file?”, the size of the image, the number of bits per pixel, etc. So let’s read this header :

if ( fread(header, 1, 54, file)!=54 ){ // If not 54 bytes read : problem printf("Not a correct BMP file\n"); return false; }

The header always begins by BM. As a matter of fact, here’s what you get when you open a .BMP file in a hexadecimal editor :



More about Texture tutorial OpenGL

FYI, just some addressing background

by mshanahan

You don't need to XXX out 192.168.XX.XXX. the 192.168.????.???? is a private address group for your lan. (All my LANS are 192.168.SOMETHING, and I have at times had as many as 6 routers running on my LAN, with only the first one after the DSL modem being the DHCP server) The linksys router will use NAT (Network Address Translation) to hide that from the internet.
Hope this helps.
PS: there are some tutorials available on the web for free on addressing. I know my netgear equipment has one on at least 2 of the CDs for the routers that I have.
Good Luck, and yes Linksys will help you, but in some cases free support stops at 90 days after purchase (Netgear for instance), but I assume that you just purchased these.

Addison-Wesley Professional OpenGL ES 2.0 Programming Guide
Book (Addison-Wesley Professional)

CorelDRAW X7 review: Customizable features make this robust design program ..  — PCWorld
.. purchase, as well as a $25 monthly subscription. People miffed by Adobe's wholesale move to subscriptions will find a great alternative here. ..

Galaxy 61 Delivers Show Open for VH1's 'The Fabulous Life  — Animation World Network
VHI recently called upon Galaxy 61 to create a show intro for the new 2014 season of The Fabulous Life, the hit series that reveals the extravagant places, possessions and pastimes of pop-culture's most famous celebrities. The Brooklyn-based ..

Hunterdon County Polytech students bridge the gap thanks to local Rotary Clubs  — Hunterdon County Democrat
This year's Job Fair, on March 25, attracted the Dave Gansfuss Allstate Agent for an agency logo; G&H Publishing and author Katherine Kurz for children's book illustrations; Artiste Salon for a new logo, brochure and social media materials; St.

Smith Micro Software Smith Micro Software Poser Debut
Software (Smith Micro Software)
  • Easy-to-use software offers a fun way to learn 3D
  • Create cool 3D art and animation; 80+ fully rigged, fully posable, 3D characters
  • 2 GB of clothing, hair, props, etc; drag-and-drop content into the scene from the Library Search
  • In-app Project Guide teaches how to use Poser with video and easy-to-follow instructions
  • Includes everything needed to get started quickly

FAQ

wave_ripper35
Are there any Tutorials for OpenGL for Texturing a basic Hallway?

Here are two great resources for opengl.
both have a section for texture mapping.

Related Posts