0
votes

I am currently working on a particle system in which each particle is a point. I am using shaders to color the particles. I would like the particle to change colors based on its position. However, when I try to include an extra vector variable in my fragment and vertex shaders, my shaders do not compile.

Fragment shader:

varying vec3 normal; 
varying vec3 vertex_to_light_vector;
varying vec3 vertex_to_eye_vector;
//out vec3 color;
//varying float red;
//varying float green;
//varying float blue;

//black body radiation color map

void main ()
{
    const vec4 AmbientColor = vec4(0.2, 0.2, 0.2, 0.2);
    const vec4 DiffuseColor = vec4(0.0, 0.1,0.3, 0.1);
    const vec4 SpecularColor = vec4(1.0, 1.0, 1.0, 0.1);
    vec3 normalized_normal = normalize(normal);
    vec3 normalized_vertex_to_light_vector = normalize(vertex_to_light_vector);
    vec3 normalized_vertex_to_eye_vector = normalize(vertex_to_eye_vector);
    vec3 bisector = normalize(vertex_to_light_vector + vertex_to_eye_vector);

    float DiffuseTerm = clamp(max(0.0, dot(normalized_normal, normalized_vertex_to_light_vector)), 0.0, 1.0);
    float SpecularTerm = clamp(max(0.0, dot(normalized_normal, bisector)), 0.0, 1.0);

    gl_FragColor = DiffuseColor * DiffuseTerm;
    //+ SpecularColor * pow(SpecularTerm, 80.0);

}

Vertex shader:

varying vec3 normal; 
varying vec3 vertex_to_light_vector;
varying vec3 vertex_to_eye_vector;
//out vec3 color;
//varying float red;
//varying float green;
//varying float blue;
//varying vec2 texture_coordinate;
//uniform sample2D my_color_texture;

void main ()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; //gettingfinal position in projection space

    if (gl_Position[0] > 0.5){
//        color = vec3(1.0, 0.0, 0.0);
//        red =1.0;
//        green = 0.0;
//        blue = 0.0;
    }
    else {
//        color = vec3(0.0, 0.0, 1.0);
//        color[0] = 0.0;
//        color[1] = 0.0;
//        color[2] = 1.0;
//        red = 0.0;
//        green = 0.0;
//        blue = 1.0;
    }

    normal = gl_NormalMatrix * gl_Normal;

    vec4 vertex_in_modelView_space = gl_ModelViewMatrix * gl_Vertex;

    vertex_to_light_vector = vec3(gl_LightSource[0].position - vertex_in_modelView_space); 

    vertex_to_eye_vector = vec3(-vertex_in_modelView_space);

    //texture_coordinate = vec2(gl_MultiTexCoord0);

}
1

1 Answers

3
votes

Since your shader does not contain a #version directive on the first line, a compliant GLSL compiler is supposed to treat your shader as GLSL 1.1. This creates a serious problem with your color output, because the in and out qualifiers were not valid for varying declarations prior to OpenGL 3.0 (GLSL 1.3).

More than likely what is actually happening is that the compiler is encountering out and this is generating a parse error. Use varying instead, since these GLSL shaders are implicitly #version 110.

Even if the shaders were written using #version 130, you have another issue that will prevent successful I/O between the vertex shader and fragment shader after linking. Vertex shader outputs are fragment shader inputs, thus your color variable would need to be declared: out vec3 color in the vertex shader and in vec3 color in the fragment shader. But again, this only applies to modern OpenGL shaders written against the OpenGL 3.0 GLSL spec. (GLSL 130) or newer.

For future reference, if you were to call glGetShaderInfoLog (...) after attempting to compile your shaders, it would inform you of the parse error. Likewise, glGetProgramInfoLog (...) will give you any linker errors after you call glLinkProgram (...) or glValidateProgram (...).