简体   繁体   中英

OpenGLES 3.0: “Only consts can be used in a global initializer ”

I am a newbie following the book <OpenGL ES 3.0 cookbook> chapter2, but I get stuck with this error during installing the demo APP with this:

2020-12-06 16:16:10.888 7549-7578/com.demo.hellosquare E/glOpenGLES3Native: Could not compile shader 35633:
ERROR: 0:6: 'RadianAngle' : Only consts can be used in a global initializer 
ERROR: 0:6: 'RadianAngle' : Only consts can be used in a global initializer 
ERROR: 0:6: 'RadianAngle' : Only consts can be used in a global initializer 
ERROR: 0:6: 'RadianAngle' : Only consts can be used in a global initializer 
ERROR: 4 compilation errors.  No code generated.

The problem is, I don't understand what is that message trying to tell me(Google has no relevant results).

The code involves "RadianAngle" are the following places:

In the top of my single CPP file, I declared:

GLuint radianAngle;

And then with my shader also in top the same file:

static const char vertexShader[] =
    "#version 300 es                                          \n"
    "in vec4        VertexPosition;                           \n"
    "in vec4        VertexColor;                              \n"
    "uniform float  RadianAngle;                              \n"

    "out vec4       TriangleColor;                            \n"
    "mat2 rotation = mat2(cos(RadianAngle),sin(RadianAngle),  \
                 -sin(RadianAngle),cos(RadianAngle)); \n"
    "void main() {                                            \n"
    "  gl_Position   = mat4(rotation)*VertexPosition;         \n"
    "  TriangleColor = VertexColor;                           \n"
    "}\n";

Finally inside my render function(will be called through JNI) in same file:

radianAngle          = glGetUniformLocation(programID, "RadianAngle");
glUniform1f(radianAngle, radian);

Strangely, I copied exactly from the book, sigh..

The issue is related to the line:

mat2 rotation = mat2(cos(RadianAngle),sin(RadianAngle),
                     -sin(RadianAngle),cos(RadianAngle));

rotation is a variable in global scope. Global variables can only be initialized with constant expressions. RadianAngle is not constant because it is a uniform variable. This causes the error:

ERROR: 0:6: 'RadianAngle': Only consts can be used in a global initializer

The error occurs 4 times, because RadianAngle is used 4 times in the initializer of rotation .

You have to set the value of rotation in main :

mat2 rotation;

void main()
{
    rotation = mat2(cos(RadianAngle),sin(RadianAngle),
                    -sin(RadianAngle),cos(RadianAngle));

    // [...]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM