第五章_OpenGL光照与材质(二).docVIP

  • 18
  • 0
  • 约2.9万字
  • 约 28页
  • 2018-04-23 发布于河南
  • 举报
第五章_OpenGL光照与材质(二)

第章 5.8 全局环境光 每个光源可以有自己的环境光,但也可以有不来自于任何光源的环境光,指定环境光的方法是: 利用参数GL_LIGHT_MODEL_AMBIENT来调用函数glLightModel*()。 如: float model_ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT , model_ambient ) ; 上述代码为场景中的物体提供了软弱的白色环境光,所以即使场景中没有任何光源,也可以看到场景中的物体。 【综合示例EP5-5】场景中绘制,然后加入一个简单的光照#include stdafx.h int nAngleY=0; // 沿Y轴旋转的角度 int nAngleX=0; // 沿X轴旋转的角度 int nAngleZ=0; // 沿Z轴旋转的角度 void CALLBACK ClockwiseRotateByX() { nAngleX--; } void CALLBACK CounterClockwiseRotateByX() { nAngleX++; } void CALLBACK ClockwiseRotateByZ() { nAngleZ--; } void CALLBACK CounterClockwiseRotateByZ() { nAngleZ++; } void CALLBACK ClockwiseRotateByY() { nAngleY--; } void CALLBACK CounterClockwiseRotateByY() { nAngleY++; } void myinit (void) { /* 将背景清为白色 */ glClearColor (0.0, 0.0,0.0, 1.0); glShadeModel (GL_SMOOTH); GLfloat ambient[] = { 0.5, 0.0, 0.0, 1.0 }; GLfloat specular[] = { 1.0, 1.0, 0.0, 1.0 }; GLfloat diffuse[] = { 0.5, 0.5, 0.5, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat model_ambient[] = { 0.3, 0.0, 0.3, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 128.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT1, GL_POSITION, light_position); //glLightfv (GL_LIGHT1, GL_AMBIENT, ambient); glLightfv (GL_LIGHT1, GL_SPECULAR, specular); glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse); // 指定全局环境光 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,model_ambient); glEnable(GL_LIGHTING); glEnable(GL_LIGHT1); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); } void CALLBACK reshape(GLsizei w, GLsizei h) { if (!h) return; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w = h) glOrtho (-500.0, 500.0, -500.0*(GLfloat)h/(GLflo

文档评论(0)

1亿VIP精品文档

相关文档