source: trunk/src/gl/gl_device.cc @ 302

Last change on this file since 302 was 302, checked in by epyon, 11 years ago
  • buffers and vertex_arrays are now handle based
File size: 4.9 KB
Line 
1// Copyright (C) 2012-2013 Kornel Kisielewicz
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#include "nv/gl/gl_device.hh"
6
7#include "nv/gl/gl_window.hh"
8#include "nv/gl/gl_program.hh"
9#include "nv/logging.hh"
10#include "nv/lib/sdl.hh"
11#include "nv/lib/sdl_image.hh"
12#include "nv/gl/gl_enum.hh"
13#include "nv/lib/gl.hh"
14
15using namespace nv;
16
17window* gl_device::create_window( uint16 width, uint16 height, bool fullscreen )
18{
19        return new gl_window( this, width, height, fullscreen );
20}
21
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
29gl_device::gl_device()
30{
31        nv::load_sdl_library();
32        m_info = NULL;
33
34        if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 )
35        {
36                NV_LOG( LOG_CRITICAL, "Video initialization failed: " << SDL_GetError( ) );
37                return; // TODO: Error report
38        }
39
40#if NV_SDL_VERSION == NV_SDL_12
41        m_info = SDL_GetVideoInfo( );
42
43        if ( !m_info )
44        {
45                NV_LOG( LOG_CRITICAL, "Video query failed: " << SDL_GetError( ) );
46                return; // TODO: Error report
47        }
48#endif
49
50}
51
52program* gl_device::create_program( const string& vs_source, const string& fs_source )
53{
54        return new gl_program( vs_source, fs_source );
55}
56
57// this is a temporary function that will be removed once we find a way to
58// pass binary file data around
59image_data* nv::gl_device::create_image_data( const std::string& filename )
60{
61        load_sdl_image_library();
62        SDL_Surface* image = IMG_Load( filename.c_str() );
63        if (!image)
64        {
65                NV_LOG( LOG_ERROR, "Image file " << filename.c_str() << " not found!" );
66                return nullptr;
67        }
68        // TODO: BGR vs RGB, single channel
69        assert( image->format->BytesPerPixel > 2 );
70        image_format format(image->format->BytesPerPixel == 3 ? RGB : RGBA, UBYTE );
71        image_data* data = new image_data( format, glm::ivec2( image->w, image->h ), (nv::uint8*)image->pixels );
72        return data;
73}
74
75uint32 gl_device::get_ticks()
76{
77        return SDL_GetTicks();
78}
79
80void gl_device::delay( uint32 ms )
81{
82        return SDL_Delay( ms );
83}
84
85gl_device::~gl_device()
86{
87        while ( m_textures.size() > 0 )
88                release( m_textures.get_handle(0) );
89        while ( m_buffers.size() > 0 )
90                release( m_buffers.get_handle(0) );
91
92        SDL_Quit();
93}
94
95nv::texture nv::gl_device::create_texture( ivec2 size, image_format aformat, sampler asampler, void* data /*= nullptr */ )
96{
97        unsigned glid = 0;
98        glGenTextures( 1, &glid );
99
100        glBindTexture( GL_TEXTURE_2D, glid );
101
102        // Detect if mipmapping was requested
103        if (( asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST ) ||
104                ( asampler.filter_max != sampler::LINEAR && asampler.filter_max != sampler::NEAREST ))
105        {
106                glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
107        }
108
109        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_min ) );
110        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_max ) );
111        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)nv::sampler_wrap_to_enum( asampler.wrap_s) );
112        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)nv::sampler_wrap_to_enum( asampler.wrap_t) );
113
114        if (data)
115        {
116                glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(aformat.format), size.x, size.y, 0, nv::image_format_to_enum(aformat.format), nv::datatype_to_gl_enum(aformat.type), data );
117        }
118
119        glBindTexture( GL_TEXTURE_2D, 0 );
120
121        texture result = m_textures.create();
122        gl_texture_info* info = m_textures.get( result );
123        info->format  = aformat;
124        info->sampler = asampler;
125        info->size    = size;
126        info->glid    = glid;
127        return result;
128}
129
130void nv::gl_device::release( texture t )
131{
132        gl_texture_info* info = m_textures.get( t );
133        if ( info )
134        {
135                if ( info->glid != 0 )
136                {
137                        glDeleteTextures( 1, &(info->glid) );
138                }
139                m_textures.destroy( t );
140        }
141}
142
143void nv::gl_device::release( buffer b )
144{
145        gl_buffer_info* info = m_buffers.get( b );
146        if ( info )
147        {
148                if ( info->glid != 0 )
149                {
150                        glDeleteBuffers( 1, &(info->glid) );
151                }
152                m_buffers.destroy( b );
153        }
154}
155
156const texture_info* nv::gl_device::get_texture_info( texture t )
157{
158        return m_textures.get( t );
159}
160
161nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
162{
163        unsigned glid   = 0;
164        unsigned glenum = buffer_type_to_enum( type );
165        glGenBuffers( 1, &glid );
166
167        glBindBuffer( glenum, glid );
168        glBufferData( glenum, (GLsizeiptr)size, source, buffer_hint_to_enum( hint ) );
169        glBindBuffer( glenum, 0 );
170
171        buffer result = m_buffers.create();
172        gl_buffer_info* info = m_buffers.get( result );
173        info->type = type;
174        info->hint = hint;
175        info->size = size;
176        info->glid = glid;
177        return result;
178}
179
180const buffer_info* nv::gl_device::get_buffer_info( buffer t )
181{
182        return m_buffers.get( t );
183}
Note: See TracBrowser for help on using the repository browser.