OpenTK 黑三角?

OpenTK black triangle?

参考之前,我已经阅读了OpenTK - fragment shader is not working (triangle is always black) 但这并没有帮助。

我正在尝试为 F# 中的 OpenTK 编写 'hello triangle' 教程。但是我的三角形是黑色而不是橙色。我想这是片段着色器无法正常工作但无法找出原因的原因。 https://opentk.net/learn/chapter1/2-hello-triangle.html

着色器class

type Shader(vertexpath, fragmentpath) as shader =
    let mutable handle = GL.CreateProgram()
    let mutable disposed = false
    let mutable vertexShader = GL.CreateShader(ShaderType.VertexShader)
    let mutable fragmentShader = GL.CreateShader(ShaderType.FragmentShader)
    
    do
        // vertexshader
        let mutable shaderSource = load vertexpath
        let mutable vertexShader = GL.CreateShader ShaderType.VertexShader
        GL.ShaderSource(vertexShader, shaderSource)
        shader.CompileShader(vertexShader)

        // fragmentshader
        let mutable shaderSource = load fragmentpath
        let mutable fragmentShader = GL.CreateShader ShaderType.VertexShader
        GL.ShaderSource(vertexShader, shaderSource)
        shader.CompileShader(fragmentShader)



        GL.AttachShader(handle, vertexShader)
        GL.AttachShader(handle, fragmentShader)

        shader.LinkProgram(handle)

        GL.DetachShader(handle, vertexShader)
        GL.DetachShader(handle, fragmentShader)
        GL.DeleteShader(fragmentShader)
        GL.DeleteShader(vertexShader)  


member _.Dispose() = 
    if not disposed then
        GL.DetachShader(handle, vertexShader)
        GL.DetachShader(handle, fragmentShader)
        GL.DeleteShader(vertexShader)
        GL.DeleteShader(fragmentShader)
        GL.DeleteProgram(handle)
        disposed <- true

member _.Use() = GL.UseProgram(handle) 

member _.CompileShader shader = 
    let mutable code = 0
    GL.CompileShader shader
    let info = GL.GetShaderInfoLog(shader)
    if System.String.IsNullOrEmpty info then
        printfn $"{info}"


    GL.GetShader(shader, ShaderParameter.CompileStatus, &code)
    if code <> int All.True then
        let infoLog = GL.GetShaderInfoLog shader
        failwith $"Error occured whilst compiling Shader({shader}).\n\n{infoLog}"


member _.LinkProgram (program: int) =
    let mutable code = 0

    GL.LinkProgram program
    let info = GL.GetProgramInfoLog(program)
    if System.String.IsNullOrEmpty info then
        printfn $"{info}"

    GL.GetProgram(program, GetProgramParameterName.LinkStatus, &code)
    if code <> int All.True then
        failwith $"Error occurred whilst linking Program({program})"
     

哪里

let load (path: string) = 
    (new StreamReader(path, Encoding.UTF8)).ReadToEnd() 
    |> fun str -> 
        printfn $"{str}"
        str

Window Class

type Window(width, height, ?title) as Win =
    inherit 
        // diviation from code on github
        GameWindow(
            GameWindowSettings.Default,
            new NativeWindowSettings()
        )

    let mutable title = defaultArg title ""


    let vertices =
        [|
            -0.5f; -0.5f; 0.0f;
             0.5f; -0.5f; 0.0f;
             0.0f;  0.5f; 0.0f;
        |]

    // throw execption if not initiated 
    let mutable vertexBufferObject = -1
    let mutable vertexArrayObject  = -1

    let mutable shader = new Shader("Shaders\shader.vert", "Shaders\shader.frag") 
    

    do
        Win.Size <- (width, height)
        Win.Title <- title
override _.OnLoad() =

        base.OnLoad()
        
        GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f)

        
        vertexBufferObject <- GL.GenBuffer()

        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObject)
        GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * 4, vertices, BufferUsageHint.StaticDraw)

        vertexArrayObject <- GL.GenVertexArray()
        GL.BindVertexArray(vertexArrayObject)

        // shader not yet implemented?? 
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * 4, 0)

        GL.EnableVertexAttribArray(0)

      
        shader.Use()
       




    override _.OnUnload() =

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

        GL.DeleteBuffer(vertexBufferObject)
        GL.DeleteVertexArray(vertexArrayObject)
        shader.Dispose()
        base.OnUnload()

  


    override _.OnRenderFrame e =
        base.OnRenderFrame e

        GL.Clear(ClearBufferMask.ColorBufferBit)

        shader.Use() 

        GL.BindVertexArray(vertexArrayObject)
        
        GL.DrawArrays(PrimitiveType.Triangles, 0, 3)

        Win.SwapBuffers()
        

    override _.OnResize e = 
        base.OnResize e
        GL.Viewport(0, 0, fst Win.Size, snd Win.Size)


    override _.OnUpdateFrame e =

        base.OnUpdateFrame e

        if Win.KeyboardState.IsKeyDown(Keys.Escape) then
            Win.Close()

缩进不对,我知道。程序再次运行没有错误,除了三角形不是正确的颜色。是否使用过https://github.com/opentk/LearnOpenTK/blob/master/Chapter1/2-HelloTriangle/Window.cs

https://github.com/opentk/LearnOpenTK/blob/master/Common/Shader.cs 对我的代码进行故障排除

shader.vert 和 shader.frag 是从上面的 git 存储库复制粘贴的。

我发现了错误,我在编译两个着色器时都以 vertexshader 对象为目标