2011-04-12 72 views
1

我試圖建立一些簡單的單向課程和我只是試圖點燃它,但燈光似乎與相機,這完全是煩人旋轉旋轉......OpenGL光照與相機

我用下面的代碼粘貼了我的代碼(這樣它就不會拉伸頁面),但是這裏也有一些解釋:

我在Display :: Init中添加了燈光(使用PoolLight類)。 相機設置在Display :: Resize中。它放置在(0,0,80)的'檯球臺'上方,俯視(0,0,-1),X軸向前/向上(1,0,0)。

攝像機的這個簡單的運動和旋轉也可以調整燈光,但不是幾何(或者它可能調整幾何,而不是燈光,我不太確定)。

我已經閱讀了一些關於同一問題的其他答案,但我並不太瞭解。我希望有人能夠以更簡單的方式向我解釋,或者可能會發現一些我意外排除的事情。

總之,這裏是代碼:

Main Display Class

PoolLight類:

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

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); 
} 

回答

3

改變相機位置後,與光的世界座標,再打電話glLightfv(lightNumber, GL_POSITION, position)。然後畫出場景。