Results 1 to 2 of 2

Thread: How do I use textures?

  1. #1
    Member Newbie
    Join Date
    Oct 2003

    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
    DirectX: A giant pile of undocumented CRAP

  2. #2
    Member Newbie
    Join Date
    Oct 2003

    Re: How do I use textures?

    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.

    3) 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

    4) 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)



    This has created our texture.

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


    glBindTexture(GL_TEXTURE_2D, tex);


    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);


    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.

Similar Threads

  1. Applying textures to a sphere. I know nothing about textures.
    By ElFlautas in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-28-2018, 03:47 AM
  2. model textures being redirected to font textures
    By Tcll5850 in forum OpenGL: Advanced Coding
    Replies: 7
    Last Post: 09-07-2014, 10:43 PM
  3. FBO Attached Textures Reused as Shader Source Textures
    By Rennie Johnson in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-22-2012, 09:43 PM
  4. Lots of small textures or fewer large textures?
    By jlamanna in forum OpenGL: Advanced Coding
    Replies: 30
    Last Post: 01-12-2004, 03:53 AM
  5. are GL_COLOR_INDEX textures converted to RGBA-Textures?
    By TB-Rex in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 12-18-2000, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean