Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.84 KB | None | 0 0
  1. #define _WIN32_WINNT 0x0500
  2.  
  3. #include <windows.h>
  4. #include <windowsx.h>
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7.  
  8. #include <dwmapi.h>
  9.  
  10. #pragma comment (lib, "opengl32.lib")
  11. #pragma comment (lib, "glu32.lib")
  12.  
  13. #pragma comment (lib, "dwmapi.lib")
  14.  
  15. #include <assert.h>
  16. #include <tchar.h>
  17.  
  18. #ifdef  assert
  19. #define verify(expr) if(!expr) assert(0)
  20. #else verify(expr) expr
  21. #endif
  22.  
  23. const TCHAR szAppName[]=_T("TransparentGL");
  24. const TCHAR wcWndName[]=_T("TransparentGL");
  25.  
  26. HDC hDC;            
  27. HGLRC m_hrc;        
  28. int w = 240;
  29. int h = 240;
  30.  
  31. BOOL initSC() {
  32.     glEnable(GL_ALPHA_TEST);        
  33.     glEnable(GL_DEPTH_TEST);        
  34.     glEnable(GL_COLOR_MATERIAL);
  35.  
  36.     glEnable(GL_LIGHTING);          
  37.     glEnable(GL_LIGHT0);            
  38.  
  39.     glEnable(GL_BLEND);            
  40.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  41.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  42.     glClearColor(0, 0, 0, 0);
  43.  
  44.     return 0;
  45. }
  46.  
  47. void resizeSC(int width,int height) {
  48.     glViewport(0,0,width,height);
  49.     glMatrixMode(GL_PROJECTION);
  50.     glLoadIdentity();
  51.  
  52.     glMatrixMode(GL_MODELVIEW );
  53.     glLoadIdentity();
  54. }
  55.  
  56. BOOL renderSC() {
  57.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  58.  
  59.     glPushMatrix();
  60.  
  61.     glColor3f(0, 1, 1);
  62.     glBegin(GL_TRIANGLES);                              // Drawing Using Triangles
  63.         glColor4f(1.0f,0.0f,0.0f,0.0f);                      // Set The Color To Red
  64.         glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
  65.         glColor4f(0.0f,1.0f,0.0f,1.0f);                      // Set The Color To Green
  66.         glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
  67.         glColor4f(0.0f,0.0f,1.0f,1.0f);                      // Set The Color To Blue
  68.         glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
  69.     glEnd();
  70.  
  71.     glPopMatrix();
  72.     glFlush();
  73.  
  74.     return 0;
  75. }
  76.  
  77. BOOL CreateHGLRC(HWND hWnd) {
  78.     PIXELFORMATDESCRIPTOR pfd = {
  79.       sizeof(PIXELFORMATDESCRIPTOR),
  80.       1,                                // Version Number
  81.       PFD_DRAW_TO_WINDOW      |         // Format Must Support Window
  82.       PFD_SUPPORT_OPENGL      |         // Format Must Support OpenGL
  83.       PFD_SUPPORT_COMPOSITION |         // Format Must Support Composition
  84.       PFD_DOUBLEBUFFER,                 // Must Support Double Buffering
  85.       PFD_TYPE_RGBA,                    // Request An RGBA Format
  86.       32,                               // Select Our Color Depth
  87.       0, 0, 0, 0, 0, 0,                 // Color Bits Ignored
  88.       8,                                // An Alpha Buffer
  89.       0,                                // Shift Bit Ignored
  90.       0,                                // No Accumulation Buffer
  91.       0, 0, 0, 0,                       // Accumulation Bits Ignored
  92.       24,                               // 16Bit Z-Buffer (Depth Buffer)
  93.       8,                                // Some Stencil Buffer
  94.       0,                                // No Auxiliary Buffer
  95.       PFD_MAIN_PLANE,                   // Main Drawing Layer
  96.       0,                                // Reserved
  97.       0, 0, 0                           // Layer Masks Ignored
  98.    };    
  99.  
  100.    HDC hdc = GetDC(hWnd);
  101.    int PixelFormat = ChoosePixelFormat(hdc, &pfd);
  102.    if (PixelFormat == 0) {
  103.       assert(0);
  104.       return FALSE ;
  105.    }
  106.  
  107.    BOOL bResult = SetPixelFormat(hdc, PixelFormat, &pfd);
  108.    if (bResult==FALSE) {
  109.       assert(0);
  110.       return FALSE ;
  111.    }
  112.  
  113.    m_hrc = wglCreateContext(hdc);
  114.    if (!m_hrc){
  115.       assert(0);
  116.       return FALSE;
  117.    }
  118.  
  119.    ReleaseDC(hWnd, hdc);
  120.  
  121.    return TRUE;
  122. }
  123.  
  124. LRESULT CALLBACK WindowFunc(HWND hWnd,UINT msg, WPARAM wParam, LPARAM lParam) {
  125.     PAINTSTRUCT ps;
  126.  
  127.     switch(msg) {
  128.         case WM_CREATE:
  129.         break;
  130.  
  131.         case WM_DESTROY:
  132.             if(m_hrc) {
  133.                 wglMakeCurrent(NULL, NULL);
  134.                 wglDeleteContext(m_hrc) ;
  135.             }
  136.             PostQuitMessage(0) ;
  137.         break;
  138.  
  139.         default:
  140.             return DefWindowProc(hWnd,msg,wParam,lParam);
  141.     }
  142.  
  143.     return 0;
  144. }
  145.  
  146. int WINAPI _tWinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPTSTR str,int nWinMode) {
  147.     WNDCLASSEX wc;
  148.     memset(&wc, 0, sizeof(wc));
  149.     wc.cbSize = sizeof(WNDCLASSEX);
  150.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  151.     wc.style = CS_HREDRAW | CS_VREDRAW;
  152.     wc.lpfnWndProc = (WNDPROC)WindowFunc;
  153.     wc.cbClsExtra  = 0;
  154.     wc.cbWndExtra  = 0;
  155.     wc.hInstance = hThisInst;
  156.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  157.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  158.     wc.hbrBackground = (HBRUSH)CreateSolidBrush(0x00000000);
  159.     wc.lpszClassName = szAppName;
  160.  
  161.     if(!RegisterClassEx(&wc)) {
  162.         MessageBox(NULL, _T("RegisterClassEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
  163.         return FALSE;
  164.     }
  165.  
  166.     HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szAppName, wcWndName,
  167.                     WS_VISIBLE | WS_POPUP, 200, 150, w, h,
  168.                     NULL, NULL, hThisInst, NULL);
  169.  
  170.     if(!hWnd) {
  171.         MessageBox(NULL, _T("CreateWindowEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
  172.         return FALSE;
  173.     }
  174.  
  175.     DWM_BLURBEHIND bb = {0};
  176.     HRGN hRgn = CreateRectRgn(0, 0, -1, -1);
  177.     bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
  178.     bb.hRgnBlur = hRgn;
  179.     bb.fEnable = TRUE;
  180.     DwmEnableBlurBehindWindow(hWnd, &bb);
  181.  
  182.     CreateHGLRC(hWnd);
  183.  
  184.     HDC hdc = GetDC(hWnd);
  185.     wglMakeCurrent(hdc, m_hrc);
  186.     initSC();
  187.     resizeSC(w, h);
  188.     ReleaseDC(hWnd, hdc);
  189.  
  190.     MSG msg;  
  191.     while(1) {
  192.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  193.             TranslateMessage(&msg);
  194.             DispatchMessage(&msg);
  195.         }
  196.         else {
  197.             HDC hdc = GetDC(hWnd);
  198.             wglMakeCurrent(hdc, m_hrc);
  199.  
  200.             renderSC();
  201.  
  202.             SwapBuffers(hdc);
  203.             ReleaseDC(hWnd, hdc);
  204.         }
  205.     }
  206.  
  207.     return (FALSE);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement