2015-02-10 29 views
3

我想为iPhone上的OpenGL ES应用程序编写一个简单的顶点着色器,但是我的数组构造函数给我带来了麻烦。如何在GLSL中编写const数组ES

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]; 

} 

当使用此代码着色器无法编译,给我的eror消息:

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

回答

-1

您是否尝试过牙套?

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)}; 
+0

根据glsles文档,glsles只支持数组构造,而不是初始化。所以没有,不幸的是不行! – Oliver 2015-02-10 15:38:03

+0

哦。这是令人惊讶和烦人的。 :(如果你只是添加'3'到你的构造函数中?'vertices [3] = vec4 [3](...)' – 2015-02-10 15:46:26

+0

不是可悲的不是:( – Oliver 2015-02-10 16:02:36

7

与ES 2.0一起使用的GLSL版本不支持常量数组。从部分“4.3.2常量限定符”规范的第30页上:

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

这个限制被提升在ES 3.0,在那里它说在相应的部分:

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

作为替代方案,你应该能够使用非常量数组,你一(未经测试)分配值之一:

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); 

或者你可以使用一系列if -statements。

+0

那么我该怎么做呢? – Oliver 2015-02-10 17:11:44

+0

我添加一个替代答案 – 2015-02-11 06:01:09

+0

Reto,如果声明在函数体之外,则替代方法将不起作用,最好的方法是声明它为“uniform vec4 vertices [3]”并指定C代码中的值。 – prideout 2015-09-08 21:41:42