Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using templates to manage glUniform functions

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

like image 607
fho Avatar asked Aug 02 '10 10:08

fho


1 Answers

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 ?

like image 174
Calvin1602 Avatar answered Sep 28 '22 07:09

Calvin1602