简体   繁体   English

片段着色器旋转

[英]Fragment Shader Rotation

I've been having a few problems with my fragment shader. 我的片段着色器遇到了一些问题。 After a bit of research, it appears that drivers clean up unused variables (ones which have no effect on the output?), which can cause glSetUniform and glGetUniform to return -1. 经过一些研究,驱动程序似乎清除了未使用的变量(对输出没有影响的变量?),这可能导致glSetUniform和glGetUniform返回-1。

My current problem is that I'm attempting to rotate a texture 180 degrees, but it appears that I'm doing something incorrectly, as the uniform int "top" appears to be garbage collected, and cannot be found. 我当前的问题是我试图将纹理旋转180度,但似乎做错了什么,因为统一的int“顶部”似乎是垃圾收集的,无法找到。 The texture is not rotated at all, but still renders. 纹理完全不旋转,但仍然渲染。 The uniform "top" returns -1, which should not happen. 统一的“顶部”返回-1,这不应发生。

Here's my relevant code: 这是我的相关代码:

Rendering and Shader Enable code: (Shader.PIPE.enable() does call glUseProgram()) 渲染和着色器启用代码:(Shader.PIPE.enable()确实调用glUseProgram())

    Shader.PIPE.enable();
    Shader.PIPE.setUniformMat4f("vw_matrix", Matrix4f.translate(new Vector3f(xScroll * 0.03f, 0.0f, 0.0f)));    
    Pipe.getTexture().bind();
    Pipe.getMesh().bind();

    for (int i = 0; i < 5 * 2; i++) {
        Shader.PIPE.setUniformMat4f("ml_matrix", pipes[i].getModelMatrix());
        Shader.PIPE.setUniform1i("top",  i % 2 == 0 ? 1 : 0);
        Pipe.getMesh().draw();
    }
    Pipe.getMesh().unbind();
    Pipe.getTexture().unBind();

Pipe.frag: Pipe.frag:

#version 330 core

layout (location = 0) out vec4 color;

in DATA 
{
    vec2 tc;
} fs_in;

uniform sampler2D tex;
uniform int top;

void main() 
{   
    vec2 myTc = vec2(fs_in.tc.x, fs_in.tc.y);
    if (top == 1) {
        myTc.y = top - myTc.y;  
    }

    color = texture(tex, fs_in.tc);
    if (color.w  < 1.0)
        discard;
}

Pipe.vert: Pipe.vert:

#version 330 core

layout (location = 0) in vec4 position;
layout (location = 1) in vec2 tc;

uniform mat4 pr_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

out DATA
{
    vec2 tc;
} vs_out;

void main() 
{
    gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
    vs_out.tc = tc;
}

The compiler can not only eliminate completely unused variables, but also values that do not contribute to the result. 编译器不仅可以消除完全未使用的变量,还可以消除对结果无用的值。 In your code, top is used here: 在您的代码中, top在这里使用:

if (top == 1) {
    myTc.y = top - myTc.y;  
}

But myTc is not used later in the shader. 但是myTc稍后不会在着色器中使用。 So the value of myTc has no effect on the output of the shader. 因此, myTc的值对着色器的输出没有影响。 Which in turn means that the value of top has no effect. 反过来意味着top的值无效。 Or in other words, the result of the shader is the same, independent of the value of top . 换句话说,着色器的结果是相同的,而与top的值无关。

This changes if myTc is used later in the shader, in a way that influences the result. 如果myTc稍后在着色器中使用, myTc以影响结果的方式更改。 I actually suspect that you meant to do that, and use it as the texture coordinates to sample your texture. 我实际上怀疑您打算这样做,并将其用作纹理坐标以对您的纹理进行采样。 So instead of: 所以代替:

color = texture(tex, fs_in.tc);

you should use: 您应该使用:

color = texture(tex, myTc);

Now top will be used, and you should get the desired result. 现在将使用top ,您将获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM