我在VB.net中使用OpenTK,我想将纹理映射添加到我的代码中,步骤如下:
1:加载纹理:
Protected Sub LoadTexture(ByVal textureId As Integer, ByVal filename As String)
Dim bmp As New Bitmap(filename)
Dim data As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GL.BindTexture(TextureTarget.Texture2D, textureId)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
bmp.Width, bmp.Height, 0, OpenGL.PixelFormat.Bgra,
PixelType.UnsignedByte, data.Scan0)
bmp.UnlockBits(data)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureMinFilter.Linear)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureMagFilter.Linear)
End Sub
2:启用和添加:
GL.Enable(EnableCap.DepthTest)
GL.Enable(EnableCap.Texture2D)
GL.GenTextures(textures.Length, textures)
LoadTexture(textures(0), "bricks.bmp")
3:绘画(加载GlControl1,在GlControl1 Paint中):
GL.PushMatrix()
GL.BindTexture(TextureTarget.Texture2D, textures(0))
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureMinFilter.Linear)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureMagFilter.Linear)
GL.Begin(BeginMode.Quads)
GL.Color3(Color.White)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 0, 200)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 100, 200)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 100, -200)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 0, -200)
GL.End()
GL.PopMatrix()
当我运行我的代码时,我收到此错误:argument Exception was unhandled
此行Dim bmp As New Bitmap(filename)
。
异常是(参数无效),就像图像一样
Try And Catch exception
注意:我像这样在根文件中添加了图像
the Image Place
我在c ++中使用了opengl和texture但是我没有这个问题,我的错误可以在加载纹理函数或在其他地方
所有代码:
Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports System.Drawing.Imaging
Imports System.Drawing
Public Class Form2
Dim loaded As Boolean = False
Protected textures(2) As Integer
Protected Sub LoadTexture(ByVal textureId As Integer, ByVal filename As String)
Try
Dim bmp As New Bitmap(filename)
Dim data As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GL.BindTexture(TextureTarget.Texture2D, textureId)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
bmp.Width, bmp.Height, 0, OpenGL.PixelFormat.Bgra,
PixelType.UnsignedByte, data.Scan0)
bmp.UnlockBits(data)
Catch ex As Exception
MsgBox(ex.Message)
End Try
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureMinFilter.Linear)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureMagFilter.Linear)
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
GL.ClearColor(0.5, 0.5, 0.6, 0)
GL.Enable(EnableCap.DepthTest)
GL.Enable(EnableCap.Texture2D)
LoadTextures()
End Sub
Protected Sub LoadTextures()
GL.GenTextures(textures.Length, textures)
LoadTexture(textures(0), "bricks.bmp")
End Sub
Private Sub GlControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GlControl1.Load
GL.ClearColor(Color.Black)
loaded = True
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
GlControl1.Invalidate()
End Sub
Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged
GlControl1.Invalidate()
End Sub
Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
GL.Clear(ClearBufferMask.ColorBufferBit)
GL.Clear(ClearBufferMask.DepthBufferBit)
Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000)
Dim lookat As Matrix4 = Matrix4.LookAt(10, 0, 0, 300, 0, 0, 0, 1, 0)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
GL.LoadMatrix(perspective)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadIdentity()
GL.LoadMatrix(lookat)
GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height)
GL.Enable(EnableCap.DepthTest)
GL.DepthFunc(DepthFunction.Less)
GL.Rotate(NumericUpDown1.Value, 0, 0, 1)
GL.Rotate(NumericUpDown2.Value, 0, 1, 0)
GL.PushMatrix()
GL.Begin(BeginMode.Quads)
GL.Color3(Color.White)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 0, 200)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 100, 200)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 100, -200)
GL.TexCoord2(0.0, 1.0)
GL.Vertex3(200, 0, -200)
GL.End()
GL.PopMatrix()
GraphicsContext.CurrentContext.VSync = True
GlControl1.SwapBuffers()
End Sub
End Class