![Page 1: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/1.jpg)
CS380 LAB IVOpenGL
Jonghyeob Lee
Reference1. [OpenGL course slides by Rasmus Stenholt]
Reference2. [http://nehe.gamedev.net/]
![Page 2: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/2.jpg)
Goal
Introduce OpenGL programming Help you do CS380 homework by yourself
2
![Page 3: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/3.jpg)
Notice
Use Noah board for your questions (http://noah.kaist.ac.kr/course/CS380)
3
![Page 4: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/4.jpg)
Outline
Lighting and Materials Texture Mapping
4
![Page 5: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/5.jpg)
5
Lighting and Materials
Why is this important? Geometry is only one third of rendering Lights and materials make up the other two Unlit objects tend to look dull and artificial
In OpenGL, lights and materials are connected Light interacts with materials Materials must be assigned to geometry to benefit
from lighting
![Page 6: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/6.jpg)
6
Lighting Principles
The result of lighting depends on various factors Material composition of object Light colour and position Global lighting parameters
ambient light two sided lighting
Lighting can be done in both RGBA and indexed color mode
![Page 7: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/7.jpg)
7
OpenGL Lighting
Lighting is computed per-vertex The lighting model is called Phong
shading Lighting properties
DiffuseSpecularAmbient
![Page 8: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/8.jpg)
8
Lighting
Normals define how a surface reflects light glNormal3f( x, y, z )
Normals can be defined per-vertex Just like colours
The current normal is used to compute lighting contributions The angles between the normal and light directions are used Use unit normals for proper lighting
Some transformations affect a normal’s length glEnable( GL_NORMALIZE )
or glEnable( GL_RESCALE_NORMAL )
![Page 9: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/9.jpg)
9
Light Properties
The command for setting lighting properties glLightfv(light, property, value); light specifies which light
Multiple lights, starting with GL_LIGHT0glGetIntegerv( GL_MAX_LIGHTS, &n );
The maximum number of lights is usually 8 Properties
colors position and type attenuation
Light color properties GL_AMBIENT GL_DIFFUSE GL_SPECULAR
![Page 10: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/10.jpg)
10
Types of Lights
OpenGL supports two types of lightsLocal (Point) light sources Infinite (Directional) light sources
Type of light controlled by w coordinate
Usually w=1 for a point light
wzw
ywxw
zyxw
at positioned Light Local
along directed LightInfinite
0
0
![Page 11: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/11.jpg)
11
Turning on the Lights
First you must tell OpenGL that lighting should be usedglEnable( GL_LIGHTING );
Then each light can be individually turned on/offglEnable( GL_LIGHTn );
![Page 12: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/12.jpg)
Load a solid teapot
Tutorials
12
void display() {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glutSolidTeapot(0.5);glFlush();
}
![Page 13: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/13.jpg)
Tutorials
Add a light on previous scene
13
void display() {glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);float light_pos[] = {-2, 2, 2, 1};float light_Ka[] = {0, 0, 0, 1};float light_Kd[] = {1, 1, 1, 1};float light_Ks[] = {1, 1, 1, 1};glLightfv(GL_LIGHT0, GL_POSITION, light_pos);glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ka);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Kd);glLightfv(GL_LIGHT0, GL_SPECULAR, light_Ks);…
}
![Page 14: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/14.jpg)
14
Material Properties
Define the surface properties of a primitive glMaterialfv( face, property, value );
Separate materials for front and back
GL_DIFFUSE Base color
GL_SPECULAR Highlight Color
GL_AMBIENT Low-light Color
GL_EMISSION Glow Color
GL_SHININESS Surface Smoothness
![Page 15: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/15.jpg)
Tutorials Add a materials on front
15
void display() {float material_Ka[] = {0.11, 0.06, 0.11, 1.00};float material_Kd[] = {0.43, 0.47, 0.54, 1.00};float material_Ks[] = {0.33, 0.33, 0.52, 1.00};float material_Ke[] = {0.00, 0.00, 0.00, 0.00};float material_Se[] = {10};glMaterialfv(GL_FRONT, GL_AMBIENT, material_Ka);glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);glMaterialfv(GL_FRONT, GL_SPECULAR, material_Ks);glMaterialfv(GL_FRONT, GL_EMISSION, material_Ke);glMaterialfv(GL_FRONT, GL_SHININESS, material_Se);
}
![Page 16: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/16.jpg)
Tutorials
Compare three teapots
16
![Page 17: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/17.jpg)
17
Texture Mapping
Applying a 1-D, 2-D, or 3-D image to geometric primitivesTexture coordinates are called (s,t,r,q)
Uses of texturingSimulating materialsReducing geometric complexityReflectionsAdvanced surface properties
![Page 18: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/18.jpg)
18
Texture Mapping
s
t
x
y
z
image
geometry screen
![Page 19: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/19.jpg)
19
Applying Textures
The short version1. Specify a texture image
1. Read or generate image2. Assign to texture3. Enable texturing
2. Specify texture parameters1. Wrapping
1. How texture edges are handled2. Filtering
1. How texture sampling is handled
3. Assign texture coordinates to vertices
![Page 20: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/20.jpg)
20
Generating a Texture Identifier
Generate texture namesglGenTextures( n, *texIds );
A texture name is just an integerThe texture name is assigned to some texture
data later on Example
unsigned int texName;glGenTextures(1, &texname)
![Page 21: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/21.jpg)
21
Binding Textures
Bind textures before usingglBindTexture( target, id ); Binding a texture means assigning a certain texture id to a certain
kind of texture The valid kinds of textures are
GL_TEXTURE_1D GL_TEXTURE_2D GL_TEXTURE_3D
ExampleglBindTexture( GL_TEXTURE_2D, texName);
![Page 22: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/22.jpg)
22
Filter Modes Handles the sampling mode of the texture Minification or magnification
Handles when the texture to screen mapping is not 1:1 Special mipmap minification filters
Wrap Modes Handles texture lookups outside the texture’s edges Clamping
Copies data from the edge of the texture Repeating
Tiles the texture Texture Functions
How to mix primitive’s color with texture’s color Blend Modulate Replace
Texture Application Methods
![Page 23: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/23.jpg)
23
Filter Modes
Texture Polygon
Magnification Minification
PolygonTexture
Example:
glTexParameteri( glTexParameteri( target, type, modetarget, type, mode ); );
![Page 24: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/24.jpg)
24
Wrapping Mode
Example:
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
texture
s
t
GL_CLAMPwrapping
GL_REPEATwrapping
![Page 25: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/25.jpg)
25
Based on parametric texture coordinates Texture coordinates are assigned to vertices
Just like colours and normals The n-D to 3-D mapping is the responsibility of
the programmer/modeller glTexCoord*() specified at each vertex
s
t
Mapping Texture
![Page 26: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/26.jpg)
26
Texture Mapping
0, 0 1, 0
(0.4, 0.2)
(0.8, 0.4)
A
B C
a
bc
1, 10, 1
(s, t) = (0.2, 0.8)
Texture Space Object Space
![Page 27: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/27.jpg)
Tutorials
Make checkerboard texture
27
int i, j, c; for (i = 0; i < 64; i++) { for (j = 0; j < 64; j++) { c = ((((i&0x8)==0)^((j&0x8))==0))*255; checkImage[i][j][0] = (GLubyte) c; checkImage[i][j][1] = (GLubyte) c; checkImage[i][j][2] = (GLubyte) c; checkImage[i][j][3] = (GLubyte) 255; } }
![Page 28: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/28.jpg)
Tutorials
Assign texture
28
glGenTextures(1, &texName); glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 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, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
![Page 29: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/29.jpg)
Tutorials
Texture mapping onto rectangle
29
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glBindTexture(GL_TEXTURE_2D, texName); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(0.5, -0.5, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, 0.5, 0.0); glEnd(); glFlush(); glDisable(GL_TEXTURE_2D);
![Page 30: CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [](https://reader038.vdocuments.site/reader038/viewer/2022103100/56649ec65503460f94bd2572/html5/thumbnails/30.jpg)
Q&A
Any questions?
30