【问题标题】:how to reuse same vertex shader to draw different gl_Position如何重用相同的顶点着色器来绘制不同的 gl_Position
【发布时间】:2023-04-06 19:29:01
【问题描述】:

我正在阅读这些关于现代 OpenGL 的 tutorials。在教程 5 中,有一个练习可以在立方体之外绘制一个额外的三角形。我的理解是我可以重用相同的顶点着色器来绘制多个三角形(即立方体的三角形和一个额外的独立三角形)。我的问题是顶点着色器是

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;
layout(location = 2) in vec3 vertexTriangle_modelspace;


// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){    

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1); // Cube

    //gl_Position =  MVP * vec4(vertexTriangle_modelspace,1); // Triangle

    // The color of each vertex will be interpolated
    // to produce the color of each fragment
    fragmentColor = vertexColor;
}

它只绘制了一个gl_Position 和最后一个。一个顶点着色器可以输出多个gl_Position吗?

【问题讨论】:

  • 我假设你知道顶点着色器是为每个单独的顶点运行的,所以只需要输出一个位置。
  • @MohamadElghawi,谢谢。作为一种魅力。我走错方向了

标签: c++ opengl glsl


【解决方案1】:

顶点着色器不渲染三角形。或立方体。管他呢。它们只是对顶点执行操作。该顶点是否是三角形、线带、立方体、擎天柱等的一部分。没关系。

顶点着色器接收一个顶点并写出一个顶点。对象由多个顶点组成。因此,当您发出渲染命令时,该命令中的每个顶点都会转到 VS。并且 VS 为它接收到的每个顶点写入一个顶点。

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    相关资源
    最近更新 更多