1

I am trying to use glClearBufferfv(GL_COLOR, 0, red) to draw a red screen.

The program displays a white screen and the loading icon on the cursor is continually rotating.

I am using glew. I am also using visual studio and i think i have linked to all the necessary libraries. I employed the whole create a temporary context to use the wglCreateContextAttribsARB extension thing.

I have 2 functions for setting things up. the first one creates the window and sets up the pfd: (the editor is not formatting my code correctly so i will leave out the function names)

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;

The second one creates the context and starts the message loop:

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;

Here is my 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);

and here is my display function:

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

red is a global variable defined as:

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

Any help on why it's not drawing to the screen?

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

Please put display() between BeginPaint and EndPaint.

Start the painting operation by calling the BeginPaint function. This function fills in the PAINTSTRUCT structure with information on the repaint request.

After you are done painting, call the EndPaint function. This function clears the update region, which signals to Windows that the window has completed painting itself.

Refer: Painting the Window

Use an example to quickly restore the problem:

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

Debug:

1


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

Debug: 3

Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • Not quite sure what it was but i started from scratch and got it working. I don't want to compare my new code to this and post a solution either. Still don't quite know what was wrong though. – Alan Sanchez Mar 07 '20 at 03:46
  • @AlanSanchez All right, I mean the loaction of `display()` may influence the dispaly of background in the `WM_PAINT` which located in the `WndProc` function – Strive Sun Mar 09 '20 at 08:07
  • @AlanSanchez Now, it seems that you have solved the issue, do you mind sharing the way you have tried? – Strive Sun Mar 09 '20 at 08:11