compound transformations, geometry, lighting and shadinglarsvmag/iti43309_13/lecture03.pdf ·...
TRANSCRIPT
Compound Transformations, Geometry, Lightingand Shading
Lars Vidar Magnusson
September 2, 2013
http://www.it.hiof.no/~larsvmag/iti43309_13/index.html
Lars Vidar Magnusson Computer Graphics 2013
Rows and Columns
Matrices can be represented in two ways, either as column-majoror as row-major. This is purely a notational difference, in that thesame result is acheived by changing either the multiplication orderor the transposition.
v ′ = Mv
v ′T = vTMT
The above order is considered post-multiplication order, which isthe standard in OpenGL. Direct3D uses pre-multiplication order.
v ′ = vM
Lars Vidar Magnusson Computer Graphics 2013
Compound Transformations
Two transformation matrices can be combined into one bymultiplying them together. In OpenGL matrix multiplication, whichinclude any of the transformation functions, is done in post-order.
M′ = MM1
A vertex is transformed by compound transformation in thefollowing way.
v ′ = M1M2M3M4v
Note then that the matrices can be perceived as being applied inthe reverse order.
Lars Vidar Magnusson Computer Graphics 2013
Storing and Restoring Matrices in OpenGL
OpenGL uses internal matrices to represent the current modelview,projection and texture matrices. These matrices can be stored on astack using the functions glPushMatrix and glPopMatrix, whichstores and retrieves the current matrix respectively.
Figure 1: Pushing and popping the modelview matrix can helphierarchical rendering
Lars Vidar Magnusson Computer Graphics 2013
Geometry
Complex models are typically represented by a meshes/polyhedrain todays three-dimensional APIs and hardware. Each mesh iscomposed of the following atomic elements:
Vertices
Edges
Lars Vidar Magnusson Computer Graphics 2013
Standard Primitives
There are a number of standard primitives which build on top ofthe vertices and edges in todays three-dimensional APIs.
Points (just the vertices)
Lines
Triangles
Quads
Polygons
The hardware available have been optimized for triangle rendering,so quads and polygons are typically converted before they aredrawn.
Lars Vidar Magnusson Computer Graphics 2013
Primitive Rendering In OpenGL
The mentioned primitives can all be rendered in OpenGL using thefollowing set of functions:
g l B e g i n ( mode ) ;g l V e r t e x f ( x , y , z ) ;g lEnd ( ) ;
The parameter mode specify what kind of primitive to draw.
GL POINTS
GL LINES
GL TRIANGLES
GL QUADS
GL POLYGON
...
Lars Vidar Magnusson Computer Graphics 2013
Primitive Modes in OpenGL
The different primitive drawing modes in OpenGL specify how thepoints, if at all, the vertices are connected.
Figure 2: GL POINTS turns eachvertex into a point
Figure 3: GL POLYGON turns allthe vertices into a face
Lars Vidar Magnusson Computer Graphics 2013
Primitive Modes in OpenGL Continued
Figure 4: GL QUADS turns 4vertices into a face
Figure 5: GL TRIANGLES turns3 vertices into a face
Figure 6: GL QUAD STRIP thevertices into strip of quads
Figure 7: GL TRIANGLE STRIPthe vertices into strip of triangles
Lars Vidar Magnusson Computer Graphics 2013
General Data Structures
The primitive modes offer a method for drawing simple primitives,but it is also possible to use a more general approach. This is doneusing an array approach.
Vertex attributes
PositionNormalTextures coordinate...
Indices
There are at least two ways of using general arrays in OpenGL, seevertex arrays and vertex buffer objects.
Lars Vidar Magnusson Computer Graphics 2013
GLU Quadrics
The GLU library offers a simple way of drawing simples shapes,such as cylinders and spheres.
Figure 8: A cylinder drawn withgluCylinder(q, br, tr, h, sl, st)
Figure 9: A sphere drawn withgluSphere(q, r, sl, st)
Lars Vidar Magnusson Computer Graphics 2013
Lighting and Materials
OpenGL (as well as Direct3D) uses a gouraud shading model alongwith a Blinn-Phong reflection model. The lighting model involvefour different kinds of light: ambient, diffuse, specular and emitted.
Lars Vidar Magnusson Computer Graphics 2013
Emitted Lighting
Emitted lighting is light emitted from a surface in the scene, andas such it depends on nothing else than the material.
Ier = Mer
Ieg = Meg
Ieb = Meb
Like ambient lighting, the emitted light is a ”flat” light in that theshape of the model is neglected.
Lars Vidar Magnusson Computer Graphics 2013
Ambient Lighting
Ambient light is cast uniformly on all objects in the scene, due tonot having a direction. The resulting intensity Ia of each of thecolor components are calculated by combining the light La with thematerial Ma:
Iar = LarMar
Iag = LagMag
Iab = LabMab
The ambient light component can both be set globally and perlight source.
Lars Vidar Magnusson Computer Graphics 2013
Diffuse Lighting
Diffuse lighting, unlike ambient, has direction, and it can eitherhave a specified source or be cast from a source infinitely far away.The direction makes it possible to alter the outcome according tohow the light hits a surface.The intensity Id are calculated by incorporating the angle αbetween the surface normal N and the diffuse light direction L.
Idr = LdrMdr cosα = LdrMdr (N · L)
Idg = LdgMdg cosα = LdgMdg (N · L)
Idb = LdbMdb cosα = LdbMdb(N · L)
Note that only angles between 0 and 90 are of interest, and thatthe vectors L and N has to be of unit length.
Lars Vidar Magnusson Computer Graphics 2013
Specular Lighting
Specular lighting aims to emulate the shininess of surfaces. Thismakes it dependent not only on the light direction L, but also onthe position of the eye.
Figure 10: A diagram of the elements of specular lighting
Isr = LsrMsr cossh β = LsrMsr (R · V )sh
Isg = LsgMsg cossh β = LsgMsg (R · V )sh
Isb = LsbMsb cossh β = LsbMsb(R · V )sh
Lars Vidar Magnusson Computer Graphics 2013
Weakening of Light
The lighting model allows for weaking of light specified by anattenuation factor fatt . This factor can be calculated using thedistance to the light source d and a set of constants.
fatt =1
c1 + c2d + c3d2
c1 specify constant attenuation
c2 specify linear attenuation
c3 specify quadric attenuation
Lars Vidar Magnusson Computer Graphics 2013
Lighting in OpenGL
We have covered the general math behind OpenGL’s lightingmodel, but now it is time to look at how to do lighting using theOpenGL API.
g l E n a b l e ( GL LIGHTING ) ;g l E n a b l e ( GL LIGHTn ) ;g l D i s a b l e ( GL LIGHTn ) ;g l D i s a b l e ( GL LIGHTING ) ;
These simple statements enable and disable lighting and a specificlight; n, which could range from 0 to 7.
Lars Vidar Magnusson Computer Graphics 2013
Lighting in OpenGL Continued
Ambient lighting is controlled using:
g l L i g h t ( lno , GL AMBIENT , Lar , Lag , Lab , a l p h a )g l M a t e r i a l ( s i d e , GL AMBIENT , Mar , Mag , Mab , a l p h a )
Diffuse lighting is controlled using:
g l L i g h t ( lno , GL DIFFUSE , Ldr , Ldg , Ldb , a l p h a )g l M a t e r i a l ( s i d e , GL DIFFUSE , Mdr , Mdg , Mdb, a l p h a )
Specular lighting is controlled using:
g l L i g h t ( lno , GL SPECULAR , Lsr , Lsg , Lsb , a l p h a )g l M a t e r i a l ( s i d e , GL SPECULAR , Msr , Msg , Msb , a l p h a )g l M a t e r i a l f ( s i d e , GL SHININESS , sh ) ;
Emitted lighting is controlled using:
g l M a t e r i a l ( s i d e , GL EMISSION , Mer , Meg , Meb , a l p h a )
Lars Vidar Magnusson Computer Graphics 2013
Light Attributes in OpenGL
We can specify the position of a light source using:
g l L i g h t ( lno , GL POSITION , x , y , z , w)
The three attenuation constants can be modified using:
g l L i g h t f v ( lno , GL CONSTANT ATTENUATION, c1 )g l L i g h t f v ( lno , GL LINEAR ATTENUATION , c2 )g l L i g h t f v ( lno , GL QUADRATIC ATTENUATION , c3 )
The spot cutoff and exponent can be set using:
g l L i g h t ( lno , GL SPOT CUTOFF , v )g l L i g h t ( lno , GL SPOT EXPONENT , e )
Lars Vidar Magnusson Computer Graphics 2013
Shading and Smoothing
Shading represent how light affect th surfaces in a scene. OpenGLuse a technique called gouraud shading which is a simplification ofphong shading. Gouraud shading works by calculating theintensities at each vertex, and then do a interpolation to determinethe intensities on the remaining surface.
Lars Vidar Magnusson Computer Graphics 2013
Gouraud Shading
Gouraud shading works by doing a linear interpolation between theintensities of the vertices.
Figure 11: A diagram illustrating gouraud shading
Lars Vidar Magnusson Computer Graphics 2013
Gouraud Shading Continued
The diagram on the previous slide declares I1, I2 and I3 as theintensities of the vertices of triangle. The intensity Ia, Ib andultimately I can then be calculated as follows:
y1 − yay1 − y2
=I1 − IaI1 − I2
Ia =ya − y2y1 − y2
I1 +y1 − yay1 − y2
I2
y3 − yby3 − y2
=I3 − IbI3 − I2
Ib =yb − y2y3 − y2
I3 +y3 − yby3 − y2
I2
I =xb − x
xb − xaIa +
x − xaxb − xa
Ib
Lars Vidar Magnusson Computer Graphics 2013
Gouraud Shading in OpenGL
Gouraud shading can be turned on or off as follows:
glShadeModel (GL SMOOTH ) ;g lShadeModel ( GL FLAT ) ;
Figure 12: The difference between GL FLAT and GL SMOOTH
Lars Vidar Magnusson Computer Graphics 2013
Limitations of Gouraud Shading
There are some limitations in the gouraud shading model that oneshould be aware of.
If a light source hits a surface without touching a singlevertex, the effect will not be seen.
Rough models will get a jittered effect due to the limitationsof linear interpolation of the intensities.
Figure 13: The limitations of gouraud shading
Lars Vidar Magnusson Computer Graphics 2013