OpenGL片段着色器 - 从窗口坐标到uv

时间:2016-04-03 16:24:33

标签: opengl glsl

作为赋值的一部分,我正在编写一个片段着色器,我在其中访问来自片段缓冲区的纹理。因此,我无法从顶点着色器获取uv坐标,这意味着我需要计算它们。我一直遇到这个版本的问题:

#version 330 core
out vec3 color;
in vec2 uv;

in vec4 gl_FragCoord ;

uniform sampler2D tex; //texture of the floor
uniform sampler2D tex_mirror; //texture of the cube (used to get reflection)

void main() {

    /// TODO: query window_width/height using the textureSize(..) function on tex_mirror
    /// TODO: use gl_FragCoord to build a new [_u,_v] coordinate to query the framebuffer
    /// NOTE: make sure you normalize gl_FragCoord by window_width/height
    /// NOTE: you will have to flip the "v" coordinate as framebuffer is upside/down
    /// TODO: mix the texture(tex,uv).rgb with the value you fetch by texture(tex_mirror,vec2(_u,_v)).rgb

    int window_width = textureSize(tex_mirror, 0).x;
    int window_height = textureSize(tex_mirror, 0).y;

    float _u = gl_FragCoord.x;
    float _v = -gl_FragCoord.y;

    vec3 color_from_texture = texture(tex,uv).rgb;
    vec3 color_from_mirror = texture(tex_mirror, vec2(_u, _v)).rgb;

    color = mix(color_from_texture, color_from_mirror, vec3(.15));
}

目标是最终获得这样的反射表面(不要介意顶部的立方体,只有地板和该立方体的反射在上面的片段着色器中相关): reflective surface

我的问题是:我计算_u _v坐标的方式很可能是有缺陷的,因为在启动程序时,我只能在没有反射的情况下找到底线。 我试图用地板的紫外坐标渲染反射,看看是否显示了什么,我得到了这个:

enter image description here

0 个答案:

没有答案