OglShader.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 //
00026 // C++ Implementation: Shader
00027 //
00028 // Description: 
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2007
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 #include <sofa/component/visualmodel/OglShader.h>
00037 #include <sofa/core/ObjectFactory.h>
00038 #include <sofa/helper/system/FileRepository.h>
00039 
00040 
00041 namespace sofa
00042 {
00043 
00044 namespace component
00045 {
00046 
00047 namespace visualmodel
00048 {
00049 
00050 
00051 SOFA_DECL_CLASS(OglShader)
00052 
00053 //Register OglShader in the Object Factory
00054 int OglShaderClass = core::RegisterObject("OglShader")
00055 .add< OglShader >()
00056 ;
00057 
00058 OglShader::OglShader():
00059     turnOn(initData(&turnOn, (bool) true, "turnOn", "Turn On the shader?")),
00060     vertFilename(initData(&vertFilename, (std::string) "shaders/toonShading.vert", "vertFilename", "Set the vertex shader filename to load")),
00061         fragFilename(initData(&fragFilename, (std::string) "shaders/toonShading.frag", "fragFilename", "Set the fragment shader filename to load")),
00062     geoFilename(initData(&geoFilename, (std::string) "", "geoFilename", "Set the geometry shader filename to load")),
00063     geometryInputType(initData(&geometryInputType, (int) -1, "geometryInputType", "Set input types for the geometry shader")),
00064     geometryOutputType(initData(&geometryOutputType, (int) -1, "geometryOutputType", "Set output types for the geometry shader")),
00065     geometryVerticesOut(initData(&geometryVerticesOut, (int) -1, "geometryVerticesOut", "Set max number of vertices in output for the geometry shader")),
00066     hasGeometryShader(false)
00067 {
00068     
00069 
00070 }
00071 
00072 OglShader::~OglShader()
00073 {
00074     m_shader.TurnOff();
00075     m_shader.Release();
00076 }
00077 
00078 void OglShader::init()
00079 {
00080     
00081 }
00082 
00083 void OglShader::reinit()
00084 {
00085     if (hasGeometryShader)
00086     {
00087         
00088     }
00089 }
00090 
00091 void OglShader::initVisual()
00092 {
00093 //  std::string shaderPath = "shaders/"; //No hard coded path!
00094     
00095     if (!sofa::helper::gl::GLSLShader::InitGLSL())
00096     {
00097         std::cerr << "OglShader : InitGLSL failed" << std::endl;
00098         return;
00099     }
00100     
00101     std::string file = vertFilename.getValue();
00102     
00103     if (!helper::system::DataRepository.findFile(file))
00104     {
00105         std::cerr << "OglShader : vertex shader file not found." << std::endl;
00106         return;
00107     }
00108     
00109     file = fragFilename.getValue();
00110     if (!helper::system::DataRepository.findFile(file))
00111     {
00112         std::cerr << "OglShader : fragment shader file not found." << std::endl;
00113         return;
00114     }
00115     
00116     file = geoFilename.getValue();
00117     if (geoFilename.getValue() == "" || !helper::system::DataRepository.findFile(file))
00118         m_shader.InitShaders(helper::system::DataRepository.getFile(vertFilename.getValue()), 
00119                              helper::system::DataRepository.getFile(fragFilename.getValue()));
00120     
00121     else 
00122     {
00123         if (geometryInputType.getValue() != -1)
00124             setGeometryInputType(geometryInputType.getValue());
00125         if (geometryOutputType.getValue() != -1) 
00126             setGeometryOutputType(geometryOutputType.getValue());
00127 #ifdef GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT
00128             GLint maxV; 
00129             glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &maxV);
00130             if (geometryVerticesOut.getValue() == -1 || geometryVerticesOut.getValue() > maxV)
00131             geometryVerticesOut.setValue(3);
00132 #endif
00133             if (geometryVerticesOut.getValue() != -1) 
00134         setGeometryVerticesOut(geometryVerticesOut.getValue());
00135         
00136         m_shader.InitShaders(helper::system::DataRepository.getFile(vertFilename.getValue()),
00137                              helper::system::DataRepository.getFile(geoFilename.getValue()),
00138                              helper::system::DataRepository.getFile(fragFilename.getValue()));
00139         
00140         hasGeometryShader = true;
00141     }
00142     
00143 }
00144 
00145 void OglShader::drawVisual()
00146 {
00147 
00148 }
00149 
00150 void OglShader::stop()
00151 {
00152     if(turnOn.getValue())
00153         m_shader.TurnOff();
00154 }
00155 
00156 void OglShader::start()
00157 {
00158     if(turnOn.getValue())
00159         m_shader.TurnOn();
00160 }
00161 
00162 void OglShader::updateVisual()
00163 {
00164     
00165 }
00166 
00167 void OglShader::addDefineMacro(const std::string &name, const std::string &value)
00168 {
00169     m_shader.AddDefineMacro(name, value);
00170 }
00171 
00172 void OglShader::setTexture(const char* name, unsigned short unit)
00173 {
00174     start();
00175     m_shader.SetInt(m_shader.GetVariable(name), unit);
00176     stop();
00177 }
00178 void OglShader::setInt(const char* name, int i)
00179 {   
00180     start();
00181     m_shader.SetInt(m_shader.GetVariable(name), i);
00182     stop();
00183 }
00184 
00185 void OglShader::setInt2(const char* name, int i1, int i2)
00186 {
00187     start();
00188     m_shader.SetInt2(m_shader.GetVariable(name), i1, i2);
00189     stop();
00190 }
00191 void OglShader::setInt3(const char* name, int i1, int i2, int i3)
00192 {
00193     start();
00194     m_shader.SetInt3(m_shader.GetVariable(name), i1, i2, i3);
00195     stop();
00196 }
00197 void OglShader::setInt4(const char* name, int i1, int i2, int i3, int i4)
00198 {
00199     start();
00200     m_shader.SetInt4(m_shader.GetVariable(name), i1, i2, i3, i4);
00201     stop();
00202 }
00203 
00204 void OglShader::setFloat(const char* name, float f1)
00205 {
00206     start();
00207     m_shader.SetFloat(m_shader.GetVariable(name), f1);
00208     stop();
00209 }
00210 void OglShader::setFloat2(const char* name, float f1, float f2)
00211 {
00212     start();
00213     m_shader.SetFloat2(m_shader.GetVariable(name), f1, f2);
00214     stop();
00215 }
00216 void OglShader::setFloat3(const char* name, float f1, float f2, float f3)
00217 {
00218     start();
00219     m_shader.SetFloat3(m_shader.GetVariable(name), f1, f2, f3);
00220     stop();
00221 }
00222 void OglShader::setFloat4(const char* name, float f1, float f2, float f3, float f4)
00223 {
00224     start();
00225     m_shader.SetFloat4(m_shader.GetVariable(name), f1, f2, f3, f4);
00226     stop();
00227 }
00228 
00229 void OglShader::setIntVector(const char* name, int count, const GLint* i)
00230 {
00231     start();
00232     m_shader.SetIntVector(m_shader.GetVariable(name), count, i);
00233     stop();
00234 }
00235 void OglShader::setIntVector2(const char* name, int count, const GLint* i)
00236 {
00237     start();
00238     m_shader.SetIntVector2(m_shader.GetVariable(name), count, i);
00239     stop();
00240 }
00241 void OglShader::setIntVector3(const char* name, int count, const GLint* i)
00242 {
00243     start();
00244     m_shader.SetIntVector3(m_shader.GetVariable(name), count, i);
00245     stop();
00246 }
00247 void OglShader::setIntVector4(const char* name, int count, const GLint* i)
00248 {
00249     start();
00250     m_shader.SetIntVector4(m_shader.GetVariable(name), count, i);
00251     stop();
00252 }
00253 
00254 void OglShader::setFloatVector(const char* name, int count, const float* f)
00255 {
00256     start();
00257     m_shader.SetFloatVector(m_shader.GetVariable(name), count, f);
00258     stop();
00259 }
00260 void OglShader::setFloatVector2(const char* name, int count, const float* f)
00261 {
00262     start();
00263     m_shader.SetFloatVector2(m_shader.GetVariable(name), count, f);
00264     stop();
00265 }
00266 void OglShader::setFloatVector3(const char* name, int count, const float* f)
00267 {
00268     start();
00269     m_shader.SetFloatVector3(m_shader.GetVariable(name), count, f);
00270     stop();
00271 }
00272 void OglShader::setFloatVector4(const char* name, int count, const float* f)
00273 {
00274     start();
00275     m_shader.SetFloatVector4(m_shader.GetVariable(name), count, f);
00276     stop();
00277 }
00278 
00279 
00280 GLint OglShader::getGeometryInputType()
00281 {
00282     return m_shader.GetGeometryInputType();
00283 }
00284 void  OglShader::setGeometryInputType(GLint v)
00285 {
00286     m_shader.SetGeometryInputType(v);
00287 }
00288 
00289 GLint OglShader::getGeometryOutputType()
00290 {
00291     return m_shader.GetGeometryOutputType();
00292 }
00293 void  OglShader::setGeometryOutputType(GLint v)
00294 {
00295     m_shader.SetGeometryOutputType(v);
00296 }
00297 
00298 GLint OglShader::getGeometryVerticesOut()
00299 {
00300     return m_shader.GetGeometryVerticesOut();
00301 }
00302 
00303 void  OglShader::setGeometryVerticesOut(GLint v)
00304 {
00305     m_shader.SetGeometryVerticesOut(v);
00306 }
00307 
00308 OglShaderElement::OglShaderElement()
00309 : id(initData(&id, (std::string) "id", "id", "Set an ID name"))
00310 {
00311     
00312 }
00313 
00314 void OglShaderElement::init()
00315 {
00316     sofa::core::objectmodel::BaseContext* context = this->getContext();
00317     shader = context->core::objectmodel::BaseContext::get<OglShader>();
00318     
00319     if (!shader)
00320     {
00321         std::cerr << "OglShaderElement: shader not found "<< std::endl;
00322         return;
00323     }
00324 }
00325 
00326 
00327 }//namespace visualmodel
00328 
00329 } //namespace component
00330 
00331 } //namespace sofa

Generated on Sun Nov 23 03:48:33 2008 for Sofa by  doxygen 1.5.7.1