Unable to use '%' in glsl

陌路散爱 提交于 2019-12-12 08:38:50

问题


While writing a shader program today, I encountered a situation where I have to use % to find the remainder. GLSL gave me an error saying that it is not available in the current version.

I've tried several problems. GLSL doesn't support recursive function and while loops, which is needed if I want to create a function that can give me the result of (a % b).

So, I'm currently stuck. Can someone help me with this?

Edit. I was trying to emulate a CRT screen using some shader code from this website as reference code. I wanted to modify the color of the pixels at certain row and columns, so I needed to make use of the modulo operator.

At first, I thought that the modulus function was done using the % operator. It didn't work, so I tried using recursive function/while loop as a workaround for modulus function, which didn't work either.


回答1:


GLSL doesn't support recursive function and while loops, which is needed if I want to create a function that can give me the result of (a % b).

First, neither recursion nor looping is necessary to compute a mod b. That's easily done by a - (b * floor(a/b)).

Which is exactly what the built-in mod function does. If your version of GLSL doesn't support the % operator, then you're probably not able to get real integers either. So just use the mod function on your values.



来源:https://stackoverflow.com/questions/35155598/unable-to-use-in-glsl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!