module 04 – texture mapping, texture filtering, lighting and blending module 04 texture mapping,...

59
Module 04 – Texture mapping, Texture filtering, Lighting Module 04 – Texture mapping, Texture filtering, Lighting and Blending and Blending Module 04 Module 04 Texture mapping, Texture Texture mapping, Texture filtering, Lighting and filtering, Lighting and Blending Blending

Upload: candice-burns

Post on 12-Jan-2016

267 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Module 04Module 04

Texture mapping, Texture Texture mapping, Texture filtering, Lighting and Blendingfiltering, Lighting and Blending

Page 2: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Overview Overview

Understanding texture mapping

Understanding texture filtering techniques

Basics about the bitmap (BMP) file

OpenGL lighting

Blending and transparency

Bump mapping

Programming OpenGL program

(loading BMP files, lighting, keyboard control , blending)

Page 3: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture mapping Texture mapping (1)(1)

OpenGL provides texture image mapping functions that fit images onto

polygons

Texture maps are composed of rectangular arrays of data. Each element of

these arrays is called a texel. Although they are rectangular arrays of data,

texture maps can be mapped to non-rectangular objects, such as spheres,

cylinders, and other 3D object models

Usually, developers use the 2D texture in their graphics, however, using 1D, 3D

and 4D textures have some unique benefits (shading, …)

When you map a texture onto a polygon, the texture will be transformed as the

polygon is transformed

Page 4: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture mapping Texture mapping (2)(2)

The 1D textures have a width and a height equal to only 1 pixel

The 2D textures have a width and a height

The 3D textures have a width, height, and depth and are sometimes called

volume textures

The 4D textures have a width, height depth and the texture image “time”

coordinate

Page 5: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture mapping Texture mapping (3)(3)

Texture coordinates

Texture coordinates are used to determine exactly how to apply a texture map to a

polygon. The lower-left corner of a texture is given the coordinates (0, 0), while the

upper-right corner of a texture is given the coordinates (1, 1). Texture coordinates

for 2D textures are given the notation (s, t), where s and t are equal to a value from

0 to 1.

Page 6: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture mapping Texture mapping (4)(4)

Syntax of setting the current texture coordinates in OpenGL:

glBegin(type of primitive mode);…glTexCoordXT(s, t, r, q); // texture coordinates…

glEnd();

• Understanding function naming

glTexCoordXT(s, t, r, q);

gl – gl libraryTexCoord – root command for texture coordinationX – number of arguments (1, 2, 3, 4)T – type of arguments (d, f, i, s)s – the horizontal texture image coordinatet – the vertical texture image coordinater – the texture image depth coordinateq – the texture image “time” coordinate

Page 7: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture mapping Texture mapping (5)(5)

Example:

glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);

glEnd()

-1

Page 8: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture mapping Texture mapping (6)(6)

Advance example:

Triangle mesh textured with base texture

+ =

Triangle mesh Base texture Result of texture mapping

Page 9: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture filtering techniques Texture filtering techniques (1)(1)

After a texture map has been applied to a transformed polygon, a single screen

pixel can represent a fraction of a texel if the viewpoint is close to the texture,

or a pixel can represent a collection of texels when the viewpoint is further

away. Texture filtering tells OpenGL how it should map the texels to pixels

when calculating the final image.

In texture filtering, magnification refers to when a screen pixel represents a

small portion of a texel. Minification refers to when a pixel contains a number of

texels. You can tell OpenGL how to handle both of these filtering cases with the

glTexParameter() function

Page 10: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture filtering techniques Texture filtering techniques (2)(2)

Examples:

Texture without texture filtering (GL_NEAREST)

Texture with texture filtering (GL_LINEAR)

Page 11: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture filtering techniques Texture filtering techniques (3)(3)

Texture image filters:

Filter Description

GL_NEAREST Nearest-neighbor filteringGL_LINEAR Linear interpolationGL_NEAREST_MIPMAP_NEAREST Nearest-neighbor mipmapped filtering GL_NEAREST_MIPMAP_LINEAR Linear interpolated mipmaps GL_LINEAR_MIPMAP_NEAREST Linear interpolation of mipmaps GL_LINEAR_MIPMAP_LINEAR Linear interpolation of interpolated mipmaps

Page 12: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture filtering techniques Texture filtering techniques (4)(4)

Syntax of setting the texture image parameters:

…glTexParameterT(GLenum target, GLenum pname, GLfloat param); …

• Understanding function naming

gl – gl libraryTexParameter – root command for texture image parametersT – type of arguments (i, f)target – must be one of GL_TEXTURE_1D or GL_TEXTURE_2Dpname – the texturing parameter to setparam – the texture filtering parameter type

Page 13: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Texture filtering techniques Texture filtering techniques (5)(5)

Example:

int LoadGLTextures()

{

// type of texture filtering (GL_NEAREST) to use when the image is larger

// (GL_TEXTURE_MAG_FILTER) or stretched on the screen than the original texture,

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

// type of texture filtering to use (GL_LINEAR) when the image is smaller

// (GL_TEXTURE_MIN_FILTER) on the screen than the actual texture

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

}

Page 14: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Basics about the bitmap (BMP) fileBasics about the bitmap (BMP) file

• Despite their limitations, Windows .BMP files are probably the most common and

widely supported files used by PCs capable of from 2 to 16.7 million colors.

• With only a few exceptions, .BMP files do not utilize data compression schemes,

so it’s simple to read and use these files in OpenGL programs.

• A .BMP file is organized into three or four sections, depending on the type of

colors used.

Page 15: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

OpenGL lightingOpenGL lighting

Lighting is a technique which adds realism to the 3D scene.

Light types:

Ambient light simulates light bouncing between surfaces so many times that the

source of the light is no longer apparent. This component is not affected by the

position of either the light or the viewer.

Diffuse light comes from a certain direction, but once it strikes a surface, it is

reflected equally in all directions. The diffuse lighting component is affected by the

position or direction of the light, but not the position of the viewer.

Specular light is directional and reflected off a surface in a particular direction.

Specularity is often referred to as shininess. The specular term is affected by the

position of both the light and the eye.

Emissive light is a cheap way to simulate objects that emit light. OpenGL does not

actually use the emissive term to illuminate surrounding objects; it simply causes

the emissive object to be more intensely lit.

Page 16: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

0

Blending and transparencyBlending and transparency

• Most special effects in OpenGL rely on some type of blending. • Blending is used to combine the color of a given pixel that is about to be drawn with

the pixel that is already on the screen. How the colors are combined is based on the alpha value of the colors, and/or the blending function that is being used.

3D cube without blending 3D cube with blending

Page 17: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Drawing scene

Steps to do:

1) load a bitmap (BMP) file

2) add a texture filtering and keyboard control to the project

3) add lighting to the scene

4) add blending to the textures

Page 18: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (1)(1)

Steps to do:

1) include header file which allows to work with files

2) declare new global function for loading files

3) declare new global function for converting loaded files into textures

4) declare new global variable which sets storage space for one texture

5) define function for loading files

6) define function for converting loaded files into textures

7) load and enable the new texture wtile setting up the OpenGL environment

8) draw a texture mapped 3D cube

Page 19: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (2)(2)

1) include header file which allows to work with files

//--------------------------------------------------------------------------------

// Header Files

//--------------------------------------------------------------------------------

#include <stdio.h> // Header File For Standard Input/Output

2) declare new global function for loading files

//--------------------------------------------------------------------------------

// Global Functions

//--------------------------------------------------------------------------------

AUX_RGBImageRec *LoadBMP(char *Filename); // Declaration For LoadBMP

Page 20: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (3)(3)

3) declare new global function for converting loaded files into textures

//--------------------------------------------------------------------------------

// Global Functions

//--------------------------------------------------------------------------------

int LoadGLTextures(); // Declaration For LoadGLTextures

4) declare new global variable which sets storage space for one texture

//--------------------------------------------------------------------------------

// Global Variables

//--------------------------------------------------------------------------------

GLuint texture[1]; // Storage For One Texture

Page 21: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (4)(4)

5) define function for loading files

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image

{

FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given

{

return NULL; // If Not Return NULL

}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?

{

fclose(File); // Close The Handle

return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer

}

return NULL; // If Load Failed Return NULL

} // end of LoadBMP()

Page 22: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (5)(5)

6) define function for converting loaded files into textures

int LoadGLTextures() / / Load Bitmaps And Convert To Textures

{

int Status=FALSE; // Status Indicator

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit

if (TextureImage[0]=LoadBMP("Data/Texture.bmp"))

{

Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap

glBindTexture(GL_TEXTURE_2D, texture[0]);

} // end of LoadGLTextures()

Page 23: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (6)(6)

6) define function for converting loaded files into textures

int LoadGLTextures()

{

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

}

if (TextureImage[0]) // If Texture Exists

{

if (TextureImage[0]->data) // If Texture Image Exists

{

free(TextureImage[0]->data); // Free The Texture Image Memory

}

free(TextureImage[0]); // Free The Image Structure

}

return Status; // Return The Status

} // end of LoadGLTextures()

Page 24: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (7)(7)

7) load and enable the new texture while setting up the OpenGL environment

int InitGL(GLvoid) // All Setup For OpenGL Goes Here

{

if (!LoadGLTextures()) // Jump To Texture Loading Routine

{

return FALSE // If Texture Didn't Load Return FALSE

}

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

} // end of InitGL()

Page 25: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (8)(8)

8) draw a texture mapped 3D cube

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{

glLoadIdentity(); // Reset The View

glTranslatef(0.0f,0.0f,-5.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture

glBegin(GL_QUADS);

// Front Face

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

} // end of DrawGLScene()

Page 26: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (9)(9)

8) draw a texture mapped 3D cube

int DrawGLScene(GLvoid)

{

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

// Top Face

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

// Bottom Face

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

...

} // end of DrawGLScene()

Page 27: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Load a bitmap (BMP) file Load a bitmap (BMP) file (10)(10)

8) draw a texture mapped 3D cube

int DrawGLScene(GLvoid)

{

// Right face

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

// Left Face

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glEnd();

} // end DrawGLScene()

Page 28: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Final result

Loading a bitmap (BMP) file

Page 29: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Practice

Open:

Lab04 / SimpleTexture /SimpleTexture.sln

Page 30: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add texture filtering and keyboard Add texture filtering and keyboard control to the projectcontrol to the project (1)(1)

Steps to do:

1) declare new variables to store whether or not the “F” key has been pressed,

new variables to keep track of which filtering technique we want to use,

and new variables to change and control axis angles and rotation speed.

2) generate textures for nearest, linear and mippapped filtering in LoadGLTextures

3) check for keyboard F, up, down, left, right, page up and page down pressing in

while(!done) loop in WinMain() function

4) draw a 3D cube with appropriate respond to keyboard actions (rotating the cube,

translating the cube and changing the cubes texture filtering mode)

Page 31: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

1) declare new variables to store whether or not the “F” key has been pressed,

new variables to keep track of which filtering technique we want to use,

and new variables to change and control axis angles and rotation speed.

//--------------------------------------------------------------------------------

// Global Variables

//--------------------------------------------------------------------------------

bool fp; // F Pressed?

GLuint filter; // Which Filter To Use

GLfloat xrot; // X Rotation

GLfloat yrot; // Y Rotation

GLfloat xspeed; // X Rotation Speed

GLfloat yspeed; // Y Rotation Speed

GLfloat z=-5.0f; // Depth Into The Screen

GLuint texture[3]; // Storage For Three Textures

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (2)(2)

Page 32: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

2) generate textures for nearest, linear and mippapped filtering in LoadGLTextures

Int LoadGLTextures()

{

if (TextureImage[0]=LoadBMP("Data/Texture.bmp"))

{

glGenTextures(3, &texture[0]); // Create Three Textures

// Create Nearest Filtered Texture

glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

} // end of LoadGLTextures()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (3)(3)

Page 33: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

2) generate textures for nearest, linear and mippapped filtering in LoadGLTextures

Int LoadGLTextures()

{

// Create Linear Filtered Texture

glBindTexture(GL_TEXTURE_2D, texture[1]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Create MipMapped Texture

glBindTexture(GL_TEXTURE_2D, texture[2]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,

GL_LINEAR_MIPMAP_NEAREST);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX,

TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

} // end of LoadGLTextures()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (4)(4)

Page 34: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard F, up, down, left, right, page up and page down pressing in

while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine,

int nCmdShow)

{

while(!done) // Loop That Runs While done=FALSE

{

else // If There Are No Messages

{

DrawGLScene()

else // Not Time To Quit, Update Screen

{

} // end of WinMain()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (5)(5)

Page 35: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard F, up, down, left, right, page up and page down pressing in

while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

SwapBuffers(hDC);if (keys['F'] && !fp)

{

fp=TRUE;

filter+=1;

if (filter>2)

{

filter=0;

}

}

if (!keys['F'])

{

fp=FALSE;

}

} // end of WinMain()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (6)(6)

Page 36: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard F, up, down, left, right, page up and page down pressing in

while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

if (keys[VK_PRIOR])

{

z-=0.02f;

}

if (keys[VK_NEXT])

{

z+=0.02f;

}

if (keys[VK_UP])

{

xspeed-=0.01f;

}

} // end of WinMain()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (7)(7)

Page 37: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard F, up, down, left, right, page up and page down pressing in

while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

if (keys[VK_DOWN])

{

xspeed+=0.01f;

}

if (keys[VK_RIGHT])

{

yspeed+=0.01f;

}

if (keys[VK_LEFT])

{

yspeed-=0.01f;

}

} //end while(!done)

} // end of WinMain()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (8)(8)

Page 38: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

4) draw a 3D cube with appropriate respond to keyboard actions (rotating the cube,

translating the cube and changing the cubes texture filtering mode)

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{

glLoadIdentity();

glTranslatef(0.0f,0.0f,z);

glRotatef(xrot,1.0f,0.0f,0.0f);

glRotatef(yrot,0.0f,1.0f,0.0f);

glBindTexture(GL_TEXTURE_2D, texture[filter]);

glBegin(GL_QUADS);

… // here draw textured 3D cube

glEnd();

xrot+=xspeed;

yrot+=yspeed;

} //end of DrawGLScene()

Add texture filtering and keyboard Add texture filtering and keyboard control to the project control to the project (9)(9)

Page 39: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Final result

Texture filtering and keyboard control

Page 40: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Practice

Open:

Lab04 / SimpleTexture2 / SimpleTexture2.sln

Page 41: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add lighting to the scene Add lighting to the scene (1)(1)

Steps to do:

1) declare new variables to store whether or not the “L” key has been pressed,

declare new variable to keep track of whether or not the lighting is on or off,

set up arrays, that will be used to create the light

2) while setting up OpenGL, set up ambient and diffuse lighting, position the light,

and enable the light

3) check for keyboard L pressing in while(!done) loop in WinMain() function

4) draw a 3D cube with appropriate normal vectors for lighting

Page 42: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add lighting to the scene Add lighting to the scene (2)(2)

1) declare new variables to store whether or not the “L” key has been pressed,

declare new variable to keep track of whether or not the lighting is on or off,

set up arrays, that will be used to create the light

//--------------------------------------------------------------------------------

// Global Variables

//--------------------------------------------------------------------------------

bool lp; // L Pressed?

bool light; // Lighting ON/OFF

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };

GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };

GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

Page 43: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add lighting to the scene Add lighting to the scene (3)(3)

2) while setting up OpenGL, set up ambient and diffuse lighting, position the light,

and enable the light

Int InitGL(GLvoid)

{

glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light

glEnable(GL_LIGHT1); // Enable Light One

} // end of InitGL()

Page 44: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard L pressing in while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine,

int nCmdShow)

{

while(!done) // Loop That Runs While done=FALSE

{

else // If There Are No Messages

{

DrawGLScene()

else // Not Time To Quit, Update Screen

{

} // end of WinMain()

Add lighting to the scene Add lighting to the scene (4)(4)

Page 45: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard L pressing in while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine,

int nCmdShow)

{

if (keys['L'] && !lp)

{

lp=TRUE;

light=!light;

if (!light)

{

glDisable(GL_LIGHTING);

}

else

{

glEnable(GL_LIGHTING);

}

}

} // end of WinMain()

Add lighting to the scene Add lighting to the scene (5)(5)

Page 46: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard L pressing in while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

if (!keys['L'])

{

lp=FALSE;

}

} //end while(!done)

} // end of WinMain()

Add lighting to the scene Add lighting to the scene (6)(6)

Page 47: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

4) draw a 3D cube with appropriate normal vectors for lighting

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{

glBegin(GL_QUADS);

// Front Face

glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

// Back Face

glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

} //end of DrawGLScene()

Add lighting to the scene Add lighting to the scene (7)(7)

Page 48: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

4) draw a 3D cube with appropriate normal vectors for lighting

int DrawGLScene(GLvoid)

{

// Top Face

glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

// Bottom Face

glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

} //end of DrawGLScene()

Add lighting to the scene Add lighting to the scene (8)(8)

Page 49: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

4) draw a 3D cube with appropriate normal vectors for lighting

int DrawGLScene(GLvoid)

{

// Right face

glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);

// Left Face

glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);

glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);

glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);

glEnd();

} // end DrawGLScene()

Add lighting to the scene Add lighting to the scene (9)(9)

Page 50: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Final result

OpenGL lighting

Page 51: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Practice

Open:

Lab04 / SimpleTexture3 / SimpleTexture3.sln

Page 52: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add blending to textures Add blending to textures (1)(1)

Steps to do:

1) declare new variables to store whether or not the “B” key has been pressed,

declare new variable to keep track of whether or not the blending is on or off,

2) while setting up OpenGL, set up brightness to full, alpha to 50% and set the

blending function for translucency

3) check for keyboard B pressing in while(!done) loop in WinMain() function

Page 53: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add blending to textures Add blending to textures (2)(2)

1) declare new variables to store whether or not the “B” key has been pressed,

declare new variable to keep track of whether or not the blending is on or off,

set up arrays, that will be used to create the light

//--------------------------------------------------------------------------------

// Global Variables

//--------------------------------------------------------------------------------

bool bp; // B Pressed?

bool blend; // Blending OFF/ON

Page 54: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Add blending to textures Add blending to textures (3)(3)

2) while setting up OpenGL, set up brightness to full, alpha to 50% and set the

blending function for translucency

Int InitGL(GLvoid)

{

glColor4f(1.0f, 1.0f, 1.0f, 0.5); // Full Brightness. 50% Alpha

// Set The Blending Function For Translucency

glBlendFunc(GL_SRC_ALPHA,GL_ONE);

} // end of InitGL()

Page 55: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard B pressing in while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine,

int nCmdShow)

{

while(!done) // Loop That Runs While done=FALSE

{

else // If There Are No Messages

{

DrawGLScene()

else // Not Time To Quit, Update Screen

{

} // end of WinMain()

Add blending to textures Add blending to textures (4)(4)

Page 56: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard B pressing in while(!done) loop in WinMain() function

Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

// Blending Code Starts Here

if (keys['B'] && !bp)

{

bp=TRUE;

blend = !blend;

if(blend)

{

glEnable(GL_BLEND); // Turn Blending On

glDisable(GL_DEPTH_TEST); // Turn Depth Testing Off

}

else

{

glDisable(GL_BLEND); // Turn Blending Off

glEnable(GL_DEPTH_TEST); // Turn Depth Testing On

}

}

} // end of WinMain()

Add blending to textures Add blending to textures (5)(5)

Page 57: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

3) check for keyboard B pressing in while(!done) loop in WinMain() function…

if (!keys['B'])

{

bp=FALSE;

}

// Blending Code Ends Here

..

} // end while(!done)

} // end of WinMain()

Add blending to textures Add blending to textures (6)(6)

Page 58: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Final result

OpenGL blending

Page 59: Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending

Module 04 – Texture mapping, Texture filtering, Lighting and BlendingModule 04 – Texture mapping, Texture filtering, Lighting and Blending

Practice

Open:

Lab04 / Blending1 / Blending1.sln