跳转到主要内容

OpenGL-光源位置的控制

demi 提交于

<font size="4" color="blue">1. 视图变换:</font>

将相机移动到准备拍摄的位置,将它对准某个方向。

<font size="4" color="blue">2. 模型变换:</font>

将准备拍摄的对象移动到场景中的指定位置。

<font size="4" color="blue">3. 光源保持静止</font>

在使用完视图和模型变换之前设置光源的位置,init()后面,display()之前。

光源出现在视图变换之前,后面进行的变换与光源没有关系,相对于视点固定。

<pre>void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

GLfloat position[] = { 0.0, 0.0, -3.0, 1.0 }; //灯泡
glLightfv (GL_LIGHT0, GL_POSITION, position);
}</pre>

<font size="4" color="blue">4. 独立的移动光源</font>

在模型变换之后设置光源位置。

<pre>void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 }; //灯泡
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();//用于隔离视图变换和模型变换
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glPushMatrix ();//用于隔离视图变换和模型变换
glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);//旋转光源
glLightfv (GL_LIGHT0, GL_POSITION, position);
glTranslated (0.0, 0.0, 1.5);
glDisable (GL_LIGHTING);
glColor3f (0.0, 1.0, 1.0);
glutWireCube (0.1);
glEnable (GL_LIGHTING);
glPopMatrix ();//局部坐标系归位

glutSolidTorus (0.25, 0.85, 8, 15);
glPopMatrix ();
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}</pre>

<center><img width="600" src="http://imgtec.eetrend.com/files/2019-06/%E5%8D%9A%E5%AE%A2/100043908-73…; alt=""></center><br>

<font size="4" color="blue">5. 光源和对象一起移动</font>

<pre>void display(void) //光源和对象一起移动
{
GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

//glPushMatrix ();
glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);//旋转对象
glLightfv (GL_LIGHT0, GL_POSITION, position);

glTranslated (0.0, 0.0, 1.5);
glDisable (GL_LIGHTING);
glColor3f (0.0, 1.0, 1.0);
glutWireCube (0.1);
glEnable (GL_LIGHTING);

glTranslated (0.0, 0.0, 1.5);
glutSolidTorus (0.275, 0.85, 8, 15);
glPopMatrix ();
glFlush ();
}</pre>

<center><img width="600" src="http://imgtec.eetrend.com/files/2019-06/%E5%8D%9A%E5%AE%A2/100043908-73…; alt=""></center><br>

<font size="4" color="blue">6. 光源和观察点一起移动</font>

在视图变换之前设置光源位置,(ex, ey, ez)控制观察点位置;(upx, upy, upz)控制朝上向量的值;现在如果观察点移动位置,光源也跟着一起移动

<pre>void display(void) //光源和一起移动
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix ();
//gluLookAt (ex, ey, ez, 0.0, 0.0, 0.0, upx, upy, upz);
gluLookAt (5.0, 0.0, move, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutSolidTorus (0.275, 0.85, 8, 15);
glPopMatrix ();
glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
glLightfv (GL_LIGHT0, GL_POSITION, position);
}
</pre>

本文转自:CSDN,翻译:博主<a href="https://blog.csdn.net/bigmoyu/article/details/75762675">bigMoYu</a&gt;,转载此文目的在于传递更多信息,版权归原作者所有。