SlideShare a Scribd company logo
1 of 33
Download to read offline
CSC 307 1.0
Graphics Programming
Budditha Hettige
Department of Statistics and Computer Science
2
Lighting
Budditha Hettige
Real-World and OpenGL Lighting
3Budditha Hettige
Using a Light Source
4Budditha Hettige
5
Lighting Principles
• Lighting simulates how objects reflect light
– material composition of object
– light’s color and position
– global lighting parameters
• ambient light
• two sided lighting
– available in both color index
and RGBA mode
Budditha Hettige
6
How OpenGL Simulates Lights
• Phong lighting model
– Computed at vertices
• Lighting contributors
– Surface material properties
– Light properties
– Lighting model properties
Budditha Hettige
OpenGL Lights
 Common Properties
 Ambient
 Diffuse
 Specular
 Types
 Directional Light
 Point Light
 Spotlight
7Budditha Hettige
Lighting….
8Budditha Hettige
9
Surface Normals
• Normals define how a surface reflects
light
• glNormal3f( x, y, z )
– Current normal is used to compute vertex’s
color
– Use unit normals for proper lighting
• scaling affects a normal’s length
glEnable( GL_NORMALIZE )
or
glEnable( GL_RESCALE_NORMAL )
CPUCPU DLDL
Poly.Poly.
Per
Vertex
Per
Vertex
RasterRaster FragFrag FBFB
PixelPixel
TextureTexture
Budditha Hettige
10
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
Budditha Hettige
11
Light Properties
• glLightfv( light, property, value
);
– light specifies which light
• multiple lights, starting with GL_LIGHT0
glGetIntegerv( GL_MAX_LIGHTS, &n );
– properties
• colors
• position and type
• attenuation
Budditha Hettige
Surface lighting
 Surface lighting properties
 Values defined in RGB colorspace
 Ambient
 Light reaching all points on a surface
 Diffuse
 Light scattering from rough surfaces
 Specular
 Light reflection from shiny surfaces
 Shininess
 Defines the spread of the specular term
 Larger values have more “mirror” like specularity
12Budditha Hettige
Lighting model
 Light Properties
 Ambient
 Color and intensity of light that interacts with a
surface materials ambient property
 Diffuse
 Color and intensity of light that interacts with a
surface materials diffuse property
 Specular
 Color and intensity of light that interacts with a
surface materials specular property
13Budditha Hettige
Lighting model
14
Ambient Diffuse+ Combination=Specular+
Budditha Hettige
15
Controlling a Light’s Position
• Modelview matrix affects a light’s position
– Different effects based on when position is
specified
• eye coordinates
• world coordinates
• model coordinates
– Push and pop matrices to uniquely control a
light’s position
Budditha Hettige
GLUT simple geometry
 GLUT can render simple geometry
 Sphere, Box, Cone, Torus, Teapot, etc
 void glutSolidSphere(GLdouble radius,
GLint slices, GLint stacks);
 radius: radius of the sphere
 slices: number of longitudinal subdivitions
 stacks: number of latitudinal subdivitions
 void glutSolidCube(GLdouble size);
 size: length of all sides of the cube
16Budditha Hettige
17
Tips for Better Lighting
• Recall lighting computed only at vertices
– model tessellation heavily affects lighting
results
• better results but more geometry to process
• Use a single infinite light for fastest
lighting
– minimal computation per vertex
Budditha Hettige
Setting up state for lighting
 Lighting and lights are disabled by default
 Enabling lighting overrides the use of assigned colors
from glColor calls
 glShadeModel(GL_SMOOTH);
 Set Gouraud shading
 glShadeModel(GL_FLAT);
 Set Flat Shading
 glEnable(GL_LIGHTING);
 Enable fixed function lighting
 glEnable(GL_LIGHT0);
 Enable light 0 in the scene
18Budditha Hettige
Hidden Surface removal
• First Enable Culling
– glEnable(GL_CULL_FACE);
• Define which face to cull
– glCullFace(GLenum face);
– Face: The polygon facing
• GL_FRONT, GL_BACK,
GL_FRONT_AND_BACK
19Budditha Hettige
Assigning Material Properties
 void glMaterialfv( GLenum face,
GLenum pname,
const GLfloat * params);
 face: The geometry facing to assign
 GL_FRONT,GL_BACK, or GL_FRONT_AND_BACK
 pname: The material property to assign
 GL_AMBIENT,GL_DIFFUSE,GL_SPECULAR, and
others
 params: pointer to an array of values
20Budditha Hettige
OpenGl calls array pointers
 Some sets of OpenGL methods take multiple
numbers and types of arguments
glVertex2i
Type of argumentNumber of arguments
glMaterialfv
Type Suffix
GLbyte b
GLshort s
i
f
d
ub
GLushort us
ui
GLint,
GLsizei
GLfloat,
GLclampf
GLdouble,
GLclampd
GLubyte,
GLboolean
GLuint,
GLenum,
GLbitfield
Method takes an array
pointer argument
GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
Budditha Hettige 21
Assign Material Property
22
//Define GLfloat arrays to hold material data
GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//Shininess will be an array of length 1
GLfloat mat_shininess[] = { 50.0 };
//Apply the material properties to the front
facing
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
Budditha Hettige
Assigning Light Properties
 Similar to Materials
 void glLightfv(GLenum light,
GLenum pname,
const GLfloat * params);
 light: The light to assign
 GL_LIGHT0, GL_LIGHT1, …, GL_LIGHT8
 pname: The light property to assign
 GL_AMBIENT,GL_DIFFUSE, GL_SPECULAR, and others
 params: pointer to an array of values
23Budditha Hettige
Assigning Light Properties
24
//Define GLfloat arrays to hold light data
GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat light_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//Apply the light properties to LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
Budditha Hettige
Point Light
 A light source originating from a zero-
volume point in the scene
 Casts light in all directions
//Light position
GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0
};
//Apply the light position
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Position
For Positions this
should be 1.0
Budditha Hettige 25
Directional Light
 A light infinitely far away from the drawn
scene
 Used most often for emulating sunlight
 Distance from sun to earth is large
 Light direction can be considered the same
//Light direction down the negative x axis
GLfloat light_direction[] = { -1.0, 0.0, 0.0, 0.0 };
//Apply the light direction
glLightfv(GL_LIGHT0, GL_POSITION, light_ambient);
Direction
-should be normalized- For Directions this
should be 0.0
Budditha Hettige 26
Spotlights
27Budditha Hettige
Spotlight
 A light source originating from a zero-
volume point in the scene
 Direction
 Direction the light is focused on
 Cutoff
 angle that defines light cone
 Exponent
 Concentration of the light
 Brightest around the center
Budditha Hettige 28
Spotlight example
//Spotlight properties
GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 };
GLfloat light_spot_direction[] = { 0.0, 0.0, -1.0};
GLfloat light_spot_cutoff[] = { 25.0 };
GLfloat light_spot_exp[] = { 2.0 };
//Apply the light position
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION , light_spot_direction);
glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF , light_spot_cufoff);
glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT , light_spot_exp);
Budditha Hettige 29
Shading
• Flat Shading
– Flat shading selects the computed color of
just one vertex
30Budditha Hettige
Shading
• Gouraud Shading
 Lighting color calculated per-vertex
 saves computation
 Vertex color is bilinearly interpolated
across polygon
31Budditha Hettige
Flat vs Gouraud shading
32
Flat Gouraud
Budditha Hettige
Drawing a Smooth-Shaded Triangle
// Enable smooth shading
glShadeModel(GL_SMOOTH);
// Draw the triangle
glBegin(GL_TRIANGLES);
// Red Apex
glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0);
glVertex3f(0.0f,200.0f,0.0f);
// Green on the right bottom corner
glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0);
glVertex3f(200.0f,-70.0f,0.0f);
// Blue on the left bottom corner
glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255);
glVertex3f(-200.0f, -70.0f, 0.0f);
glEnd();
33Budditha Hettige

More Related Content

Similar to Lighting

computer graphics slides by Talha shah
computer graphics slides by Talha shahcomputer graphics slides by Talha shah
computer graphics slides by Talha shahSyed Talha
 
Two marks with answers ME6501 CAD
Two marks with answers ME6501 CADTwo marks with answers ME6501 CAD
Two marks with answers ME6501 CADPriscilla CPG
 
Lighting & Shading in OpenGL Non-Photorealistic Rendering.ppt
Lighting & Shading in OpenGL Non-Photorealistic Rendering.pptLighting & Shading in OpenGL Non-Photorealistic Rendering.ppt
Lighting & Shading in OpenGL Non-Photorealistic Rendering.pptCharlesMatu2
 
Phong Shading over any Polygonal Surface
Phong Shading over any Polygonal Surface Phong Shading over any Polygonal Surface
Phong Shading over any Polygonal Surface Bhuvnesh Pratap
 
Curves and surfaces in OpenGL
Curves and surfaces in OpenGLCurves and surfaces in OpenGL
Curves and surfaces in OpenGLSyed Zaid Irshad
 
Part 2- Geometric Transformation.pptx
Part 2- Geometric Transformation.pptxPart 2- Geometric Transformation.pptx
Part 2- Geometric Transformation.pptxKhalil Alhatab
 
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeksBeginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeksJinTaek Seo
 
Part 2- Transformation.pptx
Part 2- Transformation.pptxPart 2- Transformation.pptx
Part 2- Transformation.pptxKhalil Alhatab
 
CS 354 Object Viewing and Representation
CS 354 Object Viewing and RepresentationCS 354 Object Viewing and Representation
CS 354 Object Viewing and RepresentationMark Kilgard
 
Part 3- Manipulation and Representation of Curves.pptx
Part 3- Manipulation and Representation of Curves.pptxPart 3- Manipulation and Representation of Curves.pptx
Part 3- Manipulation and Representation of Curves.pptxKhalil Alhatab
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2Sardar Alam
 
Shadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics HardwareShadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics Hardwarestefan_b
 
Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Takao Wada
 
10CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 710CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 7Vanishree Arun
 
PapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdf
PapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdfPapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdf
PapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdfAdam Hill
 

Similar to Lighting (20)

OpenGL Transformations
OpenGL TransformationsOpenGL Transformations
OpenGL Transformations
 
computer graphics slides by Talha shah
computer graphics slides by Talha shahcomputer graphics slides by Talha shah
computer graphics slides by Talha shah
 
CS 354 Lighting
CS 354 LightingCS 354 Lighting
CS 354 Lighting
 
Two marks with answers ME6501 CAD
Two marks with answers ME6501 CADTwo marks with answers ME6501 CAD
Two marks with answers ME6501 CAD
 
Lighting & Shading in OpenGL Non-Photorealistic Rendering.ppt
Lighting & Shading in OpenGL Non-Photorealistic Rendering.pptLighting & Shading in OpenGL Non-Photorealistic Rendering.ppt
Lighting & Shading in OpenGL Non-Photorealistic Rendering.ppt
 
Phong Shading over any Polygonal Surface
Phong Shading over any Polygonal Surface Phong Shading over any Polygonal Surface
Phong Shading over any Polygonal Surface
 
Curves and surfaces in OpenGL
Curves and surfaces in OpenGLCurves and surfaces in OpenGL
Curves and surfaces in OpenGL
 
Part 2- Geometric Transformation.pptx
Part 2- Geometric Transformation.pptxPart 2- Geometric Transformation.pptx
Part 2- Geometric Transformation.pptx
 
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeksBeginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
 
Part 2- Transformation.pptx
Part 2- Transformation.pptxPart 2- Transformation.pptx
Part 2- Transformation.pptx
 
graphics notes
graphics notesgraphics notes
graphics notes
 
CS 354 Object Viewing and Representation
CS 354 Object Viewing and RepresentationCS 354 Object Viewing and Representation
CS 354 Object Viewing and Representation
 
Part 3- Manipulation and Representation of Curves.pptx
Part 3- Manipulation and Representation of Curves.pptxPart 3- Manipulation and Representation of Curves.pptx
Part 3- Manipulation and Representation of Curves.pptx
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2
 
Shadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics HardwareShadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics Hardware
 
Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5
 
Development with OpenGL and Qt
Development with OpenGL and QtDevelopment with OpenGL and Qt
Development with OpenGL and Qt
 
Geo crash course
Geo crash courseGeo crash course
Geo crash course
 
10CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 710CSL67 CG LAB PROGRAM 7
10CSL67 CG LAB PROGRAM 7
 
PapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdf
PapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdfPapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdf
PapersWeLove - Rendering Synthetic Objects Into Real Scenes - Paul Debevec.pdf
 

More from Budditha Hettige

Graphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::BlocksGraphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::BlocksBudditha Hettige
 
Introduction to Computer Graphics
Introduction to Computer GraphicsIntroduction to Computer Graphics
Introduction to Computer GraphicsBudditha Hettige
 
Computer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentalsComputer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentalsBudditha Hettige
 
Computer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary MemoryComputer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary MemoryBudditha Hettige
 
Computer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache MemoryComputer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache MemoryBudditha Hettige
 
Computer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressingComputer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressingBudditha Hettige
 
Computer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performanceComputer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performanceBudditha Hettige
 
Computer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technologyComputer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technologyBudditha Hettige
 
Computer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architectureComputer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architectureBudditha Hettige
 

More from Budditha Hettige (20)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Sorting
SortingSorting
Sorting
 
Link List
Link ListLink List
Link List
 
Queue
QueueQueue
Queue
 
02 Stack
02 Stack02 Stack
02 Stack
 
Data Structures 01
Data Structures 01Data Structures 01
Data Structures 01
 
Drawing Fonts
Drawing FontsDrawing Fonts
Drawing Fonts
 
Texture Mapping
Texture Mapping Texture Mapping
Texture Mapping
 
Viewing
ViewingViewing
Viewing
 
OpenGL 3D Drawing
OpenGL 3D DrawingOpenGL 3D Drawing
OpenGL 3D Drawing
 
2D Drawing
2D Drawing2D Drawing
2D Drawing
 
Graphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::BlocksGraphics Programming OpenGL & GLUT in Code::Blocks
Graphics Programming OpenGL & GLUT in Code::Blocks
 
Introduction to Computer Graphics
Introduction to Computer GraphicsIntroduction to Computer Graphics
Introduction to Computer Graphics
 
Computer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentalsComputer System Architecture Lecture Note 9 IO fundamentals
Computer System Architecture Lecture Note 9 IO fundamentals
 
Computer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary MemoryComputer System Architecture Lecture Note 8.1 primary Memory
Computer System Architecture Lecture Note 8.1 primary Memory
 
Computer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache MemoryComputer System Architecture Lecture Note 8.2 Cache Memory
Computer System Architecture Lecture Note 8.2 Cache Memory
 
Computer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressingComputer System Architecture Lecture Note 7 addressing
Computer System Architecture Lecture Note 7 addressing
 
Computer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performanceComputer System Architecture Lecture Note 6: hardware performance
Computer System Architecture Lecture Note 6: hardware performance
 
Computer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technologyComputer System Architecture Lecture Note 5: microprocessor technology
Computer System Architecture Lecture Note 5: microprocessor technology
 
Computer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architectureComputer System Architecture Lecture Note 3: computer architecture
Computer System Architecture Lecture Note 3: computer architecture
 

Recently uploaded

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Lighting

  • 1. CSC 307 1.0 Graphics Programming Budditha Hettige Department of Statistics and Computer Science
  • 3. Real-World and OpenGL Lighting 3Budditha Hettige
  • 4. Using a Light Source 4Budditha Hettige
  • 5. 5 Lighting Principles • Lighting simulates how objects reflect light – material composition of object – light’s color and position – global lighting parameters • ambient light • two sided lighting – available in both color index and RGBA mode Budditha Hettige
  • 6. 6 How OpenGL Simulates Lights • Phong lighting model – Computed at vertices • Lighting contributors – Surface material properties – Light properties – Lighting model properties Budditha Hettige
  • 7. OpenGL Lights  Common Properties  Ambient  Diffuse  Specular  Types  Directional Light  Point Light  Spotlight 7Budditha Hettige
  • 9. 9 Surface Normals • Normals define how a surface reflects light • glNormal3f( x, y, z ) – Current normal is used to compute vertex’s color – Use unit normals for proper lighting • scaling affects a normal’s length glEnable( GL_NORMALIZE ) or glEnable( GL_RESCALE_NORMAL ) CPUCPU DLDL Poly.Poly. Per Vertex Per Vertex RasterRaster FragFrag FBFB PixelPixel TextureTexture Budditha Hettige
  • 10. 10 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 Budditha Hettige
  • 11. 11 Light Properties • glLightfv( light, property, value ); – light specifies which light • multiple lights, starting with GL_LIGHT0 glGetIntegerv( GL_MAX_LIGHTS, &n ); – properties • colors • position and type • attenuation Budditha Hettige
  • 12. Surface lighting  Surface lighting properties  Values defined in RGB colorspace  Ambient  Light reaching all points on a surface  Diffuse  Light scattering from rough surfaces  Specular  Light reflection from shiny surfaces  Shininess  Defines the spread of the specular term  Larger values have more “mirror” like specularity 12Budditha Hettige
  • 13. Lighting model  Light Properties  Ambient  Color and intensity of light that interacts with a surface materials ambient property  Diffuse  Color and intensity of light that interacts with a surface materials diffuse property  Specular  Color and intensity of light that interacts with a surface materials specular property 13Budditha Hettige
  • 14. Lighting model 14 Ambient Diffuse+ Combination=Specular+ Budditha Hettige
  • 15. 15 Controlling a Light’s Position • Modelview matrix affects a light’s position – Different effects based on when position is specified • eye coordinates • world coordinates • model coordinates – Push and pop matrices to uniquely control a light’s position Budditha Hettige
  • 16. GLUT simple geometry  GLUT can render simple geometry  Sphere, Box, Cone, Torus, Teapot, etc  void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);  radius: radius of the sphere  slices: number of longitudinal subdivitions  stacks: number of latitudinal subdivitions  void glutSolidCube(GLdouble size);  size: length of all sides of the cube 16Budditha Hettige
  • 17. 17 Tips for Better Lighting • Recall lighting computed only at vertices – model tessellation heavily affects lighting results • better results but more geometry to process • Use a single infinite light for fastest lighting – minimal computation per vertex Budditha Hettige
  • 18. Setting up state for lighting  Lighting and lights are disabled by default  Enabling lighting overrides the use of assigned colors from glColor calls  glShadeModel(GL_SMOOTH);  Set Gouraud shading  glShadeModel(GL_FLAT);  Set Flat Shading  glEnable(GL_LIGHTING);  Enable fixed function lighting  glEnable(GL_LIGHT0);  Enable light 0 in the scene 18Budditha Hettige
  • 19. Hidden Surface removal • First Enable Culling – glEnable(GL_CULL_FACE); • Define which face to cull – glCullFace(GLenum face); – Face: The polygon facing • GL_FRONT, GL_BACK, GL_FRONT_AND_BACK 19Budditha Hettige
  • 20. Assigning Material Properties  void glMaterialfv( GLenum face, GLenum pname, const GLfloat * params);  face: The geometry facing to assign  GL_FRONT,GL_BACK, or GL_FRONT_AND_BACK  pname: The material property to assign  GL_AMBIENT,GL_DIFFUSE,GL_SPECULAR, and others  params: pointer to an array of values 20Budditha Hettige
  • 21. OpenGl calls array pointers  Some sets of OpenGL methods take multiple numbers and types of arguments glVertex2i Type of argumentNumber of arguments glMaterialfv Type Suffix GLbyte b GLshort s i f d ub GLushort us ui GLint, GLsizei GLfloat, GLclampf GLdouble, GLclampd GLubyte, GLboolean GLuint, GLenum, GLbitfield Method takes an array pointer argument GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); Budditha Hettige 21
  • 22. Assign Material Property 22 //Define GLfloat arrays to hold material data GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; //Shininess will be an array of length 1 GLfloat mat_shininess[] = { 50.0 }; //Apply the material properties to the front facing glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); Budditha Hettige
  • 23. Assigning Light Properties  Similar to Materials  void glLightfv(GLenum light, GLenum pname, const GLfloat * params);  light: The light to assign  GL_LIGHT0, GL_LIGHT1, …, GL_LIGHT8  pname: The light property to assign  GL_AMBIENT,GL_DIFFUSE, GL_SPECULAR, and others  params: pointer to an array of values 23Budditha Hettige
  • 24. Assigning Light Properties 24 //Define GLfloat arrays to hold light data GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat light_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; //Apply the light properties to LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); Budditha Hettige
  • 25. Point Light  A light source originating from a zero- volume point in the scene  Casts light in all directions //Light position GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; //Apply the light position glLightfv(GL_LIGHT0, GL_POSITION, light_position); Position For Positions this should be 1.0 Budditha Hettige 25
  • 26. Directional Light  A light infinitely far away from the drawn scene  Used most often for emulating sunlight  Distance from sun to earth is large  Light direction can be considered the same //Light direction down the negative x axis GLfloat light_direction[] = { -1.0, 0.0, 0.0, 0.0 }; //Apply the light direction glLightfv(GL_LIGHT0, GL_POSITION, light_ambient); Direction -should be normalized- For Directions this should be 0.0 Budditha Hettige 26
  • 28. Spotlight  A light source originating from a zero- volume point in the scene  Direction  Direction the light is focused on  Cutoff  angle that defines light cone  Exponent  Concentration of the light  Brightest around the center Budditha Hettige 28
  • 29. Spotlight example //Spotlight properties GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; GLfloat light_spot_direction[] = { 0.0, 0.0, -1.0}; GLfloat light_spot_cutoff[] = { 25.0 }; GLfloat light_spot_exp[] = { 2.0 }; //Apply the light position glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION , light_spot_direction); glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF , light_spot_cufoff); glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT , light_spot_exp); Budditha Hettige 29
  • 30. Shading • Flat Shading – Flat shading selects the computed color of just one vertex 30Budditha Hettige
  • 31. Shading • Gouraud Shading  Lighting color calculated per-vertex  saves computation  Vertex color is bilinearly interpolated across polygon 31Budditha Hettige
  • 32. Flat vs Gouraud shading 32 Flat Gouraud Budditha Hettige
  • 33. Drawing a Smooth-Shaded Triangle // Enable smooth shading glShadeModel(GL_SMOOTH); // Draw the triangle glBegin(GL_TRIANGLES); // Red Apex glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0); glVertex3f(0.0f,200.0f,0.0f); // Green on the right bottom corner glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0); glVertex3f(200.0f,-70.0f,0.0f); // Blue on the left bottom corner glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255); glVertex3f(-200.0f, -70.0f, 0.0f); glEnd(); 33Budditha Hettige