Implemented the Shader widget, more or less.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 12 years ago
parent 128c2a3270
commit 00b47d6a50

@ -34,8 +34,10 @@ namespace NL3D
~CShaderManager(); ~CShaderManager();
void clear(); void clear();
void getShaderList( std::vector< std::string > &v ); void getShaderList( std::vector< std::string > &v );
void addShader( CShaderProgram *program ); bool addShader( CShaderProgram *program );
void changeShader( const std::string &name, CShaderProgram *program ); bool removeShader( const std::string &name );
bool changeShader( const std::string &name, CShaderProgram *program );
bool getShader( const std::string &name, CShaderProgram *program );
void visitShaders( IShaderVisitor *visitor ); void visitShaders( IShaderVisitor *visitor );
void visitShader( const std::string &name, IShaderVisitor *visitor ); void visitShader( const std::string &name, IShaderVisitor *visitor );

@ -41,8 +41,8 @@ namespace NL3D
void setName( const std::string &n ){ name = n; } void setName( const std::string &n ){ name = n; }
void setDescription( const std::string &d ){ description = d; } void setDescription( const std::string &d ){ description = d; }
void setVP( std::string &vp ){ vertexProgram = vp; } void setVP( const std::string &vp ){ vertexProgram = vp; }
void setFP( std::string &fp ){ fragmentProgram = fp; } void setFP( const std::string &fp ){ fragmentProgram = fp; }
uint32 getVPId() const{ return vpId; } uint32 getVPId() const{ return vpId; }
uint32 getFPId() const{ return fpId; } uint32 getFPId() const{ return fpId; }

@ -56,7 +56,7 @@ namespace NL3D
} }
} }
void CShaderManager::addShader( CShaderProgram *program ) bool CShaderManager::addShader( CShaderProgram *program )
{ {
std::string n; std::string n;
program->getName( n ); program->getName( n );
@ -64,20 +64,34 @@ namespace NL3D
std::tr1::unordered_map< std::string, CShaderProgram* >::iterator itr std::tr1::unordered_map< std::string, CShaderProgram* >::iterator itr
= programs.find( n ); = programs.find( n );
if( itr != programs.end() ) if( itr != programs.end() )
return; return false;
programs[ n ] = program; programs[ n ] = program;
return true;
} }
void CShaderManager::changeShader( const std::string &name, CShaderProgram *program ) bool CShaderManager::removeShader( const std::string &name )
{ {
std::tr1::unordered_map< std::string, CShaderProgram* >::iterator itr std::tr1::unordered_map< std::string, CShaderProgram* >::iterator itr
= programs.find( name ); = programs.find( name );
if( itr == programs.end() ) if( itr == programs.end() )
return; return false;
CShaderProgram *p = itr->second; delete itr->second;
itr->second = NULL;
programs.erase( itr );
return true;
}
bool CShaderManager::changeShader( const std::string &name, CShaderProgram *program )
{
std::tr1::unordered_map< std::string, CShaderProgram* >::iterator itr
= programs.find( name );
if( itr == programs.end() )
return false;
CShaderProgram *p = itr->second;
std::string s; std::string s;
program->getName( s ); program->getName( s );
@ -91,7 +105,32 @@ namespace NL3D
program->getFP( s ); program->getFP( s );
p->setFP( s ); p->setFP( s );
return true;
}
bool CShaderManager::getShader( const std::string &name, CShaderProgram *program )
{
std::tr1::unordered_map< std::string, CShaderProgram* >::iterator itr
= programs.find( name );
if( itr == programs.end() )
return false;
CShaderProgram *p = itr->second;
std::string s;
program->setName( name );
p->getDescription( s );
program->setDescription( s );
p->getVP( s );
program->setVP( s );
p->getFP( s );
program->setFP( s );
return true;
} }
void CShaderManager::visitShaders( IShaderVisitor *visitor ) void CShaderManager::visitShaders( IShaderVisitor *visitor )

@ -23,6 +23,7 @@ namespace NL3D
{ {
vpId = 0; vpId = 0;
fpId = 0; fpId = 0;
pId = 0;
} }
CShaderProgram::~CShaderProgram() CShaderProgram::~CShaderProgram()

@ -44,6 +44,7 @@ namespace MaterialEditor
nl3dIface = new CNel3DInterface(); nl3dIface = new CNel3DInterface();
shaderWidget = new ShaderWidget(); shaderWidget = new ShaderWidget();
shaderWidget->setNel3DInterface( nl3dIface );
materialSplitter = new MaterialSplitter(); materialSplitter = new MaterialSplitter();
materialSplitter->setNel3DIface( nl3dIface ); materialSplitter->setNel3DIface( nl3dIface );
passesWidget = new RenderPassesWidget(); passesWidget = new RenderPassesWidget();
@ -139,6 +140,7 @@ namespace MaterialEditor
void MaterialEditorWindow::onShadersClicked() void MaterialEditorWindow::onShadersClicked()
{ {
shaderWidget->load();
shaderWidget->show(); shaderWidget->show();
} }

@ -16,6 +16,8 @@
#include "nel3d_interface.h" #include "nel3d_interface.h"
#include "nel/3d/dynamic_material.h" #include "nel/3d/dynamic_material.h"
#include "nel/3d/shader_manager.h"
#include "nel/3d/shader_program.h"
#include "nel/misc/i_xml.h" #include "nel/misc/i_xml.h"
#include "nel/misc/o_xml.h" #include "nel/misc/o_xml.h"
#include "nel/misc/file.h" #include "nel/misc/file.h"
@ -180,10 +182,14 @@ namespace MaterialEditor
CNel3DInterface::CNel3DInterface() CNel3DInterface::CNel3DInterface()
{ {
mat = new NL3D::CDynMaterial(); mat = new NL3D::CDynMaterial();
shaderManager = new NL3D::CShaderManager();
} }
CNel3DInterface::~CNel3DInterface() CNel3DInterface::~CNel3DInterface()
{ {
delete shaderManager;
shaderManager = NULL;
} }
bool CNel3DInterface::loadMaterial( const char *fname ) bool CNel3DInterface::loadMaterial( const char *fname )
@ -232,5 +238,67 @@ namespace MaterialEditor
{ {
return CNelMaterialProxy( mat ); return CNelMaterialProxy( mat );
} }
void CNel3DInterface::getShaderList( std::vector< std::string > &v )
{
shaderManager->getShaderList( v );
}
bool CNel3DInterface::getShaderInfo( const std::string &name, SShaderInfo &info )
{
NL3D::CShaderProgram program;
bool ok = shaderManager->getShader( name, &program );
if( !ok )
return false;
std::string s;
info.name = name;
program.getDescription( s );
info.description = s;
program.getVP( s );
info.vp = s;
program.getFP( s );
info.fp = s;
return true;
}
bool CNel3DInterface::updateShaderInfo( const SShaderInfo &info )
{
NL3D::CShaderProgram program;
program.setName( info.name );
program.setDescription( info.description );
program.setVP( info.vp );
program.setFP( info.fp );
return shaderManager->changeShader( info.name, &program );
}
bool CNel3DInterface::addShader( const SShaderInfo &info )
{
NL3D::CShaderProgram *program = new NL3D::CShaderProgram();
program->setName( info.name );
program->setDescription( info.description );
program->setVP( info.vp );
program->setFP( info.fp );
bool ok = shaderManager->addShader( program );
if( !ok )
{
delete program;
return false;
}
return true;
}
bool CNel3DInterface::removeShader( const std::string &name )
{
return shaderManager->removeShader( name );
}
} }

@ -25,6 +25,7 @@ namespace NL3D
{ {
class CDynMaterial; class CDynMaterial;
struct SRenderPass; struct SRenderPass;
class CShaderManager;
} }
namespace MaterialEditor namespace MaterialEditor
@ -106,6 +107,14 @@ namespace MaterialEditor
}; };
struct SShaderInfo
{
std::string name;
std::string description;
std::string vp;
std::string fp;
};
/// Proxy class for Nel3D, so the material editor and Nel3D can interface /// Proxy class for Nel3D, so the material editor and Nel3D can interface
class CNel3DInterface class CNel3DInterface
{ {
@ -119,8 +128,16 @@ namespace MaterialEditor
CNelMaterialProxy getMaterial(); CNelMaterialProxy getMaterial();
void getShaderList( std::vector< std::string > &v );
bool getShaderInfo( const std::string &name, SShaderInfo &info );
bool updateShaderInfo( const SShaderInfo &info );
bool addShader( const SShaderInfo &info );
bool removeShader( const std::string &name );
private: private:
NL3D::CDynMaterial *mat; NL3D::CDynMaterial *mat;
NL3D::CShaderManager *shaderManager;
}; };
} }

@ -27,7 +27,11 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="nameEdit"/> <widget class="QLineEdit" name="nameEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item> </item>
</layout> </layout>
</item> </item>
@ -117,13 +121,6 @@
</layout> </layout>
</item> </item>
</layout> </layout>
<zorder>layoutWidget</zorder>
<zorder>layoutWidget_2</zorder>
<zorder>layoutWidget_3</zorder>
<zorder>layoutWidget_4</zorder>
<zorder>layoutWidget_5</zorder>
<zorder>vsEdit</zorder>
<zorder>label_3</zorder>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

@ -16,6 +16,7 @@
#include "shader_widget.h" #include "shader_widget.h"
#include "shader_editor.h" #include "shader_editor.h"
#include "nel3d_interface.h"
#include <QFileDialog> #include <QFileDialog>
#include <QInputDialog> #include <QInputDialog>
#include <QMessageBox> #include <QMessageBox>
@ -29,6 +30,7 @@ namespace MaterialEditor
setupConnections(); setupConnections();
shaderEditorWidget = new ShaderEditorWidget(); shaderEditorWidget = new ShaderEditorWidget();
nl3dIface = NULL;
} }
ShaderWidget::~ShaderWidget() ShaderWidget::~ShaderWidget()
@ -37,6 +39,22 @@ namespace MaterialEditor
shaderEditorWidget = NULL; shaderEditorWidget = NULL;
} }
void ShaderWidget::load()
{
std::vector< std::string > v;
nl3dIface->getShaderList( v );
shaderList->clear();
std::vector< std::string >::const_iterator itr = v.begin();
while( itr != v.end() )
{
shaderList->addItem( itr->c_str() );
++itr;
}
}
void ShaderWidget::setupConnections() void ShaderWidget::setupConnections()
{ {
connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) );
@ -44,6 +62,8 @@ namespace MaterialEditor
connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) );
connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) );
connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) );
connect( shaderList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onRowChanged( int ) ) );
} }
void ShaderWidget::onOKClicked() void ShaderWidget::onOKClicked()
@ -98,6 +118,20 @@ namespace MaterialEditor
return; return;
} }
SShaderInfo info;
info.name = name.toUtf8().data();
bool ok = nl3dIface->addShader( info );
if( !ok )
{
QMessageBox::critical(
this,
tr( "Error adding shader" ),
tr( "There was an error while trying to add the shader" )
);
return;
}
shaderList->addItem( name ); shaderList->addItem( name );
} }
@ -119,8 +153,11 @@ namespace MaterialEditor
if( selection == QMessageBox::Yes ) if( selection == QMessageBox::Yes )
{ {
QListWidgetItem *item = shaderList->takeItem( i ); QListWidgetItem *item = shaderList->takeItem( i );
std::string n = item->text().toUtf8().data();
delete item; delete item;
nl3dIface->removeShader( n );
} }
} }
void ShaderWidget::onEditClicked() void ShaderWidget::onEditClicked()
@ -132,34 +169,66 @@ namespace MaterialEditor
QString name = shaderList->item( i )->text(); QString name = shaderList->item( i )->text();
shaderEditorWidget->reset(); shaderEditorWidget->reset();
shaderEditorWidget->setName( name );
SShaderInfo info;
std::string n = name.toUtf8().data();
bool ok = nl3dIface->getShaderInfo( n, info );
if( !ok )
{
QMessageBox::critical(
this,
tr( "Error retrieving shader data" ),
tr( "There was an error while trying to retrieve shader data!" )
);
QString sname; return;
bool ok; }
int res;
do{
ok = true;
res = shaderEditorWidget->exec();
if( res == QDialog::Rejected )
return;
shaderEditorWidget->getName( sname ); shaderEditorWidget->setName( info.name.c_str() );
shaderEditorWidget->setDescription( info.description.c_str() );
shaderEditorWidget->setVertexShader( info.vp.c_str() );
shaderEditorWidget->setFragmentShader( info.fp.c_str() );
if( sname != name ) int res = shaderEditorWidget->exec();
{ if( res == QDialog::Rejected )
if( nameExists( sname ) ) return;
{
ok = false;
nameExistsMessage();
}
}
}while( !ok ); // save
QString s;
shaderList->item( i )->setText( sname ); shaderEditorWidget->getDescription( s );
info.description = s.toUtf8().data();
// save shaderEditorWidget->getVertexShader( s );
info.vp = s.toUtf8().data();
shaderEditorWidget->getFragmentShader( s );
info.fp = s.toUtf8().data();
ok = nl3dIface->updateShaderInfo( info );
if( !ok )
{
QMessageBox::critical(
this,
tr( "Error saving shader data" ),
tr( "There was an error while trying to save shader data!" )
);
}
}
void ShaderWidget::onRowChanged( int i )
{
if( i < 0 )
return;
std::string s = shaderList->item( i )->text().toUtf8().data();
SShaderInfo info;
bool ok = nl3dIface->getShaderInfo( s, info );
if( !ok )
return;
description->setPlainText( info.description.c_str() );
} }
} }

@ -23,6 +23,7 @@
namespace MaterialEditor namespace MaterialEditor
{ {
class ShaderEditorWidget; class ShaderEditorWidget;
class CNel3DInterface;
class ShaderWidget : public QWidget, public Ui::ShaderWidget class ShaderWidget : public QWidget, public Ui::ShaderWidget
{ {
@ -32,18 +33,23 @@ namespace MaterialEditor
ShaderWidget( QWidget *parent = NULL ); ShaderWidget( QWidget *parent = NULL );
~ShaderWidget(); ~ShaderWidget();
void setNel3DInterface( CNel3DInterface *iface ){ nl3dIface = iface; }
void load();
private: private:
void setupConnections(); void setupConnections();
bool nameExists( const QString &name ); bool nameExists( const QString &name );
void nameExistsMessage(); void nameExistsMessage();
ShaderEditorWidget *shaderEditorWidget; ShaderEditorWidget *shaderEditorWidget;
CNel3DInterface *nl3dIface;
private Q_SLOTS: private Q_SLOTS:
void onOKClicked(); void onOKClicked();
void onAddClicked(); void onAddClicked();
void onRemoveClicked(); void onRemoveClicked();
void onEditClicked(); void onEditClicked();
void onRowChanged( int i );
}; };
} }

Loading…
Cancel
Save