C#/ OpenTK - 将着色器应用于加载纹理的问题。纹理缩小并翻转

时间:2015-11-30 19:56:05

标签: c# opengl opentk

我一直试图弄清楚如何让它发挥作用。我使用OpenTK for C#来修改图像。现在我能够成功加载图像,但是当我尝试使用着色器修改图像时,我开始遇到问题。我有3种基本方法:

  • 1)LoadTexture(将纹理设置为GL)
  • 2)DrawImage(实际绘制纹理)
  • 3)AddShaders(此方法在DrawImage中调用。它应用了 着色器)

以下是我遇到的两个问题:

  • 1)我试图创建两个跨越整个纹理的三角形。 这是因为我希望我的片段着色器能够完成整个工作 质地。我实际得到的是一个覆盖的大三角形 屏幕左侧,右侧有一个矩形 屏幕。编辑:取得了一些进展!但仍然看起来很奇怪...... Shader脚本更新了...... EDIT2:上传了更新的进度图片

  • 2)当我希望它们使用时,形状显示为绿色 纹理的颜色,但修改了红色通道。重要的是什么 知道我需要能够分析每个像素。改变了 每个像素的红色通道只是我想要的概念证明 实际做(使用色距公式,色调偏移等,但 我需要先简单开始)

这是我成功加载为纹理的图片:Loaded Texture

这里是LoadTexture的代码:

      public int LoadTexture(string file)
    {
        Bitmap bitmap = new Bitmap(file);

        int tex;
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

        GL.GenTextures(1, out tex);
        GL.BindTexture(TextureTarget.Texture2D, tex);

        BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.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);
        bitmap.UnlockBits(data);


        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

        return tex;
    }

这是DrawImage的代码:

        public static void DrawImage(int image)
    {


        GL.MatrixMode(MatrixMode.Projection);
        GL.PushMatrix();
        GL.LoadIdentity();

        GL.Ortho(0, 1920, 0, 1080, -1, 1);

        GL.MatrixMode(MatrixMode.Modelview);
        GL.PushMatrix();
        GL.LoadIdentity();

        GL.Disable(EnableCap.Lighting);

        GL.Enable(EnableCap.Texture2D);

        GL.ActiveTexture(TextureUnit.Texture0);
        GL.BindTexture(TextureTarget.Texture2D, image);

        GL.Begin(PrimitiveType.Quads);

        GL.TexCoord2(0,1);
        GL.Vertex3(0, 0, 0);

        GL.TexCoord2(1, 1);
        GL.Vertex3(1920, 0, 0);

        GL.TexCoord2(1, 0);
        GL.Vertex3(1920, 1080, 0);

        GL.TexCoord2(0, 0);
        GL.Vertex3(0, 1080, 0);


        GL.End();

        AddShaders();

        GL.Disable(EnableCap.Texture2D);
        GL.PopMatrix();

        GL.MatrixMode(MatrixMode.Projection);
        GL.PopMatrix();

        GL.MatrixMode(MatrixMode.Modelview);

        ErrorCode ec = GL.GetError();
        if (ec != 0)
            System.Console.WriteLine(ec.ToString());
        Console.Read();

    }

以下是AddShaders的代码:

      private static void AddShaders()
    {

        /***********Vert Shader********************/
        var vertShader = GL.CreateShader(ShaderType.VertexShader);
        GL.ShaderSource(vertShader, @"attribute vec3 a_position;
                                        varying vec2 vTexCoord;
                                        void main() {
                                        vTexCoord = a_position.xy;
                                        gl_Position = vec4(a_position, 1);
                                        }");
        GL.CompileShader(vertShader);


        /***********Frag Shader ****************/
        var fragShader = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(fragShader, @"uniform sampler2D sTexture;
                                      varying vec2 vTexCoord;
                                void main ()
                                {
                                    vec4    color   = texture2D (sTexture, vTexCoord);
                                    color.r = 0.5;
                                    // Save the result
                                    gl_FragColor    = color;
                                }");
        GL.CompileShader(fragShader);

        var program = GL.CreateProgram();
        GL.AttachShader(program, vertShader);
        GL.AttachShader(program, fragShader);
        GL.LinkProgram(program);
        GL.ClearColor(Color.AliceBlue);

        // OpenGL expects vertices to be defined counter clockwise by default
        float[] vertices = {
                // Left bottom triangle
                -1f, 1f, 0f,
                -1f, -1f, 0f,
                1f, -1, 0f,
                // Right top triangle
                1f, -1f, 0f,
                1f, 1f, 0f,
                -1f, 1f, 0f
        };

        var buffer = GL.GenBuffer();
        var positionLocation = GL.GetAttribLocation(program, "a_position");
        GL.EnableVertexAttribArray(positionLocation);
        GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
        GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float,false,0,0);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(ushort)), vertices, BufferUsageHint.StaticDraw);

        GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length);
        GL.UseProgram(program);

    }  

我已经研究了几天,而且我完全陷入困境。感谢任何能够看到我做错事的人。它本身就是一个小而愚蠢的东西!

编辑:当我在AddShaders中删除所有与顶点相关的代码时,我得到了我想要的输出,除了它的1/4大小并翻转在屏幕的右上角。所以,不知何故,我的着色器甚至不关心顶点。为什么它被缩小到1/4尺寸并翻转?

EDIT2:好的,谢谢Robert Rouhani,我已经完成了这项工作! Progress看起来三角顶点可能搞砸了?

这是我的新代码。我将功能重构为方法,停止每帧创建程序/缓冲区等。现在我有类级变量来保存GL特定数据,为app创建GL程序的方法,创建着色器,创建缓冲区等。我知道1920x1080硬编码是硬编码的。这是我的盘子,使动态。

    string file = "lambo2.png";
    int program;
    int vertShader;
    int fragShader;
    int buffer;
    int positionLocation;
    int texture;
    float[] vertices = {
                // Left bottom triangle
                -1f, -1f, 0f,
                1f, -1f, 0f,
                1f, 1f, 0f,
                // Right top triangle
                1f, 1f, 0f,
                  -1f, 1f, 0f,
                 -1f, -1f, 0f
        };


    private void CreateProgram()
    {
        program = GL.CreateProgram();
        GL.AttachShader(program, vertShader);
        GL.AttachShader(program, fragShader);
        GL.LinkProgram(program);
    }
    private void CreateShaders()
    {
        /***********Vert Shader********************/
        vertShader = GL.CreateShader(ShaderType.VertexShader);
        GL.ShaderSource(vertShader, @"attribute vec3 a_position;
                                        varying vec2 vTexCoord;
                                        void main() {
                                        vTexCoord = (a_position.xy + 1) / 2;
                                        gl_Position = vec4(a_position, 1);
                                        }");
        GL.CompileShader(vertShader);


        /***********Frag Shader ****************/
        fragShader = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(fragShader, @"precision highp float;
        uniform sampler2D sTexture;
                                       varying vec2 vTexCoord;
                                 void main ()
                                 {
                                     vec4    color   = texture2D (sTexture, vTexCoord);
                                     if(color.r < 0.3){color.r = 1.0;}
                                     // Save the result
                                     gl_FragColor    = color;
                                 }");
        //   GL.ShaderSource(fragShader, System.IO.File.ReadAllText(@"C:\Users\Matt\Desktop\hue-shader-backup.ps"));
        GL.CompileShader(fragShader);
    }
    private void InitBuffers()
    {
        buffer = GL.GenBuffer();
        positionLocation = GL.GetAttribLocation(program, "a_position");
        GL.EnableVertexAttribArray(positionLocation);
        GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(ushort)), vertices, BufferUsageHint.StaticDraw);
        GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
    }
    private void Init()
    {
        texture = LoadTexture(file);
        CreateShaders();
        CreateProgram();
        InitBuffers();
    }
    public int LoadTexture(string file)
    {
        Bitmap bitmap = new Bitmap(file);

        int tex;
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

        GL.GenTextures(1, out tex);
        GL.BindTexture(TextureTarget.Texture2D, tex);
        bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
        BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.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);
        bitmap.UnlockBits(data);


        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

        return tex;
    }

    public void DrawImage(int image)
    {

        GL.Viewport(new Rectangle(0, 0, 1920, 1080));
        GL.MatrixMode(MatrixMode.Projection);
        GL.PushMatrix();
        GL.LoadIdentity();


        GL.Ortho(0, 1920, 0, 1080, 0, 1);

        GL.MatrixMode(MatrixMode.Modelview);
        GL.PushMatrix();
        GL.LoadIdentity();

        GL.Disable(EnableCap.Lighting);

        GL.Enable(EnableCap.Texture2D);

        GL.ActiveTexture(TextureUnit.Texture0);
        GL.BindTexture(TextureTarget.Texture2D, image);

        GL.Begin(PrimitiveType.Quads);

        GL.TexCoord2(0, 1);
        GL.Vertex3(0, 0, 0);

        GL.TexCoord2(1, 1);
        GL.Vertex3(1920, 0, 0);

        GL.TexCoord2(1, 0);
        GL.Vertex3(1920, 1080, 0);

        GL.TexCoord2(0, 0);
        GL.Vertex3(0, 1080, 0);

        GL.End();
        RunShaders();





        GL.Disable(EnableCap.Texture2D);
        GL.PopMatrix();

        GL.MatrixMode(MatrixMode.Projection);
        GL.PopMatrix();

        GL.MatrixMode(MatrixMode.Modelview);


        ErrorCode ec = GL.GetError();
        if (ec != 0)
            System.Console.WriteLine(ec.ToString());
        Console.Read();
    }
    private void RunShaders()
    {

        GL.ClearColor(Color.AliceBlue);
        GL.UseProgram(program);
        GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length / 3);

        ErrorCode ec = GL.GetError();
        if (ec != 0)
            System.Console.WriteLine(ec.ToString());
        Console.Read();

    }

1 个答案:

答案 0 :(得分:1)

开始回答而不是继续发表评论。你还有一些复杂的小问题。您应该评论GL.BeginGL.End之间的所有内容,因为RunShaders函数应该在屏幕上绘制您需要的所有内容。同时注释掉GL.Ortho行,如果你正在处理[-1,1]范围内的顶点,则不需要它。

其次,您的问题是您只将一半的顶点缓冲区上传到GPU。在InitBuffers行的GL.BufferData上,将sizeof(ushort)更改为sizeof(float),因为您的顶点是浮点数(4个字节长)而不是ushorts(2个字节长)。

GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);

这将使您的计划正常运作。