Top Banner
OpenGL Texturing
59

OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Dec 19, 2015

Download

Documents

Myra Morgan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

OpenGL Texturing

Page 2: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Content

Texture targetTexture environmentTexture coordinateTexture parameter (filters, wrap)Texture objectTexture transformationTexGen

MultitextureApplications

Light map 3D textures Projective texture Environment map Specular map

2Fall 2013 Revised

Page 3: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Steps in OpenGL Texture Mapping

Specify (create) the texture Set texture parameters: Indicate how the texture is to be applied to

each pixel

Enable texture mappingDraw the scene, supplying both texture and geometric coordinates

Works only in RGB mode

3Fall 2013 Revised

Page 4: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture TargetsglTexImage1D()glTexImage2D()glTexImage3D()

                                                         

4Fall 2013 Revised

Page 5: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Two Dimensional Texture

glTexImage2D (GLenum target, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) target : GL_TEXTURE_2D level : LOD number (0: base image) components : number of color components

(1|2|3|4) Format : pixel data format Border: 0 or 1 Width & height

2m + 2(border bit) w and h can be different

There are new extensions that removes this restriction

5Fall 2013 Revised

Page 6: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

glTexImage*D supplement

In GL version 1.1 or greater, pixels may be a null pointer. In this case texture memory is allocated to accommodate a texture of width width and height height. You can then download subtextures to initialize this texture memory. The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive.

6Fall 2013 Revised

Page 7: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Setting Texture Environment

glTexEnv{if}{v}(GLenum target, GLenum pname, TYPEparam); Setting how textures are to be interpreted: Target : GL_TEXTURE_ENV Pname: GL_TEXTURE_ENV_MODE Param: modes (DECAL|REPLACE|

MODULATE|BLEND|ADD|…)

7Fall 2013 Revised

Page 8: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Environment Modes

GL_REPLACEGL_MODULATE (default)GL_DECALGL_BLEND

New environment modes:GL_ADD: Cv = Cf + Ct

GL_COMBINE (ARB, see here)

8Fall 2013 Revised

Page 9: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

GL_MODULATE

Color of polygon affects the display of texture

Tree: (r,g,b,a) acutout = 0Polygon: (1,0,0) 9Fall 2013 Revised

Page 10: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

GL_BLEND

Use with:glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, colorPtr)

Cc: texture environment color

10Fall 2013 Revised

Page 11: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

GL_REPLACE

Appearance solely determined by texture

FOR TEXTURE CUT-OUTS

Tree: (r,g,b,a) acutout = 0Polygon: (1,0,0) 11Fall 2013 Revised

Page 12: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

GL_DECAL

Cp: replace

RGB: DECAL=REPLACETree: (r,g,b,a) acutout = 0Polygon: (1,0,0) 12Fall 2013 Revised

Page 13: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture + Lighting

To show fragment color, use GL_MODULATEApply specular color AFTER texture mapping:

glLightModeli (GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);

See FAQ 21.040

GL_SINGLE_COLOR

GL_SEPARATE_SPECULAR_COLOR

13Fall 2013 Revised

Page 14: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Coordinates

Texture coordinate: Associate texture location (in texture

space) with vertices in the polygonglTexCoord2i (s, t); glVertex2i (x, y);

Order cannot be reversed![Think of TexCoord as state

assignment]

14Fall 2013 Revised

Page 15: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Coordinates of Quadrics

Most quadric primitives have default setting for texture coordinatesTo turn on the default setting: gluQuadricTexture

(qobj, GL_TRUE)

15Fall 2013 Revised

Page 16: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Ex: Textures on Quadrics

16Fall 2013 Revised

Page 17: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Deeper Look into Texturing

17

Each fragment got its texture coordinates from interpolationThen via table lookup, obtain its (interpolated) color values.

Fall 2013 Revised

Page 18: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

rasterization

Polygon (in screen space) and texture

coordinates

(1,1)

(1,0)

(0,.75) (1,1)

(1,0)

(0,.75)

Texture map (4x4)

nearestlinear

Interpolate (s,t) and lookup (r,g,b,a) in the texture map

Filters

Texture Access and Lookup

18Fall 2013 Revised

Page 19: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Filters: Magnification & Minification

Nature of problem: Mismatch between texels and pixels

1

Pixels

TexelsPixels

Magnification

Minification

19Fall 2013 Revised

Page 20: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Options

MagnificationGL_NEARESTGL_LINEAR

MinificationGL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEARESTGL_LINEAR_MIPMAP_NEARESTGL_NEAREST_MIPMAP_LINEARGL_LINEAR_MIPMAP_LINEAR

Chooses the mipmap that most closely matches the size of the pixel being textured

Chooses the two mipmaps that most closely match the size of the pixel being textured

Nearest: in Manhattan distance to the center of the pixel

20Fall 2013 Revised

Page 21: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Magnification OptionsGL_LINEAR Interpolate the

texels More time

consuming but more accurate

GL_NEAREST Snap to the

nearest texel

21Fall 2013 Revised

Page 22: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Problem with Minification

without mipmap with mipmap

23Fall 2013 Revised

Page 23: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

MipmappingThe most popular method of anti-aliasing for textures‘Mip’ stands for “multum in parvo” = “many things in a

small place”The texture is downsampled to a quarter of the original

area.

2m2n

m, n∈+

2m-12n-1

d = the Level Of Detail (LOD)

Level 0

Level 1

Level 2

Level 3

Level 0 Texture

If a pixel covers2222 texels,go to level 2

and get a texel.2121,level 1

Mipmapping was invented by Lance Williams in 1983 and is described in his paper Pyramidal parametrics.

24Fall 2013 Revised

Page 24: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Minification OptionsglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR,

GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_NEAREST);

Remarks: Mipmap selection is done per pixel

Using not-yet-ready mipmap disables the texture mapping function.

Remarks: Mipmap selection is done per pixel

Using not-yet-ready mipmap disables the texture mapping function.

26Fall 2013 Revised

Page 25: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Mipmap Generation

gluBuild2DMipmaps

Storage overhead

3

4

1

11

414

141

2

27Fall 2013 Revised

Page 26: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Wrap

When the texture coordinates are outside [0,1]glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,param);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,param); param: GL_CLAMP|GL_REPEAT

28Fall 2013 Revised

Page 27: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Parameters for Wrapping

GL_CLAMP use the border

texel Diagonal: corner

texel

GL_REPEAT repeat the texels

in [0,1]

29Fall 2013 Revised

Page 28: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Object

A texture object stores texture data and makes it readily availableYou can now control many textures and go back to textures that have been previously loaded into your texture resourcesUsing texture objects is usually the fastest way to apply textures, resulting in big performance gains

it is almost always much faster to bind (reuse) an existing texture object than it is to reload a texture image using glTexImage*D()

30Fall 2013 Revised

Page 29: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Object API

glGenTextures Allocate n textures (integer identifiers)

glBindTexture Bind the current texture to specified identifier

glDeleteTextures Free the allocated texture identifiers Reverse of glGenTextures

31Fall 2013 Revised

Page 30: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

checker.c

32Fall 2013 Revised

Page 31: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

texbind.c

Once created, a named texture may be re-bound to the target of the matching dimensionality as often as needed. It is usually much faster to use glBindTexture to bind an existing named texture to one of the texture targets than it is to reload the texture image using glTexImage1D, glTexImage2D, or glTexImage3D.

33Fall 2013 Revised

Page 32: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Objects

Texture properties saved in texture objects: Minification and magnification filters, wrapping

modes, border color and texture priority

When a texture object is bound again, one may edit the contents of the bound texture object. Any commands that change the above properties will change the currently bound texture as well as the current texture state.Texture environment, texgen, etc. are NOT stored in texture objects These settings need to be repeated after texture

binding.

34Fall 2013 Revised

Page 33: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Loader (PNG, glpng.rar)

Features Build mipmap Load and bind 2D textures Load image data (image loader) Handle transparent image (cut-out texture)

Important API id = pngBind(filename, mipmap, trans, info, wrapst, minfilter, magfilter)

pngSetStencil(red, green, blue) pngSetStandardOrientation(1)

OpenGL: origin (0,0) is at lower left corner pngLoad(filename, mipmap, trans, info)

TEXTURE ID IS RETURNED TO YOU

BY PNG LOADER

35Fall 2013 Revised

Page 34: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Transforms

Texture coordinates are multiplied by a 4 by 4 matrix before any texture mapping occurs.Texture animation: using this, you can make the texture slide over the surface, rotate around it, stretch and shrink, …All matrix operations apply: Push/Pop/Mult/…

36Fall 2013 Revised

Page 35: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Texture Suite

cloud

explodesmoke

waterfire 37Fall 2013 Revised

Page 36: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Automatic TEXture Coordinate GENeration

void glTexGen{ifd}{v}(GLenum coord, GLenum pname, TYPEparam);

Coord: GL_S, GL_T, GL_R, GL_Q Pname: GL_TEXTURE_GEN_MODE Type: GL_OBJECT_LINEAR, GL_EYE_LINEAR, or

GL_SPHERE_MAP

Used when the tex coords are too cumbersome to specify, or will be changed with time.Texture environment, texgen, etc. are NOT stored in texture objects.

38Fall 2013 Revised

Page 37: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

1D Texture According to Height

Continuous color gradation(0,1/32,2/32, …, 31/32)

Discrete white lines(0,0,0,1,0,0,0,1,…)

39Fall 2013 Revised

Page 38: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

TexGen: Object/Eye_Linearfor a vertex with object coordinates (xo,yo,zo,wo), generated coordinate = p1xo + p2yo + p3zo + p4wo

Meaning: when p1, p2, p3 are normalized, this coord corresponds to signed distanceSimilarly, EYE_LINEAR applies to eye coordinates

(0,0,-1,0)(xo,yo,zo,wo)

Initially, all texture generation functions are set to GL_EYE_LINEAR and are disabled. Both s plane equations are (1, 0, 0, 0), both t plane equations are (0, 1, 0, 0), and all r and q plane equations are (0, 0, 0, 0). 40Fall 2013 Revised

Page 39: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Eye_linear (ref)

Computing this distance in eye coordinates is a little tricky, since the plane and vertex must first be transformed to eye coordinates. if M is the modelview matrix at the time glTexGenfv( GLX, GL EYE PLANE, plane ) is called, then the transformed vertex V’ is V’ = MV, let’s call V’ = (xe, ye, ze, we). The plane must also be transformed to get a new plane A’x + B’y + C’z + D’w = 0 We get (A’,B’,C’,D’) = (A,B,C,D)M−1 and M is the modelview matrix when glTexGen is invoked. The texture coordinate is then computed as A’xe + B’ye + C’ze + D’we.

41Fall 2013 Revised

Page 40: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Eye_linear Summary

e

e

e

e

o

o

o

o

e

e

e

e

w

z

y

x

dcbatex

Mdcbadcba

M

w

z

y

x

M

w

z

y

x

coord

matrix modelview:,

1

42Fall 2013 Revised

Page 41: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

SphereMap

Another mode of TexGen, SphereMap, will be explain in the “Environment Map” note.

43Fall 2013 Revised

Page 42: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Multi-Texture

What this is about … Extension Wrangler (GLEW)

44Fall 2013 Revised

Page 43: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Glew – Extension WranglerInclude <gl/glew.h> before <gl/glut.h>Remember to add glewInit() After the first glutWindow is created

Page 44: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Error MessageVersion of glew library Sometimes you’ll see this error message

when you run the program It is because the static library (in the EXE)

and the DLL files are not the same version. Recompiling the application solves the

problem

Page 45: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Usages of Multitexture

47Fall 2013 Revised

Page 46: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

(First) ARB-approved extension (1998)Support up to 32 texture units*Texture matrix and texgen commands affect the active texture environment

ARB_multitexture (ref)

* glGetIntegerv (GL_MAX_TEXTURE_UNITS, &units); My platform has only 4! OpenGL 3.1 requires minimum of 16 texture units48Fall 2013 Revised

Page 47: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Details

OpenGL's multitexture support requires that every texture unit be fully functional and maintain state that is independent of any other texture units. Each texture unit has its own texture coordinate generation state, texture matrix state, texture enable state, and texture environment state. However, each texture unit within an OpenGL context shares the same set of texture objects.

49Fall 2013 Revised

Page 48: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Multi Texture

50Fall 2013 Revised

Page 49: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

TexGen with MultitextureWhen the GL_ARB_multitexture extension is supported, glTexGen set the texture generation parameters for the currently active texture unit, selected with glActiveTexture.

TexUnit0:texture2D;(s,t) given

TexUnit1:texture1D; texGen

51Fall 2013 Revised

Page 50: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Dynamic Texture

CopyTexSubImage2DReplace a rectangular portion of 2D texture with pixels from the current READ_BUFFERThis involves a read-back from GPU to CPU.

Can you think of a way to move the dynamic texture

around?!

A better way of doing it: Render-to-texture (RTT) with framebuffer object (FBO)

52Fall 2013 Revised

Page 51: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

glTexSubImage2D

Replace part of the original image as new subimageWidth and size of image need not be 2n

E.g., dynamic texture

glTexSubImage2D (target, level, xoffset, yoffset, width, height, format, type, pixels)

glTexSubImage2D (target, level, xoffset, yoffset, width, height, format, type, pixels)

53Fall 2013 Revised

Page 52: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Other Related Functions

void glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);

defines a two-dimensional texture image with pixels from the current GL_READ_BUFFER.

void glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height); doc

replaces a rectangular portion of a two-dimensional texture image with pixels from the current GL_READ_BUFFER (rather than from main memory, as is the case for glTexSubImage2D).

void glGetTexImage(target, level, format, type, void *pixels );

Get the content in the texture target into the array

Similar to glCopyPixels

54Fall 2013 Revised

Page 53: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

3D Textures

55Fall 2013 Revised

Page 54: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

3D Perlin noise …

56Fall 2013 Revised

Page 55: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Fall 2013 Revised 57

Light MapsQuake (雷神之錘 , id software) was the first computer game to use light map

Page 56: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Fall 2013 Revised 58

Page 57: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

Spherical specular map obtained on a highly tesselated sphere`

Apply this spheremap as “specular texture” on the teapot

Specular Map

59Fall 2013 Revised

Page 58: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

A more “cartoon-ish” specular map

Fall 2013 Revised 60

Page 59: OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation.

61Fall 2013 Revised