26)光照模型有4部分:全局环境光,近视点或远视点,双面光照,镜面反射颜色是否和环境颜色,散射颜色分开。要指定全局环境光,可以如下:
GLfloat ambient[] = {0.3,0.3,0.3,1.0};glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient);
顶点的镜面反射亮度取决于该点的法线,顶点相对于光源的方向以及顶点相对于视点的方向。要使用近视点,可以这样:glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);这就将视点放置在(0,0,0)处. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE)启用双面光照。典型的光照计算中,分别计算环境光,散射光,镜面反射光和发射光的贡献,然后进行叠加,而在这之后进行纹理映射的话,镜面反射区可能被覆盖,为了解决这个问题,可以glLightModelfv(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR); 这样,每个顶点光照计算将产生两种颜色,主颜色和辅助颜色,前者包含所以非镜面反射光照的贡献,后者是所以镜面反射光照的总贡献。纹理映射的时候只将主颜色和纹理颜色混合起来,执行万纹理映射后,再将主颜色和纹理颜色的混合结果与辅助颜色混合起来。
27) 不同材质属性的示例:
![200791801.jpg](http://img.e-com-net.com/image/product/7736a2e566d94c86a6746c058efa60f5.jpg)
void
init(
void
)
{
GLfloat ambient[]
=
{
1.0
,
0.0
,
0.0
,
1.0
};
GLfloat diffuse[]
=
{
1.0
,
1.0
,
1.0
,
1.0
};
GLfloat specular[]
=
{
1.0
,
1.0
,
1.0
,
1.0
};
GLfloat position[]
=
{
0.0
,
3.0
,
2.0
,
0.0
};
GLfloat lmodel_ambient[]
=
{
0.4
,
0.4
,
0.4
,
1.0
};
GLfloat local_view[]
=
{
0.0
};
glClearColor(
0.0
,
0.1
,
0.1
,
0.0
);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void
display(
void
)
{
GLfloat no_mat[]
=
{
0.0
,
0.0
,
0.0
,
1.0
};
GLfloat mat_ambient[]
=
{
0.7
,
0.7
,
0.7
,
1.0
};
GLfloat mat_ambient_color[]
=
{
0.8
,
0.8
,
0.2
,
1.0
};
GLfloat mat_diffuse[]
=
{
0.1
,
0.5
,
0.8
,
1.0
};
GLfloat mat_specular[]
=
{
1.0
,
1.0
,
1.0
,
1.0
};
GLfloat no_shininess[]
=
{
0.0
};
GLfloat low_shininess[]
=
{
5.0
};
GLfloat high_shininess[]
=
{
100.0
};
GLfloat mat_emission[]
=
{
0.3
,
0.2
,
0.2
,
0.0
};
glClear(GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT);
/*
draw sphere in first row, first column
* diffuse reflection only; no ambient or specular
*/
glPushMatrix();
glTranslatef (
-
3.75
,
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in first row, second column
* diffuse and specular reflection; low shininess; no ambient
*/
glPushMatrix();
glTranslatef (
-
1.25
,
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in first row, third column
* diffuse and specular reflection; high shininess; no ambient
*/
glPushMatrix();
glTranslatef (
1.25
,
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in first row, fourth column
* diffuse reflection; emission; no ambient or specular reflection
*/
glPushMatrix();
glTranslatef (
3.75
,
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in second row, first column
* ambient and diffuse reflection; no specular
*/
glPushMatrix();
glTranslatef (
-
3.75
,
0.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in second row, second column
* ambient, diffuse and specular reflection; low shininess
*/
glPushMatrix();
glTranslatef (
-
1.25
,
0.0
,
0.0
);
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, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in second row, third column
* ambient, diffuse and specular reflection; high shininess
*/
glPushMatrix();
glTranslatef (
1.25
,
0.0
,
0.0
);
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, high_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in second row, fourth column
* ambient and diffuse reflection; emission; no specular
*/
glPushMatrix();
glTranslatef (
3.75
,
0.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in third row, first column
* colored ambient and diffuse reflection; no specular
*/
glPushMatrix();
glTranslatef (
-
3.75
,
-
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in third row, second column
* colored ambient, diffuse and specular reflection; low shininess
*/
glPushMatrix();
glTranslatef (
-
1.25
,
-
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in third row, third column
* colored ambient, diffuse and specular reflection; high shininess
*/
glPushMatrix();
glTranslatef (
1.25
,
-
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
/*
draw sphere in third row, fourth column
* colored ambient and diffuse reflection; emission; no specular
*/
glPushMatrix();
glTranslatef (
3.75
,
-
3.0
,
0.0
);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glutSolidSphere(
1.0
,
16
,
16
);
glPopMatrix();
glFlush();
}
28)颜色材质模式,
glColorMaterial(GL_FRONT, GL_DIFFUSE);glEnable(GL_COLOR_MATERIAL);这将导致正面的DIFFUSE总是设置为当前颜色
29)混合时,首先指定源因子和目标因子,然后按照公式进行混合(默认的操作是相加),设置因子有两种方式:glBlenFunc()和glBlendFuncSeparate()(它可以指定4个因子,即可以使用不同的方式来混合RGB和alpha值)。除了默认的加法外,还可以使用glBlendEquation()来指定其他的混合方式(如减法等)。
30) 启用反走样功能:glEnable(GL_POINT_SMOOTH)或glEnable (GL_LINE_SMOOTH);glEnable (GL_BLEND);或者采用glHint(),例如:
glEnable (GL_LINE_SMOOTH);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
31)雾的效果示例:
![200791803.jpg](http://img.e-com-net.com/image/product/276e72025b5b4c8cb9ea35d807c38545.jpg)
static
GLint fogMode;
/*
Initialize depth buffer, fog, light source,
* material property, and lighting model.
*/
static
void
init(
void
)
{
GLfloat position[]
=
{
0.5
,
0.5
,
3.0
,
0.0
};
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
GLfloat mat[
3
]
=
{
0.1745
,
0.01175
,
0.01175
};
glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
mat[
0
]
=
0.61424
; mat[
1
]
=
0.04136
; mat[
2
]
=
0.04136
;
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
mat[
0
]
=
0.727811
; mat[
1
]
=
0.626959
; mat[
2
]
=
0.626959
;
glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
glMaterialf (GL_FRONT, GL_SHININESS,
0.6
*
128.0
);
}
glEnable(GL_FOG);
{
GLfloat fogColor[
4
]
=
{
0.5
,
0.5
,
0.5
,
1.0
};
fogMode
=
GL_EXP;
glFogi (GL_FOG_MODE, fogMode);
glFogfv (GL_FOG_COLOR, fogColor);
glFogf (GL_FOG_DENSITY,
0.35
);
glHint (GL_FOG_HINT, GL_DONT_CARE);
glFogf (GL_FOG_START,
1.0
);
glFogf (GL_FOG_END,
5.0
);
}
glClearColor(
0.5
,
0.5
,
0.5
,
1.0
);
/*
fog color
*/
}
static
void
renderSphere (GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef (x, y, z);
glutSolidSphere(
0.4
,
16
,
16
);
glPopMatrix();
}
/*
display() draws 5 spheres at different z positions.
*/
void
display(
void
)
{
glClear(GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT);
renderSphere (
-
2
.,
-
0.5
,
-
1.0
);
renderSphere (
-
1
.,
-
0.5
,
-
2.0
);
renderSphere (
0
.,
-
0.5
,
-
3.0
);
renderSphere (
1
.,
-
0.5
,
-
4.0
);
renderSphere (
2
.,
-
0.5
,
-
5.0
);
glFlush();
}