我有一个glutSolidTeapot(其表面法线根据opengl.org自动生成)和一个发出漫射光的光源。当我尝试旋转茶壶时出现问题:似乎光源也在进行旋转,而不是保持在我定义的相同位置(它基本上跟随茶壶)。正如你在我的代码中看到的那样,我只在初始化时修改了光照位置,所以它不受glRotatef()的影响,因为它在设置光位置后调用。
尽管花了很多时间试图解决这个问题,我真的不知道这种行为可归因于什么。
粘贴glEnable(GL_NORMALIZE);在初始化中也没有解决问题。
我认为所需的输出应该是一个带有光泽右侧的茶壶(因为光线来自那个方向),无论茶壶旋转的角度是什么。
如果您想测试我的代码,请按空格键旋转茶壶。
#include <math.h>
#include <stdlib.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void onInitialization( ) { //creating the light source
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
GLfloat diffuse[]={0.8, 0.8, 0.8, 1.0};
GLfloat pos[]={0.5, 0.0, 0.8, 0.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glRotatef(-90, 1, 0, 0); //we want to see the top of the teapot
}
void onDisplay( ) {
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//rotating on every frame (for testing purposes only)
glRotatef(5, 0,1,0);
glutSolidTeapot(0.4);
glFinish();
glutSwapBuffers();
}
void onKeyboard(unsigned char key, int x, int y) {
if (key==32){ //do rotation upon hitting the Space key
glutPostRedisplay();
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Teapot");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
onInitialization();
glutDisplayFunc(onDisplay);
glutKeyboardFunc(onKeyboard);
glutMainLoop();
return 0;
}
答案 0 :(得分:2)
我想改变
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
到
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
是个好地方。
答案 1 :(得分:1)
我也认为你的应用程序“破坏”了模型视图 矩阵;它永远不会从头开始重新计算。你应该 用LoadIdentity()开始每一帧,然后发出 lightsource,然后根据需要旋转,最后发射 几何。
这不起作用,这不起作用,这不起作用,lalalalalalala,而常见问题显然是错误的,简单明了。
当您仅翻译或仅围绕原点旋转模型时,在glLoadIdentity()之后发出光以获取相机绑定光将在简单情况下起作用。但是当你开始做一些非平凡的,非你好世界的事情,比如围绕不同于原点的点旋转物体(平移(-v),旋转(角度),平移(v))然后移动它以便旋转中心被带到相机前面,然后“负载标识,放置光线”技术开始非常失败,并且很难推断出什么是均匀的继续。
不,我不知道解决方案。
答案 2 :(得分:1)
尝试手动设置每个顶点的正常值。
答案 3 :(得分:1)
我遇到了同样的问题并得到了解决方案。问题是我在程序的 OnLoad 部分设置了光位置 ...我认为发布一些代码是最好的。我使用OpenTK在C#中工作因为我知道我永远不会进入图形业务。 :)
结果是茶壶或其他物体与固定位置的灯一起旋转。我可以移动眼睛位置( LookAt 方法)并且光源保持静止。就像在真实的词中一样:物体可以旋转,但太阳不会旋转。 :)此外,我们可以在物体周围走动,但太阳仍然保持不受影响并保持静止。这段代码就是这样做的。
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
System.Threading.Thread.Sleep(10); //useful thing to lower CPU usage
//I can set the eye position at runtime, it's not necessary to have this
modelview = Matrix4.LookAt(this.eye, Vector3.Zero, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
//**this is the key - you have to set the light position every time**
GL.Light(LightName.Light0, LightParameter.Position, new float[4] { 1000, 1000, 0, 0 });
// We don't want the walls to rotate so we push the current matrix
GL.PushMatrix();
GL.Rotate(move, Vector3.UnitY);
move += 0.5f;
GL.Color3(1.0f, 1.0f, 1.0f);
GL.Begin(BeginMode.Triangles);
//the object that will rotate
GL.End();
GL.PopMatrix();
GL.Begin(BeginMode.Quads);
//some quads (walls) in the background that will stay still
GL.End();
SwapBuffers();
}
我从我的程序中取出了这个并删除了许多不重要的东西,所以它看起来有点奇怪。我希望它有所帮助。
答案 4 :(得分:0)
光的位置被转换 当前的ModelView矩阵 用a指定位置的时间 打电话给glLight *()。
换句话说,灯光没有定位在某种神奇的静态“世界坐标系”中;它们与模型视图矩阵相乘,就像任何几何体一样。这是有道理的;你经常希望将灯锁定在几何体上。
我还认为你的应用程序“破坏”了模型视图矩阵;它永远不会从头开始重新计算。您应该使用LoadIdentity()启动每个帧,然后发出lightsource,然后根据需要旋转,最后发出几何体。
答案 5 :(得分:0)
在我看来,你应该做的不仅是在渲染例程的开头调用glLoadIdentity(),而是推送和弹出模型视图矩阵。
对于具有全局固定光源的场景和两个独立定位并旋转此伪代码的对象,可以解决这个问题:
glLoadIdentity();
// first object
glPushMatrix();
glTranslate(...);
glRotate(...)
glPopMatrix();
// second object
glPushMatrix();
glTranslate(...);
glRotate(...);
glPopMatrix();