OpenGL光照效果

//  本程序中使用了两个光源,一个是标准的蓝色光源,另一个是红色的聚光灯
#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值