4

我怎样才能在我的顶点着色器中达到这个光结构?

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

它完全破裂。如何将此位置数据发送到着色器结构中?

4

1 回答 1

14

您必须为每种基本类型获取一个位置。在你的例子中

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...

于 2012-11-22T06:59:37.140 回答