problem with wgl pixel format

Started by
0 comments, last by eastcowboy 15 years, 10 months ago
Hello. I got a problem with wgl pixel format. the code is quite simple, I choose a pixel format which has 16 color bits, 16 depth bits, and 8 alpha bits.

        int pixelformat;
        PIXELFORMATDESCRIPTOR pfd = {0};
        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 16;
        pfd.cDepthBits = 16;
        pfd.cAlphaBits = 8;
        pixelformat = ChoosePixelFormat(hDC, &pfd);
        SetPixelFormat(hDC, pixelformat, &pfd);
        hRC = wglCreateContext(hDC);


Before I choose pixel format, I called the ChangeDisplaySettings function to change the screen display settings to 1024*768*16. The strange problem is, when my desktop display setting is 1280*800*16, the program works well (change display setting to 1024*760*16, choose a pixel format, use glRectf to draw a rectangle), but when my desktop display setting is 1280*800*32, all things go wrong. I use the glColor3f(1, 1, 1) color but I see (0, 1, 1) color on my screen. also, the FPS is quite low. And more, if I delete the "pfd.cAlphaBits = 8;" from my code, all thing go well again. I've tested my program code on two different PC, one is: intel 945G, and other is: nVidia Geforce Go 7300. but both have the same problem. I don't know why. complete code here: (language: "C", IDE: "Visual Studio 2005")

#include <windows.h>
#include <GL/gl.h>
#include <stdio.h>

#pragma comment (lib, "opengl32.lib")

static PCTSTR gs_CLASS_NAME = TEXT("Default Window Class");
static int width = 1024;
static int height = 768;
static int bpp = 16;
static int fullscreen = 1;

static HWND  hWnd;
static HDC   hDC;
static HGLRC hRC;

#define MAX_CHAR       128
void drawString(const char* str) {
    static int isFirstCall = 1;
    static GLuint lists;
    if( isFirstCall ) {
        isFirstCall = 0;
        lists = glGenLists(MAX_CHAR);
        wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists);
    }
    for(; *str!='\0'; ++str)
        glCallList(lists + *str);
}

void showfps() {
    char str[20];
    double fps;

    // cal fps
    {
        int current;
        static int last;
        static double last_fps;
        static const int n = 50;
        static int count = 0;

        if( ++count < n )
            fps = last_fps;
        else {
            count = 0;
            current = GetTickCount();
            fps = 1000.0 * n / (current - last);
            last = current;
            last_fps = fps;
        }
    }

    // draw string
    sprintf(str, "FPS: %g", fps);
    glColor3f(1, 1, 1);
    glRasterPos2f(-1.0f, 0.9f);
    drawString(str);
}

static LRESULT CALLBACK wndProc(
        HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    if( msg == WM_KEYDOWN )
        PostQuitMessage(0);
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int main() {
    DWORD style, exstyle;
    int position;

    // register class
    {
    WNDCLASSEX wc;
    wc.cbSize = sizeof(wc);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_NOCLOSE;
    wc.lpfnWndProc = wndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = GetModuleHandle(0);
    wc.hIcon = LoadIcon(0, IDI_APPLICATION);
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = 0;
    wc.lpszMenuName = 0;
    wc.lpszClassName = gs_CLASS_NAME;
    wc.hIconSm = 0;
    if( !RegisterClassEx(&wc) )
        return 1;
    }

    // set style & change resolution
    {
        if( fullscreen ) {
            DEVMODE mode = {0};
            style = WS_POPUP;
            exstyle = WS_EX_APPWINDOW | WS_EX_TOPMOST;
            position = 0;

            mode.dmSize = sizeof(mode);
            mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
            mode.dmPelsWidth = width;
            mode.dmPelsHeight = height;
            mode.dmBitsPerPel = bpp;
            if( ChangeDisplaySettings(&mode, CDS_FULLSCREEN) !=
                    DISP_CHANGE_SUCCESSFUL ) {
                fullscreen = 0;
                goto windowed;
            }
        } else {
            RECT rect;
windowed:
            style = WS_OVERLAPPEDWINDOW & (~WS_THICKFRAME);
            exstyle = WS_EX_APPWINDOW;
            position = CW_USEDEFAULT;

            rect.left = 0;
            rect.top = 0;
            rect.right = width;
            rect.bottom = height;
            AdjustWindowRectEx(&rect, style, FALSE, exstyle);
            width = rect.right - rect.left;
            height = rect.bottom - rect.top;
        }
    }

    // create window
    hWnd = CreateWindowEx(exstyle, gs_CLASS_NAME, TEXT(""), style,
            position, position, width, height,
            0, 0, GetModuleHandle(0), 0);
    if( !hWnd )
        return 1;
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
    hDC = GetDC(hWnd);
    if( !hDC )
        return 1;

    // initialize opengl
    {
        int pixelformat;
        PIXELFORMATDESCRIPTOR pfd = {0};
        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 16;
        pfd.cDepthBits = 16;
        pfd.cAlphaBits = 8;
        pixelformat = ChoosePixelFormat(hDC, &pfd);
        SetPixelFormat(hDC, pixelformat, &pfd);
        hRC = wglCreateContext(hDC);

        printf("pixel format: %d\n", pixelformat);

        wglMakeCurrent(hDC, hRC);
    }

    // message loop
    {
        MSG msg;
        for(;;) {
            if( PeekMessage(&msg, 0, 0, 0, PM_REMOVE) ) {
                if( msg.message == WM_QUIT )
                    return 0;
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            } else {
                glClear(GL_COLOR_BUFFER_BIT);
                glColor3f(1, 1, 1);
                glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
                showfps();
                SwapBuffers(hDC);
            }
        }
    }

    // end
    return 0;
}


Advertisement
some days passed but no answer :(
any body could you help me please?

I also used the NeHe's OpenGL framework to test. But the problem happens again.

Let me describe my problem again. follow these steps and I got the problem:
(1) set my desktop's bpp(bits-per-pixel) to 32. (though the Microsoft Windows System's user interface, right click the desktop and select 'property' and select 'settings', not use the Windows API functions)
(2) (in my program, ) change display settings, set bpp to 16
(3) (in my program, ) choose a pixel format with 16 color bits, 16 depth bits, and 8 alpha bits, then create OpenGL context.
(4) (in my program, ) use glColor3f(1, 1, 1) and then use glRectf to draw a white rectange. but what I saw is a rectangle with color (0, 1, 1), the red bits are missing. also, the FPS is quite slow (FPS < 20.0 at 1024*768*16)
(5) in step 1, if I set desktop's bpp to 16, then all problems disappear. I can get a white rectangle and the FPS is nearly 90. By the way, whether I set desktop's bpp to 16 or 32, the ChoosePixelFormat returns the same value.
(6) in step 3, if I choose a pixel format without alpha bits, then all problems disappear too.
(7) I test this on two different computer. one is: intel 845G, and the other is: nVidia GeForce Go 7300. but both have the same problem.

This topic is closed to new replies.

Advertisement