1

我正在尝试使用glClearBufferfv(GL_COLOR, 0, red)绘制红色屏幕。

程序显示白色屏幕,光标上的加载图标不断旋转。

我正在使用 glew。我也在使用 Visual Studio,我想我已经链接到所有必要的库。我使用整个创建一个临时上下文来使用 wglCreateContextAttribsARB 扩展的东西。

我有 2 个设置功能。第一个创建窗口并设置 pfd:(编辑器没有正确格式化我的代码,所以我将省略函数名称)

int pf;
HDC hDC;
HWND hWnd;
WNDCLASS wc;
PIXELFORMATDESCRIPTOR pfd;
static HINSTANCE hInstance = 0;
if (!hInstance) {
    hInstance = GetModuleHandle(NULL);
    wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"OpenGL";

    if (!RegisterClass(&wc)) {
        MessageBox(NULL, (LPCWSTR)L"RegisterClass() failed:  ", (LPCWSTR)L"Error", MB_OK);
        return NULL;
    }
}

hWnd = CreateWindow(TEXT("OpenGL"), TEXT("Pie"), WS_OVERLAPPEDWINDOW |
    WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
    x, y, width, height, NULL, NULL, hInstance, NULL);

if (hWnd == NULL) {
    MessageBox(NULL, TEXT("CreateWindow() failed:  Cannot create a window."),
        TEXT("Error"), MB_OK);
    return NULL;
}

hDC = GetDC(hWnd);

/* there is no guarantee that the contents of the stack that become
   the pfd are zeroed, therefore _make sure_ to clear these bits. */
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

pf = ChoosePixelFormat(hDC, &pfd);
if (pf == 0) {
    MessageBox(NULL, L"ChoosePixelFormat() failed:  "
        "Cannot find a suitable pixel format.", L"Error", MB_OK);
    return 0;
}

if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
    MessageBox(NULL, L"SetPixelFormat() failed:  "
        "Cannot set format specified.", L"Error", MB_OK);
    return 0;
}

DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

ReleaseDC(hWnd, hDC);

return hWnd;

第二个创建上下文并启动消息循环:

HDC hDC;
HGLRC hRCt, hRC;
HWND  hWnd;
MSG   msg;

hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
if (hWnd == NULL)
    exit(1);

hDC = GetDC(hWnd);
hRCt = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRCt);
glewExperimental = true;
glewInit();

int attribs[] =
{
    WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
    WGL_CONTEXT_MINOR_VERSION_ARB, 5,
    WGL_CONTEXT_FLAGS_ARB, 0,
    0
};
hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRCt);
wglMakeCurrent(hDC, hRC);

ShowWindow(hWnd, nCmdShow);

while (GetMessage(&msg, hWnd, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

wglMakeCurrent(NULL, NULL);
ReleaseDC(hWnd, hDC);
wglDeleteContext(hRC);
DestroyWindow(hWnd);

return msg.wParam;

这是我的 wndproc:

   static PAINTSTRUCT ps;

switch (uMsg) {
case WM_PAINT:
    display();
    BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
    return 0;

case WM_SIZE:
    //glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
    PostMessage(hWnd, WM_PAINT, 0, 0);
    return 0;

case WM_CHAR:
    switch (wParam) {
    case 27:            /* ESC key */
        PostQuitMessage(0);
        break;
    }
    return 0;

case WM_CLOSE:
    PostQuitMessage(0);
    return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);

这是我的显示功能:

glClearBufferfv(GL_COLOR, 0, red);
glFlush();

red 是一个全局变量,定义为:

GLfloat red[4] = {1, 0, 0, 1};

关于为什么它没有绘制到屏幕上的任何帮助?

4

1 回答 1

1

请放在和display()之间。BeginPaintEndPaint

通过调用BeginPaint函数开始绘画操作。此函数使用有关重绘请求的信息填充PAINTSTRUCT结构。

完成绘画后,调用EndPaint函数。此函数清除更新区域,它向 Windows 发出信号,表明窗口已经完成了自己的绘制。

参考:画窗

用一个例子快速还原问题:

 HDC hdc = GetDC(hWnd);
 case WM_PAINT:
        {
            PAINTSTRUCT ps; 
            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 2));         
            BeginPaint(hWnd, &ps);              
            EndPaint(hWnd, &ps);
        }

调试:

1


 HDC hdc = GetDC(hWnd);
 case WM_PAINT:
        {
            PAINTSTRUCT ps;         
            BeginPaint(hWnd, &ps);
            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 2)); 
            EndPaint(hWnd, &ps);
        }

调试: 3

于 2020-03-06T03:40:11.050 回答