OpenGL Textures

Here are some earlier notes on the subject

read this iphone article to see if this works

Here is an article from microsoft

Texture mapping from the redbook

Search for: opengl texture mapping coordinates

Read this pdf file

from ed angel: pdf

a pdf from ohio state

sorry here is the real link for angel: ppt

Search for: glTexCoord

msdn guide

Search for: opengl example texture a cube

Search for: opengl multiple texture mappings

Search for: opengl different textures for different objects

another tutorial from iphone

this might give some info

A texture is an image, typically two dimensional and rectangular. You load these from bitmaps. Then you stick these images on a surface of a figure that you draw.

Irrespective of the height and width of the texture image, they are normalized to (0,0) and (1,1). Then you take the figure (surface) and lay it on this image so that the figure is completely inside the boundaries of the image (after scaling to the 0 to 1 scale and assuming your figure is smaller than the image). Then calculate the texture coordinate for each vertex coordinate and specify it through textcoord api.


int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);

mTextureID = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);

gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
      GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,
      GL10.GL_TEXTURE_MAG_FILTER,
      GL10.GL_LINEAR);

gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
      GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
      GL10.GL_CLAMP_TO_EDGE);

gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
      GL10.GL_REPLACE);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
      GL10.GL_MODULATE);

gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
      GL10.GL_REPEAT);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
      GL10.GL_REPEAT);

public void draw(GL10 gl) 
{
   gl.glFrontFace(GL10.GL_CCW);
   gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
   gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
   gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, VERTS,
         GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
}

How can I apply multiple textures to multiple faces

How can I apply same texture to multiple faces

It looks like when you draw you have to specify the vertices and the tex coordinates at the same time. So for example if I were to draw a cube, I can draw it in one swoop with a single call to draw elements. I just don't know a way to apply multiple texture coords the same time.

I may have to draw multiple faces multiple times, each time with a different texture.

How do I texture a mesh

Search for: How do I texture a mesh

So the generalization is, if I have a mesh I am drawing with draw elements, and different surfaces need different textures how do I do that?

How do I texture a mesh with the same texture?

Search for: How do I texture a mesh with the same texture?

How can I stick a stamp using opengl textures

Search for: How can I stick a stamp using opengl textures

what is an OpenGL texture target GL_TEXTURE_2D

Search for: what is an OpenGL texture target GL_TEXTURE_2D

Here is how they are defined in the context of its glBindTexture

The OpenGL method glGenTextures is responsible for generating (more like reserving) unique IDs for textures so that those textures can be referenced later. Notice the code below


        int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);
        mTextureID = textures[0];

The first argument Is the number of textures we want and the second argument is the array for the API to write the integer IDs (names) that are returned. The third argument is the offset in the array to write to by the API. In our case we only want one texture name because we only have one bitmap that we want to load. If we want two textures then we could ask for 2 IDs. During this ID or name allocation no bitmap has been loaded. We need this ID upfront because we may want to direct OpenGL to set some parameters to control the behavior of this texture including the loading of the bitmap. In a sense this is like creating a texture object in order to start defining its properties.

f you choose GL_NEAREST, the texel with coordinates nearest the center of the pixel is used for both magnification and minification. This can result in aliasing artifacts (sometimes severe). If you choose GL_LINEAR, a weighted linear average of the 2 � 2 array of texels that lie nearest to the center of the pixel is used, again for both magnification and minification. When the texture coordinates are near the edge of the texture map, the nearest 2 � 2 array of texels might include some that are outside the texture map. In these cases, the texel values used depend on whether GL_REPEAT or GL_CLAMP is in effect and whether you've assigned a border for the texture. (See "Using a Texture's Borders.") GL_NEAREST requires less computation than GL_LINEAR and therefore might execute more quickly, but GL_LINEAR provides smoother results.

Clamping in OpenGL

Search for: Clamping in OpenGL

This is a reasonable article

when the texture coordinate is larger than 1, say 1.3, Just use the 0.3 as your new coordinate. The result of this is you end up redrawing the figure in that direction. You can apply this in both directions resulting in copying the texture in multiple directions.

Clamp Now

In contrast when you clamp when the texture coordinate is 1.3, it will set it to 1. Essentially saying repeat the last texel for the rest of the picture.

Another nice place to understand OpenGL in the context of JOGL

Here is how glTexEvn function works

This parameter allows you to either use the texture colors only, or blend with the original color, or modulate with the original color


GL_DECAL, 
GL_REPLACE, 
GL_MODULATE, 
or GL_BLEND

Ofcourse use the texture color

Modulate the texture color with the base color behavior of the pixel due to its current lighting affects: say should it be darker or paler etc. You typically multiply the texel color with the pixel color.

the texture color is blended with pixel(fragment) color in a ratio decided by the texel's alpha. If the texel's alpha is full color, then it is essentially same as REPLACE

How does GL_BLEND work

Search for: How does GL_BLEND work

A possibly good explanation of blend function

blending could be in affect with or without textures. Blending takes the foreground colors (may be called destination colors!!) and blends them based on a function you specify with the background (using depth) colors and the resulting color will be the final color.

Now in the context of a texture, use the texel color as your foreground color and use the pixel as the background color and blend them using the blend function that is in play.

why the target must be GL_TEXTURE_ENV?

Search for: why the target must be GL_TEXTURE_ENV?

Apparently this may be an ES restriction


GL_TEXTURE_ENV, 
GL_TEXTURE_FILTER_CONTROL or 
GL_POINT_SPRITE

what is a texture unit glActivetexture

Search for: what is a texture unit glActivetexture

I may have to read this multitexturing article to understand how active texture works

OpenGL environment can have multiple number of texture units. You can discover how many texture units there are at run time. The base texture unit is always called GL_TEXTURE0. Each texture unit has its own

Texture environment
Texture matrix
Texture coordinates
filter modes
clamping behavior

You can have multiple texture units active at the same time or you can control which texture unit is active.


Texture Unit
Texture Target for a given unit
Texture name (or ID) bound to a texture target

glUniform1i active texture

Search for: glUniform1i active texture


//Set your actual texture to base texture
//This is also default, so you can omit if felt
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

//For the given texture unit, set the 
//texture target (2D) and the texture name (ID)
//This is needed
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);

//For the texture sampler in GLSL set which
//texture unit to use. This is also the default.
//You can omit if needed.
//First arg: handler to the sampler uniform variable
//Second arg: An integer identifying the texture unit
//0 refers to teh GL_TEXTURE0 and 1 for GL_TEXTURE1 etc
GLES20.glUniform1i(mu2DSamplerTexture, 0);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
GLES20.glUniform1i(mu2DSamplerTexture, 0);

This is another good discussion on texture units

Here is an IOS tutorial talking about textrues

Can my texture coordinate be greater than 1?

Search for: Can my texture coordinate be greater than 1?

See this article from MSDN

An unstated detail is the size of the object and the paper. OpenGL uses a normalized approach to resolve this. OpenGL assumes that the paper is always a 1 x 1 square with its origin at (0,0) and the top right corner is at (1,1). Then OpenGL wants you to shrink your object surface so that it fits within these 1 x 1 boundaries. So the burden is on the programmer to figure out the vertices of the object surface in a 1 x 1 square. These coordinates of the object on a texture image are called texture coordinates.

When you want the texture image to take the entire space of the object surface you make sure that the object vertices all lie with in the (0,0) to (1,1) of texture coordinates. You can however, specify the texture coordinate of a particular vertex to be greater than "1" either in the "x" direction or the "y" direction. In this case you need to tell OpenGL how to map the space that is outside of (0,0) and (1,1). This is called the wrapping mode.

In one wrapping mode you can tell OpenGL to repeat the texture Image every time it crosses the boundary of 0 to 1. So If you say 1.4 as your vertex's texture coordinate, then your texel will the 40th percentile from the beginning. If your vertex is at 2.4, then your texel that gets painted at this vertex will be the 40th percentile texel again from the beginning (from 0). So you would have painted In the given direction the texture image twice and then a 4th of it by the time you get to the vertex that is at 2.4. This Is a very round about way of saying repeat the image until you run out of space. This is further explained below when we talk about wrapping modes in the context of the specific APIs.