GLART week 5: Texture Mapping

Through texturing, OpenGL applies an image to a surface. This opens up a world of possible aesthetic effects, as detailed and subtle images can be applied to geometry to create convincing "skins" for your shapes. When combined with lighting, materials and alpha blending, textures can bring a scene to life, or generate endless possibilities for abstract designs. Assigment: texture Reading: Redbook pages 351-367 "Chapter 9: Texture Mapping" Examples: Texture a Quad Texture Map a Cube Texture Examples Transparent Material (shows Z depth problem) Transparent Material (Z depth fixed) Add these images to the "images" folder: metal_texture_grid.png cube_uvw_map_dice.png velazquez_texture_256.jpg screen_bg.png Video Capture example (using JMyron) glart_jmyron_video_demo.zip See also: nate robbins texture tutorial: http://www.xmission.com/~nate/tutors.html OpenGL Texture FAQ http://www.opengl.org/resources/faq/technical/texture.htm NOTES: ================================================================= Texture mapping The process of applying an image to a surface What's needed: an array of pixel data (usually loaded from an image) a texture resource OpenGL commands to configure the texture mapping coordinates match each vertex to a point in the texture Loading an image Texture image dimensions must be powers of 2 4, 8, 16, 32, 64, etc. typically square size is limited by hardware usually 2048x2048 maximum Have to convert image format (jpg, gif, png) into pixels in an OpenGL format OpenGL can have many internal pixel formats (to accomodate different hardware) RGBA = four bytes per pixel, one byte for each color component red, green, blue, alpha (in that order) Can use Java Image will load in default java format: ARGB have to switch Alpha byte to the end have to flip image vertically (OpenGL coordinate system is 0,0 lower left) GLImage class not part of LWJGL wraps Java/OpenGL image conversion wraps LWJGL NIO buffer functions returns pixels in array and/or ByteBuffer Creating a Texture glGenTextures() allocate a texture resource in the GPU return a "texture handle": a number that refers to the texture resource glBindTexture() "select" a texture resource for use specify the texture type (GL_TEXTURE_2D) can have 1D, 2D and 3D textures GL_TEXTURE_2D is the only texture type we'll be using glTexParameter() configure texture properties how should texture repeat (vertically, horizontally) how should texture scale LINEAR - high quality (slower) NEAREST - low quality (faster) glTexImage2D() create the texture from an array of pixels Specify pixel format: RGBA glTexEnv() set global texture parameters set GL_TEXTURE_ENV_MODE to GL_MODULATE combines texture with lighting effects GL_REPLACE or GL_DECAL override lighting effects with the texture GLU.gluBuild2DMipmaps() make "mipmap": a set of lower detail textures OpenGL switches to lower detail textures as object recedes in space glGetIntegerv(GL_MAX_TEXTURE_SIZE, texSize) if necessary, use this call to find out the maximum texture size possible. Mapping texture to geometry Texture coordinates are referred to as u,v,w three letters before x,y,z u and v are the 2D coordinates into texture coordinate system coordinates range from 0-1 picture a grid superimposed on the texture image 0,0 in lower left 1,1 in upper right numbers greater than 1 will repeat texture glTextureCoord(u,v) like glNormal(), called before glVertex() maps a texture coordinate (u,v) to the vertex (x,y,z) think of it as "pinning" the texture to the geometry the texture image will be stretched or compressed to map texture to verts Rendering glEnable(GL_TEXTURE_2D) enable 2D texturing glBindTexture(texture_handle) select the texture we want to use glTextureCoord() match texture coords to geometry glColor() tint the texture with the given color (if lighting is disabled) use the fourth color value of the diffuse color (alpha) to make the texture transparent see Blending section for details if light is enabled, glColor() will have no effect, use material colors instead glMaterial() if lighting is enabled, use material colors to adjust texture appearance lighting effects are rendered first, then texture textures will obscure specular (reflections) use glLightModeli(GL12.GL_LIGHT_MODEL_COLOR_CONTROL, GL12.GL_SEPARATE_SPECULAR_COLOR) to render specular highlights on top of texture in OpenGL version 1.2 UVW mapping Flatten out a mesh and overlay the model "skin" onto a texture image wraps a complex texture image onto a model 3D modelling utilities can "unwrap" a model move the vertices around to align with points on the texture utility will save uv coords for each vert (ie. in an OBJ or 3DS files) 3DS MAX the UVW unwrap modifier handles unwrapping models Alpha Blending Alpha blending is the process of drawing a shape into the framebuffer and blending the new pixels into the existing framebuffer image, instead of replacing (overwriting) what was already there. glEnable(GL_BLEND) turns on blending (by default OpenGL treats all rendering as opaque) glBlendFunc() specifies how OpenGL will blend the incoming (source) RGBA values with the RGBA values that are already in the frame buffer (destination) glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA) a common blendfunc combo that creates a translucent quality other blending operations are available Textures can be made transparent two ways: if lighting is disabled: call glColor() with an alpha value that is less than 1 glColor(1,1,1,.5f); // 50% opacity if lighting is enabled: set the DIFFUSE material color to an alpha value less than 1 use glMaterial() float mtlDiffuse[] = { 1,1,1,.5f }; GL11.glMaterial(GL11.GL_FRONT, GL11.GL_DIFFUSE, allocFloats(mtlDiffuse)); or use the GLMaterial class GLMaterial.setDiffuse( new float[] {1,1,1,.5f} ); GLMaterial.apply();