5

I am trying to write a simple vertex shader for an OpenGL ES app on the iPhone, but my array constructor is causing me trouble.

attribute vec4 normal;
attribute vec4 position;

void main(void){

    const vec4 vertices[3] = vec4[](vec4(0.25, -0.25, 0.5, 1.0), 
                             vec4(-0.25, -0.25, 0.5, 1.0),
                             vec4(0.25, 0.25, 0.5, 1.0));
    gl_Position = vertices[gl_VertexID];

}

When using this code the shader is unable to compile, and gives me the eror message:

ERROR: 0:13: '(' : syntax error: Array size must appear after variable name

2 Answers 2

9

The GLSL version used with ES 2.0 does not support constant arrays. From section "4.3.2 Constant Qualifier" on page 30 of the spec:

Arrays and structures containing arrays may not be declared constant since they cannot be initialized.

This restriction is lifted in ES 3.0, where it says in the corresponding section:

The const qualifier can be used with any of the non-void transparent basic data types as well as structures and arrays of these.

As an alternative, you should be able to use a non-constant array, where you assign the values one by one (untested):

vec4 vertices[3];
vertices[0] = vec4(0.25, -0.25, 0.5, 1.0);
vertices[1] = vec4(-0.25, -0.25, 0.5, 1.0);
vertices[2] = vec4(0.25, 0.25, 0.5, 1.0);

Or you could use a series of if-statements.

Sign up to request clarification or add additional context in comments.

4 Comments

So what do I do instead?
I added one alternative to the answer.
Reto, your alternative won't work if it's declared outside of a function body. The best alternative is to declare it as uniform vec4 vertices[3] and specify the values from your C code.
can confirm that the alternative one is not working
-2

Have you tried braces?

const vec4 vertices[3] = {vec4(0.25, -0.25, 0.5, 1.0),
                          vec4(-0.25, -0.25, 0.5, 1.0),
                          vec4(0.25, 0.25, 0.5, 1.0)};

2 Comments

According to the glsles documentation, glsles only supports array construction, and not initialisation. So no that doesn't work unfortunately!
Oh. That's surprising and annoying. :( What about if you just add 3 to your constructor? ... vertices[3] = vec4[3](...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.