How exactly does fragment shader work for texturing?

后端 未结 2 422
南旧
南旧 2021-01-14 20:12

I am learning opengl and I thought I pretty much understand fragment shader. My intuition is that fragment shader gets applied once to every pixel but recently when working

相关标签:
2条回答
  • 2021-01-14 20:32

    first : in core context you still can use gl_FragColor.

    second : you have texel ,fragment and real_monitor_pixel.These are different things. say this line is about convert texel to fragment(or to pixel idk exactly what it does):

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    

    when texel is less then fragment(pixel)

    0 讨论(0)
  • 2021-01-14 20:33

    All varying variables are interpolated automatically.

    Thus if you put texture coordinates for each vertex into a varying, you don't need to do anything special with them after that.

    It could be as simple as this:

    // Vertex
    #version 330 compatibility
    attribute vec2 a_texcoord;
    varying vec2 v_texcoord;
    
    void main()
    {
        v_texcoord = a_texcoord;
    }
    
    // Fragment
    uniform vec2 u_texture;
    varying vec2 v_texcoord;
    
    void main()
    {
        gl_FragColor = texture2D(u_texture, v_texcoord);
    }
    

    Disclaimer: I used the old GLSL syntax. In newer GLSL versions, attribute would be replaced with in. varying would replaced with out in the vertex shader and with in in the fragment shader. gl_FragColor would be replaced with a custom out vec4 variable. texture2D() would be replaced with texture()

    Notice how this fragment shader doesn't do any manual interpolation. It receives just a single vec2 v_texcoord, which was interpolated under the hood from v_texcoords of vertices comprising a primitive1 current fragment belongs to.

    1. A primitive means a point, a line, a triangle or a quad.

    0 讨论(0)
提交回复
热议问题