【发布时间】:2015-10-21 23:15:20
【问题描述】:
我的代码是(在 void main 内):
float res;
for(int i=0; i<15; i++) {
res = float(i)/15.0;
//...
}
很遗憾,我在float(i)/15.0 收到语法错误
如果我只写i/15.0,那么错误是:
wrong operand types no operation '/' exists that takes a left-hand operand of type 'mediump int' and a right operand of type 'const float' (or there is no acceptable conversion)
如果我只是尝试i/15,那么结果是一个整数,但我想得到一个浮点数。
如何将int 转换为float?
【问题讨论】:
-
在 javascript 中这个转换是自动完成的 - res = i / 15;
-
这是 GLSL,不是 javascript
-
我pasted your code above into glslsandbox.com and it works just fine。您确定没有从着色器中的其他行收到该错误吗?
-
“我在 float(i)/15.0 收到语法错误”你确定吗?这看起来很好,float(i) 是将 i 转换为 float 的正确方法。