Changeset 245 for trunk/src/gl


Ignore:
Timestamp:
05/27/14 16:26:53 (11 years ago)
Author:
epyon
Message:
  • gl library wgl support
  • gl context and window adoption
Location:
trunk/src/gl
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gl/gl_context.cc

    r233 r245  
    362362
    363363
    364 gl_context::gl_context( device* a_device, void* a_win_handle )
    365         : context( a_device ), m_handle( nullptr )
     364gl_context::gl_context( device* a_device )
     365        : context( a_device )
     366{
     367}
     368
     369
     370nv::gl_context::~gl_context()
     371{
     372}
     373
     374
     375void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
     376{
     377        apply_render_state( rs );
     378        if ( count > 0 )
     379        {
     380                p->bind();
     381                va->bind();
     382                if ( va->has_index_buffer() )
     383                {
     384                        glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
     385                }
     386                else
     387                {
     388                        glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
     389                }
     390                va->unbind();
     391                p->unbind();
     392        }
     393}
     394
     395nv::sdl_gl_context::sdl_gl_context( device* a_device, void* a_sdl_win_handle )
     396        : gl_context( a_device ), m_handle( nullptr )
    366397{
    367398#if NV_SDL_VERSION == NV_SDL_20
    368         m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_win_handle ) );
     399        m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_sdl_win_handle ) );
    369400
    370401        if ( m_handle == 0 )
     
    384415        NV_LOG( LOG_INFO, "OpenGL GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) );
    385416#if NV_SDL_VERSION == NV_SDL_20
    386 //      SDL_GL_SetSwapInterval(1);
     417        //      SDL_GL_SetSwapInterval(1);
    387418#endif
    388419
     
    393424}
    394425
    395 
    396 nv::gl_context::~gl_context()
     426nv::sdl_gl_context::~sdl_gl_context()
    397427{
    398428#if NV_SDL_VERSION == NV_SDL_20
     
    401431}
    402432
    403 
    404 void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
    405 {
    406         apply_render_state( rs );
    407         if ( count > 0 )
    408         {
    409                 p->bind();
    410                 va->bind();
    411                 if ( va->has_index_buffer() )
    412                 {
    413                         glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
    414                 }
    415                 else
    416                 {
    417                         glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
    418                 }
    419                 va->unbind();
    420                 p->unbind();
    421         }
    422 }
     433nv::native_gl_context::native_gl_context( device* a_device, void* a_native_win_handle )
     434        : gl_context( a_device ), m_handle( nullptr )
     435{
     436#if NV_PLATFORM == NV_WINDOWS
     437
     438// TODO: error checking
     439        HDC hdc = (HDC)a_native_win_handle;
     440
     441        const int wgl_attrib_list[] =
     442        {
     443                WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
     444                WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
     445                WGL_ACCELERATION_ARB,   WGL_FULL_ACCELERATION_ARB,
     446                WGL_DOUBLE_BUFFER_ARB,  GL_TRUE,
     447                WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
     448                WGL_COLOR_BITS_ARB,     32,
     449                WGL_DEPTH_BITS_ARB,     24,
     450                WGL_STENCIL_BITS_ARB,   8,
     451                0, 0  //End
     452        };
     453
     454        unsigned int num_formats;
     455        int pixel_format;
     456        PIXELFORMATDESCRIPTOR pfd;
     457
     458        if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
     459        {
     460                return;
     461        }
     462
     463        if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
     464        {
     465                //int err = GetLastError();
     466                return;
     467        }
     468
     469        int attribs[] =
     470        {
     471                WGL_CONTEXT_MAJOR_VERSION_ARB,   2,
     472                WGL_CONTEXT_MINOR_VERSION_ARB,   1,
     473                WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
     474                //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
     475                0, 0  //End
     476        };
     477
     478        HGLRC handle;
     479        if ( 0 == (handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
     480        {
     481                return;
     482        }
     483
     484        if ( FALSE == dynwglMakeCurrent( hdc, handle ) )
     485        {
     486                return;
     487        }     
     488
     489        m_handle = (void*)handle;
     490#else
     491        NV_ASSERT( false, "Native GL context not implemented for this platform!" );
     492#endif
     493        force_apply_render_state( m_render_state );
     494}
     495
     496nv::native_gl_context::~native_gl_context()
     497{
     498#if NV_PLATFORM == NV_WINDOWS
     499        dynwglDeleteContext( (HGLRC)m_handle );
     500#endif
     501}
  • trunk/src/gl/gl_device.cc

    r229 r245  
    2020}
    2121
     22window* nv::gl_device::adopt_window( void* sys_w_handle, void* sys_dc )
     23{
     24        return new gl_window( this, sys_w_handle, sys_dc );
     25}
     26
     27
     28
    2229gl_device::gl_device()
    2330{
     
    3946                return; // TODO: Error report
    4047        }
    41 #endif
    42 
    43 //      bpp = m_info->vfmt->BitsPerPixel;
    44 
    45         SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    46         SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    47         SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    48         SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
    49         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    50 
    51 //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
    52 //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
    53 
    54 #if NV_SDL_VERSION == NV_SDL_20
    55         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    56         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    57         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    58         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    5948#endif
    6049
  • trunk/src/gl/gl_window.cc

    r243 r245  
    3434                        int capslock = !!(ke.keysym.mod & KMOD_CAPS);
    3535                        if ((shifted ^ capslock) != 0) {
    36                                 kevent.key.ascii = SDL_toupper(ucode);
     36                                kevent.key.ascii = (char8)SDL_toupper(ucode);
    3737                        }
    3838                }
     
    204204
    205205gl_window::gl_window( device* dev, uint16 width, uint16 height, bool fullscreen )
    206         : m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_handle( nullptr )
    207 {
     206        : m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_handle( nullptr ), m_adopted( false )
     207{
     208        //      bpp = m_info->vfmt->BitsPerPixel;
     209
     210        SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
     211        SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
     212        SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
     213        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
     214        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
     215
     216        //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
     217        //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
     218
     219#if NV_SDL_VERSION == NV_SDL_20
     220        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
     221        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
     222        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     223        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
     224#endif
     225
     226
    208227#if NV_SDL_VERSION == NV_SDL_12
    209228        uint32 flags = SDL_OPENGL;
     
    233252
    234253
    235         m_context = new gl_context( m_device, m_handle );
     254        m_context = new sdl_gl_context( m_device, m_handle );
    236255        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
    237256}
     
    254273void gl_window::set_title( const string& title )
    255274{
     275        if ( m_adopted ) return;
    256276#if NV_SDL_VERSION == NV_SDL_20
    257277        SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.c_str() );
     
    276296void gl_window::swap_buffers()
    277297{
     298        if ( m_adopted ) return; // NOT SURE
    278299#if NV_SDL_VERSION == NV_SDL_20
    279300        SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
     
    287308        delete m_context;
    288309#if NV_SDL_VERSION == NV_SDL_20
    289         SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
    290 #endif
    291 }
     310        if ( !m_adopted ) SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
     311#endif
     312}
     313
     314nv::gl_window::gl_window( device* dev, void* handle, void* dc )
     315        : m_device( dev ), m_width( 0 ), m_height( 0 ), m_title(), m_handle( nullptr ), m_adopted( true )
     316{
     317#if NV_PLATFORM == NV_WINDOWS
     318#if NV_SDL_VERSION == NV_SDL_20
     319        nv::load_gl_no_context();
     320        m_context = new native_gl_context( m_device, dc );
     321        SDL_Window* window = SDL_CreateWindowFrom( handle );
     322        // Doesn't work :/
     323//      RECT rect;
     324//      GetClientRect( (HWND)handle, &rect );
     325//      m_width   = (uint16)rect.right;
     326//      m_height  = (uint16)rect.bottom;
     327        m_handle  = window;
     328        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
     329#else
     330        NV_ASSERT( false, "Native GL context only working with SDL 2.0!" );
     331#endif
     332#else
     333        NV_ASSERT( false, "Native GL context adoption not implemented for this platform!" );
     334#endif
     335}
Note: See TracChangeset for help on using the changeset viewer.