Module 01 - hornad.fei.tuke.sk

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 Blending
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)
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture mapping (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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture mapping (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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture mapping (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.
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture mapping (4)

Syntax of setting the current texture coordinates in OpenGL:
glBegin(type of primitive mode);
…
glTexCoordXT(s, t, r, q);
…
glEnd();
•
// texture coordinates
Understanding function naming
glTexCoordXT(s, t, r, q);
gl
TexCoord
X
T
s
t
r
q
– gl library
– root command for texture coordination
– number of arguments (1, 2, 3, 4)
– type of arguments (d, f, i, s)
– the horizontal texture image coordinate
– the vertical texture image coordinate
– the texture image depth coordinate
– the texture image “time” coordinate
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture mapping (5)
Example:
…
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f,
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f,
glEnd()
…
0.0f);
0.0f);
0.0f);
0.0f);
-1
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture mapping (6)
Advance example:
Triangle mesh textured with base texture
+
Triangle mesh
=
Base texture
Result of texture mapping
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture filtering techniques

(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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture filtering techniques
(2)
Examples:
Texture without texture filtering
(GL_NEAREST)
Texture with texture filtering
(GL_LINEAR)
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture filtering techniques
(3)
Texture image filters:
Filter
Description
GL_NEAREST
GL_LINEAR
GL_NEAREST_MIPMAP_NEAREST
GL_NEAREST_MIPMAP_LINEAR
GL_LINEAR_MIPMAP_NEAREST
GL_LINEAR_MIPMAP_LINEAR
Nearest-neighbor filtering
Linear interpolation
Nearest-neighbor mipmapped filtering
Linear interpolated mipmaps
Linear interpolation of mipmaps
Linear interpolation of interpolated mipmaps
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture filtering techniques

(4)
Syntax of setting the texture image parameters:
…
glTexParameterT(GLenum target, GLenum pname, GLfloat param);
…
•
Understanding function naming
gl
TexParameter
T
target
pname
param
– gl library
– root command for texture image parameters
– type of arguments (i, f)
– must be one of GL_TEXTURE_1D or GL_TEXTURE_2D
– the texturing parameter to set
– the texture filtering parameter type
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Texture filtering techniques
(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);
…
}
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Basics 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.
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
OpenGL 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.
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Blending 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
Module 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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (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
…
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (4)
5) define function for loading files
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if (!Filename)
{
return NULL;
}
File=fopen(Filename,"r");
if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
// end of LoadBMP()
// Loads A Bitmap Image
// File Handle
// Make Sure A Filename Was Given
// If Not Return NULL
// Check To See If The File Exists
// Does The File Exist?
// Close The Handle
// Load The Bitmap And Return A Pointer
// If Load Failed Return NULL
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (5)
6) define function for converting loaded files into textures
int LoadGLTextures()
/
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
/ Load Bitmaps And Convert To Textures
// Status Indicator
// Create Storage Space For The Texture
// 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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (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]);
}
}
return Status;
// end of LoadGLTextures()
// Free The Image Structure
// Return The Status
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (7)
7) load and enable the new texture while setting up the OpenGL environment
int InitGL(GLvoid)
{
if (!LoadGLTextures())
{
return FALSE
}
}
glEnable(GL_TEXTURE_2D);
…
// end of InitGL()
// All Setup For OpenGL Goes Here
// Jump To Texture Loading Routine
// If Texture Didn't Load Return FALSE
// Enable Texture Mapping
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (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,
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f,
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f,
…
// end of DrawGLScene()
1.0f);
1.0f);
1.0f);
1.0f);
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (9)
8) draw a texture mapped 3D cube
int DrawGLScene(GLvoid)
{
…
glTexCoord2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
...
}
// end of DrawGLScene()
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f,
glVertex3f(-1.0f,
glVertex3f( 1.0f,
glVertex3f( 1.0f,
1.0f, -1.0f);
1.0f, 1.0f);
1.0f, 1.0f);
1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Load a bitmap (BMP) file (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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Final result
Loading a bitmap (BMP) file
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Practice
Open:
Lab04 / SimpleTexture /SimpleTexture.sln
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (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)
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (2)
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;
GLfloat
yrot;
GLfloat xspeed;
GLfloat yspeed;
GLfloat
z=-5.0f;
GLuint texture[3];
…
// X Rotation
// Y Rotation
// X Rotation Speed
// Y Rotation Speed
// Depth Into The Screen
// Storage For Three Textures
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (3)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (4)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (5)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (6)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (7)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (8)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add texture filtering and keyboard
control to the project (9)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Final result
Texture filtering and keyboard control
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Practice
Open:
Lab04 / SimpleTexture2 / SimpleTexture2.sln
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (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[]=
GLfloat LightDiffuse[]=
GLfloat LightPosition[]=
…
{ 0.5f, 0.5f, 0.5f, 1.0f };
{ 1.0f, 1.0f, 1.0f, 1.0f };
{ 0.0f, 0.0f, 2.0f, 1.0f };
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (4)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (5)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (6)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (7)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (8)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add lighting to the scene (9)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Final result
OpenGL lighting
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Practice
Open:
Lab04 / SimpleTexture3 / SimpleTexture3.sln
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add blending to textures (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
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add blending to textures (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
bool
…
bp;
blend;
// B Pressed?
// Blending OFF/ON
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add blending to textures (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);
// Set The Blending Function For Translucency
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
…
}
// end of InitGL()
// Full Brightness. 50% Alpha
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add blending to textures (4)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add blending to textures (5)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Add blending to textures (6)
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()
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Final result
OpenGL blending
Module 04 – Texture mapping, Texture filtering, Lighting and Blending
Practice
Open:
Lab04 / Blending1 / Blending1.sln