【问题标题】:Return an integer with GLSL step function使用 GLSL 阶跃函数返回一个整数
【发布时间】:2018-04-12 04:54:00
【问题描述】:

GLSL 函数 step 返回一个浮点数(0.0f1.0f)或浮点数向量。有没有办法让它返回一个整数?我正在尝试使用它来抵消索引(不引入if 语句):

const int[8] constIndicies = int[](
   0,1,2,3,
   1,3,0,2);    
...
float ratioEdge = 17.0f;
float myFloatValue = 9.0f;

... constIndicies[4*step(ratioEdge, myFloat)] ...

但是它返回一个浮点数,这是行不通的。并且将其转换为 int 似乎是错误的。

【问题讨论】:

  • GLSL 不是 Java; new int[] 不是合法的 GLSL 语法。

标签: opengl glsl


【解决方案1】:

经过一番挖掘,似乎最接近返回 intstep 函数在标量的构造函数中。

int myStepInt = int(myFloat > ratioEdge); // returns integer 1 or 0.

以上是替代

int myStepInt = myFloat > ratioEdge ? 1 : 0;

上述任何一个是否比内置的 step 函数更多/更少分支对我来说仍然是一个谜。

【讨论】:

    【解决方案2】:

    唯一的方法是将step(ratioEdge, myFloat) 转换为int。 constIndicies[4*int(step(ratioEdge, myFloat))]

    编辑: 作为对 Rabbid76 的回应,我删除了圆形建议。

    【讨论】:

    • 在这种情况下round 是没有用的,因为 step 的结果不是 0.0 就是 1.0。 round 不会改变这一点,round 的返回类型也是浮点类型。
    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2012-03-19
    相关资源
    最近更新 更多