Changeset 235


Ignore:
Timestamp:
05/12/14 16:52:16 (11 years ago)
Author:
epyon
Message:
  • camera class extension
  • additional built-in uniforms added
  • support for uniform arrays
  • BONEINDEX and BONEWEIGHT built in attributes added
  • i16vec's added to math
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/interface/camera.hh

    r232 r235  
    2525                void set_lookat( const vec3& eye, const vec3& focus, const vec3& up )
    2626                {
    27                         m_view  = glm::lookAt( eye, focus, up );
     27                        m_view      = glm::lookAt( eye, focus, up );
     28                        m_position  = eye;
     29                        m_direction = glm::normalize( focus - eye );
    2830                }
    2931
     
    4042                        return m_view;
    4143                }
     44                const vec3& get_position() const
     45                {
     46                        return m_position;
     47                }
     48                const vec3& get_direction() const
     49                {
     50                        return m_direction;
     51                }
    4252        private:
    4353                bool m_dirty;
     54                vec3 m_position;
     55                vec3 m_direction;
    4456                mat4 m_projection;
    4557                mat4 m_view;
     
    6274
    6375                mat4 get_view_inv()   const { return glm::inverse( get_view() ); }
     76                mat4 get_model_inv()  const { return glm::inverse( get_model() ); }
    6477                mat3 get_normal()     const { return glm::transpose(glm::inverse(glm::mat3( get_modelview() ) ) ); }
    6578        protected:
  • trunk/nv/interface/program.hh

    r232 r235  
    2727        enum slot
    2828        {
    29                 POSITION = 0,
    30                 TEXCOORD = 1,
    31                 NORMAL   = 2,
    32                 COLOR    = 3,
    33                 TANGENT  = 4,
     29                POSITION   = 0,
     30                TEXCOORD   = 1,
     31                NORMAL     = 2,
     32                TANGENT    = 3,
     33                BONEINDEX  = 4,
     34                BONEWEIGHT = 5,
     35                COLOR      = 6,
    3436        };
    3537
     
    9597                }
    9698
    97                 attribute* get_attribute( const string& name ) const
     99                int try_get_attribute_location( const string& name ) const
    98100                {
    99101                        attribute_map::const_iterator i = m_attribute_map.find( name );
    100102                        if ( i != m_attribute_map.end() )
    101103                        {
     104                                return i->second->get_location();
     105                        }
     106                        return -1;
     107                }
     108
     109                attribute* get_attribute( const string& name ) const
     110                {
     111                        attribute_map::const_iterator i = m_attribute_map.find( name );
     112                        if ( i != m_attribute_map.end() )
     113                        {
    102114                                return i->second;
    103115                        }
     
    128140
    129141                template < typename T >
     142                void set_uniform_array( const string& name, const T* value, uint32 count )
     143                {
     144                        uniform_base* base = get_uniform( name );
     145                        // restore typechecking, but remember to accept int for float!
     146                        // if ( /* base->get_type() != type_to_enum<T>::type */ )
     147                        // {
     148                        //              NV_LOG( LOG_ERROR, "Uniform '" << name << "' not found in program!" );
     149                        //              return;
     150                        // }
     151                        // TODO: nicer check
     152                        NV_ASSERT( count <= base->get_length(), "LENGTH CHECK FAIL" );
     153                        ((uniform<T>*)( base ))->set_value( value, count );
     154                }
     155
     156                template < typename T >
     157                void set_uniform_array( const string& name, const std::vector<T>& value )
     158                {
     159                        uniform_base* base = get_uniform( name );
     160                        // restore typechecking, but remember to accept int for float!
     161                        // if ( /* base->get_type() != type_to_enum<T>::type */ )
     162                        // {
     163                        //              NV_LOG( LOG_ERROR, "Uniform '" << name << "' not found in program!" );
     164                        //              return;
     165                        // }
     166                        // TODO: nicer check
     167                        NV_ASSERT( (int)value.size() <= base->get_length(), "LENGTH CHECK FAIL" );
     168                        ((uniform<T>*)( base ))->set_value( value.data(), value.size() );
     169                }
     170
     171                template < typename T >
     172                void set_opt_uniform_array( const string& name, const T* value, uint32 count )
     173                {
     174                        uniform_base* base = get_uniform( name );
     175                        if (!base) return;
     176                        // restore typechecking, but remember to accept int for float!
     177                        // if ( /* base->get_type() != type_to_enum<T>::type */ )
     178                        // {
     179                        //              NV_LOG( LOG_ERROR, "Uniform '" << name << "' not found in program!" );
     180                        //              return;
     181                        // }
     182                        // TODO: nicer check
     183                        NV_ASSERT( (int)count <= base->get_length(), "LENGTH CHECK FAIL" );
     184                        ((uniform<T>*)( base ))->set_value( value, count );
     185                }
     186
     187                template < typename T >
     188                void set_opt_uniform_array( const string& name, const std::vector<T>& value )
     189                {
     190                        uniform_base* base = try_get_uniform( name );
     191                        if (!base) return;
     192                        // restore typechecking, but remember to accept int for float!
     193                        // if ( /* base->get_type() != type_to_enum<T>::type */ )
     194                        // {
     195                        //              NV_LOG( LOG_ERROR, "Uniform '" << name << "' not found in program!" );
     196                        //              return;
     197                        // }
     198                        // TODO: nicer check
     199                        NV_ASSERT( (int)value.size() <= base->get_length(), "LENGTH CHECK FAIL" );
     200                        ((uniform<T>*)( base ))->set_value( value.data(), value.size() );
     201                }
     202
     203
     204                template < typename T >
    130205                void set_uniform( const string& name, const T& value )
    131206                {
    132                         uniform_base* base = get_uniform( name );
     207                        uniform_base* base = try_get_uniform( name );
    133208                        // restore typechecking, but remember to accept int for float!
    134209                        // if ( /* base->get_type() != type_to_enum<T>::type */ )
     
    209284                        factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
    210285                        factory_map[ "nv_m_view_inv" ]   = new engine_uniform_factory< engine_uniform_m_view_inv >();
    211                         factory_map[ "nv_m_view" ]       = new engine_uniform_factory< engine_uniform_m_view >();
    212286                        factory_map[ "nv_m_model" ]      = new engine_uniform_factory< engine_uniform_m_model >();
     287                        factory_map[ "nv_m_model_inv" ]  = new engine_uniform_factory< engine_uniform_m_model_inv >();
    213288                        factory_map[ "nv_m_modelview" ]  = new engine_uniform_factory< engine_uniform_m_modelview >();
    214289                        factory_map[ "nv_m_projection" ] = new engine_uniform_factory< engine_uniform_m_projection >();
    215290                        factory_map[ "nv_m_normal" ]     = new engine_uniform_factory< engine_uniform_m_normal >();
    216291                        factory_map[ "nv_m_mvp" ]        = new engine_uniform_factory< engine_uniform_m_mvp >();
     292                        factory_map[ "nv_v_camera_position" ]  = new engine_uniform_factory< engine_uniform_v_camera_position >();
     293                        factory_map[ "nv_v_camera_direction" ] = new engine_uniform_factory< engine_uniform_v_camera_direction >();
    217294
    218295                        engine_link_uniform_factory_map& factory_link_map = get_link_uniform_factory();
  • trunk/nv/interface/uniform.hh

    r232 r235  
    3232                bool is_dirty() const { return m_dirty; }
    3333                void clean() { m_dirty = false; }
     34                virtual ~uniform_base() {}
    3435        protected:
    3536                string   m_name;
     
    4748
    4849                uniform( const string& name, int location, int length )
    49                         : uniform_base( name, type_to_enum< T >::type, location, length ), m_value()
    50                 {}
     50                        : uniform_base( name, type_to_enum< T >::type, location, length ), m_value( nullptr )
     51                {
     52                        m_value = new T[ m_length ];
     53                }
    5154
    5255                void set_value( const T& value )
    5356                {
    54                         if ( value != m_value )
     57                        NV_ASSERT( m_length == 1, "set_value on array uniform!" );
     58                        if ( value != m_value[0] )
    5559                        {
    56                                 m_value = value;
     60                                m_value[0] = value;
    5761                                m_dirty = true;
    5862                        }
    5963                }
    6064
    61                 const T& get_value() { return m_value; }
     65                void set_value( const T* value, uint32 count )
     66                {
     67                        // TODO: memcmp?
     68                        std::copy( value, value + count, m_value );
     69                        m_dirty = true;
     70                }
     71
     72                const T* get_value() { return m_value; }
     73                virtual ~uniform()
     74                {
     75                        delete[] m_value;
     76                }
    6277        protected:
    63                 T m_value;
     78                T* m_value;
    6479        };
    6580
     
    145160        };
    146161
     162        class engine_uniform_m_model_inv : public engine_uniform< mat4 >
     163        {
     164        public:
     165                engine_uniform_m_model_inv( uniform_base* u ) : engine_uniform( u ) {}
     166                virtual void set( const context* , const scene_state* s ) { m_uniform->set_value( s->get_model_inv() ); }
     167        };
     168
    147169        class engine_uniform_m_modelview : public engine_uniform< mat4 >
    148170        {
     
    173195        };
    174196
     197        class engine_uniform_v_camera_position : public engine_uniform< vec3 >
     198        {
     199        public:
     200                engine_uniform_v_camera_position( uniform_base* u ) : engine_uniform( u ) {}
     201                virtual void set( const context* , const scene_state* s ) { m_uniform->set_value( s->get_camera().get_position() ); }
     202        };
     203
     204        class engine_uniform_v_camera_direction : public engine_uniform< vec3 >
     205        {
     206        public:
     207                engine_uniform_v_camera_direction( uniform_base* u ) : engine_uniform( u ) {}
     208                virtual void set( const context* , const scene_state* s ) { m_uniform->set_value( s->get_camera().get_direction() ); }
     209        };
     210
    175211        template< int VALUE >
    176212        class engine_link_uniform_int : public engine_link_uniform< int >
  • trunk/nv/math.hh

    r229 r235  
    2727        typedef glm::detail::tvec3<sint8> i8vec3;
    2828        typedef glm::detail::tvec4<sint8> i8vec4;
     29
     30        typedef glm::detail::tvec2<sint16> i16vec2;
     31        typedef glm::detail::tvec3<sint16> i16vec3;
     32        typedef glm::detail::tvec4<sint16> i16vec4;
    2933
    3034        typedef glm::vec2 vec2;
  • trunk/src/gl/gl_program.cc

    r232 r235  
    106106        if (!fragment_shader.compile( fragment_program )) { return false; }
    107107
    108         glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::POSITION ), "nv_position" );
    109         glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::TEXCOORD ), "nv_texcoord" );
    110         glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::NORMAL   ), "nv_normal"   );
    111         glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::COLOR    ), "nv_color"    );
    112         glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::TANGENT  ), "nv_tangent"  );
     108        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::POSITION   ), "nv_position"  );
     109        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::TEXCOORD   ), "nv_texcoord"  );
     110        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::NORMAL     ), "nv_normal"    );
     111        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::COLOR      ), "nv_color"     );
     112        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::TANGENT    ), "nv_tangent"   );
     113        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::BONEINDEX  ), "nv_boneindex" );
     114        glBindAttribLocation( m_name.get_value(), static_cast<GLuint>( slot::BONEWEIGHT ), "nv_boneweight");
    113115
    114116        glAttachShader( m_name.get_value(), fragment_shader.get_id() );
     
    181183
    182184                string name( name_buffer, size_t(uni_nlen) );
    183 
     185               
    184186                // skip built-ins
    185187                if ( name.substr(0,3) == "gl_" ) continue;
     
    187189                int uni_loc = glGetUniformLocation( m_name.get_value(), name.c_str() );
    188190                datatype utype = gl_enum_to_datatype( uni_type );
     191               
     192                // check for array
     193                string::size_type arrchar = name.find('[');
     194                if ( arrchar != string::npos )
     195                {
     196                        name = name.substr( 0, arrchar );
     197                }
     198
    189199                m_uniform_map[ name ] = create_uniform( utype, name, uni_loc, uni_len );
    190200        }
     
    204214                        switch( ubase->get_type() )
    205215                        {
    206                         case FLOAT          : glUniform1f( uloc, ((uniform< enum_to_type< FLOAT >::type >*)( ubase ))->get_value() ); break;
    207                         case INT            : glUniform1i( uloc, ((uniform< enum_to_type< INT >::type >*)( ubase ))->get_value() ); break;
    208                         case FLOAT_VECTOR_2 : glUniform2fv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*)( ubase ))->get_value()) ); break;
    209                         case FLOAT_VECTOR_3 : glUniform3fv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*)( ubase ))->get_value()) ); break;
    210                         case FLOAT_VECTOR_4 : glUniform4fv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*)( ubase ))->get_value()) ); break;
    211                         case INT_VECTOR_2   : glUniform2iv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< INT_VECTOR_2 >::type >*)( ubase ))->get_value()) ); break;
    212                         case INT_VECTOR_3   : glUniform3iv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< INT_VECTOR_3 >::type >*)( ubase ))->get_value()) ); break;
    213                         case INT_VECTOR_4   : glUniform4iv( uloc, 1, glm::value_ptr(((uniform< enum_to_type< INT_VECTOR_4 >::type >*)( ubase ))->get_value()) ); break;
    214                         case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, 1, GL_FALSE, glm::value_ptr(((uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*)( ubase ))->get_value()) ); break;
    215                         case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, 1, GL_FALSE, glm::value_ptr(((uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*)( ubase ))->get_value()) ); break;
    216                         case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, 1, GL_FALSE, glm::value_ptr(((uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*)( ubase ))->get_value()) ); break;
     216                        case FLOAT          : glUniform1fv( uloc, ubase->get_length(), ((uniform< enum_to_type< FLOAT >::type >*)( ubase ))->get_value() ); break;
     217                        case INT            : glUniform1iv( uloc, ubase->get_length(), ((uniform< enum_to_type< INT >::type >*)( ubase ))->get_value() ); break;
     218                        case FLOAT_VECTOR_2 : glUniform2fv( uloc, ubase->get_length(), (GLfloat*)((uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*)( ubase ))->get_value()); break;
     219                        case FLOAT_VECTOR_3 : glUniform3fv( uloc, ubase->get_length(), (GLfloat*)((uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*)( ubase ))->get_value()); break;
     220                        case FLOAT_VECTOR_4 : glUniform4fv( uloc, ubase->get_length(), (GLfloat*)((uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*)( ubase ))->get_value()); break;
     221                        case INT_VECTOR_2   : glUniform2iv( uloc, ubase->get_length(), (GLint*)((uniform< enum_to_type< INT_VECTOR_2 >::type >*)( ubase ))->get_value()); break;
     222                        case INT_VECTOR_3   : glUniform3iv( uloc, ubase->get_length(), (GLint*)((uniform< enum_to_type< INT_VECTOR_3 >::type >*)( ubase ))->get_value()); break;
     223                        case INT_VECTOR_4   : glUniform4iv( uloc, ubase->get_length(), (GLint*)((uniform< enum_to_type< INT_VECTOR_4 >::type >*)( ubase ))->get_value()); break;
     224                        case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, ubase->get_length(), GL_FALSE, (GLfloat*)((uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*)( ubase ))->get_value()); break;
     225                        case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, ubase->get_length(), GL_FALSE, (GLfloat*)((uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*)( ubase ))->get_value()); break;
     226                        case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, ubase->get_length(), GL_FALSE, (GLfloat*)((uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*)( ubase ))->get_value()); break;
    217227                        default : break; // error?
    218228                        }
Note: See TracChangeset for help on using the changeset viewer.