質問

PRE: Lights are drawn correctly.

ISSUE: When manually deleting a light (or forcing initializeGL()) due to model change, light is drawn anyway.

If I load a new model, all the old objects light's are drawn within the new ones. I am using QT's PaintGL().

This is the method that draws the lights:

if (!mLightsList.isEmpty()){     //if the list of lights is NOT empty
    glEnable(GL_LIGHTING);
    QList<Light *> LightsTempList=mLightsList;
    for (int k=0;k<LightsTempList.count();k++) {
        glEnable(GL_LIGHT0+k);
        Light* first= LightsTempList.first();
        float light_diffuse[] = {33, 33, 33, 0.5};
        float light_color[] = {255, 0, 0, 0.5};
        float light_ambient[] = {255, 0, 0, 0.5};
        float light_position[] = {first->Position().x, first->Position().y, first->Position().z,0 };
        if (first->Type()==Default){
            glLightfv(GL_LIGHT0+k, GL_DIFFUSE, light_diffuse);
            glLightfv(GL_LIGHT0+k, GL_POSITION, light_position);
            glLightfv(GL_LIGHT0+k, GL_COLOR, light_color);
            glLightfv(GL_LIGHT0+k, GL_AMBIENT, light_ambient);
        }
        else
        ...
        LightsTempList.removeFirst();
        glDisable(GL_LIGHT0+k);
    }
}
glDisable(GL_LIGHTING);
}

This is the render function (PaintGL()):

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glViewport (0, 0, mScreenWidth, mScreenHeight);
gluLookAt(mObjCamera->Pos().x,  mObjCamera->Pos().y,  mObjCamera->Pos().z,
              mObjCamera->View().x, mObjCamera->View().y, mObjCamera->View().z,
              mObjCamera->Up().x,   mObjCamera->Up().y,   mObjCamera->Up().z);

DrawGLLights();
DrawGrid();
DrawAxis();
}

And my InitializeGL() method:

void Preview::initializeGL()
{
glClearColor(0.8215, 0.8215, 0.745,0.5f);
glShadeModel(GL_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_MULTISAMPLE);
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
glEnable(GLUT_MULTISAMPLE);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);

mLightsList.clear();
mHitsList.clear();
BuildObjectsLists(mScene,1);
BuildObjectsLists(mScene,0);
}
役に立ちましたか?

解決

First of all there is no such thing like OpenGL initialization. If you call that initializeGL function nothing resets. You just set some state variables to a know values and that's it. But that function does not reset OpenGL state related do lighting so it's completely useless for that case.

The name DrawGLLights is inappropriate, because lights are not drawn. They're state you set before drawing the geometry onto which to apply lighting. I'm not seeing the code that actually draws geometry that could be illuminated, if I assume that axis and grids are drawn using GL_LINES which don't illuminate well using the fixed function pipeline (you'd require a strand illumination shader for that).

And last but not least: Don't use the fixed function pipeline if you can avoid it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top