You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS下Modern OpenGL仅黑屏,无法绘制三角形求助

Troubleshooting Your Missing OpenGL Triangle

Hey there! It’s super frustrating when everything else works (window pops up, background color changes) but that triangle won’t show up. Let’s break down the most likely culprits based on your setup (Homebrew GLFW/GLEW, CLion + CMake):

1. VAO/VBO Setup Gaps

Since you’re following The Cherno’s tutorial, you’re probably using VAOs and VBOs—small oversights here are the #1 reason triangles go missing:

  • Did you bind the VAO before configuring vertex attributes? You need glBindVertexArray(vao) before calling glVertexAttribPointer, otherwise the attributes won’t be associated with the VAO.
  • Don’t forget to enable the vertex attribute array! After glVertexAttribPointer, you have to call glEnableVertexAttribArray(0) (replace 0 with your attribute location) to tell OpenGL to use that data.
  • Double-check glBufferData parameters: Make sure you’re passing the correct size of your vertex data (e.g., sizeof(vertices) instead of a hardcoded number) and the right data pointer.

2. Shader Compilation/Linking Silent Failures

Just because your code compiles doesn’t mean your shaders did. OpenGL won’t yell at you by default—you have to check for errors manually:
Add this code after compiling your vertex/fragment shaders and linking the program:

GLint success;
char infoLog[512];

// Check vertex shader compilation
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
    glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    std::cout << "Vertex shader failed to compile:\n" << infoLog << std::endl;
}

// Check program linking
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
    glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
    std::cout << "Shader program failed to link:\n" << infoLog << std::endl;
}

Also verify that the layout (location = X) in your vertex shader matches the attribute index you used in glVertexAttribPointer (most tutorials use 0 for position).

3. Render Loop Mistakes

Even if your setup is perfect, a missed step in the loop can hide your triangle:

  • Did you call glUseProgram(shaderProgram) and glBindVertexArray(vao) inside the render loop? If you only bind them once before the loop, some other code (or GLFW itself) might unbind them.
  • Check your draw call: glDrawArrays(GL_TRIANGLES, 0, 3)—the third parameter is the number of vertices, which should be 3 for a single triangle. Don’t accidentally put 1 or another number here.
  • Don’t forget glfwSwapBuffers(window) at the end of the loop! macOS relies on double-buffering, so without this, your rendered frame will never show up on screen.

4. OpenGL Context Settings (Critical for macOS)

Homebrew’s GLFW works fine on macOS, but you need to set the right context hints when creating your window:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // macOS requires this for core profile
#endif

Without the GLFW_OPENGL_FORWARD_COMPAT flag, OpenGL might fall back to an older compatibility profile that doesn’t support core-profile features like VAOs properly.

5. CMake Configuration Checks

Make sure your CMakeLists.txt is correctly linking GLFW and GLEW:

cmake_minimum_required(VERSION 3.20)
project(YourProjectName)

set(CMAKE_CXX_STANDARD 17) # Match the C++ version the tutorial uses

find_package(glfw3 REQUIRED)
find_package(GLEW REQUIRED)

include_directories(${GLEW_INCLUDE_DIRS})
add_executable(YourProjectName main.cpp)
target_link_libraries(YourProjectName ${GLEW_LIBRARIES} glfw)

If GLEW isn’t being found correctly, you might need to specify its path manually, but Homebrew usually sets up the paths properly for CLion.

My final tip: Grab your main.cpp and compare line-by-line with the tutorial’s code for the VAO/VBO setup, shader code, and render loop. Small typos or missing lines are easy to miss when you’re focused on following along!

内容的提问来源于stack exchange,提问作者James

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:13:35