【问题标题】:Unable to set the location for input in vertex shader无法在顶点着色器中设置输入位置
【发布时间】:2019-08-17 14:03:51
【问题描述】:

我在 Unix 19.04 操作系统中将 Qt 5.13 与 opengl 4.4 和 3.3 用于 GLSL。

请注意,我使用 QOpenGLWindow 作为 Qt 应用程序的主窗口。

我正在尝试正确链接 opengl 顶点着色器的输入。我尝试了这两种方法:

1.使用GLSL中的“location”关键字,我写了这个顶点着色器,但在编译这个顶点着色器时仍然遇到问题:

#version 330 core\n
layout (location = 0) in vec3 vertex_position;\n
layout (location = 1) in vec3 vertex_color;\n
layout (location = 2) in vec2 vertex_texcoord;\n
out vs_position;\n
out vs_color;\n
out vs_texcoord;\n
void main() {\n
 vs_position = vertex_position;\n
 vs_color = vertex_color;\n
 vs_texcoord = vec2(vertex_texcoord.x, vertex_texcoord.y*-1.0f);\n
 gl_Position = vec4(vertex_position, 1.0f);\n
}

在尝试编译所有这些之后,我从信息日志中得到了这个错误:

0:5(16): 错误:语法错误,意外';',期待'{'。

2-我尝试的另一种尝试是使用“属性”关键字,

相同的代码,但编辑以“布局”字开头的三行:

attribute vec3 vertex_position;\n
attribute vec3 vertex_color;\n
attribute vec2 vertex_texcoord;\n

之后,我使用 glBindAttribLocation() 将属性与它们的位置绑定:

...
 m_functions->glBindAttribLocation(vertex_shader, 0, "vertex_position");
 m_functions->glBindAttribLocation(vertex_shader, 1, "vertex_color");
 m_functions->glBindAttribLocation(vertex_shader, 2, "vertex_texcoord");
 m_functions->glAttachShader(shader_program, vertex_shader);
 m_functions->glLinkProgram(shader_program);
...

(其中 m_functions 是:

 initializeGL() {
 m_functions = context->functions();
 ...}

)

仍然出现同样的错误!

我尝试了上面提到的方法,但都对我不起作用!

我使用的是传统方式(函数以 gl... 开头),而不是使用 Qt 中提供的类!

【问题讨论】:

    标签: c++ qt opengl glsl shader


    【解决方案1】:

    该错误与布局位置限定符或顶点着色器输入变量各自的属性无关。
    但是顶点着色器输出变量的声明中缺少类型规范:

    out vs_position;
    out vs_color;
    out vs_texcoord;
    

    必须是:

    out vec3 vs_position;
    out vec3 vs_color;
    out vec2 vs_texcoord;
    

    注意,0:5(16) 在错误消息0:5(16): error: syntax error, unexpected ';', expecting '{'. 中。表示第 5 行和第 16 个标志。
    第 5 行的第 16 个符号是分号 (;),这是意料之外的,因为 out vs_position 不是有效的声明。

    【讨论】:

    • 抱歉,我忘记在 stackoverflow 中的帖子中添加“\n”。它已经添加到我的应用程序中。由于我是逐行写的(以确保没有错误),所以我忘记了新行
    • @hassan1551 问题是输出变量的声明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多