Page 1 of 1

Rotate + OpenGL

Posted: Mon Aug 30, 2004 6:00 pm
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

Posted: Mon Aug 30, 2004 6:44 pm
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 :).

Posted: Mon Aug 30, 2004 7:28 pm
by Polo
lol, legider :lol:
thanks for this code, i'll try it tomorrow, i'll tell you if it has worked :)

Posted: Mon Aug 30, 2004 7:58 pm
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.

Posted: Mon Aug 30, 2004 8:33 pm
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.

Posted: Mon Aug 30, 2004 10:29 pm
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]

Posted: Mon Aug 30, 2004 11:23 pm
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... :)

Posted: Tue Aug 31, 2004 4:45 pm
by Polo
legider -> I've just tested your code, it works perfectly well, thanks a lot !

Posted: Tue Aug 31, 2004 7:50 pm
by legider_pb
Awesome, glad it worked for you.