Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

c++ - OpenGL - Why am i getting this error? COMPILATION_FAILED version '330' is not supported

I'm using Xcode (Version 12.0.1) on macOS 10.15.7.

I brew installed the latest versions of glew, glfw, glm with Homebrew, and tried running this simple code snippet, my OpenGL version seems to be incompatible with the shaders used.

I do not want to change the code, any way to downgrade the shaders? (Could it be done with Homebrew as well?)

Here's the code:

#include <iostream>


//#define GLEW_STATIC 1   // This allows linking with Static Library on Windows, without DLL
#include <GL/glew.h>    // Include GLEW - OpenGL Extension Wrangler

#include <GLFW/glfw3.h> // GLFW provides a cross-platform interface for creating a graphical context,
                        // initializing OpenGL and binding inputs

#include <glm/glm.hpp>  // GLM is an optimized math library with syntax to similar to OpenGL Shading Language


const char* getVertexShaderSource()
{
    // Insert Shaders here ...
     // For now, you use a string for your shader code, in the assignment, shaders will be stored in .glsl files

     return
                 "#version 330 core
"
                 "layout (location = 0) in vec3 aPos;"
                 "layout (location = 1) in vec3 aColor;"
                 "out vec3 vertexColor;"
                 "void main()"
                 "{"
                 "   vertexColor = aColor;"
                 "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);"
                 "}";

}


const char* getFragmentShaderSource()
{
    return
    "#version 330 core
"
    "in vec3 vertexColor;"
    "out vec4 FragColor;"
    "void main()"
    "{"
    "   FragColor = vec4(vertexColor.r, vertexColor.g, vertexColor.b, 1.0f);"
    "}";
}


int compileAndLinkShaders()
{
    // compile and link shader program
       // return shader program id
       // ------------------------------------


       // vertex shader
       int vertexShader = glCreateShader(GL_VERTEX_SHADER);
       const char* vertexShaderSource = getVertexShaderSource();
       glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
       glCompileShader(vertexShader);
       
       // check for shader compile errors
       int success;
       char infoLog[512];
       glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
       if (!success)
       {
           glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
           std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED
" << infoLog << std::endl;
       }
       
       // fragment shader
       int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
       const char* fragmentShaderSource = getFragmentShaderSource();
       glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
       glCompileShader(fragmentShader);
       
       // check for shader compile errors
       glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
       if (!success)
       {
           glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
           std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
" << infoLog << std::endl;
       }
       
       // link shaders
       int shaderProgram = glCreateProgram();
       glAttachShader(shaderProgram, vertexShader);
       glAttachShader(shaderProgram, fragmentShader);
       glLinkProgram(shaderProgram);
       
       // check for linking errors
       glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
       if (!success) {
           glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
           std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED
" << infoLog << std::endl;
       }
       
       glDeleteShader(vertexShader);
       glDeleteShader(fragmentShader);
       
       return shaderProgram;

    //return -1;
}

int createVertexArrayObject()
{
    // A vertex is a point on a polygon, it contains positions and other data (eg: colors)
       glm::vec3 vertexArray[] = {
           glm::vec3( 0.0f,  0.5f, 0.0f),  // top center position
           glm::vec3( 1.0f,  0.0f, 0.0f),  // top center color (red)
           glm::vec3( 0.5f, -0.5f, 0.0f),  // bottom right
           glm::vec3( 0.0f,  1.0f, 0.0f),  // bottom right color (green)
           glm::vec3(-0.5f, -0.5f, 0.0f),  // bottom left
           glm::vec3( 0.0f,  0.0f, 1.0f),  // bottom left color (blue)
       };
       
       // Create a vertex array
       GLuint vertexArrayObject;
       glGenVertexArrays(1, &vertexArrayObject);
       glBindVertexArray(vertexArrayObject);
       
       
       // Upload Vertex Buffer to the GPU, keep a reference to it (vertexBufferObject)
       GLuint vertexBufferObject;
       glGenBuffers(1, &vertexBufferObject);
       glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
       glBufferData(GL_ARRAY_BUFFER, sizeof(vertexArray), vertexArray, GL_STATIC_DRAW);

       glVertexAttribPointer(0,                   // attribute 0 matches aPos in Vertex Shader
                             3,                   // size
                             GL_FLOAT,            // type
                             GL_FALSE,            // normalized?
                             2*sizeof(glm::vec3), // stride - each vertex contain 2 vec3 (position, color)
                             (void*)0             // array buffer offset
                             );
       glEnableVertexAttribArray(0);


       glVertexAttribPointer(1,                            // attribute 1 matches aColor in Vertex Shader
                             3,
                             GL_FLOAT,
                             GL_FALSE,
                             2*sizeof(glm::vec3),
                             (void*)sizeof(glm::vec3)      // color is offseted a vec3 (comes after position)
                             );
       glEnableVertexAttribArray(1);

       glBindBuffer(GL_ARRAY_BUFFER, 0); // VAO already stored the state we just defined, safe to unbind buffer
       glBindVertexArray(0); // Unbind to not modify the VAO
       
       return vertexArrayObject;
}


int main(int argc, char*argv[])
{
    // Initialize GLFW and OpenGL version
    glfwInit();

#if defined(PLATFORM_OSX)
    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);
#else
    // On windows, we set OpenGL version to 2.1, to support more hardware
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
#endif

    // Create Window and rendering context using GLFW, resolution is 800x600
    GLFWwindow* window = glfwCreateWindow(800, 600, "Comp371 - Lab 01", NULL, NULL);
    if (window == NULL)
    {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    

    // Initialize GLEW
    glewExperimental = true; // Needed for core profile
    if (glewInit() != GLEW_OK) {
        std::cerr << "Failed to create GLEW" << std::endl;
        glfwTerminate();
        return -1;
    }

    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    
    
    // Compile and link shaders here ...
    int shaderProgram = compileAndLinkShaders();
    
    // Define and upload geometry to the GPU here ...
    int vao = createVertexArrayObject();
    
    
    // Entering Main Loop
    while(!glfwWindowShouldClose(window))
    {
        // Each frame, reset color of each pixel to glClearColor
        glClear(GL_COLOR_BUFFER_BIT);
        
        
        // TODO - draw rainbow triangle
        
        glUseProgram(shaderProgram);
        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3); // 3 vertices, starting at index 0
        glBindVertexArray(0);


        // End frame
        glfwSwapBuffers(window);
        
        // Detect inputs
        glfwPollEvents();
        
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);
    }
    
    // Shutdown GLFW
    glfwTerminate();
    
    return 0;
}

And here's the error i'm getting

2021-01-22 21:23:23.336552-0500 OpenGLtest [27358:306573] Metal API Validation Enabled
2021-01-22 21:23:23.361027-0500 OpenGLtest [27358:306750] flock failed to lock maps file: errno = 35
2021-01-22 21:23:23.361368-0500 OpenGLtest [27358:306750] flock failed to lock maps file: errno = 35
ERROR::SHADER::VERTEX::COMPILATION_FAILED
ERROR: 0:1: '' :  version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:2: 'layout' : syntax error: syntax error

ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
ERROR: 0:1: '' :  version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:2: 'f' : syntax error: syntax error

ERROR::SHADER::PROGRAM::LINKING_FAILED
ERROR: One or more attached shaders not successfully compiled

Program ended with exit code: 9

How can this be fixed?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're trying to compile #version 330 GLSL code on a GL 3.2 context. Since GL 3.2 only supports #versions up to 150 that won't work.

Either re-write your GLSL to target #version 150 or request a GL 3.3 context.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...