opengl 如何将变量从顶点着色器移动到几何着色器?

ubbxdtey  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(128)

我有以下顶点着色器:

#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;
}

我想在几何着色器中接收outOHLCoutVolumeoutTimestamp。我这样写:

#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中是否存在这种转换?)或float变量定义。

z4iuyo4d

z4iuyo4d1#

仔细阅读留言:
几何着色器输入可变变量必须声明为数组
Geometry shader的输入是基元。这意味着构成图元的顶点着色器的所有输出都是合成的。因此,几何体着色器的输入是数组:

in ivec4 inOHLC[];
in int inVolume[];
in int inTimestamp[];

此外,您必须指定几何着色器的输入图元类型。例如,对于一条线:

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;

相关问题