我具有以下顶点着色器:
#version 330
layout (location = 0) in ivec4 inOHLC;
layout (location = 1) in int inVolume;
layout (location = 2) in int inTimestamp;
out ivec4 outOHLC;
out int outVolume;
out int outTimestamp;
void main()
{
outOHLC = inOHLC;
outVolume= inVolume;
outTimestamp = inTimestamp;
}
并且我想在几何着色器中接收outOHLC
,outVolume
和outTimestamp
。我对此进行编码:
#version 330
in ivec4 inOHLC;
in int inVolume;
in int inTimestamp;
layout (line_strip, max_vertices = 2) out;
void main() {
float x = (float)inTimestamp / 100.0;
float y1 = (float)inOHLC[1] / 100.0;
float y2 = (float)inOHLC[1] / 100.0;
gl_Position = vec4(x, y1, 0.0, 0.0);
EmitVertex();
gl_Position = vec4(x, y2, 0.0, 0.0);
EmitVertex();
EndPrimitive();
}
但出现以下错误:
run:
. . . vertex compilation success.
. . . geometry compilation failed.
Shader Info Log:
ERROR: 7:1: 'inOHLC' : geometry shader input varying variable must be declared as an array
ERROR: 8:1: 'inVolume' : geometry shader input varying variable must be declared as an array
ERROR: 9:1: 'inTimestamp' : geometry shader input varying variable must be declared as an array
ERROR: 15:1: ')' : syntax error syntax error
在@ Rabbid76注释之后,我将代码修改如下:
#version 330
in ivec4 ohlc[];
in int volume[];
in int timestamp[];
layout (line_strip, max_vertices = 2) out;
void main()
{
float x = (float)timestamp / 100.0;
float y1 = (float)ohlc[1] / 100.0;
float y2 = (float)ohlc[2] / 100.0;
gl_Position = vec4(x, y1, 0.0, 0.0);
EmitVertex();
gl_Position = vec4(x, y2, 0.0, 0.0);
EmitVertex();
EndPrimitive();
}
现在我只收到一个错误:
run:
. . . vertex compilation success.
. . . geometry compilation failed.
Shader Info Log:
ERROR: 40:1: ')' : syntax error syntax error
我想这与(float)
强制转换(甚至在GLSL中存在吗?)或浮点变量定义有关。
仔细查看消息:
几何着色器输入变化变量必须声明为数组
Geometry shader的输入是基元。这意味着构成原始图元的顶点着色器的所有输出都组成了。因此,几何着色器的输入将重新排列:
in ivec4 inOHLC[];
in int inVolume[];
in int inTimestamp[];
此外,您必须为几何着色器指定input primitive type。例如一行:
layout(lines) in;
原始类型lines
表示几何着色器接收2个顶点(输入的数组大小为2)。
GLSL没有像C这样的转换运算符。如果要将int
转换为float
,则必须构造一个新的float
。重载的float
构造函数接受int
参数:
float y1 = (float)ohlc[1] / 100.0;
float y1 = float(ohlc[1]) / 100.0;
索引从0开始而不是1(例如在C,C ++,C#,Java,JavaScript,Python等中:)]
float y1 = float(ohlc[0]) / 100.0;
float y2 = float(ohlc[1]) / 100.0;