简体   繁体   English

GLSL统一数组的值不正确

[英]GLSL uniform arrays incorrect values

I am trying to pass light and lightcolor information to my shader via a uniform array. 我试图通过统一的数组将光和浅色信息传递给我的着色器。 I declared these in my shader like this: 我在着色器中这样声明了这些:

     uniform vec2 lightpositions[4];
     uniform vec4 lightcolors[4];

And in my code (C++): 在我的代码(C ++)中:

float lightpositions[8] = { 
        150, 150,
        (float) screenWidth - 150, 150,
        (float) screenWidth - 150, (float) screenHeight - 150,
        150, (float) screenHeight - 150
};

float lightcolors[16] = {
        1.0, 0.0, 0.0, 1.0,
        1.0, 1.0, 0.0, 1.0,
        1.0, 0.0, 1.0, 1.0,
        0.0, 1.0, 1.0, 1.0
};

glUniform2fv(lightposHandle, amount, lightpositions);
checkGlError("lightPositions");

glUniform4fv(lightcolorHandle, amount, lightcolors);
checkGlError("lightColors");

Where amount is 4 in this case. 在这种情况下,金额为4。

When I check my OpenGL ES debugger (PerfHud ES) I see that the values in the arrays are almost random like 1.4e+02. 当我检查我的OpenGL ES调试器(PerfHud ES)时,我发现数组中的值几乎是随机的,如1.4e + 02。

Am I doing something wrong? 难道我做错了什么? Or should I use attributes in this case? 还是在这种情况下应该使用属性?

Your shader is looks correct (see section 4.1.9 of the "ESSL specification" for a very similar declaration) and so does your API code. 您的着色器看起来正确(关于声明非常相似,请参见“ ESSL规范”的 4.1.9节),API代码也是如此。

My best guess is that is that something goes wrong when you query the uniform locations, and one or more of the locations are returned as -1. 我最好的猜测是,查询统一位置时出了点问题,并且一个或多个位置返回为-1。 Try checking after each call to glGetUniformLocation if -1 was returned. 如果返回-1,则在每次调用glGetUniformLocation之后尝试检查。

(The names used in the calls to glGetUniformLocation in the drawing code you posted do not match the shader code in your questions. This could be the bug you're looking for.) (在您发布的图形代码中调用glGetUniformLocation时使用的名称与问题中的着色器代码不匹配。这可能是您要查找的错误。)

Allright I solved it. 好吧,我解决了。

I created my arrays in a class which I then passed to my Lights class via the constructer. 我在一个类中创建了数组,然后通过构造函数将其传递给我的Lights类。 Where my light class assigns this array to a private float* lightpositions. 我的灯光类将这个数组分配给私有float *灯光位置。 This was the source of the wrong values, because I think (if someone could make this clear) that I was passing in the address of the array. 这是错误值的根源,因为我认为(如果有人可以说清楚)我正在传递数组的地址。 When I create a normal array (float test[8]) the values passed to the shader are correct. 当我创建一个普通数组(float test [8])时,传递给着色器的值是正确的。

You can not use array notation like this in OpenGL ES 2.0 shader code. 您不能在OpenGL ES 2.0着色器代码中使用像这样的数组符号。 That is a 3.0 feature. 这是一个3.0功能。

Also, I would try it on another platform with different OpenGL ES drivers. 另外,我会在具有不同OpenGL ES驱动程序的另一个平台上尝试。 You might also need the precision specifiers, like this: 您可能还需要精度说明符,如下所示:

uniform highp vec2 lightpositions[4];
uniform highp vec4 lightcolors[4];

For OpenGL ES 3.0, you would need to construct the array as an array of vec3, like this: 对于OpenGL ES 3.0,您需要将数组构造为vec3数组,如下所示:

vec3 lightcolors[6] = 
{ 
    vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f), 
    vec3(1.0f, 1.0f, 0.0f), vec3(1.0f, 0.0f, 1.0f), vec3(0.0f, 1.0f, 1.0f) 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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