GLSL: How to get pixel x,y,z world position?

后端 未结 4 1691
情深已故
情深已故 2020-12-24 02:29

I want to adjust the colors depending on which xyz position they are in the world.

I tried this in my fragment shader:

varying vec4 verpos;

void mai         


        
4条回答
  •  囚心锁ツ
    2020-12-24 02:54

    You need to pass the World/Model matrix as a uniform to the vertex shader, and then multiply it by the vertex position and send it as a varying to the fragment shader:

    /*Vertex Shader*/
    
    layout (location = 0) in vec3 Position
    
    uniform mat4 World;
    uniform mat4 WVP;
    
    //World FragPos
    out vec4 FragPos;
    
    void main()
    {
     FragPos = World * vec4(Position, 1.0);
     gl_Position = WVP * vec4(Position, 1.0);
    }
    
    /*Fragment Shader*/
    
    layout (location = 0) out vec4 Color;
    
    ... 
    
    in vec4 FragPos
    
    void main()
    {
     Color = FragPos;
    }
    

提交回复
热议问题