OpenGL L05-Texturing

62
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2014 OpenGL Graphics L05-Texturing

description

OpenGL L05-Texturing

Transcript of OpenGL L05-Texturing

Page 1: OpenGL L05-Texturing

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2014

OpenGL Graphics

L05-Texturing

Page 2: OpenGL L05-Texturing

Texturing

Page 3: OpenGL L05-Texturing

that appears in a video game needs to be textured; this includes everything from plants

to people. If things aren’t textured well, your game just won’t look right.

Page 4: OpenGL L05-Texturing

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 5: OpenGL L05-Texturing

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 6: OpenGL L05-Texturing

How to use a Texture?

• How to use a Texture?

1. Load the texture

2. Map it into a polygon

3. Draw the polygon

Page 7: OpenGL L05-Texturing

How to use a Texture?

• How to use a Texture?

1. Load the texture

2. Map it into a polygon

3. Draw the polygon

• How to use a Texture? (OpenGL)1. Specify textures in texture objects

2. Set texture filter

3. Set texture function

4. Set texture wrap mode

5. Set optional perspective correction hint

6. Bind texture object

7. Enable texturing

8. Supply texture coordinates for vertex

• How OpenGL store images?– One image per texture object

– May be shared by several graphics contexts

Page 8: OpenGL L05-Texturing

Textures Coordinates

Page 9: OpenGL L05-Texturing

Textures Coordinates

Page 10: OpenGL L05-Texturing

Texture Mapping

s

t

x

y

z

image

geometry

screen

Page 11: OpenGL L05-Texturing

Textures Coordinates

Page 12: OpenGL L05-Texturing

Textures Coordinates

Page 13: OpenGL L05-Texturing

Textures Coordinates

s

t

1, 10, 1

0, 0 1, 0

(s, t) = (0.2, 0.8)

(0.4, 0.2)

(0.8, 0.4)

A

B C

a

b

c

Texture Space Object Space

Page 14: OpenGL L05-Texturing

Textures Coordinates

Page 15: OpenGL L05-Texturing

Texturing Functions

Page 16: OpenGL L05-Texturing

Texturing Functions

• Generate texture names

glGenTextures(n, *texIds);

• Create texture objects with texture data and state

glBindTexture(target, id);

• Bind textures before using

glBindTexture(target, id);

• Define a texture image from an array of texels in CPU memory

glTexImage2D(target, level, components, w, h, border, format, type,

*texels);

• Texel colors are processed by pixel pipeline

– pixel scales, biases and lookups can be done

Page 17: OpenGL L05-Texturing

Filter Modes

• Filter modes control how pixels are minified or magnified. Generally a color is

computed using the nearest texel or by a linear average of several texels.

• The filter type, above is one of GL_TEXTURE_MIN_FILTER or GL_TEXTURE_MAG_FILTER.

• The mode is one of GL_NEAREST, GL_LINEAR, or special modes for mipmapping.

Mipmapping modes are used for minification only, and have values of:

– GL_NEAREST_MIPMAP_NEAREST

– GL_NEAREST_MIPMAP_LINEAR

– GL_LINEAR_MIPMAP_NEAREST

– GL_LINEAR_MIPMAP_LINEAR

Page 18: OpenGL L05-Texturing

Filter Modes

• Example: glTexParameteri(target, type, mode);

Texture Polygon

Magnification Minification

PolygonTexture

Page 19: OpenGL L05-Texturing

Wrapping Mode

• Wrap mode determines what should happen if a texture coordinate lies outside of the [0,1] range.

• If the GL_REPEAT wrap mode is used, for texture coordinate values less than zero or greater than

one, the integer is ignored and only the fractional value is used.

• If the GL_CLAMP wrap mode is used, the texture value at the extreme (either 0 or 1) is used.

• Example:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

textureGL_REPEATwrapping

GL_CLAMPwrapping

s

t

Page 20: OpenGL L05-Texturing

Texture Parameters

Read more: https://open.gl/textures

Page 21: OpenGL L05-Texturing

Body Color When Textured

• glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

– resultColor = Body Color + Texture Color [By default]

• glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

– resultColor = Texture Color

• glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

– resultColor = Texture Color + Blending

• GLfloat fColor[4] = { 0.0f, 0.0f, 0.0f, 1f };

• glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, fColor);

Page 22: OpenGL L05-Texturing

TexturingSomething to pay attention for

Page 23: OpenGL L05-Texturing

Something to pay attention for

• The image must be 24 bit (RGB)

• The image extension should be .bmp

• The image dimensions must be a power of 2 (2, 4, 8, 16, 32, 64, ..etc.)

– If dimensions of image are not power of 2 then scale:

• gluScaleImage(format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out);

*_in is for source image

*_out is for destination image

• White color should be assigned before drawing a textured object by setting:

glColor3f(1,1,1);

• You should call: glBindTexture(GL_TEXTURE_2D, textureID) between glBegin() and

glEnd()

Page 24: OpenGL L05-Texturing

Texturing Example

Page 25: OpenGL L05-Texturing

Texturing Example

• #include "texture.h”

• int imageTexId;

• InitGL()

– imageTexId = LoadTexture("images//tile.bmp");

• DrawGLScene()

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, imageTexId);

glScalef(5,5,1);

glBegin(GL_QUADS);

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

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

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

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

glEnd();

glDisable(GL_TEXTURE_2D);

Page 26: OpenGL L05-Texturing

Texturing Example

• #include "texture.h”

• int imageTexId;

• InitGL()

– imageTexId = LoadTexture("images//tile.bmp");

• DrawGLScene()

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, imageTexId);

glScalef(5,5,1);

glBegin(GL_QUADS);

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

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

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

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

glEnd();

glDisable(GL_TEXTURE_2D);

Page 27: OpenGL L05-Texturing

Texturing Example

• #include "texture.h”

• int imageTexId;

• InitGL()

– imageTexId = LoadTexture("images//tile.bmp");

• DrawGLScene()

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, imageTexId);

glScalef(5,5,1);

glBegin(GL_QUADS);

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

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

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

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

glEnd();

glDisable(GL_TEXTURE_2D);

Page 28: OpenGL L05-Texturing

Texturing Example

• #include "texture.h”

• int imageTexId;

• InitGL()

– imageTexId = LoadTexture("images//tile.bmp");

• DrawGLScene()

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, imageTexId);

glScalef(5,5,1);

glBegin(GL_QUADS);

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

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

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

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

glEnd();

glDisable(GL_TEXTURE_2D);

Page 29: OpenGL L05-Texturing

gluQuadricTexture

Page 30: OpenGL L05-Texturing

gluQuadricTexture

+

Page 31: OpenGL L05-Texturing

gluQuadricTexture

+ =

Page 32: OpenGL L05-Texturing

gluQuadricTexture

Page 33: OpenGL L05-Texturing

Blending

Page 34: OpenGL L05-Texturing

Blending

• You can blend objects using:

– glBlendFunc(GLenum sfactor, GLenum dfactor);

• sfactor: is the source blend factor

• dfactor: is the destination blend factor

• Transparency is implemented using:

– “GL_SRC_ALPHA” for the source

– “GL_ONE_MINUS_SRC_ALPHA” for the destination

Page 35: OpenGL L05-Texturing

Blending

• You can blend objects using:

– glBlendFunc(GLenum sfactor, GLenum dfactor);

• sfactor: is the source blend factor

• dfactor: is the destination blend factor

• Transparency is implemented using:

– “GL_SRC_ALPHA” for the source

– “GL_ONE_MINUS_SRC_ALPHA” for the destination

Page 36: OpenGL L05-Texturing

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 37: OpenGL L05-Texturing

OpenGL Architecture

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

OperationsFrameBuffer

TextureMemory

CPU

PixelOperations

Page 38: OpenGL L05-Texturing

Blending

• Combine pixels with what’s in already in the framebuffer

glBlendFunc(src, dst )

FramebufferPixel (dst)

BlendingEquation

Fragment(src)

BlendedPixel

pfr CdstCsrcC

Page 39: OpenGL L05-Texturing

Transparency with .tga (32 bit)

Page 40: OpenGL L05-Texturing

Transparency with .tga

+ =

Page 41: OpenGL L05-Texturing

Transparency with .tga

+ =

One .tga File

Page 42: OpenGL L05-Texturing

Add Transparency with .tga

• #include "tgaLoader.h”

• TGAImage grassImage;

• InitGL ()

– glEnable(GL_TEXTURE_2D);

– grassImage = LoadTGA(“images//grass.tga");

• DrawGLScene ()

glBindTexture(GL_TEXTURE_2D , grassImage.texID);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);

glBegin(GL_QUADS);

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

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

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

glTexCoord2f(0,0); glVertex3f(-5,-5,0);

glEnd();

glDisable(GL_BLEND);

Page 43: OpenGL L05-Texturing

Add Transparency with .tga

• #include "tgaLoader.h”

• TGAImage grassImage;

• InitGL ()

– glEnable(GL_TEXTURE_2D);

– grassImage = LoadTGA(“images//grass.tga");

• DrawGLScene ()

glBindTexture(GL_TEXTURE_2D , grassImage.texID);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);

glBegin(GL_QUADS);

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

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

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

glTexCoord2f(0,0); glVertex3f(-5,-5,0);

glEnd();

glDisable(GL_BLEND);

Page 44: OpenGL L05-Texturing

Add Transparency with .tga

• #include "tgaLoader.h”

• TGAImage grassImage;

• InitGL ()

– glEnable(GL_TEXTURE_2D);

– grassImage = LoadTGA(“images//grass.tga");

• DrawGLScene ()

glBindTexture(GL_TEXTURE_2D , grassImage.texID);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);

glBegin(GL_QUADS);

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

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

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

glTexCoord2f(0,0); glVertex3f(-5,-5,0);

glEnd();

glDisable(GL_BLEND);

Page 45: OpenGL L05-Texturing

Add Transparency with .tga

Page 46: OpenGL L05-Texturing

Transparency with .tga (32 bit)Something to pay attention for, again

Page 47: OpenGL L05-Texturing

Something to pay attention for, again

• The image must be 24 bit (RGB)

• The image extension should be .bmp

• The image dimensions must be a power of 2 (2, 4, 8, 16, 32, 64, ..etc.) or scale.

• White color should be assigned before drawing a textured object by setting:

glColor3f(1,1,1);

• You should call: glBindTexture(GL_TEXTURE_2D, textureID) between

glBegin() and glEnd()

• Additional stuff when you use transparency with .tga

– The image must be 32 bit (RGB)

– The image extension should be .tga

– In InitGL(), You must load .tga files LAST

– In DrawGLScene(), you must draw the transparent objects LAST

Page 48: OpenGL L05-Texturing

Texture Tilling

Page 49: OpenGL L05-Texturing

Texture TillingTiling is a very simple effect that creates a repeating pattern of

an image on the surface of a primitive object

Page 50: OpenGL L05-Texturing

Texture Tilling

Using a small image to cover a large surface makes tiling a useful way to increase the performance of your textures and decrease the size of your image files.

Page 51: OpenGL L05-Texturing

Texture Tilling, How toSimply set the texture coordinates larger than 1

as many time as you want the texture to be tilled

Page 52: OpenGL L05-Texturing

Billboarding

Page 53: OpenGL L05-Texturing

Billboarding

Page 54: OpenGL L05-Texturing

Mipmapping

Page 55: OpenGL L05-Texturing

Mipmappingmultum in parvo (mip): many things in a small place

Page 56: OpenGL L05-Texturing

Mipmapped Textures

• Mipmap allows for prefiltered texture maps of decreasing resolutions

• Lessens interpolation errors for smaller textured objects

• Declare mipmap level during texture definition

glTexImage*D(GL_TEXTURE_*D, level, …)

• GLU mipmap builder routines

gluBuild*DMipmaps()

Page 57: OpenGL L05-Texturing

2D Texture and 3D Texture?http://gamedev.stackexchange.com/questions/9668/what-are-3d-textures

Page 58: OpenGL L05-Texturing

2D Texture and 3D Texture

• All we have used so far are 2D textures mapped on a 3D coordinates.

• So what’s a 3D texture?!

– 3D texture (or sometimes called “Volume textures”) works like regular 2D texture but it is truly

3D. 2D textures has UV coordinates, 3D has UVW coordinates

Page 59: OpenGL L05-Texturing

Textures Coordinates

• Texture coordinates are named s, t, r, and q

Page 60: OpenGL L05-Texturing

2D Texture and 3D Texture

• 3D textures are used in:

– Textures mapped into a model vertices

– volumetric effects in games (fire, smoke, light rays, realistic fog)

– caching light for realtime global illumination (CryEngine for example)

– scientific (MRI, CT scans are saved into volumes)

Page 61: OpenGL L05-Texturing

2D Texture and 3D Texture

• Watch Nvidia smoke box in a XFX 9600gt

– https://www.youtube.com/watch?v=9AS4xV-CK14

Page 62: OpenGL L05-Texturing

2D Texture and 3D Texture

• Watch Global illumination in CryEngine 3 Tech Trailer (HD)

– https://www.youtube.com/watch?v=Pq39Xb7OdH8