VB.net OpenTK SimpleExample 1
Bin: Here
Source Code: Here
there will be OpenTK.DLL in Bin, on the Source Code.
' MainWindow.
Imports System.Windows.Forms
Imports OpenTK
Imports OpenTK.Input
Imports OpenTK.Graphics.OpenGL
Imports QuickFont
Public Class MainWindow : Inherits GameWindow
Private EntityArray() As Entity
Private PlayerEntity As New Player
Private GraphicsFont As QuickFont.QFont
Private FPS As Single = 0
Private Increment As Single = 0.001
Private Velocity As Single = 0
Private Lrq As Single = 1.2
Sub New()
MyBase.New(800, 600, OpenTK.Graphics.GraphicsMode.Default, "Entity Array[1]")
CreateRoom(10, 1)
End Sub
Protected Overrides Sub OnResize(e As EventArgs)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
Dim m4 As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45), CSng(Width / Height
), 1, 1000)
GL.MultMatrix(m4)
GL.Viewport(0, 0, Width, Height)
End Sub
Private Sub CreateRoom(s As Single, blockSize As Single)
ReDim EntityArray(s * s - 1)
Dim i As Single = 0
Dim l As Integer = 0
Dim px As Single = 0
Dim pz As Single = 0
For x As Single = 0 To s - 1
If l Mod 2 Then
For z As Single = 0 To s - 1
If i Mod 2 Then
EntityArray(i) = New Entity(New Vector3(px, 0, pz), 0, blockSize / 2, TopFace, True, Color.Black)
Else
EntityArray(i) = New Entity(New Vector3(px, 0, pz), 0, blockSize / 2, TopFace, True, Color.White)
End If
i += 1
pz += blockSize / 2
Next
l = 0
Else
For z As Single = 0 To s - 1
If i Mod 2 Then
EntityArray(i) = New Entity(New Vector3(px, 0, pz), 0, blockSize / 2, TopFace, True, Color.White)
Else
EntityArray(i) = New Entity(New Vector3(px, 0, pz), 0, blockSize / 2, TopFace, True, Color.Black)
End If
i += 1
pz += blockSize / 2
Next
l = 1
End If
px += blockSize / 2
pz = 0
Next
End Sub
Protected Overrides Sub OnRenderFrame(e As FrameEventArgs)
GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadIdentity()
DrawScene()
Dim pString As String = "No"
If PlayerEntity.IsGodMode Then
pString = "Yes"
End If
QFont.Begin()
GL.PushMatrix()
GL.Translate(5.0F, 5, 0.0F)
GraphicsFont.Print("x: " & PlayerEntity.Position.X & vbCrLf &
"y: " & PlayerEntity.Position.Y & vbCrLf &
"z: " & PlayerEntity.Position.Z & vbCrLf &
"pitch: " & PlayerEntity.Pitch & vbCrLf &
"yaw: " & PlayerEntity.Facing & vbCrLf &
"fps: " & FPS & vbCrLf &
"velocity: " & Velocity & vbCrLf &
"GodMode: " & pString, QFontAlignment.Left)
GL.PopMatrix()
QFont.End()
GL.Disable(EnableCap.Texture2D)
GetFPS(e.Time)
SwapBuffers()
End Sub
Private Sub DrawScene()
GL.Rotate(-PlayerEntity.Pitch, 1, 0, 0)
GL.Rotate(-PlayerEntity.Facing, 0, 1, 0)
GL.Translate(-PlayerEntity.Position)
For i = 0 To EntityArray.Length - 1
EntityArray(i).Display()
Next
End Sub
Protected Overrides Sub OnLoad(e As EventArgs)
GL.Enable(EnableCap.DepthTest)
GL.ClearColor(Color.SkyBlue)
CenterMouse()
Cursor.Hide()
GraphicsFont = New QFont(New Font("Arial", 11))
End Sub
Private Sub ChangePlane(ByVal bitWiseValue As Integer)
For i = 0 To EntityArray.Length - 1
EntityArray(i).PlanesVisible = bitWiseValue
Next
End Sub
Private Sub DoGravity(ByVal Time As Double)
Dim HitFloor As Boolean = False
For i = 0 To EntityArray.Length - 1
If PlayerEntity.Position.Y - 10 >= EntityArray(i).Location.Y And PlayerEntity.Position.Y - 10 <= EntityArray(i).Location.Y + EntityArray(i).Size And PlayerEntity.Position.Z >= EntityArray(i).Location.Z And PlayerEntity.Position.Z <= EntityArray(i).Location.Z + EntityArray(i).Size And PlayerEntity.Position.X >= EntityArray(i).Location.X And PlayerEntity.Position.X <= EntityArray(i).Location.X + EntityArray(i).Size Then
HitFloor = True
Exit For
End If
Next
If HitFloor = False Then
Velocity += Increment
PlayerEntity.Position -= New Vector3d(0, Math.Round((Lrq * Velocity) * 100 * Time, 4), 0)
Else
Velocity = 0
End If
End Sub
Private gAsKey As Boolean = False
Private lastVec As New Vector3d(0, 0, 0)
Protected Overrides Sub OnUpdateFrame(e As FrameEventArgs)
If Keyboard(Input.Key.Escape) Then
Me.Exit()
End If
If Not PlayerEntity.IsGodMode Then
DoGravity(e.Time)
End If
ProcessKeys(e.Time)
End Sub
Private Sub ProcessKeys(time As Double)
If Keyboard(Input.Key.W) Then
PlayerEntity.Position += PlayerEntity.Forwards * 100 * time
End If
If Keyboard(Input.Key.S) Then
PlayerEntity.Position -= PlayerEntity.Forwards * 100 * time
End If
If Keyboard(Input.Key.A) Then
PlayerEntity.Position += PlayerEntity.Sideways * 100 * time
End If
If Keyboard(Input.Key.D) Then
PlayerEntity.Position -= PlayerEntity.Sideways * 100 * time
End If
If Keyboard(Input.Key.F) Then
ChangePlane(FullFace)
End If
If Keyboard(Input.Key.T) Then
ChangePlane(TopFace)
End If
PlayerEntity.Facing -= (Mouse.X - (Width / 2)) * 0.33
PlayerEntity.Pitch -= (Mouse.Y - (Height / 2)) * 0.33
If Keyboard(Input.Key.Space) Then
PlayerEntity.Position += Vector3d.UnitY * 100 * time
End If
If PlayerEntity.IsGodMode AndAlso Keyboard(Input.Key.ShiftLeft) Then
PlayerEntity.Position -= Vector3d.UnitY * 100 * time
End If
If Not Keyboard(Input.Key.G) And gAsKey = True Then
PlayerEntity.IsGodMode = Not PlayerEntity.IsGodMode
gAsKey = False
Velocity = 0
ElseIf Keyboard(Input.Key.G) Then
gAsKey = True
ElseIf gAsKey = True Then
gAsKey = False
End If
CenterMouse()
lastVec = PlayerEntity.Position
End Sub
Private Sub CenterMouse()
Dim p As New Point(Width / 2, Height / 2)
Cursor.Position = PointToScreen(p)
End Sub
Private lastFPSTime As Double
Private TotalFrames As Single = 0
Private Sub GetFPS(t As Double)
lastFPSTime += t
If lastFPSTime < 1.0 Then
TotalFrames += 1
Else
FPS = TotalFrames
lastFPSTime = 0.0
TotalFrames = 0.0
End If
End Sub
End Class
No comments:
Post a Comment