从WebGl代码中访问顶点着色器中的结构体

6
如何在顶点着色器中访问此轻量级结构体?
struct LightInfo {
    vec4 Position; // Light position in eye coords.
    vec3 La; // Ambient light intensity
    vec3 Ld; // Diffuse light intensity
    vec3 Ls; // Specular light intensity
};
uniform LightInfo Light;

我尝试过:

shaderProgram.Light = gl.getUniformLocation(shaderProgram, "Light");

这段代码可以编译通过,但是当我尝试插入一些数值时:

gl.uniform4f(
    shaderProgram.Light.Position,
    parseFloat(document.getElementById("lightPositionX").value),
    parseFloat(document.getElementById("lightPositionY").value),
    parseFloat(document.getElementById("lightPositionZ").value),
    0.0
);

完全中断了。 如何将此位置数据发送到着色器结构中?
1个回答

19
你需要为每个基础类型获取一个位置。在你的例子中,
struct LightInfo {
    vec4 Position; // Light position in eye coords.
    vec3 La; // Ambient light intensity
    vec3 Ld; // Diffuse light intensity
    vec3 Ls; // Specular light intensity
};
uniform LightInfo Light;

意味着您需要

var lightPosLoc = gl.getUniformLocation(program, "Light.Position");
var lightLaLoc  = gl.getUniformLocation(program, "Light.La");
var lightLdLoc  = gl.getUniformLocation(program, "Light.Ld");
var lightLsLoc  = gl.getUniformLocation(program, "Light.Ls");

如果您有数组,您需要为每个数组的每个字段分配位置,如下所示。
struct LightInfo {
    vec4 Position; // Light position in eye coords.
    vec3 La; // Ambient light intensity
    vec3 Ld; // Diffuse light intensity
    vec3 Ls; // Specular light intensity
};
uniform LightInfo Lights[2];

var light0PosLoc = gl.getUniformLocation(program, "Lights[0].Position");
var light0LaLoc  = gl.getUniformLocation(program, "Lights[0].La");
var light0LdLoc  = gl.getUniformLocation(program, "Lights[0].Ld");
var light0LsLoc  = gl.getUniformLocation(program, "Lights[0].Ls");
var light1PosLoc = gl.getUniformLocation(program, "Lights[1].Position");
var light1LaLoc  = gl.getUniformLocation(program, "Lights[1].La");
var light1LdLoc  = gl.getUniformLocation(program, "Lights[1].Ld");
var light1LsLoc  = gl.getUniformLocation(program, "Lights[1].Ls");

以及数组的数组等。

struct LightInfo {
    vec4 Positions[2]; // Light positions in eye coords.
    vec3 La; // Ambient light intensity
    vec3 Ld; // Diffuse light intensity
    vec3 Ls; // Specular light intensity
};
uniform LightInfo Lights[2];

var light0Pos0Loc = gl.getUniformLocation(program, "Lights[0].Positions[0]");
var light0Pos1Loc = gl.getUniformLocation(program, "Lights[0].Positions[1]");
var light0LaLoc   = gl.getUniformLocation(program, "Lights[0].La");
var light0LdLoc   = gl.getUniformLocation(program, "Lights[0].Ld");
var light0LsLoc   = gl.getUniformLocation(program, "Lights[0].Ls");
var light1Pos0Loc = gl.getUniformLocation(program, "Lights[1].Positions[0]");
var light1Pos1Loc = gl.getUniformLocation(program, "Lights[1].Positions[1]");
var light1LaLoc   = gl.getUniformLocation(program, "Lights[1].La");
var light1LdLoc   = gl.getUniformLocation(program, "Lights[1].Ld");
var light1LsLoc   = gl.getUniformLocation(program, "Lights[1].Ls");

etc...


这将是可能的最佳解释。谢谢。 - pjercic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接