Mac下通过Homebrew安装GLFW/GLEW后OpenGL无绘制问题排查
Hey there, let's dig into why you're not seeing any drawn content even though your setup compiles fine. I've dealt with similar macOS/OpenGL quirks before, so here are the most likely fixes to check:
macOS is pretty strict about OpenGL contexts, especially for older fixed-function pipeline code. When you create your GLFW window, you need to explicitly request a version that supports the fixed pipeline (like 2.1, since that's the last version with full fixed-function support on macOS).
Add these hints before calling glfwCreateWindow:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); // Critical for fixed pipeline
Without the compat profile hint, macOS will default to a core profile which drops all fixed-function functionality—so your glBegin/glEnd calls won't do anything at all.
Since you're using old OpenGL, double-check that your drawing calls are properly structured. A minimal working example for drawing a triangle should look like this (make sure this is inside your render loop, after clearing the screen):
glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.5f, -0.5f); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.0f, 0.5f); glEnd(); glfwSwapBuffers(window); glfwPollEvents();
If you're using glVertex3f, make sure your projection/modelview matrices are set correctly (since without shaders, you need to set up the matrices manually). Add this once, right after creating the window context:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // 2D orthographic projection that matches your vertex coords glMatrixMode(GL_MODELVIEW); glLoadIdentity();
Without setting the projection matrix, your vertices might be outside the view frustum and get clipped entirely.
Even though it compiles, sometimes linking can be incomplete on macOS with Homebrew. Make sure your CMakeLists.txt is correctly finding and linking GLFW and GLEW. Here's a reliable template:
cmake_minimum_required(VERSION 3.10) project(OpenGLTest) find_package(glfw3 REQUIRED) find_package(GLEW REQUIRED) add_executable(OpenGLTest main.cpp) target_link_libraries(OpenGLTest PRIVATE glfw GLEW::GLEW "-framework OpenGL")
The -framework OpenGL flag is crucial on macOS—Homebrew might handle it implicitly, but explicitly adding it ensures the system OpenGL framework is linked properly.
Double-check that you have this line immediately after creating the window:
glfwMakeContextCurrent(window);
Without this, all your OpenGL calls are essentially going nowhere—there's no active context to render to.
Start with the context version hint first; that's the most common culprit for fixed-pipeline issues on macOS. If none of these fix it, feel free to share snippets of your drawing code or CMake setup and we can troubleshoot further!
内容的提问来源于stack exchange,提问作者James

