现代OpenGL在MacOS上只出现黑屏问题

3

我正在跟随Youtube上Cherno的OpenGL C++教程。我有以下代码,但无论尝试什么方法,都无法绘制三角形。窗口已经成功创建,并且我没有发现任何错误。我甚至可以通过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;
}

在发布问题到SO之前,请务必务必检查一下是否有glGetError报告的任何错误。并且请检查一下您的着色器是否编译通过。 - BDL
2个回答

6

在核心上下文中,您必须创建并绑定VAO,这不像兼容性上下文中那样是可选的。


我在浪费了两天时间跟随教程学习没有使用VAO的代码后,终于找到了你的答案!非常感谢! - Emadpres

4
除genpflaut的回答外,我要提到你的着色器代码根本无法编译。
必须删除字符串字面值之间和color = vec4(1.0, 0.0, 0.0, 1.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";

如果您在生成缓冲对象之前生成并绑定了顶点数组对象,则您的代码将正确运行。
在glBindBuffer(GL_ARRAY_BUFFER, buffer)之前添加以下行:
unsigned int vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao ); 


请参阅OpenGL 3.3核心配置规范,E.2.2已删除的功能,第344页

默认顶点数组对象(名称为零)也已过时。在未绑定缓冲区对象或未绑定顶点数组对象时调用VertexAttribPointer将生成INVALID_OPERATION错误,当未绑定顶点数组对象时调用任何数组绘制命令也会生成INVALID_OPERATION错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接