Advertisement
Robomatics

OpenTK/OpenGL 3D Continued 1

Jan 5th, 2013
1,825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.91 KB | None | 0 0
  1. Imports OpenTK
  2. Imports OpenTK.Graphics
  3. Imports OpenTK.Graphics.OpenGL
  4.  
  5. Public Class Form1
  6.  
  7.     Dim mousepos As Point
  8.  
  9.     Private Sub GlControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GlControl1.Load
  10.         'Control is loaded, set back color
  11.         GL.ClearColor(Color.Black)
  12.     End Sub
  13.  
  14.     Private Sub GlControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
  15.         mousepos = New Point(e.X, e.Y)
  16.         GlControl1.Invalidate()
  17.     End Sub
  18.  
  19.     Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
  20.         'First Clear Buffers
  21.         GL.Clear(ClearBufferMask.ColorBufferBit)
  22.         GL.Clear(ClearBufferMask.DepthBufferBit)
  23.  
  24.         'Basic Setup for viewing
  25.         Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000) 'Setup Perspective
  26.         Dim lookat As Matrix4 = Matrix4.LookAt(100, 20, 0, 0, 0, 0, 0, 1, 0) 'Setup camera
  27.         GL.MatrixMode(MatrixMode.Projection) 'Load Perspective
  28.         GL.LoadIdentity()
  29.         GL.LoadMatrix(perspective)
  30.         GL.MatrixMode(MatrixMode.Modelview) 'Load Camera
  31.         GL.LoadIdentity()
  32.         GL.LoadMatrix(lookat)
  33.         GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) 'Size of window
  34.         GL.Enable(EnableCap.DepthTest) 'Enable correct Z Drawings
  35.         GL.DepthFunc(DepthFunction.Less) 'Enable correct Z Drawings
  36.  
  37.         GL.Rotate(mousepos.X, 0, 1, 0)
  38.  
  39.         GL.PushMatrix()
  40.  
  41.         'Rotating
  42.         GL.Rotate(NumericUpDown1.Value, 0, 0, 1)
  43.         GL.Rotate(NumericUpDown2.Value, 0, 1, 0)
  44.         GL.Translate(New Vector3(0, mousepos.Y, 0))
  45.  
  46.         'Draw pyramid, Y is up, Z is twards you, X is left and right
  47.         'Vertex goes (X,Y,Z)
  48.         GL.Begin(BeginMode.Triangles)
  49.         'Face 1
  50.         GL.Color3(Color.Red)
  51.         GL.Vertex3(50, 0, 0)
  52.         GL.Color3(Color.White)
  53.         GL.Vertex3(0, 25, 0)
  54.         GL.Color3(Color.Blue)
  55.         GL.Vertex3(0, 0, 50)
  56.         'Face 2
  57.         GL.Color3(Color.Green)
  58.         GL.Vertex3(-50, 0, 0)
  59.         GL.Color3(Color.White)
  60.         GL.Vertex3(0, 25, 0)
  61.         GL.Color3(Color.Blue)
  62.         GL.Vertex3(0, 0, 50)
  63.         'Face 3
  64.         GL.Color3(Color.Red)
  65.         GL.Vertex3(50, 0, 0)
  66.         GL.Color3(Color.White)
  67.         GL.Vertex3(0, 25, 0)
  68.         GL.Color3(Color.Blue)
  69.         GL.Vertex3(0, 0, -50)
  70.         'Face 4
  71.         GL.Color3(Color.Green)
  72.         GL.Vertex3(-50, 0, 0)
  73.         GL.Color3(Color.White)
  74.         GL.Vertex3(0, 25, 0)
  75.         GL.Color3(Color.Blue)
  76.         GL.Vertex3(0, 0, -50)
  77.  
  78.         'Finish the begin mode with "end"
  79.         GL.End()
  80.  
  81.         GL.PopMatrix()
  82.  
  83.         'Rotating
  84.         GL.Rotate(NumericUpDown3.Value, 0, 0, 1)
  85.         GL.Rotate(NumericUpDown4.Value, 0, 1, 0)
  86.  
  87.         'Draw pyramid, Y is up, Z is twards you, X is left and right
  88.         'Vertex goes (X,Y,Z)
  89.         GL.Begin(BeginMode.Triangles)
  90.         'Face 1
  91.         GL.Color3(Color.Red)
  92.         GL.Vertex3(50, 0, 0)
  93.         GL.Color3(Color.White)
  94.         GL.Vertex3(0, -25, 0)
  95.         GL.Color3(Color.Blue)
  96.         GL.Vertex3(0, 0, 50)
  97.         'Face 2
  98.         GL.Color3(Color.Green)
  99.         GL.Vertex3(-50, 0, 0)
  100.         GL.Color3(Color.White)
  101.         GL.Vertex3(0, -25, 0)
  102.         GL.Color3(Color.Blue)
  103.         GL.Vertex3(0, 0, 50)
  104.         'Face 3
  105.         GL.Color3(Color.Red)
  106.         GL.Vertex3(50, 0, 0)
  107.         GL.Color3(Color.White)
  108.         GL.Vertex3(0, -25, 0)
  109.         GL.Color3(Color.Blue)
  110.         GL.Vertex3(0, 0, -50)
  111.         'Face 4
  112.         GL.Color3(Color.Green)
  113.         GL.Vertex3(-50, 0, 0)
  114.         GL.Color3(Color.White)
  115.         GL.Vertex3(0, -25, 0)
  116.         GL.Color3(Color.Blue)
  117.         GL.Vertex3(0, 0, -50)
  118.  
  119.         'Finish the begin mode with "end"
  120.         GL.End()
  121.  
  122.         'Finally...
  123.         GraphicsContext.CurrentContext.VSync = True 'Caps frame rate as to not over run GPU
  124.         GlControl1.SwapBuffers() 'Takes from the 'GL' and puts into control
  125.     End Sub
  126.  
  127.     Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
  128.         'Must be called to update the control window!
  129.         GlControl1.Invalidate()
  130.     End Sub
  131.  
  132.     Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged
  133.         'Must be called to update the control window!
  134.         GlControl1.Invalidate()
  135.     End Sub
  136.  
  137.     Private Sub NumericUpDown3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown3.ValueChanged
  138.         GlControl1.Invalidate()
  139.     End Sub
  140.  
  141.     Private Sub NumericUpDown4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown4.ValueChanged
  142.         GlControl1.Invalidate()
  143.     End Sub
  144. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement