opengl-es - 如何将 int 转换为在 GLSL(WebGL)中 float ?

标签 opengl-es glsl webgl

我的代码是(在 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那么结果是一个整数,但我想得到一个浮点数。

怎么可能投intfloat ?

最佳答案

It seems不允许您在 GLSL 中进行转换。因此,“你必须使用 a constructor”。

试试这个:

// http://www.shaderific.com/glsl-types/
// "Implicit type conversions are not supported.
// Type conversions can be done using constructors..."
float i_float = float(i);
res = i_float / 15.0;

PS:如果你看看at the documentation ,它说“......任何整数类型都可以转换为浮点数,整数和浮点数都可以转换为 double 数。” ...我觉得奇怪的是您的代码不被 GLSL 编译器接受。 (参见 Reto Koradi 的评论)

关于opengl-es - 如何将 int 转换为在 GLSL(WebGL)中 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270823/

相关文章:

c++ - 如何在 OpenGL 中更好地制作 2D 光照

webgl - WebGL 着色器输出是否应该针对 Gamma 进行调整?

javascript - 在 webgl 中制作 2d HUD 的推荐方法

webgl - 学习 WebGL 和 Three.js

iphone - 学习 opengl es 2.0 有什么好的方法吗?

iphone - OpenGL ES - 如何使某些对象保持固定大小?

android - 如何在 C 中将 *.vert 和 *.frag 转换为 char?

java - Open Gl ES 2.0 三角形始终为黑色

opengl - 着色器 - 镜面闪光 - 闪闪发光的效果

c++ - 使用 `glm::perspective` 时,Z 值始终为 1 或 -1