使用OpenGL上下文的Win32窗口没有任何内容

时间:2014-09-19 12:25:46

标签: winapi opengl

我用OpenGL(在我的电脑上用4.4)上下文写了一个小的Win32窗口。 比我使用OpenGL Loader Generator生成带有OpenGL 3.3函数指针的文件。 现在一切似乎都有效,但什么都不会被吸引。 唯一有效的是,我可以用给定的颜色清除屏幕。 但我没有得到一个三角形。 之后我尝试使用windows gl.h函数指针(OpenGL v.1.1)并尝试绘制一个没有VBO和着色器的简单三角形。 但仍然没有什么可以吸引的。 怎么了?

#include "glw.h"


#include <windows.h>
#include "wgl.h"
#include "gl.h"
#include "log.h"


HINSTANCE instanceHandle;
WNDCLASSEX windowClass;
HWND windowHandle;
HDC deviceContextHandle;
HGLRC openglContextHandle;

bool isCloseRequested;


LRESULT CALLBACK WindowMessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CLOSE:
        {
            isCloseRequested = true;
            return 0;
        }
    }
    return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}


void glw::Create()
{
    if(!(instanceHandle = GetModuleHandle(0)))
    {
        log::logRuntimeError("glw::create failed to get instance handle!");
    }

    windowClass.cbSize = sizeof(WNDCLASSEX);
    windowClass.style = CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc = WindowMessageHandler;
    windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.hInstance = instanceHandle;
    windowClass.hCursor = LoadCursor(0,IDC_ARROW);
    windowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
    windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    windowClass.lpszClassName = "atomus_window_class";
    windowClass.lpszMenuName = "menu_name";
    windowClass.hIconSm = LoadIcon(0, IDI_APPLICATION);

    if(!RegisterClassEx(&windowClass))
    {
        log::logRuntimeError("glw::create failed to register window class!");
    }

    if(!(windowHandle = CreateWindowEx( 0,
                                    "atomus_window_class",
                                    "atomus title",
                                    WS_OVERLAPPEDWINDOW,
                                    0,
                                    0,
                                    800,
                                    600,
                                    0,
                                    0,
                                    instanceHandle,
                                    0)))
    {
        log::logRuntimeError("glw::create failed to create window!");
    }

    if(!(deviceContextHandle = GetDC(windowHandle)))
    {
        log::logRuntimeError("glw::create failed to get device context handle!");
    }

    PIXELFORMATDESCRIPTOR pixelFormatDescriptor;
    pixelFormatDescriptor.nSize = sizeof(pixelFormatDescriptor);
    pixelFormatDescriptor.nVersion = 1;
    pixelFormatDescriptor.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
    pixelFormatDescriptor.iPixelType = PFD_TYPE_RGBA;
    pixelFormatDescriptor.cColorBits = 32;
    pixelFormatDescriptor.cDepthBits = 32;
    pixelFormatDescriptor.iLayerType = PFD_MAIN_PLANE;

    unsigned int pixelFormat;

    if(!(pixelFormat = ChoosePixelFormat(deviceContextHandle, &pixelFormatDescriptor)))
    {
        log::logRuntimeError("glw::create failed to find suitable pixel format!");
    }
    if(!SetPixelFormat(deviceContextHandle, pixelFormat, &pixelFormatDescriptor))
    {
        log::logRuntimeError("glw::create failed to set pixel format!");
    }

    HGLRC temporaryContext = wglCreateContext(deviceContextHandle);

    if(!temporaryContext)
    {
        log::logRuntimeError("glw::create failed to create temporary context!");
    }
    if (!(wglMakeCurrent(deviceContextHandle, temporaryContext)))
    {
        log::logRuntimeError("glw::create failed to activate temporary context!");
    }

    int major;
    int minor;
    gl::GetIntegerv(gl::MAJOR_VERSION, &major);
    gl::GetIntegerv(gl::MINOR_VERSION, &minor);
    if(!(major >= 3 && minor >= 3))
    {
        log::logRuntimeError("glw::create opengl 3.3 ist not supported!");
    }

    int attribs[] =
    {
        WGL_CONTEXT_MAJOR_VERSION_ARB, major,
        WGL_CONTEXT_MINOR_VERSION_ARB, minor,
        WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
        WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
        0
    };

    PFNWGLCREATEBUFFERREGIONARBPROC wglCreateContextAttribsARB = (PFNWGLCREATEBUFFERREGIONARBPROC)wglGetProcAddress( "wglCreateContextAttribsARB" );

    if(!wglCreateContextAttribsARB)
    {
        log::logRuntimeError("glw::create failed to find pointer to wglCreateContextAttribsARB function!");
    }
    if(!(openglContextHandle = (HGLRC)wglCreateContextAttribsARB(deviceContextHandle, 0, (UINT)attribs)))
    {
        log::logRuntimeError("glw::create failed to create forward compatible context!");
    }

    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( temporaryContext );

    if(!wglMakeCurrent(deviceContextHandle, (HGLRC)openglContextHandle))
    {
        log::logRuntimeError("glw::create failed to activate forward compatible context!");
    }

    ShowWindow(windowHandle, SW_SHOW);
    UpdateWindow(windowHandle);

    isCloseRequested = false;
}
void glw::Destroy()
{
    DestroyWindow(windowHandle);
    windowHandle = 0;

    UnregisterClass(windowClass.lpszClassName, instanceHandle);
}
void glw::Update()
{
    MSG message;
    while (PeekMessage (&message, 0, 0, 0, PM_REMOVE) > 0) //Or use an if statement
    {
         TranslateMessage (&message);
         DispatchMessage (&message);
    }
}
void glw::SwapBuffers()
{
    ::SwapBuffers(deviceContextHandle);
}
bool glw::IsCloseRequested()
{
    return isCloseRequested;
}

0 个答案:

没有答案