我想在場景中繪製對象之前顯示背景圖像。當我的opengl窗口加載它時,顯示重複的背景圖像。我做了一些測試,如果我保持我的視口X,Y尺寸爲0和1,則背景圖像顯示正確。重複背景圖像
Private _vXmin As Double = 0
Private _vXmax As Double = 1
Private _vYmin As Double = 0
Private _vYmax As Double = 1
Private _vZmin As Double = 0
Private _vZmax As Double = 1
Private Sub SetupViewport()
Dim w As Integer = Me.Width
Dim h As Integer = Me.Height
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
GL.Ortho(_vXmin, _vXmax, _vYmin, _vYmax, _vZmin, _vZmax)
GL.Viewport(0, 0, w, h)
End Sub
問題是我的繪圖對象在這個狹窄的視圖空間之外。所以我調整我的視口的東西沿着0-> 2000線,以適應所有的頂點更多...
_vXmin = 0
_vXmax = 2000
_vYmin = 0
_vYmax = 2000
_vZmin = -1
_vZmax = 1
因此,背景圖像是重複的,因爲它要保持規模0-> 1。這有什麼好處嗎?或者我從根本上通過將視口更改爲大於1來不正確地繪製對象?我是否需要將所有頂點都改爲在-1到1之間?
編輯#1
我的代碼來設置背景圖像。僅供參考,我在某個地方上線了。我不是百分之百在這裏發生了什麼。
' Set Background Image
Private Function SET_BACKGROUND_IMG() As Integer
Dim myImg As String = "C:\Users\00835182\Documents\Programs\OpenGL\_background_color.jpg"
Dim id As Integer = GL.GenTexture
GL.BindTexture(TextureTarget.Texture2D, id)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureMinFilter.Linear)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureMagFilter.Linear)
Dim bmp = New Bitmap(myImg)
Dim bmp_data As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb)
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0)
bmp.UnlockBits(bmp_data)
Return id
End Function
最後,我的代碼來渲染背景圖像。
' Render Background
Private Sub RENDER_BACKGROUND()
GL.Enable(EnableCap.Texture2D)
GL.BindTexture(TextureTarget.Texture2D, _img)
GL.Begin(PrimitiveType.Quads)
GL.TexCoord2(_vXmin, _vYmin)
GL.Vertex2(_vXmin, _vYmin)
GL.TexCoord2(_vXmax, _vYmin)
GL.Vertex2(_vXmax, _vYmin)
GL.TexCoord2(_vXmax, _vYmax)
GL.Vertex2(_vXmax, _vYmax)
GL.TexCoord2(_vXmin, _vYmax)
GL.Vertex2(_vXmin, _vYmax)
GL.End()
GL.BindTexture(TextureTarget.Texture2D, 0)
GL.Disable(EnableCap.Texture2D)
End Sub
你是如何繪製背景的?你把它渲染在四邊形上?你有沒有定義這個quad的uv座標? – Zouch