我的应用通过使用OpenGL着色器代码以全屏显示图像,如下所示。我在这里使用的顶点着色器是从某个地方复制的。谁能解释为什么在这里使用vTexCoord = (a_position.xy+1)/2;?当我尝试使用vTexCoord = a_position.xy时,我的OpenGL输出分为四个矩形,只有右上角显示图像。其他三个方面似乎都很模糊。我应该做些什么改变才能与vTexCoord = a_position.xy一起使用?

项目中使用的一些重要功能如下所示。请检查并帮助纠正。

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 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);
        gl_FragColor =color;
                             }");
        GL.CompileShader(fragShader);
    }
    private void InitBuffers()
    {
        buffer = GL.GenBuffer();
        positionLocation = GL.GetAttribLocation(program, "a_position");
        positionLocation1 = GL.GetUniformLocation(program, "sTexture");
        GL.EnableVertexAttribArray(positionLocation);
        GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
        GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
    }
 public void DrawImage(int image)
    {
        GL.Viewport(new Rectangle(0, 0, ScreenWidth, ScreenHeight));

        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.Uniform1(positionLocation1, 0);
        GL.Begin(PrimitiveType.Quads);
        GL.TexCoord2(0, 1);
        GL.Vertex3(0, 0, 0);
        GL.TexCoord2(0, 0);
        GL.Vertex3(1920, 0, 0);
        GL.TexCoord2(1, 1);
        GL.Vertex3(1920, 1080, 0);
        GL.TexCoord2(1, 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);
        glControl1.SwapBuffers();
    }
    private void RunShaders()
    {
        GL.UseProgram(program);
        GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length / 3);
    }

最佳答案

谁能解释为什么在这里使用vTexCoord = (a_position.xy+1)/2;


对于x和分量,示例中的顶点坐标在[-1,1]范围内。这与规范化的设备空间匹配。规范化的设备空间是一个从左下前(-1,-1,-1)到右上后(1、1、1)的立方体,这是“可见”的区域。该区域被映射到视口。
在您的示例中,这导致顶点坐标形成一个矩形,该矩形覆盖了整个视口。

如果纹理坐标应将整个纹理包装到四边形,则纹理坐标(u,v)必须在[0,1]范围内。 (0,0)是纹理的左下角,而(1,1)是纹理的右上角。
另见How do OpenGL texture coordinates work?

因此,a_position的x和y分量必须从[-1,1]范围映射到[0,1]范围,以用作纹理查找的uv坐标:

u = (a_position.x + 1) / 2
v = (a_position.y + 1) / 2





  我应该做些什么改变才能使用vTexCoord = a_position.xy


这是不可能的,但是您可以生成一个单独的纹理坐标属性,这很常见:

attribute vec3 a_position;
attribute vec2 a_texture;

varying vec2 vTexCoord;

void main() {
    vTexCoord   = a_texture;
    gl_Position = vec4(a_position, 1);
}


float[] vertices = {
  // x    y   z      u   v
    // Left bottom triangle
    -1f, -1f, 0f,    0f, 0f
     1f, -1f, 0f,    1f, 0f
     1f,  1f, 0f,    1f, 1f
    // Right top triangle
     1f,  1f, 0f,    1f, 1f
    -1f,  1f, 0f,    0f, 1f
    -1f, -1f, 0f     0f, 0f
};


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

positionLocation = GL.GetAttribLocation(program, "a_position");
tetureLocation   = GL.GetAttribLocation(program, "a_texture");

GL.EnableVertexAttribArray(positionLocation);
GL.EnableVertexAttribArray(tetureLocation);

int stride   = sizeof(float) * 5; // 5 because of (x, y, z, u, v)
int offsetUV = sizeof(float) * 3; // 3 because the u and v coordinates are the 4th and 5th coordinate

GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, stride, 0);
GL.VertexAttribPointer(tetureLocation,   2, VertexAttribPointerType.Float, false, stride, (IntPtr)(offsetUV));

09-11 19:04