OpenGL - 对象转换和 VBO [英] OpenGL - Object Transformations and VBOs

查看:93
本文介绍了OpenGL - 对象转换和 VBO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我编写了一个程序,它使用顶点缓冲区对象渲染网格,并让我四处移动相机.我现在想让物体独立于相机/视图移动.

So I've written a program that renders a mesh using a Vertex Buffer Object, and lets me move the camera around. I now want to make the object move independently of the camera/view.

但是,我不确定如何在空间中移动我的网格.谷歌搜索往往会找到消息来源,要么告诉我使用 glRotatef() 等旋转对象,要么使用 glRotatef() 及其兄弟姐妹是一个坏主意,因为它们已被弃用.也许我没有使用正确的搜索词,但我没有找到那么多似乎是一个好的起点.我看到对矩阵数学的模糊引用,但我不知道如何使用它以及采取什么方法.其他消息来源说我应该应用顶点着色器来转换对象.

However, I'm not sure how to go about moving my meshes through space. Googling tends to find sources either telling me to rotate the objects with glRotatef(), etc., or that using glRotatef() and its siblings is a bad idea because they are deprecated. Perhaps I'm not using the right search terms, but I'm not finding all that much that seems like a good starting point. I see vague references to matrix math, but I don't know how to use that and what approach to take. Other sources say I should apply a vertex shader to transform the objects.

我想我可以在每一帧手动重建我的网格,但这似乎是一个可怕的想法(网格经常有超过 50k 个三角形,我希望至少有几十个),但我没有如果顶点已经存储在 VBO 中,我真的需要在我的其余内存中不断使用它们......对吗?

I suppose I could manually reconstruct my mesh each frame, but that seems like a horrible idea (the meshes frequently have upwards of 50k triangles, and I'd like to have dozens of them at least), and I don't really need to have the vertices constantly in use in the rest of my memory if they are already stored in a VBO... right?

那么我如何独立于全局空间操作存储在 VBO 中的网格?我应该使用哪些资源来学习这样做?

So how do I go about manipulating meshes that are stored in VBOs independently of the global space? What resources should I use in learning to do so?

推荐答案

您应该使用 ModelView 矩阵将变换应用于顶点.要将转换应用于特定对象/网格而不是整个屏幕,请将 ModelView 矩阵的副本推入堆栈,应用转换,绘制对象,然后弹出该矩阵以返回到旧的 ModelView 矩阵.

You should be using your ModelView matrix to apply transformations to your vertices. To apply a transformation to a particular object/mesh and not to the entire screen, push a copy of your ModelView matrix onto the stack, apply your transformation, draw your object, then pop that matrix off to go back to your old ModelView matrix.

无需重新计算顶点位置!这正是这些矩阵旨在帮助您避免的问题.它们存储在 VBO 中这一事实对您来说无关紧要 - 手动传递给 OpenGL 的顶点的处理方式完全相同.

No need to recompute your vertex positions! That's exactly what these matrices are designed to help you avoid. And the fact that they're stored in a VBO won't matter to you - vertices passed to OpenGL manually are treated exactly the same.

您可能想查看这个问题,以及transformation article 其接受的答案链接 - 如果您愿意,它们会很有用仍然掌握转换和矩阵堆栈.

And you might want to check out this question, and the transformation article its accepted answer links to - they'll be useful if you're still getting a hang of transformations and the matrix stack.

希望有帮助!

为什么堆栈有用的一个简单示例.假设您正在绘制一个简单的场景:一个人在海洋中的木筏上(带帆).

A quick example of why the stack is useful. Say you're drawing a simple scene: a guy on a raft (with a sail) in the ocean.

首先,您需要设置相机角度,因此请进行设置所需的任何转换.你不需要——事实上也不想——在这里推送和弹出矩阵,因为这些变换适用于你场景中的一切(在 OpenGL 中,移动相机 = 移动整个世界.想想很奇怪,但你习惯了.)

First, you'll want to set up your camera angle, so do whatever transformations you need to set that up. You don't need - and in fact don't want - to push and pop matrices here, because these transformations apply to everything in your scene (In OpenGL, moving the camera = moving the entire world. Weird to think about, but you get used to it.).

然后你画你的海洋.不需要变换它,因为它是一个静态对象,不会移动.

Then you draw your ocean. No need to transform it, 'cause it's a static object, and doesn't move.

然后你画你的木筏.但是你的木筏已经移动了!它沿 X 轴漂移.现在,由于 raft 是一个独立的对象,并且适用于 raft 的变换不应该适用于更大的世界,因此您将矩阵推入堆栈.这将复制现有的 ModelView 矩阵.所有这些相机变换都已经应用;您在木筏上的漂移"转换是您在堆栈较低级别所做的转换的补充.

Then you draw your raft. But your raft has moved! It's drifted along the X axis. Now, since the raft is an independent object and transformations that apply to the raft shouldn't apply to the larger world, you push a matrix onto the stack. This copies the existing ModelView matrix. All those camera transformations are already applied; Your "drifting" transformation on the raft is in addition to the transformations you did at lower levels of the stack.

画木筏.然后,从堆栈中弹出矩阵之前,绘制木筏上的东西 - 人和帆.由于它们随木筏移动,因此应用于木筏的所有变换都应应用于它们.

Draw the raft. Then, before you pop that matrix off the stack, draw the things that are on the raft - the guy and the sail. Since they move with the raft, all the transformations that apply to the raft should be applied to them, to.

假设你先画你的漂流者.但他也动了——他跳到了空中.因此,您将另一个矩阵推入堆栈,应用跳跃"变换,然后渲染您的人物.如果有什么东西应该随着这个人移动——如果他拿着什么东西,比如说——你也要把它画在这里.但他不是.因此,将跳跃"矩阵从堆栈中弹出.

Say you draw your castaway first. But he's moved too - he's jumping into the air. So you push another matrix onto the stack, apply a "jumping" transformation, and then render your person. If there's anything that should move with the person - if he were holding anything, say - you'd draw it here, too. But he's not. So pop the "jumping" matrix off the stack.

现在您又回到了筏"上下文中.由于您将跳跃"转换应用于副本,因此漂移"转换在堆栈级别下保持不变.现在拉起帆,它就会在木筏的顶部,就在它应该在的地方.

Now you're back in the "raft" context. Since you applied the "jumping" transformation to a copy, the "drifting" transformation was left untouched a stack level down. Draw the sail now, and it'll be on top of the raft, right where it should be.

然后您就完成了 raft,因此您也可以将该矩阵从堆栈中弹出.你又回到了普通的相机变换.绘制更多静态几何体 - 岛屿或其他东西.

And then you're done with raft, so you can pop that matrix off the stack too. You're back down to your plain camera transform. Draw some more static geometry - islands or something.

这就是矩阵堆栈有用的原因.这也是人们将更复杂的场景构建为场景图"的原因——这样他们就可以跟踪转换的嵌套.它在骨骼动画中也很有用,其中手腕的位置取决于肘部的位置,肘部的位置取决于肩部的位置,等等.

And that's why the matrix stack is useful. It's also why people build more complicated scenes scenes as "scene graphs" - so they can keep track of the nesting of transformations. It's also useful in skeletal animation, where the position of the wrist depends on the position of the elbow, which depends on the position of the shoulder, and so forth.

那比我预期的要长得多 - 但希望有用.干杯!

And that was way longer than I expected - but hopefully useful. Cheers!

这篇关于OpenGL - 对象转换和 VBO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆