2012-05-18 63 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