views:

51

answers:

1

Hi again and welcome to another 'wouldn't it be great if we'd combine two things that i really don't understand anything about'-question ;)

This Episode: OpenGL Uniforms and c++ Templates

The Idea: Wouldn't it be great if you could write a single template function to set uniforms in an OpenGL shader?

The Problem: Uniforms in shaders are set using a number of glUniform* functions. These differ in:

  1. Type: there are several types a uniform can take. This results in different function names in OpenGL looking like: glUniform1f, glUniform1i, glUniform1fv, etc.

    These could easily be 'unified' using overloading of functions. But this results in a huge amount of similiar functions.

    First question: is it possible to use templates in this place? As far as i understand templates you would have to specialize for every new function. And in the end you'd have the same amount of functions like with simple overloading.

  2. Parameter Count: The number in glUniform functions referes to the number the given parameters. There are one to 4 parameters which relates to simple values up to vectors with 4 elements.

    Second question: is it possible to implement a 'dynamic' parameter count with templates?

  3. Matrices: To make things complicated there is another class of uniform functions which send matrices but as I am not really using these I am not really interested in them.

Possibly there is no real way to simplify uniform methods ... but maybe someone can come up with a great solution using mighty templates?

Kind regards, Florian

+2  A: 

1 : Yes, and yes, but at least when the compiler complains that it can't choose between float and double overloads, you can force him very simply, ie setUniform<float>(blah) instead of setUniform(blah)

2 : This time overloading is a simpler solution :)

3 : What is the question ?

The question is, why would you want to do such a thing ?

Calvin1602
oh, wow, thanks for the edit
Calvin1602
I guess i am just turned down by the huge number of functions that look nearly identical.
Florian
The name convention is quite simple : gl + FunctionName + NumParameters + ParamType, with ParamType ending by 'v' if it's a pointer
Calvin1602
yes ... but afaik there is no way in c to 'create' function(pointers) based on strings
Florian
@Florian I just wanted to say that when you code, it's pretty simple to know which one of the 100 setUniform functions you actually want to call : you just have to follow said convention. My remark had nothing to do with C.
Calvin1602
ah ok ... i guess have to keep my x methods then ;)
Florian