glsl parse error

Accname

2D-Graphics enthusiast
Reaction score
1,462
Hi folks, i have problems with a short glsl fragment shader i was trying to write.
I havent used glsl very often, in fact this is the third shader i am writing so the code might not be perfect.

However, i get a very weird parse error which i just cant figure out any solution for. Maybe it is because its too late in the evening. Anyways, if somebody could help me i would be very thankful.

The error is:
ERROR: 0:1: error(#132) Syntax error: "offset_array" parse error
Code:
uniform sampler2D tex;
uniform int palette;
uniform int offset_array[3];
offset_array[0] = 0;
offset_array[1] = 11;
offset_array[2] = 15;
uniform vec3 color_table[30];
 
color_table[0] = vec3(24, 24, 24);
color_table[1] = vec3(72, 56, 32);
color_table[2] = vec3(120, 128, 72);
color_table[3] = vec3(192, 192, 152);
color_table[4] = vec3(232, 112, 72);
color_table[5] = vec3(248, 192, 96);
color_table[6] = vec3(248, 248, 248);
color_table[7] = vec3(152, 64, 56);
color_table[8] = vec3(56, 144, 64);
color_table[9] = vec3(32, 90, 34);
color_table[10] = vec3(120, 232, 152);
 
color_table[11] = vec3(24, 24, 24);
color_table[12] = vec3(120, 80, 48);
color_table[13] = vec3(200, 128, 88);
color_table[14] = vec3(240, 192, 144);
 
color_table[15] = vec3(24, 24, 24);
color_table[16] = vec3(32, 72, 49);
color_table[17] = vec3(72, 128, 126);
color_table[18] = vec3(152, 192, 185);
color_table[19] = vec3(72, 232, 83);
color_table[20] = vec3(96, 248, 164);
color_table[21] = vec3(248, 248, 248);
color_table[22] = vec3(66, 152, 56);
color_table[23] = vec3(56, 64, 144);
color_table[24] = vec3(32, 42, 90);
color_table[25] = vec3(131, 120, 232);
 
color_table[26] = vec3(24, 24, 24);
color_table[27] = vec3(82, 54, 32);
color_table[28] = vec3(172, 110, 76);
color_table[29] = vec3(204, 148, 96);
 
void main(){
    vec4 color = texture2D(tex, gl_TexCoord[0].st);
    int type = color.r;
    int index = color.g;
    int offset = offset_array[type];
    vec3 final_color = color_table[offset+index+palette*15];
 
    gl_FragColor = vec4(final_color.r, final_color.g, final_color.b, color.a);
}
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Two things are wrong with this one:

First, you cannot set uniform variables in shaders. At all. You have to look at uniforms as constants. You can only change them via glUniformXX() calls through the OpenGL API.

Code:
//Your vectors. I've defined them as a float array for simplicity.
float vectors[] = {
    24.f, 24.f, 24.f,
    72.f, 56.f, 32.f,
   ...
    204.f, 148.f, 96.f
}
GLuint u = glGetUniformLocation(program, "offset_array");
glUniform3fv(u, <number of vectors you want to upload>, (GLfloat*) vectors);

Second, you cannot implicitely cast a float to an integer like this:
int type = color.r;

Instead you must use the int's constructor:
int type = int(color.r);

Same goes for index.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
Okay, by now i figured that with the typecasting out myself.
But! Uniforms can be set in the shaders when they are initialized:
[...] Uniforms are intended to be set by the user. However, you can initialize them to a default value using standard GLSL initalizer syntax: [...]
http://www.opengl.org/wiki/Uniform_(GLSL)

But i am still struggeling with parse errors for some reason. I will upload the newest version of the code soon.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
My bad, you're right. I didn't know about uniform initialization. (I don't think it's a good idea, though :p)

However, you're not initializing your uniforms, you're assigning singular indices one-by-one.

Initializing an array would look like this:

Code:
//Initialize all 10 matrices in one statement.
uniform mat4 matrices[10] = {
    mat4(0),
    mat4(0),
    mat4(0),
    ....
    mat4(0)
};

It's not initialization anymore if you're doing it after declaration of the variable.

But keep in mind that not all OpenGL drivers implement uniform initialization correctly (it says so on the site you've linked) and there probably are some kind of version restrictions (e.g. uniform initialization only available for GLSL #330 and above).
So chances are it won't even work for you (or others) even if you're writing it correctly.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
Okay, thanks for the tip, i will try this out and reply if it worked or not.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top