opengl固定灯还在动
我想在 3D 世界中设置一盏灯,放置在角落里,当我用鼠标移动时,我希望它停留在那里。
当我使用 glut: glutSolidSphere 中的函数时,一切看起来都很好。 但是,当我在世界中添加一个四边形并用鼠标移动时,四边形上的照明会发生变化。知道如何解决这个问题吗?
void World::paint(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
float mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
float no_shininess = 0.0f;
float low_shininess = 5.0f;
float high_shininess = 100.0f;
float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
camera->setup();
light->assignComponentsToGLLightX();
glEnable(GL_LIGHTING);
glMatrixMode( GL_MODELVIEW );
// WORKS
float temp[] = {0.7f, 0.0f, 0.0f, 1.0f};
glPushMatrix();
glTranslatef(3.75f, 3.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, temp);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere( 3.0, 25, 25 );
glPopMatrix();
// Doesn't work
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glBegin(GL_QUADS);
//Front
//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
//glNormal3f(1.0f, 0.0f, 0.0f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
//glNormal3f(0.0f, 0.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
//glNormal3f(-1.0f, 0.0f, 0.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glPopMatrix();
void Camera::setup() const
{
glRotatef(_rotX, 1.0,0.0,0.0);
glRotatef(_rotY, 0.0,1.0,0.0);
glTranslated(-_moveX,-_moveY,-_moveZ);
gluLookAt(3.0 , 5.0 , 25.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
}
void Light::assignComponentsToGLLightX() const
{
glLightfv(_light, GL_AMBIENT, _ambientLight);
glLightfv(_light, GL_DIFFUSE, _diffuseLight);
glLightfv(_light, GL_SPECULAR, _specularLight);
glLightfv(_light, GL_POSITION, _position);
}
所以我在camera_setup()之后设置了灯光调用 但现在四核仍然无法按照我想要的方式工作。光线仍然在变化,我很确定我的法线是正确的(检查了两次)。
我在想,这是否可能,因为当我移动/旋转时我没有改变我的 _camPosX, Y, Z 值?
I want to set a light in my 3d world, positioned in a corner and when I move with my mouse I want it to stay there and just be.
When I use the function from glut: glutSolidSphere, everything looks okay.
But when I add a quad in my world and I move with my mouse, the lighting on the quad changes. Any idea how to solve this?
void World::paint(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
float mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
float no_shininess = 0.0f;
float low_shininess = 5.0f;
float high_shininess = 100.0f;
float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};
camera->setup();
light->assignComponentsToGLLightX();
glEnable(GL_LIGHTING);
glMatrixMode( GL_MODELVIEW );
// WORKS
float temp[] = {0.7f, 0.0f, 0.0f, 1.0f};
glPushMatrix();
glTranslatef(3.75f, 3.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, temp);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere( 3.0, 25, 25 );
glPopMatrix();
// Doesn't work
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glBegin(GL_QUADS);
//Front
//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
//glNormal3f(1.0f, 0.0f, 0.0f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
//glNormal3f(0.0f, 0.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
//glNormal3f(-1.0f, 0.0f, 0.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glPopMatrix();
void Camera::setup() const
{
glRotatef(_rotX, 1.0,0.0,0.0);
glRotatef(_rotY, 0.0,1.0,0.0);
glTranslated(-_moveX,-_moveY,-_moveZ);
gluLookAt(3.0 , 5.0 , 25.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
}
void Light::assignComponentsToGLLightX() const
{
glLightfv(_light, GL_AMBIENT, _ambientLight);
glLightfv(_light, GL_DIFFUSE, _diffuseLight);
glLightfv(_light, GL_SPECULAR, _specularLight);
glLightfv(_light, GL_POSITION, _position);
}
So I setted the light call after the camera_setup()
But now the quad is still not working as I want it to. The light still changes and I am pretty sure that my normals are correct (checked them twice).
I was thinking, is this possible because I don't change my _camPosX, Y, Z values when I move/rotate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
glRotate
/glTranslate
之前调用gluLookAt
应该会有所帮助...其他想法:
Calling
gluLookAt
beforeglRotate
/glTranslate
should help...Other thoughts :
您必须在移动世界/设置相机后设置灯光的位置,即
在之后立即添加调用
You must set the lights' positions after moving the world / setting the camera i.e. add
calls right after