Advertisement
Guest User

OpenGL debug-context callstack-test

a guest
Oct 14th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #define WINDOWS_LEAN_AND_MEAN
  2. #include <Windows.h>
  3. #undef WINDOWS_LEAN_AND_MEAN
  4. #include <GL/gl.h>
  5. #include <assert.h>
  6. #include <iostream>
  7. #include "glext.h"
  8. #include "wglext.h"
  9.  
  10. //////////////////////////////////////////////////////////////////////////
  11. // TEST OF OPENGL-DEBUG-CONTEXT
  12. // to compile, switch unicode -> multi-byte and add link-input: OpenGL32.lib
  13. //////////////////////////////////////////////////////////////////////////
  14.  
  15. #define INIT_ENTRY_POINT2( FUNCTYPE, FUNCNAME ) {\
  16.     FUNCNAME = reinterpret_cast<FUNCTYPE>( wglGetProcAddress( #FUNCNAME ) ); \
  17.     if(!FUNCNAME) { std::cerr << (#FUNCNAME) << " not initialized" << std::endl; } }
  18.  
  19. GLboolean queryGlExtension( char *extName );
  20. GLboolean queryGlExtension( char *extName );
  21. LRESULT WINAPI WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  22. HWND CreateOpenGLWindow( char* title, int x, int y, int width, int height );
  23. void init();
  24.  
  25. void __stdcall DebugCallback(
  26.     GLenum,
  27.     GLenum,
  28.     GLuint,
  29.     GLenum,
  30.     GLsizei,
  31.     const GLchar*,
  32.     GLvoid* )
  33. {
  34.     __debugbreak();
  35. }
  36.  
  37.  
  38. int main() {
  39.     init();
  40.     // note: cause gl-error
  41.     glEnable( GL_BLEND_EQUATION_ALPHA );
  42.  
  43.     return 0;
  44. }
  45.  
  46.  
  47. void init() {
  48.  
  49.     HDC   m_hDC;    // device context
  50.     HGLRC m_hRC;    // opengl context
  51.     HWND  m_hWnd;   // window
  52.  
  53.     m_hWnd = CreateOpenGLWindow( "OpenGL", 0, 0, 640, 360 );
  54.     if( m_hWnd == NULL) {
  55.         exit(1);
  56.     }
  57.     m_hDC = GetDC(m_hWnd);
  58.     m_hRC = wglCreateContext(m_hDC);
  59.     wglMakeCurrent(m_hDC, m_hRC);
  60.  
  61.     PFNWGLCREATECONTEXTATTRIBSARBPROC   wglCreateContextAttribsARB = 0;
  62.     INIT_ENTRY_POINT2( PFNWGLCREATECONTEXTATTRIBSARBPROC, wglCreateContextAttribsARB );
  63.  
  64.     // Clean up and delete old GL context
  65.     wglMakeCurrent(NULL, NULL);
  66.     wglDeleteContext( m_hRC );
  67.  
  68.     // Create a new (optionally debug/core/compatibility) context
  69.     const int contextAttribs[] = {  WGL_CONTEXT_FLAGS_ARB,
  70.                                     WGL_CONTEXT_DEBUG_BIT_ARB,
  71.                                     0 };
  72.  
  73.     m_hRC = wglCreateContextAttribsARB( m_hDC, 0, contextAttribs );
  74.     if ( m_hRC == NULL )
  75.     {
  76.         int err = GetLastError();
  77.         std::cerr << err << std::endl;
  78.         assert( false && "catastrophy!" ); // note: gently handle error
  79.     }
  80.     wglMakeCurrent( m_hDC, m_hRC );
  81.  
  82.     ShowWindow( m_hWnd, SW_SHOWNORMAL );
  83.  
  84.     if( !queryGlExtension( "GL_ARB_debug_output" )) {
  85.         std::cerr << "# WARNING: GL_ARB_debug_output not supported" << std::endl;
  86.     }
  87.  
  88.     PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARB = 0;
  89.     INIT_ENTRY_POINT2( PFNGLDEBUGMESSAGECALLBACKARBPROC, glDebugMessageCallbackARB );
  90.  
  91.     glDebugMessageCallbackARB( DebugCallback, 0 );
  92.     glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB );
  93. }
  94.  
  95.  
  96. //////////////////////////////////////////////////////////////////////////
  97.  
  98. GLboolean queryGlExtension( char *extName ) {
  99.     char *p = (char *) glGetString(GL_EXTENSIONS);
  100.     assert( p );
  101.     char *end = p + strlen(p);
  102.     while (p < end) {
  103.         size_t n = strcspn(p, " ");
  104.         if( (strlen(extName)==n) && (strncmp(extName,p,n)==0) ) {
  105.             return GL_TRUE;
  106.         }
  107.         p += (n + 1);
  108.     }
  109.     return GL_FALSE;
  110. }
  111.  
  112.  
  113. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
  114. GLboolean queryWglExtension( char *extName ) {
  115.  
  116.     if ( !wglGetExtensionsStringARB ) {
  117.         INIT_ENTRY_POINT2( PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB );
  118.     }
  119.  
  120.     char const *p = static_cast<char const *>( wglGetExtensionsStringARB( wglGetCurrentDC() ) );
  121.     assert( p );
  122.     char const *end = p + strlen(p);
  123.     while (p < end) {
  124.         size_t n = strcspn(p, " ");
  125.         if ((strlen(extName)==n) && (strncmp(extName,p,n)==0)) {
  126.             return GL_TRUE;
  127.         }
  128.         p += (n + 1);
  129.     }
  130.     return GL_FALSE;
  131. }
  132.  
  133.  
  134. LRESULT WINAPI WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  135. {
  136.     switch(uMsg) {
  137.     case WM_SIZE:
  138.         PostMessage(hWnd, WM_PAINT, 0, 0);
  139.         return 0;
  140.     case WM_CHAR:
  141.         switch (wParam) {
  142.         case 'q': case 'Q': exit(0); break;
  143.         }
  144.     case WM_KEYDOWN:
  145.         switch ( wParam )
  146.         {
  147.         case VK_ESCAPE: PostQuitMessage(0); return 0;
  148.         }
  149.         return 0;
  150.     case WM_CLOSE:
  151.         exit(0);
  152.     }
  153.     return DefWindowProc( hWnd, uMsg, wParam, lParam );
  154. }
  155.  
  156. HWND CreateOpenGLWindow( char* title, int x, int y, int width, int height )
  157. {
  158.     WNDCLASS wc;
  159.     static HINSTANCE hInstance = 0;
  160.  
  161.     // only register the window class once - use hInstance as a flag
  162.     if (!hInstance) {
  163.         hInstance        = GetModuleHandle(NULL);
  164.         wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  165.         wc.lpfnWndProc   = (WNDPROC)WindowProc;
  166.         wc.cbClsExtra    = 0;
  167.         wc.cbWndExtra    = 0;
  168.         wc.hInstance     = hInstance;
  169.         wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
  170.         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  171.         wc.hbrBackground = NULL;
  172.         wc.lpszMenuName  = NULL;
  173.         wc.lpszClassName = "OpenGL";
  174.         //wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  175.  
  176.         if (!RegisterClass(&wc)) {
  177.             MessageBox(NULL, "RegisterClass() failed: Cannot register window class.", "Error", MB_OK);
  178.             return NULL;
  179.         }
  180.     }
  181.  
  182.     DWORD dwExStyle;                // Window Extended Style
  183.     DWORD dwStyle;              // Window Style
  184.     dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;           // Window Extended Style
  185.     dwStyle=WS_OVERLAPPEDWINDOW;                            // Windows Style
  186.  
  187.     HWND hWnd = CreateWindowEx( dwExStyle,
  188.         "OpenGL",
  189.         title,
  190.         dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  191.         x, y,
  192.         width, height,
  193.         NULL,
  194.         NULL,
  195.         hInstance,
  196.         NULL);
  197.  
  198.     if (hWnd == NULL) {
  199.         MessageBox(NULL, "CreateWindow() failed:  Cannot create a window.", "Error", MB_OK);
  200.         return NULL;
  201.     }
  202.  
  203.     HDC hDC = GetDC(hWnd);
  204.  
  205.     PIXELFORMATDESCRIPTOR pfd;
  206.     memset(&pfd, 0, sizeof(pfd));
  207.     pfd.nSize        = sizeof(pfd);
  208.     pfd.nVersion     = 1;
  209.     pfd.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  210.     pfd.iPixelType   = PFD_TYPE_RGBA;
  211.     pfd.cColorBits   = 32;
  212.     pfd.cDepthBits   = 24;
  213.     pfd.cStencilBits = 8;
  214.     pfd.cAlphaBits = 0;
  215.     pfd.cAccumBits = 0;
  216.     pfd.cAccumRedBits = 0;
  217.     pfd.cAccumGreenBits = 0;
  218.     pfd.cAccumBlueBits = 0;
  219.  
  220.     int pf = ChoosePixelFormat(hDC, &pfd);
  221.     if (pf == 0) {
  222.         std::cerr << "ChoosePixelFormat() failed: Cannot find a suitable pixel format." << std::endl;
  223.         return 0;
  224.     }
  225.  
  226.     if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
  227.         std::cerr << "ChoosePixelFormat() failed: Cannot find a suitable pixel format." << std::endl;
  228.         return 0;
  229.     }
  230.  
  231.     DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  232.     ReleaseDC(hWnd, hDC);
  233.  
  234.     return hWnd;
  235. }    
  236.  
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement