Welcome to the new Friends-of-FPC!

Here you can find all kinds of information about the FreePascal Compiler. We have many tutorials and howtos as well as a selection of tools to help you with your programming. We also have some example codes for you. And if you want to contribute some information/ sources/ tools yourself you can do so.
Also we have finally relaunched the FoFPC forum. It's your chance for some Q&A about everything FreePascal.

Friends-of-FPC

Tutorials: Learn how to code with FreePascal.

Source Codes: A collection of examples, miscellaneous source codes and open source stuff.

Tools and Help Files: Intro- duction of some tools that might help you with FPC.

Community

Forum: Ask or answer questions about the FreePascal Compiler, programming or just babble about coding.

Contribute! Contribute your own Tutorial, Source Codes or Tools and send them to us!

Website

About: Information about Friends-of-FPC.org.

OpenGL/ FPC - Chapter 17 - by Delax

This is (I guess) the most anticipated chapter. It's about texture mapping. A texture is a picture, that is attached (=mapped) onto a surface. That way, you don't need complex patterns of surfaces, you just need to attach a picture of them. Therefor the number of polygons is kept low. And of course: you can attach photos, text etc. to add some content to your OpenGL scene.

Texturing in OpenGL is not as hard as some of you may fear. It is complex, but you don't need to know all the bells and whistles to start off. In this chapter, we will just bring a very simple, self-generated texture onto our screen.

A texture is just a row of pixel information - much like a bitmap. Let's picture this: a pointer to some memory. It starts with the red value for the first pixel, then the green value, then the blue value. Now following is the red value of the second pixel etc. Of course this applies for a simple RGB texture, but you get the picture.

So there we have some picture information. As usual OpenGL needs an identifier for a texture. We attach this id to our memory and tell OpenGL to use our block of memory whenever we activate our id. That's the theory. now for the code.

First we need an id. This is a simple GLuint variable. We also define a pointer for our texture data.

  TextureID : GLuint;
  TextureData : pByte;

First off, don't forget to get memory for your pointer. It is the width and height of our texture, multiplied with the type. For example RGB needs 3 byte per pixel, RGBA 4 byte etc. I'll use a 128x128 texture with RGB.

  GetMem(TextureData, 128*128*3);

Now we get to the OpenGL commands. We activate texture mapping by simply enabling it. Then we tell OpenGL to get us an ID for one texture and store it in TextureID.

  glEnable(GL_TEXTURE_2D);
  glGenTextures(1, TextureID);

Now we activate the texture and bind our pointer to the activated ID (don't get confused - we use glBindTexture to activate it and glTexImage2D to load the pointer).

  glBindTexture(GL_TEXTURE_2D, TextureID);	// now use it

Now we have to initialize our texture. This is setting up the behaviour of the texture. This is important! so don't leave it out.

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

Now we are actually loading the texture data into the the OpenGL space.

  glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureData^);

We have to give the type of the pixel data, the size of the texture and the order of the pixel data. It could also be GL_BGR etc. Just look up the types in the api reference if you want to know. We also give the pointer where our data is stored.

Now before we go on, just a little something about OpenGL and texture sizes. A texture has to be the size of 2^n. Which means it is allowed to use textures like 8*8, 16*16, 32*32, 64*64, 128*128 and so on, but NOT textures like 640*480. Now you may think "What kind of crap is that?!", but it's true. Normal textures are limited this way and that's just it. There is a way of course to use textures of any given size, but these work somewhat different. I'll cover it later, OK? For now, just stick to 2^n.

So much for setting up everything. Now we just have to draw our surface and attach the texture to it. Normally you would be working with multiple textures, so first you have to bind the texture you want to use to the surfaces.

  glBindTexture(GL_TEXTURE_2D, TextureID);

Furthermore, you have to tell OpenGL how to attach the texture to your surface. This is done with the glTexCoord2f command. The drawing routine looks something like this.

    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0);
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0);
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0);

Look closely. glTexCoord2f specifies the edges of the texture and how they are attached to the surface. 0 is the left/ top edge and 1 is the right/ bottom edge. If you look at the surface, we are attaching the left/ top edge of the texture to the left/ top edge of our surface. If we specify a value of, let's say 2, the texture would be drawn 2 times. A value of 0.5 would mean only half of the texture would be drawn.

Basically that's it. So what did we forget? Right - we don't have something in our pointer to actually draw. It's empty! So you have to fill it.

If we want to have a picture as texture, we would just load it from a file. Bitmaps are quite easy to load, JPG and other compressed formats are somewhat harder. You also can generate something and draw it to the texture space. These generated textures (or math textures as they are sometimes called) have the great advantage that they take up very little space - just some bytes for the algorithm. However, we'll get to that in some later chapter. For now, just make sure you understood everything.

Delax/ Sundancer Inc.
[delax@sundancerinc.de]

Back to previous page

Useful Links









Link to us