Exemplo n.º 1
0
// Frees all resources and removes all assets from asset library
//-----------------------------------------------------------------------------
void CPUT_OGL::RestartCPUT()
{
    //
    // Clear out all CPUT resources
    //
//    CPUTInputLayoutCacheDX11::GetInputLayoutCache()->ClearLayoutCache();
    CPUTAssetLibrary::GetAssetLibrary()->ReleaseAllLibraryLists();
//	CPUTGuiControllerDX11::GetController()->DeleteAllControls();
//	CPUTGuiControllerDX11::GetController()->ReleaseResources();

    //
    // Clear out all DX resources and contexts
    //
    DestroyOGLContext();

    //
    // Signal the window to close
    //
  //  mpWindow->Destroy();

    //
    // Clear out the timer
    //
    mpTimer->StopTimer();
    mpTimer->ResetTimer();
    
    HEAPCHECK;
}
Exemplo n.º 2
0
void OGLWindow::DestroyOGLWindow()
{
	DestroyOGLContext();

	DestroyWindow( m_hwnd );

	m_hwnd = NULL;
	m_hdc = NULL;
}
Exemplo n.º 3
0
OGLWindow::~OGLWindow()
{
	//Clean up the renderable
	delete m_mesh;
	delete m_shader;
	delete m_skyBox;
	delete m_skyBoxShader;
	delete m_plane;

	DestroyOGLContext();
}
Exemplo n.º 4
0
// Actually destroy all 'global' resource handling objects, all asset handlers,
// the DX context, and everything EXCEPT the window
//-----------------------------------------------------------------------------
void CPUT_OGL::ShutdownAndDestroy()
{
    // make sure no more rendering can happen
    mbShutdown = true;

    // call the user's OnShutdown code
    Shutdown();
	
    CPUTAssetLibrary::DeleteAssetLibrary();
    CPUTGuiControllerOGL::DeleteController();
	CPUTRenderStateBlock::SetDefaultRenderStateBlock( NULL );
	
    SAFE_RELEASE(mpPerFrameConstantBuffer);
    SAFE_RELEASE(mpPerModelConstantBuffer);

    DestroyOGLContext();

    // Tell the window layer to post a close-window message to OS
    // and stop the message pump
    mpWindow->Destroy();

    HEAPCHECK;
}
HWND CPUT_OGL::CreateDummyWindow(HINSTANCE hInst)
{
    WNDCLASS  Window;      // Window class
    uint32    PixelFormat; // Format of pixel
    
    // Window settings
    Window.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;  // Have its own Device Context and also cannot be resized    
    Window.lpfnWndProc   = (WNDPROC) WndProcDummy;              // Procedure that handles OS evets
    Window.cbClsExtra    = 0;                                   // No extra window data
    Window.cbWndExtra    = 0;                                   //
    Window.hInstance     = hInst;                               // Handle to instance of program that this window belongs to
    Window.hIcon         = LoadIcon(NULL, IDI_WINLOGO);         // Load default icon
    Window.hCursor       = LoadCursor(NULL, IDC_ARROW);         // Load arrow pointer
    Window.hbrBackground = NULL;                                // No background (OpenGL will handle this)
    Window.lpszMenuName  = NULL;                                // No menu
    Window.lpszClassName = L"DummyWindowClass";                 // Class name
    
    // Registering Window Class
    if ( !RegisterClass( &Window ) )	
    {					
        cerr << "Error! Cannot register window class." << endl;
        return NULL;
    }

    // Create Dummy Window with OpenGL RC to gather required
    // information together with wgl functions pointers and
    // final Pixel Format number.
    HWND hWnd = CreateWindow(
        L"DummyWindowClass",           // Window class name
        L"CPUTFramework...",           // Title of window            
        WS_CLIPSIBLINGS |             // \_Prevents from overdrawing
        WS_CLIPCHILDREN |             // / by other windows
        WS_OVERLAPPEDWINDOW,          // Additional styles
        0, 0,                         // Position
        128,                          // True window widht
        128,                          // True window height
        NULL,                         // No parent window
        NULL,                         // No menu
        hInst,                        // Handle to this instance of program
        NULL );                       // Won't pass anything
    
    // Check if dummy window was created
    if ( hWnd == NULL )
    {
        cerr << "Error! Cannot create window." << endl;
        DestroyOGLContext();	
        return NULL;
    }  
    
    // Acquiring Device Context
    mhDC = GetDC( hWnd );
    if ( mhDC == NULL )
    {
        cerr << "Error! Cannot create device context." << endl;		
        DestroyOGLContext();	
        return NULL;
    }  
    
    // Choosing Pixel Format the old way
    PixelFormat = ChoosePixelFormat( mhDC, &pfdLegacy );
    if ( PixelFormat == NULL )
    {
        cerr << "Error! Cannot find aproprieate pixel format." << endl;	
        DestroyOGLContext();	
        return NULL;
    }  
    
    // Activating Pixel Format
    if ( !SetPixelFormat( mhDC, PixelFormat, &pfdLegacy ) )
    {	
        cerr << "Error! Cannot init pixel format." << endl;
        DestroyOGLContext();		
        return NULL;
    }  
    
    // Creating OpenGL Rendering Context
    mhRC = wglCreateContext( mhDC );
    if ( mhRC == NULL )
    {
        cerr << "Error! Cannot create window rendering context." << endl;
        DestroyOGLContext();						
        return NULL;
    }  
    
    // Activating rendering context
    if ( !wglMakeCurrent( mhDC, mhRC ) )
    {	
        cerr << "Error! Cannot activate rendering context." << endl;
        DestroyOGLContext();	
        return NULL;
    } 
    
    return hWnd;
}
CPUTResult CPUT_OGL::CreateOGLContext(CPUTContextCreation ContextParams )
{
	HWND hWnd;

	hWnd = CreateDummyWindow( GetModuleHandle(NULL));
    // Create dummy window to gather information about OpenGL
    if ( ! hWnd) 
    {
        return CPUT_ERROR;
    }

    // To get list of supported extensions, this 
    // code should be used since OpenGL 3.0 Core:
    //
    // uint32 count = 0;
    // glGetIntegerv(GL_NUM_EXTENSIONS, (GLint*)&count);
    // for(uint16 i=0; i<count; i++)
    //    support.m_extensions.insert(support.m_extensions.end(),string((char*)glGetStringi(GL_EXTENSIONS,i));
    //
    // But data about gpu, are gathered in dummy
    // OpenGL context which works in deprecaded
    // mode. This forces implementation in old
    // deprecated way:
    
    // Creates table of supported extensions strings
    extensions.clear();
    string tmp;
    sint32 begin, end;
    tmp   = string( (char*)glGetString( GL_EXTENSIONS ) );
    begin = 0;
    end   = tmp.find( ' ', 0 );
    while( end != string::npos )
    {
        extensions.insert( extensions.end(), tmp.substr( begin, end-begin ) );
        begin = end + 1;
        end   = tmp.find( ' ', begin );
    }
    
    // This extension together with WGL_EXT_swap_interval
    // should be returned in list of supported extensions
    // after calling glGetString(GL_EXTENSIONS);
    // Because NVidia drivers don't follow this rule it's
    // specification says as follow:
    // 
    // "Applications should call wglGetProcAddress to see 
    // whether or not wglGetExtensionsStringARB is supported."
    // ( http://www.opengl.org/registry/specs/ARB/wgl_extensions_string.txt )
    //
    bool wglexts = false;
    wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress( "wglGetExtensionsStringARB" );  
    if ( wglGetExtensionsStringARB != NULL )
    {
        tmp = string( (char*)wglGetExtensionsStringARB( mhDC ) );
        wglexts = true;
    }
    else
    {
        wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC) wglGetProcAddress( "wglGetExtensionsStringEXT" );
        if ( wglGetExtensionsStringEXT != NULL )
        {
            tmp = string( (char*)wglGetExtensionsStringEXT() ); 
            wglexts = true; 
        }
    }

    // If it is possible to obtain WGL extensions list add
    // them to the rest of supported extensions.
    if ( wglexts )
    {
        begin = 0;
        end   = tmp.find( ' ', 0 );
        while( end != string::npos )
        {
            extensions.insert( extensions.end(), tmp.substr( begin, end-begin ) );
            begin = end + 1;
            end   = tmp.find( ' ', begin );
        }
	}

	// Get pointers to WGL specific functions that are required during window creation
    wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );
    wglChoosePixelFormatARB    = (PFNWGLCHOOSEPIXELFORMATARBPROC)    wglGetProcAddress( "wglChoosePixelFormatARB" );
    wglChoosePixelFormatEXT    = (PFNWGLCHOOSEPIXELFORMATEXTPROC)    wglGetProcAddress( "wglChoosePixelFormatEXT" );
 
	// Set Samples count in Advanced Pixel Format
	iAttributes[27] = ContextParams.samples;

    // Choosing advanced Pixel Format in modern way 
    uint32 PixelFormat = 0;
    bool choosedPixelFormat = false;   
    if ( supportExtension( "WGL_ARB_pixel_format" ) )
    {
        uint32 numFormats;
        if ( wglChoosePixelFormatARB( mhDC, (const int*)iAttributes, fAttributes, 1, (int*)&PixelFormat, (unsigned int*)&numFormats ) )
            if ( numFormats >= 1 )
                choosedPixelFormat = true;
    }
    
    // Old way for choosing advanced Pixel Format
    if ( !choosedPixelFormat &&
         supportExtension( "WGL_EXT_pixel_format" ) )
    {
        uint32 numFormats;
        if ( wglChoosePixelFormatEXT( mhDC, (const int*)iAttributes, fAttributes, 1, (int*)&PixelFormat, (unsigned int*)&numFormats ) )
            if ( numFormats >= 1 )
                choosedPixelFormat = true;
    }

    // Basic Pixel Format
    if ( !choosedPixelFormat )
        PixelFormat = ChoosePixelFormat( mhDC, &pfdLegacy );

    // If couldn't find Pixel Format report error
    if ( PixelFormat == NULL )
    {
        cerr << "Error! Cannot find aproprieate pixel format." << endl;	
        DestroyOGLContext();	
        return CPUT_ERROR;
    } 


   // Data about OpenGL are gathered, proper Pixel Format is choosed.
    // Destroy Dummy Window and proceed to creation of proper window.
    // Disabling and deleting all rendering contexts
    wglMakeCurrent( NULL, NULL ); 
    wglDeleteContext( mhRC );
    mhRC = NULL;
    // Disabling device context
    ReleaseDC( hWnd, mhDC ); 
    mhDC = 0;
   
	// Deleting window
    DestroyWindow( hWnd );  

    // Unregistering window class
    if(TRUE != UnregisterClass( L"DummyWindowClass", GetModuleHandle(NULL) ))
	{
		return CPUT_ERROR;
	}

    // Clear message queue
    MSG msg = { 0 };
    while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );

    }

	hWnd = mpWindow->GetHWnd();

   

    // Acquiring Device Context
    mhDC = GetDC( hWnd );
    if ( mhDC == NULL )
    {
        cerr << "Error! Cannot create device context." << endl;		
        DestroyOGLContext();	
        return CPUT_ERROR;
    }  

    // Activating Pixel Format
    if ( !SetPixelFormat( mhDC, PixelFormat, &pfdLegacy ) )
    {	
        cerr << "Error! Cannot init pixel format." << endl;
        DestroyOGLContext();	
        return CPUT_ERROR;
	} 

    // OpenGL 4.0 Core profile settings
    uint32 glAttributes[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
                              WGL_CONTEXT_MINOR_VERSION_ARB, 0,
                              WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
                              GL_CONTEXT_PROFILE_MASK, GL_CONTEXT_CORE_PROFILE_BIT,
                              0, 0 };
   //         GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
     //   GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
    // Debug version supports better debugging
#ifdef _DEBUG
    glAttributes[5] |= WGL_CONTEXT_DEBUG_BIT_ARB;
#endif 

    // Try to create OpenGL context in new way
    mhRC = wglCreateContextAttribsARB( mhDC, 0, (const int *) &glAttributes );
    if ( mhRC == NULL )
    {
        cerr << "Error! Cannot create window rendering context." << endl;
        DestroyOGLContext();				
        return CPUT_ERROR;
    }  

    // Activating rendering context
    if( !wglMakeCurrent( mhDC, mhRC ) )
    {	
        std::cerr << "Error! Cannot activate rendering context." << endl;
        DestroyOGLContext();	
        return CPUT_ERROR;
    } 
    
    
    // Setting created window as current 
    ShowWindow( hWnd, SW_SHOW );
    SetForegroundWindow( hWnd );
    SetFocus( hWnd );
    
    // Link all OpenGL function pointers
    glewExperimental = TRUE;
    if ( glewInit() != GLEW_OK )
    {
        cerr << "Error! Cannot activate GLEW." << endl;
        DestroyOGLContext();	
        return CPUT_ERROR;
    }

    // GLEW's problem is that it calls glGetString(GL_EXTENSIONS) which causes 
    // GL_INVALID_ENUM on GL 3.2 core context as soon as glewInit() is called.
    // Clear errors indicator 
    glGetError();
    
    wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress( "wglSwapIntervalEXT" );
    wglSwapIntervalEXT(0);

    return CPUT_SUCCESS;
}