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/gl/gl_vertex_buffer.hh"
|
---|
10 | #include "nv/logging.hh"
|
---|
11 | #include "nv/lib/sdl.hh"
|
---|
12 | #include "nv/lib/sdl_image.hh"
|
---|
13 | #include "nv/gl/gl_enum.hh"
|
---|
14 | #include "nv/lib/gl.hh"
|
---|
15 |
|
---|
16 | using namespace nv;
|
---|
17 |
|
---|
18 | window* gl_device::create_window( uint16 width, uint16 height, bool fullscreen )
|
---|
19 | {
|
---|
20 | return new gl_window( this, width, height, fullscreen );
|
---|
21 | }
|
---|
22 |
|
---|
23 | window* nv::gl_device::adopt_window( void* sys_w_handle, void* sys_dc )
|
---|
24 | {
|
---|
25 | return new gl_window( this, sys_w_handle, sys_dc );
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | gl_device::gl_device()
|
---|
31 | {
|
---|
32 | nv::load_sdl_library();
|
---|
33 | m_info = NULL;
|
---|
34 |
|
---|
35 | if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 )
|
---|
36 | {
|
---|
37 | NV_LOG( LOG_CRITICAL, "Video initialization failed: " << SDL_GetError( ) );
|
---|
38 | return; // TODO: Error report
|
---|
39 | }
|
---|
40 |
|
---|
41 | #if NV_SDL_VERSION == NV_SDL_12
|
---|
42 | m_info = SDL_GetVideoInfo( );
|
---|
43 |
|
---|
44 | if ( !m_info )
|
---|
45 | {
|
---|
46 | NV_LOG( LOG_CRITICAL, "Video query failed: " << SDL_GetError( ) );
|
---|
47 | return; // TODO: Error report
|
---|
48 | }
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | program* gl_device::create_program( const string& vs_source, const string& fs_source )
|
---|
54 | {
|
---|
55 | return new gl_program( vs_source, fs_source );
|
---|
56 | }
|
---|
57 |
|
---|
58 | vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
59 | {
|
---|
60 | return new gl_vertex_buffer( hint, size, source );
|
---|
61 | }
|
---|
62 |
|
---|
63 | index_buffer* gl_device::create_index_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
64 | {
|
---|
65 | return new gl_index_buffer( hint, size, source );
|
---|
66 | }
|
---|
67 |
|
---|
68 | vertex_array* gl_device::create_vertex_array()
|
---|
69 | {
|
---|
70 | return new vertex_array();
|
---|
71 | }
|
---|
72 |
|
---|
73 | // this is a temporary function that will be removed once we find a way to
|
---|
74 | // pass binary file data around
|
---|
75 | image_data* nv::gl_device::create_image_data( const std::string& filename )
|
---|
76 | {
|
---|
77 | load_sdl_image_library();
|
---|
78 | SDL_Surface* image = IMG_Load( filename.c_str() );
|
---|
79 | if (!image)
|
---|
80 | {
|
---|
81 | NV_LOG( LOG_ERROR, "Image file " << filename.c_str() << " not found!" );
|
---|
82 | return nullptr;
|
---|
83 | }
|
---|
84 | // TODO: BGR vs RGB, single channel
|
---|
85 | assert( image->format->BytesPerPixel > 2 );
|
---|
86 | image_format format(image->format->BytesPerPixel == 3 ? RGB : RGBA, UBYTE );
|
---|
87 | image_data* data = new image_data( format, glm::ivec2( image->w, image->h ), (nv::uint8*)image->pixels );
|
---|
88 | return data;
|
---|
89 | }
|
---|
90 |
|
---|
91 | uint32 gl_device::get_ticks()
|
---|
92 | {
|
---|
93 | return SDL_GetTicks();
|
---|
94 | }
|
---|
95 |
|
---|
96 | void gl_device::delay( uint32 ms )
|
---|
97 | {
|
---|
98 | return SDL_Delay( ms );
|
---|
99 | }
|
---|
100 |
|
---|
101 | gl_device::~gl_device()
|
---|
102 | {
|
---|
103 | // TODO: better use release_texture
|
---|
104 | for ( auto& t : m_textures ) glDeleteTextures( 1, &t.glid );
|
---|
105 |
|
---|
106 | SDL_Quit();
|
---|
107 | }
|
---|
108 |
|
---|
109 | nv::texture nv::gl_device::create_texture( ivec2 size, image_format aformat, sampler asampler, void* data /*= nullptr */ )
|
---|
110 | {
|
---|
111 | unsigned glid = 0;
|
---|
112 | glGenTextures( 1, &glid );
|
---|
113 |
|
---|
114 | glBindTexture( GL_TEXTURE_2D, glid );
|
---|
115 |
|
---|
116 | // Detect if mipmapping was requested
|
---|
117 | if (( asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST ) ||
|
---|
118 | ( asampler.filter_max != sampler::LINEAR && asampler.filter_max != sampler::NEAREST ))
|
---|
119 | {
|
---|
120 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
121 | }
|
---|
122 |
|
---|
123 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_min ) );
|
---|
124 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_max ) );
|
---|
125 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)nv::sampler_wrap_to_enum( asampler.wrap_s) );
|
---|
126 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)nv::sampler_wrap_to_enum( asampler.wrap_t) );
|
---|
127 |
|
---|
128 | if (data)
|
---|
129 | {
|
---|
130 | 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 );
|
---|
131 | }
|
---|
132 |
|
---|
133 | glBindTexture( GL_TEXTURE_2D, 0 );
|
---|
134 |
|
---|
135 | texture result = m_textures.create();
|
---|
136 | gl_texture_info* info = m_textures.get( result );
|
---|
137 | info->format = aformat;
|
---|
138 | info->sampler = asampler;
|
---|
139 | info->size = size;
|
---|
140 | info->glid = glid;
|
---|
141 | return result;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void nv::gl_device::release_texture( texture t )
|
---|
145 | {
|
---|
146 | gl_texture_info* info = m_textures.get( t );
|
---|
147 | if ( info )
|
---|
148 | {
|
---|
149 | glDeleteTextures( 1, &(info->glid) );
|
---|
150 | m_textures.destroy( t );
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | const texture_info* nv::gl_device::get_texture_info( texture t )
|
---|
155 | {
|
---|
156 | return m_textures.get( t );
|
---|
157 | }
|
---|
158 |
|
---|