Changeset 44 for trunk


Ignore:
Timestamp:
05/28/13 16:48:29 (12 years ago)
Author:
epyon
Message:
  • context bugfixes, force apply state at creation and apply render state
  • window creates context
  • index buffer
  • vertex arrays (simulation of GL 3 functionality)
  • bugfixes
Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/gl/gl_context.hh

    r37 r44  
    2020        {
    2121        public:
    22                 void clear( const clear_state& cs );
    23                 const ivec4& get_viewport();
    24                 void set_viewport( const ivec4& viewport );
    25 
     22                gl_context();
     23                virtual void clear( const clear_state& cs );
     24                virtual const ivec4& get_viewport();
     25                virtual void set_viewport( const ivec4& viewport );
     26                virtual void apply_render_state( const render_state& state );
    2627        private:
    2728                void force_apply_render_state( const render_state& state );
    2829                void force_apply_stencil_face( int face, const stencil_test_face& stencil );
    29                 void apply_render_state( const render_state& state );
    3030                void apply_stencil_test( const stencil_test& stencil );
    3131                void apply_stencil_face( int face, stencil_test_face& stencil, const stencil_test_face& new_stencil );
  • trunk/nv/gl/gl_device.hh

    r42 r44  
    2424                virtual program* create_program( const string& vs_source, const string& fs_source );
    2525                virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, int size, void* source = nullptr );
     26                virtual index_buffer* create_index_buffer( buffer_hint hint, int size, void* source = nullptr );
     27                virtual vertex_array* create_vertex_array();
    2628                virtual ~gl_device();
    2729        private:
  • trunk/nv/gl/gl_vertex_buffer.hh

    r42 r44  
    3131        };
    3232
     33        class gl_index_buffer : public index_buffer
     34        {
     35        public:
     36                gl_index_buffer( buffer_hint hint, int size, void* data = nullptr );
     37                virtual void assign( void* data );
     38                virtual void bind();
     39                virtual void unbind();
     40                virtual bool is_valid() const;
     41        private:
     42                gl_buffer_name m_name;
     43        };
     44
     45        class gl_vertex_array : public vertex_array
     46        {
     47        public:
     48                gl_vertex_array();
     49                virtual void bind();
     50                virtual void unbind();
     51        };
     52
    3353} // namespace nv
    3454
  • trunk/nv/gl/gl_window.hh

    r38 r44  
    1414
    1515#include <nv/interface/window.hh>
     16#include <nv/gl/gl_context.hh>
    1617
    1718namespace nv
     
    2627                string get_title() const;
    2728                void set_title( const string& title );
     29                virtual context* get_context() { return m_context; }
     30                ~gl_window();
    2831        private:
    29                 uint16 m_width;
    30                 uint16 m_height;
    31                 string m_title;
    32                 void*  m_screen;
     32                uint16      m_width;
     33                uint16      m_height;
     34                string      m_title;
     35                void*       m_screen;
     36                gl_context* m_context;
    3337        };
    3438
  • trunk/nv/interface/context.hh

    r32 r44  
    2323        public:
    2424                virtual void clear( const clear_state& cs ) = 0;
     25                virtual void apply_render_state( const render_state& state ) = 0;
     26                virtual const ivec4& get_viewport() = 0;
     27                virtual void set_viewport( const ivec4& viewport ) = 0;
    2528        protected:
    2629                clear_state  m_clear_state;
  • trunk/nv/interface/device.hh

    r42 r44  
    2828                virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
    2929                virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, int size, void* source = nullptr ) = 0;
     30                virtual index_buffer* create_index_buffer( buffer_hint hint, int size, void* source = nullptr ) = 0;
     31                virtual vertex_array* create_vertex_array() = 0;
    3032        };
    3133
  • trunk/nv/interface/vertex_buffer.hh

    r42 r44  
    1414
    1515#include <nv/common.hh>
     16#include <nv/types.hh>
     17#include <unordered_map>
     18#include <string>
    1619
    1720namespace nv
     
    2528        };
    2629
    27         class vertex_buffer
     30        class buffer
    2831        {
    2932        public:
    30                 vertex_buffer( buffer_hint hint, int size ) { m_size = size; m_hint = hint; }
     33                buffer( buffer_hint hint, int size ) { m_size = size; m_hint = hint; }
    3134                virtual void assign( void* data ) = 0;
    3235                virtual void bind() = 0;
     
    4043        };
    4144
     45        class vertex_buffer : public buffer
     46        {
     47        public:
     48                vertex_buffer( buffer_hint hint, int size ) : buffer( hint, size ) {}
     49        };
     50
     51        class index_buffer  : public buffer
     52        {
     53        public:
     54                index_buffer( buffer_hint hint, int size ) : buffer( hint, size ) {}
     55        };
     56
     57        class vertex_buffer_attribute
     58        {
     59        public:
     60                vertex_buffer_attribute( vertex_buffer* buffer, type datatype, int components, int offset = 0, int stride = 0, bool owner = true )
     61                        : m_buffer( buffer ), m_datatype( datatype ), m_components( components ), m_offset( offset ), m_stride( stride ), m_owner( owner ) {}
     62
     63                vertex_buffer* get_buffer() const { return m_buffer; }
     64                type get_datatype() const { return m_datatype; }
     65                int get_components() const { return m_components; }
     66                int get_offset() const { return m_offset; }
     67                int get_stride() const { return m_stride; }
     68
     69                ~vertex_buffer_attribute()
     70                {
     71                        if (m_owner)
     72                        {
     73                                delete m_buffer;
     74                        }
     75                }
     76        protected:
     77                vertex_buffer* m_buffer;
     78                type           m_datatype;
     79                int  m_components;
     80                int  m_offset;
     81                int  m_stride;
     82                bool m_owner;
     83        };
     84
     85        typedef std::unordered_map< int, vertex_buffer_attribute* > vertex_buffer_attribute_map;
     86
     87        class vertex_array
     88        {
     89        public:
     90                vertex_array() : m_map(), m_index( nullptr ) {}
     91                void add_vertex_buffer( int location, vertex_buffer* buffer, type datatype, int components, int offset = 0, int stride = 0, bool owner = true )
     92                {
     93                        m_map[ location ] = new vertex_buffer_attribute( buffer, datatype, components, offset, stride, owner );
     94                };
     95                void set_index_buffer( index_buffer* buffer ) { m_index = buffer; }
     96                virtual void bind() = 0;
     97                virtual void unbind() = 0;
     98                ~vertex_array() {
     99                        for ( vertex_buffer_attribute_map::iterator i = m_map.begin();  i != m_map.end(); ++i )
     100                        {
     101                                delete i->second;
     102                        }
     103                        if (m_index) delete m_index;
     104                }
     105        protected:
     106                vertex_buffer_attribute_map m_map;
     107                index_buffer* m_index;
     108        };
     109
     110
    42111} // namespace nv
    43112
  • trunk/nv/interface/window.hh

    r32 r44  
    1818namespace nv
    1919{
    20 
     20        class context;
    2121        class window
    2222        {
     
    2626                virtual string get_title() const = 0;
    2727                virtual void set_title( const string& title ) = 0;
     28                virtual context* get_context() = 0;
    2829        };
    2930
  • trunk/nv/types.hh

    r37 r44  
    1616        enum type
    1717        {
     18                INT,
     19                BYTE,
     20                SHORT,
     21                UINT,
     22                UBYTE,
     23                USHORT,
    1824                FLOAT,
    1925                FLOAT_VECTOR_2,
     
    2329                FLOAT_MATRIX_3,
    2430                FLOAT_MATRIX_4,
    25                 INT,
    2631                INT_VECTOR_2,
    2732                INT_VECTOR_3,
     
    4348        template < type EnumType > struct enum_to_type {};
    4449
     50        template <> struct enum_to_type< INT >   { typedef int type; };
     51        template <> struct enum_to_type< UINT >  { typedef unsigned int type; };
     52        template <> struct enum_to_type< SHORT > { typedef short type; };
     53        template <> struct enum_to_type< USHORT >{ typedef unsigned short type; };
     54        template <> struct enum_to_type< BYTE >  { typedef char type; };
     55        template <> struct enum_to_type< UBYTE > { typedef unsigned char type; };
    4556        template <> struct enum_to_type< FLOAT > { typedef f32 type; };
    46         template <> struct enum_to_type< INT >   { typedef int type; };
    4757
    4858        template <> struct enum_to_type< FLOAT_VECTOR_2 > { typedef vec2 type; };
     
    6070        template < typename Type > struct type_to_enum {};
    6171
     72        template <> struct type_to_enum< int >           { static const type type = INT; };
     73        template <> struct type_to_enum< unsigned int >  { static const type type = UINT; };
     74        template <> struct type_to_enum< short >         { static const type type = SHORT; };
     75        template <> struct type_to_enum< unsigned short >{ static const type type = USHORT; };
     76        template <> struct type_to_enum< char >          { static const type type = BYTE; };
     77        template <> struct type_to_enum< unsigned char > { static const type type = UBYTE; };
    6278        template <> struct type_to_enum< f32 > { static const type type = FLOAT; };
    63         template <> struct type_to_enum< int > { static const type type = INT; };
    6479
    6580        template <> struct type_to_enum< vec2 > { static const type type = FLOAT_VECTOR_2; };
  • trunk/src/gl/gl_context.cc

    r37 r44  
    1313{
    1414        // apply_framebuffer
     15       
    1516        apply_scissor_test( cs.scissor_test );
    1617        apply_color_mask( cs.color_mask );
     
    3536                m_clear_stencil = cs.stencil;
    3637        }
    37 
    38         glClear( cs.buffers );
     38       
     39        glClear( clear_state_buffers_to_mask( cs.buffers ) );
    3940}
    4041
     
    347348}
    348349
     350
     351gl_context::gl_context()
     352{
     353        force_apply_render_state( m_render_state );
     354}
  • trunk/src/gl/gl_device.cc

    r42 r44  
    5656}
    5757
     58index_buffer* gl_device::create_index_buffer( buffer_hint hint, int size, void* source /*= nullptr */ )
     59{
     60        return new gl_index_buffer( hint, size, source );
     61}
     62
     63vertex_array* gl_device::create_vertex_array()
     64{
     65        return new gl_vertex_array();
     66}
     67
    5868gl_device::~gl_device()
    5969{
  • trunk/src/gl/gl_enum.cc

    r43 r44  
    186186        switch( type )
    187187        {
     188        case BYTE           : return GL_BYTE;
     189        case UBYTE          : return GL_UNSIGNED_BYTE;
     190        case SHORT          : return GL_SHORT;
     191        case USHORT         : return GL_UNSIGNED_SHORT;
     192        case INT            : return GL_INT;
     193        case UINT               : return GL_UNSIGNED_INT;
    188194        case FLOAT          : return GL_FLOAT;
    189195        case FLOAT_VECTOR_2 : return GL_FLOAT_VEC2;
     
    193199        case FLOAT_MATRIX_3 : return GL_FLOAT_MAT3;
    194200        case FLOAT_MATRIX_4 : return GL_FLOAT_MAT4;
    195         case INT            : return GL_INT;
    196201        case INT_VECTOR_2   : return GL_INT_VEC2;
    197202        case INT_VECTOR_3   : return GL_INT_VEC3;
     
    205210        switch( gl_enum )
    206211        {
    207         case GL_FLOAT      : return FLOAT;
    208         case GL_FLOAT_VEC2 : return FLOAT_VECTOR_2;
    209         case GL_FLOAT_VEC3 : return FLOAT_VECTOR_3;
    210         case GL_FLOAT_VEC4 : return FLOAT_VECTOR_4;
    211         case GL_FLOAT_MAT2 : return FLOAT_MATRIX_2;
    212         case GL_FLOAT_MAT3 : return FLOAT_MATRIX_3;
    213         case GL_FLOAT_MAT4 : return FLOAT_MATRIX_4;
    214         case GL_INT        : return INT;
    215         case GL_INT_VEC2   : return INT_VECTOR_2;
    216         case GL_INT_VEC3   : return INT_VECTOR_3;
    217         case GL_INT_VEC4   : return INT_VECTOR_4;
     212        case GL_BYTE           : return BYTE;
     213        case GL_UNSIGNED_BYTE  : return UBYTE;
     214        case GL_SHORT          : return SHORT;
     215        case GL_UNSIGNED_SHORT : return USHORT;
     216        case GL_INT            : return INT;
     217        case GL_UNSIGNED_INT   : return UINT;
     218        case GL_FLOAT          : return FLOAT;
     219        case GL_FLOAT_VEC2     : return FLOAT_VECTOR_2;
     220        case GL_FLOAT_VEC3     : return FLOAT_VECTOR_3;
     221        case GL_FLOAT_VEC4     : return FLOAT_VECTOR_4;
     222        case GL_FLOAT_MAT2     : return FLOAT_MATRIX_2;
     223        case GL_FLOAT_MAT3     : return FLOAT_MATRIX_3;
     224        case GL_FLOAT_MAT4     : return FLOAT_MATRIX_4;
     225        case GL_INT_VEC2       : return INT_VECTOR_2;
     226        case GL_INT_VEC3       : return INT_VECTOR_3;
     227        case GL_INT_VEC4       : return INT_VECTOR_4;
    218228        default : return type(0); // TODO: throw!
    219229        }
  • trunk/src/gl/gl_vertex_buffer.cc

    r42 r44  
    4040        return m_name.is_valid();
    4141}
     42
     43gl_index_buffer::gl_index_buffer( buffer_hint hint, int size, void* data )
     44        : index_buffer( hint, size ), m_name()
     45{
     46        if (data)
     47        {
     48                assign( data );
     49        }
     50}
     51
     52void gl_index_buffer::assign( void* data )
     53{
     54        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() );
     55        glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_size, data, buffer_hint_to_enum( m_hint ) );
     56        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0);
     57}
     58
     59void gl_index_buffer::bind()
     60{
     61        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_name.get_value() );
     62}
     63
     64void gl_index_buffer::unbind()
     65{
     66        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
     67}
     68
     69bool gl_index_buffer::is_valid() const
     70{
     71        return m_name.is_valid();
     72}
     73
     74gl_vertex_array::gl_vertex_array()
     75{
     76
     77}
     78
     79void gl_vertex_array::bind()
     80{
     81        for ( vertex_buffer_attribute_map::iterator i = m_map.begin();  i != m_map.end(); ++i )
     82        {
     83                int location                = i->first;
     84                vertex_buffer_attribute* va = i->second;
     85                vertex_buffer*           vb = va->get_buffer();
     86                glEnableVertexAttribArray( location );
     87                vb->bind();
     88                glVertexAttribPointer(
     89                        location,
     90                        va->get_components(),
     91                        nv::type_to_gl_enum( va->get_datatype() ),
     92                        GL_FALSE,
     93                        va->get_stride(),
     94                        (void*)va->get_offset()
     95                        );
     96                vb->unbind();
     97        }
     98
     99}
     100
     101void gl_vertex_array::unbind()
     102{
     103        for ( vertex_buffer_attribute_map::iterator i = m_map.begin();  i != m_map.end(); ++i )
     104        {
     105                glDisableVertexAttribArray( i->first );
     106        }
     107}
  • trunk/src/gl/gl_window.cc

    r38 r44  
    2929        NV_LOG( LOG_INFO, "OpenGL Version      : " << glGetString(GL_VERSION) );
    3030        NV_LOG( LOG_INFO, "OpenGL GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) );
     31
     32        m_context = new gl_context();
     33        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
    3134}
    3235
     
    5154        m_title = title;
    5255}
     56
     57gl_window::~gl_window()
     58{
     59        delete m_context;
     60}
Note: See TracChangeset for help on using the changeset viewer.