C ++ Opengl-使用聚光灯照明 [英] C++ Opengl - lighting using spotlight

查看:122
本文介绍了C ++ Opengl-使用聚光灯照明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要放在聚光灯/定向光下的模型,

I have a model that needs to be under a spotlight/directional light,

意思是,我需要在两种模式(聚光灯和定向)之间进行切换.

Meaning, I need to switch between the modes (Spotlight and directional).

这是带有一些解释的代码:

Here is the code with some explanation:

我可以通过鼠标移动旋转模型/光源,所以我正在使用

I can rotate the model / light source with mouse movements so I am using

glRotate和glTranslate.

glRotate and glTranslate for that.

一旦用户按下"L"键,就应该在两种模式之间进行切换.

Once the user pressed the 'L' key I'm supposed to switch between the modes.

这是闪电的代码:

void LightBall::projectLight(void)
{
if(LIGHT == _lightMode){
    printf("Entering LIGHT mode\n"); <--- Supposed to be a directional light
    glDisable(GL_LIGHT1);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, _light_position);
}

if(SPOT_LIGHT == _lightMode){
    printf("Entering SPOTLIGHT mode\n"); <--- Supposed to be a spotlight
    glDisable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glLightfv(GL_LIGHT1, GL_POSITION, _light_position);
    glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0);
    glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
    glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,_spotlight_position);
}
}

问题是在它们之间切换时,我总是得到相同的照明模式,

The problem is I always get the same light mode when switching between them,

以下是什么:

另一个示例是在两种光照模式之间切换后仍然获得相同的光照

And another example after switching between 2 light modes and still getting same light

具有光源旋转的光源(小球):

source with light source rotation (the small ball):

我如何获得想要的结果?

How can I get the wanted result?

这里是LIGHT0& LIGHT1定义:

Here are the LIGHT0 & LIGHT1 definitions:

    GLfloat light_ambient[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
_light_position[0] =  0.0;
_light_position[1] = 1.0;
_light_position[2] = 0.0;
_light_position[3] = 0.0;

_spotlight_position[0] = 0.0;
_spotlight_position[1] = -1.0;
_spotlight_position[2] = 0.0;

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);

谢谢!

推荐答案

GL灯是定向灯还是聚光灯取决于其位置的 w (第4个)分量.如果坐标为0,则它​​是定向的.如果非零(通常为1),则为聚光灯.您必须在调用glLightfv(..., GL_POSITION, ...)之前相应地修改_lightPosition.

Whether a GL light is a directional light or a spotlight depends on the w (4th) component of its position. If the coordinate is 0, it's directional. If nonzero (usually 1), it's a spotlight. You'll have to modify _lightPosition accordingly before calling glLightfv(..., GL_POSITION, ...).

这篇关于C ++ Opengl-使用聚光灯照明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆