Rotate + OpenGL

Advanced game related topics
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Rotate + OpenGL

Post by Polo »

Hello !
There's something on OpenGL (well that's not really on opengl here the problem is ...) i can't do : i want to rotate an object, without using the rotate command, just by changing the vertex value (the value is stored in an array or something like that, that's also not the problem :)), so in fact i just want to rotate the vertex stored in an array :) The problem is that i really don't know how to do this :D
legider_pb
User
User
Posts: 39
Joined: Tue Nov 25, 2003 12:28 pm

Post by legider_pb »

This is fairly simple compared to some other maths involved in 3D engines.

I approached this in my engine by using a form of Yaw/Pitch/Roll, but I sort of skewed it a tad to fit properly.

Lets create a function called RotateVector for sake of ease and fill it with pseudo-code.

Code: Select all

Function RotateVector( CurrentVertex.VECTOR, CurrentAxis.VECTOR, Yaw, Pitch, Roll )
    Yaw = Yaw / 57.2957795
    Pitch = Pitch / 57.2957795
    Roll = Roll / 57.2957795

    CurrentVertex = CurrentVertex - CurrentAxis

    If Yaw <> 0
        X.f = Cos(Yaw) * CurrentVertex\X - Sin(Yaw) * CurrentVertex\Z
        CurrentVertex\Z = Sin(Yaw) * CurrentVertex\X + Cos(Yaw) * CurrentVertex\Z
        CurrentVertex\X = X
    EndIf

    If Pitch <> 0
        Y.f = Cos(Pitch) * CurrentVertex\Y - Sin(Pitch) * CurrentVertex\Z
        CurrentVertex\Z = Sin(Pitch) * CurrentVertex\Y + Cos(Pitch) * CurrentVertex\Z
        CurrentVertex\Y = Y
    EndIf

    If Roll <> 0
        X.f = Cos(Roll) * CurrentVertex\X - Sin(Roll) * CurrentVertex\Y
        CurrentVertex\Y = Sin(Roll) * CurrentVertex\X + Cos(Roll) * CurrentVertex\Y
        CurrentVertex\X = X
    EndIf

    CurrentVertex = CurrentVertex + CurrentAxis

    Return CurrentVertex
EndFunction
This is pseudo-code, make sure to note that ;)

CurrentVertex originaly holds the position of the vertex you want to rotate around the point known as CurrentAxis by the Yaw/Pitch/Roll values. You would call this function for each vertex you want to rotate around your axis. I will post some more in depth stuff if you need once I get home, currently sitting in class ignoring my professor :).
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

lol, legider :lol:
thanks for this code, i'll try it tomorrow, i'll tell you if it has worked :)
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

@legider_pb a little off topic but, to add a little speed up in the long run you should be passing your vectors by reference not value. By passing them by value the function has to pass the whole structure (12 bytes * 2 vectors). You could minimize it by using a pointer to the vector, which is only 4 bytes( 4 bytes * 2 vectors). This should speed things up.

I also wonder if you could loose a little precision on the contant your using (57.2957795) and instead of dividing use a multiply :

instead of (YourNumber / 57.2957795) do:

(YourNumber * 0.0174532)

The processor may like a multiplication better than a divide (faster). You might loose a little precision, but your not working in microns either.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
legider_pb
User
User
Posts: 39
Joined: Tue Nov 25, 2003 12:28 pm

Post by legider_pb »

Thats pseudo code, CM =), i do use by reference with pointers (i love pointers, if you saw my code you would see that), but I was trying to explain the method as simple as possible, because i remember before i understood pointers I was extremely confused by them and they caused me alot of trouble. So please take note everyone, the method I have shown above should not be taken to heart and it can be made faster by use of pointers/pass-by-reference.

I may try that with the precision though, at first I was working with documents that were for highly detailed math sequences so I just followed along. Changing it to multiplication would speed it up since division adds a whole other process to complete.
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Thats pseudo code, CM =)
I didnt't notice the pseudo-code remark before your code .. sorry legider, no pun intended.
but I was trying to explain the method as simple as possible, because i remember before i understood pointers I was extremely confused by them and they caused me alot of trouble.
Your totaly right. Pointers are the root of all evil , i love them but i would never wish them upon anybody because of pure confusion. [/quote]
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Moonshine
Enthusiast
Enthusiast
Posts: 263
Joined: Tue May 25, 2004 12:13 am
Location: UK

Post by Moonshine »

I cant really understand whats so intimidating about pointers - my first introduction to pointers and direct memory access was with PB, and although a little daunting at first, I took to it like that <clicks fingers>.

Mind you, I suppose the masses of research into DirectX had something to do with it... :)
Mark my words, when you least expect it, your uppance will come...
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

legider -> I've just tested your code, it works perfectly well, thanks a lot !
legider_pb
User
User
Posts: 39
Joined: Tue Nov 25, 2003 12:28 pm

Post by legider_pb »

Awesome, glad it worked for you.
Post Reply