How to rotate a lightsource around a fixed object (OpenGL)?

I'm trying to make a light source rotate around my character model in my OpenGL project, but as I try it, all I got so far is my model rotating like crazy (or the floor).

My rendering code looks like this:

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

How could I make this new lightsource rotate around my "modelAL" object?


For the fixed pipeline, light source position assigned with glLight() are transformed with the model-view matrix, just as normal objects are. So you can use the transformation functions to position and rotate your light source as you would normal objects.

To rotate a light source (or other object) around a point, you need to follow these steps. Let L be where the light source will be when the rotation is 0 degrees, and O be the subject - the object around which you want to rotate the light source.

  • Position the light source at LO (the position of the light source relative to the subject)
  • Rotate it about the required axis (probably the Y axis)
  • Translate it by O to move it into position.
  • Because of the way OpenGL works, you essentially do these in backwards order. Basically it would go like this:

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

    Note, this only applies to positioned light sources, not directional ones ie with w=0.

    链接地址: http://www.djcxy.com/p/81834.html

    上一篇: 围绕点旋转相机

    下一篇: 如何在固定对象(OpenGL)周围旋转光源?