倾斜透视 - 处理中的投影矩阵
我想扩展处理,以便能够使用倾斜投影(内阁或骑士)渲染 3D 内容。在查看了camera()、perspective()和ortho()方法的源代码之后,我能够设置正交透视,然后将PGraphics3D#camera矩阵调整为适当的值,并取得部分成功。
void setup() {
camera(30, 30, 30, 0, 0, 0, 1, 1, 0);
ortho(-100, 100, -100, 100, -500, 500);
p3d.camera.set(1, 0, -0.433f, 0, 0, 1, 0.25f, 0, 0, 0, 0, 0, 0, 0, 0, 1);
}
void draw() {
box(20);
}
这会产生正确的视角,但没有表面填充。当删除相机和正交方法调用或两者时,屏幕是空的,尽管我希望相机(...)在稍后被覆盖的同一矩阵上进行操作。
此外,我对 PGraphics3D 中的矩阵有点困惑:相机、模型视图和投影。虽然 OpenGL 保留两个矩阵堆栈 - modelView 和投影,但这里有第三个矩阵堆栈 - 相机。有人能阐明这些矩阵之间的区别和关系吗?
这对于了解何时使用/设置哪一个将很有帮助。
I want to extend processing in order to be able to render 3D stuff with oblique projections (cabinet or cavalier). After looking around source of the camera(), perspective() and ortho() methods I was able to set up an orthographic perspective and then adjust the PGraphics3D#camera matrix to an appropriate value with partial success.
void setup() {
camera(30, 30, 30, 0, 0, 0, 1, 1, 0);
ortho(-100, 100, -100, 100, -500, 500);
p3d.camera.set(1, 0, -0.433f, 0, 0, 1, 0.25f, 0, 0, 0, 0, 0, 0, 0, 0, 1);
}
void draw() {
box(20);
}
This results in the right perspective, but without surface filling. When removing either the camera and ortho method calls or both, the screen is empty, although I'd expect camera(...) to operate on the same matrix that is overwritten later on.
Moreover I'm a little bit confused about the matrizes in PGraphics3D: camera, modelView and projection. While OpenGL keeps two matrix stacks - modelView and projection, here is a third one - camera. Can anybody shed some light on the difference and relation between these matrizes?
This would be helpful in order to know when to use/set which one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好问题!
我按照您的方式运行了以下代码,它看起来像一个白色立方体的等角视图。
发生的情况如下:
仅使用模型视图和投影矩阵执行转换。 相机矩阵只是模型视图通常初始化的一个方便的分离。
如果您使用了draw()函数,则在每次调用之前,modelview矩阵实际上会被初始化为camera矩阵。由于您没有使用draw()函数,因此您的相机矩阵从未使用相机矩阵中的倾斜变换进行更新。
如何创建倾斜投影
作为免责声明,您必须真正理解如何使用矩阵来变换坐标。顺序非常重要。这是一个很好的学习资源:
http://glprogramming.com/red/chapter03.html
我能给出的最快解释是模型视图矩阵将对象坐标转换为相对眼睛坐标,然后投影矩阵采用这些眼睛 坐标并将其转换为屏幕 坐标。因此,您需要在转换为屏幕坐标之前应用倾斜投影。
以下是创建显示一些立方体的机柜投影的可运行示例:
Great question!
I ran the following code as you had it, and it looked like an isometric view of a white cube.
Here's what's happening:
Transformations are only performed using the modelview and projection matrices. The camera matrix is merely a convenient separation of what the modelview is usually initialized to.
If you used the draw() function, the modelview matrix is actually initialized to the camera matrix before each time it is called. Since you didn't use the draw() function, your camera matrix was never updated with your oblique transform in your camera matrix.
How to create an Oblique Projection
As a disclaimer, you must truly understand how matrices are used to transform coordinates. Order is very important. This is a good resource for learning it:
http://glprogramming.com/red/chapter03.html
The quickest explanation I can give is that the modelview matrix turns object coordinates into relative eye coordinates, then the projection matrix takes those eye coordinates and turns them in to screen coordinates. So you want to apply the oblique projection before the transformation into screen coordinates.
Here's a runnable example for creating a cabinet projection that displays some cubes: