Winapi 带有OpenGL上下文的Win32窗口不绘制任何内容

Winapi 带有OpenGL上下文的Win32窗口不绘制任何内容,winapi,opengl,Winapi,Opengl,我用OpenGL(在我的pc上使用4.4)编写了一个小Win32窗口。 然后我用OpenGL 3.3函数指针生成了一个文件。 现在一切似乎都起作用了,但什么也画不出来。 唯一有效的是,我可以用给定的颜色清除屏幕。 但我没有画三角形。 之后,我尝试使用WindowsGL.h函数指针(OpenGLV.1.1),并尝试绘制一个没有VBO和着色器的简单三角形。 但仍然不会有任何结果。 怎么了 #include "glw.h" #include <windows.h> #include "

我用OpenGL(在我的pc上使用4.4)编写了一个小Win32窗口。 然后我用OpenGL 3.3函数指针生成了一个文件。 现在一切似乎都起作用了,但什么也画不出来。 唯一有效的是,我可以用给定的颜色清除屏幕。 但我没有画三角形。 之后,我尝试使用WindowsGL.h函数指针(OpenGLV.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;
}
#包括“glw.h”
#包括
#包括“wgl.h”
#包括“gl.h”
#包括“log.h”
HINSTANCE实例句柄;
WNDCLASSEX窗口类;
窗柄;
HDC deviceContextHandle;
HGLRC openglContextHandle;
bool-isclosed;
LRESULT回调WindowMessageHandler(HWND HWND、UINT uMsg、WPARAM WPARAM、LPARAM LPARAM)
{
开关(uMsg)
{
案例WM_结束:
{
iscloserequest=true;
返回0;
}
}
返回(DefWindowProc(hWnd、uMsg、wParam、lParam));
}
void glw::Create()
{
如果(!(instanceHandle=GetModuleHandle(0)))
{
log::logRuntimeError(“glw::create获取实例句柄失败!”);
}
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_箭头);
windowClass.hIcon=加载图标(0,IDI_应用程序);
windowClass.hbrBackground=(HBRUSH)GetStockObject(白色画笔);
windowClass.lpszClassName=“atomus_window_class”;
windowClass.lpszMenuName=“菜单名称”;
windowClass.hIconSm=加载图标(0,IDI_应用程序);
如果(!RegisterClass(&windowClass))
{
log::logRuntimeError(“glw::create注册窗口类失败!”);
}
如果(!(windowHandle=CreateWindowEx(0,
“atomus_窗口_类”,
“atomus标题”,
WS_重叠窗口,
0,
0,
800,
600,
0,
0,
实例句柄,
0)))
{
log::logRuntimeError(“glw::create未能创建窗口!”);
}
如果(!(deviceContextHandle=GetDC(windowHandle)))
{
log::logRuntimeError(“glw::create未能获取设备上下文句柄!”);
}
像素格式描述符像素格式描述符;
pixelFormatDescriptor.nSize=sizeof(pixelFormatDescriptor);
pixelFormatDescriptor.Inversion=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_主平面;
无符号整数像素格式;
if(!(pixelFormat=ChoosePixelFormat(deviceContextHandle和pixelFormatDescriptor)))
{
log::logRuntimeError(“glw::create未能找到合适的像素格式!”);
}
if(!SetPixelFormat(deviceContextHandle、pixelFormat和pixelFormatDescriptor))
{
log::logRuntimeError(“glw::create设置像素格式失败!”);
}
HGLRC temporaryContext=wglCreateContext(deviceContextHandle);
如果(!临时上下文)
{
log::logRuntimeError(“glw::create未能创建临时上下文!”);
}
if(!(wglMakeCurrent(deviceContextHandle,temporaryContext)))
{
log::logRuntimeError(“glw::create未能激活临时上下文!”);
}
国际专业;
int小调;
gl::GetIntegerv(gl::MAJOR\u版本,&MAJOR);
gl::GetIntegerv(gl::次要版本和次要版本);
如果(!(大调>=3和小调>=3))
{
log::logRuntimeError(“不支持glw::创建opengl 3.3!”);
}
int attribs[]=
{
WGL_上下文_主要版本_ARB,主要版本,
WGL_上下文_次要版本_ARB,次要版本,
WGL\u上下文\u标志\u ARB,WGL\u上下文\u转发\u兼容\u位\u ARB,
WGL_上下文_配置文件_掩码_ARB,WGL_上下文_核心_配置文件_位_ARB,
0
};
pfnwwglcreatebufferregionarbproc wglCreateContextAttribsARB=(PFNWGLCREATEBUFFERREGIONARBPROC)wglGetProcAddress(“wglCreateContextAttribsARB”);
如果(!wglCreateContextAttribsARB)
{
log::logRuntimeError(“glw::create未能找到指向wglCreateContextAttribsARB函数的指针!”);
}
如果(!(openglContextHandle=(HGLRC)wglCreateContextAttribsARB(deviceContextHandle,0,(UINT)attribs)))
{
log::logRuntimeError(“glw::create未能创建前向兼容上下文!”);
}
wglMakeCurrent(NULL,NULL);
wglDeleteContext(临时上下文);
如果(!wglMakeCurrent(deviceContextHandle,(HGLRC)openglContextHandle))
{
log::logRuntimeError(“glw::create未能激活前向兼容上下文!”);
}
ShowWindow(windowHandle,SW_SHOW);
更新窗口(窗口句柄);
iscloserequest=false;
}
void glw::Destroy()
{
破坏窗口(windowHandle);
windowHandle=0;
注销类(windowClass.lpszClassName,instanceHandle);
}
void glw::Update()
{
消息;
while(peek消息(&message,0,0,PM_REMOVE)>0)//或使用if语句
{
翻译消息(和消息);
DispatchMessage(&message);
}
}
void glw::SwapBuffers()
{
::SwapBuffers(deviceContextHandle);
}
bool glw::IsCloseRequested()
{
要求归还;
}

您正在创建核心配置文件上下文。您的渲染代码是为核心配置文件编写的吗?这意味着,例如,它需要对顶点数据使用VAO(顶点数组对象),需要提供用GLSL编写的着色器,并且不能使用