Getting values of an object after a call to rotatef c++

╄→гoц情女王★ 提交于 2019-12-11 06:30:36

问题


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 = Xcos(angle) - Ysen(angle) and new_Y = Xsen(angle) + Ycos(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?


回答1:


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.




回答2:


Not directly, you can retrieve the modelview matrix using float modelview[16]; glGetFloatv(GL_MODELVIEW, modelview) and do the matrix multiplication manually between your vectors and the modelview matrix.

There's no other method in GL to do it.




回答3:


It sounds like what you're after is gluProject. You supply x, y, and z coordinates of an object, current matrices (modelview and projection), and viewport coords, and it gives you back the x, y coordinates to which that point would be mapped by those matrices. The linked documentation includes the formulas for the job, if you prefer to do it yourself.



来源:https://stackoverflow.com/questions/2918008/getting-values-of-an-object-after-a-call-to-rotatef-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!