Page 2 of 2

Re: cindy - [0-255] color range in LÖVE 11

Posted: Wed Nov 22, 2023 7:01 pm
by BurrickSlayer
Just recently I noticed that the 0..1 floating point color range doesn't work so well in one specific area of LÖVE: vertex colors.

Consider the following code snippet:

Code: Select all

local COLOR_INDEX = 3
local mesh = love.graphics.newMesh(3)
mesh:setVertexAttribute(1, COLOR_INDEX, 0.1, 0.2, 0.3, 0.4)
print(mesh:getVertexAttribute(1, COLOR_INDEX))
getVertexAttribute does not return the values that were originally passed to setVertexAttribute. This is because each color component of a vertex is represented by a single byte internally. So setVertexAttribute crams the floating point values into a 0..255 value range and getVertexAttribute converts them back into floating point numbers, leading to some loss in precision.

But this can't be fixed by cindy or any other library. I consider it a small flaw in how this specific part of the API was designed. :monocle:

Best regards

Re: cindy - [0-255] color range in LÖVE 11

Posted: Thu Nov 23, 2023 12:16 am
by slime
BurrickSlayer wrote: Wed Nov 22, 2023 7:01 pm getVertexAttribute does not return the values that were originally passed to setVertexAttribute. This is because each color component of a vertex is represented by a single byte internally.
The data type of color components (and any vertex attribute component) isn't fixed like that. You can make a mesh that uses any supported data type for its vertex attributes, for example 16 bit instead of 8 bit fixed-point values for colors.

Re: cindy - [0-255] color range in LÖVE 11

Posted: Thu Nov 23, 2023 6:38 pm
by BurrickSlayer
Ahh, thanks, I knew I had missed something! Actually I forgot about this newMesh overload where you can specify a custom vertex format.

In my case it would be probably somewhat overkill to define the color components as float, only for the sake of having setters and getters that behave symmetrically. I have settled with "normalizing" the input colors (Math.floor(red * 255) / 255 etc.), so I can compare them with the output colors. But it's good to be reminded of this newMesh overload again.

Best regards