Search code examples
c++openglglrotate

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


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?


Solution

  • 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.