开发者

OpenGL Spotlights

I'm trying to make 'spotlights' over a pool table in openGL. This should be fairly simple, but something is going wrong, and I can't work out what.

I have a class 'PoolLight' that I'm using as a sort of holding class for the lights. Here is is:

#include "PoolLight.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"

PoolLight::PoolLight() {

}

PoolLight::PoolLight(GLenum lightNumber, GLenum lightType, float red, float green, float blue, bool distant, float posX, float posY, float posZ)
{
    this->lightNumber = lightNumber;
    this->lightType = lightType;

    color[0] = red; color[1] = green; color[2] = blue; color[3] = 1;
    position[0] = posX; position[1] = posY; position[2] = posZ; position[3] = (int) (!distant);
    glLightfv(lightNumber, lightType, color);
    glLightfv(lightNumber, GL_POSITION, position);

    enabled(true);
}


PoolLight::~PoolLight(void)
{
}

void PoolLight::setSpotlight(float angle, float attenuation, float dirX, float dirY, float dirZ) {
    glLightf(lightNumber, GL_SPOT_EXPONENT, angle);
    glLightf(lightNumber, GL_CONSTANT_ATTENUATION, attenuation);
    glLightf(lightNumber, GL_LINEAR_ATTENUATION, 0.0f);
    glLightf(lightNumber, GL_QUADRATIC_ATTENUATION, 0.0f);
    spotDirection[0] = dirX; spotDirection[1] = dirY; spotDirection[2] = dirZ;
    glLightfv(lightNumber, GL_SPOT_DIRECTION, spotDirection);
    glLightf(lightNumber, GL_SPOT_CUTOFF, 60);
}

void PoolLight::enabled(bool enabled) {
    if (enabled) glEnable(lightNumber);
    else glDisable(lightNumber);
}

void PoolLight::reposition() {
    glLightfv(lightNumb开发者_Go百科er, GL_POSITION, position);
}

And in Display::Init, I have this code:

glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glPointSize(6);

//Lighting
middleSpotlight = PoolLight(GL_LIGHT0, GL_DIFFUSE, .3, .3, 0.15, false, 0, 0, 50);
middleSpotlight.setSpotlight(60, 1, 0, 0, -1);
upperSpotlight = PoolLight(GL_LIGHT1, GL_DIFFUSE, .3, .3, 0.15, false, 0, 45, 50);
upperSpotlight.setSpotlight(60, 1, 0, 0, -1);
lowerSpotlight = PoolLight(GL_LIGHT2, GL_DIFFUSE, .3, .3, 0.15, false, 0, -45, 50);
lowerSpotlight.setSpotlight(60, 1, 0, 0, -1);

However, even when disabling all but the middle spotlight my scene is uniformly lit with a sort of 'blanket' lighting.

I feel like I'm probably missing something obvious, but I just can't see what.


This is due to how OpenGL default fixed function pipeline implements lighting: Lighting is evaluated only at the vertices, the resulting colour interpolated over the surface. If your whole table consists of just one large quad, exactly this happens.

Solution 1: Subdivide the mesh to a high degree.

Solution 2: Replace fixed function lighting with a per-fragment-illumination shader.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜