lecture 8: texture mapping 1 principles of interactive graphics cmscd2012 dr david england, room...

15
JMU Lecture 8: Texture Mapping Lecture 8: Texture Mapping 1 Principles of Interactive Principles of Interactive Graphics Graphics CMSCD2012 CMSCD2012 Dr David England, Room 718, ex 2271 [email protected]

Upload: magdalene-perry

Post on 12-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 1

Principles of Interactive GraphicsPrinciples of Interactive Graphics CMSCD2012CMSCD2012

Dr David England, Room 718, ex 2271 [email protected]

Page 2: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 2

Coursework 2

Coursework 2 On web page and hardcopy available from me

Page 3: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 3

Today’s Lecture: Textures

What are textures?

How can they be created ?

How are texture attributes set up?

Applying textures to objects

Page 4: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 4

Texture Example 1

The background can be drawn in a paint tool and the texture pasted onto a Quad

Page 5: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 5

Texture Example 2

Textures on

• background• ground• building• bridge• water

•Some •individual•tiled

Page 6: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 6

What are textures?

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 or pixmap Depth information can represent colour, (or lighting or

transparency) OpenGL also has 1D and 3D textures (see chapter 9 of

OpenGL Programming book)

Page 7: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 7

Creating Textures ...

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

Page 8: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 8

Creating Textures …within a program

The example program ch9text.cpp creates an array of 4-byte values checkImage

Each array item can hold values for R, G, B and alpha

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

Page 9: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 9

Using textures … 1

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?

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

Page 10: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 10

Using textures … 2

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

Page 11: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 11

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

Page 12: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 12

Texture Co-ordinates

In the first example we have used texture coordinates of 0.0 and 1.0. The entire polygon is mapped across the QUAD

Page 13: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 13

Texture Co-ordinates

In the second example we have loaded a TGA image into the program

The image is mapped the texture to different faces of a 3D cube using glTexCoord2f()

The texture coordinates are different for each face For example, Coordinates of:

0.0 and 1.0 - full image mapped 0.5 and 0.0 - part of image mapped and so on

Page 14: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 14

Texture tutorial 1

In chp9text.cpp what is the affect of changing GL_REPLACE to GL_MODULATE?

Try experimenting with different coordinate values in glTexCoord2f(). What are the results?

For the second example you need to unzip the file texture.zip into your M: drive space

The C source and header files should be added to a new project

The image file texture.tga should be in the same folder as the project

Page 15: Lecture 8: Texture Mapping 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

JMU Lecture 8: Texture MappingLecture 8: Texture Mapping 15

Texture tutorial 2

If you want to use your own image files: Make sure the width and height are a power of 2 (16,32,

64, 128, 256) Save it as an uncompressed TGA file called texture.tga

(or alter the name in the code Question:

What is the affect of different values for tex_size in the display() function?

Next 2 weeks: perspective and lighting