Advertisement
Robomatics

Texturing

Jun 2nd, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.99 KB | None | 0 0
  1.     Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
  2.  
  3.         GL.Clear(ClearBufferMask.ColorBufferBit)
  4.         GL.Clear(ClearBufferMask.DepthBufferBit)
  5.  
  6.         Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, GlControl1.Width / GlControl1.Height, 1, 100) 'Setup Perspective
  7.         Dim lookat As Matrix4 = Matrix4.LookAt(0, 0, 4, 0, 0, 0, 0, 1, 0) 'Setup camera
  8.         GL.MatrixMode(MatrixMode.Projection) 'Load Perspective
  9.         GL.LoadIdentity()
  10.         GL.LoadMatrix(perspective)
  11.         GL.MatrixMode(MatrixMode.Modelview) 'Load Camera
  12.         GL.LoadIdentity()
  13.         GL.LoadMatrix(lookat)
  14.         GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) 'Size of window
  15.         GL.Enable(EnableCap.DepthTest) 'Enable correct Z Drawings
  16.         GL.DepthFunc(DepthFunction.Less) 'Enable correct Z Drawings
  17.         GL.Enable(EnableCap.Blend)
  18.         GL.Enable(EnableCap.Texture2D)
  19.  
  20.         'GL.Rotate(eye(0), 0, eye(1), eye(2))
  21.         If mousing = True Then
  22.             TranslateStart(0) = (MousePosition.X - mousestart.X) / 100
  23.             TranslateStart(1) = (mousestart.Y - MousePosition.Y) / 100
  24.             GL.Translate(TranslateStart(0) + TranslateEnd(0), TranslateStart(1) + TranslateEnd(1), 0)
  25.         End If
  26.  
  27.         'Start Tile Find
  28.         Dim starttile As Integer = 0
  29.         Dim starttilefound As Boolean = False
  30.         For i = 0 To TileTypes.Count - 1
  31.             If TileTypes(i).Name = "Starter Tile" Then
  32.                 starttilefound = True
  33.                 starttile = i
  34.             End If
  35.         Next
  36.         If starttilefound = False Then
  37.             MessageBox.Show("No Start Tile Found, make tile named 'Starter Tile")
  38.             Return
  39.             Timer1.Enabled = False
  40.         End If
  41.  
  42.         loadtexture(TileTypes(starttile).PicPath, TileTypes(starttile).TextureCoord)
  43.         GL.Begin(BeginMode.Quads)
  44.         GL.Color3(Color.White)
  45.         GL.TexCoord2(1, 0)
  46.         GL.Vertex2(0.5, 0.5)
  47.         GL.TexCoord2(0, 0)
  48.         GL.Vertex2(-0.5, 0.5)
  49.         GL.TexCoord2(0, 1)
  50.         GL.Vertex2(-0.5, -0.5)
  51.         GL.TexCoord2(1, 1)
  52.         GL.Vertex2(0.5, -0.5)
  53.         GL.End()
  54.  
  55.         For i = 0 To Tiles.Count - 1
  56.             If Tiles(i).Used = True Then
  57.                 loadtexture(Tiles(i).PicPath, Tiles(i).TextureCoord)
  58.                 GL.Begin(BeginMode.Quads)
  59.                 GL.TexCoord2(1, 0)
  60.                 GL.Vertex2(0.5 + Tiles(i).GamePos.X, 0.5 + Tiles(i).GamePos.Y)
  61.                 GL.TexCoord2(0, 0)
  62.                 GL.Vertex2(-0.5 + Tiles(i).GamePos.X, 0.5 + Tiles(i).GamePos.Y)
  63.                 GL.TexCoord2(0, 1)
  64.                 GL.Vertex2(-0.5 + Tiles(i).GamePos.X, -0.5 + Tiles(i).GamePos.Y)
  65.                 GL.TexCoord2(1, 1)
  66.                 GL.Vertex2(0.5 + Tiles(i).GamePos.X, -0.5 + Tiles(i).GamePos.Y)
  67.                 GL.End()
  68.             End If
  69.         Next
  70.  
  71.         'Finally...
  72.         GraphicsContext.CurrentContext.VSync = True 'Caps frame rate as to not over run GPU
  73.         GlControl1.SwapBuffers() 'Takes from the 'GL' and puts into control
  74.     End Sub
  75.     'GL Load Texture
  76.     Private Sub loadtexture(ByVal srcpicpath As String, ByVal texturecoord() As Single)
  77.         Dim srcpic As Bitmap
  78.         Dim srcpicavailable As Boolean = False
  79.         If srcpicpath <> "" Then
  80.             srcpic = New Bitmap(srcpicpath)
  81.             srcpicavailable = True
  82.         Else
  83.             srcpic = New Bitmap(100, 100)
  84.         End If
  85.         Dim TextureID As Integer
  86.         Dim bitmap As New Bitmap(100, 100)
  87.         Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(bitmap)
  88.  
  89.         If srcpicavailable = True Then
  90.         g.DrawImage(srcpic, New Rectangle(0, 0, bitmap.Width, bitmap.Height),
  91.                     New Rectangle(srcpic.Width * texturecoord(0),
  92.                                   srcpic.Height * texturecoord(1),
  93.                                   srcpic.Width * texturecoord(2),
  94.                                   srcpic.Height * texturecoord(3)),
  95.                               GraphicsUnit.Pixel)
  96.         Else
  97.             g.FillRectangle(Brushes.White, New Rectangle(0, 0, bitmap.Width, bitmap.Height))
  98.         End If
  99.  
  100.         GL.GenTextures(1, TextureID)
  101.         GL.BindTexture(TextureTarget.Texture2D, TextureID)
  102.  
  103.         Dim Data As System.Drawing.Imaging.BitmapData = bitmap.LockBits(New System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
  104.             Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  105.  
  106.         GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Data.Width, Data.Height, 0,
  107.             OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, Data.Scan0)
  108.         bitmap.UnlockBits(Data)
  109.  
  110.         GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureMinFilter.Linear)
  111.         GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureMagFilter.Linear)
  112.  
  113.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement