2012-05-18 58 views
1

我試圖讓光源在我的人物模型旋轉中的OpenGL我的項目,但我試試吧,我的一切,到目前爲止是我的模型旋轉像瘋了似的(或地板)。如何旋轉固定物體(OpenGL的)圍繞lightsource?

我的渲​​染代碼如下所示:

void mainRender() { 
    updateState(); 
    renderScene(); 
    glFlush(); 
    glutPostRedisplay(); 

    //spin = (spin + 30) % 360; 

    Sleep(30); 
} 

void renderScene() { 
    glClearColor(backgrundColor[0],backgrundColor[1],backgrundColor[2],backgrundColor[3]); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // limpar o depth buffer 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    updateCam(); 
    renderFloor(); 
    modelAL.Translate(0.0f,1.0f,0.0f); 
    modelAL.Draw(); 
} 


void renderFloor() { 


    // set things up to render the floor with the texture 
    glShadeModel(GL_SMOOTH); 
    glEnable(type); 
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

    glPushMatrix(); 

    glTranslatef(-(float)planeSize/2.0f, 0.0f, -(float)planeSize/2.0f); 

    float textureScaleX = 10.0; 
    float textureScaleY = 10.0; 
    glColor4f(1.0f,1.0f,1.0f,1.0f); 
    int xQuads = 40; 
    int zQuads = 40; 
    for (int i = 0; i < xQuads; i++) { 
     for (int j = 0; j < zQuads; j++) { 
      glBegin(GL_QUADS); 
       glTexCoord2f(1.0f, 0.0f); // coords for the texture 
       glNormal3f(0.0f,1.0f,0.0f); 
       glVertex3f(i * (float)planeSize/xQuads, 0.0f, (j+1) * (float)planeSize/zQuads); 

       glTexCoord2f(0.0f, 0.0f); // coords for the texture 
       glNormal3f(0.0f,1.0f,0.0f); 
       glVertex3f((i+1) * (float)planeSize/xQuads, 0.0f, (j+1) * (float)planeSize/zQuads); 

       glTexCoord2f(0.0f, 1.0f); // coords for the texture 
       glNormal3f(0.0f,1.0f,0.0f); 
       glVertex3f((i+1) * (float)planeSize/xQuads, 0.0f, j * (float)planeSize/zQuads); 

       glTexCoord2f(1.0f, 1.0f); // coords for the texture 
       glNormal3f(0.0f,1.0f,0.0f); 
       glVertex3f(i * (float)planeSize/xQuads, 0.0f, j * (float)planeSize/zQuads); 

      glEnd(); 
     } 
    } 

    glDisable(type); 


    glPopMatrix(); 
} 

我如何才能讓這個新lightsource在我的「modelAL」對象旋轉?

回答

3

對於固定管道,與模型視圖矩陣一樣,使用glLight()指定的光源位置與普通對象一樣進行變換。所以,你可以使用轉換功能來定位和旋轉光源,你會正常的對象。

要圍繞一個點旋轉光源(或其他物體),您需要按照以下步驟操作。設L是在光源將是當旋轉爲0度,和O是主題 - 各地要旋轉光源的對象。

  1. 位置在LO光源
  2. 旋轉它有關所需軸(可能是Y軸)
  3. 被O翻譯它移動(光源的相對於主體的位置)它到位。

因爲OpenGL的工作方式,你基本上是以倒序的方式做這些。基本上它會是這樣的:

glPushMatrix(); 
glTranslatef(O.x,O.y,O.z); 
glRotate(angle,0,1,0); 
GLfloat lightpos[4] = {L.x-O.x,L.y-O.y,L.z-O.z,1}; 
glLightfv(GL_LIGHT0,GL_POSITION,lightpos); 
glPopMatrix(); 

注意,這隻適用於定位光源,而不是定向光源,即w = 0。

+0

,我只想用球面座標有效地做同樣的事情,因爲它是更清晰建議。 – imallett