Starg

S3d.au3

Aug 5th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 20.25 KB | None | 0 0
  1. ;=================================================
  2. ; S3d - Simple 3D Graphic Library
  3. ; v1.2.3 (16/AUG/2013)
  4. ;
  5. ; Author: Starg
  6. ;
  7. ; - Tested on: Windows XP SP3
  8. ;              AutoIt v3.3.8.1, v3.3.9.17
  9. ; - Requires GDIPlus.au3
  10. ;
  11. ; * Use at your own risk!
  12. ;
  13. ; - Special Thanks: S.Programs
  14. ;=================================================
  15.  
  16. #include-once
  17. #include <GDIPlus.au3>
  18.  
  19. ;=================================================
  20. ; Constants
  21. ;=================================================
  22.  
  23. Global Const $__S3d_PI = 3.14159265358979
  24.  
  25. ;=================================================
  26. ; Global Variables
  27. ;=================================================
  28.  
  29. Global $__S3d_hGraphic, $__S3d_iWidth, $__S3d_iHeight
  30. Global $__S3d_nFAngleTan, $__S3d_nFScale
  31.  
  32. Global $__S3d_hPen, $__S3d_hBrush, $__S3d_hFont, $__S3d_hFormat
  33.  
  34. Global $__S3d_aConvertMatrix = __S3d_CreateMatrix()
  35. Global $__S3d_aLocalMatrix = __S3d_CreateMatrix()
  36. Global $__S3d_aCameraMatrix = __S3d_CreateMatrix()
  37.  
  38. Global $__S3d_aCameraPos[3]
  39.  
  40. ; $__S3d_nPosBuf0X and $__S3d_nPosBuf0Y are used as "Current Position"
  41. Global $__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $__S3d_nPosBuf1X, $__S3d_nPosBuf1Y
  42.  
  43.  
  44. ;=================================================
  45. ; Functions
  46. ;=================================================
  47. ; _S3d_SelectGraphic -- select a graphic object to write to
  48. Func _S3d_SelectGraphic($hGraphic, $iWidth, $iHeight, $iSmooth = 2)
  49.     $__S3d_hGraphic = $hGraphic
  50.     $__S3d_iWidth = $iWidth
  51.     $__S3d_iHeight = $iHeight
  52.    
  53.     _GDIPlus_GraphicsSetSmoothingMode($__S3d_hGraphic, $iSmooth)    ; Thanks to UEZ
  54. EndFunc ;==>_S3d_SelectGraphic
  55.  
  56. ; _S3d_SelectPen -- select a pen object
  57. Func _S3d_SelectPen($hPen)
  58.     $__S3d_hPen = $hPen
  59. EndFunc ;==>_S3d_SelectPen
  60.  
  61. ; _S3d_SelectBrush -- select a brush object
  62. Func _S3d_SelectBrush($hBrush)
  63.     $__S3d_hBrush = $hBrush
  64. EndFunc ;==>_S3d_SelectBrush
  65.  
  66. ; _S3d_SelectFont -- select a Font object
  67. Func _S3d_SelectFont($hFont)
  68.     $__S3d_hFont = $hFont
  69. EndFunc ;==>_S3d_SelectFont
  70.  
  71. ; _S3d_SelectFormat -- select a String Format object
  72. Func _S3d_SelectFormat($hFormat)
  73.     $__S3d_hFormat = $hFormat
  74. EndFunc ;==>_S3d_SelectFormat
  75.  
  76. ;=================================================
  77.  
  78. ; _S3d_Dist -- returns the distance betwenn two points
  79. Func _S3d_Dist($nPos1X = 0, $nPos1Y = 0, $nPos1Z = 0, $nPos2X = 0, $nPos2Y = 0, $nPos2Z = 0)
  80.     Return Sqrt(($nPos2X - $nPos1X) ^ 2 + ($nPos2Y - $nPos1Y) ^ 2 + ($nPos2Z - $nPos1Z) ^ 2)
  81. EndFunc ;==>_S3d_Dist
  82.  
  83. Func _S3d_DistFromCamera($nPosX = 0, $nPosY = 0, $nPosZ = 0)
  84.     Return Sqrt(($nPosX - $__S3d_aCameraPos[0]) ^ 2 + ($nPosY - $__S3d_aCameraPos[1]) ^ 2 + ($nPosZ - $__S3d_aCameraPos[2]) ^ 2)
  85. EndFunc ;==>_S3d_DistFromCamera
  86.  
  87.  
  88. ; _S3d_SetCamera -- sets camera and target coordinates
  89. Func _S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, $nTargetX, $nTargetY, $nTargetZ, $nVAngle = 0, $nFAngle = 0.8, $nFScale = 1000)
  90.     Local $nDist = _S3d_Dist($nTargetX, $nTargetY, $nTargetZ, $nCameraX, $nCameraY, $nCameraZ)
  91.     Local $nXYLen = _S3d_Dist($nTargetX, $nTargetY,         0, $nCameraX, $nCameraY,         0)
  92.    
  93.     __S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, _
  94.         ($nTargetX - $nCameraX) / $nXYLen, ($nTargetY - $nCameraY) / $nXYLen, _
  95.         $nXYLen / $nDist, ($nTargetZ - $nCameraZ) / $nDist, _
  96.         $nVAngle, $nFAngle, $nFScale)
  97.    
  98. EndFunc ;==>_S3d_SetCamera
  99.  
  100. Func _S3d_SetCameraEx($nCameraX, $nCameraY, $nCameraZ, $nXYAngle = 0, $nXZAngle = 0, $nVAngle = 0, $nFAngle = 0.8, $nFScale = 1000)
  101.     __S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, _
  102.         Cos($nXYAngle), Sin($nXYAngle), _
  103.         Cos($nXZAngle), Sin($nXZAngle), _
  104.         $nVAngle, $nFAngle, $nFScale)
  105. EndFunc ;==>_S3d_SetCameraEx
  106.  
  107. ; _S3d_SetLocalMatrix - local coordinates -> world coordinates
  108. Func _S3d_SetLocalMatrix( _
  109.     $n00 = 1, $n01 = 0, $n02 = 0, $n03 = 0, _
  110.     $n10 = 0, $n11 = 1, $n12 = 0, $n13 = 0, _
  111.     $n20 = 0, $n21 = 0, $n22 = 1, $n23 = 0, _
  112.     $n30 = 0, $n31 = 0, $n32 = 0, $n33 = 1)
  113.    
  114.     __S3d_SetMatrix($__S3d_aLocalMatrix, _
  115.         $n00, $n01, $n02, $n03, _
  116.         $n10, $n11, $n12, $n13, _
  117.         $n20, $n21, $n22, $n23, _
  118.         $n30, $n31, $n32, $n33)
  119.    
  120.     __S3d_MultiplyMatrix($__S3d_aConvertMatrix, $__S3d_aCameraMatrix, $__S3d_aLocalMatrix)
  121.    
  122. EndFunc ;==>_S3d_SetLocalMatrix
  123.  
  124. ; _S3d_MultiplyLocalMatrix
  125. Func _S3d_MultiplyLocalMatrix( _
  126.     $n00 = 1, $n01 = 0, $n02 = 0, $n03 = 0, _
  127.     $n10 = 0, $n11 = 1, $n12 = 0, $n13 = 0, _
  128.     $n20 = 0, $n21 = 0, $n22 = 1, $n23 = 0, _
  129.     $n30 = 0, $n31 = 0, $n32 = 0, $n33 = 1)
  130.    
  131.     Local $aMatrix = __S3d_CreateMatrix(), $aBufLocalMatrix = $__S3d_aLocalMatrix
  132.    
  133.     __S3d_SetMatrix($aMatrix, _
  134.         $n00, $n01, $n02, $n03, _
  135.         $n10, $n11, $n12, $n13, _
  136.         $n20, $n21, $n22, $n23, _
  137.         $n30, $n31, $n32, $n33)
  138.    
  139.     __S3d_MultiplyMatrix($__S3d_aLocalMatrix, $aMatrix, $aBufLocalMatrix)
  140.     __S3d_MultiplyMatrix($__S3d_aConvertMatrix, $__S3d_aCameraMatrix, $__S3d_aLocalMatrix)
  141.    
  142. EndFunc ;==>_S3d_MultiplyLocalMatrix
  143.  
  144.  
  145. Func _S3d_LocalTranslate($nX, $nY, $nZ)
  146.     _S3d_MultiplyLocalMatrix( _
  147.         1, 0, 0, $nX, _
  148.         0, 1, 0, $nY, _
  149.         0, 0, 1, $nZ, _
  150.         0, 0, 0,   1)
  151.    
  152. EndFunc ;==>_S3d_LocalTranslate
  153.  
  154. Func _S3d_LocalScale($nX, $nY, $nZ)
  155.     _S3d_MultiplyLocalMatrix( _
  156.         $nX,   0,   0,   0, _
  157.           0, $nY,   0,   0, _
  158.           0,   0, $nZ,   0, _
  159.           0,   0,   0,   1)
  160.    
  161. EndFunc ;==>_S3d_LocalScale
  162.  
  163. ; $nAngle is radians if $fDeg is false, degrees if true.
  164. Func _S3d_LocalRotateX($nAngle, $fDeg = False)
  165.     If $fDeg Then $nAngle = __S3d_Deg2Rad($nAngle)
  166.     _S3d_MultiplyLocalMatrix( _
  167.         1,            0,             0,            0, _
  168.         0, Cos($nAngle), -Sin($nAngle),            0, _
  169.         0, Sin($nAngle),  Cos($nAngle),            0, _
  170.         0,            0,             0,            1)
  171.    
  172. EndFunc ;==>_S3d_LocalRotateX
  173.  
  174. Func _S3d_LocalRotateY($nAngle, $fDeg = False)
  175.     If $fDeg Then $nAngle = __S3d_Deg2Rad($nAngle)
  176.     _S3d_MultiplyLocalMatrix( _
  177.         Cos($nAngle),            0, -Sin($nAngle),            0, _
  178.                    0,            1,             0,            0, _
  179.         Sin($nAngle),            0,  Cos($nAngle),            0, _
  180.                    0,            0,             0,            1)
  181. EndFunc ;==>_S3d_LocalRotateY
  182.  
  183. Func _S3d_LocalRotateZ($nAngle, $fDeg = False)
  184.     If $fDeg Then $nAngle = __S3d_Deg2Rad($nAngle)
  185.     _S3d_MultiplyLocalMatrix( _
  186.         Cos($nAngle), -Sin($nAngle),           0,            0, _
  187.         Sin($nAngle),  Cos($nAngle),           0,            0, _
  188.                    0,             0,           1,            0, _
  189.                    0,             0,           0,            1)
  190. EndFunc ;==>_S3d_LocalRotateZ
  191.  
  192.  
  193. Func _S3d_GetLocalMatrix()
  194.     Return $__S3d_aLocalMatrix
  195. EndFunc ;==>_S3d_GetLocalMatrix
  196.  
  197. Func _S3d_SetLocalMatrixEx(ByRef $aMatrix)
  198.     $__S3d_aLocalMatrix = $aMatrix
  199.     __S3d_MultiplyMatrix($__S3d_aConvertMatrix, $__S3d_aCameraMatrix, $__S3d_aLocalMatrix)
  200. EndFunc ;==>_S3d_SetLocalMatrixEx
  201.  
  202. ;=================================================
  203.  
  204. ; _S3d_GetPos - converts 3D -> 2D
  205. Func _S3d_GetPos($nX, $nY, $nZ)
  206.     Local $aPos[2]
  207.     __S3d_ConvertPos($aPos[0], $aPos[1], $nX, $nY, $nZ)
  208.     If @error Then Return SetError(1, 0, 0)
  209.     Return $aPos
  210. EndFunc ;==>_S3d_GetPos
  211.  
  212. Func _S3d_MoveTo($nX, $nY, $nZ = Default)
  213.     If IsKeyword($nZ) Then
  214.         $__S3d_nPosBuf0X = $nX
  215.         $__S3d_nPosBuf0Y = $nY
  216.     Else
  217.         __S3d_ConvertPos($__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $nX, $nY, $nZ)
  218.         If @error Then Return SetError(1, 0, 0)
  219.     EndIf
  220.     Return 1
  221. EndFunc ;==>_S3d_MoveTo
  222.  
  223. Func _S3d_Clear($nColor = 0xFF000000)
  224.     Return _GDIPlus_GraphicsClear($__S3d_hGraphic, $nColor)
  225. EndFunc ;==>_S3d_Clear
  226.  
  227. Func _S3d_Line($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
  228.     Local $nOutX1, $nOutY1, $nOutX2, $nOutY2, $iError1, $iError2
  229.    
  230.     __S3d_ConvertPos($nOutX1, $nOutY1, $nX1, $nY1, $nZ1)
  231.     $iError1 = @error
  232.     __S3d_ConvertPos($nOutX2, $nOutY2, $nX2, $nY2, $nZ2)
  233.     $iError2 = @error
  234.    
  235.     If $iError1 And $iError2 Then
  236.         Return SetError(1, 0, 0)
  237.     ElseIf $iError1 Then
  238.         __S3d_Clip($nOutX1, $nOutY1, $nX2, $nY2, $nZ2, $nX1, $nY1, $nZ1)
  239.     ElseIf $iError2 Then
  240.         __S3d_Clip($nOutX2, $nOutY2, $nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
  241.     EndIf
  242.    
  243.     Return _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nOutX1, $nOutY1, $nOutX2, $nOutY2, $__S3d_hPen)
  244. EndFunc ;==>_S3d_Line
  245.  
  246. Func _S3d_LineTo($nX, $nY, $nZ)
  247.     Local $nOutX, $nOutY
  248.     __S3d_ConvertPos($nOutX, $nOutY, $nX, $nY, $nZ)
  249.     If @error Then Return SetError(1, 0, 0)
  250.    
  251.     _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $nOutX, $nOutY, $__S3d_hPen)
  252.    
  253.     $__S3d_nPosBuf0X = $nOutX
  254.     $__S3d_nPosBuf0Y = $nOutY
  255.     Return 1
  256. EndFunc ;==>_S3d_LineTo
  257.  
  258. Func _S3d_Box($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
  259.    
  260.     _S3d_Line($nX1, $nY1, $nZ1, $nX2, $nY1, $nZ1)
  261.     _S3d_Line($nX2, $nY1, $nZ1, $nX2, $nY2, $nZ1)
  262.     _S3d_Line($nX2, $nY2, $nZ1, $nX1, $nY2, $nZ1)
  263.     _S3d_Line($nX1, $nY2, $nZ1, $nX1, $nY1, $nZ1)
  264.    
  265.     _S3d_Line($nX1, $nY1, $nZ2, $nX2, $nY1, $nZ2)
  266.     _S3d_Line($nX2, $nY1, $nZ2, $nX2, $nY2, $nZ2)
  267.     _S3d_Line($nX2, $nY2, $nZ2, $nX1, $nY2, $nZ2)
  268.     _S3d_Line($nX1, $nY2, $nZ2, $nX1, $nY1, $nZ2)
  269.    
  270.     _S3d_Line($nX1, $nY1, $nZ1, $nX1, $nY1, $nZ2)
  271.     _S3d_Line($nX1, $nY2, $nZ1, $nX1, $nY2, $nZ2)
  272.     _S3d_Line($nX2, $nY1, $nZ1, $nX2, $nY1, $nZ2)
  273.     _S3d_Line($nX2, $nY2, $nZ1, $nX2, $nY2, $nZ2)
  274.    
  275. EndFunc ;==>_S3d_Box
  276.  
  277. Func _S3d_Arrow($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2, $nLen = 30, $nAngle = 0.6)
  278.     If $nLen < 0 Then Return SetError(1, 0, 0)
  279.    
  280.     Local $nPos1X, $nPos1Y, $nPos2X, $nPos2Y, $iError1, $iError2
  281.     __S3d_ConvertPos($nPos1X, $nPos1Y, $nX1, $nY1, $nZ1)
  282.     $iError1 = @error
  283.     __S3d_ConvertPos($nPos2X, $nPos2Y, $nX2, $nY2, $nZ2)
  284.     $iError2 = @error
  285.    
  286.     If $iError1 And $iError2 Then
  287.         Return SetError(1, 0, 0)
  288.     ElseIf $iError1 Then
  289.         __S3d_Clip($nPos1X, $nPos1Y, $nX2, $nY2, $nZ2, $nX1, $nY1, $nZ1)
  290.     ElseIf $iError2 Then
  291.         __S3d_Clip($nPos2X, $nPos2Y, $nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
  292.     EndIf
  293.    
  294.     _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nPos1X, $nPos1Y, $nPos2X, $nPos2Y, $__S3d_hPen)
  295.    
  296.     If Not $iError2 Then
  297.         Local $nDist = _S3d_Dist($nPos1X, $nPos1Y, 0, $nPos2X, $nPos2Y, 0)
  298.        
  299.         Local $nCenX = ($nPos1X - $nPos2X) / $nDist * $nLen
  300.         Local $nCenY = ($nPos1Y - $nPos2Y) / $nDist * $nLen
  301.        
  302.         Local $nCos = Cos($nAngle), $nSin = Sin($nAngle)
  303.        
  304.         Local $aPosA1X = $nCenX * $nCos - $nCenY * $nSin + $nPos2X
  305.         Local $aPosA1Y = $nCenX * $nSin + $nCenY * $nCos + $nPos2Y
  306.         ; sin (-a) = - sin a
  307.         ; cos (-a) = cos a
  308.         Local $aPosA2X = $nCenX * $nCos + $nCenY * $nSin + $nPos2X
  309.         Local $aPosA2Y = -$nCenX * $nSin + $nCenY * $nCos + $nPos2Y
  310.        
  311.         _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nPos2X, $nPos2Y, $aPosA1X, $aPosA1Y, $__S3d_hPen)
  312.         _GDIPlus_GraphicsDrawLine($__S3d_hGraphic, $nPos2X, $nPos2Y, $aPosA2X, $aPosA2Y, $__S3d_hPen)
  313.     EndIf
  314.     Return 1
  315. EndFunc ;==>_S3d_Arrow
  316.  
  317. Func _S3d_Circle($nX, $nY, $nZ, $nRad, $fFill = False)
  318.    
  319.     If $nRad <= 0 Then Return SetError(1, 0, 0)
  320.     Local $nPosX, $nPosY
  321.     ; (_S3d_DistFromCamera($nX, $nY, $nZ) * $__S3d_nFAngleTan) : $nRad = $__S3d_nFScale : $nVRad
  322.     Local $nVRad = $nRad * $__S3d_nFScale / _S3d_DistFromCamera($nX, $nY, $nZ) / $__S3d_nFAngleTan
  323.     __S3d_ConvertPos($nPosX, $nPosY, $nX, $nY, $nZ)
  324.     If @error Then Return SetError(2, 0, 0)
  325.    
  326.     If $fFill Then
  327.         Return _GDIPlus_GraphicsFillEllipse($__S3d_hGraphic, $nPosX - $nVRad, $nPosY - $nVRad, $nVRad * 2, $nVRad * 2, $__S3d_hBrush)
  328.     Else
  329.         Return _GDIPlus_GraphicsDrawEllipse($__S3d_hGraphic, $nPosX - $nVRad, $nPosY - $nVRad, $nVRad * 2, $nVRad * 2, $__S3d_hPen)
  330.     EndIf
  331. EndFunc ;==>_S3d_Circle
  332.  
  333. ; $aPoints[0][0] : The number of points
  334. ; $aPoints[n][0] : nth X
  335. ; $aPoints[n][1] : nth Y
  336. ; $aPoints[n][2] : nth Z
  337. Func _S3d_Polygon($aPoints, $fFill = False)
  338.     If Not IsArray($aPoints) Then Return SetError(1, 0, 0)
  339.     Local $a2dPoints[$aPoints[0][0] + 1][2], $i
  340.     $a2dPoints[0][0] = $aPoints[0][0]
  341.    
  342.     For $i = 1 To $aPoints[0][0]
  343.         __S3d_ConvertPos($a2dPoints[$i][0], $a2dPoints[$i][1], $aPoints[$i][0], $aPoints[$i][1], $aPoints[$i][2])
  344.         If @error Then Return SetError(2, $i, 0)
  345.     Next
  346.    
  347.     If $fFill Then
  348.         Return _GDIPlus_GraphicsFillPolygon($__S3d_hGraphic, $a2dPoints, $__S3d_hBrush)
  349.     Else
  350.         Return _GDIPlus_GraphicsDrawPolygon($__S3d_hGraphic, $a2dPoints, $__S3d_hPen)
  351.     EndIf
  352. EndFunc ;==>_S3d_Polygon
  353.  
  354. ; Always draw on XY plane
  355. Func _S3d_RegPolygon($nX, $nY, $nZ, $nRad, $iNum, $fFill = True)
  356.     If $nRad <= 0 Then Return SetError(1, 0, 0)
  357.     If (Not IsInt($iNum)) Or $iNum <= 0 Then Return SetError(2, 0, 0)
  358.    
  359.     Local $aPoints[$iNum + 1][3], $i, $nCurDir = 0
  360.     $aPoints[0][0] = $iNum
  361.    
  362.     For $i = 1 To $iNum
  363.         $aPoints[$i][0] = $nX + $nRad * Cos($nCurDir)
  364.         $aPoints[$i][1] = $nY + $nRad * Sin($nCurDir)
  365.         $aPoints[$i][2] = $nZ
  366.         $nCurDir += $__S3d_PI * 2 / $iNum
  367.     Next
  368.    
  369.     Return _S3d_Polygon($aPoints, $fFill)
  370. EndFunc ;==>_S3d_RegPolygon
  371.  
  372. ; Always draw on XY plane
  373. Func _S3d_Star($nX, $nY, $nZ, $nRad1, $nRad2, $iNum, $fFill = True)
  374.     If $nRad1 <= 0 Then Return SetError(1, 0, 0)
  375.     If $nRad2 <= 0 Then Return SetError(2, 0, 0)
  376.     If (Not IsInt($iNum)) Or $iNum <= 0 Then Return SetError(3, 0, 0)
  377.    
  378.     Local $aPoints[$iNum * 2 + 1][3], $i, $nCurDir = 0
  379.     $aPoints[0][0] = $iNum * 2
  380.    
  381.     For $i = 1 To $iNum * 2 Step 2
  382.         $aPoints[$i][0] = $nX + $nRad1 * Cos($nCurDir)
  383.         $aPoints[$i][1] = $nY + $nRad1 * Sin($nCurDir)
  384.         $aPoints[$i][2] = $nZ
  385.         $nCurDir += $__S3d_PI  / $iNum
  386.        
  387.         $aPoints[$i + 1][0] = $nX + $nRad2 * Cos($nCurDir)
  388.         $aPoints[$i + 1][1] = $nY + $nRad2 * Sin($nCurDir)
  389.         $aPoints[$i + 1][2] = $nZ
  390.         $nCurDir += $__S3d_PI / $iNum
  391.     Next
  392.    
  393.     Return _S3d_Polygon($aPoints, $fFill)
  394. EndFunc ;==>_S3d_Star
  395.  
  396. Func _S3d_Square($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2, $nX3, $nY3, $nZ3, $nX4, $nY4, $nZ4, $fFill = True)
  397.    
  398.     If $fFill Then
  399.         Local $a3dPos[4][3] = [[$nX1, $nY1, $nZ1], [$nX2, $nY2, $nZ2], [$nX3, $nY3, $nZ3], [$nX4, $nY4, $nZ4]]
  400.         Local $a2dPos[5][2], $aError[4], $i
  401.         $a2dPos[0][0] = 4
  402.        
  403.         For $i = 0 To 3
  404.             __S3d_ConvertPos($a2dPos[$i + 1][0], $a2dPos[$i + 1][1], $a3dPos[$i][0], $a3dPos[$i][1], $a3dPos[$i][2])
  405.             $aError[$i] = @error
  406.         Next
  407.        
  408.         If $aError[0] And $aError[1] And $aError[2] And $aError[3] Then
  409.             Return SetError(1, 0, 0)
  410.         ElseIf $aError[0] Or $aError[1] Or $aError[2] Or $aError[3] Then
  411.             Local $j, $k
  412.             For $j = 0 To 3
  413.                 If Not $aError[$j] Then
  414.                     For $k = 0 To 3
  415.                         If $j <> $k And $aError[$k] Then
  416.                             __S3d_Clip($a2dPos[$k + 1][0], $a2dPos[$k + 1][1], _
  417.                                 $a3dPos[$j][0], $a3dPos[$j][1], $a3dPos[$j][2], _
  418.                                 $a3dPos[$k][0], $a3dPos[$k][1], $a3dPos[$k][2])
  419.                         EndIf
  420.                     Next
  421.                     ExitLoop
  422.                 EndIf
  423.             Next
  424.         EndIf
  425.        
  426.         Return _GDIPlus_GraphicsFillPolygon($__S3d_hGraphic, $a2dPos, $__S3d_hBrush)
  427.        
  428.     Else
  429.         _S3d_Line($nX1, $nY1, $nZ1, $nX2, $nY2, $nZ2)
  430.         _S3d_Line($nX2, $nY2, $nZ2, $nX3, $nY3, $nZ3)
  431.         _S3d_Line($nX3, $nY3, $nZ3, $nX4, $nY4, $nZ4)
  432.         _S3d_Line($nX4, $nY4, $nZ4, $nX1, $nY1, $nZ1)
  433.         Return 1
  434.     EndIf
  435. EndFunc ;==>_S3d_Square
  436.  
  437. Func _S3d_MoveTo2($nXL, $nYL, $nZL, $nXR, $nYR, $nZR)
  438.     __S3d_ConvertPos($__S3d_nPosBuf0X, $__S3d_nPosBuf0Y, $nXL, $nYL, $nZL)
  439.     If @error Then Return SetError(1, 0, 0)
  440.     __S3d_ConvertPos($__S3d_nPosBuf1X, $__S3d_nPosBuf1Y, $nXR, $nYR, $nZR)
  441.     If @error Then Return SetError(2, 0, 0)
  442.     Return 1
  443. EndFunc ;==>_S3d_MoveTo2
  444.  
  445. Func _S3d_RibbonTo($nXL, $nYL, $nZL, $nXR, $nYR, $nZR)
  446.     Local $aPos[5][2]
  447.     $aPos[0][0] = 4
  448.    
  449.     $aPos[1][0] = $__S3d_nPosBuf0X
  450.     $aPos[1][1] = $__S3d_nPosBuf0Y
  451.     $aPos[2][0] = $__S3d_nPosBuf1X
  452.     $aPos[2][1] = $__S3d_nPosBuf1Y
  453.    
  454.     __S3d_ConvertPos($aPos[4][0], $aPos[4][1], $nXL, $nYL, $nZL)
  455.     If @error Then Return SetError(1, 0, 0)
  456.     __S3d_ConvertPos($aPos[3][0], $aPos[3][1], $nXR, $nYR, $nZR)
  457.     If @error Then Return SetError(2, 0, 0)
  458.    
  459.     _GDIPlus_GraphicsFillPolygon($__S3d_hGraphic, $aPos, $__S3d_hBrush)
  460.    
  461.     $__S3d_nPosBuf0X = $aPos[4][0]
  462.     $__S3d_nPosBuf0Y = $aPos[4][1]
  463.     $__S3d_nPosBuf1X = $aPos[3][0]
  464.     $__S3d_nPosBuf1Y = $aPos[3][1]
  465.    
  466.     Return 1
  467. EndFunc ;==>_S3d_RibbonTo
  468.  
  469. Func _S3d_String($sString, $nX, $nY, $nZ)
  470.     Local $nPosX, $nPosY
  471.     __S3d_ConvertPos($nPosX, $nPosY, $nX, $nY, $nZ)
  472.     If @error Then Return SetError(1, 0, 0)
  473.    
  474.     Local $tLayout = _GDIPlus_RectFCreate($nPosX, $nPosY, 0, 0)
  475.     Local $aInfo = _GDIPlus_GraphicsMeasureString($__S3d_hGraphic, $sString, $__S3d_hFont, $tLayout, $__S3d_hFormat)
  476.     Return _GDIPlus_GraphicsDrawStringEx($__S3d_hGraphic, $sString, $__S3d_hFont, $aInfo[0], $__S3d_hFormat, $__S3d_hBrush)
  477. EndFunc ;==>_S3d_String
  478.  
  479. ;=================================================
  480. ; Functions (Internal use only)
  481. ;=================================================
  482.  
  483. Func __S3d_CreateMatrix()
  484.     Local $aMatrix[4][4], $i, $j
  485.    
  486.     For $i = 0 To 3
  487.         For $j = 0 To 3
  488.             $aMatrix[$i][$j] = 0
  489.         Next
  490.     Next
  491.    
  492.     Return $aMatrix
  493. EndFunc ;==>__S3d_CreateMatrix
  494.  
  495. Func __S3d_SetMatrix(ByRef $aMatrix, _
  496.     $n00 = 1, $n01 = 0, $n02 = 0, $n03 = 0, _
  497.     $n10 = 0, $n11 = 1, $n12 = 0, $n13 = 0, _
  498.     $n20 = 0, $n21 = 0, $n22 = 1, $n23 = 0, _
  499.     $n30 = 0, $n31 = 0, $n32 = 0, $n33 = 1)
  500.    
  501.     $aMatrix[0][0] = $n00
  502.     $aMatrix[0][1] = $n01
  503.     $aMatrix[0][2] = $n02
  504.     $aMatrix[0][3] = $n03
  505.    
  506.     $aMatrix[1][0] = $n10
  507.     $aMatrix[1][1] = $n11
  508.     $aMatrix[1][2] = $n12
  509.     $aMatrix[1][3] = $n13
  510.    
  511.     $aMatrix[2][0] = $n20
  512.     $aMatrix[2][1] = $n21
  513.     $aMatrix[2][2] = $n22
  514.     $aMatrix[2][3] = $n23
  515.    
  516.     $aMatrix[3][0] = $n30
  517.     $aMatrix[3][1] = $n31
  518.     $aMatrix[3][2] = $n32
  519.     $aMatrix[3][3] = $n33
  520.    
  521. EndFunc ;==>__S3d_SetMatrix
  522.  
  523. ; $aOutMatrix = $nInMatrix1 * $nInMatrix2
  524. Func __S3d_MultiplyMatrix(ByRef $aOutMatrix, ByRef $aInMatrix1, ByRef $aInMatrix2)
  525.    
  526.     Local $i, $j, $k
  527.     For $i = 0 To 3
  528.         For $j = 0 To 3
  529.             $aOutMatrix[$i][$j] = 0
  530.             For $k = 0 To 3
  531.                 $aOutMatrix[$i][$j] += $aInMatrix1[$i][$k] * $aInMatrix2[$k][$j]
  532.             Next
  533.         Next
  534.     Next
  535.    
  536. EndFunc ;==>__S3d_MultiplyMatrix
  537.  
  538. Func __S3d_SetCamera($nCameraX, $nCameraY, $nCameraZ, $nXYCos, $nXYSin, $nXZCos, $nXZSin, $nVAngle, $nFAngle, $nFScale)
  539.    
  540.     Local $aMxXY = __S3d_CreateMatrix(), $aMxXZ = __S3d_CreateMatrix(), $aMxV = __S3d_CreateMatrix()
  541.     Local $aTransMatrix = __S3d_CreateMatrix()
  542.     Local $aTempMatrix = __S3d_CreateMatrix()
  543.    
  544.     __S3d_SetMatrix($aTransMatrix, _
  545.         1, 0, 0, -$nCameraX, _
  546.         0, 1, 0, -$nCameraY, _
  547.         0, 0, 1, -$nCameraZ, _
  548.         0, 0, 0, 1)
  549.    
  550.     __S3d_SetMatrix($aMxXY, _
  551.          $nXYCos,  $nXYSin,        0,        0, _
  552.         -$nXYSin,  $nXYCos,        0,        0, _
  553.                0,        0,        1,        0, _
  554.                0,        0,        0,        1)
  555.    
  556.     __S3d_SetMatrix($aMxXZ, _
  557.          $nXZCos,        0,  $nXZSin,        0, _
  558.                0,        1,        0,        0, _
  559.         -$nXZSin,        0,  $nXZCos,        0, _
  560.                0,        0,        0,        1)
  561.    
  562.     __S3d_SetMatrix($aMxV, _
  563.                        1,               0,                0,                0, _
  564.                        0,  Cos(-$nVAngle),  -Sin(-$nVAngle),                0, _
  565.                        0,  Sin(-$nVAngle),   Cos(-$nVAngle),                0, _
  566.                        0,               0,                0,                1)
  567.    
  568.     __S3d_MultiplyMatrix($__S3d_aCameraMatrix, $aMxXY, $aTransMatrix)
  569.     __S3d_MultiplyMatrix($aTempMatrix, $aMxXZ, $__S3d_aCameraMatrix)
  570.     __S3d_MultiplyMatrix($__S3d_aCameraMatrix, $aMxV, $aTempMatrix)
  571.    
  572.     _S3d_SetLocalMatrix()
  573.    
  574.     $__S3d_nFAngleTan = Tan($nFAngle)
  575.     $__S3d_nFScale = $nFScale
  576.    
  577.     $__S3d_aCameraPos[0] = $nCameraX
  578.     $__S3d_aCameraPos[1] = $nCameraY
  579.     $__S3d_aCameraPos[2] = $nCameraZ
  580. EndFunc ;==>__S3d_SetCamera
  581.  
  582. ; __S3d_ConvertPos - converts 3D -> 2D
  583. Func __S3d_ConvertPos(ByRef $nOutX, ByRef $nOutY, $nInX, $nInY, $nInZ)
  584.    
  585.     Local $aInPos[4] = [$nInX, $nInY, $nInZ ,1], $aOutPos[3] = [0, 0, 0], $i, $j
  586.     For $i = 0 To 2
  587.         For $j = 0 To 3
  588.             $aOutPos[$i] += $__S3d_aConvertMatrix[$i][$j] * $aInPos[$j]
  589.         Next
  590.     Next
  591.    
  592.     If $aOutPos[0] <= 0 Then Return SetError(1, 0, 0)   ; behind the camera
  593.    
  594.     $nOutX = (- $aOutPos[1] / $aOutPos[0] / $__S3d_nFAngleTan) * ($__S3d_nFScale / 2) + ($__S3d_iWidth / 2)
  595.     $nOutY = (- $aOutPos[2] / $aOutPos[0] / $__S3d_nFAngleTan) * ($__S3d_nFScale / 2) + ($__S3d_iHeight / 2)
  596.    
  597.     Return 1
  598. EndFunc ;==>__S3d_ConvertPos
  599.  
  600. Func __S3d_Clip(ByRef $nOutX, ByRef $nOutY, $nOkX, $nOkY, $nOkZ, $nFailX, $nFailY, $nFailZ)
  601.     Local $nX1 = $nOkX, $nY1 = $nOkY, $nZ1 = $nOkZ, $nX2 = $nFailX, $nY2 = $nFailY, $nZ2 = $nFailZ
  602.     Local $nMidX, $nMidY, $nMidZ, $fSuccess = False, $i = 1
  603.    
  604.     Do
  605.         $nMidX = ($nX1 + $nX2) / 2
  606.         $nMidY = ($nY1 + $nY2) / 2
  607.         $nMidZ = ($nZ1 + $nZ2) / 2
  608.        
  609.         __S3d_ConvertPos($nOutX, $nOutY, $nMidX, $nMidY, $nMidZ)
  610.        
  611.         If @error Then
  612.             $nX2 = $nMidX
  613.             $nY2 = $nMidY
  614.             $nZ2 = $nMidZ
  615.         Else
  616.             $nX1 = $nMidX
  617.             $nY1 = $nMidY
  618.             $nZ1 = $nMidZ
  619.             $fSuccess = True
  620.         EndIf
  621.        
  622.         $i += 1
  623.     Until $i > 10
  624.    
  625.     If Not $fSuccess Then __S3d_ConvertPos($nOutX, $nOutY, $nOkX, $nOkY, $nOkZ)
  626.    
  627. EndFunc ;==>__S3d_Clip
  628.  
  629. Func __S3d_Deg2Rad($nDeg)
  630.     Return $nDeg / 180 * $__S3d_PI
  631. EndFunc ;==>__S3d_Deg2Rad
Advertisement
Add Comment
Please, Sign In to add comment