// 本程序中使用了两个光源,一个是标准的蓝色光源,另一个是红色的聚光灯
#include <windows.h>
#pragma warning(disable : 4305)
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
void myinit(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK display(void);
// 初始化材质特性、光源、光照模型和深度缓冲区
void myinit(void)
{
GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0};
GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 };
GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 };
GLfloat spot_direction[]={ 1.0,1.0,-1.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);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION,light1_position);
glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void CALLBACK display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated (-3.0, -3.0, 3.0);
glDisable (GL_LIGHTING);
glColor3f (1.0, 0.0, 0.0);
auxWireCube (0.1);
glEnable (GL_LIGHTING);
glPopMatrix ();
auxSolidSphere(2.0);
glFlush();
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-5.5, 5.5, -5.5*(GLfloat)h/(GLfloat)w,
5.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-5.5*(GLfloat)w/(GLfloat)h,
5.5*(GLfloat)w/(GLfloat)h, -5.5, 5.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// 主函数,初始化窗口大小、RGBA显示模式和处理输入事件
void main(void)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 500, 500);
auxInitWindow ("位置光源和多光源演示");
myinit();
auxReshapeFunc (myReshape);
auxMainLoop(display);
}
OpenGL光照效果
最新推荐文章于 2024-02-08 20:04:55 发布