在调用rotatef c ++之后获取对象的值 [英] Getting values of an object after a call to rotatef c++

查看:108
本文介绍了在调用rotatef c ++之后获取对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问一个顶点对象的X,Y值,Ive旋转它(使用glRotate3f)。这怎么可能?我需要em来解决一个对象碰撞问题,其中碰撞将基于对象旋转度以角度相关方向发送对象。



edit ::



好吧,Ive接近找到一种方法。假设X和Y以及度数为0,90,180,270。我设置一个点作为对象的中心,假设X = 30,Y = 70,X_size为60,Y_size为140.



如果我使new_X = X * cos(angle)-Y * sen(angle)和new_Y = X * sen(angle)+ Y * cos对象中心X和Y到new_X和new_Y我实际上到达新的点,除非当角度为180°,我需要去-new_X + X。



这是解决方法吗?

解决方案

你误解了一点。



您没有通过调用glRotatef来旋转任何东西。当调用glRotate时,修改转换矩阵,对象数据保持不变。当渲染对象时,顶点数据通过顶点处理管道,乘以转换矩阵(每次渲染对象),在技术上,你不能得到计算结果 - 因为对象没有改变。



好吧,如果你真的想要它,你可以尝试使用gl反馈缓冲区(见glFeedbackBuffer),但我没有看到一个原因。我认为OpenGL红皮书可能有反馈缓冲区示例,但我不确定。反馈缓冲区可以提供你需要的功能,但我真的没有使用它们,所以我也不确定。



此外,移动大量数据并在系统内存和视频内存之间转发(当您的对象存储在视频内存中)会导致性能下降,因此即使可以,也不应该这样做。



如果要在旋转后获取对象状态,则必须手动将其乘 - 使用对象的矩阵并将副本存储在系统内存中。因为OpenGL结合了对象矩阵和视图矩阵(当你调用glRotatef),那么你需要单独存储旋转矩阵。


解决它的方法?


也许只有当你使用2D,渲染一个三角形或两个。



通常当你做转换时,你建立组合的旋转/转换矩阵,然后乘以你需要的数据使用那个矩阵 - 这将更有效率,因为它允许组合多个转换为一体。如果使用非图形3D例程(碰撞检测),您可以在代码中进行矩阵乘法/向量转换。



AFAIK OpenGL不提供矩阵处理程序(类似于D3DX中的D3DXMatrixMultiply),但它们容易从头开始编写,并且可以从多个地方获取。你可以使用D3DX矩阵函数,如果你绝望(D3D矩阵有相同的内存布局)。此外,如果你认真使用3D,你必须最终学习矩阵。


I need to access the X,Y values of a vertex object after Ive rotated it (with glRotate3f). How is it possible? I need em to solve an object collision problem where collisions will be sending objects in an angle related direction based upon objects rotation degrees.

edit::

Ok, Ive come close to find a way. Lets assume X and Y and degrees 0, 90 , 180, 270. I set a point as the center of the object, lets assume X=30, Y=70 and the X_size is 60 and Y_size is 140.

If I make new_X = X*cos(angle) - Y*sen(angle) and new_Y = X*sen(angle) + Y*cos(angle) if then I add or subtract the object center X and Y to new_X and new_Y I actually get to the new point except when angle is 180 where I would need to go with -new_X + X.

Is this the way to solve it?

解决方案

You are misunderstanding things a bit.

You didn't rotate anything by calling glRotatef. When you call glRotate, you modify transformation matrix, the object data remains unchanged. When you render object, the vertex data goes through vertex processing pipeline, gets multiplied by transformation matrix (every time you render object) and technically, you can't get result of calculations back - because object wasn't changed.

Well, if you really want it, you could try using gl feedback buffers (see glFeedbackBuffer), but I don't see a reason for that. I think that "OpenGL red book" may have feedback buffer examples, but I'm not sure about that. Feedback buffers MAY provide functionality you need, but I really haven't used them much, so I'm not sure about it either.

Also, moving lots of data back and forward between system memory and video memory (when your object is stored in video memory) leads to performance losses, so you shouldn't do that even if you can.

If you want to get object state after rotation, you will have to multiply it by hand - using object's matrix and store a copy in system memory. Since OpenGL combines object matrix with view matrix (when you call glRotatef), then you'll need to store rotation matrix separately.

Is this the way to solve it?

Maybe only if you're working with 2D, and rendering one triangle or two.

Normally when you do transformations, you build combined rotation/translations matrix, then multiply data you need using that matrix - this will be more efficient, because it allows to combine multiple transformations into one. And if you work with non-graphical 3D routines (collision detection), you do matrix multiplications/vector transformation in your code, yourself.

AFAIK OpenGL doesn't provide matrix manipulation routines (similar to D3DXMatrixMultiply in D3DX), but they are easy to write from scratch, and can be taken from multiple places. And you can use D3DX matrix functions, if you're desperate (D3D matrices have same memory layout). Besides, if you're serious about working with 3D, you'll have to learn matrices eventually.

这篇关于在调用rotatef c ++之后获取对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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