Search code examples
glslfragment-shader

What does iResolution mean in a shader?


I am new to GLSL. I find there is a variable iResolution in every demo https://www.shadertoy.com/new. What does it mean?

If I want to pass this variable to the shader, what do I need do?


Solution

  • You can see the definition if you expand the "Shader Inputs" section above the code:

    enter image description here

    The description pretty much says it all. It's the size of the window/viewport in pixels. In the example, gl_FragCoord, which is the position in pixels of the fragment, is divided by this size to obtain the relative position of the fragment within the viewport.

    You would set this like any other uniform variable:

    GLint loc = glGetUniformLocation(program, "iResolution");
    glUniform2f(loc, width, height);
    

    where width and height are the size of your window/viewport. It's odd that this is defined as a vec3, since only the x and y values are useful.