正确处理 3D 空间中的对象和灯光 - OpenGL
我正在尝试使用我的引擎启动并运行所有内容,但无法确定添加对象和灯光的顺序以使所有内容正确渲染。
我在初始化中设置了投影和模型视图矩阵:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75.0f,(GLfloat)width/(GLfloat)height, 0.1f , 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
然后,当我渲染几何图形时,我清除缓冲区并加载单位矩阵并更新我的相机。
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Camera updates
glRotatef(camera.anglePitch, 1.0f, 0.0f, 0.0f);
glRotatef(camera.angleYaw, 0.0f, 1.0f, 0.0f);
glTranslatef(-camera.position.x, -camera.position.y, -camera.position.z);
// Now render
level.render();
我对关卡的看法是一个相机,我收集的是四处移动的眼睛坐标。我正在尝试在我的关卡中渲染对象和灯光,并让它们显示在正确的位置。
我需要在世界上的特定点添加对象。例如,位于 (10, 10, 25) 的圆环。在渲染此环面对象之前,我需要做些什么才能使它们在 10、10、25 处正确显示?例如,我需要渲染然后翻译它们吗?
另外,第 2 部分与灯光有关。这几乎是同一个问题。当渲染 50、50、50 的灯光时,我必须考虑什么?我可以正常定位灯光还是需要再次将其平移到那里?还是我想多了?
I am trying to get everything up and running with my engine but am having trouble figuring out what order to add objects and lights to get everything rendered correctly.
I set up my projection and modelview matrix in my itialization:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75.0f,(GLfloat)width/(GLfloat)height, 0.1f , 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Then, when I am rendering geometry, I clear the buffer and load the identity matrix and update my camera.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Camera updates
glRotatef(camera.anglePitch, 1.0f, 0.0f, 0.0f);
glRotatef(camera.angleYaw, 0.0f, 1.0f, 0.0f);
glTranslatef(-camera.position.x, -camera.position.y, -camera.position.z);
// Now render
level.render();
My view of my level is a camera, which I gather is eye coordinates moving around. I am trying to render objects and lights in my level and have them show up in the correct places.
I need to add the objects at specific points in the world. For instance, a torus at (10, 10, 25). Is there anything I need to do before I render this torus objects that will make them appear correctly at 10, 10, 25? Do I need to render and then translate them for instance?
Also, part 2 has to do with lights. It is pretty much the same question. What consideration do I have to have when rendering say a light at 50, 50, 50? Can I just position the light normally or do again do I have to translate it to there? Or am I over thinking this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解这个问题,我会同意你想得太多了。 glTranslate 和 glRotate 的变换应用于模型视图矩阵,所有顶点都乘以该矩阵,以提供稍后在投影中裁剪的眼睛坐标。相同的变换也适用于您的灯光。
您还可以使用 glPopMatrix 和 glPushMatrix 将模型视图矩阵的不同状态应用于不同的顶点。
我还建议您参考 http://www.opengl.org/ 中的问题 9.070资源/faq/technical/transformations.htm
If I understand the issue, I would agree that you are overthinking this. The transformations from glTranslate and glRotate are applied to the modelview matrix, by which all your vertices are multiplied to provide the eye coordinates that will later be clipped in projection. The same transformations are applied to your lights as well.
You can also use glPopMatrix and glPushMatrix to apply different states of the modelview matrix to different vertices.
I would also refer you to question 9.070 in http://www.opengl.org/resources/faq/technical/transformations.htm