OpenGL3.3 Mac OSX 11.4

OpenGL3.3 Mac OSX 11.4

我实际上正在学习 OpenGL3.3 来创建一个多平台应用程序,它会绘制一些精灵。

目前,我正在为vertexShader和fragShader的编译而苦恼。 OpenGL 向我显示信息,我的设备不支持 3.3 版本。什么时候 明确表示支持

main.cpp

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string>
#include <fstream>
#include <sstream>
#include <filesystem>
float vertices[] = {
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, 0.5f, 0.0f};
std::string readFile(const std::string &fileLoc)
{
    std::ifstream file;
    file.open(fileLoc, std::ios::in);
    std::stringstream contex;
    if (!file.fail())
    {
        contex << file.rdbuf();
    }
    return contex.str();
}
GLuint createandComplieShader(const std::string &fileLoc, GLuint shaderType)
{
    GLuint shader = glCreateShader(shaderType);
    std::string shaderString = readFile(fileLoc);
    const char *ShaderSource = shaderString.c_str();
    glShaderSource(shader, 1, &ShaderSource, nullptr);
    glCompileShader(shader);
    int success;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        char infoLog[512];
        glGetShaderInfoLog(shader, 512, nullptr, infoLog);
        std::cerr << "Shader Error :" << infoLog << std::endl;
        exit(-1);
    }
    return shader;
}
GLuint createAndLinkShader()
{
    std::string path = std::filesystem::current_path(); 
    std::string VertexShader = "/vertShader.vert" ; 
    std::string FragmentShader = "/fragShader.vert"; 
    GLuint vertexShader = createandComplieShader(path + VertexShader , GL_VERTEX_SHADER);
    GLuint fragmentShader = createandComplieShader(path + FragmentShader , GL_FRAGMENT_SHADER);
    GLuint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    int succes;
    glGetProgramiv(program, GL_LINK_STATUS, &succes);
    if (!succes)
    {
        char infoLog[512];
        glGetProgramInfoLog(program, 512, nullptr, infoLog);
        std::cerr << "Linking Error: " << infoLog << std::endl;
        exit(-2);
    }
    return program;
}
int main(int argc, char **argv)
{
    if (!glfwInit())
    {
        std::cerr << "GLFW init failed..." << std::endl;
        return -1;
    }
    GLFWwindow *window = glfwCreateWindow(1280, 720, "A-Engine", nullptr, nullptr);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);

    if (window == nullptr)
    {
        std::cerr << "Window init failed..." << std::endl;
        glfwTerminate();
        return -2;
    }
    glfwMakeContextCurrent(window);
    // glfwSetKeyCallback (window, HandleKeyInput);

    if (glewInit() != GLEW_OK)
    {
        std::cerr << "Glew init failed..." << std::endl;
        glfwDestroyWindow(window);
        glfwTerminate();
        return -3;
    }
    glClearColor(0.2f, 0.2f, 0.45f, 1.0f);
    GLuint ShaderProgram = createAndLinkShader();
    unsigned int VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    int width;
    int height;
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glfwGetFramebufferSize(window, &width, &height);
        glViewport(0, 0, width, height);
        glUseProgram(ShaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
    }
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteProgram(ShaderProgram);
    glfwDestroyWindow(window);
    return EXIT_SUCCESS;
}

fragShader.vert

#version 330 core
out vec4 FragColor;

void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
} 

vertShader.vert

#version 330 core
layout (location = 0) in vec3 aPos;

void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

主要的二进制文件与 vertShader 和 fragShader 位于同一文件夹下

Shader Error :ERROR: 0:1: '' :  version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:2: 'layout' : syntax error: syntax error

根据评论,至少您应该在 创建 window 之前设置 glfw window 提示 。所以 main 中的 window 创建代码应该是...

if (!glfwInit())
{
    std::cerr << "GLFW init failed..." << std::endl;
    return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
GLFWwindow *window = glfwCreateWindow(1280, 720, "A-Engine", nullptr, nullptr);
    .
    .
    .

否则,与新创建的上下文关联的 OpenGL 版本可以是任何版本(据我所知)。