Commit dd9148bd authored by schurade's avatar schurade

[ADD] shader class to manage shader related tasks

parent 35bd188d
......@@ -19,3 +19,6 @@ syntax: regexp
syntax: glob
.project
.cproject
syntax: regexp
^Debug$
\ No newline at end of file
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include "../kernel/WKernel.h"
#include "WShader.h"
WShader::WShader()
{
}
WShader::WShader( std::string fileName )
{
m_VertexObject = readShaderFromFile( fileName + ".vs", osg::Shader::VERTEX );
m_FragmentObject = readShaderFromFile( fileName + ".fs", osg::Shader::FRAGMENT );
m_ProgramObject = new osg::Program;
if ( m_FragmentObject )
{
m_ProgramObject->addShader( m_FragmentObject );
}
if ( m_VertexObject )
{
m_ProgramObject->addShader( m_VertexObject );
}
}
WShader::~WShader()
{
}
osg::Shader* WShader::readShaderFromFile( std::string fileName, osg::Shader::Type type )
{
std::string fileText = readTextFile( fileName );
std::cout << fileText << std::endl;
osg::Shader* shader = new osg::Shader( type, fileText );
return shader;
}
std::string WShader::readTextFile( std::string fileName )
{
std::string fileText;
std::ifstream ifs( ( WKernel::getRunningKernel()->getShaderPath() + fileName ).c_str() );
std::string line;
while ( getline( ifs, line ) )
{
if ( isIncludeLine( line ) )
{
fileText += readTextFile( getIncludeFileName( line ) );
}
else
{
fileText += line;
fileText += '\n';
}
}
return fileText;
}
bool WShader::isIncludeLine( std::string line )
{
if ( line.substr( 0, 8 ) == "#include" )
{
return true;
}
return false;
}
std::string WShader::getIncludeFileName( std::string line )
{
size_t pos = 0;
std::string fileName;
while ( line[pos++] != '\"' )
{
}
while ( ( pos < line.length() ) && ( line[pos] != '\"' ) )
{
fileName += line[pos];
++pos;
}
return fileName;
}
osg::Program* WShader::getProgramObject()
{
return m_ProgramObject;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WSHADER_H
#define WSHADER_H
#include <string>
#include <osg/Geometry>
#include <boost/shared_ptr.hpp>
class WShader
{
public:
/**
* default constructor
*/
WShader();
/**
*
*/
explicit WShader( std::string fileName );
/**
* destructor
*/
virtual ~WShader();
/**
*
*/
osg::Program* getProgramObject();
private:
/**
*
*/
osg::Shader* readShaderFromFile( std::string fileName, osg::Shader::Type type );
/**
*
*/
std::string readTextFile( std::string fileName );
/**
*
*/
bool isIncludeLine( std::string line );
/**
*
*/
std::string getIncludeFileName( std::string line );
/**
*
*/
osg::Shader* m_VertexObject;
/**
*
*/
osg::Shader* m_FragmentObject;
/**
*
*/
osg::Program* m_ProgramObject;
};
#endif // WSHADER_H
......@@ -25,7 +25,6 @@
#include <string>
#include <vector>
#include <osg/ShapeDrawable>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
......@@ -33,10 +32,13 @@
#include "WNavigationSliceModule.h"
#include "../../kernel/WKernel.h"
#include "../../graphicsEngine/WShader.h"
WNavigationSliceModule::WNavigationSliceModule():
WModule()
{
// initialize members
m_shader = boost::shared_ptr< WShader > ( new WShader( "slice" ) );
}
WNavigationSliceModule::~WNavigationSliceModule()
......@@ -141,25 +143,7 @@ void WNavigationSliceModule::createSlices()
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( m_sliceNode );
std::string shaderPath = WKernel::getRunningKernel()->getShaderPath();
std::cout << "Full path is: " << shaderPath << std::endl;
osg::StateSet* sliceState = m_sliceNode->getOrCreateStateSet();
osg::Program* sliceProgramObject = new osg::Program;
osg::Shader* sliceVertexObject = osg::Shader::readShaderFile( osg::Shader::VERTEX, shaderPath + "slice.vs" );
osg::Shader* sliceFragmentObject = osg::Shader::readShaderFile( osg::Shader::FRAGMENT, shaderPath + "slice.fs" );
if ( sliceFragmentObject )
{
sliceProgramObject->addShader( sliceFragmentObject );
}
if ( sliceVertexObject )
{
sliceProgramObject->addShader( sliceVertexObject );
}
sliceState->setAttributeAndModes( sliceProgramObject, osg::StateAttribute::ON );
sliceState->setAttributeAndModes( m_shader->getProgramObject(), osg::StateAttribute::ON );
}
......@@ -26,9 +26,12 @@
#include <string>
#include "../../kernel/WModule.h"
#include <osg/Node>
#include "../../kernel/WModule.h"
#include "../../graphicsEngine/WShader.h"
/**
* \par Description:
* Simple module for testing some WKernel functionality.
......@@ -79,6 +82,11 @@ protected:
virtual void threadMain();
private:
/**
*
*/
void createSlices();
/**
*
*/
......@@ -87,7 +95,7 @@ private:
/**
*
*/
void createSlices();
boost::shared_ptr< WShader >m_shader;
};
#endif // WNAVIGATIONSLICEMODULE_H
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment