arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

progShaderFactory.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////
00002 // 7/15/03      Joe M. Kniss    Scientific Computing and Imaging Institute
00003 // School of Computing          University of Utah
00004 //
00005 //  This library is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU Lesser General Public
00007 //  License as published by the Free Software Foundation; either
00008 //  version 2.1 of the License, or (at your option) any later version.
00009 //
00010 //  This library is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 //  Lesser General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU Lesser General Public
00016 //  License along with this library; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 /////////////////////////////////////////////////////////////////////////
00019 //
00020 // ProgShaderFactory - Functions for generating progammable Shaders
00021 // 
00022 //////////////////////////////////////////////////////////////////////
00023 
00024 
00025 ///ProgShaderFactory.cpp
00026 ///  TODO: keep up the pace with new prog shader extensions
00027 ///  TODO: add parsing for NVparse and ATIShaders in text form
00028 
00029 #include "progShader.h"
00030 #include "fragProgARB.h"
00031 #include "vertexProgARB.h"
00032 #include <iostream>
00033 #include <fstream>
00034 #include <string>
00035 
00036 
00037 using namespace std;
00038 
00039 namespace glift {
00040 //////////////////////////////////////////////////////////////////////////
00041 /// get Prog from file
00042 //////////////////////////////////////////////////////////////////////////
00043 
00044 ProgShaderSP getProgShaderFromFile(const char *fileName, std::ostream *es)
00045 {
00046    /// gota handle this better!!!
00047    ifstream f(fileName);
00048    string stxt;
00049    /// get first line of shader
00050    f >> stxt;
00051    f.close();
00052 
00053    /// an arb fragment program detected
00054    if(stxt.find("!!ARBfp1.0") >= 0)
00055    {
00056       FragProgARB *fp = new FragProgARB(fileName,true,0,true);
00057 
00058       ostream *ossave;
00059       if(es)
00060       {
00061          /// save off old error stream
00062          ossave = fp->getErrStream();
00063          /// capture init errors
00064          fp->setErrStream(es);
00065       }
00066  
00067       fp->init();
00068       
00069       /// return the program's default error stream
00070       if(es)
00071       {
00072          fp->setErrStream(ossave);
00073       }
00074 
00075       return ProgShaderSP((ProgShader*)fp);
00076 
00077    }
00078    /// an arb vertex program
00079    else if(stxt.find("!!ARBvp1.0") >= 0)
00080    {
00081       VertexProgARB *vp = new VertexProgARB(fileName,true,0,true);
00082 
00083       ostream *ossave;
00084       if(es)
00085       {
00086          /// save off old error stream
00087          ossave = vp->getErrStream();
00088          /// capture init errors
00089          vp->setErrStream(es);
00090       }
00091  
00092       vp->init();
00093       
00094       /// return the program's default error stream
00095       if(es)
00096       {
00097          vp->setErrStream(ossave);
00098       }
00099 
00100       return ProgShaderSP((ProgShader*)vp);
00101    }
00102 
00103 
00104    return ProgShaderSP(0);
00105 }
00106 
00107 //////////////////////////////////////////////////////////////////////////
00108 /// get Prog from text
00109 //////////////////////////////////////////////////////////////////////////
00110 ProgShaderSP getProgShaderFromText(const char *progText, std::ostream *es)
00111 {
00112    string stxt(progText);
00113 
00114    /// an arb fragment program detected
00115    if(stxt.find("!!ARBfp1.0") >= 0)
00116    {
00117       FragProgARB *fp = new FragProgARB(progText,false,0,true);
00118 
00119       ostream *ossave;
00120       if(es)
00121       {
00122          /// save off old error stream
00123          ossave = fp->getErrStream();
00124          /// capture init errors
00125          fp->setErrStream(es);
00126       }
00127  
00128       fp->init();
00129       
00130       /// return the program's default error stream
00131       if(es)
00132       {
00133          fp->setErrStream(ossave);
00134       }
00135 
00136       return ProgShaderSP((ProgShader*)fp);
00137 
00138    }
00139    /// an arb vertex program
00140    else if(stxt.find("!!ARBvp1.0") >= 0)
00141    {
00142       VertexProgARB *vp = new VertexProgARB(progText,false,0,true);
00143 
00144       ostream *ossave;
00145       if(es)
00146       {
00147          /// save off old error stream
00148          ossave = vp->getErrStream();
00149          /// capture init errors
00150          vp->setErrStream(es);
00151       }
00152  
00153       vp->init();
00154       
00155       /// return the program's default error stream
00156       if(es)
00157       {
00158          vp->setErrStream(ossave);
00159       }
00160 
00161       return ProgShaderSP((ProgShader*)vp);
00162    }
00163 
00164    return ProgShaderSP(0);
00165 }
00166 
00167 }/// end namespace glift
00168 

Send questions, comments, and bug reports to:
jmk