Opengl 对象挤出
我有一个用 opengl 绘制的 3d 框,有人能解释一下如何在 opengl 中挤出对象吗?对于每个框,我是否只是在 z 轴上进一步平移?
解决方案这种方法可行,但您必须计算每一步要翻译多少.不过,我建议以更智能的方式生成挤压几何体.
例如,您不应为中间的盒子绘制盒盖(地板和天花板).您还必须确保两侧完美接触,否则会出现伪影.
我建议使用路径来确定每组顶点所在的平面.路径应该由一系列点和每个点的方向向量组成,这决定了围绕方向向量旋转多少.有了它,您可以非常轻松地计算 4 个环顶点,只需使用基本的向量数学即可.
因此,例如,您从上限 [(0.5,0.5,0),(-0.5,0.5,0),(-0.5,-0.5,0),(0.5,-0.5,0)] 开始并沿路径移动(第一个是中心点,第二个是右向量)[(0,0,0),(1,0,0)],[(0,5,10),(1,1,0)],[(10,5,12),(0,1,0)]
现在,您首先计算所有三个方向向量.法线是当前点和下一个点之间的差值,所以 (0,5,10) - (0,0,0) = (0,5,10).右向量必须投影到法线定义的平面上,因此我们首先使用叉积计算向上向量:(0,5,10) x (1,0,0) = (0,10,-5).作为最后一步,我们计算投影的右向量,它是正常和向上的叉积:(0,5,10) x (-2,4,-2) = (-125,0,0).然后必须对所有三个向量进行归一化,如果将它们并排放置,您将获得一个很好的转换矩阵,将其应用于上限向量,从而产生当前步骤的 4 个顶点:
<前>|-1 0 0 ||0.5||-0.5 ||0 0.894427 0.447214|* |0.5|= |0.447213|等等.|0 -0.447214 0.894427||0 ||0.223607|(我可能对符号有点搞砸了,你可能需要交换交叉乘积因子才能得到正确的结果)
然后对路径上的每一步重复相同的过程,每次绘制 4 个环形四边形.
I've got a 3d box drawn in opengl, can someone explain how to extrude objects in opengl? do i just translate further back in the z axis for each box?
解决方案That approach could work, but you have to do some calculations on how much to translate for each step. I'd recommend generating the extruded geometry in a smarter way, though.
For example, you shouldn't draw the box caps (floor and ceiling) for in-between boxes. You also have to make sure the sides touch perfectly or you will get artifacts.
I recommend using a path to determine the planes where each set of vertices goes. The path should consist of a series of points and an orientation vector for each point, that determines how much to rotate around the direction vector. With that, you can calculate the 4 ring vertices very easily, just using basic vector math.
So, for example, you start with the cap [(0.5,0.5,0),(-0.5,0.5,0),(-0.5,-0.5,0),(0.5,-0.5,0)] and move it along the path (first is center point, second is right vector) [(0,0,0),(1,0,0)],[(0,5,10),(1,1,0)],[(10,5,12),(0,1,0)]
Now, you first calculate all three orientation vectors. The normal is the difference between the current and next point, so (0,5,10) - (0,0,0) = (0,5,10). The right vector must be projected onto the plane defined by the normal, so we calculate the up vector first using the cross product: (0,5,10) x (1,0,0) = (0,10,-5). And as a last step, we calculate the projected right vector, which is the cross product between normal and up: (0,5,10) x (-2,4,-2) = (-125,0,0). All three vectors must be normalized then, and if you put them side by side, you will get a nice transformation matrix that you apply to the cap vectors, yielding the 4 vertices for the current step:
|-1 0 0 | |0.5| |-0.5 | | 0 0.894427 0.447214| * |0.5| = | 0.447213| etc. | 0 -0.447214 0.894427| |0 | | 0.223607|
(I've probably messed up with the signs a bit, you might have to swap the cross product factors to get the right results)
Then you repeat the same procedure for each step on the path and draw the 4 ring quads each time.
相关文章