OpenTK Lighting-Engine

    • VB.NET
    • .NET (FX) 4.0

    Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von 00yoshi.

      OpenTK Lighting-Engine

      Ich dachte, dass ich einfach mal eine Lighting-Engine für OpenTK schreibe (wenn man sie bisschen verändert funzt das auch mit allem anderem 3D-zeugs mit vertexfarbe) weil das eingebaute Lighting ziemlich begränzt ist. (naja spiegelung und so... aber dafür nur 8 Lights für mich (GL.GetInteger(GetPName.MaxLights) = 8))
      Und dieses Lighting wirkt sich nur auf bestimmte Vertices aus.
      Light-Klasse:

      VB.NET-Quellcode

      1. Imports OpenTK
      2. Imports OpenTK.Graphics.OpenGL
      3. Public Class Light
      4. Public Position As Vector3
      5. Public Length As Single
      6. Public Color As Vector3
      7. Public Ambient As Vector3
      8. Public AmbientEnabled As Boolean = False
      9. Sub New()
      10. Position = New Vector3
      11. Length = 0
      12. Color = New Vector3
      13. Ambient = New Vector3
      14. End Sub
      15. Sub New(Position As Vector3, Length As Single, Color As Vector3, Ambient As Vector3)
      16. Me.Position = Position
      17. Me.Length = Length
      18. Me.Color = Color
      19. Me.Ambient = Ambient
      20. End Sub
      21. Public Shared Sub Color3(Lights As List(Of Light), Position As Vector3)
      22. Dim Color As Vector3
      23. Dim AmbientColor As Vector3
      24. Dim AmbientColorCount As Integer = Lights.Count
      25. Dim Tmp As Single
      26. Dim Amb As Boolean = False
      27. For Each Light As Light In Lights
      28. Tmp = 1 - Light.GetDistance(Position) / Light.Length
      29. If Tmp > 0 Then Color += Light.Color * Tmp
      30. If Light.AmbientEnabled Then
      31. AmbientColor += Light.Ambient / AmbientColorCount
      32. End If
      33. Amb = Amb Or Light.AmbientEnabled
      34. Next
      35. If Amb Then
      36. GL.Color3(Color + AmbientColor)
      37. Else
      38. GL.Color3(Color)
      39. End If
      40. End Sub
      41. Public Shared Sub Color3(Lights As List(Of Light), Position As Vector3, VertexColor As Vector3)
      42. Dim Color As Vector3
      43. Dim AmbientColor As Vector3
      44. Dim AmbientColorCount As Integer = Lights.Count
      45. Dim Tmp As Single
      46. Dim Amb As Boolean = False
      47. For Each Light As Light In Lights
      48. Tmp = 1 - Light.GetDistance(Position) / Light.Length
      49. If Tmp > 0 Then Color += Light.Color * Tmp
      50. If Light.AmbientEnabled Then
      51. AmbientColor += Light.Ambient / AmbientColorCount
      52. End If
      53. Amb = Amb Or Light.AmbientEnabled
      54. Next
      55. Color = New Vector3(Color.X * VertexColor.X, Color.Y * VertexColor.Y, Color.Z * VertexColor.Z)
      56. If Amb Then
      57. GL.Color3(Color + AmbientColor)
      58. Else
      59. GL.Color3(Color)
      60. End If
      61. End Sub
      62. Private Function GetDistance(Position As Vector3) As Single
      63. Position -= Me.Position
      64. Return Math.Sqrt(Position.X * Position.X + Position.Y * Position.Y + Position.Z * Position.Z)
      65. End Function
      66. End Class

      DirectionalLight-Klasse:

      VB.NET-Quellcode

      1. Imports OpenTK
      2. Imports OpenTK.Graphics.OpenGL
      3. Public Class DirectionalLight
      4. Inherits Light
      5. Const RadToDeg As Double = 180 / Math.PI
      6. Dim Direction As Vector3
      7. Dim Angle As Single
      8. Sub New()
      9. Position = New Vector3
      10. Length = 0
      11. Color = New Vector3
      12. Ambient = New Vector3
      13. Direction = New Vector3
      14. End Sub
      15. Sub New(Position As Vector3, Length As Single, Color As Vector3, Ambient As Vector3, Direction As Vector3, Angle As Single)
      16. Me.Position = Position
      17. Me.Length = Length
      18. Me.Color = Color
      19. Me.Ambient = Ambient
      20. Me.Direction = Direction
      21. Me.Angle = Angle
      22. End Sub
      23. Public Overloads Shared Sub Color3(Lights As List(Of DirectionalLight), Position As Vector3)
      24. Dim Color As Vector3
      25. Dim AmbientColor As Vector3
      26. Dim AmbientColorCount As Integer = Lights.Count
      27. Dim Tmp As Single
      28. Dim TmpA As Single
      29. Dim Amb As Boolean = False
      30. For Each Light As DirectionalLight In Lights
      31. Tmp = 1 - Light.GetDistance(Position) / Light.Length
      32. TmpA = GetAngleFromAxis(Light.Position, Light.Direction, Position) * 2 / Light.Angle
      33. Tmp -= TmpA
      34. If Tmp > 0 Then Color += Light.Color * Tmp
      35. If Light.AmbientEnabled Then
      36. AmbientColor += Light.Ambient / AmbientColorCount
      37. End If
      38. Amb = Amb Or Light.AmbientEnabled
      39. Next
      40. If Amb Then
      41. GL.Color3(Color + AmbientColor)
      42. Else
      43. GL.Color3(Color)
      44. End If
      45. End Sub
      46. Public Overloads Shared Sub Color3(Lights As List(Of DirectionalLight), Position As Vector3, VertexColor As Vector3)
      47. Dim Color As Vector3
      48. Dim AmbientColor As Vector3
      49. Dim AmbientColorCount As Integer = Lights.Count
      50. Dim Tmp As Single
      51. Dim TmpA As Single
      52. Dim Amb As Boolean = False
      53. For Each Light As DirectionalLight In Lights
      54. Tmp = 1 - Light.GetDistance(Position) / Light.Length
      55. TmpA = GetAngleFromAxis(Light.Position, Light.Direction, Position) * 2 / Light.Angle
      56. Tmp -= TmpA
      57. If Tmp > 0 Then Color += Light.Color * Tmp
      58. If Light.AmbientEnabled Then
      59. AmbientColor += Light.Ambient / AmbientColorCount
      60. End If
      61. Amb = Amb Or Light.AmbientEnabled
      62. Next
      63. Color = New Vector3(Color.X * VertexColor.X, Color.Y * VertexColor.Y, Color.Z * VertexColor.Z)
      64. If Amb Then
      65. GL.Color3(Color + AmbientColor)
      66. Else
      67. GL.Color3(Color)
      68. End If
      69. End Sub
      70. Private Function GetDistance(Position As Vector3) As Single
      71. Position -= Me.Position
      72. Return Math.Sqrt(Position.X * Position.X + Position.Y * Position.Y + Position.Z * Position.Z)
      73. End Function
      74. Private Shared Function GetAngleFromAxis(Position As Vector3, Axis As Vector3, Point As Vector3) As Single
      75. Point -= Position
      76. Axis.Normalize()
      77. Return Math.Acos((Axis.X * Point.X + Axis.Y * Point.Y + Axis.Z * Point.Z) / Math.Sqrt(Point.X * Point.X + Point.Y * Point.Y + Point.Z * Point.Z) * Math.Sqrt(Axis.X * Axis.X + Axis.Y * Axis.Y + Axis.Z * Axis.Z)) * RadToDeg
      78. End Function
      79. End Class

      Die Main-Klasse (für das Programm)
      Spoiler anzeigen

      Quellcode

      1. Imports OpenTK
      2. Imports OpenTK.Graphics
      3. Imports OpenTK.Graphics.OpenGL
      4. Imports System.Drawing
      5. Public Class Main
      6. Inherits GameWindow
      7. Const Small = 0.001
      8. 'Updates pro Sekunde
      9. Const UPS As Integer = 60
      10. Const OneBySixteen = 1 / 16
      11. Const WalkSpeed As Double = 32 / UPS
      12. Const DegToRad As Double = Math.PI / 180
      13. Const RadToDeg As Double = 180 / Math.PI
      14. Dim WithEvents Keyb As Input.KeyboardDevice
      15. Dim WithEvents FPSTimer As New Timer
      16. Dim Keys(5) As Boolean
      17. Dim LookAt As Matrix4
      18. Dim Perspective As Matrix4
      19. Dim Position As New Vector3(0, 0, 0)
      20. Dim Rotation As New Vector2(0, 0)
      21. Dim Rect As Rectangle = Windows.Forms.Screen.GetBounds(New Point(0, 0))
      22. Dim ScreenRatioW As Double
      23. Dim ScreenRatioH As Double
      24. Dim ViewPoint As Vector3
      25. Dim ViewDirection As Vector3
      26. Dim Rnd As New Random
      27. Dim TexID As Integer
      28. Dim FontID As Integer
      29. Dim FramesElapsed As Integer = 0
      30. Dim FPS As Integer
      31. Dim L As New List(Of DirectionalLight)
      32. Shared Sub Main()
      33. Dim Main As New Main
      34. Main.Run(60, 60)
      35. Main.Dispose()
      36. End Sub
      37. Protected Overrides Sub OnRenderFrame(e As OpenTK.FrameEventArgs)
      38. GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
      39. Dim MP = Input.Mouse.GetState
      40. Dim MPY As Integer = Windows.Forms.Cursor.Position.Y
      41. Rotation.X = 0 - (MP.X / 4)
      42. Rotation.Y = 0 - 0 - 90 + (180 * (1 - ((1 + MPY) / (Rect.Height + 2))))
      43. ViewPoint = New Vector3(Position.X + Math.Sin(Rotation.X * Math.PI / 180) * Math.Cos(Rotation.Y * Math.PI / 180), Position.Y + Math.Sin(Rotation.Y * Math.PI / 180), Position.Z + Math.Cos(Rotation.X * Math.PI / 180) * Math.Cos(Rotation.Y * Math.PI / 180))
      44. LookAt = Matrix4.LookAt(Position.X, Position.Y, Position.Z, ViewPoint.X, ViewPoint.Y, ViewPoint.Z, 0, 1, 0)
      45. ViewPoint -= Position
      46. GL.MatrixMode(MatrixMode.Projection)
      47. GL.LoadIdentity()
      48. GL.LoadMatrix(Perspective)
      49. GL.MatrixMode(MatrixMode.Modelview)
      50. GL.LoadIdentity()
      51. GL.LoadMatrix(LookAt)
      52. GL.BindTexture(TextureTarget.Texture2D, TexID)
      53. Draw()
      54. GL.Rotate(FC, Vector3.UnitX)
      55. GL.MatrixMode(MatrixMode.Projection)
      56. GL.PushMatrix()
      57. GL.LoadIdentity()
      58. GL.Ortho(-1.0F, 1.0F, -1.0F, 1.0F, -1.0F, 1.0F)
      59. GL.MatrixMode(MatrixMode.Modelview)
      60. GL.PushMatrix()
      61. GL.LoadIdentity()
      62. GL.BindTexture(TextureTarget.Texture2D, FontID)
      63. DrawHud()
      64. GL.PopMatrix()
      65. GL.PopMatrix()
      66. Dim s As String
      67. s = GL.GetError.ToString
      68. SwapBuffers()
      69. 'GrabScreenshot.Save("C:\users\coskun\desktop\test.png")
      70. 'End
      71. FramesElapsed += 1
      72. MyBase.OnRenderFrame(e)
      73. End Sub
      74. Protected Overrides Sub OnUpdateFrame(e As OpenTK.FrameEventArgs)
      75. If Keys(1) = True Then
      76. Position.X += WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
      77. Position.Z -= WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
      78. End If
      79. If Keys(3) = True Then
      80. Position.X -= WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
      81. Position.Z += WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
      82. End If
      83. If Keys(2) = True Then
      84. Position.Z -= WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
      85. Position.X -= WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
      86. End If
      87. If Keys(0) = True Then
      88. Position.Z += WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
      89. Position.X += WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
      90. End If
      91. If Keys(5) = True Then Position.Y += WalkSpeed
      92. If Keys(4) = True Then Position.Y -= WalkSpeed
      93. MyBase.OnUpdateFrame(e)
      94. End Sub
      95. 'FrameCount
      96. Dim FC As Single = 0
      97. Protected Overrides Sub OnLoad(e As System.EventArgs)
      98. L.Add(New DirectionalLight(New Vector3(0, -15, 0), 32, New Vector3(1.0, 1.0, 1.0), New Vector3(0.0, 0.0, 0.0), New Vector3(0, 1, 0), 90))
      99. 'L.Add(New DirectionalLight(New Vector3(-4, 0, 4), 32, New Vector3(0.0, 1.0, 0.0), New Vector3(0.0, 0.0, 0.0)))
      100. 'L.Add(New DirectionalLight(New Vector3(-4, 0, -4), 32, New Vector3(0.0, 0.0, 1.0), New Vector3(0.0, 0.0, 0.0)))
      101. GL.FrontFace(FrontFaceDirection.Ccw)
      102. GL.CullFace(CullFaceMode.Back)
      103. 'GL.Enable(EnableCap.CullFace)
      104. GL.Enable(EnableCap.DepthTest)
      105. GL.DepthFunc(DepthFunction.Less)
      106. 'GL.Enable(EnableCap.Lighting)
      107. 'GL.Enable(EnableCap.Light0)
      108. 'GL.Light(LightName.Light0, LightParameter.Position, New Single() {1.0F, 1.0F, -0.5F})
      109. 'GL.Light(LightName.Light0, LightParameter.Ambient, New Single() {0.3F, 0.3F, 0.3F, 1.0F})
      110. 'GL.Light(LightName.Light0, LightParameter.Diffuse, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
      111. 'GL.Light(LightName.Light0, LightParameter.Specular, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
      112. 'GL.Light(LightName.Light0, LightParameter.SpotExponent, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
      113. 'GL.LightModel(LightModelParameter.LightModelAmbient, New Single() {0.2F, 0.2F, 0.2F, 1.0F})
      114. 'GL.LightModel(LightModelParameter.LightModelTwoSide, 1)
      115. 'GL.LightModel(LightModelParameter.LightModelLocalViewer, 1)
      116. '
      117. ''Use GL.Material to set your object's material parameters.
      118. 'GL.Material(MaterialFace.Front, MaterialParameter.Ambient, New Single() {0.3F, 0.3F, 0.3F, 1.0F})
      119. 'GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
      120. 'GL.Material(MaterialFace.Front, MaterialParameter.Specular, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
      121. 'GL.Material(MaterialFace.Front, MaterialParameter.Shininess, New Single() {50.0F})
      122. 'GL.Material(MaterialFace.Front, MaterialParameter.Emission, New Single() {0.0F, 0.0F, 0.0F, 1.0F})
      123. 'GL.Enable(EnableCap.Lighting)
      124. 'GL.Enable(EnableCap.ColorMaterial)
      125. 'GL.Enable(EnableCap.Light0)
      126. 'Dim mat_specular() As Single = {1.0F, 1.0F, 1.0F, 1.0F}
      127. 'Dim mat_shininess() As Single = {10.0F}
      128. 'Dim light_position() As Single = {10.0F, 10.0F, 0.0F, 0.0F}
      129. 'Dim light_ambient() As Single = {0.0F, 0.0F, 0.0F, 1.0F}
      130. '
      131. 'GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular)
      132. 'GL.Material(MaterialFace.Front, MaterialParameter.Shininess, mat_shininess)
      133. 'GL.Light(LightName.Light1, LightParameter.Position, light_position)
      134. 'GL.Light(LightName.Light1, LightParameter.Ambient, light_ambient)
      135. 'GL.Light(LightName.Light1, LightParameter.Diffuse, mat_specular)
      136. 'GL.Light(LightName.Light0, LightParameter.Ambient, New Single() {0.0, 0.0, 0.0, 1.0})
      137. 'GL.Light(LightName.Light0, LightParameter.Diffuse, New Single() {1.0, 1.0, 0.0, 1.0})
      138. 'GL.Light(LightName.Light0, LightParameter.Specular, New Single() {1.0, 1.0, 1.0, 1.0})
      139. 'GL.Light(LightName.Light0, LightParameter.Position, New Single() {10, 10, 10, 1.0})
      140. 'GL.Light(LightName.Light0, LightParameter.SpotDirection, New Single() {1, 1, 1})
      141. 'GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, New Single() {1.5})
      142. 'GL.Light(LightName.Light0, LightParameter.LinearAttenuation, New Single() {0.5})
      143. 'GL.Light(LightName.Light0, LightParameter.QuadraticAttenuation, New Single() {0.2})
      144. 'GL.LightModel(LightModelParameter.LightModelAmbient, New Single() {0.2, 0.2, 0.2, 1.0})
      145. 'GL.LightModel(LightModelParameter.LightModelLocalViewer, New Single() {1.0})
      146. 'GL.Material(MaterialFace.Front, MaterialParameter.AmbientAndDiffuse, New Single() {1.0, 1.0, 1.0, 1.0})
      147. 'GL.Material(MaterialFace.Front, MaterialParameter.Specular, New Single() {1.0, 1.0, 1.0, 1.0})
      148. 'GL.Material(MaterialFace.Front, MaterialParameter.Shininess, New Single() {50.0})
      149. 'GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, New Single() {1.0, 1.0, 0.0, 1.0})
      150. Me.WindowState = WindowState.Maximized
      151. 'GL.Enable(EnableCap.Fog)
      152. GL.Fog(FogParameter.FogColor, {1.0F, 1.0F, 1.0F})
      153. GL.Fog(FogParameter.FogMode, FogMode.Linear)
      154. GL.Fog(FogParameter.FogDensity, 1.0F)
      155. GL.Fog(FogParameter.FogStart, 8)
      156. GL.Fog(FogParameter.FogEnd, 128)
      157. GL.Hint(HintTarget.FogHint, HintMode.Fastest)
      158. GL.ClearColor(New Color4(0.5F, 0.5F, 0.5F, 1.0F))
      159. GL.PointSize(4)
      160. MyBase.OnLoad(e)
      161. End Sub
      162. Protected Overrides Sub OnUnload(e As System.EventArgs)
      163. MyBase.OnUnload(e)
      164. End Sub
      165. Protected Overrides Sub OnResize(e As System.EventArgs)
      166. ScreenRatioW = Width / Height
      167. ScreenRatioH = Height / Width
      168. Perspective = Matrix4.CreatePerspectiveFieldOfView(1, ClientRectangle.Width / ClientRectangle.Height, 0.1, 8192)
      169. GL.Viewport(ClientRectangle.Size)
      170. MyBase.OnResize(e)
      171. End Sub
      172. Dim Cnt As Integer = 0
      173. Sub Draw()
      174. DrawQuadArrayY(32, -15, New Vector3(1, 1, 1))
      175. DrawQuadArrayY(32, -10, New Vector3(1, 0, 0))
      176. DrawQuadArrayY(32, -5, New Vector3(0, 1, 0))
      177. DrawQuadArrayY(32, 0, New Vector3(0, 0, 1))
      178. DrawQuadArrayY(32, 5, New Vector3(1, 1, 0))
      179. DrawQuadArrayY(32, 10, New Vector3(1, 1, 1))
      180. GL.Color3(Color.Black)
      181. GL.Begin(PrimitiveType.Quads)
      182. GL.Vertex3(4096, 4096, 4096)
      183. GL.Vertex3(4096, -4096, 4096)
      184. GL.Vertex3(4096, -4096, -4096)
      185. GL.Vertex3(4096, 4096, -4096)
      186. GL.End()
      187. Cnt += 1
      188. End Sub
      189. Sub DrawHud()
      190. End Sub
      191. Protected Overrides Sub OnKeyDown(e As OpenTK.Input.KeyboardKeyEventArgs)
      192. If e.Key = Input.Key.W Then
      193. Keys(0) = True
      194. End If
      195. If e.Key = Input.Key.A Then
      196. Keys(1) = True
      197. End If
      198. If e.Key = Input.Key.S Then
      199. Keys(2) = True
      200. End If
      201. If e.Key = Input.Key.D Then
      202. Keys(3) = True
      203. End If
      204. If e.Key = Input.Key.LShift Then
      205. Keys(4) = True
      206. End If
      207. If e.Key = Input.Key.Space Then
      208. Keys(5) = True
      209. End If
      210. If e.Key = Input.Key.F2 Then
      211. GrabScreenshot().Save("C:\users\coskun\desktop\" & Cnt & ".png")
      212. End If
      213. MyBase.OnKeyDown(e)
      214. End Sub
      215. Protected Overrides Sub OnKeyup(e As OpenTK.Input.KeyboardKeyEventArgs)
      216. If e.Key = Input.Key.W Then
      217. Keys(0) = False
      218. End If
      219. If e.Key = Input.Key.A Then
      220. Keys(1) = False
      221. End If
      222. If e.Key = Input.Key.S Then
      223. Keys(2) = False
      224. End If
      225. If e.Key = Input.Key.D Then
      226. Keys(3) = False
      227. End If
      228. If e.Key = Input.Key.LShift Then
      229. Keys(4) = False
      230. End If
      231. If e.Key = Input.Key.Space Then
      232. Keys(5) = False
      233. End If
      234. MyBase.OnKeyUp(e)
      235. End Sub
      236. Private Sub DrawBall(Position As Vector3, Size As Double, PartsWidth As Integer, PartsHigh As Integer)
      237. Dim X As Integer
      238. 'Circle Count
      239. Dim CC As Integer = PartsWidth
      240. Dim Tmp As Vector3
      241. For I As Integer = -PartsHigh / 2 - 1 To PartsHigh / 2
      242. GL.Begin(PrimitiveType.Quads)
      243. For O As Integer = 0 To CC - 1
      244. Tmp = New Vector3(Size * Math.Cos(90 / (PartsHigh / 2) * I * Math.PI / 180) * Math.Sin(360 / CC * O * Math.PI / 180) + Position.X, Size * Math.Sin(90 / (PartsHigh / 2) * I * Math.PI / 180) + Position.Y, Size * Math.Cos(90 / (PartsHigh / 2) * I * Math.PI / 180) * Math.Cos(360 / CC * O * Math.PI / 180) + Position.Z)
      245. DirectionalLight.Color3(L, Tmp)
      246. GL.Vertex3(Tmp)
      247. O += 1
      248. Tmp = New Vector3(Size * Math.Cos(90 / (PartsHigh / 2) * I * Math.PI / 180) * Math.Sin(360 / CC * O * Math.PI / 180) + Position.X, Size * Math.Sin(90 / (PartsHigh / 2) * I * Math.PI / 180) + Position.Y, Size * Math.Cos(90 / (PartsHigh / 2) * I * Math.PI / 180) * Math.Cos(360 / CC * O * Math.PI / 180) + Position.Z)
      249. 'Light.Color3(L, Tmp)
      250. GL.Vertex3(Tmp)
      251. X = I + 1
      252. Tmp = New Vector3(Size * Math.Cos(90 / (PartsHigh / 2) * X * Math.PI / 180) * Math.Sin(360 / CC * O * Math.PI / 180) + Position.X, Size * Math.Sin(90 / (PartsHigh / 2) * X * Math.PI / 180) + Position.Y, Size * Math.Cos(90 / (PartsHigh / 2) * X * Math.PI / 180) * Math.Cos(360 / CC * O * Math.PI / 180) + Position.Z)
      253. 'Light.Color3(L, Tmp)
      254. GL.Vertex3(Tmp)
      255. O -= 1
      256. Tmp = New Vector3(Size * Math.Cos(90 / (PartsHigh / 2) * X * Math.PI / 180) * Math.Sin(360 / CC * O * Math.PI / 180) + Position.X, Size * Math.Sin(90 / (PartsHigh / 2) * X * Math.PI / 180) + Position.Y, Size * Math.Cos(90 / (PartsHigh / 2) * X * Math.PI / 180) * Math.Cos(360 / CC * O * Math.PI / 180) + Position.Z)
      257. 'Light.Color3(L, Tmp)
      258. GL.Vertex3(Tmp)
      259. Next
      260. GL.End()
      261. Next
      262. End Sub
      263. Private Sub DrawPointArray(Size As Integer)
      264. Dim HalfSize As Single = Size / 2
      265. GL.Begin(PrimitiveType.Points)
      266. For X As Integer = -HalfSize To HalfSize
      267. For Y As Integer = -HalfSize To HalfSize
      268. For Z As Integer = -HalfSize To HalfSize
      269. DirectionalLight.Color3(L, New Vector3(X, Y, Z))
      270. If GetAngleFromAxis(New Vector3(0, 0, 0), New Vector3(0, 1, 0), New Vector3(X, Y, Z)) * 2 <= 20 Then
      271. GL.Color3(1 - GetAngleFromAxis(New Vector3(0, 0, 0), New Vector3(0, 1, 0), New Vector3(X, Y, Z)) / 20, 1 - GetAngleFromAxis(New Vector3(0, 0, 0), New Vector3(1, 0, 0), New Vector3(X, Y, Z)) / 20, 1 - GetAngleFromAxis(New Vector3(0, 0, 0), New Vector3(1, 0, 0), New Vector3(X, Y, Z)) / 20)
      272. GL.Vertex3(X, Y, Z)
      273. End If
      274. Next
      275. Next
      276. Next
      277. GL.End()
      278. End Sub
      279. Private Sub DrawQuadArrayYLPQ(Size As Integer, HS As Integer)
      280. Dim HalfSize As Single = Size / 2
      281. GL.Begin(PrimitiveType.Quads)
      282. For X As Integer = -HalfSize To HalfSize - 1
      283. For Y As Integer = -HalfSize To HalfSize - 1
      284. DirectionalLight.Color3(L, New Vector3(X, 0, Y))
      285. GL.Vertex3(X, 0 + HS, Y)
      286. GL.Vertex3(X + 1, 0 + HS, Y)
      287. GL.Vertex3(X + 1, 0 + HS, Y + 1)
      288. GL.Vertex3(X, 0 + HS, Y + 1)
      289. Next
      290. Next
      291. For X As Integer = -HalfSize To HalfSize - 1
      292. For Y As Integer = -HalfSize To HalfSize - 1
      293. DirectionalLight.Color3(L, New Vector3(X, 10, Y))
      294. GL.Vertex3(X, 10 + HS, Y)
      295. GL.Vertex3(X + 1, 10 + HS, Y)
      296. GL.Vertex3(X + 1, 10 + HS, Y + 1)
      297. GL.Vertex3(X, 10 + HS, Y + 1)
      298. Next
      299. Next
      300. For X As Integer = -HalfSize To HalfSize - 1
      301. For Y As Integer = -HalfSize To HalfSize - 1
      302. DirectionalLight.Color3(L, New Vector3(X, -10, Y))
      303. GL.Vertex3(X, -10 + HS, Y)
      304. GL.Vertex3(X + 1, -10 + HS, Y)
      305. GL.Vertex3(X + 1, -10 + HS, Y + 1)
      306. GL.Vertex3(X, -10 + HS, Y + 1)
      307. Next
      308. Next
      309. GL.End()
      310. End Sub
      311. Private Sub DrawQuadArrayY(Size As Integer, YPos As Integer, Color As Vector3)
      312. Dim HalfSize As Single = Size / 2
      313. GL.Begin(PrimitiveType.Quads)
      314. For X As Integer = -HalfSize To HalfSize - 1
      315. For Y As Integer = -HalfSize To HalfSize - 1
      316. DirectionalLight.Color3(L, New Vector3(X, YPos, Y), Color)
      317. GL.Vertex3(X, YPos, Y)
      318. DirectionalLight.Color3(L, New Vector3(X + 1, YPos, Y), Color)
      319. GL.Vertex3(X + 1, YPos, Y)
      320. DirectionalLight.Color3(L, New Vector3(X + 1, YPos, Y + 1), Color)
      321. GL.Vertex3(X + 1, YPos, Y + 1)
      322. DirectionalLight.Color3(L, New Vector3(X, YPos, Y + 1), Color)
      323. GL.Vertex3(X, YPos, Y + 1)
      324. Next
      325. Next
      326. GL.End()
      327. End Sub
      328. Private Sub RandomColor()
      329. GL.Color3(Rnd.NextDouble)
      330. End Sub
      331. Function GrabScreenshot() As Bitmap
      332. If (GraphicsContext.CurrentContext Is Nothing) Then
      333. Throw New GraphicsContextMissingException
      334. End If
      335. Dim bmp As Bitmap = New Bitmap(ClientSize.Width, ClientSize.Height)
      336. Dim data As System.Drawing.Imaging.BitmapData = bmp.LockBits(ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
      337. GL.ReadPixels(0, 0, ClientSize.Width, ClientSize.Height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0)
      338. bmp.UnlockBits(data)
      339. bmp.RotateFlip(RotateFlipType.RotateNoneFlipY)
      340. Return bmp
      341. End Function
      342. Private Function IsInCone(Angle As Double, Direction As Vector3, Point As Vector3) As Boolean
      343. Angle *= DegToRad
      344. Direction.Normalize()
      345. Return Math.Acos((Direction.X * Point.X + Direction.Y * Point.Y + Direction.Z * Point.Z) / Math.Sqrt(Point.X * Point.X + Point.Y * Point.Y + Point.Z * Point.Z) * Math.Sqrt(Direction.X * Direction.X + Direction.Y * Direction.Y + Direction.Z * Direction.Z)) <= Angle
      346. End Function
      347. Private Shared Function GetAngleFromAxis(Position As Vector3, Axis As Vector3, Point As Vector3) As Single
      348. Point -= Position
      349. Axis.Normalize()
      350. Return Math.Acos((Axis.X * Point.X + Axis.Y * Point.Y + Axis.Z * Point.Z) / (Math.Sqrt(Point.X * Point.X + Point.Y * Point.Y + Point.Z * Point.Z) * Math.Sqrt(Axis.X * Axis.X + Axis.Y * Axis.Y + Axis.Z * Axis.Z))) * RadToDeg
      351. End Function
      352. End Class

      So, das ist alles.
      DLL im anhang.
      (und Test-Programm auch)
      Dateien