gpt4 book ai didi

c++ - 无法让 openGL 的 glDrawElements 与几何着色器一起工作

转载 作者:行者123 更新时间:2023-11-30 05:07:29 25 4
gpt4 key购买 nike

我已经附加了着色器,但我找不到任何关于如何将 glDrawElements 与附加到着色器程序的几何着色器一起使用的信息。该程序会在没有几何着色器的情况下在屏幕上输出四边形,现在我正在尝试做同样的事情,但附加了几何着色器。

//In my .cpp file
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


// Vertex shader
#version 440
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;

uniform mat4 world_matrix;
uniform mat4 view_matrix;
uniform mat4 projection_matrix;

out vec3 color;

void main() {
color = vertex_color;

gl_Position = projection_matrix*view_matrix* world_matrix *
vec4(vertex_position, 1.0);

}

// Geometry shader
#version 440 core
layout (triangle_strip) in;
layout (triangle_strip, max_vertices = 6) out;
layout(location = 1) in vec3 vertex_color;

out vec3 color;

void main()
{
for(int i = 0; i < gl_in.length(); i++)
{
// copy attributes
gl_Position = gl_in[i].gl_Position;

color=vertex_color;
// done with the vertex
EmitVertex();
}
EndPrimitive();

}

//Fragment shader
#version 440
in vec3 color;
out vec4 fragment_color;

void main () {
fragment_color = vec4 (color, 1.0);
}

最佳答案

请参阅 Khronos Group 的便捷 OpenGL wiki 站点以获取 Shader stage inputs and outputs :

Global variables declared with the in qualifier are shader stage input variables. These variables are given values by the previous stage (possibly via interpolation of values output from multiple shader executions).

Global variables declared with the out qualifier are shader stage output variables. These values are passed to the next stage of the pipeline (possibly via interpolation of values output from multiple shader executions).

Geometry Shader inputs are aggregated into arrays, one per vertex in the primitive. The length of the array depends on the input primitive type used by the GS. Each array index represents a single vertex in the input primitive.

您有一个顶点着色器、一个几何着色器和一个片段着色器。在这种情况下,顶点着色器是第一个着色器阶段,然后是几何着色器,最后一个着色器阶段是片段着色器。
所以几何着色器的输入变量必须匹配到顶点着色器的输出变量。片段着色器的输入变量必须匹配几何着色器的输出变量。

进一步注意,可能的输入图元说明符是pointslineslines_adjacencytriangles三角形邻接
另见 Geometry Shader - Primitive in/out specification .


这意味着您的代码必须看起来像这样:

顶点着色器:

#version 440
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;

uniform mat4 world_matrix;
uniform mat4 view_matrix;
uniform mat4 projection_matrix;

out vec3 vert_stage_color;

void main()
{
vert_out_color = vertex_color;

gl_Position = projection_matrix*view_matrix* world_matrix * vec4(vertex_position, 1.0);
}

几何着色器:

#version 440 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 6) out;
layout(location = 1) in vec3 vertex_color;

in vec3 vert_stage_color[];
out vec3 geo_stage_color;

void main()
{
for(int i = 0; i < gl_in.length(); i++)
{
// copy attributes
gl_Position = gl_in[i].gl_Position;

geo_stage_color = vert_stage_color[i];
// done with the vertex
EmitVertex();
}
EndPrimitive();
}

片段着色器:

#version 440
in vec3 geo_stage_color;
out vec4 fragment_color;

void main ()
{
fragment_color = vec4(geo_stage_color, 1.0);
}

关于c++ - 无法让 openGL 的 glDrawElements 与几何着色器一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com