Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader Uniform Arrays #14

Closed
matthiasGmayer opened this issue Jan 23, 2018 · 4 comments
Closed

Shader Uniform Arrays #14

matthiasGmayer opened this issue Jan 23, 2018 · 4 comments

Comments

@matthiasGmayer
Copy link

Is there any way to set a shaders uniform arrays.
e.g.
glsl:
uniform vec3 lightPositions[10];

c#:
i can reference the array using shaderProgram["lightPositions[0]"], but i can only set the first value.
shaderProgram["lightPositions[1]"] or shaderProgram["lightPositions[]"] returns null.

@na2axl
Copy link

na2axl commented Jan 23, 2018

Hi @spyion ,
I think that the method used to set shaders uniform arrays in opengl4csharp should be the same than the OpenGL API so you have to do:

shaderProgram["lightPositions[0]"].SetValue(lights[0].Position);
shaderProgram["lightPositions[1]"].SetValue(lights[1].Position);
shaderProgram["lightPositions[2]"].SetValue(lights[2].Position);
// ...
shaderProgram["lightPositions[10]"].SetValue(lights[10].Position);

assuming that the variable lights is an array of Light.

Hope this will help you! :-)

@matthiasGmayer
Copy link
Author

I don't know why, but this is not working.
I managed to set an array using
Gl.ProgramUniform3fv(shaderProgram.ProgramID, Gl.GetUniformLocation(shaderProgram.ProgramID, "lightPositions[1]"), 3, positions[1].ToFloat());

@na2axl
Copy link

na2axl commented Jan 24, 2018

I've browsed the source code of opengl4csharp and it seems that the list of uniform variables is created once when the shader is compiled in OpenGL and not when the user want to request it (and this method work only for non-array variables, and maybe also for structs...)
So you can get the uniform location with GL.GetUniformLocation(shaderProgram.ProgramID, "lightPositions[0]"); and set the value with GL.Uniform3fv(location, yourXYZarray); and do it for all others array members.
You can take a look on this implementation

Hope this will help you! :-)

@giawa
Copy link
Owner

giawa commented Jan 16, 2019

Thanks for the comments (and for the slow response). Yes, the uniform variables are currently only found at link time. It should be expanded to work with other types, as mentioned. However, this work hasn't been done yet.

@giawa giawa closed this as completed Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants