无法在glsl中使用'%' [英] Unable to use '%' in glsl
问题描述
今天在编写着色器程序时,遇到一种情况,我必须使用%
查找其余部分. GLSL给我一个错误,说它在当前版本中不可用.
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.
我尝试了几个问题. GLSL不支持递归函数和while循环,如果我想创建一个可以给我(a % b)
结果的函数,则需要GLSL.
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.
起初,我认为模量函数是使用%
运算符完成的.它没有用,所以我尝试使用递归函数/while循环作为解决方法
用于模数函数,该函数也不起作用.
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.
推荐答案
GLSL不支持递归函数和while循环,如果我想创建一个可以给我(a%b)结果的函数,则需要这.
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).
首先,递归或循环对于计算a mod b
都是不必要的. a - (b * floor(a/b))
可以轻松做到这一点.
First, neither recursion nor looping is necessary to compute a mod b
. That's easily done by a - (b * floor(a/b))
.
这正是内置mod
函数的功能.如果您的GLSL版本不支持%
运算符,则您也可能无法获得实数.因此,只需在值上使用mod
函数即可.
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.
这篇关于无法在glsl中使用'%'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!