11

I want to try a lighting example from the book OpenGL ES 2.0 Programming Guide. In the shader they have made two structures.

struct directional_light   
{  

    vec3 direction; // normalized light direction in eye space  
    vec3 halfplane; // normalized half-plane vector  

    vec4 ambient_color;
    vec4 diffuse_color;
    vec4 specular_color;
};

struct material_properties 
{ 
    vec4 ambient_color; 
    vec4 diffuse_color; 
    vec4 specular_color; 
    float specular_exponent;
};

They have also made two uniforms, based on these structures.

uniform  material_properties u_material_properties;   
uniform directional_light u_directional_light;

The problem is, I do not know how to pass own structures into the actual shader.

I want to create the same structures in my main code and pass the objects into the shader. How is this possible?

Regards Niclas

1 Answer 1

12

You can't, OpenGL ES doesn't have that functionality, to upload you have to get the location of each of your nested variables and call glUniform* on each of them.

For example:

GLuint loc = glGetUniformLocation(program, "u_material_properties.ambient_color");
glUniform4f(loc, 1.0, 1.0, 1.0, 0.0);
Sign up to request clarification or add additional context in comments.

4 Comments

Do you know how I pass in variables? Lets say I have a vec3 in my shader : u_directional_light.direction. I am trying to pass in a GLfloat array but it doesnt seem to work. uniforms[UNIFORM_DIRECTIONAL_LIGHT_DIRECTION] = glGetUniformLocation(program, "u_directional_light.direction"); GLfloat na_ambient_color[] = {0.0,1.0,0.0}; glUniform1fv(UNIFORM_DIRECTIONAL_LIGHT_DIRECTION, 1, (GLfloat *) na_ambient_color);
You need to use glUniform3fv for that. And glUniform4fv for vec4's, etc.
how to access if there a array of structures, like : "uniform directional_light lights[8];" ?
i assume , "GLuint loc = glGetUniformLocation(program, "lights[0].ambient_color"); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.