Search code examples
opengl-esfragment-shadervertex-shader

OpenGL shader builder errors on compiling


I'm using OpenGL shader builder v2.2.2 and took sample code from book. For vertex shader

#version 140
uniform float CoolestTemp;
uniform float TempRange;
uniform mat4  MVPMatrix;
in  vec4  mcVertex;
in  float VertexTemp;

out float Temperature;
void main() {

    Temperature = (VertexTemp - CoolestTemp) /  TempRange;
    gl_Position = MVPMatrix * mcVertex;
}

And for fragment shader

#version 140

uniform vec3 CoolestColor;
uniform vec3 HottestColor;

in float Temperature;
out vec4 FragmentColor;

void main() 
{    
    vec3 color = mix(CoolestColor, HottestColor, Temperature);
    FragmentColor = vec4(color, 1.0);
}

My problem is to execute this code because version 140 and 320 is not supported. Then I tryed to remove versions, compiler said "Invalid qualifiers 'in' in global variable context" and same for 'out', then tried to replace in/out to 'varying' but in fragment shader "Left-hand-side of assignment must not be read-only" error appears. For vertex shader have warning about varying fields will not read in next stages. How can I figure out it to adapt an old-styled code to new? So I'm absolutely new to GLSL


Solution

  • It's much more common to go from the old naming to new. But if you need the other way, the conversion for the vertex shader is:

    in --> attribute
    out --> varying
    

    For fragment shader:

    in --> varying
    out --> (delete)
    

    For the out of the fragment shader, you can delete the declaration of the variable. Instead, use the built-in gl_FragColor variable.

    For your example, the vertex shader will look like this:

    uniform float CoolestTemp;
    uniform float TempRange;
    uniform mat4  MVPMatrix;
    attribute vec4  mcVertex;
    attribute float VertexTemp;
    varying float Temperature;
    
    void main() {
        Temperature = (VertexTemp - CoolestTemp) /  TempRange;
        gl_Position = MVPMatrix * mcVertex;
    }
    

    And the fragment shader:

    uniform vec3 CoolestColor;
    uniform vec3 HottestColor;
    varying float Temperature;
    
    void main() {
        vec3 color = mix(CoolestColor, HottestColor, Temperature);
        gl_FragColor = vec4(color, 1.0);
    }