opengl programming part ii -...
TRANSCRIPT
Graphics
Graphics Lab @ Korea University http://kucg.korea.ac.kr
OpenGL Programming
Part II
Korea Univ.
Computer Graphics Lab.
KUCG
Graphics Lab @ Korea University
View Volume
void glOrtho(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble zNear,
GLdouble zFar)
void glFrustum(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble zNear,
GLdouble zFar)
void gluPerspective(GLdouble fov,
GLdouble aspect, GLdouble near, GLdouble far)
fov : field of view (degree, 0~180)
aspect : aspect ratio (Width/Height)
near : near clipping plane
far : far clipping plane
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
How the View Volume Works (1/2)
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
Projection
Projection
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
How the View Volume Works (2/2)
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
Projection
Projection
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
glFrustum Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Viewing Parameters
View Reference Point (px, py, pz)
Eye point
View Direction (dx, dy, dz)
Up Vector (ux, uy, uz)
Field of view (xfov, yfov)
Viewplane
“Look at” point
Viewplane normal
Up Vector
Right
Vector
View Reference
Point
Viewplane
“Look at” Point
View Direction
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Viewing Transformation
void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
GLdouble atx, GLdouble aty, GLdouble atz, GLdouble upx, GLdouble upy,
GLdouble upz)
eyex, eyey, eyez : Camera Position(x, y, z)
atx, aty, atz : Camera Focus(x, y, z)
upx, upy, upz : Camera Up-vector
Default
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
gluLookAt Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Viewport Transformation
glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
Left-low corner coordinate (x, y)
Viewport size (width, height)
Example // initialize the size of a window
glutInitWindowSize(200, 200);
// sets the position and size of a viewport
glViewport(0, 0, 100, 100); (0, 0) (100, 0) (200, 0)
(0, 100)
(0, 200)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Viewport Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Callback Functions (1/3)
void glutReshapeFunc(void(*func)(int width,
int height))
Registers the callback function called when the
window size or shape is changed
width : new width of a window
height : new height of a window
void glutKeyboardFunc(void(*func)(unsigned char key, int x,
int y))
Registers the callback function called when a key is
pressed
key : ASCII code of the pressed key
(x, y) : the coordinate of a mouse
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Keyboard Callback Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Callback Functions (2/3)
void glutMouseFunc(void(*func)(int button, int state, int x, int y))
Registers the callback function called when a mouse button is pressed button
GLUT_LEFT_BUTTON , GLUT_RIGHT_BUTTON, GLUT_MIDDLE_BUTTON
state
GLUT_DOWN, GLUT_UP
(x, y) : the coordinate of a mouse
void glutMotionFunc(void(*func)(int x, int y))
Registers the callback function called when a mouse is dragged
void glutPassiveMotionFunc(void(*func)(int x, int y))
Registers the callback function called when a mouse is moved without being pressed
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Mouse Callback Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Callback Functions (3/3)
void glutIdleFunc(void (*func)(void))
Registers the callback function called when there are
no events to be processed idle time
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Front Face Detection (1/2)
GLuint glFrontFace(GLenum mode)
Determine which direction of the face definition is the
front face
mode
GL_CCW : counter-clockwise (default)
GL_CW : clockwise
GLuint glCullFace(GLenum mode)
Determine which face must be culled
mode
GL_FRONT
GL_BACK (default)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Front Face Detection (2/2)
void glEnable/glDisable(GLenum feature)
Enable or disable the operation of OpenGL according
to the macro „feature‟
feature
GL_CULL_FACE
Default : glDisable(GL_CULL_FACE);
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Matrix-related Functions (1/2)
void glMatrixMode(GLenum mode)
Sets the type of a matrix stack to be processed
mode
GL_MODELVIEW
When we move the object
GL_PROJECTION
When we define the clipping view volume
GL_TEXTURE
When we manipulate the texture coordinates
void glLoadIdentity()
Loads the identity matrix on the current matrix stack
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Matrix-related Functions (2/2)
void glLoadMatrixf(const GLfloat *M)
Loads the given matrix on the current matrix stack
void glMultMatrixf(const GLfloat *M)
Multiplies the given matrix with the current matrix of
the matrix stack
*M
16-length array
means 4x4 matrix
Column-major notation
151173
141062
13951
12840
mmmm
mmmm
mmmm
mmmm
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Transformations : Rotation
void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Rotation axis (x, y, z)
Example // Declare the transformation mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// z-Rotate the objects by 45 degree
glRotatef(45.0, 0.0, 0.0, 1.0);
// Draw the solid cube
glutSolidCube(0.3);
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Transformations : Translation
void glTranslatef(GLfloat dx, GLfloat dy, GLfloat dz)
Example glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Translate the objects by (0.5, -0.5, 0.0)
glTranslatef(0.5, -0.5, 0.0);
glutSolidCube(0.3);
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Transformations : Scaling
void glScalef(GLfloat sx, GLfloat sy, GLfloat sz)
Example glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Scale the objects by (2.0, 1.0, 1.0)
glScalef(2.0, 1.0, 1.0);
glutSolidCube(0.3);
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Composite Transformation Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
OpenGL Lighting
Turn on the „lighting calculation‟ and each „light
source‟ glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
All colors will be assigned based on
Light sources and
Material properties
Not by glColor3f( )
Maximum # of Light Sources : 8(GL_LIGHT0~7)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Enable Lighting (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Enable Lighting (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Light Function
void glLightfv(GLenum light, GLenum pname, const GLfloat*
params)
Selects one of the available light sources and assigns its
various properties
Light
GL_LIGHT0 ~ GL_LIGHT7
pname
Parameter name
params
Parameter value
ex)
float ambientLight[] = {1.0f, 1.0f, 1.0f, 1.0f}; // white ambient light
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); // sets ambient light property
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Light Function Parameters
pname Default Value Meaning
GL_POSITION (0.0, 0.0, 1.0, 0.0) Light Position (x, y, z, w)
GL_SPOT_DIRECTION (0.0, 0.0, -1.0) Spotlight Direction Vector (x, y, z)
GL_SPOT_CUTOFF 180.0 Spotlight Cutoff Angle
GL_SPOT_EXPONENT 0.0 Spotlight Concentrating Factor n
GL_AMBIENT (0.0, 0.0, 0.0, 1.0) Ambient Light Intensity
GL_DIFFUSE (1.0, 1.0, 1.0, 1.0) Diffuse Light Intensity
GL_SPECULAR (1.0, 1.0, 1.0, 1.0) Specular Light Intensity
GL_CONSTANT_ATTENUNATION 1.0 Attenuation Constant Coefficient k0
GL_LINEAR_ATTENUATION 0.0 Attenuation Linear Coefficient k1
GL_QUADRATIC_ATTENUNATION 0.0 Attenuation Quadratic Coefficient k2
2
210
1
dkdkkatt
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Light Position
GL_POSITION
(x, y, z, 1) : point light source at (x, y, z)
(x, y, z, 0) : directional light source from (x, y, z) direction
ex)
GLfloat LightPosition[] = {0.0, 1.0, 0.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
GLfloat LightPosition[] = {-1.0, 1.0, 0.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Spotlight
Set a point light source
Then assign some spotlight properties
GL_SPOT_CUTOFF
GL_SPOT_DIRECTION
GL_SPOT_EXPONENT
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Material Property
void glMaterialfv(GLenum face, GLenum pname, GLfloat *param)
Defines material properties for the light calculation
face : where to apply
GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
pname : property name
param : property value
pname Meaning
GL_AMBIENT Ambient Coefficient Ka
GL_DIFFUSE Diffuse Coefficient Kd
GL_AMBIENT_AND_DIFFUSE Ambient/Diffuse Coefficient Ka & Kd
GL_SPECULAR Specular Coefficient Ks
GL_SHININESS Reflection Concentrating Factor n
GL_EMISSION Emitted Light Intensity
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Lighting Models
void glLightModelfv(GLenum pname, GLfloat* param)
pname : property name
param : property value
Pname Meaning
GL_LIGHT_MODEL_AMBIENT Ambient Light Intensity
Default : (0.2, 0.2, 0.2, 1.0)
GL_LIGHT_MODEL_LOCAL_VIWER Set the viewing vector same to the all polygons or not?
Default : GL_FALSE (Same)
GL_LIGHT_MODEL_TWO_SIDE Illuminate the back faces or not?
Default : GL_FALSE (Only Front Faces)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Specifying a Light Source
Defaults for “glEnable(GL_LIGHT0)”
No ambient light
Diffuse and specular component
(1.0, 1.0, 1.0, 1.0) pure white
Position
(0.0, 0.0, 1.0, 0.0) directional light
void glLightfv(GLenum light, GLenum param, TYPE *value)
light
GL_LIGHT0, GL_LIGHT1, …
param
GL_POSITION, GL_DIFFUSE, GL_SPECLAR, GL_AMBIENT
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
light for glLightfv()
Directional Light
GL_POSITION, GL_DIFFUSE, GL_SPECULAR,
GL_AMBIENT
Point Light
GL_CONSTANT_ATTENUATION,
GL_LINEAR_ATTENUATION,
GL_QUADRATIC_ATTENUATION
Spotlight
GL_SPOT_DIRECTION, GL_SPOT_CUTOFF,
GL_SPOT_EXPONENT
2
210
1
dkdkkatt
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Lighting Example
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Point Light (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Point Light (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Control of Light Position (1/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Control of Light Position (2/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Control of Light Position (3/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Control of Light Position (4/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Complex Light (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Complex Light (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Attenuation : Point Light (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Attenuation : Point Light (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Spotlight (1/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Spotlight (2/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Spotlight (3/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Spotlight (4/4)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Multiple Lights (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Multiple Lights (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Specifying a Material (1/5)
void glMaterialfv(GLenum face, GLenum name, TYPE*
value)
Defines the material reflection properties
face
GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
name
GL_DIFFUSE, GL_SPECLAR, GL_AMBIENT,
GL_AMBIENT_AND_DIFFUSE, GL_EMISSION,
GL_SHININESS
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Specifying a Material (2/5)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Specifying a Material (3/5)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Specifying a Material (4/5)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Specifying a Material (5/5)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
White Shiny Material (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
White Shiny Material (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Brass Material (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Brass Material (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Red Plastic Material (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Red Plastic Material (2/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Multiple Objects (1/2)
http://kucg.korea.ac.kr
KUCG
Graphics Lab @ Korea University
Multiple Objects (2/2)
http://kucg.korea.ac.kr