在 vertexShader 中使用 OpenGl 未声明的标识符,我在顶点着色器中绘制三角形时遇到问题

Posted

技术标签:

【中文标题】在 vertexShader 中使用 OpenGl 未声明的标识符,我在顶点着色器中绘制三角形时遇到问题【英文标题】:undeclared identifier with OpenGl in vertexShader, i have a problem drawing a triangle in the vertex shader 【发布时间】:2020-07-05 22:31:43 【问题描述】:
"Fallo al compilar vertexShader ",  gl_position" : undeclared identifier.

还有:

"assign" : cannot convert from "attribute 4-component vector of highp float" to "highp float"

当我编译这个脚本时,我得到了上面的错误,我真的不明白为什么它会在 VertexShader 中给出这个错误

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

static unsigned int CompileShader(unsigned int type, const std::string& source)

    unsigned int  id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE)
    
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        std::cout << "falló al compilar el shader!" << (type == GL_VERTEX_SHADER ? "vertex" : 
        "fragment") << "shader!" <<
         std::endl;
    std::cout << message << std::endl;
    glDeleteShader(id);
        return 0;
        


 

 return id;
 
 static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
 
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;



int main(void)

    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;



    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    
        glfwTerminate();
        return -1;
    

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)
        std::cout << "error" << std::endl;

    std::cout << glGetString(GL_VERSION) << std::endl;

    float positions[6] = 
        -0.5f,-0.5f,
         0.0f, 0.5f,
         0.5f, -0.5f
    ;

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);


    std::string vertexShader =
        "#version 330 core\n"
        "\n"
        "layout(location = 0) in vec4 position;\n"
        "\n"
        "void main()\n"
        "\n"
        "gl_position = position;\n"
        "\n";
    std::string fragmentShader =
        "#version 330 core\n"
        "\n"
        "layout(location = 0) out vec4 color;\n"
        "\n"
        "void main()\n"
        "\n"
        "   color = vec4(1.0,0.0,0.0,1.0); \n"
        "\n";
    unsigned int shader = CreateShader(vertexShader,fragmentShader);
    glUseProgram(shader);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);
   

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    

    glDeleteProgram(shader);
    glfwTerminate();
    return 0;

【问题讨论】:

在您的顶点着色器中,将gl_position 替换为gl_Positiongl_position 不是 OpenGL 着色语言 (GLSL) 中的内置变量,因为 GLSL 区分大小写。 Here's a link 到 GLSL 中的内置变量列表。 是的,它有效!感谢您的帮助和额外的信息! 【参考方案1】:

GLSL 区分大小写,您应该使用 gl_Position 而不是 gl_position。不幸的是,我无法重现第二个错误。

【讨论】:

哦,它有效!大写两个错误都消失了,谢谢!

以上是关于在 vertexShader 中使用 OpenGl 未声明的标识符,我在顶点着色器中绘制三角形时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL学习——Shader

OpenGL3:高级篇 GLSL

OpenGL ES 2.0 渲染管线 学习笔记

绘制球体OpenGL C++

OpenGL学习——绘制三角形

OpenGL的版本号历史和发展