Computer Graphics
irpan@mikroskil.ac.id
Prosesnya cepat
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glPushMatrix();
glTranslatef( 1.0, -1.0, 1.0 );
glutSolidCube(1.5);
glPopMatrix();
glDisable(GL_LIGHT1);
glDisable(GL_LIGHTING);
3. Perhitungan Lighting
•Cahaya yang dihitung dalam hal ini adalah cahaya kedua, yaitu total
cahaya yang diterima oleh mata kita setelah melalui proses iluminasi.
•Total cahaya ini dirumuskan dengan :
•(sumber cahaya x cahaya material)
•Jika sumber cahaya lebih dari satu :
([sumber cahaya1 + sumber cahaya2 + ...] x cahaya material)
contoh :
•sumber cahaya lampu = {LR, LG, LB}
•cahaya material (objek) = {MR, MB, MB)
•maka, total cahaya yang diterima oleh mata :
• (LR x MR, LG x MR, LB x MB)
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
44
How OpenGL Simulates
Lights
• Phong lighting model
• Computed at vertices
• Lighting contributors
• Surface material properties
• Light properties
• Lighting model properties
45
Surface Poly.
Poly.
Per
Per
Vertex
Vertex
Normals
CPU DL Raster Frag FB
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
46
Material Properties
• Define the surface properties of a primitive
• glMaterialfv( face, property, value );
47
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
48
Light Sources (cont.)
• Light color properties
• GL_AMBIENT
• GL_DIFFUSE
• GL_SPECULAR
49
Types of Lights
• OpenGL supports two types of Lights
• Local (Point) light sources
• Infinite (Directional) light sources
• Type of light controlled by w coordinate
50
Turning on the Lights
• Flip each light’s switch
glEnable( GL_LIGHTn );
• Turn on the power
glEnable( GL_LIGHTING );
51
Light Material Tutorial
52
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
53
Light Position Tutorial
void gluLookAt( GLdouble eyeX,
GLdouble eyeY,
GLdouble eyeZ,
GLdouble centerX,
GLdouble centerY,
GLdouble centerZ,
GLdouble upX,
GLdouble upY,
GLdouble upZ);
54
Advanced Lighting
Features
• Spotlights
• localize lighting affects
• GL_SPOT_DIRECTION
• GL_SPOT_CUTOFF
• GL_SPOT_EXPONENT
55
Advanced Lighting
Features
• Light attenuation
• decrease light intensity with distance
• GL_CONSTANT_ATTENUATION
• GL_LINEAR_ATTENUATION
• GL_QUADRATIC_ATTENUATION
1
fi
kc kl d k q d 2
56
Light Model Properties
• glLightModelfv( property, value );
• Enabling two sided lighting
GL_LIGHT_MODEL_TWO_SIDE
• Global ambient color
GL_LIGHT_MODEL_AMBIENT
• Local viewer mode
GL_LIGHT_MODEL_LOCAL_VIEWER
• Separate specular color
GL_LIGHT_MODEL_COLOR_CONTROL
57
Shading
Non-Shading
Shading
2. Jenis-Jenis Shading
A. Flat Shading
Flat shading disebut juga dengan konstan shading, yaitu teknik shading
yang memberikan satu jenis warna pada setiap polygon dan hanya
memerlukan satu kali kalkulasi untuk pewarnaan. Tipe flat shading
diantaranya Lambertian Shading dan Uniform Shading.
b) Phong Shading
• Phong Shading dikembangkan oleh Bui Tuong Phong pada tahun 1973 dalam
disertasinya di Universitas Utah. Sama seperti Gourad Shading, hasil Phong Shading
akan memberikan gradasi warna pada permukaan objeknya.
Kelebihannya :
- Memberikan efek shadow & refleksi
- Memberikan warna tiap garisnya, sehingga lebih halus.
Kekurangannya :
- Kalkulasi lama
Shadow
- Bidang dianggap y = 0.
- Jika sumber cahaya terlalu jauh dari objek, maka :