mod 函数是不是在 OpenGL 3.3 核心中返回 highp 浮点数?

Posted

技术标签:

【中文标题】mod 函数是不是在 OpenGL 3.3 核心中返回 highp 浮点数?【英文标题】:Does mod function returns highp float in OpenGL 3.3 core?mod 函数是否在 OpenGL 3.3 核心中返回 highp 浮点数? 【发布时间】:2020-05-08 00:41:21 【问题描述】:

我试图理解为什么以下代码会在几何着色器中生成以下错误:

#version 330 core

layout (points) in;
layout (line_strip, max_vertices = 256) out;

in int gs_timestampLabel[];

...

int numberChar[10];
int number = gs_timestampLabel[0];
numberChar[digit] = mod(number, 10);

...

我也试过了:

numberChar[digit] = number - 10 * floor(number/10); // number % 10

错误:

Error compiling shader: shaders/TimestampLabels.geometry_2.glsl
Shader log: ERROR: 0:774: 'assign' :  cannot convert from 'highp float' to 'highp int'

mod 总是返回浮点数吗?应该怎么做才能使用整数正确完成这个模运算?

【问题讨论】:

【参考方案1】:

GLSL 中的mod 函数始终是浮点mod。 GLSL 3.30 可以将整数隐式转换为浮点数,但不会将浮点数隐式转换为整数。因此出现错误。

如果您想要一个整数 mod,请执行您在 C++ 中会执行的操作:使用 %

【讨论】:

出于某种原因,我认为您不能使用 %。 % 已与 GLSL 130 (OpenGL 3.0) 一起引入,因此 mod() 是与旧 OpenGL 2.x 兼容的代码的唯一选项OpenGL ES 2.0,但这不是你的情况。

以上是关于mod 函数是不是在 OpenGL 3.3 核心中返回 highp 浮点数?的主要内容,如果未能解决你的问题,请参考以下文章