标签:main model break robot title 颜色 and class 红色
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
代码如下:
#define GLUT_DISABLE_ATEXIT_HACK
#include "gl/glut.h"
#pragma comment(lib, "glut32.lib")
//显示 回调函数
void OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.7f,0.7f,0.7f);
glutSolidSphere(50.0f,50.0f,50.0f); //绘制一个浅灰色的球体
glutSwapBuffers();
}
//窗口尺寸变化后的回调函数
void OnReShape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式
glLoadIdentity();
GLfloat aspect =(float)w/(float)h;
if(w<=h)
{
glOrtho(-100.0f,100.0f,-100.0f/aspect,100.0f/aspect,-100.0f,100.0f);
}
else
{
glOrtho(-100.0f*aspect,100.0f*aspect,-100.0f,100.0f,-100.0f,100.0f);
}
}
//按键处理回调函数
void OnKey(GLubyte key,GLint x,GLint y)
{
switch(key)
{
case 0x31: //打开红色射灯
glDisable(GL_LIGHT2);
glDisable(GL_LIGHT3);
glEnable(GL_LIGHT1);
break;
case 0x32: //打开绿色射灯
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT3);
glEnable(GL_LIGHT2);
break;
case 0x33: //打开蓝色射灯
glDisable(GL_LIGHT2);
glDisable(GL_LIGHT1);
glEnable(GL_LIGHT3);
break;
case 0x34: //打开红色主光源
glDisable(GL_LIGHT0);
glEnable(GL_LIGHT4);
break;
default: //关闭所有射灯并将主光源换成白色
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT2);
glDisable(GL_LIGHT3);
glDisable(GL_LIGHT4);
glEnable(GL_LIGHT0);
break;
}
glutPostRedisplay();
}
//设置渲染环境
void SetupRC()
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
//设置光照
void SetupLights()
{
GLfloat ambientLight[]={0.2f,0.2f,0.2f,1.0f};//白色主光源环境光
GLfloat diffuseLight[]={0.8f,0.8f,0.8f,1.0f};//白色主光源漫反射
GLfloat specularLight[]={1.0f,1.0f,1.0f,1.0f};//白色主光源镜面光
GLfloat redAmbiLight[]={0.2f,0.0f,0.0f,1.0f};//红色主光源环境光
GLfloat redDiffLight[]={0.8f,0.0f,0.0f,1.0f};//红色主光源漫反射
GLfloat redSpecLight[]={1.0f,0.0f,0.0f,1.0f};//红色主光源镜面光
GLfloat lightPos[]={50.0f,120.0f,120.0f,1.0f}; //光源位置
GLfloat spotLightPos[]={0.0f,0.0f,200.0f,1.0f}; //射灯位置
GLfloat spotDir[]={0.0f,0.0f,-1.0f}; //射灯方向
GLfloat redLight[]={1.0f,0.0f,0.0f,1.0f}; //红色光源
GLfloat greenLight[]={0.0f,1.0f,0.0f,1.0f}; //绿色光源
GLfloat blueLight[]={0.0f,0.0f,1.0f,1.0f}; //蓝色光源
glEnable(GL_LIGHTING); //启用光照
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); //设置环境光源
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); //设置漫反射光源
glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight); //设置镜面光源
glLightfv(GL_LIGHT0,GL_POSITION,lightPos); //设置灯光位置
glEnable(GL_LIGHT0); //打开白色主光源
glLightfv(GL_LIGHT4,GL_AMBIENT,redAmbiLight);
glLightfv(GL_LIGHT4,GL_DIFFUSE,redDiffLight);
glLightfv(GL_LIGHT4,GL_SPECULAR,redSpecLight);
glLightfv(GL_LIGHT4,GL_POSITION,lightPos);
glLightfv(GL_LIGHT1,GL_SPECULAR,redLight); //红色射灯
glLightfv(GL_LIGHT1,GL_POSITION,spotLightPos);
glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spotDir);
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,85.0f);
glLightfv(GL_LIGHT2,GL_SPECULAR,greenLight); //绿色射灯
glLightfv(GL_LIGHT2,GL_POSITION,spotLightPos);
glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,spotDir);
glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,85.0f);
glLightfv(GL_LIGHT3,GL_SPECULAR,blueLight); //蓝色射灯
glLightfv(GL_LIGHT3,GL_POSITION,spotLightPos);
glLightfv(GL_LIGHT3,GL_SPOT_DIRECTION,spotDir);
glLightf(GL_LIGHT3,GL_SPOT_CUTOFF,85.0f);
glEnable(GL_COLOR_MATERIAL); //启用材质的颜色跟踪
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); //指定材料着色的面
glMaterialfv(GL_FRONT,GL_SPECULAR,specularLight); //指定材料对镜面光的反应
glMateriali(GL_FRONT,GL_SHININESS,100); //指定反射系数
}
// main
void main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 480);
glutCreateWindow("OpenGL光源");
glutReshapeFunc(OnReShape);
glutDisplayFunc(OnDisplay);
glutKeyboardFunc(OnKey);
SetupRC();
SetupLights();
glutMainLoop();
}
运行效果如下:
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
标签:main model break robot title 颜色 and class 红色
原文地址:https://www.cnblogs.com/sownchz/p/10500924.html