Download presentation
Presentation is loading. Please wait.
1
o عَلَّمَهُ الْبَيَانَ
Allah says o عَلَّمَهُ الْبَيَانَ He has taught him speech (and intelligence). Al-Qur'an, (Ar-Rahman)
2
Contents In this lecture we’ll cover the following:
Introduction to Rendering Hidden Surface Removal Lighting Shading & Texture Models
3
Uptil Now We have learnt How to model and draw some basic 3D objects
How to view those objects (through camera & projections) Culling
4
Hidden Surface Removal: The problem
Consider a simple scene (with cone drawn after the tea pot being drawn at the origin) Hidden surfaces are the surfaces that are obscured by other objects in front of them ZCone= -1 ZCone= +1 ZCone= 0 The object drawn after another object obscures it if the hidden surface is not removed
5
ZCone= +1 ZCone= 0 ZCone= -1
6
Z-Buffering: The Solution
Z-Buffering is used to perform Hidden Surface Removal A buffer (2D array) in memory (called Z-Buffer) is used to keep track of the surface that is closest to the eye for any given pixel Closest object finally gets drawn (Z-Culling)
7
Z-Buffering Initialize Z(x,y)= MAX_DEPTH //z-buffer
C(x,y)= Background Color //color buffer For each Polygon P For each pixel (x,y) in P Compute z-depth at (x,y) if( z-depth < Z(x,y) ) Z(x,y)= z-depth C(x,y)= Intensity of P at (x,y) end End Display C(x,y)
8
Z-Buffering in OpenGL Enabling Z-Buffer (in Initialization function)
glutInitDisplayMode(GLUT_DEPTH | … ) glEnable(GL_DEPTH_TEST) Clearing the Z-Buffer (in the Display function) glClear(GL_DEPTH_BUFFER_BIT | …)
9
Lighting Light emanates from a light source and bounces off objects and then reaches our eyes What we see due to a light depends upon Location of the light Intensity of the light Color of the light Position of the surface Material of the surface Normal to the surface Location of the viewer Lighting adds realism to the 3D objects
10
Law of reflection The angle made by a reflected ray of light with the normal to the surface is the same as the one made by the incident ray of light
11
The Lighting Model: Phong Model
Phong proposed a reflectance model with four reflectance components Ambient Reflectance Diffuse Reflectance Specular Reflectance Emission
12
Ambient Reflectance Ambient Reflectance Ambient light has no direction
Impinges equally on all surfaces from all directions Reflected by a constant multiple Let the incident light have a color (Ri,Gi,Bi) and the material has the ambient reflectance of (Ra,Ga,Ba) then the reflected light due to ambient reflection will have a color (Rra, Gra, Bra) = (RiRa, GiGa, BiBa)
13
Ambient lighting in OpenGL
For ambient lighting we can control the intensity and color of the light along with material reflectance properties Setting the ambient light color and intensity (in Initialization function) GLfloat light_ambient[] = { 0.6, 0.6, 0.6, 1.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); Enabling Lighting (in Initialization function) glEnable(GL_LIGHTING); Setting material properties (prior to drawing the object) GLfloat mat_ambient[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
14
Ambient lighting in OpenGL
15
Diffuse Reflectance Surfaces with diffuse reflectance scatter light equally in all directions The amount of light reflected is directly proportional to the angle of incidence of the incoming beam of light Can be observed on dull objects (paper or cloth)
16
Diffuse Reflectance: Lambert’s law
The diffuse component can be modeled using Lambert’s law Let the light have a diffuse color of (Rid, Gid, Bid) The surface has a diffuse reflectance coefficient of (Rd,Gd,Bd) Them the diffuse component of the reflected light is given by (Rrd, Grd, Brd) = (RidRdcos(θ), GidGdcos(θ), BidBdcos(θ)) when θ is less than 90 degrees
17
Diffuse Reflectance in OpenGL
For diffuse reflectance we need to define (one or more) specific lights which have A specified position A specified color and intensity We also need to specify the diffuse reflectance properties of the material being drawn We can define up to eight lights in OpenGL
18
Diffuse Reflectance in OpenGL…
Setting up the light color and intensity (in Initialization) GLfloat light_diffuse[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glEnable(GL_LIGHT0); Setting up the light position (in Initialization) GLfloat light_position[]={0,5,+5,W}; glLightfv(GL_LIGHT0,GL_POSITION,light_position); Setting up the material properties (Before Drawing) GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); The last parameter (W) in light_position indicates whether the light is a directional one (when zero) or not (when one)
19
Diffuse Reflectance in OpenGL
Non-Directional Light Source Directional Light Source (W=1)
20
Specular Reflectance Specular reflection produces shiny highlights often seen on shiny objects like glass or metal Specular reflection depends upon Direction of the incident light View angle The specular reflection component is highest when the viewing direction is parallel to the reflected light
21
Specular Reflection using OpenGL
For specular reflection we need to specify (one or more) specific lights (can be the same as the ones used in diffuse lighting) The specular light has a specific color and intensity For the material we can specify the specular reflectance coefficients along with the Shininess
22
Specular Reflection using OpenGL…
Setting up the light color and intensity (in initialization) GLfloat light_specular[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); Setting up the material properties (before drawing) GLfloat mat_specular[] = { 1, 1.0, 1.0, 1.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); GLfloat shininess[]={f}; glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
23
Specular Reflection using OpenGL…
With f=5 With f=50 F ranges from 0 to 127 in OpenGL
24
Emission The emissive color of a surface adds intensity to the object but is unaffected by any light sources Used to simulate lights The emission component is zero for objects that do not emit light Property of the material GLfloat mat_emission[]={1,1,1,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
25
A point to note… Position and direction of light is transformed by the Model View matrix Rotating the light around the object (in Display) glPushMatrix(); spin++; glRotatef(spin,0,1,0); glLightfv(GL_LIGHT0,GL_POSITION,light_position); Draw_light(); glPopMatrix();
26
Autopsy of a complete Lighting Program
#include <windows.h> #include <gl\glut.h> float spin=0; GLfloat light_position[]={3,0,0,0}; void drawLight() //draws a sphere at the same location as the light { glTranslatef(light_position[0],light_position[1],light_position[2]); GLfloat mat_emission[]={1,1,1,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(0.1,10,10); } void drawTeapot() //draws tea-pot with material properties { GLfloat mat_ambient[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); GLfloat mat_specular[] = { 1, 1.0, 1.0, 1.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); GLfloat low_shininess[]={20}; glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); GLfloat mat_emission_tpt[]={0.0,0.0,0.0,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission_tpt); glutSolidTeapot(1.0); #include <windows.h> //the windows include file, required by all windows applications #include <gl\glut.h> float spin=0; GLfloat light_position[]={3,0,0,0}; void drawLight() { glTranslatef(light_position[0],light_position[1],light_position[2]); GLfloat mat_emission[]={1,1,1,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(0.1,100,100); } void drawTeapot() GLfloat mat_ambient[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); GLfloat mat_specular[] = { 1, 1.0, 1.0, 1.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); GLfloat low_shininess[]={20}; glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); GLfloat mat_emission_tpt[]={0.0,0.0,0.0,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission_tpt); glutSolidTeapot(1.0); void setLight0() glShadeModel (GL_SMOOTH); GLfloat light_ambient[] = { 0.6, 0.6, 0.6, 1.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); glEnable(GL_LIGHTING); GLfloat light_diffuse[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0,GL_POSITION,light_position); GLfloat light_specular[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); void Display(void) // glutSwapBuffers(); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode (GL_MODELVIEW); glPushMatrix(); spin++; glRotatef(spin,0,1,0); drawLight(); glPopMatrix(); drawTeapot(); glFlush(); glutPostRedisplay(); void reshape (int w, int h) // on reshape and on startup, keep the viewport to be the entire size of the window glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5, 0.5, 1., 40.0); gluLookAt(0.,5,5, 0,0,0,0.,1.,0.); void init(void){ //set the clear color to be black glClearColor(0.0,0.0,0.0,0.0); setLight0(); glEnable(GL_DEPTH_TEST); void main(int argc, char* argv[]) glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (320, 200); glutCreateWindow("Rendering 3D shapes"); init(); glutDisplayFunc(Display); glutReshapeFunc(reshape); glutMainLoop();
27
void setLight0() //sets the light components
{ glShadeModel (GL_SMOOTH); GLfloat light_ambient[] = { 0.6, 0.6, 0.6, 1.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); glEnable(GL_LIGHTING); GLfloat light_diffuse[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0,GL_POSITION,light_position); GLfloat light_specular[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); } void Display(void) //display the “light” and the tea-pot // glutSwapBuffers(); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode (GL_MODELVIEW); glPushMatrix(); spin++; glRotatef(spin,0,1,0); drawLight(); glPopMatrix(); drawTeapot(); glFlush(); glutPostRedisplay();
28
void reshape (int w, int h) //reshape handler
{ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5, 0.5, 1., 40.0); glMatrixMode (GL_MODELVIEW); gluLookAt(0.,5,5, 0,0,0,0.,1.,0.); } void init(void){ //set the clear color to be black glClearColor(0.0,0.0,0.0,0.0); setLight0(); glEnable(GL_DEPTH_TEST); void main(int argc, char* argv[]) glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (320, 200); glutCreateWindow("Rendering 3D shapes"); init(); glutDisplayFunc(Display); glutReshapeFunc(reshape); glutMainLoop();
29
OpenGL Lights Distant Light Example: Sun Parallel rays
Position makes no different on intensity In OpenGL GLfloat light_position[]={x,y,z,W}; glLightfv(GL_LIGHT0,GL_POSITION,light_position); If W = 0 The light is taken to be a distant light and intensity does not change with position #include <windows.h> //the windows include file, required by all windows applications #include <gl\glut.h> float spin=0; GLfloat light_position[]={3,0,0,1}; void drawLight() { glTranslatef(light_position[0],light_position[1],light_position[2]); GLfloat mat_emission[]={1,1,1,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(0.1,10,10); } void drawTeapot() GLfloat mat_ambient[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); GLfloat mat_specular[] = { 1, 1.0, 1.0, 1.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); GLfloat low_shininess[]={20}; glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); GLfloat mat_emission_tpt[]={0.0,0.0,0.0,0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission_tpt); glutSolidTeapot(1.0); void setLight0() glShadeModel (GL_SMOOTH); GLfloat light_ambient[] = { 0.6, 0.6, 0.6, 1.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_ambient); glEnable(GL_LIGHTING); GLfloat light_diffuse[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0,GL_POSITION,light_position); GLfloat light_specular[]={0.8,0.8,0.8,1.0}; glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,10); glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0); void Display(void) // glutSwapBuffers(); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode (GL_MODELVIEW); glPushMatrix(); spin++; glRotatef(spin,0,1,0); GLfloat spot_direction[]={-1,0,0}; glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spot_direction); drawLight(); glPopMatrix(); drawTeapot(); glFlush(); glutPostRedisplay(); void reshape (int w, int h) // on reshape and on startup, keep the viewport to be the entire size of the window glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5, 0.5, 1., 40.0); gluLookAt(0.,5,5, 0,0,0,0.,1.,0.); void init(void){ //set the clear color to be black glClearColor(0.0,0.0,0.0,0.0); setLight0(); glEnable(GL_DEPTH_TEST); void main(int argc, char* argv[]) glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (320, 200); glutCreateWindow("Rendering 3D shapes"); init(); glutDisplayFunc(Display); glutReshapeFunc(reshape); glutMainLoop();
30
OpenGL Lights… Point light Example: Bulb
Light emanates from a single point Set W = 1 in position
31
OpenGL Lights Spot Light Example: Bulb with Shade In OpenGL
GLfloat light_position[]={x,y,z,1}; glLightfv(GL_LIGHT0,GL_POSITION,light_position); GLfloat spot_direction[]={-1,0,0}; glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spot_direction); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,angle); angle Position Direction
32
OpenGL Lights Attenuation
The intensity of light falls off with the square of the distance from the light to the surface In OpenGL glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1.0);
33
Role of Normals in Lighting
Diffuse and specular reflection depend upon the normal vector to the surface Normals are implicitly specified on the basis of order in which vertices of a polygon are specified A Normal can explicitly be specified by using glNormal3 or glNormal3fv commands prior to calling glVertex*() for the vertices to which this normal is associated glNormal3(GLfloat nx, GLfloat ny, GLfloat nz) glNormal3fv(const GLfloat *v)
34
Defining Normals
35
Shading Models: Flat Shading
Each surface is assumed to have one normal vector (usually the average of its vertex normals) This normal vector is then used in the lighting calculations and the resulting color is assigned to the entire surface glShadeModel(GL_flat) Simple and Fast but unrealistic Used for preview and for small objects
36
Flat Shading
37
Gourad Shading This shading is used when smooth shading effects are desired. The normal to each vertex is used to find the color at each vertex. These colors are averaged to give color on the inside of the polygon glShadeModel(GL_SMOOTH);
38
What’s Missing? Shadows!!
39
NEXT Loading a BMP File VRML File Formats and Models Texture Mapping
Splines for Advanced Modeling
40
Because of the nature of Moore's law, anything that an extremely clever graphics programmer can do at one point can be replicated by a merely competent programmer some number of years later. John Carmack Associated Lab Tasks: [COMPLETE this chapter before 25th and take test] Drawing a 3D circle in XZ plane and looking at it through different angles by changing the view and changing the projection matrices to illustrate basic 3D concepts Drawing a simple triangular pyramid Drawing a simple sphere … 3D transformations exercises Drawing VRML objects Can also access matrices (and other parts of the state) by query functions glGetIntegerv(GL_enum, GLinteger *) glGetFloatv(GL_enum, GLfloat *) glGetBooleanv(...,...) glGetDoublev(...,...) glIsEnabled(GL_enum) //feature enabled For matrices, we use as double m[16]; glGetDoublev(GL_MODELVIEW, m); End of Lecture-8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.