PRE : 灯光绘制正确。
问题initializeGL()
:由于模型更改而手动删除灯光(或强制)时,无论如何都会绘制灯光。
如果我加载一个新模型,所有旧物体的灯光都会在新模型中绘制。我正在使用 QT 的PaintGL()
.
这是绘制灯光的方法:
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);
}
这是渲染函数(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();
}
还有我的 InitializeGL() 方法:
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);
}