Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:37

0001 #pragma once
0002 /**
0003 SGLFW_Attrib.h
0004 =================
0005 
0006 SGLFW_GLboolean
0007    string parse
0008 
0009 SGLFW_bool
0010    string parse
0011 
0012 SGLFW_GLenum
0013    string parse
0014 
0015 SGLFW_Attrib
0016    parse attribute metadata strings such as "4,GL_FLOAT,GL_FALSE,64,0,false"
0017 
0018 
0019 
0020 
0021 **/
0022 
0023 
0024 struct SGLFW_GLboolean
0025 {
0026     static constexpr const char* GL_FALSE_  = "GL_FALSE" ;
0027     static constexpr const char* GL_TRUE_   = "GL_TRUE" ;
0028     static GLboolean Value(const char* name);
0029     static const char* Name(GLboolean value);
0030 };
0031 inline GLboolean SGLFW_GLboolean::Value(const char* name)
0032 {
0033     GLboolean value = GL_FALSE ;
0034     if( strcmp( name, GL_FALSE_) == 0 ) value = GL_FALSE ;
0035     if( strcmp( name, GL_TRUE_)  == 0 ) value = GL_TRUE ;
0036     return value ;
0037 }
0038 inline const char* SGLFW_GLboolean::Name(GLboolean value)
0039 {
0040     const char* s = nullptr ;
0041     switch(value)
0042     {
0043        case GL_FALSE: s = GL_FALSE_ ; break ;
0044        case GL_TRUE:  s = GL_TRUE_ ; break ;
0045     }
0046     return s ;
0047 }
0048 
0049 
0050 struct SGLFW_bool
0051 {
0052     static constexpr const char* false_ = "false" ;
0053     static constexpr const char* true_  = "true" ;
0054     static bool Value(const char* name);
0055     static const char* Name(bool value);
0056 };
0057 inline bool SGLFW_bool::Value(const char* name)
0058 {
0059     bool value = false ;
0060     if( strcmp( name, false_) == 0 ) value = false ;
0061     if( strcmp( name, true_)  == 0 ) value = true ;
0062     return value ;
0063 }
0064 inline const char* SGLFW_bool::Name(bool value)
0065 {
0066     return value ? true_ : false_ ;
0067 }
0068 
0069 
0070 
0071 struct SGLFW_GLenum
0072 {
0073     static constexpr const char* GL_BYTE_           = "GL_BYTE" ;
0074     static constexpr const char* GL_UNSIGNED_BYTE_  = "GL_UNSIGNED_BYTE" ;
0075     static constexpr const char* GL_SHORT_          = "GL_SHORT" ;
0076     static constexpr const char* GL_UNSIGNED_SHORT_ = "GL_UNSIGNED_SHORT" ;
0077     static constexpr const char* GL_INT_            = "GL_INT" ;
0078     static constexpr const char* GL_UNSIGNED_INT_   = "GL_UNSIGNED_INT" ;
0079     static constexpr const char* GL_HALF_FLOAT_     = "GL_HALF_FLOAT" ;
0080     static constexpr const char* GL_FLOAT_          = "GL_FLOAT" ;
0081     static constexpr const char* GL_DOUBLE_         = "GL_DOUBLE" ;
0082 
0083     static const char* Name(GLenum type);
0084     static GLenum      Type(const char* name);
0085 };
0086 
0087 inline const char* SGLFW_GLenum::Name(GLenum type)
0088 {
0089     const char* s = nullptr ;
0090     switch(type)
0091     {
0092         case GL_BYTE:           s = GL_BYTE_           ; break ;
0093         case GL_UNSIGNED_BYTE:  s = GL_UNSIGNED_BYTE_  ; break ;
0094         case GL_SHORT:          s = GL_SHORT_          ; break ;
0095         case GL_UNSIGNED_SHORT: s = GL_UNSIGNED_SHORT_ ; break ;
0096         case GL_INT:            s = GL_INT_            ; break ;
0097         case GL_UNSIGNED_INT:   s = GL_UNSIGNED_INT_   ; break ;
0098         case GL_HALF_FLOAT:     s = GL_HALF_FLOAT_     ; break ;
0099         case GL_FLOAT:          s = GL_FLOAT_          ; break ;
0100         case GL_DOUBLE:         s = GL_DOUBLE_         ; break ;
0101         default:                s = nullptr            ; break ;
0102     }
0103     return s ;
0104 }
0105 
0106 inline GLenum SGLFW_GLenum::Type(const char* name)
0107 {
0108     GLenum type = 0 ;
0109     if( strcmp( name, GL_BYTE_) == 0 )           type = GL_BYTE ;
0110     if( strcmp( name, GL_UNSIGNED_BYTE_) == 0 )  type = GL_UNSIGNED_BYTE ;
0111     if( strcmp( name, GL_SHORT_) == 0 )          type = GL_SHORT ;
0112     if( strcmp( name, GL_UNSIGNED_SHORT_) == 0 ) type = GL_UNSIGNED_SHORT ;
0113     if( strcmp( name, GL_INT_) == 0 )            type = GL_INT ;
0114     if( strcmp( name, GL_UNSIGNED_INT_) == 0 )   type = GL_UNSIGNED_INT ;
0115     if( strcmp( name, GL_HALF_FLOAT_) == 0 )     type = GL_HALF_FLOAT ;
0116     if( strcmp( name, GL_FLOAT_) == 0 )          type = GL_FLOAT ;
0117     if( strcmp( name, GL_DOUBLE_) == 0 )         type = GL_DOUBLE ;
0118     return type ;
0119 }
0120 
0121 
0122 /**
0123 SGLFW_Attrib
0124 -----------------
0125 
0126 Parse a string of the below form into 6 fields::
0127 
0128     rpos:4,GL_FLOAT,GL_FALSE,64,0,false
0129 
0130 **/
0131 struct SGLFW_Attrib
0132 {
0133     const char* name ;
0134     const char* spec ;
0135     std::vector<std::string> field ;
0136 
0137     GLuint index ;                 // set externally to the result of SGLFW_Program::getAttribLocation(name)
0138 
0139     GLint size ;                   // field 0 : number of components of the attribute (aka item), must be one of : 1,2,3,4
0140     GLenum type ;                  // field 1 : normally GL_FLOAT
0141     GLboolean normalized ;         // field 2 : normalized means in range 0->1
0142     GLsizei stride ;               // field 3 : in bytes eg for 4,4 float photon/record struct stride is 4*4*4=64
0143     size_t   byte_offset ;         // field 4 : allows access to different parts of array of structs
0144     bool     integer_attribute ;   // field 5
0145 
0146     void*    byte_offset_pointer ; // derived from byte_offset
0147 
0148 
0149     SGLFW_Attrib( const char* name, const char* spec );
0150     std::string desc() const ;
0151 };
0152 
0153 
0154 inline SGLFW_Attrib::SGLFW_Attrib(const char* name_, const char* spec_)
0155     :
0156     name(strdup(name_)),
0157     spec(strdup(spec_)),
0158     index(0),
0159     size(0),
0160     type(0),
0161     normalized(false),
0162     stride(0),
0163     byte_offset(0),
0164     integer_attribute(false),
0165     byte_offset_pointer(nullptr)
0166 {
0167     char delim = ',' ;
0168     std::stringstream ss;
0169     ss.str(spec)  ;
0170     std::string s;
0171     while (std::getline(ss, s, delim)) field.push_back(s) ;
0172     assert( field.size() == 6 );
0173 
0174     size =  std::atoi(field[0].c_str()) ;           assert( size == 1 || size == 2 || size == 3 || size == 4 ) ;
0175     type =  SGLFW_GLenum::Type(field[1].c_str()) ;  assert( type > 0 );
0176     normalized = SGLFW_GLboolean::Value(field[2].c_str()) ;
0177     stride = std::atoi( field[3].c_str() );          assert( stride > 0 );
0178     byte_offset = std::atoi( field[4].c_str() );     assert( byte_offset >= 0 );
0179     integer_attribute = SGLFW_bool::Value(field[5].c_str()) ;
0180 
0181     byte_offset_pointer = (void*)byte_offset ;
0182 }
0183 
0184 inline std::string SGLFW_Attrib::desc() const
0185 {
0186     std::stringstream ss ;
0187     ss << "SGLFW_Attrib::desc" << std::endl
0188        << std::setw(20) << "name"  << " : " << name << std::endl
0189        << std::setw(20) << "spec"  << " : " << spec << std::endl
0190        << std::setw(20) << "index" << " : " << index << std::endl
0191        << std::setw(20) << "size"  << " : " << size << std::endl
0192        << std::setw(20) << "type"  << " : " << SGLFW_GLenum::Name(type) << std::endl
0193        << std::setw(20) << "normalized" << " : " << SGLFW_GLboolean::Name(normalized) << std::endl
0194        << std::setw(20) << "stride" << " : " << stride << std::endl
0195        << std::setw(20) << "byte_offset" << " : " << byte_offset << std::endl
0196        << std::setw(20) << "integer_attribute" << " : " << SGLFW_bool::Name(integer_attribute) << std::endl
0197        << std::setw(20) << "byte_offset_pointer" << " : " << byte_offset_pointer << std::endl
0198        ;
0199 
0200     for(unsigned i=0 ; i < field.size() ; i++ ) ss << std::setw(20) << i << " : " << field[i] << std::endl ;
0201     std::string s = ss.str();
0202     return s ;
0203 }
0204 
0205 
0206