0

I would like to have a generic solution for adding parameters to shaders via openTK. OpenTK offers implementations for different types of parameters (float, int, double...) via SetUniform(). Now in my program I would like to do something like ShaderParam<T>(string name, params T[] values). Unfortunately I have two problems with this. I see no good solution to store objects of ShaderParam and I'm not sure what's the most elegant solution to translate this to the openTK interface. I don't like to do a switch(typeof(T)), is there a more generic way to do it?

pixartist
  • 1,137
  • 2
  • 18
  • 40

2 Answers2

0

The OpenGL specification provides different functions for each uniform type: glUniform[1234][fui][v]. In practical terms, this means that the switch(typeof(T)) is mandated by the fundamental design of OpenGL.

All is not lost, however! OpenGL 3.1 and higher include Uniform Buffer Objects that allow you to upload uniforms using GL.BufferData<T> or GL.MapBuffer. If you can live with the requirement of OpenGL 3.1+, UBOs lead to cleaner and faster code.

j-p
  • 1,622
  • 10
  • 18
The Fiddler
  • 2,726
  • 22
  • 28
  • [overloading, what it means exactely](http://en.wikipedia.org/wiki/Function_overloading), mandated by the common sens – j-p Apr 16 '14 at 08:51
  • where have you seen reference to generic type in my answer? where have you seen ?? – j-p Apr 16 '14 at 23:37
  • I am trying to answer the original question, which explicitly mentions generics: `I would like to have a generic solution for adding parameters to shaders via openTK`. Please be constructive and try to stay on topic. – The Fiddler Apr 17 '14 at 08:14
  • Ok, I was maybe a bit aggaced, sorry. But be constructive too, and replace `overloads`by functions. – j-p Apr 17 '14 at 12:21
-1

OpenTK api wrap OpenGL api in c that does not handle overloading. The wrapping is automated and for now, it strip OpenGLTypes and create overloads.

Edit: (specialy for fidler)

Overloading is achieve declaring same function name pointing to different entry point, exemple:

[AutoGenerated(Category = "ARB_shader_objects", Version = "", EntryPoint = "glUniform1ivARB")]
[CLSCompliant(false)]
public static void Uniform1(Int32 location, Int32 count, Int32[] value);

[AutoGenerated(Category = "ARB_shader_objects", Version = "", EntryPoint = "glUniform1fvARB")]
[CLSCompliant(false)]
public static unsafe void Uniform1(Int32 location, Int32 count, Single* value);

The most elegant way would be to hack directly OpenTK and improve the wrapper generator, stripping the array and matrix qualifier of the uniform functions.

j-p
  • 1,622
  • 10
  • 18
  • @The Fiddler: I've not mention generic type, I'm talking about extending actual gl functions overloading, which is limited to gl types stripping]. [where to start, line 43...](https://github.com/andykorth/opentk/blob/master/Source/Bind/FuncProcessor.cs) – j-p Apr 16 '14 at 23:04
  • The original question explicitly asks about generics, not overloading: `ShaderParam(string name, params T[] values)`. The only way to upload uniforms via generics is UBOs - no amount of overloading will change that fact. – The Fiddler Apr 17 '14 at 08:12