简体   繁体   English

WebGL-推和弹出矩阵的替代方法是什么?

[英]WebGL - What is the alternative to pushing & popping matrices?

I read the following post about the push/pop approach when rendering is not really being required when working with a scene/graph "system" (reference link below). 当使用场景/图形“系统”时,实际上并不需要渲染时,我阅读了以下有关“推/弹出”方法的文章(下面的参考链接)。

I am just curious now, if no matrix push/pop is required, how does one render (and transform) a set of objects properly, like the ubiquitous example of a rotating Wheel & Bolts? 我现在很好奇,如果不需要矩阵推入/弹出,那么如何正确渲染(和变换)一组对象,例如无处不在的“旋转的车轮和螺栓”示例?

See reference: Does webGL contain push/popMatrix? 请参阅参考: webGL是否包含push / popMatrix?

All that pushing and popping achieves is a matrix stack. 推入和弹出实现的只是矩阵堆栈。 All a matrix stack achieves is storage and restoration of a previous active matrix. 矩阵堆栈所能实现的全部是存储和恢复先前的活动矩阵。 So it's a convenient way to manage a scene for the same reason that a call stack is a convenient way to run a program but it's far from required. 因此,这是一种管理场景的便捷方法,其原因与调用堆栈是一种运行程序的便捷方法相同,但远非必需。

So eg you might just have the camera matrix, M , and for each object i , the object matrix Oi . 因此,例如,您可能只具有相机矩阵M ,而对于每个对象i ,对象矩阵Oi

Then to calculate the equivalent of GL's old MODELVIEW for each object in turn that's just the composition of M and Oi . 然后MODELVIEW为每个对象计算GL的旧MODELVIEW的等效项,而这只是MOi No need to store or restore anything from a stack. 无需从堆栈存储或还原任何内容。 You know M and you know Oi and that's all you need to know. 您知道M ,也知道Oi ,这就是您需要知道的全部。

EDIT: eg for argument's sake suppose you were writing Battlezone . 编辑:例如,为了论证,假设您正在编写Battlezone Then you'd have a bunch of objects on the play field. 然后,您将在运动场上放上一堆物品。 Each object would have a position and a rotation. 每个对象都有一个位置和一个旋转。 One such object would be the player, to which the camera is mounted. 这样的对象之一就是安装摄像机的播放器。 You might draw the scene by (i) computing a view matrix from the player's position and rotation; 您可以通过(i)根据玩家的位置和旋转来计算视图矩阵来绘制场景; then (ii) looping through every object on the play field, computing a model matrix from its position and rotation; 然后(ii)遍历运动场上的每个对象,根据其位置和旋转计算模型矩阵; and, for each, (iii) composing the two, posting the result to OpenGL and making such geometry calls as are necessary to draw the object. (iii)将两者组成,然后将结果发布到OpenGL,并进行绘制对象所需的几何调用。

No stack. 没有堆栈。

The same thing applies for any play field that's just a flat list of objects (aside: I'm ignoring broad phase model culling for simplicity). 同样的情况适用于只是一个平面对象列表的任何游戏场(顺便说一句:为了简单起见,我忽略了广义阶段模型剔除)。

Now suppose some objects are child objects, for whom position and orientation is defined only in terms of a parent object. 现在假设一些对象是子对象,其位置和方向仅根据父对象定义。 In that case, whether you store it as one or not, you have a tree of objects. 在这种情况下,无论是否将其存储为一个对象树。

One solution might be essentially identical to the Battlezone solution, whereby you iterate through every object in turn but now you do: 一种解决方案可能与Battlezone解决方案基本相同,在该解决方案中,您依次遍历每个对象,但现在您可以这样做:

model matrix = identity
while(object is not root)
{
    compose model matrix with matrix for this object
    let object = parent object
}

So you're walking back up from child to root. 因此,您正在从孩子回到根。

Still no stack. 仍然没有堆栈。

Conversely, you could flip things around and just let the normal call stack be a stack implicitly: 相反,您可以翻转一下内容,而只是让普通调用堆栈隐式地成为堆栈:

draw object: object, parent matrix: parent matrix

    child matrix = composition of parent matrix and this object's matrix
    draw object

    for(children of object)
    {
         draw object: child, parent matrix: child matrix
    }

So there's a stack in there but it's the call stack. 因此,这里有一个堆栈,但它是调用堆栈。

Of course your tree doesn't have to be a formal tree data structure. 当然,您的树不必是正式的树数据结构。 In the Battlezone example you had an implicit tree where everything is a first-level child of the root. 在“战区”示例中,您有一棵隐式树,其中所有内容都是根的第一级子级。 Similarly any other code structure could effect an implicit tree. 类似地,任何其他代码结构都可能影响隐式树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM