发信人: wend.bbs@bbs.cst.sh.cn (问道), 信区: cnprogram
标 题: OPENGL基础篇(6)
发信站: 生命玄机站 (Sat Feb 28 23:37:23 1998)
转信站: Lilac!ustcnews!cstshnews!lifebbs
紧接上一次,这回讲材质:
OPENGL通过材料对R、G、B的近似反光率来近似定义材料颜色。
也分为环境、漫反射、镜面反射成分。他们决定材料对环境光、漫反射光和
镜面反射光的反射程度。将材料的特性与光源特性结合就是观察的最终显示
效果。例如红色塑料球,大部分是红色,在光源形成的高光处,则出现光源
的特性颜色。很EASY,不是么?
材质的定义:
void glMaterial{if}[v](GLenum face,GLenum pname,TYPE param);
其中:
face:可以是GL_FRONT、GL_BACK、GL_FRONT_AND_BACK,它表明当前材质应用
到物体的哪一个表面上。
pname说明特定材质属性(很类似上一次光源的定义):
pname 缺省值 说明
GL_AMBIENT 0.2,0.2,0.2,1.0 材质的环境反射光
GL_DIFFUSE 0.8,0.8,0.8,1.0 材质的漫反射光
GL_AMBIENT_AND_DIFFUSE 材质的环境光和漫反射光颜色
GL_SPECULAR 0.0,0.0.0.0,1.0 材质镜面反射光
GL_SHINESS 0.0 镜面指数(光照度)
GL_EMISSION 0.0,0.0,0.0,1.0 材质的辐射颜色
GL_COLOR_INDEXES 0,1,1 材质的环境光、漫反射光和镜面
反射光颜色
请看下面材质的简单例子:
////////////////////////////////////////////////////////////////////////
//sample.cpp
#include "glos.h"
#include <GL/gl.h>
#include <GL/glaux.h>
#include "windows.h"
void myinit(void);
void CALLBACK display(void);
void CALLBACK reshape(GLsizei w,GLsizei h);
void myinit(void)
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(0,0,500,500);
auxInitWindow("sample1");
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
// glShadeModel(GL_FLAT);
//首先定义一个材质,定义方法非常类似前面讲到的光源定义。
GLfloat mat_ambient[]={0.8,0.8,0.8,1.0};
//定义 紫色 的漫反射特性
GLfloat mat_diffuse[]={0.8,0.0,0.8,1.0};
//定义 亮紫色 的镜面反射特性
GLfloat mat_specular[]={1.0,0.0,1.0,1.0};
//定义镜面反射的光亮度
GLfloat mat_shininess[]={50.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,mat_shininess);
//这里我们简化光源,以显示材质的效果。
//这里我们只指定光源位置,其他默认:白色光源。
//你也可以加入光源的定义,看看光源和材质的合成的效果
//正是因为它们能够合成,才能产生比真实生活中多的多的效果,这也正是
//3D技术吸引人的魅力所在。
GLfloat light_position[]={1.0,1.0,1.0,0.0};
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
// GLfloat light_diffuse[]={0.0,0.0,1.0,1.0};
// glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
//将光源设置应用
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//着色消隐
//*******其实说白乐,这就是大名鼎鼎的 Z-BUFFER 呀************//
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void CALLBACK reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void draw(void)
{
auxSolidSphere(1.0);
}
void CALLBACK display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
draw();
glFlush();
}
void main(void)
{
myinit();
auxReshapeFunc(reshape);
auxMainLoop(display);
}
//end of sample
////////////////////////////////////////////////////
通过以上的例子,我们会看到材质定义的实现和光源的效果是一样的,
就我们日常的感觉,定义材质更加符合人们的习惯。而光源使用白光。
这里我们看到的是一个紫色的球,其高光部分呈现亮紫色。
作为比较,下面我们给出一个12个彩色球的例子,大家可以看出各种
光源、材质应用的效果,在一起比较。
给出例子之前,介绍两个函数:
void glPushMatrix();
void glPopMatrix();
我前面已经讲过,所有几何投影变换都是矩阵相乘的结果。如果你希望
保持一个初始坐标点,就要用到这两个重要函数:矩阵入栈和矩阵出栈。
你可以这样理解,为了在左上角画一个球,你先保存当前矩阵,glPushMatrix()
(入栈),然后把坐标平移到左上角,然后调用auxSolidSphere(1.0),画
一个半径1.0的球,这时再调用glPopMatrix()(矩阵出栈),就又回到原始坐标
点,这时的球就在左上角乐。
这个程序很长,但实际上,长的部分统统是重复画12个球的工作,所以也比较
好理解。
//////////////////////////////////////////////////////////////////
//sample.cpp
#include "glos.h"
#include <GL/gl.h>
#include <GL/glaux.h>
#include "windows.h"
void myinit(void);
void CALLBACK display(void);
void CALLBACK reshape(GLsizei w,GLsizei h);
void myinit(void)
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(0,0,500,500);
auxInitWindow("sample1");
glClearColor(0.0,0.1,0.1,0.0);
glClear(GL_COLOR_BUFFER_BIT);
// glShadeModel(GL_FLAT);
//首先定义光源
GLfloat light_ambient[]={0.0,0.0,0.0,1.0};
GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light_specular[]={1.0,1.0,1.0,1.0};
GLfloat light_position[]={0.0,3.0,2.0,0.0};
GLfloat Imodel_ambient[]={0.4,0.4,0.4,1.0};
//应用光源
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//初始化Z BUFFER
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void CALLBACK reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-6.0,6.0,-6.0*(GLfloat)h/(GLfloat)w,
6.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-6.0*(GLfloat)h/(GLfloat)w,
6.0*(GLfloat)h/(GLfloat)w,-6.0,6.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void draw(void)
{
auxSolidSphere(1.0);
}
void CALLBACK display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//建立材质数据库
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};
//////////////////////////////////////////////////////////
//1-1 仅有漫反射光,无环境光和镜面光
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);
draw();
glPopMatrix();
//1-2 有漫反射光,并且有低高光,无环境光
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);
draw();
glPopMatrix();
//1-3 有漫反射光和镜面光,很亮的高光,无环境光
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);
draw();
glPopMatrix();
//1-4 有漫反射光和辐射光,无环境光和镜面反射光
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);
draw();
glPopMatrix();
//////////////////////////////////////////////////////////////
//2-1 有漫反射光和环境光,无镜面反射光
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);
draw();
glPopMatrix();
//2-2 有漫反射光、环境光和镜面光,而且有低高光
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);
draw();
glPopMatrix();
//2-3 有漫反射光环境光和镜面光,而且有很亮的高光
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);
draw();
glPopMatrix();
//2-4有漫反射光、环境光和辐射光,无镜面光
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);
draw();
glPopMatrix();
///////////////////////////////////////////////////////////////
//3-1 有漫反射光和有颜色的环境光,无镜面光
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);
draw();
glPopMatrix();
//3-2有漫反射光和有颜色的环境光以及镜面光,且有低高光
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);
draw();
glPopMatrix();
//3-3 有漫反射光和有颜色的环境光以及镜面光,且有很亮的高光
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);
draw();
glPopMatrix();
//3-4 有漫反射光和有颜色的环境光以及辐射光,无镜面光
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);
draw();
glPopMatrix();
glFlush();
}
void main(void)
{
myinit();
auxReshapeFunc(reshape);
auxMainLoop(display);
}
--
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.74.90]
--
※ 来源:·“生命玄机”站 bbs.cst.sh.cn·[FROM: Wend.bbs@bbs.net.tsi]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:211.658毫秒