Ofelia icon indicating copy to clipboard operation
Ofelia copied to clipboard

Passing an array of floats to a shader using setUniform1fv

Open dom1817 opened this issue 5 years ago • 2 comments

If I use the function setUniform1fv to pass an array of floats to a shader I get this error: Error in ofShader::setUniform1fv (arg 3), expected 'float const *' got 'table'

This probably happens because in the Lua script I define the float array as a table: a = {} a[1] = 2.0 a[2] = 4.0

Is there a way to pass the right variable type?

dom1817 avatar Sep 23 '19 09:09 dom1817

I don't think this is possible yet as the Lua bindings don't know how to translate from a table to a float vector. This would need to be handled by a custom typemap ala ofData binary string -> binary data bytes.

danomatika avatar Sep 30 '19 15:09 danomatika

I ended up passing my float array as a 3×3 matrix, which is also semantically correct as I was passing a convolution matrix. Actually I'm not aware of many uses of uniform arrays in shaders.

In case someone else needs this:

local matrix = ofMatrix3x3()
matrix:set(1,1,1,0,0,0,1,1,1)
local shader = ofShader()
shader:setUniformMatrix3f("convolution", matrix:mat3())

dom1817 avatar Sep 30 '19 15:09 dom1817