spotLight()

Lights Camera / Lights

Description

Adds a spotlight. Creates a cone of light from a specific position pointing in a direction.

Syntax

void spotLight(float r, float g, float b, float x, float y, float z, float nx, float ny, float nz, float angle, float concentration)

Parameters

NameTypeDescription
r/g/bfloatcolor components
x/y/zfloatposition
nx/ny/nzfloatdirection
anglefloatcone angle in radians
concentrationfloatexponent determining center bias

Returns

void

Related

Under the Hood

From Processing.h:

void spotLight(float r, float g, float b, float x, float y, float z, float nx, float ny, float nz, float angle, float conc); inline void PGraphics::spotLight(float r, float g, float b, float x, float y, float z, float nx, float ny, float nz, float angle, float conc) { if(PApplet::g_papplet) PApplet::g_papplet->spotLight(r,g,b,x,y,z,nx,ny,nz,angle,conc); }

Under the Hood

From Processing.cpp:

void PApplet::spotLight(float r, float g, float b, float x, float y, float z, float nx, float ny, float nz, float angle, float conc) { // Java Processing sets spotlight pos/dir through the CURRENT modelview // (which includes LookAt * Scale(1,-1,1)) -- same as world space draw. // We flip Y to compensate for our glScalef(1,-1,1) baked into modelview. if (lightIndex >= 8) return; GLenum lt = GL_LIGHT0 + lightIndex++; GLfloat col[] = { lc(r), lc(g), lc(b), 1.0f }; GLfloat zero[] = { 0.0f, 0.0f, 0.0f, 1.0f }; glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glDisable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); lightsEnabled = true; glEnable(lt); glLightfv(lt, GL_AMBIENT, zero); glLightfv(lt, GL_DIFFUSE, col); glLightfv(lt, GL_SPECULAR, zero); // Store concentration and cutoff cosine for the shader // (GL_SPOT_EXPONENT caps at 128, but Java uses values like 600) int li = lightIndex - 1; lightConcentration[li] = conc; lightCutoffCos[li] = std::cos(angle); // precompute cos(cutoff) // Still set GL_SPOT_CUTOFF so fixed-function fallback works float cutDeg = std::min(angle * 180.0f / PI, 90.0f); glLightf(lt, GL_SPOT_CUTOFF, cutDeg); glLightf(lt, GL_SPOT_EXPONENT, std::min(conc, 128.0f)); // capped for GL glLightf(lt, GL_CONSTANT_ATTENUATION, 1.0f); glLightf(lt, GL_LINEAR_ATTENUATION, 0.0f); glLightf(lt, GL_QUADRATIC_ATTENUATION, 0.0f); // Position: NO Y-flip (modelview Scale already handles it correctly) // Direction: Y-flip ny to compensate for Scale(1,-1,1) in modelview GLfloat posW[] = { x, y, z, 1.0f }; GLfloat dirW[] = { nx, -ny, nz }; glLightfv(lt, GL_POSITION, posW); glLightfv(lt, GL_SPOT_DIRECTION, dirW); }