前提・実現したいこと
stack overflow:How to make an OpenGL rendering context with transparent background?に記載されているコードを実行したところうまく動作しましたがWM_CREATE
において何も宣言されておらずほとんどの動作がWM_SIZE
で記述されているところやグローバル変数で記述されている点を書き直してみましたがうまく動作しません.
1)glEnable
等のgl関数の記述・初期化場所はあっていますか?
2)glEnable
で有効化されているものは全部必要なのでしょうか?
3)何が原因で表示がされないのでしょうか?
4)WM_CREATE
のCreateDIBSectionの第二引数でBITMAPHEADERINFO*
をBITMAPINFO*
に変更していますがこれはBITMAPHEADERINFO*
の頭のBITMAPINFO*
を強引に入れたものでしょうか?
5)stack overflowのコードではassert.h
でエラーチェック押しているようですが日本語ではあまり見かけない気がします.どんなメリットがあるのでしょうか?
該当のソースコード
C
1#define UNICODE 2 3#pragma comment(lib,"opengl32.lib") 4 5#include <windows.h> 6#include <gl/GL.h> 7 8LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM); 9 10const wchar_t CLASS_NAME[] = L"CLASS"; 11 12int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR nCmdLine, int nCmdShow) { 13 UNREFERENCED_PARAMETER(hPrevInstance); 14 UNREFERENCED_PARAMETER(nCmdLine); 15 16 WNDCLASSEX wc = { 17 sizeof(WNDCLASSEX),CS_VREDRAW | CS_HREDRAW, WindowProc, 18 0, 0, hInstance, 19 NULL, (HCURSOR)LoadCursor(NULL,IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), 20 NULL, CLASS_NAME, NULL 21 }; 22 23 RegisterClassEx(&wc); 24 25 HWND hwnd = CreateWindowEx( 26 0, CLASS_NAME, L"Template", WS_OVERLAPPEDWINDOW, 27 50, 50, 960, 525, 28 NULL, NULL, hInstance, NULL 29 ); 30 31 ShowWindow(hwnd, nCmdShow); 32 UpdateWindow(hwnd); 33 34 MSG msg = {}; 35 36 while (GetMessage(&msg, NULL, 0, 0) != 0) { 37 TranslateMessage(&msg); 38 DispatchMessage(&msg); 39 } 40 41 return (int)msg.wParam; 42} 43 44LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 45 switch (uMsg) { 46 static RECT rc; 47 static int iwidth; 48 static int iheight; 49 static BITMAPINFOHEADER bih; 50 static HDC pdcDIB; 51 static HGLRC m_hrc; 52 case WM_CREATE: 53 { 54 GetClientRect(hwnd, &rc); 55 iwidth = rc.right - rc.left; 56 iheight = rc.bottom - rc.top; 57 58 int iSize = sizeof(BITMAPINFOHEADER); 59 memset(&bih, 0, iSize); 60 61 bih.biSize = iSize; 62 bih.biPlanes = 1; 63 bih.biBitCount = 24; 64 bih.biCompression = BI_RGB; 65 bih.biWidth = iwidth; 66 bih.biHeight = iheight; 67 68 pdcDIB = CreateCompatibleDC(NULL); 69 70 void* pv; 71 HBITMAP hbmpDIB = CreateDIBSection(pdcDIB, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &pv, NULL, 0); 72 73 SelectObject(pdcDIB, hbmpDIB); 74 75 76 /* CreateHGLRC */ 77 PIXELFORMATDESCRIPTOR pfd = 78 { 79 sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, 80 PFD_TYPE_RGBA, 32, 81 0, 0, 0, 0, 0, 0, 0, 0, 82 0, 83 0, 0, 0, 0, 84 24, //Number of bits for the depthbuffer 85 8, //Number of bits for the stencilbuffer 86 0, //Number of Aux buffers in the framebuffer. 87 PFD_MAIN_PLANE, 88 0, 89 0, 0, 0 90 }; 91 92 int format = ChoosePixelFormat(pdcDIB, &pfd); 93 94 if (format == 0) { 95 MessageBox(hwnd, L"pixel format error", L"Caution", MB_OK); 96 return -1; 97 } 98 99 if (SetPixelFormat(pdcDIB, format, &pfd) == FALSE) { 100 MessageBox(hwnd, L"Set error", L"Caution", MB_OK); 101 return -1; 102 } 103 104 /* MakeCurrent */ 105 106 wglMakeCurrent(pdcDIB, m_hrc); 107 108 /* init glfunc */ 109 glEnable(GL_ALPHA_TEST); 110 glEnable(GL_DEPTH_TEST); 111 glEnable(GL_COLOR_MATERIAL); 112 glEnable(GL_LIGHTING); 113 glEnable(GL_LIGHT0); 114 glEnable(GL_BLEND); 115 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 116 glClearColor(0, 0, 0, 0); 117 118 } 119 break; 120 121 case WM_SIZE: 122 { 123 iwidth = LOWORD(lParam); 124 iheight = HIWORD(lParam); 125 glViewport(0, 0, iwidth, iheight); 126 127 glMatrixMode(GL_PROJECTION); 128 glLoadIdentity(); 129 130 glMatrixMode(GL_MODELVIEW); 131 glLoadIdentity(); 132 133 InvalidateRect(hwnd, NULL, TRUE); 134 } 135 136 137 break; 138 139 case WM_PAINT: 140 { 141 PAINTSTRUCT ps = {}; 142 HDC hdc = BeginPaint(hwnd, &ps); 143 144 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 145 glPushMatrix(); 146 147 glColor3f(0, 1, 1); 148 149 glBegin(GL_TRIANGLES); 150 glColor3f(1.0f, 0.0f, 0.0f); 151 glVertex3f(0.0f, 1.0f, 0.0f); 152 glColor3f(0.0f, 1.0f, 0.0f); 153 glVertex3f(-1.0f, -1.0f, 0.0f); 154 glColor3f(0.0f, 0.0f, 1.0f); 155 glVertex3f(1.0f, -1.0f, 0.0f); 156 glEnd(); 157 158 glPopMatrix(); 159 glFlush(); 160 161 EndPaint(hwnd, &ps); 162 } 163 break; 164 165 case WM_DESTROY: 166 PostQuitMessage(0); 167 return 0; 168 } 169 170 return DefWindowProc(hwnd, uMsg, wParam, lParam); 171}
ウィンドウの透過処理(WS_EX_RAYERED
,SetLeyeredWindowAttribute
)はしていません.
補足情報
Windows10 Pro
VisualStudio2017 Community
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。