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

标签 c++ opengl shader geometry-shader

我已经附加了着色器,但我找不到任何关于如何将 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/

相关文章:

c++ - 具有智能指针的虚拟构造函数习语

c++ - 如何从 MAC 中的/Library/Framework 文件夹中包含 C++ 中的文件

c++ - 单精度不是 6 位数字保证吗?

java - libGDX - TextButton 字体的自定义着色器

opengl - 一个OpenGL程序如何与不同的显卡接口(interface)

c++ - gl_FragCoord 与 ivec2 转换

unity3d - 在 Unity 着色器中波动浮点值

c++ - 将 Apache ActiveMQ 与 C/C++ 结合使用

shader - Ogre 渲染 + 顶点着色器

android - 如何在不剪裁基础纹理的情况下将纹理叠加在另一个纹理上?