[英]how to get a spotlight in opengl?
参考http://www.chai3d.org/doc/classc_light.html
码
light2->setPos(cVector3d( 0, 0,0.0)); // position the light source
light2->m_ambient.set(0.8, 0.8, 0.8);
light2->m_diffuse.set(0.8, 0.8, 0.8);
light2->m_specular.set(0.8, 0.8, 0.8);
light2->setDirectionalLight(false);//make a positional light
使用opengl的渲染代码是
void cLight::renderLightSource()
{
// check if light source enabled
if (m_enabled == false)
{
// disable OpenGL light source
glDisable(m_glLightNumber);
return;
}
computeGlobalCurrentObjectOnly();
// enable this light in OpenGL
glEnable(m_glLightNumber);
// set lighting components
glLightfv(m_glLightNumber, GL_AMBIENT, m_ambient.pColor());
glLightfv(m_glLightNumber, GL_DIFFUSE, m_diffuse.pColor() );
glLightfv(m_glLightNumber, GL_SPECULAR, m_specular.pColor());
// position the light source in (global) space (because we're not
// _rendered_ as part of the scene graph)
float position[4];
position[0] = (float)m_globalPos.x;
position[1] = (float)m_globalPos.y;
position[2] = (float)m_globalPos.z;
//position[0] = (float)m_localPos.x;
//position[1] = (float)m_localPos.y;
//position[2] = (float)m_localPos.z;
// Directional light source...
if (m_directionalLight) position[3] = 0.0f;
// Positional light source...
else position[3] = 1.0f;
glLightfv(m_glLightNumber, GL_POSITION, (const float *)&position);
// set cutoff angle
glLightf(m_glLightNumber, GL_SPOT_CUTOFF, m_cutOffAngle);
// set the direction of my light beam, if I'm a _positional_ spotlight
if (m_directionalLight == false)
{
cVector3d dir = m_globalRot.getCol0();
float direction[4];
direction[0] = (float)dir.x;
direction[1] = (float)dir.y;
direction[2] = (float)dir.z;
direction[3] = 0.0f;
glLightfv(m_glLightNumber, GL_SPOT_DIRECTION, (const float *)&direction);
}
// set attenuation factors
glLightf(m_glLightNumber, GL_CONSTANT_ATTENUATION, m_attConstant);
glLightf(m_glLightNumber, GL_LINEAR_ATTENUATION, m_attLinear);
glLightf(m_glLightNumber, GL_QUADRATIC_ATTENUATION, m_attQuadratic);
// set exponent factor
glLightf(m_glLightNumber, GL_SPOT_EXPONENT, m_spotExponent);
}
为什么我要均匀照亮我的整个环境? 我如何在原点0,0,0周围得到聚集的光,该光在1或2单位距离后逐渐消失? 我的起源是网格中的中间立方体。
告急者:这是我过去迷恋OpenGL的“从我的头顶”。
我认为OpenGL的“定向光”概念有点像无穷远的点光源,这意味着光矢量在整个场景中是不变的。
为了产生聚光效果,您需要形成光方向和光矢量(从顶点到光的矢量)的点积,并随着角度的增加以指数方式衰减光。
我记得曾经阅读过有关此的教程,将会搜索...
正确, 这里描述的内容向下滚动到“聚光灯”的描述。
在您列出的代码中,我相信您需要确保m_spotExponent
大于零才能获得“圆锥”效果。 较高的值会产生从圆锥体的“亮”部分到“黑暗”部分的“更陡峭”过渡(我认为)。
希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.