Opengl___真实感技术1:光照
光照
设置glLightfv(灯光,属性,属性值);
例如:
1.glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
2.glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
3.glLightfv(GL_LIGHT0, GL_POSITION, position);
设置物体材质glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>
// 评测代码所用头文件-开始
using namespace std;
// 评测代码所用头文件-结束
void init(void)
{
GLfloat position[] = { 0.0,3.0,2.0,0.0 };
GLfloat ambient[] = { 0.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 lmodel_ambient[] = { 0.4,0.4,0.4,1.0 };
GLfloat local_view[] = { 0.0 };
glClearColor(0.0, 0.1, 0.1, 0.0);
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);
glEnable(GL_DEPTH_TEST);
}
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);
glPushMatrix();
glTranslatef(-3.0, 0.0, 0.0);
// 请在此添加你的代码,左侧圆球只有漫反射
/********** Begin ********/
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
/********** End *********/
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glPushMatrix();
// 请在此添加你的代码,中间圆球有环境光和漫反射
/********** Begin ********/
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
/********** End *********/
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0);
// 请在此添加你的代码,右侧圆球有彩色环境光和漫反射
/********** Begin ********/
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
/********** End *********/
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glFlush();
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 600 * 3);//分配内存
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 600);
glutCreateWindow("几何变换示例");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
镜面反射
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>
using namespace std;
// 评测代码所用头文件-结束
void init(void)
{
GLfloat position[] = { 0.0,3.0,2.0,0.0 };
GLfloat ambient[] = { 0.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 lmodel_ambient[] = { 0.4,0.4,0.4,1.0 };
GLfloat local_view[] = { 0.0 };
glClearColor(0.0, 0.1, 0.1, 0.0);
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);
glEnable(GL_DEPTH_TEST);
}
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);
glClearColor(0.0, 0.1, 0.1, 0.0);
glPushMatrix();
glTranslatef(-3.0, 0.0, 0.0);
// 请在此添加你的代码,左侧圆球有环境光、漫反射镜面低反射
/********** Begin ********/
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);
/********** End *********/
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glPushMatrix();
// 请在此添加你的代码,中间圆球有环境光、漫反射镜面高反射
/********** Begin ********/
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);
/********** End *********/
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0);
// 请在此添加你的代码,右侧圆球有彩色环境光、漫反射、镜面高反射和材料辐射
/********** Begin ********/
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, mat_emission);
/********** End *********/
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glFlush();
//glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 600 * 3);//分配内存
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 600);
glutCreateWindow("几何变换示例");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
茶壶光照
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>
using namespace std;
// 评测代码所用头文件-结束
//定义左边茶壶材质系数
GLfloat left_ambient[] = { 5.0f, 0.0f, 0.0f, 1.0f };
GLfloat left_diffuse[] = { 0.8f, 0.8f, 0.0f, 1.0f };
GLfloat left_specular[] = { 1.0f, 1.0f, 0.0f, 1.0f };
GLfloat left_emission[] = { 0.1f, 0.0f, 0.0f, 1.0f };
//定义中间茶壶材质系数
GLfloat mid_ambient[] = { 0.0f, 0.2f, 0.0f, 1.0f };
GLfloat mid_diffuse[] = { 0.0f, 0.8f, 0.0f, 1.0f };
GLfloat mid_specular[] = { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat mid_shininess[] = { 80.0f };
GLfloat mid_emission[] = { 0.0f, 0.1f, 0.0f, 1.0f };
//定义右边茶壶材质系数
GLfloat right_ambient[] = { 0.0f, 0.0f, 0.1f, 1.0f };
GLfloat right_diffuse[] = { 0.0f, 0.0f, 0.8f, 1.0f };
GLfloat right_specular[] = { 0.0f, 0.0f, 0.9f, 1.0f };
GLfloat right_shininess[] = { 50.0f };
GLfloat right_emission[] = { 0.0f, 0.1f, 0.0f, 1.0f };
void myInit(void)
{
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// 请在此添加你的代码,中间茶壶
/********** Begin ********/
glMaterialfv(GL_FRONT, GL_AMBIENT, mid_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mid_diffuse);
//glMaterialfv(GL_FRONT, GL_SPECULAR, mid_specular);
//glMaterialfv(GL_FRONT, GL_SHININESS, mid_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mid_emission);
/********** End *********/
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
glPushMatrix();
glTranslatef(2.0, 0.0, 0.0);
// 请在此添加你的代码,右边茶壶
/********** Begin ********/
glMaterialfv(GL_FRONT, GL_AMBIENT, right_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, right_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, right_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, right_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, right_emission);
/********** End *********/
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
glPushMatrix();
glDisable(GL_LIGHT0);
glTranslatef(-2.0, 0.0, 0.0);
// 请在此添加你的代码,左边茶壶
/********** Begin ********/
glMaterialfv(GL_FRONT, GL_AMBIENT, left_ambient);
//glMaterialfv(GL_FRONT, GL_DIFFUSE, left_diffuse);
//glMaterialfv(GL_FRONT, GL_SPECULAR, left_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, left_emission);
/********** End *********/
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.5, 1.5, -1.5 * (GLfloat)h / (GLfloat)w,
1.5 * (GLfloat)h / (GLfloat)w, -10.0, 10.0);
else
glOrtho(-1.5 * (GLfloat)w / (GLfloat)h,
1.5 * (GLfloat)w / (GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(1000, 400);
glutCreateWindow("几何变换示例");
myInit();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}