Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a structure in vertex shader from the code in WebGl

How can I reach this light structure in my vertex shader?

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;

I tried:

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

which compiles, but when I try to insert some value like:

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

It breaks completely. How would one send this position data into a shader structure?

like image 650
pjercic Avatar asked Dec 08 '22 19:12

pjercic


1 Answers

you must get a location for each base type. In your example

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;

Means you need

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

If you have arrays you'd need locations for each field of each array as in

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

And arrays of arrays etc.

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

like image 111
gman Avatar answered May 16 '23 09:05

gman