Commit c5349439 authored by Jan Möbius's avatar Jan Möbius
Browse files

Support-special-pick-shaders-in-ShaderNode

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@5399 383ad7c9-94d9-4d36-a494-682f7c89f535
parent b48419b1
......@@ -86,14 +86,15 @@ ShaderNode::~ShaderNode() {
//----------------------------------------------------------------------------
bool
ShaderNode::
hasShader( unsigned int _drawmode ) {
hasShader( unsigned int _drawmode, bool _pick ) {
unsigned int mode = _drawmode;
for (size_t i = 0; i < sizeof(unsigned int)*8 ; ++i) {
if (mode & 1) {
if ( shaders[i].initialized ) {
return true;
}
if (_pick)
return pickShaders[i].initialized;
else
return shaders[i].initialized;
}
mode >>= 1;
......@@ -119,14 +120,40 @@ ShaderNode::enter(GLState& /*_state*/, unsigned int _drawmode )
}
}
//----------------------------------------------------------------------------
void
ShaderNode::enterPick(GLState& /*_state*/, PickTarget /*_target*/, unsigned int _drawmode )
{
unsigned int mode = _drawmode;
for (size_t i = 0; i < sizeof(unsigned int)*8 ; ++i) {
if (mode & 1) {
if ( pickShaders[i].initialized ) {
pickShaders[i].program->use();
}
}
mode >>= 1;
}
}
//----------------------------------------------------------------------------
std::string
ShaderNode::vertexShaderName(unsigned int _drawmode) {
ShaderNode::vertexShaderName(unsigned int _drawmode, bool _pick) {
unsigned int mode = _drawmode;
for (size_t i = 0; i < sizeof(unsigned int)*8 ; ++i) {
if (mode & 1) {
if ( shaders[i].initialized ) {
return shaders[i].vertexShaderFile;
if (_pick)
{
if ( pickShaders[i].initialized ) {
return pickShaders[i].vertexShaderFile;
}
}
else
{
if ( shaders[i].initialized ) {
return shaders[i].vertexShaderFile;
}
}
}
......@@ -138,12 +165,21 @@ ShaderNode::vertexShaderName(unsigned int _drawmode) {
//----------------------------------------------------------------------------
std::string
ShaderNode::fragmentShaderName(unsigned int _drawmode) {
ShaderNode::fragmentShaderName(unsigned int _drawmode, bool _pick) {
unsigned int mode = _drawmode;
for (size_t i = 0; i < sizeof(unsigned int)*8 ; ++i) {
if (mode & 1) {
if ( shaders[i].initialized ) {
return shaders[i].fragmentShaderFile;
if (_pick)
{
if ( pickShaders[i].initialized ) {
return pickShaders[i].fragmentShaderFile;
}
}
else
{
if ( shaders[i].initialized ) {
return shaders[i].fragmentShaderFile;
}
}
}
......@@ -173,15 +209,41 @@ void ShaderNode::leave(GLState& /*_state*/, unsigned int _drawmode )
//----------------------------------------------------------------------------
void ShaderNode::leavePick(GLState& /*_state*/, PickTarget /*_target*/, unsigned int _drawmode )
{
unsigned int mode = _drawmode;
for (size_t i = 0; i < sizeof(unsigned int)*8 ; ++i) {
if (mode & 1) {
if ( pickShaders[i].initialized ) {
pickShaders[i].program->disable();
}
}
mode >>= 1;
}
}
//----------------------------------------------------------------------------
/// Get the shader for the given drawMode
GLSL::PtrProgram
ShaderNode::
getShader( unsigned int _drawmode ) {
getShader( unsigned int _drawmode, bool _pick ) {
unsigned int mode = _drawmode;
for (size_t i = 0; i < sizeof(unsigned int)*8 ; ++i) {
if (mode & 1) {
if ( shaders[i].initialized ) {
return shaders[i].program;
if (_pick)
{
if ( pickShaders[i].initialized ) {
return pickShaders[i].program;
}
}
else
{
if ( shaders[i].initialized ) {
return shaders[i].program;
}
}
}
......@@ -200,7 +262,9 @@ void
ShaderNode::
setShader( unsigned int _drawmode ,
std::string _vertexShader,
std::string _fragmentShader) {
std::string _fragmentShader,
std::string _pickVertexShader,
std::string _pickFragmentShader) {
// OpenGl 2.0 is needed for GLSL support
if (!glewIsSupported("GL_VERSION_2_0")) {
......@@ -259,6 +323,48 @@ setShader( unsigned int _drawmode ,
shaders[i].initialized = true;
if ( pickShaders[i].initialized ) {
if ( pickShaders[i].program != 0 )
delete pickShaders[i].program;
if ( pickShaders[i].vertexShader != 0 )
delete pickShaders[i].vertexShader;
if ( pickShaders[i].fragmentShader != 0 )
delete pickShaders[i].fragmentShader;
pickShaders[i].initialized = false;
}
if (_pickVertexShader.length () > 0 && _pickFragmentShader.length () > 0)
{
pickShaders[i].vertexShaderFile = shaderDir_ + _pickVertexShader;
pickShaders[i].fragmentShaderFile = shaderDir_ + _pickFragmentShader;
const char* vertexShaderFilePath = pickShaders[i].vertexShaderFile.c_str();
const char* fragmentShaderFilePath = pickShaders[i].fragmentShaderFile.c_str();
pickShaders[i].vertexShader = GLSL::loadVertexShader(vertexShaderFilePath);
pickShaders[i].fragmentShader = GLSL::loadFragmentShader(fragmentShaderFilePath);
pickShaders[i].program = GLSL::PtrProgram(new GLSL::Program());
if ( (pickShaders[i].vertexShader == 0) ||
(pickShaders[i].fragmentShader == 0) ||
(pickShaders[i].program == 0) ) {
std::cerr << "Unable to load pick shaders" << pickShaders[i].vertexShaderFile <<
" or " << pickShaders[i].fragmentShaderFile << std::endl;
pickShaders[i].vertexShader = 0;
pickShaders[i].fragmentShader = 0;
pickShaders[i].program = 0;
pickShaders[i].initialized = false;
return;
}
pickShaders[i].program->attach(pickShaders[i].vertexShader);
pickShaders[i].program->attach(pickShaders[i].fragmentShader);
pickShaders[i].program->link();
pickShaders[i].initialized = true;
}
}
mode >>= 1;
......
......@@ -84,14 +84,21 @@ public:
/// disable shader
void leave(GLState& /*_state*/, unsigned int _drawmode);
/// set shader
void enterPick(GLState& /*_state*/, PickTarget _target, unsigned int _drawmode);
/// disable shader
void leavePick(GLState& /*_state*/, PickTarget _target, unsigned int _drawmode);
/// Sets the shader dir.
void setShaderDir( std::string _shaderDir);
std::string shaderDir() { return shaderDir_; };
std::string vertexShaderName(unsigned int _drawmode);
std::string vertexShaderName(unsigned int _drawmode, bool _pick = false);
std::string fragmentShaderName(unsigned int _drawmode, bool _pick = false);
std::string fragmentShaderName(unsigned int _drawmode);
/** Sets a Shader for this draw Mode
* @param _drawmode Set the drawmode for which the shader should be activated
......@@ -100,13 +107,15 @@ public:
*/
void setShader( unsigned int _drawmode ,
std::string _vertexShader,
std::string _fragmentShader);
std::string _fragmentShader,
std::string _pickVertexShader = "",
std::string _pickFragmentShader = "");
/// Get the shader for the given drawMode
GLSL::PtrProgram getShader( unsigned int _drawmode );
GLSL::PtrProgram getShader( unsigned int _drawmode, bool _pick = false);
/// Check if a shader is available for the given drawMode
bool hasShader( unsigned int _drawmode );
bool hasShader( unsigned int _drawmode, bool _pick = false);
/// return available draw modes
unsigned int availableDrawModes() const;
......@@ -140,6 +149,7 @@ private :
};
ShaderInfo shaders[32];
ShaderInfo pickShaders[32];
};
......
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