Docsity

Lighting and Shading in OpenGL: Setting Up Lights, Shading Models, and Backface Culling, Slides of Fundamentals of E-Commerce

Toggle sidebar
bg1
Lighting in OpenGL
The following sample code will set up a simple
light:
float amb[4] = { 0.3, 0.3, 0.3, 1.0 };
float dif[4] = { 0.5, 0.5, 0.5, 1.0 };
float pos[4] = { 0, 0, -10, 0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
GL_LIGHT0 is the first possible light in GL.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lighting and Shading in OpenGL: Setting Up Lights, Shading Models, and Backface Culling and more Slides Fundamentals of E-Commerce in PDF only on Docsity!

Lighting in OpenGL ^

The following sample code will set up a simplelight:

float

amb[4]

=

{

0.3,

0.3,

0.3,

};

float

dif[4]

=

{

0.5,

0.5,

0.5,

};

float

pos[4]

=

{

0,

0,

-10,

0

};

glLightfv(GL_LIGHT0,

GL_AMBIENT,

amb);

glLightfv(GL_LIGHT0,

GL_DIFFUSE,

dif);

glLightfv(GL_LIGHT0,

GL_POSITION,

pos);

glEnable(GL_LIGHT0);glEnable(GL_LIGHTING);glEnable(GL_COLOR_MATERIAL);

^

GL_LIGHT0 is the first possible light in GL.

Lighting in OpenGL - specular highlights ^

When using specular lighting inOpenGL, the default ‘shininess’setting used in the lightingcalculations is set to themaximum possible shininess:

^

To see a clear highlight, you’llneed to turn down the shine:^ glMaterialf(GL_FRONT_AND_BACK,

GL_SHININESS,10);

//

possible

range:

0--128.

Shading Models ^

There are a couple of different ways to shadeyour model:

Flat Shading

Smooth Shading

Shading Models ^

In

flat shading

, the renderer uses the normal of

the polygon to shade the entire polygonuniformly, from corner to corner.

P

P

P

P

P

Shading models in OpenGL ^

To specify the shading model in OpenGL: ^

Smooth shading:^ 

glShadeModel(GL_SMOOTH); ^

Flat shading:^ 

glShadeModel(GL_FLAT); ^

Note that solid wireframe models look best in flatshading, but most curving surfaces look best insmooth shading.

Backface Culling ^

Another use of normals is

backface culling

, an

optimisation to speed up rendering. ^

If you have a solid object, opaque, then you can’tsee the back of it because the front is in the way. ^

So you can optimize rendering and never evenprocess any polygon on the back of the object. ^

How to tell if it’s on the back? Well, one sure-firetrick is to see if the polygon

faces away from the

camera

. If we see the poly’s back then there

must be some other poly between that poly andour camera.

Backface Culling in OpenGL ^

By default, backface culling is enabled inOpenGL. ^

To enable backface culling:^ 

glEnable(GL_CULL_FACE); ^

To disable backface culling:^ 

glDisable(GL_CULL_FACE); ^

To choose between culling fronts or backs:^ 

glCullFace(GL_FRONT);  glCullFace(GL_BACK);

// (Default)