GLSL Math

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

GLSL Math

Post by Ref » Fri Aug 30, 2013 4:13 pm

Could someone provide GLSL logic?
Why doesn't:

Code: Select all

a - ((a / b) * b)
equal zero?
see line 17 of grid.lua shader.
Attachments
grid.love
Simple shader
(1.38 KiB) Downloaded 70 times

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: GLSL Math

Post by raidho36 » Fri Aug 30, 2013 4:29 pm

Simple: foating point math has some degree of error, and it tends to accumulate along the computations.

User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: GLSL Math

Post by RedHot » Sat Aug 31, 2013 12:48 pm

Raidho is wrong, as usual. :joker:

On the serious note. Your function signature is :

Code: Select all

int mod(int a, int b) { return a - ((a / b) * b); }
GLSL acts like the C language.
In C,C++, Java (that's the languages I have observed with this behaviour) an int divided by int ALWAYS return an integer.



4/3 = 1 (not 1.(3) ). That has to do with type casting. If both arguments are integers none gets promoted to a float or double, hence the return value is an integer aswell.

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: GLSL Math

Post by raidho36 » Sat Aug 31, 2013 1:08 pm

Previous statement wasn't wrong, just not exactly related. When people are talking about GLSL, it is assumed that any numbers are floats, so did I.

It is true that integer division always results in integer result rounded down. You can cast values at any time by prefixing them with desired cast type (or use function-like syntax):

Code: Select all

float myfloat = (float)myint
float myfloat = float(myint)
float mod ( int a, int b ) { return (float)a - ( ( (float)a / (float)b ) * (float)b ); }
Last line is what you should probably use. You can omit some of the explicit typecasts, but that only would result in implicit typecast. Implicit things may not work the way you expect it, and it's side-effects could be evil, so make sure you avoid it.

User avatar
Nixola
Inner party member
Posts: 1941
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: GLSL Math

Post by Nixola » Sat Aug 31, 2013 1:13 pm

Also, if I remember correctly some video cards just hate implicit casts and won't compile any shader that uses them
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: GLSL Math

Post by vrld » Sat Aug 31, 2013 1:21 pm

Even more unrelated, GLSL already defines a modulo function, see here.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: GLSL Math

Post by RedHot » Sat Aug 31, 2013 1:26 pm

Nixola wrote:Also, if I remember correctly some video cards just hate implicit casts and won't compile any shader that uses them
Quite the opposite. A lot of ATI drivers require implcit casting as they are often reluctant to cast on their own.

@Vrld
Just my thought :)
Last edited by RedHot on Sat Aug 31, 2013 2:01 pm, edited 1 time in total.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: GLSL Math

Post by Robin » Sat Aug 31, 2013 1:46 pm

RedHot wrote:A lot of ATI drivers require implcit casting as they are often reluctant to cast on their own.
Aren't you confusing "implicit" with "explicit" there?
Help us help you: attach a .love.

User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: GLSL Math

Post by RedHot » Sat Aug 31, 2013 2:01 pm

Robin wrote:
RedHot wrote:A lot of ATI drivers require implcit casting as they are often reluctant to cast on their own.
Aren't you confusing "implicit" with "explicit" there?
I most certainly am! My bad. Robin is of course right. Dunno why I confused the two.

User avatar
Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

Re: GLSL Math

Post by Ref » Sat Aug 31, 2013 10:12 pm

Thanks all for your responses.
GLSL still is a black art for me.
Any and all help greatly appreciated.
[quote="raidho36"]

Code: Select all

float mod ( int a, int b ) { return (float)a - ( ( (float)a / (float)b ) * (float)b ); }
quote]
Added this to grid.lua and got:
ERROR: 0:23: ')' : syntax error parse error
Further playing around and found that I didn't need any of this code to run the script.
Don't know what the original intent was. Just part of the magic I guess.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 42 guests