Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;=================================================
- ; S3d - Simple 3D Graphic Library
- ; v1.2.3 (16/AUG/2013)
- ;
- ; Author: Starg
- ;
- ; - Tested on: Windows XP SP3
- ; AutoIt v3.3.8.1, v3.3.9.17
- ; - Requires GDIPlus.au3
- ;
- ; * Use at your own risk!
- ;
- ; - Special Thanks: S.Programs
- ;=================================================
- #include-once
- #include <GDIPlus.au3>
- ;=================================================
- ; Constants
- ;=================================================
- Global Const $__S3d_PI = 3.14159265358979
- ;=================================================
- ; Global Variables
- ;=================================================
- Global $__S3d_hGraphic, $__S3d_iWidth, $__S3d_iHeight
- Global $__S3d_nFAngleTan, $__S3d_nFScale
- Global $__S3d_hPen, $__S3d_hBrush, $__S3d_hFont, $__S3d_hFormat
- Global $__S3d_aConvertMatrix = __S3d_CreateMatrix()
- Global $__S3d_aLocalMatrix = __S3d_CreateMatrix()
- Global $__S3d_aCameraMatrix = __S3d_CreateMatrix()
- Global $__S3d_aCameraPos[3]
- ; $__S3d_nPosBuf0X and $__S3d_nPosBuf0Y are used as "Current Position"
- Global $__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $__S3d_nPosBuf1X, $__S3d_nPosBuf1Y
- ;=================================================
- ; Functions
- ;=================================================
- ; _S3d_SelectGraphic -- select a graphic object to write to
- Func _S3d_SelectGraphic($hGraphic, $iWidth, $iHeight, $iSmooth = 2)
- $__S3d_hGraphic = $hGraphic
- $__S3d_iWidth = $iWidth
- $__S3d_iHeight = $iHeight
- _GDIPlus_GraphicsSetSmoothingMode($__S3d_hGraphic, $iSmooth) ; Thanks to UEZ
- EndFunc ;==>_S3d_SelectGraphic
- ; _S3d_SelectPen -- select a pen object
- Func _S3d_SelectPen($hPen)
- $__S3d_hPen = $hPen
- EndFunc ;==>_S3d_SelectPen
- ; _S3d_SelectBrush -- select a brush object
- Func _S3d_SelectBrush($hBrush)
- $__S3d_hBrush = $hBrush
- EndFunc ;==>_S3d_SelectBrush
- ; _S3d_SelectFont -- select a Font object
- Func _S3d_SelectFont($hFont)
- $__S3d_hFont = $hFont
- EndFunc ;==>_S3d_SelectFont
- ; _S3d_SelectFormat -- select a String Format object
- Func _S3d_SelectFormat($hFormat)
- $__S3d_hFormat = $hFormat
- EndFunc ;==>_S3d_SelectFormat
- ;=================================================
- ; _S3d_Dist -- returns the distance betwenn two points
- Func _S3d_Dist($nPos1X = 0, $nPos1Y = 0, $nPos1Z = 0, $nPos2X = 0, $nPos2Y = 0, $nPos2Z = 0)
- Return Sqrt(($nPos2X - $nPos1X) ^ 2 + ($nPos2Y - $nPos1Y) ^ 2 + ($nPos2Z - $nPos1Z) ^ 2)
- EndFunc ;==>_S3d_Dist
- Func _S3d_DistFromCamera($nPosX = 0, $nPosY = 0, $nPosZ = 0)
- Return Sqrt(($nPosX - $__S3d_aCameraPos[0]) ^ 2 + ($nPosY - $__S3d_aCameraPos[1]) ^ 2 + ($nPosZ - $__S3d_aCameraPos[2]) ^ 2)
- EndFunc ;==>_S3d_DistFromCamera
- ; _S3d_SetCamera -- sets camera and target coordinates
- Func _S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, $nTargetX, $nTargetY, $nTargetZ, $nVAngle = 0, $nFAngle = 0.8, $nFScale = 1000)
- Local $nDist = _S3d_Dist($nTargetX, $nTargetY, $nTargetZ, $nCameraX, $nCameraY, $nCameraZ)
- Local $nXYLen = _S3d_Dist($nTargetX, $nTargetY, 0, $nCameraX, $nCameraY, 0)
- __S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, _
- ($nTargetX - $nCameraX) / $nXYLen, ($nTargetY - $nCameraY) / $nXYLen, _
- $nXYLen / $nDist, ($nTargetZ - $nCameraZ) / $nDist, _
- $nVAngle, $nFAngle, $nFScale)
- EndFunc ;==>_S3d_SetCamera
- Func _S3d_SetCameraEx($nCameraX, $nCameraY, $nCameraZ, $nXYAngle = 0, $nXZAngle = 0, $nVAngle = 0, $nFAngle = 0.8, $nFScale = 1000)
- __S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, _
- Cos($nXYAngle), Sin($nXYAngle), _
- Cos($nXZAngle), Sin($nXZAngle), _
- $nVAngle, $nFAngle, $nFScale)
- EndFunc ;==>_S3d_SetCameraEx
- ; _S3d_SetLocalMatrix - local coordinates -> world coordinates
- Func _S3d_SetLocalMatrix( _
- $n00 = 1, $n01 = 0, $n02 = 0, $n03 = 0, _
- $n10 = 0, $n11 = 1, $n12 = 0, $n13 = 0, _
- $n20 = 0, $n21 = 0, $n22 = 1, $n23 = 0, _
- $n30 = 0, $n31 = 0, $n32 = 0, $n33 = 1)
- __S3d_SetMatrix($__S3d_aLocalMatrix, _
- $n00, $n01, $n02, $n03, _
- $n10, $n11, $n12, $n13, _
- $n20, $n21, $n22, $n23, _
- $n30, $n31, $n32, $n33)
- __S3d_MultiplyMatrix($__S3d_aConvertMatrix, $__S3d_aCameraMatrix, $__S3d_aLocalMatrix)
- EndFunc ;==>_S3d_SetLocalMatrix
- ; _S3d_MultiplyLocalMatrix
- Func _S3d_MultiplyLocalMatrix( _
- $n00 = 1, $n01 = 0, $n02 = 0, $n03 = 0, _
- $n10 = 0, $n11 = 1, $n12 = 0, $n13 = 0, _
- $n20 = 0, $n21 = 0, $n22 = 1, $n23 = 0, _
- $n30 = 0, $n31 = 0, $n32 = 0, $n33 = 1)
- Local $aMatrix = __S3d_CreateMatrix(), $aBufLocalMatrix = $__S3d_aLocalMatrix
- __S3d_SetMatrix($aMatrix, _
- $n00, $n01, $n02, $n03, _
- $n10, $n11, $n12, $n13, _
- $n20, $n21, $n22, $n23, _
- $n30, $n31, $n32, $n33)
- __S3d_MultiplyMatrix($__S3d_aLocalMatrix, $aMatrix, $aBufLocalMatrix)
- __S3d_MultiplyMatrix($__S3d_aConvertMatrix, $__S3d_aCameraMatrix, $__S3d_aLocalMatrix)
- EndFunc ;==>_S3d_MultiplyLocalMatrix
- Func _S3d_LocalTranslate($nX, $nY, $nZ)
- _S3d_MultiplyLocalMatrix( _
- 1, 0, 0, $nX, _
- 0, 1, 0, $nY, _
- 0, 0, 1, $nZ, _
- 0, 0, 0, 1)
- EndFunc ;==>_S3d_LocalTranslate
- Func _S3d_LocalScale($nX, $nY, $nZ)
- _S3d_MultiplyLocalMatrix( _
- $nX, 0, 0, 0, _
- 0, $nY, 0, 0, _
- 0, 0, $nZ, 0, _
- 0, 0, 0, 1)
- EndFunc ;==>_S3d_LocalScale
- ; $nAngle is radians if $fDeg is false, degrees if true.
- Func _S3d_LocalRotateX($nAngle, $fDeg = False)
- If $fDeg Then $nAngle = __S3d_Deg2Rad($nAngle)
- _S3d_MultiplyLocalMatrix( _
- 1, 0, 0, 0, _
- 0, Cos($nAngle), -Sin($nAngle), 0, _
- 0, Sin($nAngle), Cos($nAngle), 0, _
- 0, 0, 0, 1)
- EndFunc ;==>_S3d_LocalRotateX
- Func _S3d_LocalRotateY($nAngle, $fDeg = False)
- If $fDeg Then $nAngle = __S3d_Deg2Rad($nAngle)
- _S3d_MultiplyLocalMatrix( _
- Cos($nAngle), 0, -Sin($nAngle), 0, _
- 0, 1, 0, 0, _
- Sin($nAngle), 0, Cos($nAngle), 0, _
- 0, 0, 0, 1)
- EndFunc ;==>_S3d_LocalRotateY
- Func _S3d_LocalRotateZ($nAngle, $fDeg = False)
- If $fDeg Then $nAngle = __S3d_Deg2Rad($nAngle)
- _S3d_MultiplyLocalMatrix( _
- Cos($nAngle), -Sin($nAngle), 0, 0, _
- Sin($nAngle), Cos($nAngle), 0, 0, _
- 0, 0, 1, 0, _
- 0, 0, 0, 1)
- EndFunc ;==>_S3d_LocalRotateZ
- Func _S3d_GetLocalMatrix()
- Return $__S3d_aLocalMatrix
- EndFunc ;==>_S3d_GetLocalMatrix
- Func _S3d_SetLocalMatrixEx(ByRef $aMatrix)
- $__S3d_aLocalMatrix = $aMatrix
- __S3d_MultiplyMatrix($__S3d_aConvertMatrix, $__S3d_aCameraMatrix, $__S3d_aLocalMatrix)
- EndFunc ;==>_S3d_SetLocalMatrixEx
- ;=================================================
- ; _S3d_GetPos - converts 3D -> 2D
- Func _S3d_GetPos($nX, $nY, $nZ)
- Local $aPos[2]
- __S3d_ConvertPos($aPos[0], $aPos[1], $nX, $nY, $nZ)
- If @error Then Return SetError(1, 0, 0)
- Return $aPos
- EndFunc ;==>_S3d_GetPos
- Func _S3d_MoveTo($nX, $nY, $nZ = Default)
- If IsKeyword($nZ) Then
- $__S3d_nPosBuf0X = $nX
- $__S3d_nPosBuf0Y = $nY
- Else
- __S3d_ConvertPos($__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $nX, $nY, $nZ)
- If @error Then Return SetError(1, 0, 0)
- EndIf
- Return 1
- EndFunc ;==>_S3d_MoveTo
- Func _S3d_Clear($nColor = 0xFF000000)
- Return _GDIPlus_GraphicsClear($__S3d_hGraphic, $nColor)
- EndFunc ;==>_S3d_Clear
- Func _S3d_Line($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
- Local $nOutX1, $nOutY1, $nOutX2, $nOutY2, $iError1, $iError2
- __S3d_ConvertPos($nOutX1, $nOutY1, $nX1, $nY1, $nZ1)
- $iError1 = @error
- __S3d_ConvertPos($nOutX2, $nOutY2, $nX2, $nY2, $nZ2)
- $iError2 = @error
- If $iError1 And $iError2 Then
- Return SetError(1, 0, 0)
- ElseIf $iError1 Then
- __S3d_Clip($nOutX1, $nOutY1, $nX2, $nY2, $nZ2, $nX1, $nY1, $nZ1)
- ElseIf $iError2 Then
- __S3d_Clip($nOutX2, $nOutY2, $nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
- EndIf
- Return _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nOutX1, $nOutY1, $nOutX2, $nOutY2, $__S3d_hPen)
- EndFunc ;==>_S3d_Line
- Func _S3d_LineTo($nX, $nY, $nZ)
- Local $nOutX, $nOutY
- __S3d_ConvertPos($nOutX, $nOutY, $nX, $nY, $nZ)
- If @error Then Return SetError(1, 0, 0)
- _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $nOutX, $nOutY, $__S3d_hPen)
- $__S3d_nPosBuf0X = $nOutX
- $__S3d_nPosBuf0Y = $nOutY
- Return 1
- EndFunc ;==>_S3d_LineTo
- Func _S3d_Box($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
- _S3d_Line($nX1, $nY1, $nZ1, $nX2, $nY1, $nZ1)
- _S3d_Line($nX2, $nY1, $nZ1, $nX2, $nY2, $nZ1)
- _S3d_Line($nX2, $nY2, $nZ1, $nX1, $nY2, $nZ1)
- _S3d_Line($nX1, $nY2, $nZ1, $nX1, $nY1, $nZ1)
- _S3d_Line($nX1, $nY1, $nZ2, $nX2, $nY1, $nZ2)
- _S3d_Line($nX2, $nY1, $nZ2, $nX2, $nY2, $nZ2)
- _S3d_Line($nX2, $nY2, $nZ2, $nX1, $nY2, $nZ2)
- _S3d_Line($nX1, $nY2, $nZ2, $nX1, $nY1, $nZ2)
- _S3d_Line($nX1, $nY1, $nZ1, $nX1, $nY1, $nZ2)
- _S3d_Line($nX1, $nY2, $nZ1, $nX1, $nY2, $nZ2)
- _S3d_Line($nX2, $nY1, $nZ1, $nX2, $nY1, $nZ2)
- _S3d_Line($nX2, $nY2, $nZ1, $nX2, $nY2, $nZ2)
- EndFunc ;==>_S3d_Box
- Func _S3d_Arrow($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2, $nLen = 30, $nAngle = 0.6)
- If $nLen < 0 Then Return SetError(1, 0, 0)
- Local $nPos1X, $nPos1Y, $nPos2X, $nPos2Y, $iError1, $iError2
- __S3d_ConvertPos($nPos1X, $nPos1Y, $nX1, $nY1, $nZ1)
- $iError1 = @error
- __S3d_ConvertPos($nPos2X, $nPos2Y, $nX2, $nY2, $nZ2)
- $iError2 = @error
- If $iError1 And $iError2 Then
- Return SetError(1, 0, 0)
- ElseIf $iError1 Then
- __S3d_Clip($nPos1X, $nPos1Y, $nX2, $nY2, $nZ2, $nX1, $nY1, $nZ1)
- ElseIf $iError2 Then
- __S3d_Clip($nPos2X, $nPos2Y, $nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
- EndIf
- _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nPos1X, $nPos1Y, $nPos2X, $nPos2Y, $__S3d_hPen)
- If Not $iError2 Then
- Local $nDist = _S3d_Dist($nPos1X, $nPos1Y, 0, $nPos2X, $nPos2Y, 0)
- Local $nCenX = ($nPos1X - $nPos2X) / $nDist * $nLen
- Local $nCenY = ($nPos1Y - $nPos2Y) / $nDist * $nLen
- Local $nCos = Cos($nAngle), $nSin = Sin($nAngle)
- Local $aPosA1X = $nCenX * $nCos - $nCenY * $nSin + $nPos2X
- Local $aPosA1Y = $nCenX * $nSin + $nCenY * $nCos + $nPos2Y
- ; sin (-a) = - sin a
- ; cos (-a) = cos a
- Local $aPosA2X = $nCenX * $nCos + $nCenY * $nSin + $nPos2X
- Local $aPosA2Y = -$nCenX * $nSin + $nCenY * $nCos + $nPos2Y
- _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nPos2X, $nPos2Y, $aPosA1X, $aPosA1Y, $__S3d_hPen)
- _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nPos2X, $nPos2Y, $aPosA2X, $aPosA2Y, $__S3d_hPen)
- EndIf
- Return 1
- EndFunc ;==>_S3d_Arrow
- Func _S3d_Circle($nX, $nY, $nZ, $nRad, $fFill = False)
- If $nRad <= 0 Then Return SetError(1, 0, 0)
- Local $nPosX, $nPosY
- ; (_S3d_DistFromCamera($nX, $nY, $nZ) * $__S3d_nFAngleTan) : $nRad = $__S3d_nFScale : $nVRad
- Local $nVRad = $nRad * $__S3d_nFScale / _S3d_DistFromCamera($nX, $nY, $nZ) / $__S3d_nFAngleTan
- __S3d_ConvertPos($nPosX, $nPosY, $nX, $nY, $nZ)
- If @error Then Return SetError(2, 0, 0)
- If $fFill Then
- Return _GDIPlus_GraphicsFillEllipse($__S3d_hGraphic, $nPosX - $nVRad, $nPosY - $nVRad, $nVRad * 2, $nVRad * 2, $__S3d_hBrush)
- Else
- Return _GDIPlus_GraphicsDrawEllipse($__S3d_hGraphic, $nPosX - $nVRad, $nPosY - $nVRad, $nVRad * 2, $nVRad * 2, $__S3d_hPen)
- EndIf
- EndFunc ;==>_S3d_Circle
- ; $aPoints[0][0] : The number of points
- ; $aPoints[n][0] : nth X
- ; $aPoints[n][1] : nth Y
- ; $aPoints[n][2] : nth Z
- Func _S3d_Polygon($aPoints, $fFill = False)
- If Not IsArray($aPoints) Then Return SetError(1, 0, 0)
- Local $a2dPoints[$aPoints[0][0] + 1][2], $i
- $a2dPoints[0][0] = $aPoints[0][0]
- For $i = 1 To $aPoints[0][0]
- __S3d_ConvertPos($a2dPoints[$i][0], $a2dPoints[$i][1], $aPoints[$i][0], $aPoints[$i][1], $aPoints[$i][2])
- If @error Then Return SetError(2, $i, 0)
- Next
- If $fFill Then
- Return _GDIPlus_GraphicsFillPolygon($__S3d_hGraphic, $a2dPoints, $__S3d_hBrush)
- Else
- Return _GDIPlus_GraphicsDrawPolygon($__S3d_hGraphic, $a2dPoints, $__S3d_hPen)
- EndIf
- EndFunc ;==>_S3d_Polygon
- ; Always draw on XY plane
- Func _S3d_RegPolygon($nX, $nY, $nZ, $nRad, $iNum, $fFill = True)
- If $nRad <= 0 Then Return SetError(1, 0, 0)
- If (Not IsInt($iNum)) Or $iNum <= 0 Then Return SetError(2, 0, 0)
- Local $aPoints[$iNum + 1][3], $i, $nCurDir = 0
- $aPoints[0][0] = $iNum
- For $i = 1 To $iNum
- $aPoints[$i][0] = $nX + $nRad * Cos($nCurDir)
- $aPoints[$i][1] = $nY + $nRad * Sin($nCurDir)
- $aPoints[$i][2] = $nZ
- $nCurDir += $__S3d_PI * 2 / $iNum
- Next
- Return _S3d_Polygon($aPoints, $fFill)
- EndFunc ;==>_S3d_RegPolygon
- ; Always draw on XY plane
- Func _S3d_Star($nX, $nY, $nZ, $nRad1, $nRad2, $iNum, $fFill = True)
- If $nRad1 <= 0 Then Return SetError(1, 0, 0)
- If $nRad2 <= 0 Then Return SetError(2, 0, 0)
- If (Not IsInt($iNum)) Or $iNum <= 0 Then Return SetError(3, 0, 0)
- Local $aPoints[$iNum * 2 + 1][3], $i, $nCurDir = 0
- $aPoints[0][0] = $iNum * 2
- For $i = 1 To $iNum * 2 Step 2
- $aPoints[$i][0] = $nX + $nRad1 * Cos($nCurDir)
- $aPoints[$i][1] = $nY + $nRad1 * Sin($nCurDir)
- $aPoints[$i][2] = $nZ
- $nCurDir += $__S3d_PI / $iNum
- $aPoints[$i + 1][0] = $nX + $nRad2 * Cos($nCurDir)
- $aPoints[$i + 1][1] = $nY + $nRad2 * Sin($nCurDir)
- $aPoints[$i + 1][2] = $nZ
- $nCurDir += $__S3d_PI / $iNum
- Next
- Return _S3d_Polygon($aPoints, $fFill)
- EndFunc ;==>_S3d_Star
- Func _S3d_Square($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2, $nX3, $nY3, $nZ3, $nX4, $nY4, $nZ4, $fFill = True)
- If $fFill Then
- Local $a3dPos[4][3] = [[$nX1, $nY1, $nZ1], [$nX2, $nY2, $nZ2], [$nX3, $nY3, $nZ3], [$nX4, $nY4, $nZ4]]
- Local $a2dPos[5][2], $aError[4], $i
- $a2dPos[0][0] = 4
- For $i = 0 To 3
- __S3d_ConvertPos($a2dPos[$i + 1][0], $a2dPos[$i + 1][1], $a3dPos[$i][0], $a3dPos[$i][1], $a3dPos[$i][2])
- $aError[$i] = @error
- Next
- If $aError[0] And $aError[1] And $aError[2] And $aError[3] Then
- Return SetError(1, 0, 0)
- ElseIf $aError[0] Or $aError[1] Or $aError[2] Or $aError[3] Then
- Local $j, $k
- For $j = 0 To 3
- If Not $aError[$j] Then
- For $k = 0 To 3
- If $j <> $k And $aError[$k] Then
- __S3d_Clip($a2dPos[$k + 1][0], $a2dPos[$k + 1][1], _
- $a3dPos[$j][0], $a3dPos[$j][1], $a3dPos[$j][2], _
- $a3dPos[$k][0], $a3dPos[$k][1], $a3dPos[$k][2])
- EndIf
- Next
- ExitLoop
- EndIf
- Next
- EndIf
- Return _GDIPlus_GraphicsFillPolygon($__S3d_hGraphic, $a2dPos, $__S3d_hBrush)
- Else
- _S3d_Line($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
- _S3d_Line($nX2, $nY2, $nZ2, $nX3, $nY3, $nZ3)
- _S3d_Line($nX3, $nY3, $nZ3, $nX4, $nY4, $nZ4)
- _S3d_Line($nX4, $nY4, $nZ4, $nX1, $nY1, $nZ1)
- Return 1
- EndIf
- EndFunc ;==>_S3d_Square
- Func _S3d_MoveTo2($nXL, $nYL, $nZL, $nXR, $nYR, $nZR)
- __S3d_ConvertPos($__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $nXL, $nYL, $nZL)
- If @error Then Return SetError(1, 0, 0)
- __S3d_ConvertPos($__S3d_nPosBuf1X, $__S3d_nPosBuf1Y, $nXR, $nYR, $nZR)
- If @error Then Return SetError(2, 0, 0)
- Return 1
- EndFunc ;==>_S3d_MoveTo2
- Func _S3d_RibbonTo($nXL, $nYL, $nZL, $nXR, $nYR, $nZR)
- Local $aPos[5][2]
- $aPos[0][0] = 4
- $aPos[1][0] = $__S3d_nPosBuf0X
- $aPos[1][1] = $__S3d_nPosBuf0Y
- $aPos[2][0] = $__S3d_nPosBuf1X
- $aPos[2][1] = $__S3d_nPosBuf1Y
- __S3d_ConvertPos($aPos[4][0], $aPos[4][1], $nXL, $nYL, $nZL)
- If @error Then Return SetError(1, 0, 0)
- __S3d_ConvertPos($aPos[3][0], $aPos[3][1], $nXR, $nYR, $nZR)
- If @error Then Return SetError(2, 0, 0)
- _GDIPlus_GraphicsFillPolygon($__S3d_hGraphic, $aPos, $__S3d_hBrush)
- $__S3d_nPosBuf0X = $aPos[4][0]
- $__S3d_nPosBuf0Y = $aPos[4][1]
- $__S3d_nPosBuf1X = $aPos[3][0]
- $__S3d_nPosBuf1Y = $aPos[3][1]
- Return 1
- EndFunc ;==>_S3d_RibbonTo
- Func _S3d_String($sString, $nX, $nY, $nZ)
- Local $nPosX, $nPosY
- __S3d_ConvertPos($nPosX, $nPosY, $nX, $nY, $nZ)
- If @error Then Return SetError(1, 0, 0)
- Local $tLayout = _GDIPlus_RectFCreate($nPosX, $nPosY, 0, 0)
- Local $aInfo = _GDIPlus_GraphicsMeasureString($__S3d_hGraphic, $sString, $__S3d_hFont, $tLayout, $__S3d_hFormat)
- Return _GDIPlus_GraphicsDrawStringEx($__S3d_hGraphic, $sString, $__S3d_hFont, $aInfo[0], $__S3d_hFormat, $__S3d_hBrush)
- EndFunc ;==>_S3d_String
- ;=================================================
- ; Functions (Internal use only)
- ;=================================================
- Func __S3d_CreateMatrix()
- Local $aMatrix[4][4], $i, $j
- For $i = 0 To 3
- For $j = 0 To 3
- $aMatrix[$i][$j] = 0
- Next
- Next
- Return $aMatrix
- EndFunc ;==>__S3d_CreateMatrix
- Func __S3d_SetMatrix(ByRef $aMatrix, _
- $n00 = 1, $n01 = 0, $n02 = 0, $n03 = 0, _
- $n10 = 0, $n11 = 1, $n12 = 0, $n13 = 0, _
- $n20 = 0, $n21 = 0, $n22 = 1, $n23 = 0, _
- $n30 = 0, $n31 = 0, $n32 = 0, $n33 = 1)
- $aMatrix[0][0] = $n00
- $aMatrix[0][1] = $n01
- $aMatrix[0][2] = $n02
- $aMatrix[0][3] = $n03
- $aMatrix[1][0] = $n10
- $aMatrix[1][1] = $n11
- $aMatrix[1][2] = $n12
- $aMatrix[1][3] = $n13
- $aMatrix[2][0] = $n20
- $aMatrix[2][1] = $n21
- $aMatrix[2][2] = $n22
- $aMatrix[2][3] = $n23
- $aMatrix[3][0] = $n30
- $aMatrix[3][1] = $n31
- $aMatrix[3][2] = $n32
- $aMatrix[3][3] = $n33
- EndFunc ;==>__S3d_SetMatrix
- ; $aOutMatrix = $nInMatrix1 * $nInMatrix2
- Func __S3d_MultiplyMatrix(ByRef $aOutMatrix, ByRef $aInMatrix1, ByRef $aInMatrix2)
- Local $i, $j, $k
- For $i = 0 To 3
- For $j = 0 To 3
- $aOutMatrix[$i][$j] = 0
- For $k = 0 To 3
- $aOutMatrix[$i][$j] += $aInMatrix1[$i][$k] * $aInMatrix2[$k][$j]
- Next
- Next
- Next
- EndFunc ;==>__S3d_MultiplyMatrix
- Func __S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, $nXYCos, $nXYSin, $nXZCos, $nXZSin, $nVAngle, $nFAngle, $nFScale)
- Local $aMxXY = __S3d_CreateMatrix(), $aMxXZ = __S3d_CreateMatrix(), $aMxV = __S3d_CreateMatrix()
- Local $aTransMatrix = __S3d_CreateMatrix()
- Local $aTempMatrix = __S3d_CreateMatrix()
- __S3d_SetMatrix($aTransMatrix, _
- 1, 0, 0, -$nCameraX, _
- 0, 1, 0, -$nCameraY, _
- 0, 0, 1, -$nCameraZ, _
- 0, 0, 0, 1)
- __S3d_SetMatrix($aMxXY, _
- $nXYCos, $nXYSin, 0, 0, _
- -$nXYSin, $nXYCos, 0, 0, _
- 0, 0, 1, 0, _
- 0, 0, 0, 1)
- __S3d_SetMatrix($aMxXZ, _
- $nXZCos, 0, $nXZSin, 0, _
- 0, 1, 0, 0, _
- -$nXZSin, 0, $nXZCos, 0, _
- 0, 0, 0, 1)
- __S3d_SetMatrix($aMxV, _
- 1, 0, 0, 0, _
- 0, Cos(-$nVAngle), -Sin(-$nVAngle), 0, _
- 0, Sin(-$nVAngle), Cos(-$nVAngle), 0, _
- 0, 0, 0, 1)
- __S3d_MultiplyMatrix($__S3d_aCameraMatrix, $aMxXY, $aTransMatrix)
- __S3d_MultiplyMatrix($aTempMatrix, $aMxXZ, $__S3d_aCameraMatrix)
- __S3d_MultiplyMatrix($__S3d_aCameraMatrix, $aMxV, $aTempMatrix)
- _S3d_SetLocalMatrix()
- $__S3d_nFAngleTan = Tan($nFAngle)
- $__S3d_nFScale = $nFScale
- $__S3d_aCameraPos[0] = $nCameraX
- $__S3d_aCameraPos[1] = $nCameraY
- $__S3d_aCameraPos[2] = $nCameraZ
- EndFunc ;==>__S3d_SetCamera
- ; __S3d_ConvertPos - converts 3D -> 2D
- Func __S3d_ConvertPos(ByRef $nOutX, ByRef $nOutY, $nInX, $nInY, $nInZ)
- Local $aInPos[4] = [$nInX, $nInY, $nInZ ,1], $aOutPos[3] = [0, 0, 0], $i, $j
- For $i = 0 To 2
- For $j = 0 To 3
- $aOutPos[$i] += $__S3d_aConvertMatrix[$i][$j] * $aInPos[$j]
- Next
- Next
- If $aOutPos[0] <= 0 Then Return SetError(1, 0, 0) ; behind the camera
- $nOutX = (- $aOutPos[1] / $aOutPos[0] / $__S3d_nFAngleTan) * ($__S3d_nFScale / 2) + ($__S3d_iWidth / 2)
- $nOutY = (- $aOutPos[2] / $aOutPos[0] / $__S3d_nFAngleTan) * ($__S3d_nFScale / 2) + ($__S3d_iHeight / 2)
- Return 1
- EndFunc ;==>__S3d_ConvertPos
- Func __S3d_Clip(ByRef $nOutX, ByRef $nOutY, $nOkX, $nOkY, $nOkZ, $nFailX, $nFailY, $nFailZ)
- Local $nX1 = $nOkX, $nY1 = $nOkY, $nZ1 = $nOkZ, $nX2 = $nFailX, $nY2 = $nFailY, $nZ2 = $nFailZ
- Local $nMidX, $nMidY, $nMidZ, $fSuccess = False, $i = 1
- Do
- $nMidX = ($nX1 + $nX2) / 2
- $nMidY = ($nY1 + $nY2) / 2
- $nMidZ = ($nZ1 + $nZ2) / 2
- __S3d_ConvertPos($nOutX, $nOutY, $nMidX, $nMidY, $nMidZ)
- If @error Then
- $nX2 = $nMidX
- $nY2 = $nMidY
- $nZ2 = $nMidZ
- Else
- $nX1 = $nMidX
- $nY1 = $nMidY
- $nZ1 = $nMidZ
- $fSuccess = True
- EndIf
- $i += 1
- Until $i > 10
- If Not $fSuccess Then __S3d_ConvertPos($nOutX, $nOutY, $nOkX, $nOkY, $nOkZ)
- EndFunc ;==>__S3d_Clip
- Func __S3d_Deg2Rad($nDeg)
- Return $nDeg / 180 * $__S3d_PI
- EndFunc ;==>__S3d_Deg2Rad
Advertisement
Add Comment
Please, Sign In to add comment