OpenGL+GLUT+GLEW在Dell XPS 9550上窗口始终空白问题求助
Hey there, let's break down why your GLUT-based OpenGL setup is showing a blank window on the GTX 960M, while GLFW works fine and it runs smoothly on a GTX 1050. Here's what's going on and how to fix it step by step:
核心问题排查与修复步骤
1. 修正缓冲模式与缓冲区操作的矛盾
Your code uses GLUT_SINGLE (single-buffered mode) but calls glutSwapBuffers()—this is a direct conflict! Single-buffered mode doesn't require buffer swapping, while glutSwapBuffers() is exclusively for double-buffered setups (GLUT_DOUBLE). The GTX 960M's driver is stricter about this mismatch, whereas the GTX 1050's driver has more lenient error handling.
Fix:
Change this line:
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
to:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
If you insist on using single-buffered mode, replace glutSwapBuffers() in the display() function with glFlush(); instead.
2. 补充GLEW初始化(你漏掉了关键一步!)
You included the GLEW header but never called glewInit()—this is critical. Without initializing GLEW, OpenGL core features won't be properly enabled, and older mobile GPUs like the GTX 960M are far pickier about context initialization than newer desktop cards.
Fix:
Add this block right after glutCreateWindow() and before init():
// Initialize GLEW to access modern OpenGL functions GLenum err = glewInit(); if (GLEW_OK != err) { std::cerr << "GLEW initialization failed: " << glewGetErrorString(err) << std::endl; return 1; } // Verify OpenGL and GPU info for debugging std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; std::cout << "GPU Vendor: " << glGetString(GL_VENDOR) << std::endl;
3. 强制指定OpenGL上下文版本
GLUT defaults to creating an older compatibility context, which the GTX 960M's driver might not handle well. GLFW typically requests a higher context version by default, which is why it works.
Fix:
Add these lines right after glutInit() to force a compatible context that supports your fixed-pipeline code:
glutInit(&argc, argv); // Request OpenGL 3.2 compatibility profile (works with glBegin/glEnd) glutInitContextVersion(3, 2); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
If you plan to move to modern OpenGL (without fixed pipeline), switch to GLUT_CORE_PROFILE instead.
4. 确保程序绑定到独立显卡
Many laptops use dual-GPU setups (integrated + dedicated). GLUT might be defaulting to the integrated Intel GPU, while GLFW correctly binds to the NVIDIA GTX 960M.
Fix:
- Open NVIDIA Control Panel → Manage 3D Settings → Program Settings
- Select your compiled executable and set it to use the NVIDIA High-Performance Processor
修正后的完整测试代码
Here's the fully fixed version of your code incorporating all the above changes:
#include <Windows.h> #include <iostream> #include <GL/glew.h> #include <GL/glut.h> #include <glm/glm.hpp> #define WINDOW_WIDTH 320 #define WINDOW_HEIGHT 240 void display(); void init(); int main(int argc, char* argv[]) { glutInit(&argc, argv); // Force compatible OpenGL context for fixed pipeline code glutInitContextVersion(3, 2); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); // Use double-buffered mode to match glutSwapBuffers() glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutCreateWindow("Main Window"); // Initialize GLEW and verify setup GLenum err = glewInit(); if (GLEW_OK != err) { std::cerr << "GLEW initialization failed: " << glewGetErrorString(err) << std::endl; return 1; } std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; std::cout << "GPU Vendor: " << glGetString(GL_VENDOR) << std::endl; init(); glutDisplayFunc(display); glutMainLoop(); return 0; } void init() { glClearColor(1.0f, 0.0f, 0.0f, 0.0f); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, WINDOW_WIDTH - 1, WINDOW_HEIGHT - 1, 0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glBegin(GL_QUADS); glColor3ub(255, 255, 255); glVertex2f(10, 10); glVertex2f(100, 10); glVertex2f(100, 100); glVertex2f(10, 100); glEnd(); glutSwapBuffers(); }
渲染对比说明
- 正常渲染截图:[白色矩形渲染在红色背景上的窗口]
- 空白窗口截图:[无任何内容的空白窗口]
为什么GTX 1050 worked?
The GTX 1050 is a newer desktop GPU with a more forgiving driver that ignores minor mismatches like buffer mode conflicts or missing GLEW initialization. It also defaults to using the dedicated GPU without extra configuration, which hides these issues. The GTX 960M's mobile driver is stricter about context setup and resource binding, so these small oversights result in a blank window.
内容的提问来源于stack exchange,提问作者Joon. P

