Exercise 4:材质
要求:
1. 在练习3的基础中为每个茶壶加入不同的材质;
2. 将练习3中的点光源改为不同的颜色;
考察目的:
1. OpenGL中的材质的使用;
2. 理解光源和材质如何综合影响最终的光照结果;
by/scu xx
#include <iostream>
#include <gl\glew.h>
#define GLUT_DISABLE_ATEXIT_HACK//若没有这一句会出现无法解析外部符号的错误,在glut.h前加上这一句就正确了
#include <glut.h>
#pragma comment(lib,"glew32.lib")
using namespace std;
bool isRotatef[4]; // 控制要旋转的茶壶或旋转视图
bool isLightOpen[3]; //定义光源是否打开的bool值
float lightRotatefSpeed = 0; //第一个光源绕三个茶壶的中心旋转的角度
GLfloat LightPosition1[] = {0.0f, 0.0f, 0.0f, 1.0f}; //第一个点光源的位置
GLfloat LightPosition2[] = {4.0f, 4.0f, 6.0f, 1.0f}; //第二个点光源的位置
GLfloat LightPosition3[] = {-1.0f, 1.0f, 2.0f, 1.0f}; //第三个点光源的位置
static int mousePosX = 0,mousePosY = 0, tempX, tempY;
bool isMosDownMove=false;//鼠标按下是否移动的判断变量
struct simpleCamera
{
float camera[3];
float lookat[3];
float cam[3];
};
simpleCamera camera;
GLfloat no_mat[] = {0.0, 0.0, 0.0, 1.0}; //材质发射光颜色
//1号茶壶的材料属性
GLfloat Ambient_Teapot1[] = {0.1, 0.1, 0.1, 1.0}; //材质的环境颜色
GLfloat Diffuse_Teapot1[] = {0.9, 0.8, 0.0, 1.0}; //材质的散射颜色
GLfloat Specular_Teapot1[] = {0.9, 0.8, 0.1, 1.0}; //材质的镜面反射颜色
GLfloat Shininess_Teapot1[] = {110.0}; //镜面反射指数
//2号茶壶的材料属性
GLfloat Ambient_Teapot2[] = {0.5, 0.0, 0.1, 1.0};
GLfloat Diffuse_Teapot2[] = {0.1, 0.5, 0.99, 1.0};
GLfloat Specular_Teapot2[] = {0.2, 0.1, 0.4, 1.0};
GLfloat Shininess_Teapot2[] = {10.0};
GLfloat Emission_Teapot2[] = {0.5, 0.5, 0.0, 1.0};
//3号茶壶的材料属性
GLfloat Ambient_Teapot3[] = {0.0, 0.0, 0.99, 1.0};
GLfloat Diffuse_Teapot3[] = {0.5, 0.99, 0.5, 1.0};
GLfloat Specular_Teapot3[] = {0.0, 0.0, 0.99, 1.0};
GLfloat Shininess_Teapot3[] = {110.0};
void display();
void init();
void keyboard(unsignedchar key, int x, int y);
void motion(int x,int z);
void mouse(int button,int state, int x,int z);
void reshape(int w,int h);
void createLight();
void init(void)
{
glClearColor(0.7f,0.7f, 0.7f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
isLightOpen[0] =isLightOpen[1] = isLightOpen[2] = true; //三个光源都开启
glEnable(GL_DEPTH_TEST);
if (GLEW_OK != glewInit()) {
cout << "Fail to initialize GLEW!\n";
exit(1);
}
createLight();
isRotatef[3] = true; //默认移动鼠标旋转整个视图
}
void createLight()
{