36
OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab Korea Univ. Computer Graphics Lab

OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Embed Size (px)

Citation preview

Page 1: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

OpenGL Programming Guide : Texture Mapping

2004. 02. 10. Yoo jin wook2004. 02. 10. Yoo jin wookKorea Univ. Computer Graphics LabKorea Univ. Computer Graphics Lab

Page 2: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Mapping ( 1/3 )Texture Mapping ( 1/3 )

Texture Simply rectangular arrays of data

ex) Color data, luminance data, color & alpha data

Texel Individual values in a texture arraycf) texel ≠Pixel requires filtering

Page 3: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Mapping ( 2/3 )Texture Mapping ( 2/3 )

Page 4: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Mapping ( 3/3 )Texture Mapping ( 3/3 )

An Overview & an Example Specifying the Texture Filtering Texture Objects Texture Functions Assigning Texture Coordinates Automatic Texture-Coordinate Generation Advanced Feature (Multitexturing)

Page 5: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

An Overview & an Example An Overview & an Example ( 1/2 )( 1/2 )

Steps in Texture Mapping1. Create object & Specify a texture2. Indicate how the texture is to be applied to each pixel3. Enable Texture mapping4. Draw the scene

Page 6: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

An Overview & an Example An Overview & an Example ( 2/2 )( 2/2 )

Example 9-1

Page 7: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Specifying the Texture ( 1/3 )Specifying the Texture ( 1/3 )

target : GL_TEXTURE_2D, GL_PROXY_TEXTURE_2DinternalFormat : 1~4 (R,G,B,A)Format : GL_RGB, GL_RGBA, GL_RED ….

void glTexImage2D (GLenum target, GLint level, GLint internalFormat, GLsizei width,

GLsizei height, GLint border, GLenum format, GLenum type,

const GLvoid *texels)

Page 8: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Specifying the Texture ( 2/3 )Specifying the Texture ( 2/3 )

void gluScaleImage (GLenum format, GLint widthin, GLint heightin, GLenum typein,

const void *datain, GLint widthout, GLint heightout, GLenum typeout, void *dataout)

Page 9: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Specifying the Texture ( 3/3 )Specifying the Texture ( 3/3 )

: Create a two-dimensional texture using framebuffer

void glCopyTexImage2D (GLenum target, GLint level, GLint internalFormat, GLint x,

GLint y, GLsizei width, Glsizei height GLint border)

Page 10: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Proxy ( 1/3 )Texture Proxy ( 1/3 )

Texture Proxy: answers the question of whether a texture is capable of being loaded into texture memory

Steps in Texture Proxy1. Create texture proxy with glTexImage2D( )2. Query the texture state variables with glGetTexLevelPar

ameter*( )

Page 11: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Proxy ( 2/3 )Texture Proxy ( 2/3 )

pname : GL_TEXTURE_WIDTH, GL_TEXTURE_DEPTH GL_TEXTURE_HEIGHT…..

void glGetTexLevelParameter*v(GLenum target, GLint level, GLenum pname, TYPE*params)

Page 12: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Proxy ( 3/3 )Texture Proxy ( 3/3 )

Ex) Glint width;

glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA8, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);

Page 13: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Replacing a Texture Image Replacing a Texture Image ( 1/2 )( 1/2 )

xoffset, yoffset : texel offset in the x-,y- direction with (0,0) at the lower left corner of the t

exture

void glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width,

GLsizei height, GLenum format, GLenum type, const GLvoid *texels)

Page 14: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Replacing a Texture Image Replacing a Texture Image ( 2/2 )( 2/2 )

Example 9-3

Page 15: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multiple Levels of Detail ( 1/4 )Multiple Levels of Detail ( 1/4 )

Mipmap: series of prefiltered texture maps of decreasing resolution

s

->openGL automatically determines which texture map to use based on the size(in pixels)

->must provide all sizes of your texture in powers of 2 between the largest size and 1×1 mapex) 64×32 : 32×8, 16×4, 8×2, 4×1, 2×1, 1×1

Page 16: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multiple Levels of Detail ( 2/4 )Multiple Levels of Detail ( 2/4 )

Example 9-5

Page 17: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multiple Levels of Detail ( 3/4 )Multiple Levels of Detail ( 3/4 )

Automated Mipmap Generation: define the pyramid of mipmaps down to a resolution 1×1

void gluBuild3DMipmaps(GLenum target, GLint internalFormat, GLint width, GLint height, GLint depth, GLenum format,Glenum type,void *texels)

Page 18: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multiple Levels of Detail ( 4/4 )Multiple Levels of Detail ( 4/4 )

Advanced Mipmap Details

parameter Description Values

GL_TEXTURE_BASE_LEVEL

level for highest-resolution texture in use

non-negative integer

GL_TEXTURE_MAX_LEVELlevel for smallest-resolution texture in use

non-negative integer

GL_TEXTURE_MIN_LOD** minimum value for λ any value

GL_TEXTURE_MAX_LOD** maximum value for λ any value

glTexParameter*( )

** : openGL 1.2

Page 19: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Filtering ( 1/2 )Filtering ( 1/2 )

Page 20: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Filtering ( 2/2 )Filtering ( 2/2 )

parameter Values

GL_TEXTURE_MAG_FILTER GL_NEAREST or GL_LINEAR

GL_TEXTURE_MIN_FILTER GL_NEAREST, GL_LINEARGL_NEAREST_MIPMAP_NEARESTGL_NEAREST_MIPMAP_LINEARGL_LINEAR_MIPMAP_NEARESTGL_LINEAR_MIPMAP_LINEAR

glTexParameter*( )

Page 21: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Objects ( 1/4 )Texture Objects ( 1/4 )

Steps1. Generate texture names2. Initially bind texture objects to texture data3. See if you have enough space for all your

texture object4. Bind and rebind texture objects

Page 22: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Objects ( 2/4 )Texture Objects ( 2/4 )

Naming A texture Object

void glGenTextures (GLsizei n, GLuint *textureNames) Glboolean glIsTexture (GLuint textureName)

Page 23: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Objects ( 3/4 )Texture Objects ( 3/4 )

Creating and Using Texture Objects

void glBindTexture (GLenum target, Gluint textureNames)void glDeleteTexture (GLsizei n,

const Gluint *textureNames)

Page 24: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Objects ( 4/4 )Texture Objects ( 4/4 )

A Working Set of Resident Textures: queries the texture residence status of the n texture object

void glAreTexturesResident ( GLsizei n, const Gluint *textureNames, Glboolean *residen

ces)

Page 25: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Functions ( 1/3 )Texture Functions ( 1/3 )

target : GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COLOR

void glTexEnv* (GLenum target, GLenum pname, TYPE param)

void glTexEnv*v (GLenum target, GLenum pname, TYPE *param)

Page 26: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Functions ( 2/3 )Texture Functions ( 2/3 )

pname param

GL_TEXTURE_ENV_MODE GL_DECALGL_REPLACEGL_MODULATEGL_BLEND

GL_TEXTURE_ENV_COLOR** Four floating value (R,G,B,A)

** : used only if the GL_BLEND texture function has been specified

Page 27: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Texture Functions ( 3/3 )Texture Functions ( 3/3 )

< GL_DECAL ≈ GL_REPLACE>

< GL_MODULATE >

Page 28: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Assigning Texture Coordinates Assigning Texture Coordinates ( 1/3 )( 1/3 )

Page 29: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Assigning Texture Coordinates Assigning Texture Coordinates ( 2/3 )( 2/3 )

Page 30: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Assigning Texture Coordinates Assigning Texture Coordinates ( 3/3 )( 3/3 )

Repeat & ClampglTexImage2D(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,

GL_REPEAT/GL_CLAMP); glTexImage2D(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,

GL_REPEAT/GL_CLAMP);

Page 31: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Automatic Texture-CoordinateAutomatic Texture-CoordinateGeneration ( 1/2 )Generation ( 1/2 )

coord : GL_S, GL_T, GL_R, GL_Qpname : GL_TEXTURE_GEN_MODE, GL_OBJECT_PLANE, GL_EYE_PLANEparam : GL_OBJECT_LINEAR, GL_EYE_LINEAR, GL_SHPERE_MAP or an array of te

xture generation parameters

void glTexGen* (GLenum coord, GLenum pname, TYPE param)

Page 32: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Automatic Texture-CoordinateAutomatic Texture-CoordinateGeneration ( 2/2 )Generation ( 2/2 )

<eye coordinate> <object coordinate> <slanted coordinate>

Page 33: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multitexturing ( 1/4 )Multitexturing ( 1/4 )

Page 34: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multitexturing ( 2/4 )Multitexturing ( 2/4 ) Steps

1. Check to see if multitexturing is supported- glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, ….)

2. For each texturing unit, establish the texture state- glActiveTextureARB()

3. During vertex specification, use glMultiTexCoord*ARB()

Page 35: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multitexturing ( 3/4 )Multitexturing ( 3/4 ) Establishing Texture Unit

GLuint texNames[2]; …………………………….

glActiveTextureARB(GL_TEXTURE0_ARB);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texNames[0]);…………………………….. glActiveTextureARB(GL_TEXTURE1_ARB);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texNames[1]);……………………………..

Page 36: OpenGL Programming Guide : Texture Mapping 2004. 02. 10. Yoo jin wook Korea Univ. Computer Graphics Lab

Multitexturing ( 4/4 )Multitexturing ( 4/4 ) Specifying

glBegin(GL_TRIANGLES);glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);glVertex2f(0.0, 0.0);glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.5, 1.0);glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.5, 0.0);glVertex2f(50.0, 100.0);glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);glVertex2f(100.0, 0.0)glEnd();