How do I use textures?

How would I put a texture on, say, a quad?
What if I want to put it over a 100x100 grid of points (each with thier own x, y, and z values)?
This is nothing urgent, but I would really like to know.
Thanks, Andrew

To render a textured quad you should have a texture.

To create a texture you should:

  1. get a valid texture name. You do that with
    glGenTextures(GLsizei n, GLuint* textures);

    This function creates n texture names and stores them in the textures parameter.

  2. you should bind the texture so that you make you texture the current texture (so to speak, even if it is not correct)

You do that by calling
glBindTexture(GLenum target,GLuint texture);

target id GL_TEXTURE_2D (or 1D, or 3D) and represents the way the texture is interpreted by opengl and texture is a valid texture name.

  1. load the texture into the graphics card with

glTexImage1D or 2D

void glTexImage2D(
GLenum target, //GL_TEXTURE_2D
GLint level, //the mipmap level (0 for the first level)
GLint internalformat, //GL_RGB, or GL_RGBA or … (there are more) (have it match your framebuffer or you’ll lose performance)
GLsizei width, //the width of the texture
GLsizei height, //the height of the texture
GLint border, //0 for no border
GLenum format,//the format of the data to be fetched from the “pixels” argument
GLenum type, //the type --| |----
const GLvoid *pixels //pointer to the data
);

  1. you should set the GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER parameters used for minifying or maximizing.

Use glTexParameteri(GLenum target, GLenum pname, GLint param);

Here is an example:

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(G:_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

(let’s suppose that data is a pointer to
an array of 128 * 128 * 3 unsigned bytes, and that it contains our texture)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

This has created our texture.

In order to use a texture, you must first bind it:

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, tex);

glBegin(GL_QUADS);

 glTexCoord2f(0, 0);
 glVertex3f(0, 0, 0);

 glTexCoord2f(0, 1);
 glVertex3f(0, 1, 0);

 glTexCoord2f(1, 1);
 glVertex3f(1, 1, 0);

 glTexCoord2f(1, 0);
 glVertex3f(1, 0, 0);

glEnd();

glTexCoord2f specifies the coordinates of the texels that map to the next vertex.
In the example above, the (0, 0) texel maps
to the (0, 0, 0) vertex and so on. The rest of the fragments are texturated using interpolation.

Aaaaaa, and most important, in order to use the textures you’ll have to enable them with glEnable(GL_TEXTURE_2D) (or 1D, or 3D)

If you want to texture multiple poligons you’ll have to thik of a way of creating the mapping from vertex coordinates to texture coordinates and use the same methodology as for a single quad.