source: trunk/src/gl/gl_context.cc @ 172

Last change on this file since 172 was 171, checked in by epyon, 12 years ago
  • sdl - full 2.0 version implemented in the same header
  • sdl - nova fully runs on SDL 2.0 *also*
File size: 11.5 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_context.hh"
6
7#include "nv/gl/gl_enum.hh"
8#include "nv/lib/gl.hh"
9#include "nv/lib/sdl.hh"
10
11using namespace nv;
12
13void gl_context::clear( const clear_state& cs )
14{
15        // apply_framebuffer
16       
17        apply_scissor_test( cs.scissor_test );
18        apply_color_mask( cs.color_mask );
19        apply_depth_mask( cs.depth_mask );
20        // stencil_mask_separate
21
22        if ( m_clear_color != cs.color )
23        {
24                glClearColor( cs.color.r, cs.color.g, cs.color.b, cs.color.a );
25                m_clear_color = cs.color;
26        }
27
28        if ( m_clear_depth != cs.depth )
29        {
30                glClearDepth( cs.depth );
31                m_clear_depth = cs.depth;
32        }
33
34        if ( m_clear_stencil != cs.stencil )
35        {
36                glClearStencil( cs.stencil );
37                m_clear_stencil = cs.stencil;
38        }
39       
40        glClear( clear_state_buffers_to_mask( cs.buffers ) );
41}
42
43const ivec4& gl_context::get_viewport()
44{
45        return m_viewport;
46}
47
48void gl_context::set_viewport( const ivec4& viewport )
49{
50        if ( viewport.z < 0 || viewport.w < 0 )
51        {
52                NV_THROW( logic_error, "viewport width and height must be greater than zero!");
53        }
54
55        m_viewport = viewport;
56        glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
57}
58
59void gl_context::enable( unsigned int what, bool value )
60{
61        if ( value )
62        {
63                glEnable( what );
64        }
65        else
66        {
67                glDisable( what );
68        }
69}
70
71void gl_context::apply_stencil_test( const stencil_test& stencil )
72{
73        if ( m_render_state.stencil_test.enabled != stencil.enabled )
74        {
75                enable( GL_STENCIL_TEST, stencil.enabled );
76                m_render_state.stencil_test.enabled = stencil.enabled;
77        }
78
79        if ( stencil.enabled )
80        {
81                apply_stencil_face( GL_FRONT, m_render_state.stencil_test.front_face, stencil.front_face );
82                apply_stencil_face( GL_BACK,  m_render_state.stencil_test.back_face,  stencil.back_face );
83        }
84}
85
86void gl_context::apply_stencil_face( unsigned face, stencil_test_face& stencil, const stencil_test_face& new_stencil )
87{
88        if (( stencil.op_fail       != new_stencil.op_fail       ) ||
89                ( stencil.op_depth_fail != new_stencil.op_depth_fail ) ||
90                ( stencil.op_depth_pass != new_stencil.op_depth_pass ) )
91        {
92                glStencilOpSeparate( face,
93                        stencil_operation_to_enum( new_stencil.op_fail ),
94                        stencil_operation_to_enum( new_stencil.op_depth_fail ),
95                        stencil_operation_to_enum( new_stencil.op_depth_pass )
96                );
97
98                stencil.op_fail       = new_stencil.op_fail;
99                stencil.op_depth_fail = new_stencil.op_depth_fail;
100                stencil.op_depth_pass = new_stencil.op_depth_pass;
101        }
102
103        if (( stencil.function  != new_stencil.function ) ||
104                ( stencil.ref_value != new_stencil.ref_value ) ||
105                ( stencil.mask      != new_stencil.mask ))
106        {
107                glStencilFuncSeparate( face,
108                        stencil_function_to_enum( new_stencil.function ),
109                        new_stencil.ref_value,
110                        new_stencil.mask
111                );
112
113                stencil.function  = new_stencil.function;
114                stencil.ref_value = new_stencil.ref_value;
115                stencil.mask      = new_stencil.mask;
116        }
117}
118
119void gl_context::apply_scissor_test( const scissor_test& scissor )
120{
121        if ( m_render_state.scissor_test.enabled != scissor.enabled )
122        {
123                enable( GL_SCISSOR_TEST, scissor.enabled );
124                m_render_state.scissor_test.enabled = scissor.enabled;
125        }
126
127        if ( scissor.dim.x < 0 || scissor.dim.y < 0 )
128        {
129                NV_THROW( logic_error, "scissor_test.rect width and height must be greater than zero!" );
130        }
131
132        if ( scissor.enabled )
133        {
134                if ( m_render_state.scissor_test.dim != scissor.dim || m_render_state.scissor_test.pos != scissor.pos )
135                {
136                        glScissor(
137                                scissor.pos.x, scissor.pos.y,
138                                scissor.dim.x, scissor.dim.y
139                        );
140                        m_render_state.scissor_test.dim = scissor.dim;
141                        m_render_state.scissor_test.pos = scissor.pos;
142                }
143        }
144}
145
146void gl_context::apply_depth_test( const depth_test& depth )
147{
148        if ( m_render_state.depth_test.enabled != depth.enabled )
149        {
150                enable( GL_DEPTH_TEST, depth.enabled );
151                m_render_state.depth_test.enabled = depth.enabled;
152        }
153
154        if ( depth.enabled )
155        {
156                if ( m_render_state.depth_test.function != depth.function )
157                {
158                        glDepthFunc( depth_state_function_to_enum( depth.function ) );
159                        m_render_state.depth_test.function = depth.function;
160                }
161        }
162}
163
164void gl_context::apply_depth_mask( bool mask )
165{
166        if ( m_render_state.depth_mask != mask )
167        {
168                glDepthMask( mask );
169                m_render_state.depth_mask = mask;
170        }
171}
172
173void gl_context::apply_depth_range( const depth_range& range )
174{
175        if ( range.near < 0.0 || range.near > 1.0 )
176        {
177                NV_THROW( logic_error, "render_state.depth_range.near must be between zero and one!");
178        }
179        if ( range.far < 0.0 || range.far > 1.0 )
180        {
181                NV_THROW( logic_error, "render_state.depth_range.far must be between zero and one!");
182        }
183
184        if ((m_render_state.depth_range.far  != range.far) ||
185                (m_render_state.depth_range.near != range.near))
186        {
187                glDepthRange( range.near, range.far );
188
189                m_render_state.depth_range.far  = range.far;
190                m_render_state.depth_range.near = range.near;
191        }
192}
193
194void gl_context::apply_color_mask( const color_mask& mask )
195{
196        if ( m_render_state.color_mask != mask )
197        {
198                glColorMask( mask.red, mask.green, mask.blue, mask.alpha );
199                m_render_state.color_mask = mask;
200        }
201}
202
203void gl_context::apply_blending( const blending& blend )
204{
205        if ( m_render_state.blending.enabled != blend.enabled )
206        {
207                enable( GL_BLEND, blend.enabled );
208                m_render_state.blending.enabled = blend.enabled;
209        }
210
211        if ( blend.enabled )
212        {
213                if ((m_render_state.blending.src_rgb_factor   != blend.src_rgb_factor ) ||
214                        (m_render_state.blending.dst_rgb_factor   != blend.dst_rgb_factor ) ||
215                        (m_render_state.blending.src_alpha_factor != blend.src_alpha_factor ) ||
216                        (m_render_state.blending.dst_alpha_factor != blend.dst_alpha_factor ))
217                {
218                        glBlendFuncSeparate(
219                                blending_factor_to_enum( blend.src_rgb_factor ),
220                                blending_factor_to_enum( blend.dst_rgb_factor ),
221                                blending_factor_to_enum( blend.src_alpha_factor ),
222                                blending_factor_to_enum( blend.dst_alpha_factor )
223                        );
224
225                        m_render_state.blending.src_rgb_factor   = blend.src_rgb_factor;
226                        m_render_state.blending.dst_rgb_factor   = blend.dst_rgb_factor;
227                        m_render_state.blending.src_alpha_factor = blend.src_alpha_factor;
228                        m_render_state.blending.dst_alpha_factor = blend.dst_alpha_factor;
229                }
230
231                if ((m_render_state.blending.rgb_equation   != blend.rgb_equation ) ||
232                        (m_render_state.blending.alpha_equation != blend.alpha_equation ))
233                {
234                        glBlendEquationSeparate(
235                                blending_equation_to_enum( blend.rgb_equation ),
236                                blending_equation_to_enum( blend.alpha_equation )
237                        );
238
239                        m_render_state.blending.rgb_equation   = blend.rgb_equation;
240                        m_render_state.blending.alpha_equation = blend.alpha_equation;
241                }
242
243                if (( m_render_state.blending.color != blend.color ))
244                {
245                        glBlendColor( blend.color.r, blend.color.g, blend.color.b, blend.color.a );
246                        m_render_state.blending.color = blend.color;
247                }
248        }
249}
250
251
252void gl_context::apply_culling( const culling& cull )
253{
254        if ( m_render_state.culling.enabled != cull.enabled )
255        {
256                enable( GL_CULL_FACE, cull.enabled );
257                m_render_state.culling.enabled = cull.enabled;
258        }
259
260        if ( cull.enabled )
261        {
262                if ( m_render_state.culling.face != cull.face )
263                {
264                        glCullFace( culling_face_type_to_enum(cull.face) );
265                        m_render_state.culling.face = cull.face;
266                }
267
268                if ( m_render_state.culling.order != cull.order )
269                {
270                        glFrontFace( culling_order_type_to_enum( cull.order ) );
271                        m_render_state.culling.order = cull.order;
272                }
273        }
274}
275
276
277void gl_context::force_apply_render_state( const render_state& state )
278{
279        enable( GL_CULL_FACE, state.culling.enabled );
280        glCullFace( culling_face_type_to_enum( state.culling.face ) );
281        glFrontFace( culling_order_type_to_enum( state.culling.order ) );
282
283        enable( GL_SCISSOR_TEST, state.scissor_test.enabled );
284        glScissor(
285                state.scissor_test.pos.x, state.scissor_test.pos.y,
286                state.scissor_test.dim.x, state.scissor_test.dim.y
287        );
288
289        enable( GL_STENCIL_TEST, state.stencil_test.enabled );
290        force_apply_stencil_face( GL_FRONT, state.stencil_test.front_face );
291        force_apply_stencil_face( GL_BACK,  state.stencil_test.back_face  );
292
293        enable( GL_DEPTH_TEST, state.depth_test.enabled );
294        glDepthFunc( depth_state_function_to_enum( state.depth_test.function ) );
295        glDepthRange( state.depth_range.near, state.depth_range.far );
296
297        enable( GL_BLEND, state.blending.enabled );
298        glBlendFuncSeparate(
299                blending_factor_to_enum( state.blending.src_rgb_factor ),
300                blending_factor_to_enum( state.blending.dst_rgb_factor ),
301                blending_factor_to_enum( state.blending.src_alpha_factor ),
302                blending_factor_to_enum( state.blending.dst_alpha_factor )
303        );
304        glBlendEquationSeparate(
305                blending_equation_to_enum( state.blending.rgb_equation ),
306                blending_equation_to_enum( state.blending.alpha_equation )
307        );
308        glBlendColor(
309                state.blending.color.r, state.blending.color.g,
310                state.blending.color.b, state.blending.color.a
311        );
312
313        glDepthMask( state.depth_mask );
314        glColorMask(
315                state.color_mask.red, state.color_mask.green,
316                state.color_mask.blue, state.color_mask.alpha
317        );
318}
319
320void gl_context::force_apply_stencil_face( unsigned face, const stencil_test_face& stencil )
321{
322        glStencilOpSeparate( face,
323                stencil_operation_to_enum( stencil.op_fail ),
324                stencil_operation_to_enum( stencil.op_depth_fail ),
325                stencil_operation_to_enum( stencil.op_depth_pass )
326        );
327
328        glStencilFuncSeparate( face,
329                stencil_function_to_enum( stencil.function ),
330                stencil.ref_value,
331                stencil.mask
332        );
333}
334
335
336void gl_context::apply_render_state( const render_state& state )
337{
338        // apply_primitive_restart
339        apply_culling( state.culling );
340        // apply_program_point_size
341        // apply_rasterization_mode
342        apply_scissor_test( state.scissor_test );
343        apply_stencil_test( state.stencil_test );
344        apply_depth_test( state.depth_test );
345        apply_depth_range( state.depth_range );
346        apply_blending( state.blending );
347        apply_color_mask( state.color_mask );
348        apply_depth_mask( state.depth_mask );
349}
350
351
352gl_context::gl_context( device* a_device, void* a_win_handle )
353        : context( a_device ), m_handle( nullptr )
354{
355#if NV_SDL_VERSION == NV_SDL_20
356        m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_win_handle ) );
357
358        if ( m_handle == 0 )
359        {
360                NV_LOG( LOG_CRITICAL, "GL Context creation failed: " << SDL_GetError( ) );
361                return; // TODO: Error report
362        }
363#else
364        NV_UNUSED( a_win_handle );
365#endif
366
367        nv::load_gl_library();
368        NV_LOG( LOG_INFO, "OpenGL Vendor       : " << glGetString(GL_VENDOR) );
369        NV_LOG( LOG_INFO, "OpenGL Renderer     : " << glGetString(GL_RENDERER) );
370        NV_LOG( LOG_INFO, "OpenGL Version      : " << glGetString(GL_VERSION) );
371        NV_LOG( LOG_INFO, "OpenGL GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) );
372#if NV_SDL_VERSION == NV_SDL_20
373//      SDL_GL_SetSwapInterval(1);
374#endif
375
376        // TODO: do we really need this?
377        glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
378
379        force_apply_render_state( m_render_state );
380}
381
382
383nv::gl_context::~gl_context()
384{
385#if NV_SDL_VERSION == NV_SDL_20
386        SDL_GL_DeleteContext(static_cast<SDL_GLContext>( m_handle ) );
387#endif
388}
389
390
391void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
392{
393        apply_render_state( rs );
394        if ( count > 0 )
395        {
396                p->bind();
397                va->bind();
398                if ( va->has_index_buffer() )
399                {
400                        glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
401                }
402                else
403                {
404                        glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
405                }
406                va->unbind();
407                p->unbind();
408        }
409}
Note: See TracBrowser for help on using the repository browser.