Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to use TIFF file, maybe try this.

Similar presentations


Presentation on theme: "CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to use TIFF file, maybe try this."— Presentation transcript:

1 CH6 Texture

2 Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to use TIFF file, maybe try this. http://gnuwin32.sourceforge.net/packages/tiff.htm Download the developer files. Or … just change TIFF files to BMP files ˋ (′ ~‵ ") ˊ

3 Before coding … 2/2 Add GlAux.Lib into your project.

4 Example Program 1/4 #include #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

5 Example Program 2/4 void LoadTexture(char* filename){ img = auxDIBImageLoad(filename); glGenTextures(TEX_NUM, texObject); glBindTexture(GL_TEXTURE_2D, texObject[0]); #ifdef MIPMAP gluBuild2DMipmaps(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); #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 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); }

6 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(); }

7 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(); }

8 Check.bmp Width/Height: 256/256

9 Load BMP file AUX_RGBImageRec* auxDIBImageLoad(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.

10 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

11 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

12 Specifies 2D texture image 1/4 void glTexImage2D( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels );

13 Specifies 2D texture image 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

14 Specifies 2D texture image 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.

15 Specifies 2D texture image 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 … )

16 Apply texture to objects 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

17 Apply texture to objects 2/3 Base Internal Format GL_REPLACEGL_MODULATEGL_ADD GL_ALPHAC = Cf, A = AtC = Cf, A = AfAtC=Cf, A=AfAt GL_LUMINANCEC = Lt, A = AfC = CfLt, A = AfC=Cf+CT, A=Af GL_LUMINANCE_ALPHAC = Lt, A = AtC = CfLt, A = AfAtC=Cf+Ct, A=AfAt GL_INTENSITYC = It, A = ItC = CfIt, A = AfItC=Cf+Ct, A=Af+At GL_RGBC = Ct, A = AfC = CfCt, A = AfC=Cf+Ct, A=Af GL_RGBAC = Ct, A = AtC = CfCt, A = AfAtC=Cf+Ct, A=AfAt Base Internal Format GL_DECALGL_BLEND GL_ALPHAUndefinedC = Cf, A = AfAt GL_LUMINANCEUndefinedC = Cf(1-Lt)+CcLt, A = Af GL_LUMINANCE_ALPHAUndefinedC = Cf(1-Lt)+CcLt, A = AfAt GL_INTENSITYUndefinedC = Cf(1-It)+CcIt, A = Af(1-It)+AcIt GL_RGBC = Ct, A = AfC = Cf(1-Ct)+CcCt, A = Af GL_RGBAC = Cf(1-At)+CtAt, A = AtC = Cf(1-Ct)+CcCt, A = AfAt Xc indicates the values assigned with GL_TEXTURE_ENV_COLOR. * GL_TEXTURE_ENV_COLOR is defined by glGetTexEnvfv Xt is the values form texture, Xf is the values in the color buffers. C is the final output color value and A is the output alpha value.

18 Apply texture to objects 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

19 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

20 Texture parameter 2/5 pnameparam GL_TEXTURE_WARP_S GL_TEXTURE_WARP_TGL_REPEAT, GL_CLAMP GL_TEXTURE_MAG_FILTER GL_TEXTURE_MIN_FILTERGL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST GL_TEXTURE_BORDER_COLORcolor value

21 Texture parameter 3/5 WARP Repeat both S, T Clamp both S, T Repeat T but Clamp S

22 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

23 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.

24 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

25 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.

26 Build mip-maps 3/3 without mip-map with mip-map

27 How to use texture? Remember to enable … glEnable(GL_TEXTURE_2D); Remember to bind … glBindTexture(GL_TEXTURE_2D, textName); Remember to assign texel for each vertex … glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, 0.0, 0.0);

28 Review display function 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(); }

29 Example Program 2 1/3 #include 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); }

30 Example Program 2 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); }

31 Example Program 2 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(); }

32 environment.bmp Width/Height: 128/128

33 Automatic Texture-Coordinate Generation 1/4 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

34 Automatic Texture-Coordinate Generation 2/4 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

35 Automatic Texture-Coordinate Generation 3/4 void LoadTexture(char* filename){ GLint par[] = {1, 1, 1, 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); glTexGeniv(GL_S, GL_OBJECT_PLANE, par); glTexGeniv(GL_T, GL_OBJECT_PLANE, par); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_2D); }

36

37 Automatic Texture-Coordinate Generation 4/4 If change parameters … par[] = {1, 0, 0, 0} par[] = {0, 1, 0, 0}

38 Environment map … (review) 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); }

39 So easy … v(  ̄︶ ̄ )y


Download ppt "CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to use TIFF file, maybe try this."

Similar presentations


Ads by Google