giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
232 stars 61 forks source link

Shader Uniform Arrays #14

Closed matthiasGmayer closed 5 years ago

matthiasGmayer commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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.