前提・実現したいこと
C言語でシンプルにWinAPIでマルチスレッドを作成しOpenGLのメッセージループを回すというプログラムを書きたいと思ったのですが表示されません。スレッドをまたがるGLの書き方が間違っているのでしょうか
OpenGLのコードは青のバックに白線で連続の直線を表示させているものです。
該当のソースコード
C
1#define UNICODE 2 3#pragma comment(lib,"OpenGL32.lib") 4 5#include <windows.h> 6#include <gl/GL.h> 7#include <gl/GLU.h> 8 9const wchar_t CLASS_NAME[] = L"OpenGL window project"; 10 11 12HINSTANCE hInst; 13HDC hdc; 14HGLRC glrc; 15BOOL bRet; 16 17MSG msg = {}; 18 19DWORD WINAPI GLThread(void); 20 21const wchar_t WINDOW_NAME[] = L"Multi-thread OpenGL Window"; 22 23LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 24 25int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpwsCmdLine, int nCmdShow) { 26 27 hInst = hInstance; 28 29 WNDCLASSEX wc = { 30 sizeof(WNDCLASSEX), 31 CS_HREDRAW | CS_VREDRAW, 32 WindowProc, 33 0,0, 34 hInstance, 35 NULL,NULL, 36 (HBRUSH)GetStockObject(WHITE_BRUSH), 37 NULL, 38 CLASS_NAME, 39 NULL 40 }; 41 42 RegisterClassEx(&wc); 43 44 HWND hwnd = CreateWindowEx( 45 0, CLASS_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 46 100, 100, 780, 540, 47 NULL, NULL, hInstance, NULL 48 ); 49 50 if (hwnd == NULL)return 0; 51 52 ShowWindow(hwnd, nCmdShow); 53 UpdateWindow(hwnd); 54 55 while ((bRet = GetMessage(&msg, NULL, 0, 0) != 0)) { 56 if (bRet == -1)break; 57 else { 58 TranslateMessage(&msg); 59 DispatchMessage(&msg); 60 } 61 } 62 63 return (int)msg.lParam; 64} 65 66LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 67 68 static HANDLE hgl; 69 static DWORD dwID; 70 71 switch (uMsg) { 72 case WM_CREATE: 73 { 74 PIXELFORMATDESCRIPTOR pfd = { 75 sizeof(PIXELFORMATDESCRIPTOR), 76 1, 77 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA, 78 32, 79 0,0,0,0,0,0, 80 0,0,0, 81 0,0,0,0, 82 24, 83 8, 84 0, 85 PFD_MAIN_PLANE, 86 0, 87 0,0,0 88 }; 89 90 hdc = GetDC(hwnd); 91 92 int format = ChoosePixelFormat(hdc, &pfd); 93 94 if (format == 0) { 95 MessageBox(hwnd, L"pixel format error", L"Caution", MB_OK); 96 return 0; 97 } 98 99 if (!SetPixelFormat(hdc, format, &pfd)) { 100 MessageBox(hwnd, L"pixel format error", L"Caution", MB_OK); 101 return 0; 102 } 103 104 glrc = wglCreateContext(hdc); 105 106 hgl = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GLThread, NULL, 0, &dwID); 107 108 if(hgl==NULL) { 109 MessageBox(hwnd, L"Thread error", L"Caution", MB_OK); 110 return 0; 111 } 112 113 break; 114 } 115 116 case WM_CLOSE: 117 118 wglMakeCurrent(NULL, NULL); 119 wglDeleteContext(glrc); 120 ReleaseDC(hwnd, hdc); 121 MessageBox(hwnd, L"Call WM_CLOSE Message", L"Caution", MB_OK); 122 123 DestroyWindow(hwnd); 124 break; 125 126 case WM_DESTROY: 127 PostQuitMessage(0); 128 break; 129 130 default: 131 return DefWindowProcW(hwnd, uMsg, wParam, lParam); 132 } 133 return 0; 134} 135 136DWORD WINAPI GLThread(void) { 137 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { 138 wglMakeCurrent(hdc, glrc); 139 140 glClearColor(0.0f, 0.5f, 1.0f, 1.0f); 141 glClear(GL_COLOR_BUFFER_BIT); 142 glBegin(GL_LINE_STRIP); 143 144 glVertex2f(0, 0); 145 glVertex2f(0.2, 0.4); 146 glVertex2f(0.4, -0.9); 147 148 glEnd(); 149 150 glVertex2d(1.5f, 5.5f); 151 152 glFlush(); 153 SwapBuffers(hdc); 154 wglMakeCurrent(NULL, NULL); 155 } 156 157 return 0; 158}
補足情報
Windows10 Pro
VisualStudio2017 Community

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/15 08:00