【问题标题】:Error when writing a shader in OpenGL Xcode [closed]在 OpenGL Xcode 中编写着色器时出错 [关闭]
【发布时间】:2021-12-01 03:10:30
【问题描述】:

我正在使用什么:

  1. MacBook Pro 13 2015
  2. MacOS 11.6 大苏尔
  3. Xcode 13.0

在我的 main.cpp 文件中将着色器作为字符串写入时出错。

错误如下:

Failed to compile vertex
ERROR: 0:1: '' :  version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:3: 'layout' : syntax error: syntax error

Failed to compile fragment
ERROR: 0:1: '' :  version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:3: 'layout' : syntax error: syntax error
    

着色器:

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";

完整的主文件:

#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 << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << 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);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    //writing to the console the version of OpenGL
    std::cout << glGetString(GL_VERSION) << std::endl;

    //float array for the vertexes of the triangle, in pairs
    float position[6]{
        -0.5f, -0.5f,
         0.0f,  0.5f,
         0.5f, -0.5f
    };

    //an unsigned int which is used as the buffer
    unsigned int buffer;
    //selecting buffer as a buffer
    glGenBuffers(1, &buffer);
    //choosing the buffer "buffer"
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    //giving the chosed buffer the data, in this case, the position array
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), position, 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;
}

编辑:现在错误是:

4.1 INTEL-16.5.2
Failed to compile vertex
ERROR: 0:3: Invalid use of layout 'location'
ERROR: 0:7: Use of undeclared identifier 'position'

Failed to compile fragment
ERROR: 0:3: Invalid use of layout 'location'
ERROR: 0:7: Use of undeclared identifier 'color'

【问题讨论】:

    标签: c++ macos opengl glsl glfw


    【解决方案1】:

    您正在请求 GL 3.2 上下文,但尝试在其上使用 #version 330 GLSL。

    将您的 GLSL 反向移植到 #version 150 或请求 GL 3.3 上下文。

    另外,glfwWindowHint() 调用只会影响 下一个 glfwCreateWindow() 调用。所以调用glfwCreateWindow()之前设置你的提示。

    【讨论】:

    • 我得到同样的错误
    • @FrancescoCagnin:您的glfwWindowHint() 呼叫发生得太晚了。答案已编辑。
    • 我现在把它放在 main 函数的开头。但我仍然得到同样的错误
    • 所以在glfwInit() 之前?这早了。
    • 我把它放在 glfwInit() 之后。还是不行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多