我应该如何在 3D 场景中指定矩形?
当然,在渲染 3D 矩形(即 3D 空间中的矩形)时,它们被指定为两个三角形的顶点列表。然而,该表示包含大量无关信息,多次编码会让人厌烦。我想创建一个“矩形”对象,它允许我指定其纹理、大小、位置和空间方向并导出顶点(和索引)列表,但我不确定最好的方法做吧。我应该指定左下角的位置(预旋转)还是矩形的中心?我应该如何指定方向,作为包含旋转角度的向量?这是一个如此简单和标准的要求,我确信人们以前已经考虑过它,但我在这个网站或其他地方找不到任何关于这个主题的内容。我计划经常使用这些对象,因此我的主要目标(除了性能之外)是易于使用,而不是与内部表示有关。对我来说,简单地编写我能想到的第一件事并不难,但我不想错过任何东西并使其变得不必要的困难。
那么,我应该如何表示 Rectangle 对象呢?欢迎提出意见,但来源会特别有帮助。
编辑:如果有帮助的话,我相信我主要会在立方体的面上使用矩形,尽管不一定作为这些立方体的整个面。
When rendering 3D rectangles (i.e. rectangles in 3D space), of course, they are specified as a list of vertexes for two triangles. However, that representation contains a lot of extraneous information that gets tiresome to code multiple times. I'd like to create a "Rectangle" object that will allow me to specify its texture, size, position, and orientation in space and export the list of vertexes (and indexes), but I'm not sure of the best way to do it. Should I specify the position of the lower left corner (pre-rotation), or the center of the rectangle? How should I specify the orientation, as a vector containing rotation angles? This is such a simple and standard requirement that I'm sure people have thought about it before, but I can't find anything on this site or elsewhere on the subject. I plan to use these objects a lot, so my primary goal (apart from performance) is ease of use rather than anything to do with the internal representation. It wouldn't be hard for me to simply code the first thing I can think of, but I don't want to miss anything and make it unnecessarily difficult.
So, how should I represent a Rectangle object? Opinions are welcome, but sources would be especially helpful.
Edit: if it helps, I believe I'd primarily be using the rectangles on the faces of cubes, though not necessarily as the entire faces of those cubes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法可能是存储将标准的轴对齐正方形变换到所需位置的齐次矩阵,以及确定如何将纹理映射到其上的单独矩阵。
对于位置矩阵,您可以存储不影响
w
坐标的 4x3 矩阵。这只是有点多余:它使用 12 个值,而一般矩形需要 8 个值,但另一方面,将其转换回可用于渲染的形式会更容易。或者,您可以存储一个点位置(边缘或中心,取决于最方便的方式)和两个方向向量,描述每条边缘的方向和长度;您依靠矩形生成器来确保边缘向量是正交的。这将需要 9 个值,这几乎是您能做到的最好结果。
对于纹理映射,您可以存储一个 3x2 矩阵,该矩阵定义 (u,v) 坐标到由矩形边缘定义的坐标的仿射映射。您可以根据您的应用程序方便的方式选择从零开始的 (0,1)x(0,1) 映射或对称 (-1,1)x(-1,1) 映射。无论如何,这将需要 6 个值。
It would probably be simplest to store the homogeneous matrix that transforms a standard, axis-aligned square into the desired location, along with a separate matrix that determines how to map the texture onto it.
For the location matrix, you can store the 4x3 matrix that doesn't affect the
w
-coordinate. This is only a bit redundant: it uses 12 values where a general rectangle needs 8, but on the other hand, it will be much easier to convert it back to a form usable for rendering.Alternately, you can store a point location (edge or center depending on whatever is most convenient), and two direction vectors, describing the direction and length of each edge; you are relying on your rectangle generator to make sure the edge vectors are orthogonal. This will take 9 values, which is almost the best you can do.
For the texture mapping, you can store a 3x2 matrix that defines an affine mapping of the (u,v) coordinates onto the coordinates defined by the edges of the rectangle. You can choose a zero-based (0,1)x(0,1) mapping, or a symmetric (-1,1)x(-1,1) mapping, based on whatever is convenient for your application. In any case, this will require 6 values.
由于矩形只是一个有界平面,那么将其存储为该平面的扩展怎么样:一个点和一个法向量(定义中心——或者可能是一个角——和方向);但是再添加两个组件来设置宽度和高度边界?
As a rectangle is just a bounded plane, what about storing it as an extension of that: a point and a normal vector (defining the centre---or perhaps one of the corners---and orientation); but add in two more components for the width and height bounds?
我认为这实际上取决于您打算如何使用矩形。
例如:如果您有很多矩形,存储两个三角形之一的三个点可能是最好的,因为这样您只需再计算一个点。
如果您通常将矩形居中于某物,则中心点、宽度、高度和旋转角度可能更合适。
我想说:从你认为自然的事情开始。确保您的类能够执行所有必要的计算并将它们隐藏在访问器后面。为此进行一套良好的测试。
这样您就可以随时更改实施。或者你甚至可以有不同的矩形实现来满足不同的需求
I think it really depends on how you intend to use the rectangle.
For example: If you have lots of rectangles, storing the three points of one of the two triangles might be best, because it you only have to calculate one more point.
If you typically center your rectangles on something the center point, width, height and rotation angles might be more appropriate.
I'd say: start with what ever seems naturally for you. Make sure your class is able to do all the necessary calculations and hide them behind accessors. Have a good suite of tests for that.
That way you can change the implementation any time. Or you can even have different rectangle implementations for different needs