如何在 GLSL ES 中编写 const 数组 [英] How to write const array in GLSL ES

查看:21
本文介绍了如何在 GLSL ES 中编写 const 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 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:

ERROR: 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 限定符可以与任何非 void 透明基本数据类型以及它们的结构和数组一起使用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆