gldrawarrays上的黑屏

时间:2019-05-18 01:21:31

标签: c# opengl

在使着色器与vbo和vao一起工作时遇到问题。我只做过glbegin()和drawelements(),我认为这要简单得多。

我没有得到任何glerror(),但是屏幕呈现黑色,我在做什么错?

着色器即时通讯可能正在使用它。

底部的两个着色器位于tutorial.vert和tutorial.frag文件中。

更新:我进行了代码检查以检查着色器中的编译错误,并相应地更改了着色器文件,但仍然是黑屏。

namespace VBO
{
    class Program
    {
        static public IntPtr window;
        static IntPtr gl_context;

        public static int w = 640;
        public static int h = 640;

        public static uint[] vao = new uint[1];
        public static uint[] vbo = new uint[2];

        public static float[,] v = new float[4, 2] {
        {  0.5f,  0.5f  },
        {  0.5f, -0.5f  }, 
        { -0.5f, -0.5f  }, 
        { -0.5f,  0.5f  } };

        public static float[,] c = new float[4, 3] {
        {  1.0f,  0.0f,  0.0f  },
        {  0.0f,  1.0f,  0.0f  },
        {  0.0f,  0.0f,  1.0f  },
        {  1.0f,  1.0f,  1.0f  } };

        static void Main(string[] args)
        {
            SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING);
            Gl.Initialize();

            window = SDL.SDL_CreateWindow("test", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, w, h, SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL);
            gl_context = SDL.SDL_GL_CreateContext(window);
            SDL.SDL_GL_SetSwapInterval(1);
        SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 2);

            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
            SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, 24);

            Gl.LineWidth(4);

            Gl.GenVertexArrays(vao);
            Gl.BindVertexArray(vao[0]);

            Gl.GenBuffers(vbo);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            Gl.BufferData(BufferTarget.ArrayBuffer, 8, v, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(0, 2, VertexAttribType.Float, false, 0, 0);

            Gl.EnableVertexAttribArray(0);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            Gl.BufferData(BufferTarget.ArrayBuffer, 12, c, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(1, 3, VertexAttribType.Float, false, 0, 0);

            Gl.EnableVertexAttribArray(1);
            uint vertexshader, fragmentshader;
            uint shaderprogram;

            string[] vertexsource = filetobuf("tutorial.vert");
            string[] fragmentsource = filetobuf("tutorial.frag");

            for (int i = 0; i < vertexsource.Length; i++)
            {
                vertexsource[i] = vertexsource[i].Insert(vertexsource[i].Length,"\n");
            }

            for (int i = 0; i < fragmentsource.Length; i++)
            {
                fragmentsource[i] = fragmentsource[i].Insert(fragmentsource[i].Length, "\n");
            }

            vertexshader = Gl.CreateShader(ShaderType.VertexShader);
            Gl.ShaderSource(vertexshader, vertexsource);
            Gl.CompileShader(vertexshader);

            Gl.GetShader(vertexshader, ShaderParameterName.CompileStatus, out int compiled);
            if (compiled == 0)
            { 
                const int logMaxLength = 1024;

                StringBuilder infolog = new StringBuilder(logMaxLength);
                int infologLength;

                Gl.GetShaderInfoLog(vertexshader, logMaxLength, out infologLength, infolog);

                throw new InvalidOperationException($"unable to compile shader: {infolog}");
            }

            fragmentshader = Gl.CreateShader(ShaderType.FragmentShader);
            Gl.ShaderSource(fragmentshader, fragmentsource);
            Gl.CompileShader(fragmentshader);

            Gl.GetShader(fragmentshader, ShaderParameterName.CompileStatus, out compiled);
            if (compiled == 0)
            {
                const int logMaxLength = 1024;

                StringBuilder infolog = new StringBuilder(logMaxLength);
                int infologLength;

                Gl.GetShaderInfoLog(fragmentshader, logMaxLength, out infologLength, infolog);

                throw new InvalidOperationException($"unable to compile shader: {infolog}");
            }

            shaderprogram = Gl.CreateProgram();
            Gl.AttachShader(shaderprogram, vertexshader);
            Gl.AttachShader(shaderprogram, fragmentshader);

            Gl.BindAttribLocation(shaderprogram, 0, "aPosition");
            Gl.BindAttribLocation(shaderprogram, 1, "aColor");

            Gl.LinkProgram(shaderprogram);
            Gl.GetProgram(shaderprogram, ProgramProperty.LinkStatus, out compiled);
            if (compiled == 0)
            {
                const int logMaxLength = 1024;

                StringBuilder infolog = new StringBuilder(logMaxLength);
                int infologLength;

                Gl.GetProgramInfoLog(shaderprogram, logMaxLength, out infologLength, infolog);

                throw new InvalidOperationException($"unable to compile shader: {infolog}");
                }

            Gl.UseProgram(shaderprogram);

            Gl.ClearColor(0f, 0f, 0f, 1f);
            Gl.Color3(1f,1f,1f);

            while (true)
            {
                Gl.Rotate(0.2, 0, 0, 1);

                Gl.Clear(ClearBufferMask.ColorBufferBit);

                Gl.DrawArrays(PrimitiveType.LineLoop, 0, 4);

                SDL.SDL_GL_SwapWindow(window);
            }
        }

        static string[] filetobuf(string f)
        {
            return File.ReadAllLines(f);
       }
    }
}

0 个答案:

没有答案