Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在opengl中获得聚光灯?_C++_Opengl - Fatal编程技术网

C++ 如何在opengl中获得聚光灯?

C++ 如何在opengl中获得聚光灯?,c++,opengl,C++,Opengl,参考文献 代码 使用opengl的渲染代码是 void cLight::renderLightSource() { // check if light source enabled if (m_enabled == false) { // disable OpenGL light source glDisable(m_glLightNumber); return; } computeGlobalCurre

参考文献

代码

使用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的“平行光”概念有点像无穷远处的点光源,这意味着光矢量在整个场景中是不变的

为了实现聚光灯效果,需要形成灯光方向和灯光矢量(从顶点到灯光的矢量)的点积,并随着角度的增加以指数方式衰减灯光

我记得曾经读过一篇关于这个的教程,will search

对,它被描述了 向下滚动至“聚光灯”的说明

在您列出的代码中,我相信您需要确保
m_spotExponent
大于零才能获得“锥形”效果。值越高,圆锥体从“亮”部分到“暗”部分的过渡越“尖锐”(我认为)


希望这对您有所帮助

这不是一个与OpenGL相关的问题,因为您使用的是现有的框架。而且,我没有时间研究这个框架并检查它是如何工作的。如果框架使用固定功能管道,则需要在使用照明之前启用照明。检查是否启用以下标志:glEnable(GL_照明);glEnable(GL_颜色_材料);也许灯光是靠近点的,试着将灯光设置到远处。没关系,我可以降低强度吗?我真的不知道固定功能管道是如何工作的。遗憾的是,这么多框架仍然支持ffp。
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);    
}