Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get a spotlight in opengl?
    text
    copied!<p>reference <a href="http://www.chai3d.org/doc/classc_light.html" rel="nofollow noreferrer">http://www.chai3d.org/doc/classc_light.html</a></p> <p>code</p> <pre><code> light2-&gt;setPos(cVector3d( 0, 0,0.0)); // position the light source light2-&gt;m_ambient.set(0.8, 0.8, 0.8); light2-&gt;m_diffuse.set(0.8, 0.8, 0.8); light2-&gt;m_specular.set(0.8, 0.8, 0.8); light2-&gt;setDirectionalLight(false);//make a positional light </code></pre> <p>the rendering code which uses opengl is</p> <pre><code>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 *)&amp;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 *)&amp;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); } </code></pre> <p>why do I get my whole environment uniformly lighted? how do I get a concetrated light around the origin 0,0,0, which fades away after 1 or 2 unit distance? My origin is the middle cube in the grid.<img src="https://i.stack.imgur.com/vS3Dh.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload