2017-05-27 87 views
1

存储我试图做纹理在OpenGL 3.3(OpenTK)在VB.NET。 OutputOpenTK - GLSL - BufferData的纹理坐标分别

,其中实际图像是:

Original image

的代码是:

Imports OpenTK 
Imports OpenTK.Graphics.OpenGL 
Imports System.IO 
Imports System.Drawing.Imaging 

Public Class Form1 



    Private vertices() As Vector3 = {New Vector3(0.5F, 0.5F, 0.0F), 
            New Vector3(0.5F, -0.5F, 0.0F), 
            New Vector3(-0.5F, -0.5F, 0.0F), 
            New Vector3(-0.5F, 0.5F, 0.0F)} 

    Private texcoords() As Vector2 = {New Vector2(1.0F, 1.0F), 
            New Vector2(1.0F, 0.0F), 
            New Vector2(0.0F, 0.0F), 
            New Vector2(0.0F, 1.0F)} 

    Private indices() As UInteger = {0, 1, 3, 
            1, 2, 3} 

    Private VBO, VAO, EBO As Integer 

    Private vert_shader_source, frag_shader_source As String 
    Private vertex_shader, fragment_shader, shader_program As Integer 

    '-------------- 
    Private texture As Integer 
    Private texture_bmp As New Bitmap("Textures/tex4.jpg") 
    Private TBO As Integer 


    Private Sub load_texture(ByRef tex_bmp As Bitmap, ByRef texture_index As Integer) 

     GL.Enable(EnableCap.Texture2D) 

     GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest) 

     GL.GenTextures(1, texture_index) 
     GL.BindTexture(TextureTarget.Texture2D, texture_index) 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, CInt(TextureMinFilter.Linear)) 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, CInt(TextureMagFilter.Linear)) 

     Dim data As BitmapData = tex_bmp.LockBits(New System.Drawing.Rectangle(0, 0, tex_bmp.Width, tex_bmp.Height), ImageLockMode.[ReadOnly], System.Drawing.Imaging.PixelFormat.Format32bppArgb) 

     GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, 
    OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0) 

     tex_bmp.UnlockBits(data) 

    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     vert_shader_source = openShader("Shaders/vertex.glsl") 
     frag_shader_source = openShader("Shaders/fragment.glsl") 

     createShaders() 
     create_handles() 

     createVAO() 

     '--------------- 
     load_texture(texture_bmp, texture) 

    End Sub 

    Sub create_handles() 

     VAO = GL.GenVertexArray() 
     VBO = GL.GenBuffer() 
     EBO = GL.GenBuffer() 

     '------------- 
     TBO = GL.GenBuffer() 

    End Sub 

    Private Sub GlControl1_Load(sender As Object, e As EventArgs) Handles GlControl1.Load 

    End Sub 

    Sub copy_to_gpu() 

     GL.BindBuffer(BufferTarget.ArrayBuffer, VBO) 
     GL.BufferData(Of Vector3)(BufferTarget.ArrayBuffer, New IntPtr(vertices.Length * Vector3.SizeInBytes), 
            vertices, BufferUsageHint.StaticDraw) 

     GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBO) 
     GL.BufferData(BufferTarget.ElementArrayBuffer, New IntPtr(indices.Length * Len(New UInteger)), 
            indices, BufferUsageHint.StaticDraw) 


     '----------- 
     GL.BindBuffer(BufferTarget.TextureBuffer, TBO) 
     GL.BufferData(Of Vector2)(BufferTarget.TextureBuffer, New IntPtr(texcoords.Length * Vector2.SizeInBytes), 
            texcoords, BufferUsageHint.StaticDraw) 

    End Sub 

    Sub link_vertex_attribs() 

     GL.EnableVertexAttribArray(0) 
     GL.BindBuffer(BufferTarget.ArrayBuffer, VBO) 
     GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, False, Vector3.SizeInBytes, 0) 

     '------------- 
     GL.EnableVertexAttribArray(1) 
     GL.BindBuffer(BufferTarget.TextureBuffer, TBO) 
     GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, False, Vector3.SizeInBytes, 0) 

    End Sub 


    Sub createVAO() 

     GL.BindVertexArray(VAO) 

     copy_to_gpu() 
     link_vertex_attribs() 

     GL.BindBuffer(BufferTarget.ArrayBuffer, 0) 
     GL.BindVertexArray(0) 

    End Sub 

    Sub createShaders() 

     'vertex shader 
     vertex_shader = GL.CreateShader(ShaderType.VertexShader) 
     GL.ShaderSource(vertex_shader, vert_shader_source) 
     GL.CompileShader(vertex_shader) 

     'fragment shader 
     fragment_shader = GL.CreateShader(ShaderType.FragmentShader) 
     GL.ShaderSource(fragment_shader, frag_shader_source) 
     GL.CompileShader(fragment_shader) 

     'create shader program 
     shader_program = GL.CreateProgram() 


     'attach shader program 
     GL.AttachShader(shader_program, vertex_shader) 
     GL.AttachShader(shader_program, fragment_shader) 


     'link shader program 
     GL.LinkProgram(shader_program) 


     GL.DeleteShader(vertex_shader) 
     GL.DeleteShader(fragment_shader) 

    End Sub 

    Function openShader(location As String) As String 

     Dim ret As String = "" 

     Try 
      ' Open the file using a stream reader. 
      Using sr As New StreamReader(location) 
       Dim line As String 
       ' Read the stream to a string and write the string to the console. 
       line = sr.ReadToEnd() 

       ret &= line 
      End Using 
     Catch e As Exception 
      MsgBox("Error Opening shader", MsgBoxStyle.Critical, "Error") 
      Me.Close() 
     End Try 

     Return ret 

    End Function 

    Private Sub GlControl1_Paint(sender As Object, e As PaintEventArgs) Handles GlControl1.Paint 

     GL.ClearColor(0.2F, 0.3F, 0.3F, 1.0F) 
     GL.Clear(ClearBufferMask.ColorBufferBit) 

     GL.UseProgram(shader_program) 

     '''''''' 

     GL.ActiveTexture(TextureUnit.Texture0) 
     GL.BindTexture(TextureTarget.Texture2D, texture) 
     GL.Uniform1(GL.GetUniformLocation(shader_program, "ourTexture"), 0) 

     '''''''' 

     GL.BindVertexArray(VAO) 

     GL.DrawElements(BeginMode.Triangles, 6, DrawElementsType.UnsignedInt, 0) 
     GL.BindVertexArray(0) 

     GlControl1.SwapBuffers() 


    End Sub 

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 

     GL.DeleteVertexArray(VAO) 
     GL.DeleteBuffer(VBO) 
     GL.DeleteBuffer(EBO) 

     GL.DeleteTexture(texture) 

    End Sub 
End Class 
但是我碰到导致下面的输出试图纹理映射到四,当一个奇怪的问题来了

我单独存储顶点数据和纹理坐标,并试图使用BufferData单独缓冲GPU。

问题似乎是BufferData无法正常工作。因为无论我分配给纹理坐标的值如何,输出都不受影响!

这里是顶点着色器:

#version 330 core 

layout (location = 0) in vec3 position; 
layout (location = 1) in vec2 texCoord; 

out vec2 TexCoord; 

void main() 
{ 
    gl_Position = vec4(position.x, position.y, position.z, 1.0); 

    TexCoord = texCoord; 
} 

片段着色器:

#version 330 core 

out vec4 color; 

in vec2 TexCoord; 
uniform sampler2D ourTexture; 

void main() 
{ 
    color = texture(ourTexture, TexCoord); 
} 

但是,如果我调整片段着色器到这一点:

color = texture(ourTexture, vec2(TexCoord.x-1.0f,1.0f-TexCoord.y)); 

它的工作原理。但纹理坐标似乎不会影响输出。

回答

1

有几件事情去错在这里

1)存储纹理坐标缓冲区是没有纹理缓存。纹理缓冲区用于处理纹理的数据,而不用于存储纹理坐标。纹理坐标是属性,类似于位置,颜色等,并且必须存储在GL_ARRAY_BUFFER中。

2)由于纹理缓冲区不能用作属性的输入,所以第二次调用VertexAttribPointer仍然使用位置vbo,它可以准确地给出结果(纹理坐标从-0.5到0.5)。将纹理坐标vbo也绑定到GL_ARRAY_BUFFER

3)在上传数据使用2个维向量,但在VertexAttribPointer呼叫时,您告诉您的数据组成的3D矢量的OpenGL。正确的代码是:

GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, False, Vector2.SizeInBytes, 0) 
+0

关于第三部分:我也想知道同样的事情。 GL.VertexAttribPointer(1,2,VertexAttribPointerType.Float,False,Vector2.SizeInBytes,0) 给了我一个变形的输出 –

+0

是的,它的工作。谢谢。由于错误的属性类型,设置为vector3的步幅上次不工作... –