140 likes | 295 Views
CD2012 Principles of Interactive Graphics Lecture 07. Texture Mapping Abir Hussain (JPB6.33, a.hussain@livjm.ac.uk). Previous lecture. Perspective projection Lighting Shading Viewpoint gluLookAt function 3D shapes and surfaces
E N D
CD2012Principles of Interactive GraphicsLecture 07 Texture Mapping Abir Hussain (JPB6.33, a.hussain@livjm.ac.uk)
Previous lecture • Perspective projection • Lighting • Shading • Viewpoint • gluLookAt function • 3D shapes and surfaces • specify each vertex as a point in 3D space using the x, y and z values CD2012-07
Today’s Lecture and Lab CD2012-07
Introduction • Textures: an image that can be glued onto a polygon • Reduces the need to draw many polygons to make up a complex image • Texture data: Rectangular areas of data with a width, height and depth • Usually a bitmap • Depth information can represent colour, (or lighting or transparency) • OpenGL also has 1D and 3D textures (see chapter 9 of OpenGL Programming book) CD2012-07
Introduction • Textures on • background • ground • building • bridge • water • Some • individual • tiled CD2012-07
Creating texture • Textures can be created by either • Creating the texture data in the program • Loading the texture data from an texture (image) file • OpenGL supports creating texture objects but has no support loading images. • Programmer has to provide • the image loading functions • Assign the image data to the texture object CD2012-07
Creating textures within program • The first step is to create the required image within the program • refer to ch9txt.cpp program in the lab. • The next steps are then the same for internal or file-based textures • Before the data can be used texture mapping must be enabled in OpenGL and a texture object created int textname; glEnable(GL_TEXTURE_2D); glGenTextures(1, &texname); CD2012-07
Using texture • We need to say what is the current texture to be used • glBindTexture(GL_TEXTURE_2D, texname); • We can then set the drawing context for applying the texture • For example should the texture appear once or be tiled? CD2012-07
Using texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); • We can use GL_GLAMP or GL_REPEAT CD2012-07
Using texture • We can also set how the texture affects the colours of the underlying polygon glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); • GL_REPLACE replaces the underlying colours • GL_MODULATE works with the underlying colours • Finally we set the co-ordinates of the texture as we draw the polygons to which it is applied. CD2012-07
Texture co-ordinates • The coordinates of textures are named s along the X axis and t along the Y axis • The values of S and T range between 0.0 and 1.0 • We can use values between 0.0 and 1.0 to select part of the imaged to be mapped, or • We can use values greater than 1.0 to map multiple copies of the single texture across a polygon • Texture coordinates are set for each vertex of the target polygon with glTexCoord2f() CD2012-07
Texture co-ordinates CD2012-07
Summary • Creating texture within program • Using texture • Texture co-ordinates CD2012-07