Changeset 10766

Show
Ignore:
Timestamp:
11/18/09 12:25:28 (5 years ago)
Author:
robert
Message:

Added osg::ShaderBinary::readShaderBinaryFile(..) static method

Location:
OpenSceneGraph/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/include/osg/Shader

    r10754 r10766  
    3636{ 
    3737    public: 
    38          
     38 
    3939        ShaderBinary(); 
    4040 
     
    4646        /** Allocated a data buffer of specified size/*/ 
    4747        void allocate(unsigned int size); 
    48          
     48 
    4949        /** Assign shader binary data, copying the specified data into locally stored data buffer, the original data can then be deleted.*/ 
    5050        void assign(unsigned int size, const unsigned char* data); 
    51          
     51 
    5252        /** Get the size of the shader binary data.*/ 
    5353        unsigned int getSize() const { return _data.size(); } 
     
    5858        /** Get a const ptr to the shader binary data.*/ 
    5959        const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); } 
    60          
     60 
     61        /** Read shader binary from file. 
     62          * Return the resulting Shader or 0 if no valid shader binary could be read.*/ 
     63        static ShaderBinary* readShaderBinaryFile(const std::string& fileName); 
     64 
    6165    protected: 
    62          
     66 
    6367        typedef std::vector<unsigned char> Data; 
    6468        Data _data; 
     
    133137 
    134138        /** Read shader source from file and then constructor shader of specified type. 
    135           * Return the resulting Shader or 0 if no valid shader source code be read.*/ 
     139          * Return the resulting Shader or 0 if no valid shader source could be read.*/ 
    136140        static Shader* readShaderFile( Type type, const std::string& fileName ); 
    137141 
  • OpenSceneGraph/trunk/src/osg/Shader.cpp

    r10760 r10766  
    6464} 
    6565 
    66         /////////////////////////////////////////////////////////////////////////// 
     66ShaderBinary* ShaderBinary::readShaderBinaryFile(const std::string& fileName) 
     67{ 
     68    std::ifstream fin; 
     69    fin.open(fileName.c_str(), std::ios::binary); 
     70    if (!fin) return 0; 
     71 
     72    fin.seekg(0, std::ios::end); 
     73    int length = fin.tellg(); 
     74    if (length==0) return 0; 
     75 
     76    osg::ref_ptr<ShaderBinary> shaderBinary = new osg::ShaderBinary; 
     77    shaderBinary->allocate(length); 
     78    fin.seekg(0, std::ios::beg); 
     79    fin.read(reinterpret_cast<char*>(shaderBinary->getData()), length); 
     80    fin.close(); 
     81 
     82    return shaderBinary.release(); 
     83} 
     84 
     85/////////////////////////////////////////////////////////////////////////// 
    6786// static cache of glShaders flagged for deletion, which will actually 
    6887// be deleted in the correct GL context.