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:
DirectionalLight-Klasse:
Die Main-Klasse (für das Programm)
Spoiler anzeigen
So, das ist alles.
DLL im anhang.
(und Test-Programm auch)
Und dieses Lighting wirkt sich nur auf bestimmte Vertices aus.
Light-Klasse:
VB.NET-Quellcode
- Imports OpenTK
- Imports OpenTK.Graphics.OpenGL
- Public Class Light
- Public Position As Vector3
- Public Length As Single
- Public Color As Vector3
- Public Ambient As Vector3
- Public AmbientEnabled As Boolean = False
- Sub New()
- Position = New Vector3
- Length = 0
- Color = New Vector3
- Ambient = New Vector3
- End Sub
- Sub New(Position As Vector3, Length As Single, Color As Vector3, Ambient As Vector3)
- Me.Position = Position
- Me.Length = Length
- Me.Color = Color
- Me.Ambient = Ambient
- End Sub
- Public Shared Sub Color3(Lights As List(Of Light), Position As Vector3)
- Dim Color As Vector3
- Dim AmbientColor As Vector3
- Dim AmbientColorCount As Integer = Lights.Count
- Dim Tmp As Single
- Dim Amb As Boolean = False
- For Each Light As Light In Lights
- Tmp = 1 - Light.GetDistance(Position) / Light.Length
- If Tmp > 0 Then Color += Light.Color * Tmp
- If Light.AmbientEnabled Then
- AmbientColor += Light.Ambient / AmbientColorCount
- End If
- Amb = Amb Or Light.AmbientEnabled
- Next
- If Amb Then
- GL.Color3(Color + AmbientColor)
- Else
- GL.Color3(Color)
- End If
- End Sub
- Public Shared Sub Color3(Lights As List(Of Light), Position As Vector3, VertexColor As Vector3)
- Dim Color As Vector3
- Dim AmbientColor As Vector3
- Dim AmbientColorCount As Integer = Lights.Count
- Dim Tmp As Single
- Dim Amb As Boolean = False
- For Each Light As Light In Lights
- Tmp = 1 - Light.GetDistance(Position) / Light.Length
- If Tmp > 0 Then Color += Light.Color * Tmp
- If Light.AmbientEnabled Then
- AmbientColor += Light.Ambient / AmbientColorCount
- End If
- Amb = Amb Or Light.AmbientEnabled
- Next
- Color = New Vector3(Color.X * VertexColor.X, Color.Y * VertexColor.Y, Color.Z * VertexColor.Z)
- If Amb Then
- GL.Color3(Color + AmbientColor)
- Else
- GL.Color3(Color)
- End If
- End Sub
- Private Function GetDistance(Position As Vector3) As Single
- Position -= Me.Position
- Return Math.Sqrt(Position.X * Position.X + Position.Y * Position.Y + Position.Z * Position.Z)
- End Function
- End Class
DirectionalLight-Klasse:
VB.NET-Quellcode
- Imports OpenTK
- Imports OpenTK.Graphics.OpenGL
- Public Class DirectionalLight
- Inherits Light
- Const RadToDeg As Double = 180 / Math.PI
- Dim Direction As Vector3
- Dim Angle As Single
- Sub New()
- Position = New Vector3
- Length = 0
- Color = New Vector3
- Ambient = New Vector3
- Direction = New Vector3
- End Sub
- Sub New(Position As Vector3, Length As Single, Color As Vector3, Ambient As Vector3, Direction As Vector3, Angle As Single)
- Me.Position = Position
- Me.Length = Length
- Me.Color = Color
- Me.Ambient = Ambient
- Me.Direction = Direction
- Me.Angle = Angle
- End Sub
- Public Overloads Shared Sub Color3(Lights As List(Of DirectionalLight), Position As Vector3)
- Dim Color As Vector3
- Dim AmbientColor As Vector3
- Dim AmbientColorCount As Integer = Lights.Count
- Dim Tmp As Single
- Dim TmpA As Single
- Dim Amb As Boolean = False
- For Each Light As DirectionalLight In Lights
- Tmp = 1 - Light.GetDistance(Position) / Light.Length
- TmpA = GetAngleFromAxis(Light.Position, Light.Direction, Position) * 2 / Light.Angle
- Tmp -= TmpA
- If Tmp > 0 Then Color += Light.Color * Tmp
- If Light.AmbientEnabled Then
- AmbientColor += Light.Ambient / AmbientColorCount
- End If
- Amb = Amb Or Light.AmbientEnabled
- Next
- If Amb Then
- GL.Color3(Color + AmbientColor)
- Else
- GL.Color3(Color)
- End If
- End Sub
- Public Overloads Shared Sub Color3(Lights As List(Of DirectionalLight), Position As Vector3, VertexColor As Vector3)
- Dim Color As Vector3
- Dim AmbientColor As Vector3
- Dim AmbientColorCount As Integer = Lights.Count
- Dim Tmp As Single
- Dim TmpA As Single
- Dim Amb As Boolean = False
- For Each Light As DirectionalLight In Lights
- Tmp = 1 - Light.GetDistance(Position) / Light.Length
- TmpA = GetAngleFromAxis(Light.Position, Light.Direction, Position) * 2 / Light.Angle
- Tmp -= TmpA
- If Tmp > 0 Then Color += Light.Color * Tmp
- If Light.AmbientEnabled Then
- AmbientColor += Light.Ambient / AmbientColorCount
- End If
- Amb = Amb Or Light.AmbientEnabled
- Next
- Color = New Vector3(Color.X * VertexColor.X, Color.Y * VertexColor.Y, Color.Z * VertexColor.Z)
- If Amb Then
- GL.Color3(Color + AmbientColor)
- Else
- GL.Color3(Color)
- End If
- End Sub
- Private Function GetDistance(Position As Vector3) As Single
- Position -= Me.Position
- Return Math.Sqrt(Position.X * Position.X + Position.Y * Position.Y + Position.Z * Position.Z)
- End Function
- Private Shared Function GetAngleFromAxis(Position As Vector3, Axis As Vector3, Point As Vector3) As Single
- Point -= Position
- Axis.Normalize()
- 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
- End Function
- End Class
Die Main-Klasse (für das Programm)
Quellcode
- Imports OpenTK
- Imports OpenTK.Graphics
- Imports OpenTK.Graphics.OpenGL
- Imports System.Drawing
- Public Class Main
- Inherits GameWindow
- Const Small = 0.001
- 'Updates pro Sekunde
- Const UPS As Integer = 60
- Const OneBySixteen = 1 / 16
- Const WalkSpeed As Double = 32 / UPS
- Const DegToRad As Double = Math.PI / 180
- Const RadToDeg As Double = 180 / Math.PI
- Dim WithEvents Keyb As Input.KeyboardDevice
- Dim WithEvents FPSTimer As New Timer
- Dim Keys(5) As Boolean
- Dim LookAt As Matrix4
- Dim Perspective As Matrix4
- Dim Position As New Vector3(0, 0, 0)
- Dim Rotation As New Vector2(0, 0)
- Dim Rect As Rectangle = Windows.Forms.Screen.GetBounds(New Point(0, 0))
- Dim ScreenRatioW As Double
- Dim ScreenRatioH As Double
- Dim ViewPoint As Vector3
- Dim ViewDirection As Vector3
- Dim Rnd As New Random
- Dim TexID As Integer
- Dim FontID As Integer
- Dim FramesElapsed As Integer = 0
- Dim FPS As Integer
- Dim L As New List(Of DirectionalLight)
- Shared Sub Main()
- Dim Main As New Main
- Main.Run(60, 60)
- Main.Dispose()
- End Sub
- Protected Overrides Sub OnRenderFrame(e As OpenTK.FrameEventArgs)
- GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
- Dim MP = Input.Mouse.GetState
- Dim MPY As Integer = Windows.Forms.Cursor.Position.Y
- Rotation.X = 0 - (MP.X / 4)
- Rotation.Y = 0 - 0 - 90 + (180 * (1 - ((1 + MPY) / (Rect.Height + 2))))
- 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))
- LookAt = Matrix4.LookAt(Position.X, Position.Y, Position.Z, ViewPoint.X, ViewPoint.Y, ViewPoint.Z, 0, 1, 0)
- ViewPoint -= Position
- GL.MatrixMode(MatrixMode.Projection)
- GL.LoadIdentity()
- GL.LoadMatrix(Perspective)
- GL.MatrixMode(MatrixMode.Modelview)
- GL.LoadIdentity()
- GL.LoadMatrix(LookAt)
- GL.BindTexture(TextureTarget.Texture2D, TexID)
- Draw()
- GL.Rotate(FC, Vector3.UnitX)
- GL.MatrixMode(MatrixMode.Projection)
- GL.PushMatrix()
- GL.LoadIdentity()
- GL.Ortho(-1.0F, 1.0F, -1.0F, 1.0F, -1.0F, 1.0F)
- GL.MatrixMode(MatrixMode.Modelview)
- GL.PushMatrix()
- GL.LoadIdentity()
- GL.BindTexture(TextureTarget.Texture2D, FontID)
- DrawHud()
- GL.PopMatrix()
- GL.PopMatrix()
- Dim s As String
- s = GL.GetError.ToString
- SwapBuffers()
- 'GrabScreenshot.Save("C:\users\coskun\desktop\test.png")
- 'End
- FramesElapsed += 1
- MyBase.OnRenderFrame(e)
- End Sub
- Protected Overrides Sub OnUpdateFrame(e As OpenTK.FrameEventArgs)
- If Keys(1) = True Then
- Position.X += WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
- Position.Z -= WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
- End If
- If Keys(3) = True Then
- Position.X -= WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
- Position.Z += WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
- End If
- If Keys(2) = True Then
- Position.Z -= WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
- Position.X -= WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
- End If
- If Keys(0) = True Then
- Position.Z += WalkSpeed * Math.Cos(Rotation.X * Math.PI / 180)
- Position.X += WalkSpeed * Math.Sin(Rotation.X * Math.PI / 180)
- End If
- If Keys(5) = True Then Position.Y += WalkSpeed
- If Keys(4) = True Then Position.Y -= WalkSpeed
- MyBase.OnUpdateFrame(e)
- End Sub
- 'FrameCount
- Dim FC As Single = 0
- Protected Overrides Sub OnLoad(e As System.EventArgs)
- 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))
- '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)))
- '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)))
- GL.FrontFace(FrontFaceDirection.Ccw)
- GL.CullFace(CullFaceMode.Back)
- 'GL.Enable(EnableCap.CullFace)
- GL.Enable(EnableCap.DepthTest)
- GL.DepthFunc(DepthFunction.Less)
- 'GL.Enable(EnableCap.Lighting)
- 'GL.Enable(EnableCap.Light0)
- 'GL.Light(LightName.Light0, LightParameter.Position, New Single() {1.0F, 1.0F, -0.5F})
- 'GL.Light(LightName.Light0, LightParameter.Ambient, New Single() {0.3F, 0.3F, 0.3F, 1.0F})
- 'GL.Light(LightName.Light0, LightParameter.Diffuse, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
- 'GL.Light(LightName.Light0, LightParameter.Specular, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
- 'GL.Light(LightName.Light0, LightParameter.SpotExponent, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
- 'GL.LightModel(LightModelParameter.LightModelAmbient, New Single() {0.2F, 0.2F, 0.2F, 1.0F})
- 'GL.LightModel(LightModelParameter.LightModelTwoSide, 1)
- 'GL.LightModel(LightModelParameter.LightModelLocalViewer, 1)
- '
- ''Use GL.Material to set your object's material parameters.
- 'GL.Material(MaterialFace.Front, MaterialParameter.Ambient, New Single() {0.3F, 0.3F, 0.3F, 1.0F})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Specular, New Single() {1.0F, 1.0F, 1.0F, 1.0F})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Shininess, New Single() {50.0F})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Emission, New Single() {0.0F, 0.0F, 0.0F, 1.0F})
- 'GL.Enable(EnableCap.Lighting)
- 'GL.Enable(EnableCap.ColorMaterial)
- 'GL.Enable(EnableCap.Light0)
- 'Dim mat_specular() As Single = {1.0F, 1.0F, 1.0F, 1.0F}
- 'Dim mat_shininess() As Single = {10.0F}
- 'Dim light_position() As Single = {10.0F, 10.0F, 0.0F, 0.0F}
- 'Dim light_ambient() As Single = {0.0F, 0.0F, 0.0F, 1.0F}
- '
- 'GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular)
- 'GL.Material(MaterialFace.Front, MaterialParameter.Shininess, mat_shininess)
- 'GL.Light(LightName.Light1, LightParameter.Position, light_position)
- 'GL.Light(LightName.Light1, LightParameter.Ambient, light_ambient)
- 'GL.Light(LightName.Light1, LightParameter.Diffuse, mat_specular)
- 'GL.Light(LightName.Light0, LightParameter.Ambient, New Single() {0.0, 0.0, 0.0, 1.0})
- 'GL.Light(LightName.Light0, LightParameter.Diffuse, New Single() {1.0, 1.0, 0.0, 1.0})
- 'GL.Light(LightName.Light0, LightParameter.Specular, New Single() {1.0, 1.0, 1.0, 1.0})
- 'GL.Light(LightName.Light0, LightParameter.Position, New Single() {10, 10, 10, 1.0})
- 'GL.Light(LightName.Light0, LightParameter.SpotDirection, New Single() {1, 1, 1})
- 'GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, New Single() {1.5})
- 'GL.Light(LightName.Light0, LightParameter.LinearAttenuation, New Single() {0.5})
- 'GL.Light(LightName.Light0, LightParameter.QuadraticAttenuation, New Single() {0.2})
- 'GL.LightModel(LightModelParameter.LightModelAmbient, New Single() {0.2, 0.2, 0.2, 1.0})
- 'GL.LightModel(LightModelParameter.LightModelLocalViewer, New Single() {1.0})
- 'GL.Material(MaterialFace.Front, MaterialParameter.AmbientAndDiffuse, New Single() {1.0, 1.0, 1.0, 1.0})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Specular, New Single() {1.0, 1.0, 1.0, 1.0})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Shininess, New Single() {50.0})
- 'GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, New Single() {1.0, 1.0, 0.0, 1.0})
- Me.WindowState = WindowState.Maximized
- 'GL.Enable(EnableCap.Fog)
- GL.Fog(FogParameter.FogColor, {1.0F, 1.0F, 1.0F})
- GL.Fog(FogParameter.FogMode, FogMode.Linear)
- GL.Fog(FogParameter.FogDensity, 1.0F)
- GL.Fog(FogParameter.FogStart, 8)
- GL.Fog(FogParameter.FogEnd, 128)
- GL.Hint(HintTarget.FogHint, HintMode.Fastest)
- GL.ClearColor(New Color4(0.5F, 0.5F, 0.5F, 1.0F))
- GL.PointSize(4)
- MyBase.OnLoad(e)
- End Sub
- Protected Overrides Sub OnUnload(e As System.EventArgs)
- MyBase.OnUnload(e)
- End Sub
- Protected Overrides Sub OnResize(e As System.EventArgs)
- ScreenRatioW = Width / Height
- ScreenRatioH = Height / Width
- Perspective = Matrix4.CreatePerspectiveFieldOfView(1, ClientRectangle.Width / ClientRectangle.Height, 0.1, 8192)
- GL.Viewport(ClientRectangle.Size)
- MyBase.OnResize(e)
- End Sub
- Dim Cnt As Integer = 0
- Sub Draw()
- DrawQuadArrayY(32, -15, New Vector3(1, 1, 1))
- DrawQuadArrayY(32, -10, New Vector3(1, 0, 0))
- DrawQuadArrayY(32, -5, New Vector3(0, 1, 0))
- DrawQuadArrayY(32, 0, New Vector3(0, 0, 1))
- DrawQuadArrayY(32, 5, New Vector3(1, 1, 0))
- DrawQuadArrayY(32, 10, New Vector3(1, 1, 1))
- GL.Color3(Color.Black)
- GL.Begin(PrimitiveType.Quads)
- GL.Vertex3(4096, 4096, 4096)
- GL.Vertex3(4096, -4096, 4096)
- GL.Vertex3(4096, -4096, -4096)
- GL.Vertex3(4096, 4096, -4096)
- GL.End()
- Cnt += 1
- End Sub
- Sub DrawHud()
- End Sub
- Protected Overrides Sub OnKeyDown(e As OpenTK.Input.KeyboardKeyEventArgs)
- If e.Key = Input.Key.W Then
- Keys(0) = True
- End If
- If e.Key = Input.Key.A Then
- Keys(1) = True
- End If
- If e.Key = Input.Key.S Then
- Keys(2) = True
- End If
- If e.Key = Input.Key.D Then
- Keys(3) = True
- End If
- If e.Key = Input.Key.LShift Then
- Keys(4) = True
- End If
- If e.Key = Input.Key.Space Then
- Keys(5) = True
- End If
- If e.Key = Input.Key.F2 Then
- GrabScreenshot().Save("C:\users\coskun\desktop\" & Cnt & ".png")
- End If
- MyBase.OnKeyDown(e)
- End Sub
- Protected Overrides Sub OnKeyup(e As OpenTK.Input.KeyboardKeyEventArgs)
- If e.Key = Input.Key.W Then
- Keys(0) = False
- End If
- If e.Key = Input.Key.A Then
- Keys(1) = False
- End If
- If e.Key = Input.Key.S Then
- Keys(2) = False
- End If
- If e.Key = Input.Key.D Then
- Keys(3) = False
- End If
- If e.Key = Input.Key.LShift Then
- Keys(4) = False
- End If
- If e.Key = Input.Key.Space Then
- Keys(5) = False
- End If
- MyBase.OnKeyUp(e)
- End Sub
- Private Sub DrawBall(Position As Vector3, Size As Double, PartsWidth As Integer, PartsHigh As Integer)
- Dim X As Integer
- 'Circle Count
- Dim CC As Integer = PartsWidth
- Dim Tmp As Vector3
- For I As Integer = -PartsHigh / 2 - 1 To PartsHigh / 2
- GL.Begin(PrimitiveType.Quads)
- For O As Integer = 0 To CC - 1
- 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)
- DirectionalLight.Color3(L, Tmp)
- GL.Vertex3(Tmp)
- O += 1
- 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)
- 'Light.Color3(L, Tmp)
- GL.Vertex3(Tmp)
- X = I + 1
- 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)
- 'Light.Color3(L, Tmp)
- GL.Vertex3(Tmp)
- O -= 1
- 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)
- 'Light.Color3(L, Tmp)
- GL.Vertex3(Tmp)
- Next
- GL.End()
- Next
- End Sub
- Private Sub DrawPointArray(Size As Integer)
- Dim HalfSize As Single = Size / 2
- GL.Begin(PrimitiveType.Points)
- For X As Integer = -HalfSize To HalfSize
- For Y As Integer = -HalfSize To HalfSize
- For Z As Integer = -HalfSize To HalfSize
- DirectionalLight.Color3(L, New Vector3(X, Y, Z))
- If GetAngleFromAxis(New Vector3(0, 0, 0), New Vector3(0, 1, 0), New Vector3(X, Y, Z)) * 2 <= 20 Then
- 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)
- GL.Vertex3(X, Y, Z)
- End If
- Next
- Next
- Next
- GL.End()
- End Sub
- Private Sub DrawQuadArrayYLPQ(Size As Integer, HS As Integer)
- Dim HalfSize As Single = Size / 2
- GL.Begin(PrimitiveType.Quads)
- For X As Integer = -HalfSize To HalfSize - 1
- For Y As Integer = -HalfSize To HalfSize - 1
- DirectionalLight.Color3(L, New Vector3(X, 0, Y))
- GL.Vertex3(X, 0 + HS, Y)
- GL.Vertex3(X + 1, 0 + HS, Y)
- GL.Vertex3(X + 1, 0 + HS, Y + 1)
- GL.Vertex3(X, 0 + HS, Y + 1)
- Next
- Next
- For X As Integer = -HalfSize To HalfSize - 1
- For Y As Integer = -HalfSize To HalfSize - 1
- DirectionalLight.Color3(L, New Vector3(X, 10, Y))
- GL.Vertex3(X, 10 + HS, Y)
- GL.Vertex3(X + 1, 10 + HS, Y)
- GL.Vertex3(X + 1, 10 + HS, Y + 1)
- GL.Vertex3(X, 10 + HS, Y + 1)
- Next
- Next
- For X As Integer = -HalfSize To HalfSize - 1
- For Y As Integer = -HalfSize To HalfSize - 1
- DirectionalLight.Color3(L, New Vector3(X, -10, Y))
- GL.Vertex3(X, -10 + HS, Y)
- GL.Vertex3(X + 1, -10 + HS, Y)
- GL.Vertex3(X + 1, -10 + HS, Y + 1)
- GL.Vertex3(X, -10 + HS, Y + 1)
- Next
- Next
- GL.End()
- End Sub
- Private Sub DrawQuadArrayY(Size As Integer, YPos As Integer, Color As Vector3)
- Dim HalfSize As Single = Size / 2
- GL.Begin(PrimitiveType.Quads)
- For X As Integer = -HalfSize To HalfSize - 1
- For Y As Integer = -HalfSize To HalfSize - 1
- DirectionalLight.Color3(L, New Vector3(X, YPos, Y), Color)
- GL.Vertex3(X, YPos, Y)
- DirectionalLight.Color3(L, New Vector3(X + 1, YPos, Y), Color)
- GL.Vertex3(X + 1, YPos, Y)
- DirectionalLight.Color3(L, New Vector3(X + 1, YPos, Y + 1), Color)
- GL.Vertex3(X + 1, YPos, Y + 1)
- DirectionalLight.Color3(L, New Vector3(X, YPos, Y + 1), Color)
- GL.Vertex3(X, YPos, Y + 1)
- Next
- Next
- GL.End()
- End Sub
- Private Sub RandomColor()
- GL.Color3(Rnd.NextDouble)
- End Sub
- Function GrabScreenshot() As Bitmap
- If (GraphicsContext.CurrentContext Is Nothing) Then
- Throw New GraphicsContextMissingException
- End If
- Dim bmp As Bitmap = New Bitmap(ClientSize.Width, ClientSize.Height)
- Dim data As System.Drawing.Imaging.BitmapData = bmp.LockBits(ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
- GL.ReadPixels(0, 0, ClientSize.Width, ClientSize.Height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0)
- bmp.UnlockBits(data)
- bmp.RotateFlip(RotateFlipType.RotateNoneFlipY)
- Return bmp
- End Function
- Private Function IsInCone(Angle As Double, Direction As Vector3, Point As Vector3) As Boolean
- Angle *= DegToRad
- Direction.Normalize()
- 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
- End Function
- Private Shared Function GetAngleFromAxis(Position As Vector3, Axis As Vector3, Point As Vector3) As Single
- Point -= Position
- Axis.Normalize()
- 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
- End Function
- End Class
So, das ist alles.
DLL im anhang.
(und Test-Programm auch)