如何在GLSL ES中编写const数组 [英] How to write const array in GLSL ES
问题描述
我正在尝试为iPhone上的OpenGL ES应用程序编写一个简单的顶点着色器,但是我的数组构造函数给我带来了麻烦.
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:
错误:0:13:'(':语法错误:数组大小必须出现在变量名之后
ERROR: 0:13: '(' : syntax error: Array size must appear after variable name
推荐答案
与ES 2.0一起使用的GLSL版本不支持常量数组.从规范第30页的"4.3.2常量限定符"部分开始:
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.
在ES 3.0中取消了此限制,该限制在相应的部分中说明:
This restriction is lifted in ES 3.0, where it says in the corresponding section:
const限定符可以与任何非无效的透明基本数据类型以及它们的结构和数组一起使用.
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);
或者您可以使用一系列if
语句.
Or you could use a series of if
-statements.
这篇关于如何在GLSL ES中编写const数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!