Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität...

33
Texture Mapping Beispiel Universität Frankfurt

Transcript of Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität...

Page 1: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Texture Mapping

Beispiel

Universität Frankfurt

Page 2: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Texture Mapping

Universität Frankfurt

Warum?

- Detailreiche Oberfläche ohne zusätzliche Polygone

Page 3: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Texturen I

Universität Frankfurt

- 1D-Texturen

- 2D-Texturen

- 3D-Texturen

Element der Textur: Texel

Page 4: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Texturen II

Universität Frankfurt

- Änderung der Farben (Farbtexturen)

- Änderung der Normalen (Bump-Mapping)

- Änderung der Helligkeiten

- Änderung der Parameter des Beleuchtungsmodells

Page 5: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Texture Mapping I

Universität Frankfurt

Page 6: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Texture Mapping II

Universität Frankfurt

Page 7: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping I

Universität Frankfurt

glEnable(GL_Texture_1D)

glDisable (GL_Texture_1D)

glEnable(GL_Texture_2D)

glDisable (GL_Texture_2D)

glEnable(GL_Texture_3D)

glDisable(GL_Texture_3D)

Page 8: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping II

Laden einer Textur

Universität Frankfurt

glTexImage{1,2}D(GLenum target, GLint level, GLint internalformat,

GLsizei width, {GLsizei height},GLint border,

GLenum format, GLenum type, void *data)

target: GL_TEXTURE_1D, GL_TEXTURE_2D, GL_PROXY_TEXTURE_2D

level: Mipmap-Level, 0 bezeichnet das Ausgangsbild, n bezeichnet die n-teMipmap-Reduktionsstufe

internalformat: Anzahl der Farbkomponenten der Textur, entweder 1,2,3,4, oder GL_RGB, GL_RGBA, GL_ALPHA, .......

width: Breite der Textur in Anzahl Texel, Größe: 2n+2(border) texel

{height}: Höhe der Textur in Anzahl Texel, Größe: 2n+2(border) texel

border: Rahmenbreite, 0 oder 1 Texel

format: Pixel-Format: GL_RGB, GL_RGBA, GL_ALPHA, .....

type: GL_UNSIGNED_BYTE, GL_BYTE, GL_SHORT, GL_INT

data: Zeiger auf Pixel-Daten

Page 9: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping IIa

Laden einer Textur#define checkImageWidth 64

#define checkImageHeight 64

static GLubyte checkImage[checkImageHeight][checkImageWidth][4];

int i, j, c;

/* Create checkerboard texture */

for (i = 0; i < checkImageHeight; i++) {

for (j = 0; j < checkImageWidth; j++) {

c = ((((i&0x8)==0)^((j&0x8))==0))*255;

checkImage[i][j][0] = (GLubyte) c;

checkImage[i][j][1] = (GLubyte) c;

checkImage[i][j][2] = (GLubyte) c;

checkImage[i][j][3] = (GLubyte) 255; } }

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,

checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);

Universität Frankfurt

Page 10: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping IIb

Laden einer Textur

GLbyte *pBytes;

GLint iWidth, iHeight, iComponents, eFormat;

GLenum eFormat;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

pBytes = gltLoadTGA("Stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);

glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat,

GL_UNSIGNED_BYTE, pBytes);

free(pBytes);

Universität Frankfurt

Page 11: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping III

Texture Environment

Universität Frankfurt

glTexEnv{i,f} (GLenum target, GLenum pname, {GLint, GLfloat } param)

glTexEnv{i,f}v (GLenum target, GLenum pname, {GLint, GLfloat } *params)

target: GL_TEXTURE_ENV

pname: GL_TEXTURE_ENV_MODE, GL_TEXTURE_ENV_COLOR

param: GL_MODULATE, GL_DECAL, GL_REPLACE, GL_BLEND, (red, green, blue, alpha)

Beispiele:

glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

GLfloat Color[4] = {1.0, 0.0, 1.0, 0.5);glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Color);

Page 12: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping IV

Texture Parameter

Universität Frankfurt

glTexParameter{i,f} (GLenum target, GLenum pname, {GLint, GLfloat } param)

glTexParameter{i,f}v (GLenum target, GLenum pname, {GLint, GLfloat } *params)

target: GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D

pname: GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER,GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T, ....

param: GL_LINEAR, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_GL_REPEAT,

Beispiele:

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

Page 13: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

OpenGL Texture Mapping VI

Textur-Koordinaten

Universität Frankfurt

glTexCoord1{d,f,i,s} (GLx s) x∈∈∈∈{double, float, int, short}

glTexCoord2{d,f,i,s} (GLx s, GLy t), x,y∈∈∈∈{double, float, int, short}

glTexCoord1{d,f,i,s}v (const GLx *v) x∈∈∈∈{double, float, int, short}

glTexCoord2{d,f,i,s}v (const GLx *v), x∈∈∈∈{double, float, int, short}

Beispiele:glTexCoord2f(1.0f, 2.0f);glVertex3fv(vCorners[0]);

glTexCoord2f(0.0f, 0.0f);glVertex3fv(vCorners[2]);

glTexCoord2f(2.0f, 0.0f);glVertex3fv(vCorners[1]);

Page 14: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Multiple Textures I

Universität Frankfurt

glGenTextures(GLsizei n, GLuint *textures)

n: Anzahl der benötigten Texture-Identifier

textures: Feld, in dem die Texture-Identifier abgelegt werden

glBindTexture(GLenum target, GLuint texture)

target: GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D

texture: bezeichnet einen der von OpenGL generierten Identifier

glDeleteTextures(GLsizei n, const GLuint *textures)

n: Anzahl der zu löschenden Texturentextures: Feld, in dem die zu löschendenTexture-Identifier abgelegt sind

Page 15: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Multiple Textures II

Beispiel

Universität Frankfurt

#define NO_TEXTURES 2#define CHECKER 0#define STONE 1

static GLuint textures[NO_TEXTURES];glGenTextures(NO_TEXTURES, textures);

//Texture CheckerboardglBindTexture(GL_TEXTURE_2D, textures[CHECKER]);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

//Texture StoneglBindTexture(GL_TEXTURE_2D, textures[STONE]);..................

Page 16: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Globale Beleuchtungsparameter

Universität Frankfurt

void glLightModel{i,f}(GLenum pname, TYPE param)

void glLightModel{i,f}v(GLenum pname,TYPE *param)

Parameter-Name Parameter

GL_LIGHT_MODEL_COLOR_CONTROL GL_SINGLE_COLOR

GL_SEPARATE_SPECULAR_COLOR

Page 17: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Globale Beleuchtungsparameter

Universität Frankfurt

GL_SINGLE_COLOR GL_SEPARATE_SPECULAR_COLOR

Page 18: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Mipmapping I

Universität Frankfurt

Mip: „multum in parvo“

Problem: Szintillation

Page 19: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Mipmapping II

Universität Frankfurt

Page 20: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Mipmapping III

Universität Frankfurt

glTexParameter{i,f} (GLenum target, GLenum pname, {GLint, GLfloat } param)

glTexParameter{i,f}v (GLenum target, GLenum pname, {GLint, GLfloat } *params)

target: GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3Dpname: GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER,

, ....param: ...., GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST,

GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, .....

Beispiel:

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

Page 21: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

How to Generate a Mipmap I

Universität Frankfurt

1.) Do it by hand/program

Page 22: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

How to Generate a Mipmap II

Universität Frankfurt

1.) Do it by hand/program

GLubyte mipmapImage32[32][32][4];

GLubyte mipmapImage16[16][16][4];

GLubyte mipmapImage8[8][8][4];

GLubyte mipmapImage4[4][4][4];

GLubyte mipmapImage2[2][2][4];

GLubyte mipmapImage1[1][1][4];

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE,

mipmapImage32);

glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,

mipmapImage16);

glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE,

mipmapImage8);

glTexImage2D(GL_TEXTURE_2D, 3, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,

mipmapImage4);

glTexImage2D(GL_TEXTURE_2D, 4, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,

mipmapImage2);

glTexImage2D(GL_TEXTURE_2D, 5, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,

mipmapImage1);

Page 23: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

How to Generate a Mipmap III

Universität Frankfurt

2.) Let OpenGL do it!

GLint gluBuild2DMipmaps (GLenum target, GLint internalFormat,

GLsizei width, GLsizei height, GLenum format,

GLenum type, void *data)

target: GL_TEXTURE_2DinternalFormat: Anzahl der Farbkomponenten der Textur, entweder 1,2,3,4,

oder GL_RGB, GL_RGBA, GL_ALPHA, .......width: Breite der Textur in Anzahl Texelheight: Höhe der Textur in Anzahl Texelformat: Pixel-Format: GL_RGB, GL_RGBA, GL_ALPHA, .....type: GL_UNSIGNED_BYTE, GL_BYTE, GL_SHORT, GL_INTdata: Zeiger auf Pixel-Daten

Beispiel: gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, 32, 32,GL_RGBA, GL_UNSIGNED_BYTE, checkImage);

Page 24: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation I

Contour Lines & Environment Mapping

Universität Frankfurt

Page 25: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation II

Contour Lines

Universität Frankfurt

glEnable(GL_TEXTURE_GEN_{S, T, ...});

glTexGen{ifd} (GLenum coord, GLenum pname, TYPE param)

glTexGen{ifd}v (GLenum coord, GLenum pname, TYPE *param)

coord: GL_S, GL_T, ...

pname: TEXTURE_GEN_MODE, GL_OBJECT_PLANE, GL_EYE_PLANE

param: GL_OBJECT_LINEAR, GL_EYE_LINEAR, GL_SPHERE_MAP

Beispiele:glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

glTexGenfv(GL_S, GL_OBJECT_PLANE, *plane);

Page 26: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation III

Contour Lines Defined in Eye Coordinate System

Universität Frankfurt

Ebene: (nx, ny, nz, d)

Abstand eines Punktes (x, y, z, w) von der Ebene bestimmt die Texturkoordinate:

{s,t}= nx*x +ny*y+nz*z+d*w

Beispiel:

xequalzero = {1.0, 0.0, 0.0, 0.0};

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

glTexGenfv(GL_S, GL_OBJECT_PLANE, xequalzero);

Page 27: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation IV

Contour Lines

Universität Frankfurt

Page 28: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation V

Contour Lines Defined in Object Coordinate System

Universität Frankfurt

Ebene: (n‘x, n‘y, nz, d)= (nx, ny, nz, d)M-1, mit M Model View Matrix

Abstand eines Punktes (x, y, z, w) von der Ebene bestimmt die Texturkoordinate:

{s,t}= n‘x*x +n‘y*y+n‘z*z+d‘*w

Beispiel: xequalzero = {1.0, 0.0, 0.0, 0.0};

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

glTexGenfv(GL_S, GL_EYE_PLANE, xequalzero);

Page 29: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation VI

Reflective Ray

Universität Frankfurt

Page 30: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation VII

Sphere Mapping

Universität Frankfurt

Page 31: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation VIII

Sphere Mapping - Texture

Universität Frankfurt

Page 32: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation IX

The Mathematics of Sphere Mapping

( )1222

+++= rrr zp

yx

2(NV)NVR −=

Universität Frankfurt2

1

2

2

1

2

+=

+=

pt

ps

r

r

y

x

Page 33: Texture Mapping Beispiel - gdv.informatik.uni-frankfurt.de · Texturen II Universität Frankfurt-Änderung der Farben (Farbtexturen)-Änderung der Normalen (Bump-Mapping)-Änderung

Automatic Texture Coordinate Generation IX

Sphere Mapping

Universität Frankfurt

//Turn on texture coordinate generationglEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);

// Sphere MapglTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);