⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter06.html

📁 OpenGl红宝书
💻 HTML
📖 第 1 页 / 共 5 页
字号:
        glTranslatef (0.0, 0.0, -5.0); 

        glPushMatrix ();
            glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);
            glRotated (0.0, 1.0, 0.0, 0.0);
            glLightfv (GL_LIGHT0, GL_POSITION, position);
            glTranslated (0.0, 0.0, 1.5);
            glDisable (GL_LIGHTING);
            glColor3f (0.0, 1.0, 1.0);
            auxWireCube (0.1);
            glEnable (GL_LIGHTING);
        glPopMatrix ();

        auxSolidTorus (0.275, 0.85);
    glPopMatrix ();
    glFlush ();
}

void myReshape(GLsizei w, GLsizei h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
}


int main(int argc, char** argv)
{
    auxInitDisplayMode (AUX_SINGLE | AUX_RGBA | AUX_DEPTH);
    auxInitPosition (0, 0, 500, 500);
    auxInitWindow (argv[0]);
    myinit();
    auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, movelight);
    auxReshapeFunc (myReshape);
    auxMainLoop(display);
}</PRE>
</UL>

<HR>
<H2>
Selecting a Lighting Model</H2>
OpenGL's notion of a lighting model has three components:
<UL>The global ambient light intensity
<BR>&nbsp;
<P>Whether the viewpoint position is local to the scene or whether it should
be considered to be an infinite distance away
<BR>&nbsp;
<P>Whether lighting calculations should be performed differently for both
the front and back faces of objects</UL>
This section explains how to specify a lighting model. It also discusses
how to enable lighting - that is, how to tell OpenGL that you want lighting
calculations performed.
<H3>
Global Ambient Light</H3>
As discussed earlier, each light source can contribute ambient light to
a scene. In addition, there can be other ambient light that's not from
any particular source. To specify the RGBA intensity of such global ambient
light, use the GL_LIGHT_MODEL_AMBIENT parameter as follows:
<PRE>GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);</PRE>
In this example, the values used for <I>lmodel_ambient</I> are the default
values for GL_LIGHT_MODEL_AMBIENT. Since these numbers yield a small amount
of white ambient light, even if you don't add a specific light source to
your scene, you can still see the objects in the scene. Figure J-18 shows
the effect of different amounts of global ambient light.
<H3>
Local or Infinite Viewpoint</H3>
The location of the viewpoint affects the calculations for highlights produced
by specular reflectance. More specifically, the intensity of the highlight
at a particular vertex depends on the normal at that vertex, the direction
from the vertex to the light source, and the direction from the vertex
to the viewpoint. Keep in mind that the viewpoint isn't actually being
moved by calls to lighting commands (you need to change the projection
transformation, as described in "Projection Transformations" ); instead,
different assumptions are made for the lighting calculations as if the
viewpoint were moved.
<P>With an infinite viewpoint, the direction between it and any vertex
in the scene remains constant. A local viewpoint tends to yield more realistic
results, but since the direction has to be calculated for each vertex,
overall performance is decreased with a local viewpoint. By default, an
infinite viewpoint is assumed. Here's how to change to a local viewpoint:
<PRE>glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);</PRE>
This call places the viewpoint at (0, 0, 0) in eye coordinates. To switch
back to an infinite viewpoint, pass in GL_FALSE as the argument.
<H3>
Two-sided Lighting</H3>
Lighting calculations are performed for all polygons, whether they're front-facing
or back-facing. Since you usually set up lighting conditions with the front-facing
polygons in mind, however, the back-facing ones typically aren't correctly
illuminated. In Example 6-1 where the object is a sphere, only the front
faces are ever seen, since they're the ones on the outside of the sphere.
So, in this case, it doesn't matter what the back-facing polygons look
like. If the sphere was going to be cut away so that its inside surface
would be visible, however, you might want to have the inside surface be
fully lit according to the lighting conditions you've defined; you might
also want to supply a different material description for the back faces.
When you turn on two-sided lighting, as follows
<PRE>glLightModeli(LIGHT_MODEL_TWO_SIDE, GL_TRUE);</PRE>
OpenGL reverses the surface normals for back-facing polygons; typically,
this means that the surface normals of visible back- and front-facing polygons
face the viewer, rather than pointing away. As a result, all polygons are
illumnated correctly.
<P>To turn two-sided lighting off, pass in GL_FALSE as the argument in
the preceding call. See "Defining Material Properties" for information
about how to supply material properties for both faces. You can also control
which faces OpenGL considers to be front-facing with the command <B>glFrontFace()</B>.
See "Reversing and Culling Polygon Faces" for more information about this
command.
<H3>
Enabling Lighting</H3>
With OpenGL, you need to explicitly enable (or disable) lighting. If lighting
isn't enabled, the current color is simply mapped onto the current vertex,
and no calculations concerning normals, light sources, the lighting model,
and material properties are performed. Here's how to enable lighting:
<PRE>glEnable(GL_LIGHTING);</PRE>
To disable lighting, call <B>glDisable()</B> with GL_LIGHTING as the argument.
<P>You also need to explicitly enable each light source that you define,
after you've specified the parameters for that source. Example 6-1 uses
only one light, GL_LIGHT0:
<PRE>glEnable(GL_LIGHT0);</PRE>

<HR>
<BR>&nbsp;
<H2>
Defining Material Properties</H2>
You've seen how to create light sources with certain characteristics and
how to define the desired lighting model. This section describes how to
define the material properties of the objects in the scene: the ambient,
diffuse, and specular colors, the shininess, and the color of any emitted
light. The equations used in the lighting and material-property calculations
are described in "The Mathematics of Lighting." Most of the material properties
are conceptually similar to ones you've already used to create light sources.
The mechanism for setting them is similar, except that the command used
is called <B>glMaterial*()</B>. void <B>glMaterial</B>{if}[v](GLenum <I>face</I>,
GLenum <I>pname</I>, <I>TYPEparam</I>);
<P>Specifies a current material property for use in lighting calculations.
The face parameter can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK to indicate
which face of the object the material should be applied to. The particular
material property being set is identified by pname and the desired values
for that property are given by param, which is either a pointer to a group
of values (if the vector version is used) or the actual value (if the nonvector
version is used). The nonvector version works only for setting GL_SHININESS.
The possible values for pname are shown in Table 6-2 . Note that GL_AMBIENT_AND_DIFFUSE
allows you to set both the ambient and diffuse material colors simultaneously
to the same RGBA value.
<TABLE BORDER CELLPADDING=10 >
<CAPTION ALIGN=TOP><B>Table 6-2 : </B>Default Values for pname Parameter
of glMaterial*()</CAPTION>

<TR ALIGN=LEFT VALIGN=TOP>
<TH>Parameter Name</TH>

<TH>Default Value</TH>

<TH>Meaning</TH>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_AMBIENT</TD>

<TD>(0.2, 0.2, 0.2, 1.0)</TD>

<TD>ambient color of material</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_DIFFUSE</TD>

<TD>(0.8, 0.8, 0.8, 1.0)</TD>

<TD>diffuse color of material</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_AMBIENT_AND_DIFFUSE</TD>

<TD></TD>

<TD>ambient and diffuse color of material</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_SPECULAR</TD>

<TD>(0.0, 0.0, 0.0, 1.0)</TD>

<TD>specular color of material</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_SHININESS</TD>

<TD>0.0</TD>

<TD>specular exponent</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_EMISSION</TD>

<TD>(0.0, 0.0, 0.0, 1.0)</TD>

<TD>emissive color of material</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_COLOR_INDEXES</TD>

<TD>(0,1,1)</TD>

<TD>ambient, diffuse, and specular color indices</TD>
</TR>
</TABLE>

<BR>&nbsp;
<P>As discussed in "Selecting a Lighting Model," you can choose to have
lighting calculations performed differently for the front- and back-facing
polygons of objects. If the back faces might indeed be seen, you can supply
different material properties for the front and the back surfaces by using
the <I>face</I> parameter of <B>glMaterial*()</B>. See Figure J-19 for
an example of an object drawn with different inside and outside material
properties.
<P>To give you an idea of the possible effects you can achieve by manipulating
material properties, see Figure J-21 . This figure shows the same object
drawn with several different sets of material properties. The same light
source and lighting model are used for the entire figure. The sections
that follow discuss the specific properties used to draw each of these
spheres.
<P>Note that most of the material properties set with <B>glMaterial*()</B>
are (R, G, B, A) colors. Regardless of what alpha values are supplied for
other parameters, the alpha value at any particular vertex is the diffuse-material
alpha value (that is, the alpha value given to GL_DIFFUSE with the <B>glMaterial*()</B>
command, as described in the next section. (See "Blending" for a complete
discussion of alpha values.) Also, none of the RGBA material properties
apply in color-index mode; see "Lighting in Color-Index Mode" for more
information about what parameters are relevant in color-index mode.
<H3>
Diffuse and Ambient Reflection</H3>
The GL_DIFFUSE and GL_AMBIENT parameters set with <B>glMaterial*()</B>
affect the color of the diffuse and ambient light reflected by an object.
Diffuse reflectance plays the most important role in determining what you
perceive the color of an object to be. It's affected by the color of the
incident diffuse light and the angle of the incident light relative to
the normal direction. (It's most intense where the incident light falls
perpendicular to the surface.) The position of the viewpoint doesn't affect
diffuse reflectance at all.
<P>Ambient reflectance affects the overall color of the object. Because
diffuse reflectance is brightest where an object is directly illuminated,
ambient reflectance is most noticeable where an object receives no direct
illumination. An object's total ambient reflectance is affected by the
global ambient light and ambient light from individual light sources. Like
diffuse reflectance, ambient reflectance isn't affected by the position
of the viewpoint.
<P>For real-world objects, diffuse and ambient reflectance are normally
the same color. For this reason, OpenGL provides you with a convenient
way of assigning the same value to both simultaneously with <B>glMaterial*()</B>:
<PRE>GLfloat mat_amb_diff[] = { 0.1, 0.5, 0.8, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mat_amb_diff);</PRE>
In this example, the RGBA color (0.1, 0.5, 0.8, 1.0) - a deep blue color
- represents the current ambient and diffuse reflectance for both the front-
and back-facing polygons.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -