ch6 texture. pixel pipeline vertex pipeline course map transformation & lighting primitive...
TRANSCRIPT
![Page 1: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/1.jpg)
CH6 Texture
![Page 2: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/2.jpg)
Pixel pipeline
Vertex pipeline
Course Map
Transformation&
Lighting
Primitiveassembly
Viewport culling&
clipping
Textureblending
Per Fragmentoperations
Bufferoperations
Rasterizersetup
Framebuffer
We are here!
![Page 3: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/3.jpg)
Before coding… 1/3
Prepare all texture images you need. BMP file is a good choice If you want to load other format,
you can try the FreeImage library. Or…just convert image files to BMP
files
![Page 4: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/4.jpg)
Before coding… 2/3
Add GlAux.Lib into your project.
![Page 5: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/5.jpg)
Before coding… 3/3
Prepare a mesh with texture coordinate For simple shapes, you can assign texture
coordinate by manually coding. For complex objects
Use automatic texture-coordinate generation Or use modeling tools
Maya, 3DS Max, Blender…
![Page 6: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/6.jpg)
How to use texture? 1/2
Initial Texture Generate Texture
glGenTextures(number of textures , texture id array) Bind
glBindTexture(GL_TEXTURE_2D, textId); Move Date into Texture
glTexImage2D (…) or gluBuild2DMipmaps(…)
Set the Options glTexEnv (…) glTexParameteri (…)
![Page 7: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/7.jpg)
How to use texture? 2/2
Draw Object with Texture Enable
glEnable(GL_TEXTURE_2D); Bind
glBindTexture(GL_TEXTURE_2D, textId); Assign texture coordinate for each vertex
glTexCoord2f(u, v); glVertex3f(x, y, z);
Disable and unbind them when you don’t want to use them on next object.
![Page 8: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/8.jpg)
Example Program 1/4
#include<gl/glaux.h>#include<gl/glut.h>#define TEX_NUM 1 //the number of textures you use.
#define MIPMAP
AUX_RGBImageRec * img;//to save image file
GLuint texObject[TEX_NUM]; //texture object
![Page 9: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/9.jpg)
Example Program 2/4
void LoadTexture(char* filename){img = auxDIBImageLoadA(filename);glGenTextures(TEX_NUM, texObject);glBindTexture(GL_TEXTURE_2D, texObject[0]);
#ifdef MIPMAPgluBuild2DMipmaps(GL_TEXTURE_2D, 4, img->sizeX, img->sizeY, GL_RGB, GL_UNSIGNED_BYTE, img->data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
#elseglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->sizeX, img->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
#endifglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
![Page 10: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/10.jpg)
Example Program 3/4
void GL_display(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texObject[0]);glBegin(GL_QUADS);glTexCoord2f(0.0, 0.0);glVertex3f(-50.0, 0.0, 0.0);glTexCoord2f(0.0, 100.0);glVertex3f(-50.0, 0.0, 100.0);glTexCoord2f(100.0, 100.0);glVertex3f(50.0, 0.0, 100.0);glTexCoord2f(100.0, 0.0);glVertex3f(50.0, 0.0, 0.0);glEnd();glFlush();
}
![Page 11: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/11.jpg)
Example Program 4/4
void GL_reshape(GLsizei w, GLsizei h){
glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 100.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0, 0.5, 0.0, 50.0, 0.0, 50.0, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv){
glutInit(&argc, argv);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);glutCreateWindow("Texture");LoadTexture("check.bmp");glutDisplayFunc(GL_display);glutReshapeFunc(GL_reshape);glutMainLoop();
}
![Page 12: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/12.jpg)
Result
Check_old.bmp
Width/Height: 256/256
![Page 13: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/13.jpg)
Load BMP file AUX_RGBImageRec* auxDIBImageLoadA(char
*); Load a BMP file.
typedef struct _AUX_RGBImageRec { GLint sizeX, sizeY;
unsigned char *data;};
sizeX, sizeY : image width / height. data : A pointer to the image data in memory.
![Page 14: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/14.jpg)
Generate Texture Object void glGenTextures( GLsizei n, GLuint
*textures ); To generate texture names. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/
glfunc02_3p6b.asp n: the number of texture names to be generated. textures: pointer to the first element of an array in
which the texture are stored. GLboolean glIsTexture( GLuint texture );
To determine if a name corresponds to a texture. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/
glfunc03_7cv9.asp
![Page 15: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/15.jpg)
Bind texture void glBindTexture( GLenum target, GLuint
texture );
To bind the texture to the target. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc01_3bad.asp
target: GL_TEXTURE_1D, GL_TEXTURE_2D
void glDeleteTextures( GLsizei n, const GLuint
*textures ); Delete named textures. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc01_6vqr.asp
![Page 16: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/16.jpg)
Send texture data: glTexImage2D 1/4 void glTexImage2D(
GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels
);
![Page 17: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/17.jpg)
Send texture data: glTexImage2D 2/4
Specifies a 2D texture image http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/
glfunc03_16jo.asp
target: Must be GL_TEXTURE_2D level: level-of-detail level. It must be 0 if you
don’t want to use mip-map. internalformat: GL_RGBA and many
others… Width / height : image witdh / height
![Page 18: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/18.jpg)
Send texture data: glTexImage2D 3/4
border: 0 or 1 0 : no border, image width & height must
be power of 2. 1 : use border, image width & height
must be power of 2 plus 2.
![Page 19: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/19.jpg)
Send texture data: glTexImage2D 4/4
format: format of the image data GL_RGB, GL_RGBA, and many others…
type: data type of the image data pixel: A pointer to the image data in
memory. (Hint: the BMP file you have loaded…)
![Page 20: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/20.jpg)
Texture Setting: glTexEnv 1/3
void glTexEnv{fi}(GLenum target, GLenum pname, GLfloat param ); to indicate how the texels are combined
with the original pixels http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_6xyu.asp
target: must be GL_TEXTURE_ENV pname: must be GL_TEXTURE_ENV_MODE param: 5 choices, see next page
![Page 21: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/21.jpg)
Texture Setting: glTexEnv 2/3
Base Internal Format GL_REPLACE GL_MODULATE GL_ADD
GL_ALPHA C = Cf, A = At C = Cf, A = AfAt C=Cf, A=AfAtGL_LUMINANCE C = Lt, A = Af C = CfLt, A = Af C=Cf+CT, A=AfGL_LUMINANCE_ALPHA C = Lt, A = At C = CfLt, A = AfAt C=Cf+Ct, A=AfAtGL_INTENSITY C = It, A = It C = CfIt, A = AfItC=Cf+Ct, A=Af+AtGL_RGB C = Ct, A = Af C = CfCt, A = Af C=Cf+Ct, A=AfGL_RGBA C = Ct, A = At C = CfCt, A = AfAt C=Cf+Ct, A=AfAt
Base Internal Format GL_DECAL GL_BLEND
GL_ALPHA Undefined C = Cf, A = AfAtGL_LUMINANCE Undefined C = Cf(1-Lt)+CcLt, A = AfGL_LUMINANCE_ALPHA Undefined C = Cf(1-Lt)+CcLt, A = AfAtGL_INTENSITY Undefined C = Cf(1-It)+CcIt, A = Af(1-It)+AcItGL_RGB C = Ct, A = Af C = Cf(1-Ct)+CcCt, A = AfGL_RGBA C = Cf(1-At)+CtAt, A = At C = Cf(1-Ct)+CcCt, A = AfAt
Ct = color of texture , At = alpha of textureCf = color of frame buffer , Af = alpha of frame buffer
![Page 22: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/22.jpg)
Texture Setting: glTexEnv 3/3
glTexEnv{fi}v(GLenum target, GLenum pname, const GLfloat *params ) target: must be GL_TEXTURE_ENV pname: GL_TEXTURE_ENV_COLOR or
GL_TEXTURE_ENV_MODE params:
if pname is GL_TEXTURE_ENV_MODE : A pointer to an array of parameters.(GL_MODULATE, GL_DECAL, and GL_BLEND)
if pname is GL_TEXTURE_ENV_COLOR: a pointer to an array of R, G, B, A
![Page 23: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/23.jpg)
Texture parameter 1/5
glTexParameter{if}(GLenum target, GLenum pname, GLfloat param ) Set texture parameters. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/
glfunc03_9upe.asp
target: GL_TEXTURE_1D, GL_TEXTURE_2D
pname / param: see next page
![Page 24: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/24.jpg)
Texture parameter 2/5
pname param
GL_TEXTURE_WRAP_SGL_TEXTURE_WRAP_T GL_REPEAT, GL_CLAMP
GL_TEXTURE_MAG_FILTERGL_TEXTURE_MIN_FILTER GL_NEAREST, GL_LINEAR,
GL_NEAREST_MIPMAP_NEAREST,
GL_NEAREST_MIPMAP_LINEAR,GL_LINEAR_MIPMAP_LINEAR,GL_LINEAR_MIPMAP_NEAREST
GL_TEXTURE_BORDER_COLOR color value
![Page 25: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/25.jpg)
Texture parameter 3/5
WRAP
Repeat both S, T
Clamp both S, T
Repeat T but Clamp S
![Page 26: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/26.jpg)
Texture parameter 4/5
MAG filter When the pixel being textured maps to an
area less than or equal to one texture element
MIN filter When the pixel being textured maps to an
area greater then one texture element
![Page 27: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/27.jpg)
Texture parameter 5/5
GL_NEARST
pixel
texel
GL_LINEAR
Get average color...
GL_NEAREST_MIPMAP_XXXXXX Find the most closely match mipmap
GL_LINEAR_MIPMAP_XXXXXXX Find the most 2 closely match mipmap and get average.
![Page 28: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/28.jpg)
Build mip-maps 1/3
int gluBuild2DMipmaps( GLenum target, GLint components, GLint width, GLint height, GLenum format, GLenum type, const void *data );
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glufnc01_406r.asp
![Page 29: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/29.jpg)
Build mip-maps 2/3
target : Must be GL_TEXTURE_2D. components :The number of color
components in the texture. Must be 1, 2, 3, or 4.
width, height :width / height of image. format : GL_RGB, GL_RGBA and many
others… type :The data type for data data :A pointer to the image data in
memory.
![Page 30: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/30.jpg)
Build mip-maps 3/3
without mip-map with mip-map
![Page 31: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/31.jpg)
Automatic TextureCoordinate Generation No need to use glTexCoord Four types
Sphere Object plane Eye plane Cube
![Page 32: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/32.jpg)
Sphere
environment.bmp
Width/Height: 128/128
![Page 33: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/33.jpg)
![Page 34: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/34.jpg)
![Page 35: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/35.jpg)
Object plane -1
S = 1.0 x + 0.0 y + 0.0 z + 0.0 w
T = 0.0 x + 1.0 y + 0.0 z + 0.0 w
xyz in world space
![Page 36: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/36.jpg)
Object plane -2
S = 1.0 x + 0.0 y + 0.0 z + 0.0 w
T = 0.0 x + 2.0 y + 0.0 z + 0.0 w
xyz in world space
![Page 37: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/37.jpg)
Eye planeS = 1.0 x + 0.0 y + 0.0 z + 0.0
wT = 0.0 x + 1.0 y + 0.0 z + 0.0
wxyz in eye space
![Page 38: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/38.jpg)
Sphere Example 1/3
#include<gl/glaux.h>#include<gl/glut.h>
AUX_RGBImageRec * img;GLuint texObject;
void LoadTexture(char* filename){img = auxDIBImageLoad(filename);glGenTextures(1, &texObject);glBindTexture(GL_TEXTURE_2D, texObject);gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->sizeX, img->sizeY, GL_RGB, GL_UNSIGNED_BYTE, img->data);glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);glEnable(GL_TEXTURE_2D);
}
![Page 39: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/39.jpg)
Sphere Example 2/3
void GL_display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glEnable(GL_DEPTH_TEST);glColor3f(1.0, 1.0, 1.0);glutSolidTeapot(18);glFlush();
}
void GL_reshape(GLsizei w, GLsizei h){
glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 100.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
![Page 40: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/40.jpg)
Sphere Example 3/3
int main(int argc, char** argv){
glutInit(&argc, argv);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);glutCreateWindow("Texture");LoadTexture("environment.bmp");glutDisplayFunc(GL_display);glutReshapeFunc(GL_reshape);glutMainLoop();
}
![Page 41: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/41.jpg)
Object Plane Examplevoid LoadTexture(char* filename){
float params1[] = {1.0 , 0.0 , 0.0 ,0.0};float params2[] = {0.0 , 1.0 , 0.0 ,0.0};
img = auxDIBImageLoad(filename);glGenTextures(1, &texObject);glBindTexture(GL_TEXTURE_2D, texObject);gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->sizeX, img->sizeY, GL_RGB,GL_UNSIGNED_BYTE, img->data);glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);glTexGenfv(GL_S , GL_OBJECT_PLANE , params1);glTexGenfv(GL_T , GL_OBJECT_PLANE , params2);glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);glEnable(GL_TEXTURE_2D);
}
![Page 42: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/42.jpg)
Eye Plane Examplevoid LoadTexture(char* filename){
float params1[] = {1.0 , 0.0 , 0.0 ,0.0};float params2[] = {0.0 , 1.0 , 0.0 ,0.0};
img = auxDIBImageLoad(filename);glGenTextures(1, &texObject);glBindTexture(GL_TEXTURE_2D, texObject);gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->sizeX, img->sizeY, GL_RGB,GL_UNSIGNED_BYTE, img->data);glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);glTexGenfv(GL_S , GL_EYE_PLANE , params1);glTexGenfv(GL_T , GL_EYE_PLANE , params2);glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);glEnable(GL_TEXTURE_2D);
}
![Page 43: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/43.jpg)
Cube
![Page 44: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/44.jpg)
Result
Reflection Normal
![Page 45: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/45.jpg)
Cube Example Bind Once, send six images.glBindTexture(GL_TEXTURE_CUBE_MAP, id);glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB,
imageSize, imageSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image1);glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB,
imageSize, imageSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB,
imageSize, imageSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image3);glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB,
imageSize, imageSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image4);glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB,
imageSize, imageSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image5);glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB,
imageSize, imageSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image6);glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
![Page 46: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/46.jpg)
Texture CoordinateglTexGeni(GL_S, GL_TEXTURE_GEN_MODE,
GL_REFLECTION_MAP);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,
GL_REFLECTION_MAP);glTexGeni(GL_R, GL_TEXTURE_GEN_MODE,
GL_REFLECTION_MAP);glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);glEnable(GL_TEXTURE_GEN_R);glEnable(GL_TEXTURE_CUBE_MAP);
//draw your objects
![Page 47: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/47.jpg)
Automatic Texture-Coordinate Generation
glTexGen{dfi}(GLenum coord, GLenum pname, GLdouble param); control the generation of texture
coordinates http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_73u6.asp
coord: GL_S, GL_T, GL_R, or GL_Q. pname: must be GL_TEXTURE_GEN_MODE. param: GL_OBJECT_LINEAR,
GL_EYE_LINEAR, or GL_SPHERE_MAP
![Page 48: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/48.jpg)
Automatic Texture-Coordinate Generation glTexGen{dfi}v(GLenum coord,
GLenum pname, const GLdouble *params ); coord: GL_S, GL_T, GL_R, or GL_Q. pname: GL_TEXTURE_GEN_MODE,
GL_OBJECT_PLANE, or GL_EYE_PLANE … params: the array of texture generation
parameters, if pname is GL_OBJECT_PLANE, or GL_EYE_PLANE.
*Generated plane = p1X + p2Y + p3Z + p4W
![Page 49: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/49.jpg)
Multi-texture
Op 0
Op 1
Op 2
Op n
glActiveTexture( GL_TEXTURE0 );glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, …);
Pixel Color
final color
glActiveTexture( GL_TEXTURE1 );glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, …);
glActiveTexture( GL_TEXTURE2 );glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, …);
glActiveTexture( GL_TEXTUREn );glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, …);
![Page 50: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/50.jpg)
Texture Channel void glActiveTexture(GLenum texture) Parameter:
GL_TEXTURE0 ~ GL_MAX_TEXTURE_UNITS – 1
Choose channel before binding texture Bind 0 => Disable the chosen
channel
![Page 51: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/51.jpg)
Operation glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, operation);
Operation GL_MODULATE / GL_ADD or / ... / GL_SUBTRACT
![Page 52: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/52.jpg)
Multiple Texture Coordinate glMultiTexCoord2fARB(GLenum target, GLshort s, GLshort
t);
Target GL_TEXTURE0 ~ GL_MAX_TEXTURE_UNITS – 1
s, t The texture coordinate of vertex
Set the texture coordinate before calling glVertex
You need the glew libraray. The setting of glew is similar to the glaux
![Page 53: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/53.jpg)
Example#include "glew.h"#include "glaux.h"#include "glut.h“#define TEX_NUM 2void reshape(GLsizei w, GLsizei h);void display();int main(int argc, char** argv){
glutInit(&argc, argv);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);glutCreateWindow("Texture");glewInit();glGenTextures(TEX_NUM, texObject);LoadTexture(“FirstTexture.bmp", 0);LoadTexture("SecondTexture.bmp", 1);glutDisplayFunc(display);glutReshapeFunc(reshape);glutMainLoop();return 0;
}
![Page 54: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/54.jpg)
void LoadTexture(char* filename, int index)
{
img = auxDIBImageLoadA(filename);
glBindTexture(GL_TEXTURE_2D, texObject[index]);
#ifdef MIPMAP
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, img->sizeX, img->sizeY, GL_RGB, GL_UNSIGNED_BYTE, img->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->sizeX, img->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, img->data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
#endif
}
![Page 55: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/55.jpg)
void display(){
glClearColor(0.0, 0.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);
//bind texture 0glActiveTexture(GL_TEXTURE0);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texObject[0]);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
//bind texture 1glActiveTexture(GL_TEXTURE1);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texObject[1]);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
![Page 56: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/56.jpg)
glBegin(GL_QUADS);glColor3f(1.0, 1.0, 1.0)glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);glVertex3f(-1.0, -1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);glMultiTexCoord2f(GL_TEXTURE1, 3.0, 0.0);glVertex3f(1.0, -1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);glMultiTexCoord2f(GL_TEXTURE1, 3.0, 3.0);glVertex3f(1.0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);glMultiTexCoord2f(GL_TEXTURE1, 0.0, 3.0);glVertex3f(-1.0, 1.0, 0.0);glEnd();
![Page 57: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/57.jpg)
//unbind texture 1glActiveTexture(GL_TEXTURE1);glDisable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, 0);
//unbind texture 0glActiveTexture(GL_TEXTURE0);glDisable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, 0);
glFlush();}
![Page 58: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/58.jpg)
void reshape(GLsizei w, GLsizei h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glLoadIdentity();}
![Page 59: CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment](https://reader033.vdocuments.mx/reader033/viewer/2022051215/5697bfa91a28abf838c99ff9/html5/thumbnails/59.jpg)
Result
1st texture 2nd texture Result