简单的OpenTK Shader无法正常工作 [英] Simple OpenTK Shader not working

查看:96
本文介绍了简单的OpenTK Shader无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用OpenGL超级脚本和

使用注释中的建议更新了代码,不会改变结果.

还验证了vbo是否已正确填充gDEBugger:

Edit2:我从链接/使用着色器添加了信息日志,并将所有信息日志重新路由到控制台.我收到一些可以忽略的警告.

我还测试了是否从所有着色器中删除了浮动后缀,但是是否一切都保持不变.

解决方案

最后找到问题的答案了:

  GL.VertexAttribPointer(0,3,VertexAttribPointerType.Float,false,Vector3.SizeInBytes * _triangleVert.Length,0); 

第5个参数仅用于指定单个属性的大小,而不是整个数组的大小:

  GL.VertexAttribPointer(0,3,VertexAttribPointerType.Float,false,Vector3.SizeInBytes,0); 

现在它可以正常工作了,感谢所有有用的评论!

I'm currently trying to get into GLSL using the OpenGL superbible and http://www.learnopengl.com/. My desired environment is OpenTK.

I tried to convert the "Hello Triangle" entry level tutorial from learnopengl.com into OpenTK, it is meant to draw a triangle using very simple shaders, without using any perspective/world/model transformations.

My result is just the colorbuffer, but else an empty window and i simply do not understand where i have made a mistake so any help in finding and more importantly understanding them would be very appreciated.

My OpenTK code:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

namespace learnopengl.HelloTriangle
{
    public class MainWindow : GameWindow
    {
        int _shaderId;
        int _vao;
        int _glbuf;
        int _fragObj;
        int _vertexObj;
        Vector3[] _triangleVert;

        public MainWindow()
            : base(800,600)
        {
            KeyDown += MainWindow_KeyDown;
            _triangleVert = new 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)
            };

        }

        void MainWindow_KeyDown(object sender, KeyboardKeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                this.Exit();

            if (e.Key == Key.F11)
                if (this.WindowState == WindowState.Fullscreen)
                    this.WindowState = WindowState.Normal;
                else
                    this.WindowState = WindowState.Fullscreen;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            System.Diagnostics.Trace.WriteLine(string.Format("OpenGL version: {0}", GL.GetString(StringName.Version)));
            GL.ClearColor(Color.Purple);

            _vertexObj = GL.CreateShader(ShaderType.VertexShader);
            _fragObj = GL.CreateShader(ShaderType.FragmentShader);
            int statusCode;
            string info;

            GL.ShaderSource(_vertexObj, File.ReadAllText(@"shader\triangle.vert"));
            GL.CompileShader(_vertexObj);
            info = GL.GetShaderInfoLog(_vertexObj);
            Console.Write(string.Format("triangle.vert compile: {0}",info));
            GL.GetShader(_vertexObj, ShaderParameter.CompileStatus, out statusCode);
            if (statusCode != 1) throw new ApplicationException(info);

            GL.ShaderSource(_fragObj, File.ReadAllText(@"shader\triangle.frag"));
            GL.CompileShader(_fragObj);
            info = GL.GetShaderInfoLog(_fragObj);
            Console.Write(string.Format("triangle.frag compile: {0}", info));
            GL.GetShader(_fragObj, ShaderParameter.CompileStatus, out statusCode);
            if (statusCode != 1) throw new ApplicationException(info);

            _shaderId = GL.CreateProgram();
            GL.AttachShader(_shaderId, _fragObj);
            GL.AttachShader(_shaderId, _vertexObj);
            GL.LinkProgram(_shaderId);
            Console.Write(string.Format("link program: {0}", GL.GetProgramInfoLog(_shaderId)));
            GL.UseProgram(_shaderId);
            Console.Write(string.Format("use program: {0}", GL.GetProgramInfoLog(_shaderId)));

            _vao = GL.GenVertexArray();
            _glbuf = GL.GenBuffer();
            GL.BindVertexArray(_vao);
            GL.BindBuffer(BufferTarget.ArrayBuffer, _glbuf);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, new IntPtr(Vector3.SizeInBytes * _triangleVert.Length), _triangleVert, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes * _triangleVert.Length, 0);
            GL.EnableVertexAttribArray(0);
            GL.BindVertexArray(0);
        }

        protected override void OnResize(EventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);

            GL.BindVertexArray(_vao);
            GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
            GL.BindVertexArray(0);

            this.SwapBuffers();
        }

        protected override void OnUnload(EventArgs e)
        {
            GL.DeleteProgram(_shaderId);
            GL.DeleteShader(_vertexObj);
            GL.DeleteShader(_fragObj);
            GL.DeleteVertexArray(_vao);
            GL.DeleteBuffer(_glbuf);
            base.OnClosed(e);
        }
    }
}

triangle.vert:

#version 330 core

layout(location = 0) in vec3 position;

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

triangle.frag:

#version 330 core

//out vec4 color;

void main()
{
    gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    //color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}

My Result:

Edit: Updated the code with the suggestions from the comments, which don't change the outcome.

Also verified that the vbo is filled correctly with gDEBugger:

Edit2: I added the Info log from linking/using the shaders and reroutet all info log to the console. I get some warnings which can be ignored.

I also tested if any changes occur if i remove the float suffix from all shaders, but everything stayed the same.

解决方案

Finally found an answer to the problem it's the line:

GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes * _triangleVert.Length, 0);

The 5th parameter is meant to only specify a single attribute's size, not of the whole array:

GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);

Now it works like it should, thanks for all the helpful comments!

这篇关于简单的OpenTK Shader无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆