Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

dof6.bi

Uploader:MitgliedThePuppetMaster
Datum/Zeit:31.10.2013 00:39:21

'##############################################################################################################################################
'6 Dimensions of Freedom for OGL
'##############################################################################################################################################
'Idea: 2013.10.25-22:19
'Author.: Martin Wiemann (freebasic@deltalabs.de)
'Licence: do what u want to do if u dont earn more many with this as me
'##############################################################################################################################################
'-How to use-
'1. add this .bi and call DOF6_Init() one time at start of your app
'2. use DOF6_AddAll to ad changeings in directions and position or the seperated functions
'3. add go OGL-Code:
'  GLMatrixMode(GL_MODELVIEW)
'  GLLoadIdentity()
'  DOF6_MultiOGLModelMatrix()
'  GLTranslateF(G_DOF6_Pos.X, G_DOF6_Pos.Y, G_DOF6_Pos.Z)
'  GLPushMatrix()
'    <your 3d code>
'  GLPopMatrix()
'4. hf while fly around!
'
'To set the current position: G_DOF6_Pos = <new position vector>
'To reset the direction: DOF6_ResetDirection



'##############################################################################################################################################
Type Vector
    X                           as Double
    Y                           as Double
    Z                           as Double
End Type



'##############################################################################################################################################
Const G_DEG2RAD                 as Double = 0.0174532925199
Const G_RAD2DEG                 as Double = 57.2957795131



'##############################################################################################################################################
Dim Shared G_DOF6_Pos           as Vector
Dim Shared G_DOF6_Rot           as Vector
Dim Shared G_DOF6_BaseX         as Vector
Dim Shared G_DOF6_BaseY         as Vector
Dim Shared G_DOF6_BaseZ         as Vector
Dim Shared G_DOF6_MovX          as Vector
Dim Shared G_DOF6_MovY          as Vector
Dim Shared G_DOF6_MovZ          as Vector



'##############################################################################################################################################
Function DOF6_Vector_Rotate(V_Vector as Vector, V_Direction as Vector, V_RAD as Double) as Vector
Dim TVec as Vector
Dim TCos as Double = Cos(V_RAD)
Dim TSin as Double = Sin(V_RAD)
Dim TM(1 to 3, 1 to 3) as Double
TM(1, 1) = (V_Direction.X * V_Direction.X) * (1 - TCos) + TCos
TM(1, 2) = (V_Direction.X * V_Direction.Y) * (1 - TCos) - (V_Direction.Z * TSin)
TM(1, 3) = (V_Direction.X * V_Direction.Z) * (1 - TCos) + (V_Direction.Y * TSin)
TM(2, 1) = (V_Direction.Y * V_Direction.X) * (1 - TCos) + (V_Direction.Z * TSin)
TM(2, 2) = (V_Direction.Y * V_Direction.Y) * (1 - TCos) + TCos
TM(2, 3) = (V_Direction.Y * V_Direction.Z) * (1 - TCos) - (V_Direction.X * TSin)
TM(3, 1) = (V_Direction.Z * V_Direction.X) * (1 - TCos) - (V_Direction.Y * TSin)
TM(3, 2) = (V_Direction.Z * V_Direction.Y) * (1 - TCos) + (V_Direction.X * TSin)
TM(3, 3) = (V_Direction.Z * V_Direction.Z) * (1 - TCos) + TCos
TVec.X = (V_Vector.X * TM(1, 1)) + (V_Vector.Y * TM(2, 1)) + (V_Vector.Z * TM(3, 1))
TVec.Y = (V_Vector.X * TM(1, 2)) + (V_Vector.Y * TM(2, 2)) + (V_Vector.Z * TM(3, 2))
TVec.Z = (V_Vector.X * TM(1, 3)) + (V_Vector.Y * TM(2, 3)) + (V_Vector.Z * TM(3, 3))
Return TVec
End Function

'----------------------------------------------------------------------------------------------------------------------------------------------
Sub DOF6_Vector_Normalize(RV_Vector as Vector)
With RV_Vector
    Dim TDist as Double = Sqr((.X * .X) + (.Y * .Y) + (.Z * .Z))
    .X /= TDist
    .Y /= TDist
    .Z /= TDist
End With
End Sub

'----------------------------------------------------------------------------------------------------------------------------------------------
Function DOF6_Vector_Dot(V_Vector1 as Vector, V_Vector2 as Vector) as Double
Dim TDot as Double
TDot += V_Vector1.X * V_Vector2.X
TDot += V_Vector1.X * V_Vector2.Y
TDot += V_Vector1.X * V_Vector2.Z
TDot += V_Vector1.Y * V_Vector2.X
TDot += V_Vector1.Y * V_Vector2.Y
TDot += V_Vector1.Y * V_Vector2.Z
TDot += V_Vector1.Z * V_Vector2.X
TDot += V_Vector1.Z * V_Vector2.Y
TDot += V_Vector1.Z * V_Vector2.Z
Return TDot
End Function

'----------------------------------------------------------------------------------------------------------------------------------------------
Function DOF6_Vector_Multi(V_Vector1 as Vector, V_Vector2 as Vector) as Vector
Dim TVec as Vector
TVec.X = V_Vector1.X * V_Vector2.X
TVec.Y = V_Vector1.Y * V_Vector2.Y
TVec.Z = V_Vector1.Z * V_Vector2.Z
Return TVec
End Function



'##############################################################################################################################################
Sub DOF6_MultiOGLModelMatrix()
Dim TRotX as Vector
Dim TRotY as Vector
Dim TRotZ as Vector
TRotX.X = G_DOF6_MovX.X
TRotX.Y = G_DOF6_MovY.X
TRotX.Z = G_DOF6_MovZ.X
TRotY.X = G_DOF6_MovX.Y
TRotY.Y = G_DOF6_MovY.Y
TRotY.Z = G_DOF6_MovZ.Y
TRotZ.X = G_DOF6_MovX.Z
TRotZ.Y = G_DOF6_MovY.Z
TRotZ.Z = G_DOF6_MovZ.Z
Dim TMat(0 to 15) as Double
TMat(0) =  TRotX.X
TMat(1) =  TRotX.Y
TMat(2) =  TRotX.Z
TMat(3) =  0.0
TMat(4) =  TRotY.X
TMat(5) =  TRotY.Y
TMat(6) =  TRotY.Z
TMat(7) =  0.0
TMat(8) =  TRotZ.X
TMat(9) =  TRotZ.Y
TMat(10) = TRotZ.Z
TMat(11) = 0.0
TMat(12) = 0.0
TMat(13) = 0.0
TMat(14) = 0.0
TMat(15) = 1.0
GLMultMatrixD(@TMat(0))
End Sub



'##############################################################################################################################################
Sub DOF6_RecalculateRot()
Dim TRotX as Vector
Dim TRotY as Vector
Dim TRotZ as Vector
TRotX.X = G_DOF6_MovX.X
TRotX.Y = G_DOF6_MovY.X
TRotX.Z = G_DOF6_MovZ.X
TRotY.X = G_DOF6_MovX.Y
TRotY.Y = G_DOF6_MovY.Y
TRotY.Z = G_DOF6_MovZ.Y
TRotZ.X = G_DOF6_MovX.Z
TRotZ.Y = G_DOF6_MovY.Z
TRotZ.Z = G_DOF6_MovZ.Z
DOF6_Vector_Normalize(TRotX)
DOF6_Vector_Normalize(TRotY)
DOF6_Vector_Normalize(TRotZ)
G_DOF6_Rot.X = -180 + DOF6_Vector_Dot(G_DOF6_BaseX, TRotX) * 180
G_DOF6_Rot.Y = -180 + DOF6_Vector_Dot(G_DOF6_BaseY, TRotY) * 180
G_DOF6_Rot.Z = -180 + DOF6_Vector_Dot(G_DOF6_BaseZ, TRotZ) * 180
End Sub



'##############################################################################################################################################
Sub DOF6_Init()
G_DOF6_BaseX.X = 1.0
G_DOF6_BaseY.Y = 1.0
G_DOF6_BaseZ.Z = 1.0
G_DOF6_MovX.X = 1.0
G_DOF6_MovY.Y = 1.0
G_DOF6_MovZ.Z = 1.0
DOF6_RecalculateRot()
End Sub



'##############################################################################################################################################
Sub DOF6_AddPitch(V_Value as Double)
Dim TValRAD as Double = V_Value * G_DEG2RAD
Dim TRotVec as Vector = G_DOF6_MovX
G_DOF6_MovX = DOF6_Vector_Rotate(G_DOF6_MovX, TRotVec, TValRAD)
G_DOF6_MovY = DOF6_Vector_Rotate(G_DOF6_MovY, TRotVec, TValRAD)
G_DOF6_MovZ = DOF6_Vector_Rotate(G_DOF6_MovZ, TRotVec, TValRAD)
DOF6_Vector_Normalize(G_DOF6_MovX)
DOF6_Vector_Normalize(G_DOF6_MovY)
DOF6_Vector_Normalize(G_DOF6_MovZ)
DOF6_RecalculateRot()
End Sub

'----------------------------------------------------------------------------------------------------------------------------------------------
Sub DOF6_AddYaw(V_Value as Double)
Dim TValRAD as Double = V_Value * G_DEG2RAD
Dim TRotVec as Vector = G_DOF6_MovY
G_DOF6_MovX = DOF6_Vector_Rotate(G_DOF6_MovX, TRotVec, TValRAD)
G_DOF6_MovY = DOF6_Vector_Rotate(G_DOF6_MovY, TRotVec, TValRAD)
G_DOF6_MovZ = DOF6_Vector_Rotate(G_DOF6_MovZ, TRotVec, TValRAD)
DOF6_Vector_Normalize(G_DOF6_MovX)
DOF6_Vector_Normalize(G_DOF6_MovY)
DOF6_Vector_Normalize(G_DOF6_MovZ)
DOF6_RecalculateRot()
End Sub

'----------------------------------------------------------------------------------------------------------------------------------------------
Sub DOF6_AddRoll(V_Value as Double)
Dim TValRAD as Double = V_Value * G_DEG2RAD
Dim TRotVec as Vector = G_DOF6_MovZ
G_DOF6_MovX = DOF6_Vector_Rotate(G_DOF6_MovX, TRotVec, TValRAD)
G_DOF6_MovY = DOF6_Vector_Rotate(G_DOF6_MovY, TRotVec, TValRAD)
G_DOF6_MovZ = DOF6_Vector_Rotate(G_DOF6_MovZ, TRotVec, TValRAD)
DOF6_Vector_Normalize(G_DOF6_MovX)
DOF6_Vector_Normalize(G_DOF6_MovY)
DOF6_Vector_Normalize(G_DOF6_MovZ)
DOF6_RecalculateRot()
End Sub

'----------------------------------------------------------------------------------------------------------------------------------------------
Sub DOF6_ResetDirection()
G_DOF6_MovX.X = 1.0
G_DOF6_MovY.Y = 1.0
G_DOF6_MovZ.Z = 1.0
DOF6_RecalculateRot()
End Sub



'##############################################################################################################################################
Sub DOF6_AddMoveX(V_Value as Double)
Dim TVal as Double = V_Value * -1
G_DOF6_Pos.X += G_DOF6_MovX.X * TVal
G_DOF6_Pos.Y += G_DOF6_MovX.Y * TVal
G_DOF6_Pos.Z += G_DOF6_MovX.Z * TVal
End Sub

'----------------------------------------------------------------------------------------------------------------------------------------------
Sub DOF6_AddMoveY(V_Value as Double)
Dim TVal as Double = V_Value * -1
G_DOF6_Pos.X += G_DOF6_MovY.X * TVal
G_DOF6_Pos.Y += G_DOF6_MovY.Y * TVal
G_DOF6_Pos.Z += G_DOF6_MovY.Z * TVal
End Sub

'----------------------------------------------------------------------------------------------------------------------------------------------
Sub DOF6_AddMoveZ(V_Value as Double)
Dim TVal as Double = V_Value * -1
G_DOF6_Pos.X += G_DOF6_MovZ.X * TVal
G_DOF6_Pos.Y += G_DOF6_MovZ.Y * TVal
G_DOF6_Pos.Z += G_DOF6_MovZ.Z * TVal
End Sub



'##############################################################################################################################################
Sub DOF6_AddAll(V_X as Double, V_Y as Double, V_Z as Double, V_Pitch as Double, V_Yaw as Double, V_Roll as Double)
If V_X      <> 0 Then DOF6_AddMoveX(V_X)
If V_Y      <> 0 Then DOF6_AddMoveY(V_Y)
If V_Z      <> 0 Then DOF6_AddMoveZ(V_Z)
If V_Pitch  <> 0 Then DOF6_AddPitch(V_Pitch)
If V_Yaw    <> 0 Then DOF6_AddYaw(V_Yaw)
If V_Roll   <> 0 Then DOF6_AddRoll(V_Roll)
End Sub