c++ - 现代 OpenGL 仅 MacOS 黑屏

标签 c++ macos opengl cmake clion

我正在学习 C++ 中的 OpenGL 教程(Youtube 上的 The Cherno)。我有以下代码,但无论我尝试什么,我都无法绘制三角形。创建了一个窗口,我没有得到任何错误等,我什至可以使用 glClearColor 更改背景颜色。但是没有三角形!

有关信息,我通过 Homebrew 安装了 GLFW/GLEW,我正在使用 CLion。

CMakeList.txt

cmake_minimum_required(VERSION 3.3)
project(Lib_Test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")

set(SOURCE_FILES src/main.cpp CMakeLists.txt)

# add extra include directories
include_directories(/usr/local/include)

# add extra lib directories
link_directories(/usr/local/lib)

add_executable(Lib_Test main.cpp)
target_link_libraries(Lib_Test glfw)
target_link_libraries(Lib_Test glew)
find_package (GLM REQUIRED)
include_directories(include)

main.cpp

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


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(){

    // Initialise GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
    window = glfwCreateWindow( 640, 480, "My App", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window.\n" );
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

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

    while( !glfwWindowShouldClose(window) )
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0 ,3);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

最佳答案

必须在核心上下文中创建和绑定(bind)一个 VAO,它不像在兼容性上下文中那样是可选的。

关于c++ - 现代 OpenGL 仅 MacOS 黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48714591/

相关文章:

c++ - 当文件被删除并再次创建时,inotify 停止监视文件

c++ - CUDA 错误 : "dynamic initialization is not supported for __device__, __constant__ and __shared__ variables"

c++ - 如何从 multimap 中删除特定的重复项?

macos - 在到达应用程序之前调试 OS X 中的关键事件流

macos - 如何在 macOS 上安装和使用 GNU "ls"?

c++ - 简单 C++ OpenGL 程序的 Makefile

c++ - 未定义模板的qt隐式实例化 'QList<VPNConnection>'

mongodb - 在mac中安装mongo shell

c++ - 在 OpenGL 中使用多个纹理

c++ - 围绕某个点旋转对象,即使原始枢轴不同?