o(╯□╰)o深患中度拖延症,也是从开始写这篇笔记到结束居然用了一个月。。。虽然中间是发生了不少事,不过明明就有无数机会可以完成,就是拖着没写代码,各种借口。。。面对如此拖延症该如何是好QAQ
正文:
突然觉得这些日记写着写着就没什么意思。。。只是简单梳理一下书中的内容,没经过很多的思考,可不写心里更虚,怕自己几天就把看的书忘了。对于很多概念,都由于没有好好去写代码验证,而理解流于表面。对于光照这章也是下决心细细琢磨一番(现在才下的决心o(╯□╰)o),毕竟这很重要。
pname 参数名 | 缺省值 | 说明 |
GL_AMBIENT | (0.0, 0.0, 0.0, 1.0) | RGBA模式下环境光 |
GL_DIFFUSE | (1.0, 1.0, 1.0, 1.0) | RGBA模式下漫反射光 |
GL_SPECULAR | (1.0,1.0,1.0,1.0) | RGBA模式下镜面光 |
GL_POSITION | (0.0,0.0,1.0,0.0) | 光源位置齐次坐标(x,y,z,w) |
GL_SPOT_DIRECTION | (0.0,0.0,-1.0) | 点光源聚光方向矢量(x,y,z) |
GL_SPOT_EXPONENT | 0.0 | 点光源聚光指数 |
GL_SPOT_CUTOFF | 180.0 | 点光源聚光截止角 |
GL_CONSTANT_ATTENUATION | 1.0 | 常数衰减因子 |
GL_LINER_ATTENUATION | 0.0 | 线性衰减因子 |
GL_QUADRATIC_ATTENUATION | 0.0 | 平方衰减因子 |
参数名
|
默认值
|
含义
|
GL_LIGHT_MODEL_AMBIENT
|
(0.2,0.2,0.2,1.0)
|
整个场景的环境光强度 |
GL_LIGHT_MODEL_LOCAL_VIEWER
|
0.0 或 GL_FALSE |
镜面反射角度是如何计算的
|
GL_LIGHT_MODEL_TWO_SIDE
|
0.0 或 GL_FALSE
|
指定了单面还是双面光照 |
GL_LIGHT_MODEL_COLOR_CONTROL
|
GL_SINGLE_COLOR
|
镜面颜色的计算是否从环境和散射颜色中分离出来
|
参数名
|
默认值
|
含义
|
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_SHININESS
|
0.0
|
镜面指数 |
GL_EMISSION
|
(0.0,0.0,0.0,1.0)
|
材料的发射颜色 |
GL_COLOR_INDEXES
|
0,1,1
|
环境,散射和镜面颜色索引(索引模式下)
|
#include "light.h"
#include "CLVector.h"
//灯塔聚光灯,
//多球:材质,变化
//对光源位置,方向的操控
//动态更新材料属性
static GLfloat light1pos[] = { 0.0, 0.0, -5.0, 0.0 };
static GLfloat light1angle = 0.0;
static GLfloat camposx = 0;
//light2
static GLfloat light2pos[] = { 0.0, 0.0, -5.0, 1.0 }; //方向性光源
static GLfloat light2ambient[] = { 0.0, 0.0, 0.0, 1.0 }; //环境光强度
static GLfloat light2diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; //散射光强度
static GLfloat light2specular[] = { 0.0, 0.0, 0.0, 1.0 }; //镜面光光强度
static GLfloat light2dir[] = { 0.0, 0.0, 1.0 };
//材料属性
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[] = { 1.0, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shiiness[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = { 0.3, 0.2, 0.2, 0.0 };//发射颜色
GLfloat mat_emission_blue[] = { 0.0, 0.2, 1.0, 0.0 };//发射颜色
void lightInit()
{
glLightfv(GL_LIGHT0, GL_POSITION, light1pos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT1, GL_POSITION, light2pos);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light2dir);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 20.0f);
glLightfv(GL_LIGHT1, GL_AMBIENT, light2ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, light2diffuse );
glLightfv( GL_LIGHT1, GL_SPECULAR, light2specular );
glEnable(GL_LIGHT1);
}
void lightUpdate(float dt)
{
light1angle += 0.0001;
//if (light1angle > 360.0f)
//{
//light1angle = 0.0f;
//}
static float dir = 0.001;
if (camposx <= -10)
{
dir = 0.001;
}
if (camposx >= 10)
{
dir = -0.001;
}
camposx += dir;
}
void lightDiaplay()
{
//位置和方向
glPushMatrix();
glLoadIdentity();
gluLookAt(0.0, 0.0, -30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//glRotatef(light1angle, 0.0, 1.0, 0.0);
//gluLookAt(camposx, 0.0, -30.0, camposx, 0.0, 0.0, 0.0, 1.0, 0.0);//当我们尝试去平移摄像机(沿x轴),可以明显看到效果是光在球体上掠过,说明了光源跟随了摄像机移动。光源的位置是以摄像机坐标系存储的
glPushMatrix();
//glRotatef( light1angle, 0.0, 10000.0, 0.0 );//这样的旋转并没用改变光轴的方向,所以显得十分奇怪
//glTranslatef(-5.0,0.0,0.0);
CLVector vec(light2dir[0], light2dir[1], light2dir[2]);
vec.Rotatef(0.001, 0.0, 1.0, 0.0);
light2dir[0] = vec.x, light2dir[1] = vec.y, light2dir[2] = vec.z;
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light2dir);//光轴旋转,实现灯塔效果
glLightfv(GL_LIGHT1, GL_POSITION, light2pos);//如果这行单独打开,对光源的位置重新设定,由于光源在世界坐标系中位置不变,而我们使摄像机平移,这时球体的光照并没有任何改变。
//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light1dir);
glPopMatrix();
glPushMatrix();
GLUquadricObj* qobj = gluNewQuadric(); //二次曲面对象
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_mat );
glMaterialfv( GL_FRONT, GL_EMISSION, no_mat );
gluSphere(qobj, 2.0, 20, 16); //[2,2]
glTranslatef( 0,8.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_shiiness);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
gluSphere(qobj, 2.0, 20, 16); //[1,2]
glTranslatef(-10, 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);
gluSphere(qobj, 2.0, 20, 16);//[1,3]
glTranslatef(20, 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, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
gluSphere(qobj, 2.0, 20, 16);//[1,1]
glTranslatef(0, -8.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_mat);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
gluSphere(qobj, 2.0, 20, 16);//[2,1]
glTranslatef(-20, 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_mat);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
gluSphere(qobj, 2.0, 20, 16);//[2,3]
glTranslatef(0, -8.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_mat);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission_blue);
gluSphere(qobj, 2.0, 20, 16);//[3,3]
glTranslatef(10.0, 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_mat);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
gluSphere(qobj, 2.0, 20, 16);//[3,2]
glTranslatef(10.0, 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_mat);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
gluSphere(qobj, 2.0, 20, 16);//[3,1]
glPopMatrix();
glPopMatrix();
}