views:

360

answers:

3

Hi, I'm working on some OpenGL in C++ in Visual Studio 2005.

// SetUpOpenGL sets the pixel format and a rendering
// context then returns the RC
HGLRC COpenGLBaseWnd::SetUpOpenGL(HWND hwnd)
{
    static PIXELFORMATDESCRIPTOR pfd = 
    {
        sizeof (PIXELFORMATDESCRIPTOR), // strcut size 
        1,                              // Version number
        PFD_DRAW_TO_WINDOW |    // Flags, draw to a window,
            PFD_DOUBLEBUFFER |    // enable double buffering
            PFD_SUPPORT_OPENGL, // use OpenGL
        PFD_TYPE_RGBA,          // RGBA pixel values
        24,                     // 24-bit color
        0, 0, 0,                // RGB bits & shift sizes.
        0, 0, 0,                // Don't care about them
        0, 0,                   // No alpha buffer info
        0, 0, 0, 0, 0,          // No accumulation buffer
        32,                     // 32-bit depth buffer
        0,                      // No stencil buffer
        0,                      // No auxiliary buffers
        PFD_MAIN_PLANE,         // Layer type
        0,                      // Reserved (must be 0)
        0,                      // No layer mask
        0,                      // No visible mask
        0                       // No damage mask
    };

    pCDC = pWnd->GetDC();
    hDC = pCDC->GetSafeHdc();

    PixelFormatID = ChoosePixelFormat(hDC, &pfd);

    if (!PixelFormatID)
    {
        // catch errors here.
        // If nMyPixelFormat is zero, then there's
        // something wrong... most likely the window's
        // style bits are incorrect (in CreateWindow() )
        // or OpenGl isn't installed on this machine
        //printf("ChoosePixelFormat Failed %d\r\n",GetLastError());
        abort();
        exit(-1);
    }

    if (pfd.dwFlags & PFD_NEED_PALETTE)
    {
        //printf("Choosen Pixel Format requires a palette.\r\n");
        abort();
        exit(-1);
    }

   SetPixelFormat(hDC, PixelFormatID, &pfd);


And that SetPixelFormat call's where it goes bang during run-time. The annoying thing is, it only crashes on my machine, not on my colleague's.

I found this answer, which looks related, on stackoverflow, but either I don't know what to do to fix the problem using this information in C++, or it's not the same problem.

I need advice on how to implement that solution in C++, or other potential solutions.

A: 

Check if your pointers are NULL.

Your video card may not support specified pixel format. Try other settings. What video card you have? Compared to another on which it worked.

Where does pWnd come from?

Cedrik
ChoosePixelFormat will match the nearest pixel format if the one you request isn't avialable - I've never seen it fail though.
Martin Beckett
Yeah, Choose isn't failing, but it looks like Set is. And I know so little about openGL that my ability to diagnose the problem is LOW. The values for this stuff in debug seem sensible (except possibly hDC).No pointers are NULL.
deworde
A: 

You mentioned a linker error and a crash. Can you check if your object file has a definition for the function where it crashes? Are you using the right lib to link the OpenGL?

dirkgently
The linker error refers to the answer linked to in the question. The linker succeeds, but according to the answer I linked to, it's the order it does it in that causes problems.
deworde
+1  A: 

Ensure that when you create the window class, CS_PARENTDC is not specified and CS_OWNDC is.

Whether SetPixelFormat() detects that the DC is shared (the effect of messing up either of the above flags) I don't know, but OpenGL will not work properly unless the window has a dedicated hDC value.

Zooba