diff --git a/code/nel/include/nel/3d/dyn_mat_loader.h b/code/nel/include/nel/3d/dyn_mat_loader.h index 79f7da411..1de84d59a 100644 --- a/code/nel/include/nel/3d/dyn_mat_loader.h +++ b/code/nel/include/nel/3d/dyn_mat_loader.h @@ -1,45 +1,45 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef DYN_MAT_LOADER_H -#define DYN_MAT_LOADER_H - -#include - -namespace NL3D -{ - class CDynMaterial; - - class CDynMatLoader - { - public: - CDynMatLoader(); - ~CDynMatLoader(); - - bool loadFrom( const std::string &fileName ); - - CDynMaterial* getDynMat() const{ return mat; } - - private: - CDynMaterial *mat; - - }; -} - -#endif - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef DYN_MAT_LOADER_H +#define DYN_MAT_LOADER_H + +#include + +namespace NL3D +{ + class CDynMaterial; + + class CDynMatLoader + { + public: + CDynMatLoader(); + ~CDynMatLoader(); + + bool loadFrom( const std::string &fileName ); + + CDynMaterial* getDynMat() const{ return mat; } + + private: + CDynMaterial *mat; + + }; +} + +#endif + + diff --git a/code/nel/include/nel/3d/dynamic_material.h b/code/nel/include/nel/3d/dynamic_material.h index dbad244e0..cf0fb671b 100644 --- a/code/nel/include/nel/3d/dynamic_material.h +++ b/code/nel/include/nel/3d/dynamic_material.h @@ -1,121 +1,121 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef DYN_MATERIAL_H -#define DYN_MATERIAL_H - -#include "nel/misc/stream.h" -#include "nel/misc/variant.h" -#include -#include - -namespace NL3D -{ - /// Rendering property - struct SDynMaterialProp - { - enum EPropertyType - { - Color, - Vector4, - Float, - Double, - Int, - Uint, - Matrix4, - Texture - }; - - /// Id of the property - std::string prop; - - /// Label of the property ( user-friendly name ) - std::string label; - - /// type of the property ( see EPropertyType ) - uint8 type; - - NLMISC::CVariant value; - - void serial( NLMISC::IStream &f ); - }; - - - /// Rendering pass data, contains the rendering properties for the pass - struct SRenderPass - { - public: - void addProperty( const SDynMaterialProp &prop ); - void removeProperty( const std::string &name ); - bool changeProperty( const std::string &name, const SDynMaterialProp &prop ); - void setName( const std::string &n ){ name = n; } - void getName( std::string &n ) const { n = name; } - void getShaderRef( std::string &s ) const{ s = shaderRef; } - void setShaderRef( const std::string &s ){ shaderRef = s; } - void serial( NLMISC::IStream &f ); - - /// Returns the number of properties this pass has - uint32 count(){ return properties.size(); } - - /// Clears all properties - void clear(){ properties.clear(); } - - const SDynMaterialProp* getProperty( uint32 i ) const; - - private: - std::vector< SDynMaterialProp > properties; - std::string shaderRef; - std::string name; - }; - - - - /// Multi-pass material for rendering using user shaders - class CDynMaterial : public NLMISC::IStreamable - { - public: - CDynMaterial(); - ~CDynMaterial(); - CDynMaterial& operator=( const CDynMaterial &other ); - - /// Clears all passes, and then adds a new pass - void reconstruct(); - - /// Clears all passes - void clear(); - void serial( NLMISC::IStream &f ); - std::string getClassName(){ return "CDynMaterial"; } - - void addPass( const SRenderPass &pass ); - void removePass( const std::string &name ); - void renamePass( const std::string &from, const std::string &to ); - void movePassUp( const std::string &name ); - void movePassDown( const std::string &name ); - - SRenderPass* getPass( const std::string &name ); - SRenderPass* getPass( uint32 i ); - uint32 getPassCount(){ return passes.size(); } - void getPassList( std::vector< std::string > &l ); - - private: - std::vector< SRenderPass* > passes; - }; -} - -#endif - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef DYN_MATERIAL_H +#define DYN_MATERIAL_H + +#include "nel/misc/stream.h" +#include "nel/misc/variant.h" +#include +#include + +namespace NL3D +{ + /// Rendering property + struct SDynMaterialProp + { + enum EPropertyType + { + Color, + Vector4, + Float, + Double, + Int, + Uint, + Matrix4, + Texture + }; + + /// Id of the property + std::string prop; + + /// Label of the property ( user-friendly name ) + std::string label; + + /// type of the property ( see EPropertyType ) + uint8 type; + + NLMISC::CVariant value; + + void serial( NLMISC::IStream &f ); + }; + + + /// Rendering pass data, contains the rendering properties for the pass + struct SRenderPass + { + public: + void addProperty( const SDynMaterialProp &prop ); + void removeProperty( const std::string &name ); + bool changeProperty( const std::string &name, const SDynMaterialProp &prop ); + void setName( const std::string &n ){ name = n; } + void getName( std::string &n ) const { n = name; } + void getShaderRef( std::string &s ) const{ s = shaderRef; } + void setShaderRef( const std::string &s ){ shaderRef = s; } + void serial( NLMISC::IStream &f ); + + /// Returns the number of properties this pass has + uint32 count(){ return properties.size(); } + + /// Clears all properties + void clear(){ properties.clear(); } + + const SDynMaterialProp* getProperty( uint32 i ) const; + + private: + std::vector< SDynMaterialProp > properties; + std::string shaderRef; + std::string name; + }; + + + + /// Multi-pass material for rendering using user shaders + class CDynMaterial : public NLMISC::IStreamable + { + public: + CDynMaterial(); + ~CDynMaterial(); + CDynMaterial& operator=( const CDynMaterial &other ); + + /// Clears all passes, and then adds a new pass + void reconstruct(); + + /// Clears all passes + void clear(); + void serial( NLMISC::IStream &f ); + std::string getClassName(){ return "CDynMaterial"; } + + void addPass( const SRenderPass &pass ); + void removePass( const std::string &name ); + void renamePass( const std::string &from, const std::string &to ); + void movePassUp( const std::string &name ); + void movePassDown( const std::string &name ); + + SRenderPass* getPass( const std::string &name ); + SRenderPass* getPass( uint32 i ); + uint32 getPassCount(){ return passes.size(); } + void getPassList( std::vector< std::string > &l ); + + private: + std::vector< SRenderPass* > passes; + }; +} + +#endif + + diff --git a/code/nel/include/nel/3d/shape_material_serializer.h b/code/nel/include/nel/3d/shape_material_serializer.h index cb42b5d6e..b505bbbc6 100644 --- a/code/nel/include/nel/3d/shape_material_serializer.h +++ b/code/nel/include/nel/3d/shape_material_serializer.h @@ -1,37 +1,37 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef SHAPE_MAT_SERIALIZER_H -#define SHAPE_MAT_SERIALIZER_H - -namespace NL3D -{ - class IShape; - - class ShapeMatSerial - { - public: - ShapeMatSerial(); - ~ShapeMatSerial(); - void setShape( IShape *s ){ shape = s; } - void serial( const char *sPath ); - - private: - IShape *shape; - }; -} - -#endif +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef SHAPE_MAT_SERIALIZER_H +#define SHAPE_MAT_SERIALIZER_H + +namespace NL3D +{ + class IShape; + + class ShapeMatSerial + { + public: + ShapeMatSerial(); + ~ShapeMatSerial(); + void setShape( IShape *s ){ shape = s; } + void serial( const char *sPath ); + + private: + IShape *shape; + }; +} + +#endif diff --git a/code/nel/include/nel/3d/usr_shader_loader.h b/code/nel/include/nel/3d/usr_shader_loader.h index 7066d4246..d4d5a9c78 100644 --- a/code/nel/include/nel/3d/usr_shader_loader.h +++ b/code/nel/include/nel/3d/usr_shader_loader.h @@ -1,44 +1,44 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef SHADER_LOADER_H -#define SHADER_LOADER_H - -#include - -namespace NL3D -{ - class CUsrShaderManager; - - class CUsrShaderLoader - { - public: - CUsrShaderLoader(); - ~CUsrShaderLoader(); - void setManager( CUsrShaderManager *mgr ){ manager = mgr; } - void loadShaders( const std::string &directory ); - - private: - void loadShader( const std::string &file ); - - CUsrShaderManager *manager; - }; -} - - -#endif - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef SHADER_LOADER_H +#define SHADER_LOADER_H + +#include + +namespace NL3D +{ + class CUsrShaderManager; + + class CUsrShaderLoader + { + public: + CUsrShaderLoader(); + ~CUsrShaderLoader(); + void setManager( CUsrShaderManager *mgr ){ manager = mgr; } + void loadShaders( const std::string &directory ); + + private: + void loadShader( const std::string &file ); + + CUsrShaderManager *manager; + }; +} + + +#endif + diff --git a/code/nel/include/nel/3d/usr_shader_manager.h b/code/nel/include/nel/3d/usr_shader_manager.h index 48be96abc..0afe4ce27 100644 --- a/code/nel/include/nel/3d/usr_shader_manager.h +++ b/code/nel/include/nel/3d/usr_shader_manager.h @@ -1,65 +1,65 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef USR_SHADER_MANAGER_H -#define USR_SHADER_MANAGER_H - -#include -#include -#include - -namespace NL3D -{ - class CUsrShaderProgram; - class IUsrShaderVisitor; - - /// Manages user defined shaders ( add, remove, change ) - class CUsrShaderManager - { - public: - CUsrShaderManager(); - ~CUsrShaderManager(); - - /// Throw away the currently loaded shaders - void clear(); - - /// Returns a list of the currently loaded shaders - void getShaderList( std::vector< std::string > &v ); - - /// Adds a shader - bool addShader( CUsrShaderProgram *program ); - - /// Removes a shader - bool removeShader( const std::string &name ); - - /// Changes a shader - bool changeShader( const std::string &name, CUsrShaderProgram *program ); - - /// Looks up and returns a shader - bool getShader( const std::string &name, CUsrShaderProgram *program ); - - - void visitShaders( IUsrShaderVisitor *visitor ); - void visitShader( const std::string &name, IUsrShaderVisitor *visitor ); - - private: - std::map< std::string, CUsrShaderProgram* > programs; - }; -} - -#endif - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef USR_SHADER_MANAGER_H +#define USR_SHADER_MANAGER_H + +#include +#include +#include + +namespace NL3D +{ + class CUsrShaderProgram; + class IUsrShaderVisitor; + + /// Manages user defined shaders ( add, remove, change ) + class CUsrShaderManager + { + public: + CUsrShaderManager(); + ~CUsrShaderManager(); + + /// Throw away the currently loaded shaders + void clear(); + + /// Returns a list of the currently loaded shaders + void getShaderList( std::vector< std::string > &v ); + + /// Adds a shader + bool addShader( CUsrShaderProgram *program ); + + /// Removes a shader + bool removeShader( const std::string &name ); + + /// Changes a shader + bool changeShader( const std::string &name, CUsrShaderProgram *program ); + + /// Looks up and returns a shader + bool getShader( const std::string &name, CUsrShaderProgram *program ); + + + void visitShaders( IUsrShaderVisitor *visitor ); + void visitShader( const std::string &name, IUsrShaderVisitor *visitor ); + + private: + std::map< std::string, CUsrShaderProgram* > programs; + }; +} + +#endif + diff --git a/code/nel/include/nel/3d/usr_shader_program.h b/code/nel/include/nel/3d/usr_shader_program.h index 35e424246..705ba3385 100644 --- a/code/nel/include/nel/3d/usr_shader_program.h +++ b/code/nel/include/nel/3d/usr_shader_program.h @@ -1,71 +1,71 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef SHADER_PROGRAM_H -#define SHADER_PROGRAM_H - -#include "nel/misc/stream.h" -#include - -namespace NL3D -{ - class CUsrShaderProgram : public NLMISC::IStreamable - { - public: - CUsrShaderProgram(); - - ~CUsrShaderProgram(); - - std::string getClassName(){ return "CUsrShaderProgram"; } - - void serial( NLMISC::IStream &f ); - - void getName( std::string &n ) const{ n = name; } - void getDescription( std::string &d ) const{ d = description; } - void getVP( std::string &vp ) const{ vp = vertexProgram; } - void getFP( std::string &fp ) const{ fp = fragmentProgram; } - - void setName( const std::string &n ){ name = n; } - void setDescription( const std::string &d ){ description = d; } - void setVP( const std::string &vp ){ vertexProgram = vp; } - void setFP( const std::string &fp ){ fragmentProgram = fp; } - - uint32 getVPId() const{ return vpId; } - uint32 getFPId() const{ return fpId; } - uint32 getPId() const{ return pId; } - - void setVPId( uint32 id ){ vpId = id; } - void setFPId( uint32 id ){ fpId = id;} - void setPid( uint32 id ){ pId = id; } - - private: - std::string name; - std::string description; - std::string vertexProgram; - std::string fragmentProgram; - - - uint32 vpId; - uint32 fpId; - uint32 pId; - }; -} - - -#endif - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef SHADER_PROGRAM_H +#define SHADER_PROGRAM_H + +#include "nel/misc/stream.h" +#include + +namespace NL3D +{ + class CUsrShaderProgram : public NLMISC::IStreamable + { + public: + CUsrShaderProgram(); + + ~CUsrShaderProgram(); + + std::string getClassName(){ return "CUsrShaderProgram"; } + + void serial( NLMISC::IStream &f ); + + void getName( std::string &n ) const{ n = name; } + void getDescription( std::string &d ) const{ d = description; } + void getVP( std::string &vp ) const{ vp = vertexProgram; } + void getFP( std::string &fp ) const{ fp = fragmentProgram; } + + void setName( const std::string &n ){ name = n; } + void setDescription( const std::string &d ){ description = d; } + void setVP( const std::string &vp ){ vertexProgram = vp; } + void setFP( const std::string &fp ){ fragmentProgram = fp; } + + uint32 getVPId() const{ return vpId; } + uint32 getFPId() const{ return fpId; } + uint32 getPId() const{ return pId; } + + void setVPId( uint32 id ){ vpId = id; } + void setFPId( uint32 id ){ fpId = id;} + void setPid( uint32 id ){ pId = id; } + + private: + std::string name; + std::string description; + std::string vertexProgram; + std::string fragmentProgram; + + + uint32 vpId; + uint32 fpId; + uint32 pId; + }; +} + + +#endif + + diff --git a/code/nel/include/nel/3d/usr_shader_saver.h b/code/nel/include/nel/3d/usr_shader_saver.h index 7ab2d5ce3..de1704ce4 100644 --- a/code/nel/include/nel/3d/usr_shader_saver.h +++ b/code/nel/include/nel/3d/usr_shader_saver.h @@ -1,50 +1,50 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef USR_SHADER_SAVER_H -#define USR_SHADER_SAVER_H - -#include "nel/3d/usr_shader_visitor.h" -#include - -namespace NL3D -{ - class CUsrShaderProgram; - class CUsrShaderManager; - - class CUsrShaderSaver : public IUsrShaderVisitor - { - public: - CUsrShaderSaver(); - ~CUsrShaderSaver(); - - void setManager( CUsrShaderManager *mgr ){ manager = mgr; } - - void visit( CUsrShaderProgram *program ); - - void saveShaders( const std::string &directory ); - void saveShader( const std::string &directory, const std::string &name ); - - private: - CUsrShaderManager *manager; - std::string outputDir; - }; -} - -#endif - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef USR_SHADER_SAVER_H +#define USR_SHADER_SAVER_H + +#include "nel/3d/usr_shader_visitor.h" +#include + +namespace NL3D +{ + class CUsrShaderProgram; + class CUsrShaderManager; + + class CUsrShaderSaver : public IUsrShaderVisitor + { + public: + CUsrShaderSaver(); + ~CUsrShaderSaver(); + + void setManager( CUsrShaderManager *mgr ){ manager = mgr; } + + void visit( CUsrShaderProgram *program ); + + void saveShaders( const std::string &directory ); + void saveShader( const std::string &directory, const std::string &name ); + + private: + CUsrShaderManager *manager; + std::string outputDir; + }; +} + +#endif + + diff --git a/code/nel/include/nel/3d/usr_shader_visitor.h b/code/nel/include/nel/3d/usr_shader_visitor.h index f8d655e0e..4a0a6e9a2 100644 --- a/code/nel/include/nel/3d/usr_shader_visitor.h +++ b/code/nel/include/nel/3d/usr_shader_visitor.h @@ -1,40 +1,40 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef USR_SHADER_VISITOR_H -#define USR_SHADER_VISITOR_H - -namespace NL3D -{ - class CUsrShaderProgram; - - class IUsrShaderVisitor - { - public: - IUsrShaderVisitor(){} - ~IUsrShaderVisitor(){} - - virtual void visit( CUsrShaderProgram *program ) = 0; - - }; - -} - - -#endif - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef USR_SHADER_VISITOR_H +#define USR_SHADER_VISITOR_H + +namespace NL3D +{ + class CUsrShaderProgram; + + class IUsrShaderVisitor + { + public: + IUsrShaderVisitor(){} + ~IUsrShaderVisitor(){} + + virtual void visit( CUsrShaderProgram *program ) = 0; + + }; + +} + + +#endif + + diff --git a/code/nel/include/nel/misc/variant.h b/code/nel/include/nel/misc/variant.h index 5c617f5d1..140d878dd 100644 --- a/code/nel/include/nel/misc/variant.h +++ b/code/nel/include/nel/misc/variant.h @@ -1,109 +1,109 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef NLVARIANT_H -#define NLVARIANT_H - -#include "nel/misc/stream.h" -#include - -#define VARIANT_VVAL_END 16 - -namespace NLMISC -{ - class CVariant : public IStreamable - { - public: - - enum EVarType - { - Float, - Double, - Int, - UInt, - String, - Vector4, - Matrix4 - }; - - CVariant(); - ~CVariant(); - - std::string getClassName(){ return "CVariant"; } - - void serial( IStream &f ); - - void setDouble( double d ){ - type = Double; - uvalue.dval = d; - } - - void setFloat( float f ){ - type = Float; - uvalue.fval = f; - } - - void setInt( int i ){ - type = Int; - uvalue.ival = i; - } - - void setUInt( unsigned int u ){ - type = UInt; - uvalue.uval = u; - } - - void setString( const std::string &s ){ - type = String; - sval = s; - } - - void setVector4( const float *v ); - void setMatrix4( const float *m ); - - double toDouble() const{ return uvalue.dval; } - float toFloat() const{ return uvalue.fval; } - int toInt() const{ return uvalue.ival; } - unsigned int toUInt() const{ return uvalue.uval; } - std::string toString() const{ return sval; } - void getVector4( float *v ) const; - void getMatrix4( float *m ) const; - - bool valueAsString( std::string &s ) const; - void fromString( const std::string &s, EVarType t ); - - EVarType getType() const{ return type; } - - private: - - union{ - double dval; - float fval; - int ival; - unsigned int uval; - float vval[ VARIANT_VVAL_END ]; - }uvalue; - - std::string sval; - - EVarType type; - }; -} - - -#endif - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef NLVARIANT_H +#define NLVARIANT_H + +#include "nel/misc/stream.h" +#include + +#define VARIANT_VVAL_END 16 + +namespace NLMISC +{ + class CVariant : public IStreamable + { + public: + + enum EVarType + { + Float, + Double, + Int, + UInt, + String, + Vector4, + Matrix4 + }; + + CVariant(); + ~CVariant(); + + std::string getClassName(){ return "CVariant"; } + + void serial( IStream &f ); + + void setDouble( double d ){ + type = Double; + uvalue.dval = d; + } + + void setFloat( float f ){ + type = Float; + uvalue.fval = f; + } + + void setInt( int i ){ + type = Int; + uvalue.ival = i; + } + + void setUInt( unsigned int u ){ + type = UInt; + uvalue.uval = u; + } + + void setString( const std::string &s ){ + type = String; + sval = s; + } + + void setVector4( const float *v ); + void setMatrix4( const float *m ); + + double toDouble() const{ return uvalue.dval; } + float toFloat() const{ return uvalue.fval; } + int toInt() const{ return uvalue.ival; } + unsigned int toUInt() const{ return uvalue.uval; } + std::string toString() const{ return sval; } + void getVector4( float *v ) const; + void getMatrix4( float *m ) const; + + bool valueAsString( std::string &s ) const; + void fromString( const std::string &s, EVarType t ); + + EVarType getType() const{ return type; } + + private: + + union{ + double dval; + float fval; + int ival; + unsigned int uval; + float vval[ VARIANT_VVAL_END ]; + }uvalue; + + std::string sval; + + EVarType type; + }; +} + + +#endif + diff --git a/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp b/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp index ba8e8e3ec..3bc8c9260 100644 --- a/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp @@ -1,1531 +1,1531 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "sstream" -#include "driver_glsl_shader_generator.h" -#include "nel/3d/vertex_buffer.h" -#include "driver_opengl_shader_desc.h" - -namespace -{ - inline bool hasFlag( uint32 data, uint32 flag ) - { - if( ( data & flag ) != 0 ) - return true; - else - return false; - } - - enum AttribOffset - { - Position, - Weight, - Normal, - PrimaryColor, - SecondaryColor, - Fog, - PaletteSkin, - Empty, - TexCoord0, - TexCoord1, - TexCoord2, - TexCoord3, - TexCoord4, - TexCoord5, - TexCoord6, - TexCoord7, - NumOffsets - }; -} - -namespace NL3D -{ - uint16 vertexFlags[ CVertexBuffer::NumValue ] = - { - CVertexBuffer::PositionFlag, - CVertexBuffer::WeightFlag, - CVertexBuffer::NormalFlag, - CVertexBuffer::PrimaryColorFlag, - CVertexBuffer::SecondaryColorFlag, - CVertexBuffer::FogFlag, - CVertexBuffer::PaletteSkinFlag, - 0, - CVertexBuffer::TexCoord0Flag, - CVertexBuffer::TexCoord1Flag, - CVertexBuffer::TexCoord2Flag, - CVertexBuffer::TexCoord3Flag, - CVertexBuffer::TexCoord4Flag, - CVertexBuffer::TexCoord5Flag, - CVertexBuffer::TexCoord6Flag, - CVertexBuffer::TexCoord7Flag - }; - - const char *attribNames[ CVertexBuffer::NumValue ] = - { - "position", - "weight", - "normal", - "color", - "color2", - "fog", - "paletteSkin", - "none", - "texCoord0", - "texCoord1", - "texCoord2", - "texCoord3", - "texCoord4", - "texCoord5", - "texCoord6", - "texCoord7" - }; - - const char *texelNames[ SHADER_MAX_TEXTURES ] = - { - "texel0", - "texel1", - "texel2", - "texel3" - }; - - const char *constantNames[ 4 ] = - { - "constant0", - "constant1", - "constant2", - "constant3" - }; - - const char *shaderNames[] = - { - "Normal", - "Bump", - "Usercolor", - "Lightmap", - "Specular", - "Caustics", - "Per-Pixel Lighting", - "Per-Pixel Lighting, no specular", - "Cloud", - "Water" - }; - - CGLSLShaderGenerator::CGLSLShaderGenerator() - { - reset(); - } - - CGLSLShaderGenerator::~CGLSLShaderGenerator() - { - } - - void CGLSLShaderGenerator::reset() - { - material = NULL; - vbFormat = 0; - desc = NULL; - ss.str( "" ); - ss.clear(); - } - - void CGLSLShaderGenerator::generateVS( std::string &vs ) - { - ss.str( "" ); - ss.clear(); - ss << "// " << shaderNames[ material->getShader() ] << " Vertex Shader" << std::endl; - ss << std::endl; - - ss << "#version 330" << std::endl; - ss << "#extension GL_ARB_separate_shader_objects : enable" << std::endl; - ss << "out gl_PerVertex" << std::endl; - ss << "{" << std::endl; - ss << "vec4 gl_Position;" << std::endl; - ss << "};" << std::endl; - ss << std::endl; - ss << "uniform mat4 modelViewProjection;" << std::endl; - ss << std::endl; - - for( int i = Position; i < NumOffsets; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << "layout ( location = "; - ss << i; - ss << " ) "; - ss << "in vec4 "; - ss << "v" << attribNames[ i ]; - ss << ";"; - ss << std::endl; - } - } - ss << std::endl; - - for( int i = Weight; i < NumOffsets; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << "smooth out vec4 "; - ss << attribNames[ i ] << ";" << std::endl; - } - } - ss << std::endl; - - if( !desc->useTextures() ) - { - addAmbient(); - addDiffuse(); - addSpecular(); - } - - switch( material->getShader() ) - { - case CMaterial::Normal: - case CMaterial::UserColor: - case CMaterial::LightMap: - case CMaterial::Cloud: - generateNormalVS(); - break; - - case CMaterial::Specular: - generateSpecularVS(); - break; - - case CMaterial::PerPixelLighting: - case CMaterial::PerPixelLightingNoSpec: - generatePPLVS(); - break; - - case CMaterial::Water: - generateWaterVS(); - break; - } - - vs.assign( ss.str() ); - } - - void CGLSLShaderGenerator::generatePS( std::string &ps ) - { - ss.str( "" ); - ss.clear(); - - ss << "// " << shaderNames[ material->getShader() ] << " Pixel Shader" << std::endl; - ss << std::endl; - - ss << "#version 330" << std::endl; - ss << std::endl; - - ss << "out vec4 fragColor;" << std::endl; - - for( int i = Weight; i < NumOffsets; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << "smooth in vec4 "; - ss << attribNames[ i ] << ";" << std::endl; - } - } - ss << std::endl; - - switch( material->getShader() ) - { - case CMaterial::Normal: - case CMaterial::UserColor: - generateNormalPS(); - break; - - case CMaterial::LightMap: - generateLightMapPS(); - break; - - case CMaterial::Specular: - generateSpecularPS(); - break; - - case CMaterial::PerPixelLighting: - case CMaterial::PerPixelLightingNoSpec: - generatePPLPS(); - break; - - case CMaterial::Water: - generateWaterPS(); - break; - - case CMaterial::Cloud: - generateCloudPS(); - break; - } - - ps.assign( ss.str() ); - } - - void CGLSLShaderGenerator::addAmbient() - { - ss << "uniform vec4 ambientColor;" << std::endl; - } - - void CGLSLShaderGenerator::addDiffuse() - { - ss << "uniform vec4 diffuseColor;" << std::endl; - } - - void CGLSLShaderGenerator::addSpecular() - { - ss << "uniform vec4 specularColor;" << std::endl; - } - - void CGLSLShaderGenerator::addColor() - { - ss << "uniform vec4 materialColor;" << std::endl; - } - - void CGLSLShaderGenerator::addConstants() - { - ss << "uniform vec4 constant0;" << std::endl; - ss << "uniform vec4 constant1;" << std::endl; - ss << "uniform vec4 constant2;" << std::endl; - ss << "uniform vec4 constant3;" << std::endl; - } - - void CGLSLShaderGenerator::addNormalMatrix() - { - ss << "uniform mat3 normalMatrix;" << std::endl; - } - - void CGLSLShaderGenerator::addViewMatrix() - { - ss << "uniform mat4 viewMatrix;" << std::endl; - } - - void CGLSLShaderGenerator::addAlphaTreshold() - { - ss << "uniform float alphaTreshold;" << std::endl; - } - - void CGLSLShaderGenerator::addAlphaTest() - { - if( material->getAlphaTest() ) - ss << "if( fragColor.a <= ( alphaTreshold - 0.0001 ) ) discard;" << std::endl; - } - - void CGLSLShaderGenerator::addFogUniform() - { - if( !desc->fogEnabled() ) - return; - - ss << "uniform float fogStart;" << std::endl; - ss << "uniform float fogEnd;" << std::endl; - ss << "uniform vec4 fogColor;" << std::endl; - - if( desc->getFogMode() == CShaderDesc::Linear ) - return; - - ss << "uniform float fogDensity;" << std::endl; - } - - void CGLSLShaderGenerator::addFogFunction() - { - if( !desc->fogEnabled() ) - return; - - switch( desc->getFogMode() ) - { - case CShaderDesc::Linear: - ss << "vec4 applyFog( vec4 col )" << std::endl; - ss << "{" << std::endl; - ss << "float z = ecPos.z / ecPos.w;" << std::endl; - ss << "z = abs( z );" << std::endl; - ss << "float fogFactor = ( fogEnd - z ) / ( fogEnd - fogStart );" << std::endl; - ss << "fogFactor = clamp( fogFactor, 0.0, 1.0 );" << std::endl; - ss << "vec4 fColor = mix( fogColor, col, fogFactor );" << std::endl; - ss << "return fColor;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - break; - } - } - - void CGLSLShaderGenerator::addFog() - { - ss << "fragColor = applyFog( fragColor );" << std::endl; - } - - - /////////////////////////////////////////////////////////// Lights //////////////////////////////////////////////////////////////////// - - void CGLSLShaderGenerator::addLightUniformsVS() - { - for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) - { - switch( desc->getLight( i ) ) - { - case CShaderDesc::Nolight: - continue; - break; - - case CShaderDesc::Directional: - ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl; - ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; - ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; - ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; - ss << "uniform float light" << i << "Shininess;" << std::endl; - break; - - case CShaderDesc::Point: - ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl; - ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; - ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; - ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; - ss << "uniform float light" << i << "Shininess;" << std::endl; - ss << "uniform float light" << i << "ConstAttn;" << std::endl; - ss << "uniform float light" << i << "LinAttn;" << std::endl; - ss << "uniform float light" << i << "QuadAttn;" << std::endl; - break; - } - } - } - - void CGLSLShaderGenerator::addLightUniformsFS() - { - for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) - { - switch( desc->getLight( i ) ) - { - case CShaderDesc::Nolight: - continue; - break; - - case CShaderDesc::Directional: - break; - } - } - - } - - void CGLSLShaderGenerator::addLightOutsVS() - { - ss << "smooth out vec4 lightColor;" << std::endl; - } - - void CGLSLShaderGenerator::addLightInsFS() - { - ss << "smooth in vec4 lightColor;" << std::endl; - } - - void CGLSLShaderGenerator::addDirectionalFunctionVS( int num ) - { - ss << "float getIntensity" << num << "( vec3 normal3, vec3 lightDir )" << std::endl; - ss << "{" << std::endl; - ss << "float angle = dot( lightDir, normal3 );" << std::endl; - ss << "angle = max( 0.0, angle );" << std::endl; - ss << "return angle;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - - ss << "float getSpecIntensity" << num << "( vec3 normal3, vec3 lightDir )" << std::endl; - ss << "{" << std::endl; - ss << "vec3 halfVector = normalize( lightDir + normal3 );" << std::endl; - ss << "float angle = dot( normal3, halfVector );" << std::endl; - ss << "angle = max( 0.0, angle );" << std::endl; - ss << "float si = pow( angle, light" << num << "Shininess );" << std::endl; - ss << "return si;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - - ss << "vec4 getLight" << num << "Color()" << std::endl; - ss << "{" << std::endl; - ss << "vec4 lightDir4 = viewMatrix * vec4( light" << num << "DirOrPos, 1.0 );" << std::endl; - ss << "vec3 lightDir = lightDir4.xyz / lightDir4.w;" << std::endl; - ss << "lightDir = normalize( lightDir );" << std::endl; - ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; - ss << "normal3 = normalMatrix * normal3;" << std::endl; - ss << "normal3 = normalize( normal3 );" << std::endl; - - if( desc->useTextures() || ( material->getShader() == CMaterial::LightMap ) ) - { - ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff + "; - ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec + "; - ss << "light" << num << "ColAmb;" << std::endl; - } - else - { - ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff * diffuseColor + "; - ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec * specularColor + "; - ss << "light" << num << "ColAmb * ambientColor;" << std::endl; - } - - ss << "return lc;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - } - - void CGLSLShaderGenerator::addPointLightFunctionVS( int num ) - { - ss << "float getIntensity" << num << "( vec3 normal3, vec3 direction3 )" << std::endl; - ss << "{" << std::endl; - ss << "float angle = dot( direction3, normal3 );" << std::endl; - ss << "angle = max( 0.0, angle );" << std::endl; - ss << "return angle;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - - ss << "float getSpecIntensity" << num << "( vec3 normal3, vec3 direction3 )" << std::endl; - ss << "{" << std::endl; - ss << "vec3 halfVector = normalize( direction3 + normal3 );" << std::endl; - ss << "float angle = dot( normal3, halfVector );" << std::endl; - ss << "angle = max( 0.0, angle );" << std::endl; - ss << "float si = pow( angle, light" << num << "Shininess );" << std::endl; - ss << "return si;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - - ss << "vec4 getLight" << num << "Color()" << std::endl; - ss << "{" << std::endl; - ss << "vec3 ecPos3 = ecPos4.xyz / ecPos4.w;" << std::endl; - ss << "vec4 lightPos4 = viewMatrix * vec4( light" << num << "DirOrPos, 1.0 );" << std::endl; - ss << "vec3 lightPos = lightPos4.xyz / lightPos4.w;" << std::endl; - ss << "vec3 lightDirection = lightPos - ecPos3;" << std::endl; - ss << "float lightDistance = length( lightDirection );" << std::endl; - ss << "lightDirection = normalize( lightDirection );" << std::endl; - - ss << "float attenuation = light" << num << "ConstAttn + "; - ss << "light" << num << "LinAttn * lightDistance +"; - ss << "light" << num << "QuadAttn * lightDistance * lightDistance;" << std::endl; - - ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; - ss << "normal3 = normalMatrix * normal3;" << std::endl; - ss << "normal3 = normalize( normal3 );" << std::endl; - - if( desc->useTextures() || ( material->getShader() == CMaterial::LightMap ) ) - { - ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff + "; - ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec + "; - ss << "light" << num << "ColAmb;" << std::endl; - } - else - { - ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff * diffuseColor+ "; - ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec * specularColor + "; - ss << "light" << num << "ColAmb * ambientColor;" << std::endl; - } - - ss << "lc = lc / attenuation;" << std::endl; - ss << "return lc;" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - } - - void CGLSLShaderGenerator::addLightsFunctionVS() - { - for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) - { - switch( desc->getLight( i ) ) - { - case CShaderDesc::Nolight: - continue; - break; - - case CShaderDesc::Directional: - addDirectionalFunctionVS( i ); - break; - - case CShaderDesc::Point: - addPointLightFunctionVS( i ); - break; - - } - - } - } - - void CGLSLShaderGenerator::addLightsFunctionFS() - { - ss << "vec4 applyLights( vec4 col )" << std::endl; - ss << "{" << std::endl; - ss << "return col * lightColor;" << std::endl; - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::addLightsVS() - { - ss << "lightColor = vec4( 0.0, 0.0, 0.0, 1.0 );" << std::endl; - - for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) - { - if( desc->getLight( i ) == CShaderDesc::Nolight ) - continue; - - ss << "lightColor = lightColor + getLight" << i << "Color();" << std::endl; - } - } - - void CGLSLShaderGenerator::addLightsFS() - { - ss << "fragColor = applyLights( fragColor );" << std::endl; - } - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - void CGLSLShaderGenerator::generateNormalVS() - { - if( desc->fogEnabled() || desc->hasPointLight() ) - { - ss << "uniform mat4 modelView;" << std::endl; - } - if( desc->lightingEnabled() ) - { - addViewMatrix(); - } - if( desc->fogEnabled() || desc->hasPointLight() ) - { - ss << "vec4 ecPos4;" << std::endl; - } - - if( desc->fogEnabled() ) - ss << "smooth out vec4 ecPos;" << std::endl; - - ss << std::endl; - - if( desc->lightingEnabled() ) - { - addNormalMatrix(); - addLightUniformsVS(); - addLightOutsVS(); - ss << std::endl; - - addLightsFunctionVS(); - ss << std::endl; - } - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - - ss << "gl_Position = modelViewProjection * " << "v" << attribNames[ 0 ] << ";" << std::endl; - - if( desc->fogEnabled() || desc->hasPointLight() ) - ss << "ecPos4 = modelView * v" << attribNames[ 0 ] << ";" << std::endl; - - if( desc->fogEnabled() ) - ss << "ecPos = ecPos4;" << std::endl; - - if( desc->lightingEnabled() ) - addLightsVS(); - - for( int i = Weight; i < NumOffsets; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << attribNames[ i ]; - ss << " = "; - ss << "v" << attribNames[ i ] << ";" << std::endl; - } - } - - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::generateSpecularVS() - { - - ss << "uniform mat4 modelView;" << std::endl; - ss << "uniform mat4 texMatrix0;" << std::endl; - ss << "smooth out vec3 cubeTexCoords;" << std::endl; - - if( desc->fogEnabled() || desc->hasPointLight() ) - { - ss << "vec4 ecPos4;" << std::endl; - } - - if( desc->fogEnabled() ) - ss << "smooth out vec4 ecPos;" << std::endl; - ss << std::endl; - - if( desc->lightingEnabled() ) - { - addViewMatrix(); - addNormalMatrix(); - addLightUniformsVS(); - addLightOutsVS(); - ss << std::endl; - - addLightsFunctionVS(); - ss << std::endl; - } - - ss << "vec3 ReflectionMap( const in vec3 eyePos, const in vec3 normal )" << std::endl; - ss << "{" << std::endl; - ss << "vec3 u = normalize( eyePos );" << std::endl; - ss << "return reflect( u, normal );" << std::endl; - ss << "}" << std::endl; - ss << std::endl; - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - ss << "vec4 eyePosition = modelView * v" << attribNames[ 0 ] << ";" << std::endl; - - if( desc->hasPointLight() ) - ss << "ecPos4 = eyePosition;" << std::endl; - - if( desc->fogEnabled() ) - ss << "ecPos = eyePosition;" << std::endl; - - ss << "vec3 ep = eyePosition.xyz / eyePosition.w;" << std::endl; - ss << "vec3 n = vnormal.xyz;" << std::endl; - ss << "cubeTexCoords = ReflectionMap( ep, n );" << std::endl; - ss << "vec4 t = vec4( cubeTexCoords, 1.0 );" << std::endl; - ss << "t = t * texMatrix0;" << std::endl; - ss << "cubeTexCoords = t.xyz;" << std::endl; - ss << "gl_Position = modelViewProjection * v" << attribNames[ 0 ] << ";" << std::endl; - - if( desc->lightingEnabled() ) - addLightsVS(); - - for( int i = Weight; i < NumOffsets; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << attribNames[ i ]; - ss << " = "; - ss << "v" << attribNames[ i ] << ";" << std::endl; - } - } - - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::generatePPLVS() - { - ss << "uniform vec4 lightPosition;" << std::endl; - - if( material->getShader() == CMaterial::PerPixelLighting ) - { - ss << "uniform vec4 viewerPos;" << std::endl; - ss << "uniform mat4 invModelMat;" << std::endl; - - } - ss << std::endl; - - ss << "smooth out vec3 cubeTexCoords0;" << std::endl; - - if( material->getShader() == CMaterial::PerPixelLighting ) - ss << "smooth out vec3 cubeTexCoords2;" << std::endl; - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - ss << "gl_Position = modelViewProjection * v" << attribNames[ 0 ] << ";" << std::endl; - - for( int i = Weight; i < NumOffsets; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - { - ss << attribNames[ i ]; - ss << " = "; - ss << "v" << attribNames[ i ] << ";" << std::endl; - } - } - - ss << "vec4 n = normalize( vnormal ); //normalized normal" << std::endl; - ss << "vec4 T; // second basis, Tangent" << std::endl; - ss << "T = texCoord1;" << std::endl; - ss << "T = T - n * dot( N, T ); // Gramm-Schmidt process" << std::endl; - ss << "T = normalize( T );" << std::endl; - ss << "vec4 B;" << std::endl; - ss << "B.xyz = cross( n.xyz, T.xyz ); // B = N x T" << std::endl; - ss << "vec4 L = lightPos - vposition; //Inverse light vector" << std::endl; - ss << "L = normalize( L );" << std::endl; - ss << "L * [ T B N ]" << std::endl; - ss << "cubeTexCoords0.x = dot( T.xyz, L.xyz );" << std::endl; - ss << "cubeTexCoords0.y = dot( B.xyz, L.xyz );" << std::endl; - ss << "cubeTexCoords0.z = dot( n.xyz, L.xyz );" << std::endl; - - if( material->getShader() == CMaterial::PerPixelLighting ) - { - ss << "vec4 V = invModelMat * viewerPos - vposition;" << std::endl; - ss << "V = normalize( V );" << std::endl; - ss << "vec4 H = L + V; // half-angle" << std::endl; - ss << "vec4 H = normalize( H );" << std::endl; - ss << "cubeTexCoords2.x = dot( T, H );" << std::endl; - ss << "cubeTexCoords2.y = dot( B, H );" << std::endl; - ss << "cubeTexCoords2.w = dot( n, H );" << std::endl; - } - - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::generateWaterVS() - { - bool diffuse = false; - if( material->getTexture( 3 ) ) - diffuse = true; - - ss << "smooth out vec4 texCoord0;" << std::endl; - ss << "smooth out vec4 texCoord1;" << std::endl; - ss << "smooth out vec4 texCoord2;" << std::endl; - ss << "flat out vec4 bump0ScaleBias;" << std::endl; - ss << "flat out vec4 bump1ScaleBias;" << std::endl; - - if( diffuse ) - ss << "smooth out vec4 texCoord3;" << std::endl; - - ss << std::endl; - - if( diffuse ) - { - ss << "uniform vec4 diffuseMapVector0;" << std::endl; - ss << "uniform vec4 diffuseMapVector1;" << std::endl; - } - - ss << "uniform vec4 bumpMap0Scale;" << std::endl; - ss << "uniform vec4 bumpMap1Scale;" << std::endl; - ss << "uniform vec4 bumpMap0Offset;" << std::endl; - ss << "uniform vec4 bumpMap1Offset;" << std::endl; - - // no fog yet - //ss << "uniform mat4 modelView;" << std::endl; - ss << std::endl; - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - ss << "position = vposition;" << std::endl; - ss << "gl_Position = modelViewProjection * position;" << std::endl; - ss << "bump0ScaleBias = bumpMap0Scale;" << std::endl; - ss << "bump1ScaleBias = bumpMap1Scale;" << std::endl; - - // no fog yet - //ss << "vec4 v = modelView[ 3 ];" << std::endl; - //fog.x = dot( position, v ); - - ss << "texCoord0 = position * bumpMap0Scale + bumpMap0Offset;" << std::endl; - ss << "texCoord1 = position * bumpMap1Scale + bumpMap1Offset;" << std::endl; - ss << "vec4 eyeDirection = normalize( eye - position );" << std::endl; - - ss << "texCoord2 = -1 * eyeDirection * vec4( 0.5, 0.05, 0.0, 1.0 ) + vec4( 0.5, 0.05, 0.0, 1.0 );" << std::endl; - - if( diffuse ) - { - ss << "texCoord3.x = dot( position, diffuseMapVector0 );" << std::endl; - ss << "texCoord3.y = dot( position, diffuseMapVector1 );" << std::endl; - } - - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::generateNormalPS() - { - uint sampler = 0; - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - { - if( desc->getUseTexStage( i ) ) - ss << "uniform sampler2D sampler" << sampler << ";" << std::endl; - sampler++; - } - - addColor(); - addConstants(); - addAlphaTreshold(); - addFogUniform(); - - if( desc->fogEnabled() ) - ss << "smooth in vec4 ecPos;" << std::endl; - - ss << std::endl; - - if( desc->lightingEnabled() ) - { - addLightUniformsFS(); - addLightInsFS(); - ss << std::endl; - - addLightsFunctionFS(); - ss << std::endl; - } - - if( desc->fogEnabled() ) - addFogFunction(); - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - - bool textures = false; - sampler = 0; - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - { - if( desc->getUseTexStage( i ) ) - { - ss << "vec4 texel" << sampler << " = texture2D( sampler" << sampler << ","; - - if( !desc->getUseFirstTexCoords() ) - ss << attribNames[ TexCoord0 + i ] << " );"; - else - ss << attribNames[ TexCoord0 ] << " );"; - - ss << std::endl; - - textures = true; - } - sampler++; - } - - bool vertexColor = false; - if( hasFlag( vbFormat, vertexFlags[ PrimaryColor ] ) ) - vertexColor = true; - - if( textures && !vertexColor ) - ss << "vec4 texel = vec4( 1.0, 1.0, 1.0, 1.0 );" << std::endl; - else - if( vertexColor ) - ss << "vec4 texel = color;" << std::endl; - else - ss << "vec4 texel = vec4( 0.5, 0.5, 0.5, 1.0 );" << std::endl; - - generateTexEnv(); - - // This is just an idea I had, but it seems to be working. - // Unfortunately it's not documented anywhere I looked in the GL spec, but if I don't have this modulation here, - // the Ryzom UI looks horrific. - if( vertexColor ) - ss << "texel = color * texel;" << std::endl; - - ss << "fragColor = texel;" << std::endl; - - if( desc->lightingEnabled() ) - addLightsFS(); - - if( desc->fogEnabled() ) - addFog(); - - addAlphaTest(); - - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::generateTexEnv() - { - uint32 stage = 0; - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - { - if( desc->getUseTexStage( i ) ) - { - generateTexEnvRGB( stage ); - generateTexEnvAlpha( stage ); - } - stage++; - } - } - - void CGLSLShaderGenerator::generateTexEnvRGB( unsigned int stage ) - { - std::string arg0; - std::string arg1; - std::string arg2; - - switch( material->_TexEnvs[ stage ].Env.OpRGB ) - { - case CMaterial::Replace: - { - buildArg( stage, 0, false, arg0 ); - ss << "texel.rgb = " << arg0 << ";" << std::endl; - } - break; - - case CMaterial::Modulate: - { - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - ss << "texel.rgb = " << arg0 << " * " << arg1 << ";" << std::endl; - //ss << "texel.rgb = " << arg0 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb * " << arg1 << ";" << std::endl; - } - break; - - case CMaterial::Add: - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - ss << "texel.rgb = " << arg0 << " + " << arg1 << ";" << std::endl; - //ss << "texel.rgb = " << arg0 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb + " << arg1 << ";" << std::endl; - break; - - case CMaterial::AddSigned: - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - ss << "texel.rgb = " << arg0 << " + " << arg1 << " - vec3( 0.5, 0.5, 0.5 );" << std::endl; - //ss << "texel.rgb = " << arg0 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb + " << arg1 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb - vec3( 0.5, 0.5, 0.5 );" << std::endl; - break; - - case CMaterial::Mad: - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - buildArg( stage, 2, false, arg2 ); - ss << "texel.rgb = " << arg0 << " * " << arg1 << " + " << arg2 << ";" << std::endl; - //ss << "texel.rgb = " << arg0 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb * " << arg1 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb + " << arg2 << ";" << std::endl; - break; - - case CMaterial::InterpolateTexture: - { - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - - std::string As = texelNames[ stage ]; - As.append( ".a" ); - - ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - - case CMaterial::InterpolatePrevious: - { - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - - std::string As = "texel.a"; - - ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - - case CMaterial::InterpolateDiffuse: - { - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - - std::string As = "diffuseColor.a"; - - ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - - case CMaterial::InterpolateConstant: - { - buildArg( stage, 0, false, arg0 ); - buildArg( stage, 1, false, arg1 ); - - std::string As = constantNames[ stage ]; - As.append( ".a" ); - - ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - } - } - - void CGLSLShaderGenerator::generateTexEnvAlpha( unsigned int stage ) - { - std::string arg0; - std::string arg1; - std::string arg2; - - switch( material->_TexEnvs[ stage ].Env.OpRGB ) - { - case CMaterial::Replace: - { - buildArg( stage, 0, true, arg0 ); - ss << "texel.a = " << arg0 << ";" << std::endl; - } - break; - - case CMaterial::Modulate: - { - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - ss << "texel.a = " << arg0 << " * " << arg1 << ";" << std::endl; - //ss << "texel.a = " << arg0 << ";" << std::endl; - //ss << "texel.a = texel.a * " << arg1 << ";" << std::endl; - } - break; - - case CMaterial::Add: - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - ss << "texel.a = " << arg0 << " + " << arg1 << ";" << std::endl; - //ss << "texel.a = " << arg0 << ";" << std::endl; - //ss << "texel.a = texel.a + " << arg1 << ";" << std::endl; - break; - - case CMaterial::AddSigned: - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - ss << "texel.a = " << arg0 << " * " << arg1 << " - vec3( 0.5, 0.5, 0.5 );" << std::endl; - //ss << "texel.rgb = " << arg0 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb + " << arg1 << ";" << std::endl; - //ss << "texel.rgb = texel.rgb - vec3( 0.5, 0.5, 0.5 );" << std::endl; - break; - - case CMaterial::Mad: - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - buildArg( stage, 2, true, arg2 ); - ss << "texel.a = " << arg0 << ";" << std::endl; - ss << "texel.a = texel.a * " << arg1 << ";" << std::endl; - ss << "texel.a = texel.a + " << arg2 << ";" << std::endl; - break; - - case CMaterial::InterpolateTexture: - { - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - - std::string As = texelNames[ stage ]; - As.append( ".a" ); - - ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - - case CMaterial::InterpolatePrevious: - { - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - - std::string As = "texel.a"; - - ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - - case CMaterial::InterpolateDiffuse: - { - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - - std::string As = "diffuseColor.a"; - - ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - - case CMaterial::InterpolateConstant: - { - buildArg( stage, 0, true, arg0 ); - buildArg( stage, 1, true, arg1 ); - - std::string As = constantNames[ stage ]; - As.append( ".a" ); - - ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; - ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; - } - break; - } - } - - - void CGLSLShaderGenerator::buildArg( unsigned int stage, unsigned int n, bool alpha, std::string &arg ) - { - uint32 src; - uint32 op; - - if( !alpha ) - { - src = material->_TexEnvs[ n ].getColorArg( n ); - op = material->_TexEnvs[ n ].getColorOperand( n ); - } - else - { - src = material->_TexEnvs[ n ].getAlphaArg( n ); - op = material->_TexEnvs[ n ].getAlphaOperand( n ); - } - - std::stringstream ds; - - switch( src ) - { - case CMaterial::Texture: - { - - switch( op ) - { - case CMaterial::SrcColor: - ds << texelNames[ stage ] << ".rgb"; - break; - - case CMaterial::InvSrcColor: - ds << "( vec3( 1.0, 1.0, 1.0 ) - "; - ds << texelNames[ stage ] << ".rgb )"; - break; - - case CMaterial::SrcAlpha: - ds << texelNames[ stage ] << ".a"; - break; - - case CMaterial::InvSrcAlpha: - ds << "( 1.0 - "; - ds << texelNames[ stage ] << ".a )"; - break; - } - } - break; - - case CMaterial::Previous: - - switch( op ) - { - case CMaterial::SrcColor: - ds << "texel.rgb"; - break; - - case CMaterial::InvSrcColor: - ds << "( vec3( 1.0, 1.0, 1.0 ) - texel.rgb )"; - break; - - case CMaterial::SrcAlpha: - ds << "texel.a"; - break; - - case CMaterial::InvSrcAlpha: - ds << "( 1.0 - texel.a )"; - break; - } - break; - - case CMaterial::Diffuse: - - switch( op ) - { - case CMaterial::SrcColor: - ds << "materialColor.rgb"; - break; - - case CMaterial::InvSrcColor: - ds << "( vec3( 1.0, 1.0, 1.0 ) - materialColor.rgb )"; - break; - - case CMaterial::SrcAlpha: - ds << "materialColor.a"; - break; - - case CMaterial::InvSrcAlpha: - ds << "( 1.0 - materialColor.a )"; - break; - } - break; - - case CMaterial::Constant: - - switch( op ) - { - case CMaterial::SrcColor: - ds << constantNames[ stage ] << ".rgb"; - break; - - case CMaterial::InvSrcColor: - ds << "( vec3( 1.0, 1.0, 1.0 ) - " << constantNames[ stage ] << ".rgb )"; - break; - - case CMaterial::SrcAlpha: - ds << constantNames[ stage ] << ".a"; - break; - - case CMaterial::InvSrcAlpha: - ds << "( 1.0 - " << constantNames[ stage ] << ".a )"; - break; - } - break; - } - - arg.assign( ds.str() ); - } - - - void CGLSLShaderGenerator::generateLightMapPS() - { - int ls = material->_LightMaps.size(); - ls++; // lightmaps + color texture - - int ntextures = 0; - for( int i = TexCoord0; i < TexCoord4; i++ ) - { - if( hasFlag( vbFormat, vertexFlags[ i ] ) ) - ntextures++; - } - - ntextures = std::max( ntextures, ls ); - - for( int i = 0; i < ntextures; i++ ) - ss << "uniform sampler2D sampler" << i << ";" << std::endl; - - addConstants(); - - addDiffuse(); - - addAlphaTreshold(); - - addFogUniform(); - - if( desc->fogEnabled() ) - ss << "smooth in vec4 ecPos;" << std::endl; - - ss << std::endl; - - if( desc->lightingEnabled() ) - { - addLightUniformsFS(); - addLightInsFS(); - ss << std::endl; - - addLightsFunctionFS(); - ss << std::endl; - } - - if( desc->fogEnabled() ) - addFogFunction(); - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - - // Lightmap UV coords are at position 1 - for( int i = 0; i < ntextures - 1; i++ ) - { - ss << "vec4 texel" << i; - ss << " = texture2D( sampler" << i; - ss << ", " << attribNames[ TexCoord1 ] << " );" << std::endl; - } - - // Color map UV coords are at position 0 - ss << "vec4 texel" << ntextures - 1 << " = texture2D( sampler" << ntextures - 1 << ", " << attribNames[ TexCoord0 ] << " );" << std::endl; - - //ss << "vec4 texel = diffuseColor;" << std::endl; - //ss << "vec4 texel = vec4( 1.0, 1.0, 1.0, 1.0 );" << std::endl; - ss << "vec4 texel = vec4( 0.0, 0.0, 0.0, 1.0 );" << std::endl; - - // Lightmaps - for( int i = 0; i < ntextures - 1; i++ ) - { - ss << "texel.rgb = " << texelNames[ i ] << ".rgb * " << constantNames[ i ] << ".rgb + texel.rgb;" << std::endl; - ss << "texel.a = " << texelNames[ i ] << ".a * texel.a + texel.a;" << std::endl; - } - - // Texture - ss << "texel.rgb = " << texelNames[ ntextures - 1 ] << ".rgb * texel.rgb;" << std::endl; - ss << "texel.a = " << texelNames[ ntextures - 1] << ".a;" << std::endl; - - if( material->_LightMapsMulx2 ) - { - ss << "texel.rgb = texel.rgb * 2.0;" << std::endl; - ss << "texel.a = texel.a * 2.0;" << std::endl; - } - - ss << "fragColor = texel;" << std::endl; - - if( desc->lightingEnabled() ) - addLightsFS(); - - if( desc->fogEnabled() ) - addFog(); - - addAlphaTest(); - - ss << "}" << std::endl; - ss << std::endl; - } - - void CGLSLShaderGenerator::generateSpecularPS() - { - ss << "smooth in vec3 cubeTexCoords;" << std::endl; - ss << "uniform sampler2D sampler0;" << std::endl; - ss << "uniform samplerCube sampler1;" << std::endl; - addDiffuse(); - addAlphaTreshold(); - addFogUniform(); - - if( desc->lightingEnabled() ) - { - addLightUniformsFS(); - addLightInsFS(); - ss << std::endl; - - addLightsFunctionFS(); - ss << std::endl; - } - - if( desc->fogEnabled() ) - ss << "smooth in vec4 ecPos;" << std::endl; - - ss << std::endl; - - if( desc->fogEnabled() ) - addFogFunction(); - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - ss << "vec4 texel0 = texture2D( sampler0, texCoord0 );" << std::endl; - ss << "vec4 texel1 = textureCube( sampler1, cubeTexCoords );" << std::endl; - ss << "vec4 texel;" << std::endl; - ss << "texel.rgb = texel0.rgb * diffuseColor.rgb;" << std::endl; - ss << "texel.a = texel0.a;" << std::endl; - ss << "texel.rgb = texel1.rgb * texel.a + texel.rgb;" << std::endl; - ss << "texel.a = texel1.a;" << std::endl; - ss << "fragColor = texel;" << std::endl; - - if( desc->lightingEnabled() ) - addLightsFS(); - - if( desc->fogEnabled() ) - addFog(); - - addAlphaTest(); - - ss << "}" << std::endl; - } - - void CGLSLShaderGenerator::generatePPLPS() - { - ss << "smooth in vec3 cubeTexCoords0;" << std::endl; - if( material->getShader() == CMaterial::PerPixelLighting ) - ss << "smooth in vec3 cubeTexCoords2;" << std::endl; - ss << std::endl; - - ss << "uniform samplerCube cubeSampler0;" << std::endl; - ss << "uniform sampler2D sampler1;" << std::endl; - - if( material->getShader() == CMaterial::PerPixelLighting ) - ss << "uniform samplerCube cubeSampler2;" << std::endl; - - addDiffuse(); - - addConstants(); - - addAlphaTreshold(); - - ss << std::endl; - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - - ss << "vec4 texel0 = textureCube( cubeSampler0, cubeTexCoords0 );" << std::endl; - ss << "vec4 texel1 = texture2D( sampler1, texCoord1 );" << std::endl; - - if( material->getShader() == CMaterial::PerPixelLighting ) - ss << "vec4 texel2 = textureCube( cubeSampler2, cubeTexCoords2 );" << std::endl; - - ss << "vec4 texel;" << std::endl; - - if( material->getShader() == CMaterial::PerPixelLighting ) - { - ss << "texel.rgb = texel0.rgb * constant0.rgb + constant0.rgb;" << std::endl; - ss << "texel.rgb = texel1.rgb * texel.rgb;" << std::endl; - ss << "texel.rgb = texel2.rgb * constant2.rgb + texel.rgb;" << std::endl; - ss << "texel.a = texel0.a * diffuseColor.a" << std::endl; - ss << "texel.a = texel1.a * texel.a;" << std::endl; - } - else - { - ss << "texel.rgb = texel0.rgb * constant0.rgb + color.rgb;" << std::endl; - ss << "texel.rgb = texel1.rgb * texel.rgb;" << std::endl; - ss << "texel.a = texel0.a * diffuseColor.a;" << std::endl; - ss << "texel.a = texel1.a * diffuseColor.a;" << std::endl; - } - - ss << "fragColor = texel;" << std::endl; - addAlphaTest(); - ss << "}" << std::endl; - } - - - void CGLSLShaderGenerator::generateWaterPS() - { - bool diffuse = false; - if( material->getTexture( 3 ) != NULL ) - diffuse = true; - - ss << "smooth in texCoord0;" << std::endl; - ss << "smooth in texCoord1;" << std::endl; - ss << "smooth in texCoord2;" << std::endl; - - if( diffuse ) - ss << "smooth in texCoord3;" << std::endl; - - ss << "flat in vec4 bump0ScaleBias;" << std::endl; - ss << "flat in vec4 bump1ScaleBias;" << std::endl; - - ss << std::endl; - - ss << "uniform sampler2D sampler0;" << std::endl; - ss << "uniform sampler2D sampler1;" << std::endl; - ss << "uniform sampler2D sampler2;" << std::endl; - - if( diffuse ) - ss << "uniform sampler2D sampler3;" << std::endl; - - addAlphaTreshold(); - - ss << std::endl; - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - ss << "vec4 texel0 = texture2D( sampler0, texCoord0 );" << std::endl; - ss << "texel0 = texel0 * bump0ScaleBias.xxxx + bump0ScaleBias.yyzz;" << std::endl; - ss << "texel0 = texel0 + texCoord1;" << std::endl; - ss << "vec4 texel1 = texture2D( sampler1, texel0 );" << std::endl; - ss << "texel1 = texel1 * bump1ScaleBias.xxxx + bump1ScaleBias.yyzz;" << std::endl; - ss << "texel1 = texel1 + texCoord2;" << std::endl; - ss << "vec4 texel2 = texture2D( sampler2, texel1 );" << std::endl; - - if( diffuse ) - { - ss << "vec4 texel3 = texture2D( sampler3, texCoord3 );" << std::endl; - ss << "texel3 = texel3 * texel2;" << std::endl; - } - - // No fog yet, so for later - //vec4 tmpFog = clamp( fogValue.x * fogFactor.x + fogFactor.y ); - //vec4 fragColor = mix( texel3, fogColor, tmpFog.x ); - - if( diffuse ) - ss << "fragColor = texel3;" << std::endl; - else - ss << "fragColor = texel2" << std::endl; - - addAlphaTest(); - - ss << "}" << std::endl; - - ss << std::endl; - } - - void CGLSLShaderGenerator::generateCloudPS() - { - ss << "uniform sampler2D sampler0;" << std::endl; - ss << "uniform sampler2D sampler1;" << std::endl; - addDiffuse(); - addAlphaTreshold(); - ss << std::endl; - - ss << "void main( void )" << std::endl; - ss << "{" << std::endl; - ss << "vec4 tex0 = texture2D( sampler0, texCoord0 );" << std::endl; - ss << "vec4 tex1 = texture2D( sampler1, texCoord1 );" << std::endl; - ss << "vec4 tex = mix( tex0, tex1, diffuse.a );" << std::endl; - ss << "tex.a = 0;" << std::endl; - ss << "fragColor = tex;" << std::endl; - addAlphaTest(); - ss << "}" << std::endl; - ss << std::endl; - - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "sstream" +#include "driver_glsl_shader_generator.h" +#include "nel/3d/vertex_buffer.h" +#include "driver_opengl_shader_desc.h" + +namespace +{ + inline bool hasFlag( uint32 data, uint32 flag ) + { + if( ( data & flag ) != 0 ) + return true; + else + return false; + } + + enum AttribOffset + { + Position, + Weight, + Normal, + PrimaryColor, + SecondaryColor, + Fog, + PaletteSkin, + Empty, + TexCoord0, + TexCoord1, + TexCoord2, + TexCoord3, + TexCoord4, + TexCoord5, + TexCoord6, + TexCoord7, + NumOffsets + }; +} + +namespace NL3D +{ + uint16 vertexFlags[ CVertexBuffer::NumValue ] = + { + CVertexBuffer::PositionFlag, + CVertexBuffer::WeightFlag, + CVertexBuffer::NormalFlag, + CVertexBuffer::PrimaryColorFlag, + CVertexBuffer::SecondaryColorFlag, + CVertexBuffer::FogFlag, + CVertexBuffer::PaletteSkinFlag, + 0, + CVertexBuffer::TexCoord0Flag, + CVertexBuffer::TexCoord1Flag, + CVertexBuffer::TexCoord2Flag, + CVertexBuffer::TexCoord3Flag, + CVertexBuffer::TexCoord4Flag, + CVertexBuffer::TexCoord5Flag, + CVertexBuffer::TexCoord6Flag, + CVertexBuffer::TexCoord7Flag + }; + + const char *attribNames[ CVertexBuffer::NumValue ] = + { + "position", + "weight", + "normal", + "color", + "color2", + "fog", + "paletteSkin", + "none", + "texCoord0", + "texCoord1", + "texCoord2", + "texCoord3", + "texCoord4", + "texCoord5", + "texCoord6", + "texCoord7" + }; + + const char *texelNames[ SHADER_MAX_TEXTURES ] = + { + "texel0", + "texel1", + "texel2", + "texel3" + }; + + const char *constantNames[ 4 ] = + { + "constant0", + "constant1", + "constant2", + "constant3" + }; + + const char *shaderNames[] = + { + "Normal", + "Bump", + "Usercolor", + "Lightmap", + "Specular", + "Caustics", + "Per-Pixel Lighting", + "Per-Pixel Lighting, no specular", + "Cloud", + "Water" + }; + + CGLSLShaderGenerator::CGLSLShaderGenerator() + { + reset(); + } + + CGLSLShaderGenerator::~CGLSLShaderGenerator() + { + } + + void CGLSLShaderGenerator::reset() + { + material = NULL; + vbFormat = 0; + desc = NULL; + ss.str( "" ); + ss.clear(); + } + + void CGLSLShaderGenerator::generateVS( std::string &vs ) + { + ss.str( "" ); + ss.clear(); + ss << "// " << shaderNames[ material->getShader() ] << " Vertex Shader" << std::endl; + ss << std::endl; + + ss << "#version 330" << std::endl; + ss << "#extension GL_ARB_separate_shader_objects : enable" << std::endl; + ss << "out gl_PerVertex" << std::endl; + ss << "{" << std::endl; + ss << "vec4 gl_Position;" << std::endl; + ss << "};" << std::endl; + ss << std::endl; + ss << "uniform mat4 modelViewProjection;" << std::endl; + ss << std::endl; + + for( int i = Position; i < NumOffsets; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + { + ss << "layout ( location = "; + ss << i; + ss << " ) "; + ss << "in vec4 "; + ss << "v" << attribNames[ i ]; + ss << ";"; + ss << std::endl; + } + } + ss << std::endl; + + for( int i = Weight; i < NumOffsets; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + { + ss << "smooth out vec4 "; + ss << attribNames[ i ] << ";" << std::endl; + } + } + ss << std::endl; + + if( !desc->useTextures() ) + { + addAmbient(); + addDiffuse(); + addSpecular(); + } + + switch( material->getShader() ) + { + case CMaterial::Normal: + case CMaterial::UserColor: + case CMaterial::LightMap: + case CMaterial::Cloud: + generateNormalVS(); + break; + + case CMaterial::Specular: + generateSpecularVS(); + break; + + case CMaterial::PerPixelLighting: + case CMaterial::PerPixelLightingNoSpec: + generatePPLVS(); + break; + + case CMaterial::Water: + generateWaterVS(); + break; + } + + vs.assign( ss.str() ); + } + + void CGLSLShaderGenerator::generatePS( std::string &ps ) + { + ss.str( "" ); + ss.clear(); + + ss << "// " << shaderNames[ material->getShader() ] << " Pixel Shader" << std::endl; + ss << std::endl; + + ss << "#version 330" << std::endl; + ss << std::endl; + + ss << "out vec4 fragColor;" << std::endl; + + for( int i = Weight; i < NumOffsets; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + { + ss << "smooth in vec4 "; + ss << attribNames[ i ] << ";" << std::endl; + } + } + ss << std::endl; + + switch( material->getShader() ) + { + case CMaterial::Normal: + case CMaterial::UserColor: + generateNormalPS(); + break; + + case CMaterial::LightMap: + generateLightMapPS(); + break; + + case CMaterial::Specular: + generateSpecularPS(); + break; + + case CMaterial::PerPixelLighting: + case CMaterial::PerPixelLightingNoSpec: + generatePPLPS(); + break; + + case CMaterial::Water: + generateWaterPS(); + break; + + case CMaterial::Cloud: + generateCloudPS(); + break; + } + + ps.assign( ss.str() ); + } + + void CGLSLShaderGenerator::addAmbient() + { + ss << "uniform vec4 ambientColor;" << std::endl; + } + + void CGLSLShaderGenerator::addDiffuse() + { + ss << "uniform vec4 diffuseColor;" << std::endl; + } + + void CGLSLShaderGenerator::addSpecular() + { + ss << "uniform vec4 specularColor;" << std::endl; + } + + void CGLSLShaderGenerator::addColor() + { + ss << "uniform vec4 materialColor;" << std::endl; + } + + void CGLSLShaderGenerator::addConstants() + { + ss << "uniform vec4 constant0;" << std::endl; + ss << "uniform vec4 constant1;" << std::endl; + ss << "uniform vec4 constant2;" << std::endl; + ss << "uniform vec4 constant3;" << std::endl; + } + + void CGLSLShaderGenerator::addNormalMatrix() + { + ss << "uniform mat3 normalMatrix;" << std::endl; + } + + void CGLSLShaderGenerator::addViewMatrix() + { + ss << "uniform mat4 viewMatrix;" << std::endl; + } + + void CGLSLShaderGenerator::addAlphaTreshold() + { + ss << "uniform float alphaTreshold;" << std::endl; + } + + void CGLSLShaderGenerator::addAlphaTest() + { + if( material->getAlphaTest() ) + ss << "if( fragColor.a <= ( alphaTreshold - 0.0001 ) ) discard;" << std::endl; + } + + void CGLSLShaderGenerator::addFogUniform() + { + if( !desc->fogEnabled() ) + return; + + ss << "uniform float fogStart;" << std::endl; + ss << "uniform float fogEnd;" << std::endl; + ss << "uniform vec4 fogColor;" << std::endl; + + if( desc->getFogMode() == CShaderDesc::Linear ) + return; + + ss << "uniform float fogDensity;" << std::endl; + } + + void CGLSLShaderGenerator::addFogFunction() + { + if( !desc->fogEnabled() ) + return; + + switch( desc->getFogMode() ) + { + case CShaderDesc::Linear: + ss << "vec4 applyFog( vec4 col )" << std::endl; + ss << "{" << std::endl; + ss << "float z = ecPos.z / ecPos.w;" << std::endl; + ss << "z = abs( z );" << std::endl; + ss << "float fogFactor = ( fogEnd - z ) / ( fogEnd - fogStart );" << std::endl; + ss << "fogFactor = clamp( fogFactor, 0.0, 1.0 );" << std::endl; + ss << "vec4 fColor = mix( fogColor, col, fogFactor );" << std::endl; + ss << "return fColor;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + break; + } + } + + void CGLSLShaderGenerator::addFog() + { + ss << "fragColor = applyFog( fragColor );" << std::endl; + } + + + /////////////////////////////////////////////////////////// Lights //////////////////////////////////////////////////////////////////// + + void CGLSLShaderGenerator::addLightUniformsVS() + { + for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) + { + switch( desc->getLight( i ) ) + { + case CShaderDesc::Nolight: + continue; + break; + + case CShaderDesc::Directional: + ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl; + ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; + ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; + ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; + ss << "uniform float light" << i << "Shininess;" << std::endl; + break; + + case CShaderDesc::Point: + ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl; + ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; + ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; + ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; + ss << "uniform float light" << i << "Shininess;" << std::endl; + ss << "uniform float light" << i << "ConstAttn;" << std::endl; + ss << "uniform float light" << i << "LinAttn;" << std::endl; + ss << "uniform float light" << i << "QuadAttn;" << std::endl; + break; + } + } + } + + void CGLSLShaderGenerator::addLightUniformsFS() + { + for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) + { + switch( desc->getLight( i ) ) + { + case CShaderDesc::Nolight: + continue; + break; + + case CShaderDesc::Directional: + break; + } + } + + } + + void CGLSLShaderGenerator::addLightOutsVS() + { + ss << "smooth out vec4 lightColor;" << std::endl; + } + + void CGLSLShaderGenerator::addLightInsFS() + { + ss << "smooth in vec4 lightColor;" << std::endl; + } + + void CGLSLShaderGenerator::addDirectionalFunctionVS( int num ) + { + ss << "float getIntensity" << num << "( vec3 normal3, vec3 lightDir )" << std::endl; + ss << "{" << std::endl; + ss << "float angle = dot( lightDir, normal3 );" << std::endl; + ss << "angle = max( 0.0, angle );" << std::endl; + ss << "return angle;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + + ss << "float getSpecIntensity" << num << "( vec3 normal3, vec3 lightDir )" << std::endl; + ss << "{" << std::endl; + ss << "vec3 halfVector = normalize( lightDir + normal3 );" << std::endl; + ss << "float angle = dot( normal3, halfVector );" << std::endl; + ss << "angle = max( 0.0, angle );" << std::endl; + ss << "float si = pow( angle, light" << num << "Shininess );" << std::endl; + ss << "return si;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + + ss << "vec4 getLight" << num << "Color()" << std::endl; + ss << "{" << std::endl; + ss << "vec4 lightDir4 = viewMatrix * vec4( light" << num << "DirOrPos, 1.0 );" << std::endl; + ss << "vec3 lightDir = lightDir4.xyz / lightDir4.w;" << std::endl; + ss << "lightDir = normalize( lightDir );" << std::endl; + ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; + ss << "normal3 = normalMatrix * normal3;" << std::endl; + ss << "normal3 = normalize( normal3 );" << std::endl; + + if( desc->useTextures() || ( material->getShader() == CMaterial::LightMap ) ) + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff + "; + ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec + "; + ss << "light" << num << "ColAmb;" << std::endl; + } + else + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff * diffuseColor + "; + ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec * specularColor + "; + ss << "light" << num << "ColAmb * ambientColor;" << std::endl; + } + + ss << "return lc;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + } + + void CGLSLShaderGenerator::addPointLightFunctionVS( int num ) + { + ss << "float getIntensity" << num << "( vec3 normal3, vec3 direction3 )" << std::endl; + ss << "{" << std::endl; + ss << "float angle = dot( direction3, normal3 );" << std::endl; + ss << "angle = max( 0.0, angle );" << std::endl; + ss << "return angle;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + + ss << "float getSpecIntensity" << num << "( vec3 normal3, vec3 direction3 )" << std::endl; + ss << "{" << std::endl; + ss << "vec3 halfVector = normalize( direction3 + normal3 );" << std::endl; + ss << "float angle = dot( normal3, halfVector );" << std::endl; + ss << "angle = max( 0.0, angle );" << std::endl; + ss << "float si = pow( angle, light" << num << "Shininess );" << std::endl; + ss << "return si;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + + ss << "vec4 getLight" << num << "Color()" << std::endl; + ss << "{" << std::endl; + ss << "vec3 ecPos3 = ecPos4.xyz / ecPos4.w;" << std::endl; + ss << "vec4 lightPos4 = viewMatrix * vec4( light" << num << "DirOrPos, 1.0 );" << std::endl; + ss << "vec3 lightPos = lightPos4.xyz / lightPos4.w;" << std::endl; + ss << "vec3 lightDirection = lightPos - ecPos3;" << std::endl; + ss << "float lightDistance = length( lightDirection );" << std::endl; + ss << "lightDirection = normalize( lightDirection );" << std::endl; + + ss << "float attenuation = light" << num << "ConstAttn + "; + ss << "light" << num << "LinAttn * lightDistance +"; + ss << "light" << num << "QuadAttn * lightDistance * lightDistance;" << std::endl; + + ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; + ss << "normal3 = normalMatrix * normal3;" << std::endl; + ss << "normal3 = normalize( normal3 );" << std::endl; + + if( desc->useTextures() || ( material->getShader() == CMaterial::LightMap ) ) + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff + "; + ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec + "; + ss << "light" << num << "ColAmb;" << std::endl; + } + else + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff * diffuseColor+ "; + ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec * specularColor + "; + ss << "light" << num << "ColAmb * ambientColor;" << std::endl; + } + + ss << "lc = lc / attenuation;" << std::endl; + ss << "return lc;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + } + + void CGLSLShaderGenerator::addLightsFunctionVS() + { + for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) + { + switch( desc->getLight( i ) ) + { + case CShaderDesc::Nolight: + continue; + break; + + case CShaderDesc::Directional: + addDirectionalFunctionVS( i ); + break; + + case CShaderDesc::Point: + addPointLightFunctionVS( i ); + break; + + } + + } + } + + void CGLSLShaderGenerator::addLightsFunctionFS() + { + ss << "vec4 applyLights( vec4 col )" << std::endl; + ss << "{" << std::endl; + ss << "return col * lightColor;" << std::endl; + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::addLightsVS() + { + ss << "lightColor = vec4( 0.0, 0.0, 0.0, 1.0 );" << std::endl; + + for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) + { + if( desc->getLight( i ) == CShaderDesc::Nolight ) + continue; + + ss << "lightColor = lightColor + getLight" << i << "Color();" << std::endl; + } + } + + void CGLSLShaderGenerator::addLightsFS() + { + ss << "fragColor = applyLights( fragColor );" << std::endl; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + void CGLSLShaderGenerator::generateNormalVS() + { + if( desc->fogEnabled() || desc->hasPointLight() ) + { + ss << "uniform mat4 modelView;" << std::endl; + } + if( desc->lightingEnabled() ) + { + addViewMatrix(); + } + if( desc->fogEnabled() || desc->hasPointLight() ) + { + ss << "vec4 ecPos4;" << std::endl; + } + + if( desc->fogEnabled() ) + ss << "smooth out vec4 ecPos;" << std::endl; + + ss << std::endl; + + if( desc->lightingEnabled() ) + { + addNormalMatrix(); + addLightUniformsVS(); + addLightOutsVS(); + ss << std::endl; + + addLightsFunctionVS(); + ss << std::endl; + } + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + + ss << "gl_Position = modelViewProjection * " << "v" << attribNames[ 0 ] << ";" << std::endl; + + if( desc->fogEnabled() || desc->hasPointLight() ) + ss << "ecPos4 = modelView * v" << attribNames[ 0 ] << ";" << std::endl; + + if( desc->fogEnabled() ) + ss << "ecPos = ecPos4;" << std::endl; + + if( desc->lightingEnabled() ) + addLightsVS(); + + for( int i = Weight; i < NumOffsets; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + { + ss << attribNames[ i ]; + ss << " = "; + ss << "v" << attribNames[ i ] << ";" << std::endl; + } + } + + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::generateSpecularVS() + { + + ss << "uniform mat4 modelView;" << std::endl; + ss << "uniform mat4 texMatrix0;" << std::endl; + ss << "smooth out vec3 cubeTexCoords;" << std::endl; + + if( desc->fogEnabled() || desc->hasPointLight() ) + { + ss << "vec4 ecPos4;" << std::endl; + } + + if( desc->fogEnabled() ) + ss << "smooth out vec4 ecPos;" << std::endl; + ss << std::endl; + + if( desc->lightingEnabled() ) + { + addViewMatrix(); + addNormalMatrix(); + addLightUniformsVS(); + addLightOutsVS(); + ss << std::endl; + + addLightsFunctionVS(); + ss << std::endl; + } + + ss << "vec3 ReflectionMap( const in vec3 eyePos, const in vec3 normal )" << std::endl; + ss << "{" << std::endl; + ss << "vec3 u = normalize( eyePos );" << std::endl; + ss << "return reflect( u, normal );" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + ss << "vec4 eyePosition = modelView * v" << attribNames[ 0 ] << ";" << std::endl; + + if( desc->hasPointLight() ) + ss << "ecPos4 = eyePosition;" << std::endl; + + if( desc->fogEnabled() ) + ss << "ecPos = eyePosition;" << std::endl; + + ss << "vec3 ep = eyePosition.xyz / eyePosition.w;" << std::endl; + ss << "vec3 n = vnormal.xyz;" << std::endl; + ss << "cubeTexCoords = ReflectionMap( ep, n );" << std::endl; + ss << "vec4 t = vec4( cubeTexCoords, 1.0 );" << std::endl; + ss << "t = t * texMatrix0;" << std::endl; + ss << "cubeTexCoords = t.xyz;" << std::endl; + ss << "gl_Position = modelViewProjection * v" << attribNames[ 0 ] << ";" << std::endl; + + if( desc->lightingEnabled() ) + addLightsVS(); + + for( int i = Weight; i < NumOffsets; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + { + ss << attribNames[ i ]; + ss << " = "; + ss << "v" << attribNames[ i ] << ";" << std::endl; + } + } + + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::generatePPLVS() + { + ss << "uniform vec4 lightPosition;" << std::endl; + + if( material->getShader() == CMaterial::PerPixelLighting ) + { + ss << "uniform vec4 viewerPos;" << std::endl; + ss << "uniform mat4 invModelMat;" << std::endl; + + } + ss << std::endl; + + ss << "smooth out vec3 cubeTexCoords0;" << std::endl; + + if( material->getShader() == CMaterial::PerPixelLighting ) + ss << "smooth out vec3 cubeTexCoords2;" << std::endl; + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + ss << "gl_Position = modelViewProjection * v" << attribNames[ 0 ] << ";" << std::endl; + + for( int i = Weight; i < NumOffsets; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + { + ss << attribNames[ i ]; + ss << " = "; + ss << "v" << attribNames[ i ] << ";" << std::endl; + } + } + + ss << "vec4 n = normalize( vnormal ); //normalized normal" << std::endl; + ss << "vec4 T; // second basis, Tangent" << std::endl; + ss << "T = texCoord1;" << std::endl; + ss << "T = T - n * dot( N, T ); // Gramm-Schmidt process" << std::endl; + ss << "T = normalize( T );" << std::endl; + ss << "vec4 B;" << std::endl; + ss << "B.xyz = cross( n.xyz, T.xyz ); // B = N x T" << std::endl; + ss << "vec4 L = lightPos - vposition; //Inverse light vector" << std::endl; + ss << "L = normalize( L );" << std::endl; + ss << "L * [ T B N ]" << std::endl; + ss << "cubeTexCoords0.x = dot( T.xyz, L.xyz );" << std::endl; + ss << "cubeTexCoords0.y = dot( B.xyz, L.xyz );" << std::endl; + ss << "cubeTexCoords0.z = dot( n.xyz, L.xyz );" << std::endl; + + if( material->getShader() == CMaterial::PerPixelLighting ) + { + ss << "vec4 V = invModelMat * viewerPos - vposition;" << std::endl; + ss << "V = normalize( V );" << std::endl; + ss << "vec4 H = L + V; // half-angle" << std::endl; + ss << "vec4 H = normalize( H );" << std::endl; + ss << "cubeTexCoords2.x = dot( T, H );" << std::endl; + ss << "cubeTexCoords2.y = dot( B, H );" << std::endl; + ss << "cubeTexCoords2.w = dot( n, H );" << std::endl; + } + + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::generateWaterVS() + { + bool diffuse = false; + if( material->getTexture( 3 ) ) + diffuse = true; + + ss << "smooth out vec4 texCoord0;" << std::endl; + ss << "smooth out vec4 texCoord1;" << std::endl; + ss << "smooth out vec4 texCoord2;" << std::endl; + ss << "flat out vec4 bump0ScaleBias;" << std::endl; + ss << "flat out vec4 bump1ScaleBias;" << std::endl; + + if( diffuse ) + ss << "smooth out vec4 texCoord3;" << std::endl; + + ss << std::endl; + + if( diffuse ) + { + ss << "uniform vec4 diffuseMapVector0;" << std::endl; + ss << "uniform vec4 diffuseMapVector1;" << std::endl; + } + + ss << "uniform vec4 bumpMap0Scale;" << std::endl; + ss << "uniform vec4 bumpMap1Scale;" << std::endl; + ss << "uniform vec4 bumpMap0Offset;" << std::endl; + ss << "uniform vec4 bumpMap1Offset;" << std::endl; + + // no fog yet + //ss << "uniform mat4 modelView;" << std::endl; + ss << std::endl; + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + ss << "position = vposition;" << std::endl; + ss << "gl_Position = modelViewProjection * position;" << std::endl; + ss << "bump0ScaleBias = bumpMap0Scale;" << std::endl; + ss << "bump1ScaleBias = bumpMap1Scale;" << std::endl; + + // no fog yet + //ss << "vec4 v = modelView[ 3 ];" << std::endl; + //fog.x = dot( position, v ); + + ss << "texCoord0 = position * bumpMap0Scale + bumpMap0Offset;" << std::endl; + ss << "texCoord1 = position * bumpMap1Scale + bumpMap1Offset;" << std::endl; + ss << "vec4 eyeDirection = normalize( eye - position );" << std::endl; + + ss << "texCoord2 = -1 * eyeDirection * vec4( 0.5, 0.05, 0.0, 1.0 ) + vec4( 0.5, 0.05, 0.0, 1.0 );" << std::endl; + + if( diffuse ) + { + ss << "texCoord3.x = dot( position, diffuseMapVector0 );" << std::endl; + ss << "texCoord3.y = dot( position, diffuseMapVector1 );" << std::endl; + } + + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::generateNormalPS() + { + uint sampler = 0; + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + { + if( desc->getUseTexStage( i ) ) + ss << "uniform sampler2D sampler" << sampler << ";" << std::endl; + sampler++; + } + + addColor(); + addConstants(); + addAlphaTreshold(); + addFogUniform(); + + if( desc->fogEnabled() ) + ss << "smooth in vec4 ecPos;" << std::endl; + + ss << std::endl; + + if( desc->lightingEnabled() ) + { + addLightUniformsFS(); + addLightInsFS(); + ss << std::endl; + + addLightsFunctionFS(); + ss << std::endl; + } + + if( desc->fogEnabled() ) + addFogFunction(); + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + + bool textures = false; + sampler = 0; + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + { + if( desc->getUseTexStage( i ) ) + { + ss << "vec4 texel" << sampler << " = texture2D( sampler" << sampler << ","; + + if( !desc->getUseFirstTexCoords() ) + ss << attribNames[ TexCoord0 + i ] << " );"; + else + ss << attribNames[ TexCoord0 ] << " );"; + + ss << std::endl; + + textures = true; + } + sampler++; + } + + bool vertexColor = false; + if( hasFlag( vbFormat, vertexFlags[ PrimaryColor ] ) ) + vertexColor = true; + + if( textures && !vertexColor ) + ss << "vec4 texel = vec4( 1.0, 1.0, 1.0, 1.0 );" << std::endl; + else + if( vertexColor ) + ss << "vec4 texel = color;" << std::endl; + else + ss << "vec4 texel = vec4( 0.5, 0.5, 0.5, 1.0 );" << std::endl; + + generateTexEnv(); + + // This is just an idea I had, but it seems to be working. + // Unfortunately it's not documented anywhere I looked in the GL spec, but if I don't have this modulation here, + // the Ryzom UI looks horrific. + if( vertexColor ) + ss << "texel = color * texel;" << std::endl; + + ss << "fragColor = texel;" << std::endl; + + if( desc->lightingEnabled() ) + addLightsFS(); + + if( desc->fogEnabled() ) + addFog(); + + addAlphaTest(); + + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::generateTexEnv() + { + uint32 stage = 0; + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + { + if( desc->getUseTexStage( i ) ) + { + generateTexEnvRGB( stage ); + generateTexEnvAlpha( stage ); + } + stage++; + } + } + + void CGLSLShaderGenerator::generateTexEnvRGB( unsigned int stage ) + { + std::string arg0; + std::string arg1; + std::string arg2; + + switch( material->_TexEnvs[ stage ].Env.OpRGB ) + { + case CMaterial::Replace: + { + buildArg( stage, 0, false, arg0 ); + ss << "texel.rgb = " << arg0 << ";" << std::endl; + } + break; + + case CMaterial::Modulate: + { + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + ss << "texel.rgb = " << arg0 << " * " << arg1 << ";" << std::endl; + //ss << "texel.rgb = " << arg0 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb * " << arg1 << ";" << std::endl; + } + break; + + case CMaterial::Add: + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + ss << "texel.rgb = " << arg0 << " + " << arg1 << ";" << std::endl; + //ss << "texel.rgb = " << arg0 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb + " << arg1 << ";" << std::endl; + break; + + case CMaterial::AddSigned: + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + ss << "texel.rgb = " << arg0 << " + " << arg1 << " - vec3( 0.5, 0.5, 0.5 );" << std::endl; + //ss << "texel.rgb = " << arg0 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb + " << arg1 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb - vec3( 0.5, 0.5, 0.5 );" << std::endl; + break; + + case CMaterial::Mad: + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + buildArg( stage, 2, false, arg2 ); + ss << "texel.rgb = " << arg0 << " * " << arg1 << " + " << arg2 << ";" << std::endl; + //ss << "texel.rgb = " << arg0 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb * " << arg1 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb + " << arg2 << ";" << std::endl; + break; + + case CMaterial::InterpolateTexture: + { + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + + std::string As = texelNames[ stage ]; + As.append( ".a" ); + + ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + + case CMaterial::InterpolatePrevious: + { + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + + std::string As = "texel.a"; + + ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + + case CMaterial::InterpolateDiffuse: + { + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + + std::string As = "diffuseColor.a"; + + ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + + case CMaterial::InterpolateConstant: + { + buildArg( stage, 0, false, arg0 ); + buildArg( stage, 1, false, arg1 ); + + std::string As = constantNames[ stage ]; + As.append( ".a" ); + + ss << "texel.rgb = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.rgb = texel.rgb + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + } + } + + void CGLSLShaderGenerator::generateTexEnvAlpha( unsigned int stage ) + { + std::string arg0; + std::string arg1; + std::string arg2; + + switch( material->_TexEnvs[ stage ].Env.OpRGB ) + { + case CMaterial::Replace: + { + buildArg( stage, 0, true, arg0 ); + ss << "texel.a = " << arg0 << ";" << std::endl; + } + break; + + case CMaterial::Modulate: + { + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + ss << "texel.a = " << arg0 << " * " << arg1 << ";" << std::endl; + //ss << "texel.a = " << arg0 << ";" << std::endl; + //ss << "texel.a = texel.a * " << arg1 << ";" << std::endl; + } + break; + + case CMaterial::Add: + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + ss << "texel.a = " << arg0 << " + " << arg1 << ";" << std::endl; + //ss << "texel.a = " << arg0 << ";" << std::endl; + //ss << "texel.a = texel.a + " << arg1 << ";" << std::endl; + break; + + case CMaterial::AddSigned: + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + ss << "texel.a = " << arg0 << " * " << arg1 << " - vec3( 0.5, 0.5, 0.5 );" << std::endl; + //ss << "texel.rgb = " << arg0 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb + " << arg1 << ";" << std::endl; + //ss << "texel.rgb = texel.rgb - vec3( 0.5, 0.5, 0.5 );" << std::endl; + break; + + case CMaterial::Mad: + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + buildArg( stage, 2, true, arg2 ); + ss << "texel.a = " << arg0 << ";" << std::endl; + ss << "texel.a = texel.a * " << arg1 << ";" << std::endl; + ss << "texel.a = texel.a + " << arg2 << ";" << std::endl; + break; + + case CMaterial::InterpolateTexture: + { + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + + std::string As = texelNames[ stage ]; + As.append( ".a" ); + + ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + + case CMaterial::InterpolatePrevious: + { + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + + std::string As = "texel.a"; + + ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + + case CMaterial::InterpolateDiffuse: + { + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + + std::string As = "diffuseColor.a"; + + ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + + case CMaterial::InterpolateConstant: + { + buildArg( stage, 0, true, arg0 ); + buildArg( stage, 1, true, arg1 ); + + std::string As = constantNames[ stage ]; + As.append( ".a" ); + + ss << "texel.a = " << arg0 << " * " << As << ";" << std::endl; + ss << "texel.a = texel.a + " << arg1 << " * " << "( 1.0 - " << As << " );" << std::endl; + } + break; + } + } + + + void CGLSLShaderGenerator::buildArg( unsigned int stage, unsigned int n, bool alpha, std::string &arg ) + { + uint32 src; + uint32 op; + + if( !alpha ) + { + src = material->_TexEnvs[ n ].getColorArg( n ); + op = material->_TexEnvs[ n ].getColorOperand( n ); + } + else + { + src = material->_TexEnvs[ n ].getAlphaArg( n ); + op = material->_TexEnvs[ n ].getAlphaOperand( n ); + } + + std::stringstream ds; + + switch( src ) + { + case CMaterial::Texture: + { + + switch( op ) + { + case CMaterial::SrcColor: + ds << texelNames[ stage ] << ".rgb"; + break; + + case CMaterial::InvSrcColor: + ds << "( vec3( 1.0, 1.0, 1.0 ) - "; + ds << texelNames[ stage ] << ".rgb )"; + break; + + case CMaterial::SrcAlpha: + ds << texelNames[ stage ] << ".a"; + break; + + case CMaterial::InvSrcAlpha: + ds << "( 1.0 - "; + ds << texelNames[ stage ] << ".a )"; + break; + } + } + break; + + case CMaterial::Previous: + + switch( op ) + { + case CMaterial::SrcColor: + ds << "texel.rgb"; + break; + + case CMaterial::InvSrcColor: + ds << "( vec3( 1.0, 1.0, 1.0 ) - texel.rgb )"; + break; + + case CMaterial::SrcAlpha: + ds << "texel.a"; + break; + + case CMaterial::InvSrcAlpha: + ds << "( 1.0 - texel.a )"; + break; + } + break; + + case CMaterial::Diffuse: + + switch( op ) + { + case CMaterial::SrcColor: + ds << "materialColor.rgb"; + break; + + case CMaterial::InvSrcColor: + ds << "( vec3( 1.0, 1.0, 1.0 ) - materialColor.rgb )"; + break; + + case CMaterial::SrcAlpha: + ds << "materialColor.a"; + break; + + case CMaterial::InvSrcAlpha: + ds << "( 1.0 - materialColor.a )"; + break; + } + break; + + case CMaterial::Constant: + + switch( op ) + { + case CMaterial::SrcColor: + ds << constantNames[ stage ] << ".rgb"; + break; + + case CMaterial::InvSrcColor: + ds << "( vec3( 1.0, 1.0, 1.0 ) - " << constantNames[ stage ] << ".rgb )"; + break; + + case CMaterial::SrcAlpha: + ds << constantNames[ stage ] << ".a"; + break; + + case CMaterial::InvSrcAlpha: + ds << "( 1.0 - " << constantNames[ stage ] << ".a )"; + break; + } + break; + } + + arg.assign( ds.str() ); + } + + + void CGLSLShaderGenerator::generateLightMapPS() + { + int ls = material->_LightMaps.size(); + ls++; // lightmaps + color texture + + int ntextures = 0; + for( int i = TexCoord0; i < TexCoord4; i++ ) + { + if( hasFlag( vbFormat, vertexFlags[ i ] ) ) + ntextures++; + } + + ntextures = std::max( ntextures, ls ); + + for( int i = 0; i < ntextures; i++ ) + ss << "uniform sampler2D sampler" << i << ";" << std::endl; + + addConstants(); + + addDiffuse(); + + addAlphaTreshold(); + + addFogUniform(); + + if( desc->fogEnabled() ) + ss << "smooth in vec4 ecPos;" << std::endl; + + ss << std::endl; + + if( desc->lightingEnabled() ) + { + addLightUniformsFS(); + addLightInsFS(); + ss << std::endl; + + addLightsFunctionFS(); + ss << std::endl; + } + + if( desc->fogEnabled() ) + addFogFunction(); + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + + // Lightmap UV coords are at position 1 + for( int i = 0; i < ntextures - 1; i++ ) + { + ss << "vec4 texel" << i; + ss << " = texture2D( sampler" << i; + ss << ", " << attribNames[ TexCoord1 ] << " );" << std::endl; + } + + // Color map UV coords are at position 0 + ss << "vec4 texel" << ntextures - 1 << " = texture2D( sampler" << ntextures - 1 << ", " << attribNames[ TexCoord0 ] << " );" << std::endl; + + //ss << "vec4 texel = diffuseColor;" << std::endl; + //ss << "vec4 texel = vec4( 1.0, 1.0, 1.0, 1.0 );" << std::endl; + ss << "vec4 texel = vec4( 0.0, 0.0, 0.0, 1.0 );" << std::endl; + + // Lightmaps + for( int i = 0; i < ntextures - 1; i++ ) + { + ss << "texel.rgb = " << texelNames[ i ] << ".rgb * " << constantNames[ i ] << ".rgb + texel.rgb;" << std::endl; + ss << "texel.a = " << texelNames[ i ] << ".a * texel.a + texel.a;" << std::endl; + } + + // Texture + ss << "texel.rgb = " << texelNames[ ntextures - 1 ] << ".rgb * texel.rgb;" << std::endl; + ss << "texel.a = " << texelNames[ ntextures - 1] << ".a;" << std::endl; + + if( material->_LightMapsMulx2 ) + { + ss << "texel.rgb = texel.rgb * 2.0;" << std::endl; + ss << "texel.a = texel.a * 2.0;" << std::endl; + } + + ss << "fragColor = texel;" << std::endl; + + if( desc->lightingEnabled() ) + addLightsFS(); + + if( desc->fogEnabled() ) + addFog(); + + addAlphaTest(); + + ss << "}" << std::endl; + ss << std::endl; + } + + void CGLSLShaderGenerator::generateSpecularPS() + { + ss << "smooth in vec3 cubeTexCoords;" << std::endl; + ss << "uniform sampler2D sampler0;" << std::endl; + ss << "uniform samplerCube sampler1;" << std::endl; + addDiffuse(); + addAlphaTreshold(); + addFogUniform(); + + if( desc->lightingEnabled() ) + { + addLightUniformsFS(); + addLightInsFS(); + ss << std::endl; + + addLightsFunctionFS(); + ss << std::endl; + } + + if( desc->fogEnabled() ) + ss << "smooth in vec4 ecPos;" << std::endl; + + ss << std::endl; + + if( desc->fogEnabled() ) + addFogFunction(); + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + ss << "vec4 texel0 = texture2D( sampler0, texCoord0 );" << std::endl; + ss << "vec4 texel1 = textureCube( sampler1, cubeTexCoords );" << std::endl; + ss << "vec4 texel;" << std::endl; + ss << "texel.rgb = texel0.rgb * diffuseColor.rgb;" << std::endl; + ss << "texel.a = texel0.a;" << std::endl; + ss << "texel.rgb = texel1.rgb * texel.a + texel.rgb;" << std::endl; + ss << "texel.a = texel1.a;" << std::endl; + ss << "fragColor = texel;" << std::endl; + + if( desc->lightingEnabled() ) + addLightsFS(); + + if( desc->fogEnabled() ) + addFog(); + + addAlphaTest(); + + ss << "}" << std::endl; + } + + void CGLSLShaderGenerator::generatePPLPS() + { + ss << "smooth in vec3 cubeTexCoords0;" << std::endl; + if( material->getShader() == CMaterial::PerPixelLighting ) + ss << "smooth in vec3 cubeTexCoords2;" << std::endl; + ss << std::endl; + + ss << "uniform samplerCube cubeSampler0;" << std::endl; + ss << "uniform sampler2D sampler1;" << std::endl; + + if( material->getShader() == CMaterial::PerPixelLighting ) + ss << "uniform samplerCube cubeSampler2;" << std::endl; + + addDiffuse(); + + addConstants(); + + addAlphaTreshold(); + + ss << std::endl; + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + + ss << "vec4 texel0 = textureCube( cubeSampler0, cubeTexCoords0 );" << std::endl; + ss << "vec4 texel1 = texture2D( sampler1, texCoord1 );" << std::endl; + + if( material->getShader() == CMaterial::PerPixelLighting ) + ss << "vec4 texel2 = textureCube( cubeSampler2, cubeTexCoords2 );" << std::endl; + + ss << "vec4 texel;" << std::endl; + + if( material->getShader() == CMaterial::PerPixelLighting ) + { + ss << "texel.rgb = texel0.rgb * constant0.rgb + constant0.rgb;" << std::endl; + ss << "texel.rgb = texel1.rgb * texel.rgb;" << std::endl; + ss << "texel.rgb = texel2.rgb * constant2.rgb + texel.rgb;" << std::endl; + ss << "texel.a = texel0.a * diffuseColor.a" << std::endl; + ss << "texel.a = texel1.a * texel.a;" << std::endl; + } + else + { + ss << "texel.rgb = texel0.rgb * constant0.rgb + color.rgb;" << std::endl; + ss << "texel.rgb = texel1.rgb * texel.rgb;" << std::endl; + ss << "texel.a = texel0.a * diffuseColor.a;" << std::endl; + ss << "texel.a = texel1.a * diffuseColor.a;" << std::endl; + } + + ss << "fragColor = texel;" << std::endl; + addAlphaTest(); + ss << "}" << std::endl; + } + + + void CGLSLShaderGenerator::generateWaterPS() + { + bool diffuse = false; + if( material->getTexture( 3 ) != NULL ) + diffuse = true; + + ss << "smooth in texCoord0;" << std::endl; + ss << "smooth in texCoord1;" << std::endl; + ss << "smooth in texCoord2;" << std::endl; + + if( diffuse ) + ss << "smooth in texCoord3;" << std::endl; + + ss << "flat in vec4 bump0ScaleBias;" << std::endl; + ss << "flat in vec4 bump1ScaleBias;" << std::endl; + + ss << std::endl; + + ss << "uniform sampler2D sampler0;" << std::endl; + ss << "uniform sampler2D sampler1;" << std::endl; + ss << "uniform sampler2D sampler2;" << std::endl; + + if( diffuse ) + ss << "uniform sampler2D sampler3;" << std::endl; + + addAlphaTreshold(); + + ss << std::endl; + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + ss << "vec4 texel0 = texture2D( sampler0, texCoord0 );" << std::endl; + ss << "texel0 = texel0 * bump0ScaleBias.xxxx + bump0ScaleBias.yyzz;" << std::endl; + ss << "texel0 = texel0 + texCoord1;" << std::endl; + ss << "vec4 texel1 = texture2D( sampler1, texel0 );" << std::endl; + ss << "texel1 = texel1 * bump1ScaleBias.xxxx + bump1ScaleBias.yyzz;" << std::endl; + ss << "texel1 = texel1 + texCoord2;" << std::endl; + ss << "vec4 texel2 = texture2D( sampler2, texel1 );" << std::endl; + + if( diffuse ) + { + ss << "vec4 texel3 = texture2D( sampler3, texCoord3 );" << std::endl; + ss << "texel3 = texel3 * texel2;" << std::endl; + } + + // No fog yet, so for later + //vec4 tmpFog = clamp( fogValue.x * fogFactor.x + fogFactor.y ); + //vec4 fragColor = mix( texel3, fogColor, tmpFog.x ); + + if( diffuse ) + ss << "fragColor = texel3;" << std::endl; + else + ss << "fragColor = texel2" << std::endl; + + addAlphaTest(); + + ss << "}" << std::endl; + + ss << std::endl; + } + + void CGLSLShaderGenerator::generateCloudPS() + { + ss << "uniform sampler2D sampler0;" << std::endl; + ss << "uniform sampler2D sampler1;" << std::endl; + addDiffuse(); + addAlphaTreshold(); + ss << std::endl; + + ss << "void main( void )" << std::endl; + ss << "{" << std::endl; + ss << "vec4 tex0 = texture2D( sampler0, texCoord0 );" << std::endl; + ss << "vec4 tex1 = texture2D( sampler1, texCoord1 );" << std::endl; + ss << "vec4 tex = mix( tex0, tex1, diffuse.a );" << std::endl; + ss << "tex.a = 0;" << std::endl; + ss << "fragColor = tex;" << std::endl; + addAlphaTest(); + ss << "}" << std::endl; + ss << std::endl; + + } +} + + diff --git a/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.h b/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.h index 7aa824f4f..decdba35a 100644 --- a/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.h +++ b/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.h @@ -1,175 +1,175 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef GLSL_SHADER_GENERATOR -#define GLSL_SHADER_GENERATOR - -#include -#include - -namespace NL3D -{ - class CMaterial; - class CShaderDesc; - - /// GLSL 330+ shader program generator - class CGLSLShaderGenerator - { - public: - CGLSLShaderGenerator(); - ~CGLSLShaderGenerator(); - - /// Resets the generator to 0. - void reset(); - - /// Generate Vertex Shader based on the data provided in material, descriptor and vertexbuffer flags - void generateVS( std::string &vs ); - - /// Generate Pixel Shader based on the data provided in material, descriptor and vertexbuffer flags - void generatePS( std::string &ps ); - - void setMaterial( CMaterial *mat ){ material = mat; } - void setVBFormat( uint16 format ){ vbFormat = format; } - void setShaderDesc( CShaderDesc *d ){ desc = d; } - - private: - /// Adds ambient color constant uniform declaration to the program - void addAmbient(); - - /// Adds diffuse constant uniform declaration to the program - void addDiffuse(); - - /// Adds specular color constant uniform declaration to the program - void addSpecular(); - - /// Adds Color constant uniform declaration to the program - void addColor(); - - /// Adds constant uniform declarations to the program - void addConstants(); - - /// Adds the normal matrix declaration to the program - void addNormalMatrix(); - - void addViewMatrix(); - - //////////////////////////// Alpha Threshold ////////////////// - - /// Adds the alpha threshold uniform to the program - void addAlphaTreshold(); - - /// Adds the actual alpha test to the program ( discards fragment if below threshold ) - void addAlphaTest(); - - ////////////////////////////////////////////////////////////// - - /////////////////////////// Fog /////////////////////////////// - - /// Adds the fog uniforms to the program - void addFogUniform(); - - /// Adds the fog function to the program - void addFogFunction(); - - /// Adds the fog call to the program - void addFog(); - - /////////////////////////////////////////////////////////////// - - //////////////////////////// Lights /////////////////////////// - - /// Adds the Vertex Shader light uniforms to the program - void addLightUniformsVS(); - - /// Adds the Pixel Shader light uniforms to the program - void addLightUniformsFS(); - - /// Adds the Vertex Shader light output variables to the program - void addLightOutsVS(); - - /// Adds the Pixel Shader light output variables to the program - void addLightInsFS(); - - /// Adds the directional light Vertex Shader function, num is the light number - void addDirectionalFunctionVS( int num ); - - /// Adds the point-light Vertex Shader function, num is the light number - void addPointLightFunctionVS( int num ); - - /// Adds the appropriate light functions to the Vertex Shader - void addLightsFunctionVS(); - - /// Adds the appropriate light functions to the Pixel Shader - void addLightsFunctionFS(); - - /// Adds the lights to the Vertex Shader ( calls the appropriate functions ) - void addLightsVS(); - - /// Adds the lights to the Fragment Shader ( calls the appropriate functions ) - void addLightsFS(); - - ////////////////////////////////////////////////////////////// - - - - //////////////////////////////////////// Vertex Shader generation //////////////////////////////////// - - - void generateNormalVS(); - void generateSpecularVS(); - - /// Per-Pixel Lighting - void generatePPLVS(); - - void generateWaterVS(); - - - /////////////////////////////////////////////////////////////////////////////////////////////////////// - - - /////////////////////////////////////// Pixel Shader generation /////////////////////////////////////// - - void generateNormalPS(); - - - void generateTexEnv(); - void generateTexEnvRGB( unsigned int stage ); - void generateTexEnvAlpha( unsigned int stage ); - void buildArg( unsigned int stage, unsigned int n, bool alpha, std::string &arg ); - - void generateLightMapPS(); - void generateSpecularPS(); - - /// Per-Pixel Lighting - void generatePPLPS(); - - void generateWaterPS(); - - void generateCloudPS(); - - //////////////////////////////////////////////////////////////////////////////////////////////////////// - - std::stringstream ss; - uint16 vbFormat; - CMaterial const *material; - CShaderDesc const *desc; - }; -} - -#endif - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef GLSL_SHADER_GENERATOR +#define GLSL_SHADER_GENERATOR + +#include +#include + +namespace NL3D +{ + class CMaterial; + class CShaderDesc; + + /// GLSL 330+ shader program generator + class CGLSLShaderGenerator + { + public: + CGLSLShaderGenerator(); + ~CGLSLShaderGenerator(); + + /// Resets the generator to 0. + void reset(); + + /// Generate Vertex Shader based on the data provided in material, descriptor and vertexbuffer flags + void generateVS( std::string &vs ); + + /// Generate Pixel Shader based on the data provided in material, descriptor and vertexbuffer flags + void generatePS( std::string &ps ); + + void setMaterial( CMaterial *mat ){ material = mat; } + void setVBFormat( uint16 format ){ vbFormat = format; } + void setShaderDesc( CShaderDesc *d ){ desc = d; } + + private: + /// Adds ambient color constant uniform declaration to the program + void addAmbient(); + + /// Adds diffuse constant uniform declaration to the program + void addDiffuse(); + + /// Adds specular color constant uniform declaration to the program + void addSpecular(); + + /// Adds Color constant uniform declaration to the program + void addColor(); + + /// Adds constant uniform declarations to the program + void addConstants(); + + /// Adds the normal matrix declaration to the program + void addNormalMatrix(); + + void addViewMatrix(); + + //////////////////////////// Alpha Threshold ////////////////// + + /// Adds the alpha threshold uniform to the program + void addAlphaTreshold(); + + /// Adds the actual alpha test to the program ( discards fragment if below threshold ) + void addAlphaTest(); + + ////////////////////////////////////////////////////////////// + + /////////////////////////// Fog /////////////////////////////// + + /// Adds the fog uniforms to the program + void addFogUniform(); + + /// Adds the fog function to the program + void addFogFunction(); + + /// Adds the fog call to the program + void addFog(); + + /////////////////////////////////////////////////////////////// + + //////////////////////////// Lights /////////////////////////// + + /// Adds the Vertex Shader light uniforms to the program + void addLightUniformsVS(); + + /// Adds the Pixel Shader light uniforms to the program + void addLightUniformsFS(); + + /// Adds the Vertex Shader light output variables to the program + void addLightOutsVS(); + + /// Adds the Pixel Shader light output variables to the program + void addLightInsFS(); + + /// Adds the directional light Vertex Shader function, num is the light number + void addDirectionalFunctionVS( int num ); + + /// Adds the point-light Vertex Shader function, num is the light number + void addPointLightFunctionVS( int num ); + + /// Adds the appropriate light functions to the Vertex Shader + void addLightsFunctionVS(); + + /// Adds the appropriate light functions to the Pixel Shader + void addLightsFunctionFS(); + + /// Adds the lights to the Vertex Shader ( calls the appropriate functions ) + void addLightsVS(); + + /// Adds the lights to the Fragment Shader ( calls the appropriate functions ) + void addLightsFS(); + + ////////////////////////////////////////////////////////////// + + + + //////////////////////////////////////// Vertex Shader generation //////////////////////////////////// + + + void generateNormalVS(); + void generateSpecularVS(); + + /// Per-Pixel Lighting + void generatePPLVS(); + + void generateWaterVS(); + + + /////////////////////////////////////////////////////////////////////////////////////////////////////// + + + /////////////////////////////////////// Pixel Shader generation /////////////////////////////////////// + + void generateNormalPS(); + + + void generateTexEnv(); + void generateTexEnvRGB( unsigned int stage ); + void generateTexEnvAlpha( unsigned int stage ); + void buildArg( unsigned int stage, unsigned int n, bool alpha, std::string &arg ); + + void generateLightMapPS(); + void generateSpecularPS(); + + /// Per-Pixel Lighting + void generatePPLPS(); + + void generateWaterPS(); + + void generateCloudPS(); + + //////////////////////////////////////////////////////////////////////////////////////////////////////// + + std::stringstream ss; + uint16 vbFormat; + CMaterial const *material; + CShaderDesc const *desc; + }; +} + +#endif + + diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp index 689b8e922..7979c5a78 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp @@ -1,1023 +1,1023 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "driver_opengl.h" -#include "driver_glsl_shader_generator.h" -#include "driver_opengl_vertex_buffer_hard.h" -#include "nel/3d/dynamic_material.h" -#include "nel/3d/usr_shader_manager.h" -#include "nel/3d/usr_shader_program.h" - -namespace -{ - const char *constNames[ NL3D::IDRV_MAT_MAXTEXTURES ] = - { - "constant0", - "constant1", - "constant2", - "constant3" - }; - - - uint16 vertexFlags[ NL3D::CVertexBuffer::NumValue ] = - { - NL3D::CVertexBuffer::PositionFlag, - NL3D::CVertexBuffer::WeightFlag, - NL3D::CVertexBuffer::NormalFlag, - NL3D::CVertexBuffer::PrimaryColorFlag, - NL3D::CVertexBuffer::SecondaryColorFlag, - NL3D::CVertexBuffer::FogFlag, - NL3D::CVertexBuffer::PaletteSkinFlag, - 0, - NL3D::CVertexBuffer::TexCoord0Flag, - NL3D::CVertexBuffer::TexCoord1Flag, - NL3D::CVertexBuffer::TexCoord2Flag, - NL3D::CVertexBuffer::TexCoord3Flag, - NL3D::CVertexBuffer::TexCoord4Flag, - NL3D::CVertexBuffer::TexCoord5Flag, - NL3D::CVertexBuffer::TexCoord6Flag, - NL3D::CVertexBuffer::TexCoord7Flag - }; - - enum AttribOffset - { - Position, - Weight, - Normal, - PrimaryColor, - SecondaryColor, - Fog, - PaletteSkin, - Empty, - TexCoord0, - TexCoord1, - TexCoord2, - TexCoord3, - TexCoord4, - TexCoord5, - TexCoord6, - TexCoord7, - NumOffsets - }; -} - -namespace NL3D -{ - bool CDriverGL3::supportVertexProgram(CVertexProgram::TProfile profile) const - { - if( profile == IProgram::glsl330v ) - return true; - else - return false; - } - - bool CDriverGL3::compileVertexProgram( CVertexProgram *program ) - { - if( program->m_DrvInfo != NULL ) - return false; - - IProgram::CSource *src = NULL; - for( int i = 0; i < program->getSourceNb(); i++ ) - { - src = program->getSource( i ); - if( src->Profile == IProgram::glsl330v ) - break; - - src = NULL; - } - if( src == NULL ) - return false; - - const char *s = src->SourcePtr; - glGetError(); - unsigned int id = nglCreateShaderProgramv( GL_VERTEX_SHADER, 1, &s ); - - if( id == 0 ) - return false; - - GLint ok; - nglGetProgramiv( id, GL_LINK_STATUS, &ok ); - if( ok == 0 ) - { - char errorLog[ 1024 ]; - nglGetProgramInfoLog( id, 1024, NULL, errorLog ); - nlinfo( "%s", errorLog ); - return false; - } - - - GLenum error = glGetError(); - if( error != GL_NO_ERROR ) - return false; - - ItGPUPrgDrvInfoPtrList it = _GPUPrgDrvInfos.insert( _GPUPrgDrvInfos.end(),(NL3D::IProgramDrvInfos*)NULL ); - CVertexProgramDrvInfosGL3 *drvInfo = new CVertexProgramDrvInfosGL3( this, it ); - *it = drvInfo; - - program->m_DrvInfo = drvInfo; - - drvInfo->setProgramId( id ); - - program->buildInfo( src ); - - return true; - } - - bool CDriverGL3::activeVertexProgram( CVertexProgram *program ) - { - if( program == NULL ) - return true; - - IProgramDrvInfos *di = program->m_DrvInfo; - CVertexProgramDrvInfosGL3 *drvInfo = dynamic_cast< CVertexProgramDrvInfosGL3* >( di ); - if( drvInfo == NULL ) - return false; - - glGetError(); - - nglUseProgramStages( ppoId, GL_VERTEX_SHADER_BIT, drvInfo->getProgramId() ); - - GLenum error = glGetError(); - if( error != GL_NO_ERROR ) - { - - return false; - } - - currentProgram.vp = program; - - return true; - } - - bool CDriverGL3::supportPixelProgram( IProgram::TProfile profile ) const - { - if( profile == IProgram::glsl330f ) - return true; - else - return false; - } - - bool CDriverGL3::compilePixelProgram( CPixelProgram *program ) - { - if( program->m_DrvInfo != NULL ) - return false; - - IProgram::CSource *src = NULL; - - for( int i = 0; i < program->getSourceNb(); i++ ) - { - src = program->getSource( i ); - if( src->Profile == IProgram::glsl330f ) - break; - - src = NULL; - } - - if( src == NULL ) - return false; - - const char *s = src->SourcePtr; - glGetError(); - unsigned int id = nglCreateShaderProgramv( GL_FRAGMENT_SHADER, 1, &s ); - if( id == 0 ) - return false; - - GLint ok; - nglGetProgramiv( id, GL_LINK_STATUS, &ok ); - if( ok == 0 ) - { - char errorLog[ 1024 ]; - nglGetProgramInfoLog( id, 1024, NULL, errorLog ); - nlinfo( "%s", errorLog ); - return false; - } - - GLenum error = glGetError(); - if( error != GL_NO_ERROR ) - return false; - - ItGPUPrgDrvInfoPtrList it = _GPUPrgDrvInfos.insert( _GPUPrgDrvInfos.end(), ( NL3D::IProgramDrvInfos* )NULL ); - CPixelProgramDrvInfosGL3 *drvInfo = new CPixelProgramDrvInfosGL3( this, it ); - *it = drvInfo; - drvInfo->setProgramId( id ); - program->m_DrvInfo = drvInfo; - - program->buildInfo( src ); - - return true; - } - - bool CDriverGL3::activePixelProgram( CPixelProgram *program ) - { - if( program == NULL ) - return true; - - if( program->m_DrvInfo == NULL ) - return false; - - IProgramDrvInfos *di = program->m_DrvInfo; - CPixelProgramDrvInfosGL3 *drvInfo = dynamic_cast< CPixelProgramDrvInfosGL3* >( di ); - if( drvInfo == NULL ) - return false; - - glGetError(); - - nglUseProgramStages( ppoId, GL_FRAGMENT_SHADER_BIT, drvInfo->getProgramId() ); - - GLenum error = glGetError(); - if( error != GL_NO_ERROR ) - { - return false; - } - - currentProgram.pp = program; - - return true; - } - - - uint32 CDriverGL3::getProgramId( TProgram program ) const - { - uint32 id = 0; - - switch( program ) - { - case IDriver::VertexProgram: - { - if( currentProgram.vp != NULL ) - { - IProgramDrvInfos *di = currentProgram.vp->m_DrvInfo; - CVertexProgramDrvInfosGL3 *drvInfo = dynamic_cast< CVertexProgramDrvInfosGL3* >( di ); - if( drvInfo != NULL ) - id = drvInfo->getProgramId(); - } - } - break; - - case IDriver::PixelProgram: - if( currentProgram.pp != NULL ) - { - IProgramDrvInfos *di = currentProgram.pp->m_DrvInfo; - CPixelProgramDrvInfosGL3 *drvInfo = dynamic_cast< CPixelProgramDrvInfosGL3* >( di ); - if( drvInfo != NULL ) - id = drvInfo->getProgramId(); - } - break; - - case IDriver::GeometryProgram: - break; - } - - return id; - } - - IProgram* CDriverGL3::getProgram( TProgram program ) const - { - IProgram *p = NULL; - - switch( program ) - { - case IDriver::VertexProgram: - p = currentProgram.vp; - break; - - case IDriver::PixelProgram: - p = currentProgram.pp; - break; - - case IDriver::GeometryProgram: - p = currentProgram.gp; - break; - } - - return p; - } - - int CDriverGL3::getUniformLocation( TProgram program, const char *name ) - { - uint32 id = getProgramId( program ); - return nglGetUniformLocation( id, name ); - } - - void CDriverGL3::setUniform1f( TProgram program, uint index, float f0 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform1f( id, index, f0 ); - } - - void CDriverGL3::setUniform2f( TProgram program, uint index, float f0, float f1 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform2f( id, index, f0, f1 ); - } - - void CDriverGL3::setUniform3f( TProgram program, uint index, float f0, float f1, float f2 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform3f( id, index, f0, f1, f2 ); - } - - void CDriverGL3::setUniform4f( TProgram program, uint index, float f0, float f1, float f2, float f3 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4f( id, index, f0, f1, f2, f3 ); - } - - void CDriverGL3::setUniform1i( TProgram program, uint index, sint32 i0 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform1i( id, index, i0 ); - } - - void CDriverGL3::setUniform2i( TProgram program, uint index, sint32 i0, sint32 i1 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform2i( id, index, i0, i1 ); - } - - void CDriverGL3::setUniform3i( TProgram program, uint index, sint32 i0, sint32 i1, sint32 i2 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform3i( id, index, i0, i1, i2 ); - } - - void CDriverGL3::setUniform4i( TProgram program, uint index, sint32 i0, sint32 i1, sint32 i2, sint32 i3 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4i( id, index, i0, i1, i2, i3 ); - } - - void CDriverGL3::setUniform1ui( TProgram program, uint index, uint32 ui0 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform1ui( id, index, ui0 ); - } - - void CDriverGL3::setUniform2ui( TProgram program, uint index, uint32 ui0, uint32 ui1 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform2ui( id, index, ui0, ui1 ); - } - - void CDriverGL3::setUniform3ui( TProgram program, uint index, uint32 ui0, uint32 ui1, uint32 ui2 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform3ui( id, index, ui0, ui1, ui2 ); - } - - void CDriverGL3::setUniform4ui( TProgram program, uint index, uint32 ui0, uint32 ui1, uint32 ui2, uint32 ui3 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4ui( id, index, ui0, ui1, ui2, ui3 ); - } - - void CDriverGL3::setUniform3f( TProgram program, uint index, const CVector &v ) - { - uint32 id = getProgramId( program ); - nglProgramUniform3f( id, index, v.x, v.y, v.z ); - } - - void CDriverGL3::setUniform4f( TProgram program, uint index, const CVector &v, float f3 ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4f( id, index, v.x, v.y, v.z, f3 ); - } - - void CDriverGL3::setUniform4f( TProgram program, uint index, const NLMISC::CRGBAF& rgba ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4f( id, index, rgba.R, rgba.G, rgba.B, rgba.A ); - } - - void CDriverGL3::setUniform3x3f( TProgram program, uint index, const float *src ) - { - uint32 id = getProgramId( program ); - nglProgramUniformMatrix3fv( id, index, 1, false, src ); - } - - void CDriverGL3::setUniform4x4f( TProgram program, uint index, const CMatrix &m ) - { - uint32 id = getProgramId( program ); - nglProgramUniformMatrix4fv( id, index, 1, false, m.get() ); - } - - void CDriverGL3::setUniform4x4f( TProgram program, uint index, const float *src ) - { - uint32 id = getProgramId( program ); - nglProgramUniformMatrix4fv( id, index, 1, false, src ); - } - - void CDriverGL3::setUniform4fv( TProgram program, uint index, size_t num, const float *src ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4fv( id, index, num, src ); - } - - void CDriverGL3::setUniform4iv( TProgram program, uint index, size_t num, const sint32 *src ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4iv( id, index, num, src ); - } - - void CDriverGL3::setUniform4uiv( TProgram program, uint index, size_t num, const uint32 *src ) - { - uint32 id = getProgramId( program ); - nglProgramUniform4uiv( id, index, num, src ); - } - - void CDriverGL3::setUniformMatrix( TProgram program, uint index, TMatrix matrix, TTransform transform ) - { - uint32 id = getProgramId( program ); - CMatrix mat; - - - switch( matrix ) - { - case IDriver::ModelView: - mat = _ModelViewMatrix; - break; - case IDriver::Projection: - mat = _GLProjMat; - break; - case IDriver::ModelViewProjection: - mat = _ModelViewMatrix * _GLProjMat; - break; - } - - switch( transform ) - { - case IDriver::Inverse: - mat.invert(); - break; - case IDriver::Transpose: - mat.transpose(); - break; - case IDriver::InverseTranspose: - mat.transpose(); - mat.invert(); - break; - } - - - nglProgramUniformMatrix4fv( id, index, 1, false, mat.get() ); - } - - void CDriverGL3::setUniformFog( TProgram program, uint index ) - { - uint32 id = getProgramId( program ); - const float *v = _ModelViewMatrix.get(); - nglProgramUniform4f( id, index, -v[ 2 ], -v[ 6 ], -v[ 10 ], -v[ 4 ] ); - } - - void CDriverGL3::generateShaderDesc( CShaderDesc &desc, CMaterial &mat ) - { - desc.setShaderType( mat.getShader() ); - desc.setVBFlags( _CurrentVertexBufferHard->VB->getVertexFormat() ); - - if( mat.getShader() == CMaterial::LightMap ) - desc.setNLightMaps( mat._LightMaps.size() ); - - //int i = 0; - - if( mat.getShader() == CMaterial::Normal ) - { - bool useTextures = false; - - int maxTextures = std::min( int( SHADER_MAX_TEXTURES ), int( IDRV_MAT_MAXTEXTURES ) ); - for( int i = 0; i < maxTextures; i++ ) - { - desc.setTexEnvMode( i, mat.getTexEnvMode( i ) ); - } - - for( int i = 0; i < maxTextures; i++ ) - { - if( desc.hasVBFlags( vertexFlags[ TexCoord0 + i ] ) ) - { - desc.setUseTexStage( i, true ); - useTextures = true; - } - } - - if( useTextures && !desc.getUseTexStage( 1 ) ) - { - for( int i = 1; i < maxTextures; i++ ) - { - if( mat.getTexture( i ) != NULL ) - { - desc.setUseTexStage( i, true ); - desc.setUseFirstTexCoords( true ); - } - } - } - else - if( !useTextures ) - { - desc.setNoTextures( true ); - } - } - - if( mat.getAlphaTest() ) - { - desc.setAlphaTest( true ); - desc.setAlphaTestThreshold( mat.getAlphaTestThreshold() ); - } - - if( fogEnabled() ) - { - desc.setFog( true ); - desc.setFogMode( CShaderDesc::Linear ); - } - - int maxLights = std::min( int( SHADER_MAX_LIGHTS ), int( MaxLight ) ); - bool enableLights = false; - for( int i = 0; i < maxLights; i++ ) - { - if( !_UserLightEnable[ i ] ) - continue; - - enableLights = true; - - switch( _LightMode[ i ] ) - { - case CLight::DirectionalLight: - desc.setLight( i, CShaderDesc::Directional ); - break; - - case CLight::PointLight: - desc.setLight( i, CShaderDesc::Point ); - break; - - case CLight::SpotLight: - desc.setLight( i, CShaderDesc::Spot ); - break; - } - - } - - desc.setLighting( enableLights ); - } - - - bool CDriverGL3::setupProgram( CMaterial &mat ) - { - if( mat.getDynMat() != NULL ) - return true; - - CVertexProgram *vp = NULL; - CPixelProgram *pp = NULL; - SShaderPair sp; - - CShaderDesc desc; - - generateShaderDesc( desc, mat ); - - // See if we've already generated and compiled this shader - sp = shaderCache.findShader( desc ); - - // Yes we have! - if( !sp.empty() ) - { - if( currentProgram.vp == NULL ) - { - if( !activeVertexProgram( sp.vp ) ) - return false; - } - - if( currentProgram.pp == NULL ) - { - if( !activePixelProgram( sp.pp ) ) - return false; - } - } - // No we need to generate it now - else - { - std::string vs; - std::string ps; - bool cacheShaders = true; - - shaderGenerator->reset(); - shaderGenerator->setMaterial( &mat ); - shaderGenerator->setVBFormat( _CurrentVertexBufferHard->VB->getVertexFormat() ); - shaderGenerator->setShaderDesc( &desc ); - - // If we don't already have a vertex program attached, we'll generate it now - if( currentProgram.vp == NULL ) - { - shaderGenerator->generateVS( vs ); - vp = new CVertexProgram(); - { - IProgram::CSource *src = new IProgram::CSource(); - src->Profile = IProgram::glsl330v; - src->DisplayName = ""; - src->setSource( vs ); - vp->addSource( src ); - } - - if( !compileVertexProgram( vp ) ) - { - delete vp; - vp = NULL; - return false; - } - - if( !activeVertexProgram( vp ) ) - { - delete vp; - vp = NULL; - return false; - } - } - else - cacheShaders = false; - - // If we don't already have a pixel program attached, we'll generate it now - if( currentProgram.pp == NULL ) - { - shaderGenerator->generatePS( ps ); - pp = new CPixelProgram(); - { - IProgram::CSource *src = new IProgram::CSource(); - src->Profile = IProgram::glsl330f; - src->DisplayName = ""; - src->setSource( ps ); - pp->addSource( src ); - } - - if( !compilePixelProgram( pp ) ) - { - delete vp; - vp = NULL; - delete pp; - pp = NULL; - return false; - } - - if( !activePixelProgram( pp ) ) - { - delete vp; - vp = NULL; - delete pp; - pp = NULL; - return false; - } - } - else - cacheShaders = false; - - - // If we already have a shader attached we won't cache this shaderpair, since we didn't generate it - if( cacheShaders ) - { - sp.vp = vp; - sp.pp = pp; - desc.setShaders( sp ); - shaderCache.cacheShader( desc ); - } - } - - setupUniforms(); - - return true; - } - - bool CDriverGL3::setupDynMatProgram( CMaterial& mat, uint pass ) - { - if( ( currentProgram.vp != NULL ) && ( currentProgram.pp != NULL ) ) - return true; - - CDynMaterial *m = mat.getDynMat(); - const SRenderPass *rp = m->getPass( pass ); - std::string shaderRef; - rp->getShaderRef( shaderRef ); - - NL3D::CUsrShaderProgram prg; - - if( !usrShaderManager->getShader( shaderRef, &prg ) ) - return false; - - std::string shaderSource; - std::string log; - std::string name; - - if( currentProgram.vp == NULL ) - { - prg.getVP( shaderSource ); - prg.getName( name ); - - CVertexProgram *vp = new CVertexProgram(); - { - IProgram::CSource *src = new IProgram::CSource(); - src->Profile = IProgram::glsl330v; - src->DisplayName = name; - src->setSource( shaderSource.c_str() ); - vp->addSource( src ); - } - - if( !compileVertexProgram( vp ) ) - { - delete vp; - return false; - } - - if( !activeVertexProgram( vp ) ) - { - delete vp; - return false; - } - - if( currentProgram.dynmatVP != NULL ) - delete currentProgram.dynmatVP; - currentProgram.dynmatVP = vp; - - } - - if( currentProgram.pp == NULL ) - { - - CPixelProgram *pp = new CPixelProgram(); - - prg.getFP( shaderSource ); - { - IProgram::CSource *src = new IProgram::CSource(); - src->Profile = IProgram::glsl330f; - src->DisplayName = name; - src->setSource( shaderSource.c_str() ); - pp->addSource( src ); - } - - if( !compilePixelProgram( pp ) ) - { - delete pp; - return false; - } - - if( !activePixelProgram( pp ) ) - { - delete pp; - return false; - } - - if( currentProgram.dynmatPP != NULL ) - delete currentProgram.dynmatPP; - currentProgram.dynmatPP = pp; - - } - - return true; - } - - - void CDriverGL3::setupUniforms() - { - setupUniforms( IDriver::VertexProgram); - setupUniforms( IDriver::PixelProgram ); - } - - void CDriverGL3::setupUniforms( TProgram program ) - { - CMaterial &mat = *_CurrentMaterial; - IProgram *p = getProgram( program ); - - int mvpIndex = p->getUniformIndex( CProgramIndex::ModelViewProjection ); - if( mvpIndex != -1 ) - { - CMatrix mvp = _GLProjMat * _ModelViewMatrix; - setUniform4x4f( program, mvpIndex, mvp ); - } - - int vmIndex = p->getUniformIndex( CProgramIndex::ViewMatrix ); - if( vmIndex != -1 ) - { - setUniform4x4f( program, vmIndex, _ViewMtx ); - } - - int mvIndex = p->getUniformIndex( CProgramIndex::ModelView ); - if( mvIndex != -1 ) - { - setUniform4x4f( program, mvIndex, _ModelViewMatrix ); - } - - int nmIdx = p->getUniformIndex( CProgramIndex::NormalMatrix ); - if( nmIdx != -1 ) - { - // normal matrix is the inverse-transpose of the rotation part of the modelview matrix - // Inverse-transpose of the rotation matrix, is itself - const float *mv = _ModelViewMatrix.get(); - float nm[ 3 * 3 ]; - nm[ 0 ] = mv[ 0 ]; - nm[ 1 ] = mv[ 1 ]; - nm[ 2 ] = mv[ 2 ]; - nm[ 3 ] = mv[ 4 ]; - nm[ 4 ] = mv[ 5 ]; - nm[ 5 ] = mv[ 6 ]; - nm[ 6 ] = mv[ 8 ]; - nm[ 7 ] = mv[ 9 ]; - nm[ 8 ] = mv[ 10 ]; - - setUniform3x3f( program, nmIdx, nm ); - } - - int fogStartIdx = p->getUniformIndex( CProgramIndex::FogStart ); - if( fogStartIdx != -1 ) - { - setUniform1f( program, fogStartIdx, getFogStart() ); - } - - int fogEndIdx = p->getUniformIndex( CProgramIndex::FogEnd ); - if( fogEndIdx != -1 ) - { - setUniform1f( program, fogEndIdx, getFogEnd() ); - } - - int fogColorIdx = p->getUniformIndex( CProgramIndex::FogColor ); - if( fogColorIdx != -1 ) - { - GLfloat glCol[ 4 ]; - CRGBA col = getFogColor(); - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - setUniform4f( program, fogColorIdx, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - - int colorIndex = p->getUniformIndex( CProgramIndex::Color ); - if( colorIndex != -1 ) - { - GLfloat glCol[ 4 ]; - CRGBA col = mat.getColor(); - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - - setUniform4f( program, colorIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - - int diffuseIndex = p->getUniformIndex( CProgramIndex::DiffuseColor ); - if( diffuseIndex != -1 ) - { - GLfloat glCol[ 4 ]; - CRGBA col = mat.getDiffuse(); - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - - setUniform4f( program, diffuseIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - - - int maxLights = std::min( int( MaxLight ), int( SHADER_MAX_LIGHTS ) ); - for( int i = 0; i < maxLights; i++ ) - { - if( !_UserLightEnable[ i ] ) - continue; - - ////////////////// Temporary insanity /////////////////////////////// - if( ( _LightMode[ i ] != CLight::DirectionalLight ) && ( _LightMode[ i ] != CLight::PointLight ) ) - continue; - ////////////////////////////////////////////////////////////////////// - - if( _LightMode[ i ] == CLight::DirectionalLight ) - { - int ld = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0DirOrPos + i ) ); - if( ld != -1 ) - { - CVector v = -1 * _UserLight[ i ].getDirection(); - setUniform3f( program, ld, v.x, v.y, v.z ); - } - } - else - { - int lp = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0DirOrPos + i ) ); - if( lp != -1 ) - { - CVector v = _UserLight[ i ].getPosition(); - float pos[ 3 ]; - pos[ 0 ] = v.x - _PZBCameraPos.x; - pos[ 1 ] = v.y - _PZBCameraPos.y; - pos[ 2 ] = v.z - _PZBCameraPos.z; - setUniform3f( program, lp, pos[ 0 ], pos[ 1 ], pos[ 2 ] ); - } - } - - int ldc = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ColDiff + i ) ); - if( ldc != -1 ) - { - GLfloat glCol[ 4 ]; - CRGBA col = _UserLight[ i ].getDiffuse(); - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - setUniform4f( program, ldc, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - - int lsc = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ColSpec + i ) ); - if( lsc != -1 ) - { - GLfloat glCol[ 4 ]; - CRGBA col = _UserLight[ i ].getSpecular(); - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - setUniform4f( program, lsc, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - - int shl = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0Shininess + i ) ); - if( shl != -1 ) - { - setUniform1f( program, shl, mat.getShininess() ); - } - - int lac = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ColAmb + i ) ); - if( lac != -1 ) - { - GLfloat glCol[ 4 ]; - CRGBA col; - if( mat.getShader() == CMaterial::LightMap ) - col = _UserLight[ i ].getAmbiant(); - else - col.add( _UserLight[ i ].getAmbiant(), mat.getEmissive() ); - - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - setUniform4f( program, lac, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - - int lca = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ConstAttn + i ) ); - if( lca != -1 ) - { - setUniform1f( program, lca, _UserLight[ i ].getConstantAttenuation() ); - } - - int lla = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0LinAttn + i ) ); - if( lla != -1 ) - { - setUniform1f( program, lla, _UserLight[ i ].getLinearAttenuation() ); - } - - int lqa = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0QuadAttn + i ) ); - if( lqa != -1 ) - { - setUniform1f( program, lqa, _UserLight[ i ].getQuadraticAttenuation() ); - } - } - - - // Lightmaps have special constants - if( mat.getShader() != CMaterial::LightMap ) - { - - for( int i = 0; i < IDRV_MAT_MAXTEXTURES; i++ ) - { - int cl = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Constant0 + i ) ); - if( cl != -1 ) - { - CRGBA col = mat._TexEnvs[ i ].ConstantColor; - GLfloat glCol[ 4 ]; - glCol[ 0 ] = col.R / 255.0f; - glCol[ 1 ] = col.G / 255.0f; - glCol[ 2 ] = col.B / 255.0f; - glCol[ 3 ] = col.A / 255.0f; - - setUniform4f( program, cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); - } - } - - } - } - - bool CDriverGL3::initPipeline() - { - ppoId = 0; - - nglGenProgramPipelines( 1, &ppoId ); - if( ppoId == 0 ) - return false; - - nglBindProgramPipeline( ppoId ); - - return true; - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "driver_opengl.h" +#include "driver_glsl_shader_generator.h" +#include "driver_opengl_vertex_buffer_hard.h" +#include "nel/3d/dynamic_material.h" +#include "nel/3d/usr_shader_manager.h" +#include "nel/3d/usr_shader_program.h" + +namespace +{ + const char *constNames[ NL3D::IDRV_MAT_MAXTEXTURES ] = + { + "constant0", + "constant1", + "constant2", + "constant3" + }; + + + uint16 vertexFlags[ NL3D::CVertexBuffer::NumValue ] = + { + NL3D::CVertexBuffer::PositionFlag, + NL3D::CVertexBuffer::WeightFlag, + NL3D::CVertexBuffer::NormalFlag, + NL3D::CVertexBuffer::PrimaryColorFlag, + NL3D::CVertexBuffer::SecondaryColorFlag, + NL3D::CVertexBuffer::FogFlag, + NL3D::CVertexBuffer::PaletteSkinFlag, + 0, + NL3D::CVertexBuffer::TexCoord0Flag, + NL3D::CVertexBuffer::TexCoord1Flag, + NL3D::CVertexBuffer::TexCoord2Flag, + NL3D::CVertexBuffer::TexCoord3Flag, + NL3D::CVertexBuffer::TexCoord4Flag, + NL3D::CVertexBuffer::TexCoord5Flag, + NL3D::CVertexBuffer::TexCoord6Flag, + NL3D::CVertexBuffer::TexCoord7Flag + }; + + enum AttribOffset + { + Position, + Weight, + Normal, + PrimaryColor, + SecondaryColor, + Fog, + PaletteSkin, + Empty, + TexCoord0, + TexCoord1, + TexCoord2, + TexCoord3, + TexCoord4, + TexCoord5, + TexCoord6, + TexCoord7, + NumOffsets + }; +} + +namespace NL3D +{ + bool CDriverGL3::supportVertexProgram(CVertexProgram::TProfile profile) const + { + if( profile == IProgram::glsl330v ) + return true; + else + return false; + } + + bool CDriverGL3::compileVertexProgram( CVertexProgram *program ) + { + if( program->m_DrvInfo != NULL ) + return false; + + IProgram::CSource *src = NULL; + for( int i = 0; i < program->getSourceNb(); i++ ) + { + src = program->getSource( i ); + if( src->Profile == IProgram::glsl330v ) + break; + + src = NULL; + } + if( src == NULL ) + return false; + + const char *s = src->SourcePtr; + glGetError(); + unsigned int id = nglCreateShaderProgramv( GL_VERTEX_SHADER, 1, &s ); + + if( id == 0 ) + return false; + + GLint ok; + nglGetProgramiv( id, GL_LINK_STATUS, &ok ); + if( ok == 0 ) + { + char errorLog[ 1024 ]; + nglGetProgramInfoLog( id, 1024, NULL, errorLog ); + nlinfo( "%s", errorLog ); + return false; + } + + + GLenum error = glGetError(); + if( error != GL_NO_ERROR ) + return false; + + ItGPUPrgDrvInfoPtrList it = _GPUPrgDrvInfos.insert( _GPUPrgDrvInfos.end(),(NL3D::IProgramDrvInfos*)NULL ); + CVertexProgramDrvInfosGL3 *drvInfo = new CVertexProgramDrvInfosGL3( this, it ); + *it = drvInfo; + + program->m_DrvInfo = drvInfo; + + drvInfo->setProgramId( id ); + + program->buildInfo( src ); + + return true; + } + + bool CDriverGL3::activeVertexProgram( CVertexProgram *program ) + { + if( program == NULL ) + return true; + + IProgramDrvInfos *di = program->m_DrvInfo; + CVertexProgramDrvInfosGL3 *drvInfo = dynamic_cast< CVertexProgramDrvInfosGL3* >( di ); + if( drvInfo == NULL ) + return false; + + glGetError(); + + nglUseProgramStages( ppoId, GL_VERTEX_SHADER_BIT, drvInfo->getProgramId() ); + + GLenum error = glGetError(); + if( error != GL_NO_ERROR ) + { + + return false; + } + + currentProgram.vp = program; + + return true; + } + + bool CDriverGL3::supportPixelProgram( IProgram::TProfile profile ) const + { + if( profile == IProgram::glsl330f ) + return true; + else + return false; + } + + bool CDriverGL3::compilePixelProgram( CPixelProgram *program ) + { + if( program->m_DrvInfo != NULL ) + return false; + + IProgram::CSource *src = NULL; + + for( int i = 0; i < program->getSourceNb(); i++ ) + { + src = program->getSource( i ); + if( src->Profile == IProgram::glsl330f ) + break; + + src = NULL; + } + + if( src == NULL ) + return false; + + const char *s = src->SourcePtr; + glGetError(); + unsigned int id = nglCreateShaderProgramv( GL_FRAGMENT_SHADER, 1, &s ); + if( id == 0 ) + return false; + + GLint ok; + nglGetProgramiv( id, GL_LINK_STATUS, &ok ); + if( ok == 0 ) + { + char errorLog[ 1024 ]; + nglGetProgramInfoLog( id, 1024, NULL, errorLog ); + nlinfo( "%s", errorLog ); + return false; + } + + GLenum error = glGetError(); + if( error != GL_NO_ERROR ) + return false; + + ItGPUPrgDrvInfoPtrList it = _GPUPrgDrvInfos.insert( _GPUPrgDrvInfos.end(), ( NL3D::IProgramDrvInfos* )NULL ); + CPixelProgramDrvInfosGL3 *drvInfo = new CPixelProgramDrvInfosGL3( this, it ); + *it = drvInfo; + drvInfo->setProgramId( id ); + program->m_DrvInfo = drvInfo; + + program->buildInfo( src ); + + return true; + } + + bool CDriverGL3::activePixelProgram( CPixelProgram *program ) + { + if( program == NULL ) + return true; + + if( program->m_DrvInfo == NULL ) + return false; + + IProgramDrvInfos *di = program->m_DrvInfo; + CPixelProgramDrvInfosGL3 *drvInfo = dynamic_cast< CPixelProgramDrvInfosGL3* >( di ); + if( drvInfo == NULL ) + return false; + + glGetError(); + + nglUseProgramStages( ppoId, GL_FRAGMENT_SHADER_BIT, drvInfo->getProgramId() ); + + GLenum error = glGetError(); + if( error != GL_NO_ERROR ) + { + return false; + } + + currentProgram.pp = program; + + return true; + } + + + uint32 CDriverGL3::getProgramId( TProgram program ) const + { + uint32 id = 0; + + switch( program ) + { + case IDriver::VertexProgram: + { + if( currentProgram.vp != NULL ) + { + IProgramDrvInfos *di = currentProgram.vp->m_DrvInfo; + CVertexProgramDrvInfosGL3 *drvInfo = dynamic_cast< CVertexProgramDrvInfosGL3* >( di ); + if( drvInfo != NULL ) + id = drvInfo->getProgramId(); + } + } + break; + + case IDriver::PixelProgram: + if( currentProgram.pp != NULL ) + { + IProgramDrvInfos *di = currentProgram.pp->m_DrvInfo; + CPixelProgramDrvInfosGL3 *drvInfo = dynamic_cast< CPixelProgramDrvInfosGL3* >( di ); + if( drvInfo != NULL ) + id = drvInfo->getProgramId(); + } + break; + + case IDriver::GeometryProgram: + break; + } + + return id; + } + + IProgram* CDriverGL3::getProgram( TProgram program ) const + { + IProgram *p = NULL; + + switch( program ) + { + case IDriver::VertexProgram: + p = currentProgram.vp; + break; + + case IDriver::PixelProgram: + p = currentProgram.pp; + break; + + case IDriver::GeometryProgram: + p = currentProgram.gp; + break; + } + + return p; + } + + int CDriverGL3::getUniformLocation( TProgram program, const char *name ) + { + uint32 id = getProgramId( program ); + return nglGetUniformLocation( id, name ); + } + + void CDriverGL3::setUniform1f( TProgram program, uint index, float f0 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform1f( id, index, f0 ); + } + + void CDriverGL3::setUniform2f( TProgram program, uint index, float f0, float f1 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform2f( id, index, f0, f1 ); + } + + void CDriverGL3::setUniform3f( TProgram program, uint index, float f0, float f1, float f2 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform3f( id, index, f0, f1, f2 ); + } + + void CDriverGL3::setUniform4f( TProgram program, uint index, float f0, float f1, float f2, float f3 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4f( id, index, f0, f1, f2, f3 ); + } + + void CDriverGL3::setUniform1i( TProgram program, uint index, sint32 i0 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform1i( id, index, i0 ); + } + + void CDriverGL3::setUniform2i( TProgram program, uint index, sint32 i0, sint32 i1 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform2i( id, index, i0, i1 ); + } + + void CDriverGL3::setUniform3i( TProgram program, uint index, sint32 i0, sint32 i1, sint32 i2 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform3i( id, index, i0, i1, i2 ); + } + + void CDriverGL3::setUniform4i( TProgram program, uint index, sint32 i0, sint32 i1, sint32 i2, sint32 i3 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4i( id, index, i0, i1, i2, i3 ); + } + + void CDriverGL3::setUniform1ui( TProgram program, uint index, uint32 ui0 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform1ui( id, index, ui0 ); + } + + void CDriverGL3::setUniform2ui( TProgram program, uint index, uint32 ui0, uint32 ui1 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform2ui( id, index, ui0, ui1 ); + } + + void CDriverGL3::setUniform3ui( TProgram program, uint index, uint32 ui0, uint32 ui1, uint32 ui2 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform3ui( id, index, ui0, ui1, ui2 ); + } + + void CDriverGL3::setUniform4ui( TProgram program, uint index, uint32 ui0, uint32 ui1, uint32 ui2, uint32 ui3 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4ui( id, index, ui0, ui1, ui2, ui3 ); + } + + void CDriverGL3::setUniform3f( TProgram program, uint index, const CVector &v ) + { + uint32 id = getProgramId( program ); + nglProgramUniform3f( id, index, v.x, v.y, v.z ); + } + + void CDriverGL3::setUniform4f( TProgram program, uint index, const CVector &v, float f3 ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4f( id, index, v.x, v.y, v.z, f3 ); + } + + void CDriverGL3::setUniform4f( TProgram program, uint index, const NLMISC::CRGBAF& rgba ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4f( id, index, rgba.R, rgba.G, rgba.B, rgba.A ); + } + + void CDriverGL3::setUniform3x3f( TProgram program, uint index, const float *src ) + { + uint32 id = getProgramId( program ); + nglProgramUniformMatrix3fv( id, index, 1, false, src ); + } + + void CDriverGL3::setUniform4x4f( TProgram program, uint index, const CMatrix &m ) + { + uint32 id = getProgramId( program ); + nglProgramUniformMatrix4fv( id, index, 1, false, m.get() ); + } + + void CDriverGL3::setUniform4x4f( TProgram program, uint index, const float *src ) + { + uint32 id = getProgramId( program ); + nglProgramUniformMatrix4fv( id, index, 1, false, src ); + } + + void CDriverGL3::setUniform4fv( TProgram program, uint index, size_t num, const float *src ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4fv( id, index, num, src ); + } + + void CDriverGL3::setUniform4iv( TProgram program, uint index, size_t num, const sint32 *src ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4iv( id, index, num, src ); + } + + void CDriverGL3::setUniform4uiv( TProgram program, uint index, size_t num, const uint32 *src ) + { + uint32 id = getProgramId( program ); + nglProgramUniform4uiv( id, index, num, src ); + } + + void CDriverGL3::setUniformMatrix( TProgram program, uint index, TMatrix matrix, TTransform transform ) + { + uint32 id = getProgramId( program ); + CMatrix mat; + + + switch( matrix ) + { + case IDriver::ModelView: + mat = _ModelViewMatrix; + break; + case IDriver::Projection: + mat = _GLProjMat; + break; + case IDriver::ModelViewProjection: + mat = _ModelViewMatrix * _GLProjMat; + break; + } + + switch( transform ) + { + case IDriver::Inverse: + mat.invert(); + break; + case IDriver::Transpose: + mat.transpose(); + break; + case IDriver::InverseTranspose: + mat.transpose(); + mat.invert(); + break; + } + + + nglProgramUniformMatrix4fv( id, index, 1, false, mat.get() ); + } + + void CDriverGL3::setUniformFog( TProgram program, uint index ) + { + uint32 id = getProgramId( program ); + const float *v = _ModelViewMatrix.get(); + nglProgramUniform4f( id, index, -v[ 2 ], -v[ 6 ], -v[ 10 ], -v[ 4 ] ); + } + + void CDriverGL3::generateShaderDesc( CShaderDesc &desc, CMaterial &mat ) + { + desc.setShaderType( mat.getShader() ); + desc.setVBFlags( _CurrentVertexBufferHard->VB->getVertexFormat() ); + + if( mat.getShader() == CMaterial::LightMap ) + desc.setNLightMaps( mat._LightMaps.size() ); + + //int i = 0; + + if( mat.getShader() == CMaterial::Normal ) + { + bool useTextures = false; + + int maxTextures = std::min( int( SHADER_MAX_TEXTURES ), int( IDRV_MAT_MAXTEXTURES ) ); + for( int i = 0; i < maxTextures; i++ ) + { + desc.setTexEnvMode( i, mat.getTexEnvMode( i ) ); + } + + for( int i = 0; i < maxTextures; i++ ) + { + if( desc.hasVBFlags( vertexFlags[ TexCoord0 + i ] ) ) + { + desc.setUseTexStage( i, true ); + useTextures = true; + } + } + + if( useTextures && !desc.getUseTexStage( 1 ) ) + { + for( int i = 1; i < maxTextures; i++ ) + { + if( mat.getTexture( i ) != NULL ) + { + desc.setUseTexStage( i, true ); + desc.setUseFirstTexCoords( true ); + } + } + } + else + if( !useTextures ) + { + desc.setNoTextures( true ); + } + } + + if( mat.getAlphaTest() ) + { + desc.setAlphaTest( true ); + desc.setAlphaTestThreshold( mat.getAlphaTestThreshold() ); + } + + if( fogEnabled() ) + { + desc.setFog( true ); + desc.setFogMode( CShaderDesc::Linear ); + } + + int maxLights = std::min( int( SHADER_MAX_LIGHTS ), int( MaxLight ) ); + bool enableLights = false; + for( int i = 0; i < maxLights; i++ ) + { + if( !_UserLightEnable[ i ] ) + continue; + + enableLights = true; + + switch( _LightMode[ i ] ) + { + case CLight::DirectionalLight: + desc.setLight( i, CShaderDesc::Directional ); + break; + + case CLight::PointLight: + desc.setLight( i, CShaderDesc::Point ); + break; + + case CLight::SpotLight: + desc.setLight( i, CShaderDesc::Spot ); + break; + } + + } + + desc.setLighting( enableLights ); + } + + + bool CDriverGL3::setupProgram( CMaterial &mat ) + { + if( mat.getDynMat() != NULL ) + return true; + + CVertexProgram *vp = NULL; + CPixelProgram *pp = NULL; + SShaderPair sp; + + CShaderDesc desc; + + generateShaderDesc( desc, mat ); + + // See if we've already generated and compiled this shader + sp = shaderCache.findShader( desc ); + + // Yes we have! + if( !sp.empty() ) + { + if( currentProgram.vp == NULL ) + { + if( !activeVertexProgram( sp.vp ) ) + return false; + } + + if( currentProgram.pp == NULL ) + { + if( !activePixelProgram( sp.pp ) ) + return false; + } + } + // No we need to generate it now + else + { + std::string vs; + std::string ps; + bool cacheShaders = true; + + shaderGenerator->reset(); + shaderGenerator->setMaterial( &mat ); + shaderGenerator->setVBFormat( _CurrentVertexBufferHard->VB->getVertexFormat() ); + shaderGenerator->setShaderDesc( &desc ); + + // If we don't already have a vertex program attached, we'll generate it now + if( currentProgram.vp == NULL ) + { + shaderGenerator->generateVS( vs ); + vp = new CVertexProgram(); + { + IProgram::CSource *src = new IProgram::CSource(); + src->Profile = IProgram::glsl330v; + src->DisplayName = ""; + src->setSource( vs ); + vp->addSource( src ); + } + + if( !compileVertexProgram( vp ) ) + { + delete vp; + vp = NULL; + return false; + } + + if( !activeVertexProgram( vp ) ) + { + delete vp; + vp = NULL; + return false; + } + } + else + cacheShaders = false; + + // If we don't already have a pixel program attached, we'll generate it now + if( currentProgram.pp == NULL ) + { + shaderGenerator->generatePS( ps ); + pp = new CPixelProgram(); + { + IProgram::CSource *src = new IProgram::CSource(); + src->Profile = IProgram::glsl330f; + src->DisplayName = ""; + src->setSource( ps ); + pp->addSource( src ); + } + + if( !compilePixelProgram( pp ) ) + { + delete vp; + vp = NULL; + delete pp; + pp = NULL; + return false; + } + + if( !activePixelProgram( pp ) ) + { + delete vp; + vp = NULL; + delete pp; + pp = NULL; + return false; + } + } + else + cacheShaders = false; + + + // If we already have a shader attached we won't cache this shaderpair, since we didn't generate it + if( cacheShaders ) + { + sp.vp = vp; + sp.pp = pp; + desc.setShaders( sp ); + shaderCache.cacheShader( desc ); + } + } + + setupUniforms(); + + return true; + } + + bool CDriverGL3::setupDynMatProgram( CMaterial& mat, uint pass ) + { + if( ( currentProgram.vp != NULL ) && ( currentProgram.pp != NULL ) ) + return true; + + CDynMaterial *m = mat.getDynMat(); + const SRenderPass *rp = m->getPass( pass ); + std::string shaderRef; + rp->getShaderRef( shaderRef ); + + NL3D::CUsrShaderProgram prg; + + if( !usrShaderManager->getShader( shaderRef, &prg ) ) + return false; + + std::string shaderSource; + std::string log; + std::string name; + + if( currentProgram.vp == NULL ) + { + prg.getVP( shaderSource ); + prg.getName( name ); + + CVertexProgram *vp = new CVertexProgram(); + { + IProgram::CSource *src = new IProgram::CSource(); + src->Profile = IProgram::glsl330v; + src->DisplayName = name; + src->setSource( shaderSource.c_str() ); + vp->addSource( src ); + } + + if( !compileVertexProgram( vp ) ) + { + delete vp; + return false; + } + + if( !activeVertexProgram( vp ) ) + { + delete vp; + return false; + } + + if( currentProgram.dynmatVP != NULL ) + delete currentProgram.dynmatVP; + currentProgram.dynmatVP = vp; + + } + + if( currentProgram.pp == NULL ) + { + + CPixelProgram *pp = new CPixelProgram(); + + prg.getFP( shaderSource ); + { + IProgram::CSource *src = new IProgram::CSource(); + src->Profile = IProgram::glsl330f; + src->DisplayName = name; + src->setSource( shaderSource.c_str() ); + pp->addSource( src ); + } + + if( !compilePixelProgram( pp ) ) + { + delete pp; + return false; + } + + if( !activePixelProgram( pp ) ) + { + delete pp; + return false; + } + + if( currentProgram.dynmatPP != NULL ) + delete currentProgram.dynmatPP; + currentProgram.dynmatPP = pp; + + } + + return true; + } + + + void CDriverGL3::setupUniforms() + { + setupUniforms( IDriver::VertexProgram); + setupUniforms( IDriver::PixelProgram ); + } + + void CDriverGL3::setupUniforms( TProgram program ) + { + CMaterial &mat = *_CurrentMaterial; + IProgram *p = getProgram( program ); + + int mvpIndex = p->getUniformIndex( CProgramIndex::ModelViewProjection ); + if( mvpIndex != -1 ) + { + CMatrix mvp = _GLProjMat * _ModelViewMatrix; + setUniform4x4f( program, mvpIndex, mvp ); + } + + int vmIndex = p->getUniformIndex( CProgramIndex::ViewMatrix ); + if( vmIndex != -1 ) + { + setUniform4x4f( program, vmIndex, _ViewMtx ); + } + + int mvIndex = p->getUniformIndex( CProgramIndex::ModelView ); + if( mvIndex != -1 ) + { + setUniform4x4f( program, mvIndex, _ModelViewMatrix ); + } + + int nmIdx = p->getUniformIndex( CProgramIndex::NormalMatrix ); + if( nmIdx != -1 ) + { + // normal matrix is the inverse-transpose of the rotation part of the modelview matrix + // Inverse-transpose of the rotation matrix, is itself + const float *mv = _ModelViewMatrix.get(); + float nm[ 3 * 3 ]; + nm[ 0 ] = mv[ 0 ]; + nm[ 1 ] = mv[ 1 ]; + nm[ 2 ] = mv[ 2 ]; + nm[ 3 ] = mv[ 4 ]; + nm[ 4 ] = mv[ 5 ]; + nm[ 5 ] = mv[ 6 ]; + nm[ 6 ] = mv[ 8 ]; + nm[ 7 ] = mv[ 9 ]; + nm[ 8 ] = mv[ 10 ]; + + setUniform3x3f( program, nmIdx, nm ); + } + + int fogStartIdx = p->getUniformIndex( CProgramIndex::FogStart ); + if( fogStartIdx != -1 ) + { + setUniform1f( program, fogStartIdx, getFogStart() ); + } + + int fogEndIdx = p->getUniformIndex( CProgramIndex::FogEnd ); + if( fogEndIdx != -1 ) + { + setUniform1f( program, fogEndIdx, getFogEnd() ); + } + + int fogColorIdx = p->getUniformIndex( CProgramIndex::FogColor ); + if( fogColorIdx != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = getFogColor(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + setUniform4f( program, fogColorIdx, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + int colorIndex = p->getUniformIndex( CProgramIndex::Color ); + if( colorIndex != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = mat.getColor(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + + setUniform4f( program, colorIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + int diffuseIndex = p->getUniformIndex( CProgramIndex::DiffuseColor ); + if( diffuseIndex != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = mat.getDiffuse(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + + setUniform4f( program, diffuseIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + + int maxLights = std::min( int( MaxLight ), int( SHADER_MAX_LIGHTS ) ); + for( int i = 0; i < maxLights; i++ ) + { + if( !_UserLightEnable[ i ] ) + continue; + + ////////////////// Temporary insanity /////////////////////////////// + if( ( _LightMode[ i ] != CLight::DirectionalLight ) && ( _LightMode[ i ] != CLight::PointLight ) ) + continue; + ////////////////////////////////////////////////////////////////////// + + if( _LightMode[ i ] == CLight::DirectionalLight ) + { + int ld = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0DirOrPos + i ) ); + if( ld != -1 ) + { + CVector v = -1 * _UserLight[ i ].getDirection(); + setUniform3f( program, ld, v.x, v.y, v.z ); + } + } + else + { + int lp = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0DirOrPos + i ) ); + if( lp != -1 ) + { + CVector v = _UserLight[ i ].getPosition(); + float pos[ 3 ]; + pos[ 0 ] = v.x - _PZBCameraPos.x; + pos[ 1 ] = v.y - _PZBCameraPos.y; + pos[ 2 ] = v.z - _PZBCameraPos.z; + setUniform3f( program, lp, pos[ 0 ], pos[ 1 ], pos[ 2 ] ); + } + } + + int ldc = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ColDiff + i ) ); + if( ldc != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = _UserLight[ i ].getDiffuse(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + setUniform4f( program, ldc, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + int lsc = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ColSpec + i ) ); + if( lsc != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col = _UserLight[ i ].getSpecular(); + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + setUniform4f( program, lsc, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + int shl = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0Shininess + i ) ); + if( shl != -1 ) + { + setUniform1f( program, shl, mat.getShininess() ); + } + + int lac = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ColAmb + i ) ); + if( lac != -1 ) + { + GLfloat glCol[ 4 ]; + CRGBA col; + if( mat.getShader() == CMaterial::LightMap ) + col = _UserLight[ i ].getAmbiant(); + else + col.add( _UserLight[ i ].getAmbiant(), mat.getEmissive() ); + + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + setUniform4f( program, lac, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + + int lca = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0ConstAttn + i ) ); + if( lca != -1 ) + { + setUniform1f( program, lca, _UserLight[ i ].getConstantAttenuation() ); + } + + int lla = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0LinAttn + i ) ); + if( lla != -1 ) + { + setUniform1f( program, lla, _UserLight[ i ].getLinearAttenuation() ); + } + + int lqa = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Light0QuadAttn + i ) ); + if( lqa != -1 ) + { + setUniform1f( program, lqa, _UserLight[ i ].getQuadraticAttenuation() ); + } + } + + + // Lightmaps have special constants + if( mat.getShader() != CMaterial::LightMap ) + { + + for( int i = 0; i < IDRV_MAT_MAXTEXTURES; i++ ) + { + int cl = p->getUniformIndex( CProgramIndex::TName( CProgramIndex::Constant0 + i ) ); + if( cl != -1 ) + { + CRGBA col = mat._TexEnvs[ i ].ConstantColor; + GLfloat glCol[ 4 ]; + glCol[ 0 ] = col.R / 255.0f; + glCol[ 1 ] = col.G / 255.0f; + glCol[ 2 ] = col.B / 255.0f; + glCol[ 3 ] = col.A / 255.0f; + + setUniform4f( program, cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ] ); + } + } + + } + } + + bool CDriverGL3::initPipeline() + { + ppoId = 0; + + nglGenProgramPipelines( 1, &ppoId ); + if( ppoId == 0 ) + return false; + + nglBindProgramPipeline( ppoId ); + + return true; + } +} + + diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.cpp index 11876a9dc..b1b7a8fb9 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.cpp @@ -1,62 +1,62 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "driver_opengl_shader_cache.h" - -namespace NL3D -{ - CShaderCache::CShaderCache() - { - } - - CShaderCache::~CShaderCache() - { - clearCache(); - } - - SShaderPair CShaderCache::findShader( const CShaderDesc &desc ) const - { - for( int i = 0; i < shaders.size(); i++ ) - { - if( shaders[ i ] == desc ) - return shaders[ i ].getShaders(); - } - - return SShaderPair(); - } - - void CShaderCache::cacheShader( CShaderDesc &desc ) - { - shaders.push_back( desc ); - } - - void CShaderCache::clearCache() - { - std::vector< CShaderDesc >::iterator itr = shaders.begin(); - while( itr != shaders.end() ) - { - SShaderPair sp; - sp = itr->getShaders(); - delete sp.vp; - delete sp.pp; - ++itr; - } - - shaders.clear(); - } -} - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "driver_opengl_shader_cache.h" + +namespace NL3D +{ + CShaderCache::CShaderCache() + { + } + + CShaderCache::~CShaderCache() + { + clearCache(); + } + + SShaderPair CShaderCache::findShader( const CShaderDesc &desc ) const + { + for( int i = 0; i < shaders.size(); i++ ) + { + if( shaders[ i ] == desc ) + return shaders[ i ].getShaders(); + } + + return SShaderPair(); + } + + void CShaderCache::cacheShader( CShaderDesc &desc ) + { + shaders.push_back( desc ); + } + + void CShaderCache::clearCache() + { + std::vector< CShaderDesc >::iterator itr = shaders.begin(); + while( itr != shaders.end() ) + { + SShaderPair sp; + sp = itr->getShaders(); + delete sp.vp; + delete sp.pp; + ++itr; + } + + shaders.clear(); + } +} + diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.h b/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.h index d1d7be45d..84d0c4796 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.h +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_shader_cache.h @@ -1,50 +1,50 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef OPENGL_SHADER_CACHE -#define OPENGL_SHADER_CACHE - -#include "driver_opengl_shader_desc.h" -#include - -namespace NL3D -{ - /// Caches generated shaders, so they don't have to be generated every frame - class CShaderCache - { - public: - CShaderCache(); - ~CShaderCache(); - - /// Checks if there's a shader cached that was generated from the specified descriptor - SShaderPair findShader( const CShaderDesc &desc ) const; - - /// Caches a shader with the specified descriptor as key - void cacheShader( CShaderDesc &desc ); - - /// Clears the caches, removes the cached shaders - void clearCache(); - - private: - std::vector< CShaderDesc > shaders; - - }; -} - - -#endif - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef OPENGL_SHADER_CACHE +#define OPENGL_SHADER_CACHE + +#include "driver_opengl_shader_desc.h" +#include + +namespace NL3D +{ + /// Caches generated shaders, so they don't have to be generated every frame + class CShaderCache + { + public: + CShaderCache(); + ~CShaderCache(); + + /// Checks if there's a shader cached that was generated from the specified descriptor + SShaderPair findShader( const CShaderDesc &desc ) const; + + /// Caches a shader with the specified descriptor as key + void cacheShader( CShaderDesc &desc ); + + /// Clears the caches, removes the cached shaders + void clearCache(); + + private: + std::vector< CShaderDesc > shaders; + + }; +} + + +#endif + diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_shader_desc.h b/code/nel/src/3d/driver/opengl3/driver_opengl_shader_desc.h index a116776cf..cb7cce2f8 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_shader_desc.h +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_shader_desc.h @@ -1,278 +1,278 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef SHADER_DESC -#define SHADER_DESC - -#include "nel/misc/types_nl.h" - -#define SHADER_MAX_TEXTURES 4 -#define SHADER_MAX_LIGHTS 8 - -namespace NL3D -{ - class CVertexProgram; - class CPixelProgram; - - struct SShaderPair - { - SShaderPair() - { - vp = NULL; - pp = NULL; - } - - ~SShaderPair() - { - vp = NULL; - pp = NULL; - } - - bool empty() const{ - if( ( vp == NULL ) && ( pp == NULL ) ) - return true; - else - return false; - } - - CVertexProgram *vp; - CPixelProgram *pp; - }; - - class CShaderDesc - { - public: - - enum TShaderType - { - Normal, - Bump_unused, - UserColor, - LightMap, - Specular, - Caustics_unused, - PerPixelLighting, - PerPixelNoSpecular, - Cloud, - Water - }; - - enum TFogMode - { - Exponential, - Exponential2, - Linear - }; - - enum TLightMode - { - Nolight, - Directional, - Point, - Spot - }; - - CShaderDesc(){ - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - texEnvMode[ i ] = 0; - - for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) - lightMode[ i ] = Nolight; - - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - useTextureStage[ i ] = false; - useFirstTextureCoordSet = false; - noTextures = true; - - features = None; - shaderType = Normal; - vbFlags = 0; - nlightmaps = 0; - alphaTestTreshold = 0.5f; - fogMode = Linear; - pointLight = false; - } - - ~CShaderDesc(){ - } - - bool operator==( const CShaderDesc &o ) const - { - if( noTextures != o.noTextures ) - return false; - - if( shaderType != o.shaderType ) - return false; - - if( vbFlags != o.vbFlags ) - return false; - - if( nlightmaps != o.nlightmaps ) - return false; - - if( features != o.features ) - return false; - - if( ( features & AlphaTest ) != 0 ) - { - if( alphaTestTreshold > o.alphaTestTreshold + 0.0001f ) - return false; - - if( alphaTestTreshold < o.alphaTestTreshold - 0.0001f ) - return false; - } - - if( fogEnabled() ) - { - if( fogMode != o.fogMode ) - return false; - } - - if( shaderType == Normal ) - { - if( useFirstTextureCoordSet != o.useFirstTextureCoordSet ) - return false; - - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - if( texEnvMode[ i ] != o.texEnvMode[ i ] ) - return false; - - for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) - if( useTextureStage[ i ] != o.useTextureStage[ i ] ) - return false; - } - - if( lightingEnabled() ) - { - for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) - if( lightMode[ i ] != o.lightMode[ i ] ) - return false; - } - - return true; - } - - void setTexEnvMode( uint32 index, uint32 mode ){ texEnvMode[ index ] = mode; } - - void setUseFirstTexCoords( bool b ){ useFirstTextureCoordSet = b; } - bool getUseFirstTexCoords() const{ return useFirstTextureCoordSet; } - - void setUseTexStage( uint8 stage, bool b ){ useTextureStage[ stage ] = b; } - bool getUseTexStage( uint8 stage ) const{ return useTextureStage[ stage ]; } - - void setNoTextures( bool b ){ noTextures = b; } - bool useTextures() const{ return !noTextures; } - - void setVBFlags( uint32 flags ){ vbFlags = flags; } - bool hasVBFlags( uint32 flags ) const{ - if( ( vbFlags & flags ) != 0 ) - return true; - else - return false; - } - - void setShaderType( uint32 type ){ shaderType = type; } - void setNLightMaps( uint32 n ){ nlightmaps = n; } - - void setAlphaTest( bool b ) - { - if( b ) - features |= AlphaTest; - else - features &= ~AlphaTest; - } - - void setAlphaTestThreshold( float t ){ alphaTestTreshold = t; } - - void setFog( bool b ) - { - if( b ) - features |= Fog; - else - features &= ~Fog; - } - - bool fogEnabled() const - { - if( ( features & Fog ) != 0 ) - return true; - else - return false; - } - - void setFogMode( TFogMode mode ){ fogMode = mode; } - - uint32 getFogMode() const{ return fogMode; } - - void setLighting( bool b ) - { - if( b ) - features |= Lighting; - else - features &= ~Lighting; - } - - bool lightingEnabled() const - { - if( ( features & Lighting ) != 0 ) - return true; - else - return false; - } - - void setLight( int idx, TLightMode mode ) - { - lightMode[ idx ] = mode; - if( mode == Point ) - pointLight = true; - } - - TLightMode getLight( int idx ) const{ return lightMode[ idx ]; } - - bool hasPointLight() const{ return pointLight; } - - void setShaders( SShaderPair sp ){ shaderPair = sp; } - SShaderPair getShaders() const{ return shaderPair; } - - private: - - enum TShaderFeatures - { - None = 0, - AlphaTest = 1, - Fog = 2, - Lighting = 4 - }; - - uint32 features; - uint32 texEnvMode[ SHADER_MAX_TEXTURES ]; - bool useTextureStage[ SHADER_MAX_TEXTURES ]; - bool useFirstTextureCoordSet; - bool noTextures; - uint32 vbFlags; - uint32 shaderType; - uint32 nlightmaps; - float alphaTestTreshold; - uint32 fogMode; - TLightMode lightMode[ SHADER_MAX_LIGHTS ]; - bool pointLight; - - SShaderPair shaderPair; - }; -} - -#endif - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef SHADER_DESC +#define SHADER_DESC + +#include "nel/misc/types_nl.h" + +#define SHADER_MAX_TEXTURES 4 +#define SHADER_MAX_LIGHTS 8 + +namespace NL3D +{ + class CVertexProgram; + class CPixelProgram; + + struct SShaderPair + { + SShaderPair() + { + vp = NULL; + pp = NULL; + } + + ~SShaderPair() + { + vp = NULL; + pp = NULL; + } + + bool empty() const{ + if( ( vp == NULL ) && ( pp == NULL ) ) + return true; + else + return false; + } + + CVertexProgram *vp; + CPixelProgram *pp; + }; + + class CShaderDesc + { + public: + + enum TShaderType + { + Normal, + Bump_unused, + UserColor, + LightMap, + Specular, + Caustics_unused, + PerPixelLighting, + PerPixelNoSpecular, + Cloud, + Water + }; + + enum TFogMode + { + Exponential, + Exponential2, + Linear + }; + + enum TLightMode + { + Nolight, + Directional, + Point, + Spot + }; + + CShaderDesc(){ + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + texEnvMode[ i ] = 0; + + for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) + lightMode[ i ] = Nolight; + + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + useTextureStage[ i ] = false; + useFirstTextureCoordSet = false; + noTextures = true; + + features = None; + shaderType = Normal; + vbFlags = 0; + nlightmaps = 0; + alphaTestTreshold = 0.5f; + fogMode = Linear; + pointLight = false; + } + + ~CShaderDesc(){ + } + + bool operator==( const CShaderDesc &o ) const + { + if( noTextures != o.noTextures ) + return false; + + if( shaderType != o.shaderType ) + return false; + + if( vbFlags != o.vbFlags ) + return false; + + if( nlightmaps != o.nlightmaps ) + return false; + + if( features != o.features ) + return false; + + if( ( features & AlphaTest ) != 0 ) + { + if( alphaTestTreshold > o.alphaTestTreshold + 0.0001f ) + return false; + + if( alphaTestTreshold < o.alphaTestTreshold - 0.0001f ) + return false; + } + + if( fogEnabled() ) + { + if( fogMode != o.fogMode ) + return false; + } + + if( shaderType == Normal ) + { + if( useFirstTextureCoordSet != o.useFirstTextureCoordSet ) + return false; + + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + if( texEnvMode[ i ] != o.texEnvMode[ i ] ) + return false; + + for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) + if( useTextureStage[ i ] != o.useTextureStage[ i ] ) + return false; + } + + if( lightingEnabled() ) + { + for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) + if( lightMode[ i ] != o.lightMode[ i ] ) + return false; + } + + return true; + } + + void setTexEnvMode( uint32 index, uint32 mode ){ texEnvMode[ index ] = mode; } + + void setUseFirstTexCoords( bool b ){ useFirstTextureCoordSet = b; } + bool getUseFirstTexCoords() const{ return useFirstTextureCoordSet; } + + void setUseTexStage( uint8 stage, bool b ){ useTextureStage[ stage ] = b; } + bool getUseTexStage( uint8 stage ) const{ return useTextureStage[ stage ]; } + + void setNoTextures( bool b ){ noTextures = b; } + bool useTextures() const{ return !noTextures; } + + void setVBFlags( uint32 flags ){ vbFlags = flags; } + bool hasVBFlags( uint32 flags ) const{ + if( ( vbFlags & flags ) != 0 ) + return true; + else + return false; + } + + void setShaderType( uint32 type ){ shaderType = type; } + void setNLightMaps( uint32 n ){ nlightmaps = n; } + + void setAlphaTest( bool b ) + { + if( b ) + features |= AlphaTest; + else + features &= ~AlphaTest; + } + + void setAlphaTestThreshold( float t ){ alphaTestTreshold = t; } + + void setFog( bool b ) + { + if( b ) + features |= Fog; + else + features &= ~Fog; + } + + bool fogEnabled() const + { + if( ( features & Fog ) != 0 ) + return true; + else + return false; + } + + void setFogMode( TFogMode mode ){ fogMode = mode; } + + uint32 getFogMode() const{ return fogMode; } + + void setLighting( bool b ) + { + if( b ) + features |= Lighting; + else + features &= ~Lighting; + } + + bool lightingEnabled() const + { + if( ( features & Lighting ) != 0 ) + return true; + else + return false; + } + + void setLight( int idx, TLightMode mode ) + { + lightMode[ idx ] = mode; + if( mode == Point ) + pointLight = true; + } + + TLightMode getLight( int idx ) const{ return lightMode[ idx ]; } + + bool hasPointLight() const{ return pointLight; } + + void setShaders( SShaderPair sp ){ shaderPair = sp; } + SShaderPair getShaders() const{ return shaderPair; } + + private: + + enum TShaderFeatures + { + None = 0, + AlphaTest = 1, + Fog = 2, + Lighting = 4 + }; + + uint32 features; + uint32 texEnvMode[ SHADER_MAX_TEXTURES ]; + bool useTextureStage[ SHADER_MAX_TEXTURES ]; + bool useFirstTextureCoordSet; + bool noTextures; + uint32 vbFlags; + uint32 shaderType; + uint32 nlightmaps; + float alphaTestTreshold; + uint32 fogMode; + TLightMode lightMode[ SHADER_MAX_LIGHTS ]; + bool pointLight; + + SShaderPair shaderPair; + }; +} + +#endif + diff --git a/code/nel/src/3d/dyn_mat_loader.cpp b/code/nel/src/3d/dyn_mat_loader.cpp index c91d3bf2e..291812f29 100644 --- a/code/nel/src/3d/dyn_mat_loader.cpp +++ b/code/nel/src/3d/dyn_mat_loader.cpp @@ -1,61 +1,61 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/3d/dyn_mat_loader.h" -#include "nel/3d/dynamic_material.h" -#include "nel/misc/file.h" -#include "nel/misc/i_xml.h" - -namespace NL3D -{ - CDynMatLoader::CDynMatLoader() - { - mat = NULL; - } - - CDynMatLoader::~CDynMatLoader() - { - mat = NULL; - } - - bool CDynMatLoader::loadFrom( const std::string &fileName ) - { - NLMISC::CIFile ifile; - if( !ifile.open( fileName, true ) ) - { - nlinfo( "Error opening file %s", fileName.c_str() ); - return false; - } - - NLMISC::CIXml xml; - if( !xml.init( ifile ) ) - { - ifile.close(); - nlinfo( "Error initializing XML stream for file %s", fileName.c_str() ); - return false; - } - - mat = new CDynMaterial(); - mat->serial( xml ); - - ifile.close(); - - return true; - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/3d/dyn_mat_loader.h" +#include "nel/3d/dynamic_material.h" +#include "nel/misc/file.h" +#include "nel/misc/i_xml.h" + +namespace NL3D +{ + CDynMatLoader::CDynMatLoader() + { + mat = NULL; + } + + CDynMatLoader::~CDynMatLoader() + { + mat = NULL; + } + + bool CDynMatLoader::loadFrom( const std::string &fileName ) + { + NLMISC::CIFile ifile; + if( !ifile.open( fileName, true ) ) + { + nlinfo( "Error opening file %s", fileName.c_str() ); + return false; + } + + NLMISC::CIXml xml; + if( !xml.init( ifile ) ) + { + ifile.close(); + nlinfo( "Error initializing XML stream for file %s", fileName.c_str() ); + return false; + } + + mat = new CDynMaterial(); + mat->serial( xml ); + + ifile.close(); + + return true; + } +} + + diff --git a/code/nel/src/3d/dynamic_material.cpp b/code/nel/src/3d/dynamic_material.cpp index 8a0ac150a..440428836 100644 --- a/code/nel/src/3d/dynamic_material.cpp +++ b/code/nel/src/3d/dynamic_material.cpp @@ -1,383 +1,383 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "nel/3d/dynamic_material.h" - -namespace NL3D -{ - void SDynMaterialProp::serial( NLMISC::IStream &f ) - { - f.xmlPush( "property" ); - - f.xmlPush( "id" ); - f.serial( prop ); - f.xmlPop(); - - f.xmlPush( "label" ); - f.serial( label ); - f.xmlPop(); - - f.xmlPush( "type" ); - f.serial( type ); - f.xmlPop(); - - f.xmlPush( "value" ); - f.serial( value ); - f.xmlPop(); - - f.xmlPop(); - } - - void SRenderPass::serial( NLMISC::IStream &f ) - { - f.xmlPush( "pass" ); - - f.xmlPush( "name" ); - f.serial( name ); - f.xmlPop(); - - f.xmlPush( "shader" ); - f.serial( shaderRef ); - f.xmlPop(); - - f.xmlPush( "properties" ); - - if( !f.isReading() ) - { - uint32 n = properties.size(); - f.xmlPush( "count" ); - f.serial( n ); - f.xmlPop(); - - std::vector< SDynMaterialProp >::iterator itr = properties.begin(); - while( itr != properties.end() ) - { - itr->serial( f ); - ++itr; - } - } - else - { - uint32 n; - f.xmlPush( "count" ); - f.serial( n ); - f.xmlPop(); - - for( uint32 i = 0; i < n; i++ ) - { - SDynMaterialProp prop; - prop.serial( f ); - properties.push_back( prop ); - } - } - - f.xmlPop(); - - f.xmlPop(); - } - - void SRenderPass::addProperty( const SDynMaterialProp &prop ) - { - std::vector< SDynMaterialProp >::const_iterator itr = properties.begin(); - while( itr != properties.end() ) - { - if( itr->prop == prop.prop ) - break; - ++itr; - } - if( itr != properties.end() ) - return; - - properties.push_back( prop ); - } - - void SRenderPass::removeProperty( const std::string &name ) - { - std::vector< SDynMaterialProp >::iterator itr = properties.begin(); - while( itr != properties.end() ) - { - if( itr->prop == name ) - break; - ++itr; - } - if( itr == properties.end() ) - return; - - properties.erase( itr ); - } - - bool SRenderPass::changeProperty( const std::string &name, const SDynMaterialProp &prop ) - { - std::vector< SDynMaterialProp >::iterator itr = properties.begin(); - while( itr != properties.end() ) - { - if( itr->prop == name ) - break; - ++itr; - } - if( itr == properties.end() ) - return false; - - itr->prop = prop.prop; - itr->label = prop.label; - itr->type = prop.type; - itr->value = prop.value; - - return true; - } - - - const SDynMaterialProp* SRenderPass::getProperty( uint32 i ) const - { - if( i >= properties.size() ) - return NULL; - - return &( properties[ i ] ); - } - - CDynMaterial::CDynMaterial() - { - reconstruct(); - } - - CDynMaterial::~CDynMaterial() - { - clear(); - } - - CDynMaterial& CDynMaterial::operator=( const CDynMaterial &other ) - { - if( &other != this ) - { - clear(); - - std::vector< SRenderPass* >::const_iterator itr = other.passes.begin(); - while( itr != other.passes.end() ) - { - SRenderPass *pass = new SRenderPass(); - *pass = *(*itr); - passes.push_back( pass ); - ++itr; - } - } - - return *this; - } - - void CDynMaterial::reconstruct() - { - clear(); - SRenderPass *p = new SRenderPass(); - p->setName( "pass1" ); - passes.push_back( p ); - } - - void CDynMaterial::clear() - { - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - delete *itr; - ++itr; - } - passes.clear(); - } - - void CDynMaterial::serial( NLMISC::IStream &f ) - { - f.xmlPush( "Material" ); - - int version = f.serialVersion( 1 ); - - f.xmlPush( "passes" ); - - if( !f.isReading() ) - { - uint32 n = passes.size(); - f.xmlPush( "count" ); - f.serial( n ); - f.xmlPop(); - - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - (*itr)->serial( f ); - ++itr; - } - } - else - { - clear(); - uint32 n; - f.xmlPush( "count" ); - f.serial( n ); - f.xmlPop(); - - for( uint32 i = 0; i < n; i++ ) - { - SRenderPass *pass = new SRenderPass(); - pass->serial( f ); - passes.push_back( pass ); - } - } - - f.xmlPop(); - - f.xmlPop(); - } - - void CDynMaterial::addPass( const SRenderPass &pass ) - { - std::string n; - std::string name; - pass.getName( name ); - - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - (*itr)->getName( n ); - if( n == name ) - break; - ++itr; - } - if( itr != passes.end() ) - return; - - SRenderPass *p = new SRenderPass(); - *p = pass; - passes.push_back( p ); - } - - void CDynMaterial::removePass( const std::string &name ) - { - std::string n; - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - (*itr)->getName( n ); - if( n == name ) - break; - ++itr; - } - - if( itr != passes.end() ) - { - delete *itr; - passes.erase( itr ); - } - } - - void CDynMaterial::renamePass( const std::string &from, const std::string &to ) - { - std::string n; - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - (*itr)->getName( n ); - if( n == from ) - break; - ++itr; - } - - if( itr != passes.end() ) - (*itr)->setName( to ); - } - - void CDynMaterial::movePassUp( const std::string &name ) - { - std::string n; - uint32 i = 0; - for( i = 0; i < passes.size(); i++ ) - { - passes[ i ]->getName( n ); - if( n == name ) - break; - } - - if( i >= passes.size() ) - return; - - if( i == 0 ) - return; - - SRenderPass *temp = passes[ i ]; - passes[ i ] = passes[ i - 1 ]; - passes[ i - 1 ] = temp; - } - - void CDynMaterial::movePassDown( const std::string &name ) - { - std::string n; - uint32 i = 0; - for( i = 0; i < passes.size(); i++ ) - { - passes[ i ]->getName( n ); - if( n == name ) - break; - } - - if( i >= passes.size() ) - return; - - if( i == ( passes.size() - 1 ) ) - return; - - SRenderPass *temp = passes[ i ]; - passes[ i ] = passes[ i + 1 ]; - passes[ i + 1 ] = temp; - } - - SRenderPass* CDynMaterial::getPass( const std::string &name ) - { - std::string n; - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - (*itr)->getName( n ); - if( n == name ) - break; - ++itr; - } - if( itr == passes.end() ) - return NULL; - else - return *itr; - } - - SRenderPass* CDynMaterial::getPass( uint32 i ) - { - if( i >= passes.size() ) - return NULL; - else - return passes[ i ]; - } - - void CDynMaterial::getPassList( std::vector< std::string > &l ) - { - std::vector< SRenderPass* >::iterator itr = passes.begin(); - while( itr != passes.end() ) - { - std::string name; - - (*itr)->getName( name ); - l.push_back( name ); - - ++itr; - } - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "nel/3d/dynamic_material.h" + +namespace NL3D +{ + void SDynMaterialProp::serial( NLMISC::IStream &f ) + { + f.xmlPush( "property" ); + + f.xmlPush( "id" ); + f.serial( prop ); + f.xmlPop(); + + f.xmlPush( "label" ); + f.serial( label ); + f.xmlPop(); + + f.xmlPush( "type" ); + f.serial( type ); + f.xmlPop(); + + f.xmlPush( "value" ); + f.serial( value ); + f.xmlPop(); + + f.xmlPop(); + } + + void SRenderPass::serial( NLMISC::IStream &f ) + { + f.xmlPush( "pass" ); + + f.xmlPush( "name" ); + f.serial( name ); + f.xmlPop(); + + f.xmlPush( "shader" ); + f.serial( shaderRef ); + f.xmlPop(); + + f.xmlPush( "properties" ); + + if( !f.isReading() ) + { + uint32 n = properties.size(); + f.xmlPush( "count" ); + f.serial( n ); + f.xmlPop(); + + std::vector< SDynMaterialProp >::iterator itr = properties.begin(); + while( itr != properties.end() ) + { + itr->serial( f ); + ++itr; + } + } + else + { + uint32 n; + f.xmlPush( "count" ); + f.serial( n ); + f.xmlPop(); + + for( uint32 i = 0; i < n; i++ ) + { + SDynMaterialProp prop; + prop.serial( f ); + properties.push_back( prop ); + } + } + + f.xmlPop(); + + f.xmlPop(); + } + + void SRenderPass::addProperty( const SDynMaterialProp &prop ) + { + std::vector< SDynMaterialProp >::const_iterator itr = properties.begin(); + while( itr != properties.end() ) + { + if( itr->prop == prop.prop ) + break; + ++itr; + } + if( itr != properties.end() ) + return; + + properties.push_back( prop ); + } + + void SRenderPass::removeProperty( const std::string &name ) + { + std::vector< SDynMaterialProp >::iterator itr = properties.begin(); + while( itr != properties.end() ) + { + if( itr->prop == name ) + break; + ++itr; + } + if( itr == properties.end() ) + return; + + properties.erase( itr ); + } + + bool SRenderPass::changeProperty( const std::string &name, const SDynMaterialProp &prop ) + { + std::vector< SDynMaterialProp >::iterator itr = properties.begin(); + while( itr != properties.end() ) + { + if( itr->prop == name ) + break; + ++itr; + } + if( itr == properties.end() ) + return false; + + itr->prop = prop.prop; + itr->label = prop.label; + itr->type = prop.type; + itr->value = prop.value; + + return true; + } + + + const SDynMaterialProp* SRenderPass::getProperty( uint32 i ) const + { + if( i >= properties.size() ) + return NULL; + + return &( properties[ i ] ); + } + + CDynMaterial::CDynMaterial() + { + reconstruct(); + } + + CDynMaterial::~CDynMaterial() + { + clear(); + } + + CDynMaterial& CDynMaterial::operator=( const CDynMaterial &other ) + { + if( &other != this ) + { + clear(); + + std::vector< SRenderPass* >::const_iterator itr = other.passes.begin(); + while( itr != other.passes.end() ) + { + SRenderPass *pass = new SRenderPass(); + *pass = *(*itr); + passes.push_back( pass ); + ++itr; + } + } + + return *this; + } + + void CDynMaterial::reconstruct() + { + clear(); + SRenderPass *p = new SRenderPass(); + p->setName( "pass1" ); + passes.push_back( p ); + } + + void CDynMaterial::clear() + { + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + delete *itr; + ++itr; + } + passes.clear(); + } + + void CDynMaterial::serial( NLMISC::IStream &f ) + { + f.xmlPush( "Material" ); + + int version = f.serialVersion( 1 ); + + f.xmlPush( "passes" ); + + if( !f.isReading() ) + { + uint32 n = passes.size(); + f.xmlPush( "count" ); + f.serial( n ); + f.xmlPop(); + + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + (*itr)->serial( f ); + ++itr; + } + } + else + { + clear(); + uint32 n; + f.xmlPush( "count" ); + f.serial( n ); + f.xmlPop(); + + for( uint32 i = 0; i < n; i++ ) + { + SRenderPass *pass = new SRenderPass(); + pass->serial( f ); + passes.push_back( pass ); + } + } + + f.xmlPop(); + + f.xmlPop(); + } + + void CDynMaterial::addPass( const SRenderPass &pass ) + { + std::string n; + std::string name; + pass.getName( name ); + + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + (*itr)->getName( n ); + if( n == name ) + break; + ++itr; + } + if( itr != passes.end() ) + return; + + SRenderPass *p = new SRenderPass(); + *p = pass; + passes.push_back( p ); + } + + void CDynMaterial::removePass( const std::string &name ) + { + std::string n; + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + (*itr)->getName( n ); + if( n == name ) + break; + ++itr; + } + + if( itr != passes.end() ) + { + delete *itr; + passes.erase( itr ); + } + } + + void CDynMaterial::renamePass( const std::string &from, const std::string &to ) + { + std::string n; + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + (*itr)->getName( n ); + if( n == from ) + break; + ++itr; + } + + if( itr != passes.end() ) + (*itr)->setName( to ); + } + + void CDynMaterial::movePassUp( const std::string &name ) + { + std::string n; + uint32 i = 0; + for( i = 0; i < passes.size(); i++ ) + { + passes[ i ]->getName( n ); + if( n == name ) + break; + } + + if( i >= passes.size() ) + return; + + if( i == 0 ) + return; + + SRenderPass *temp = passes[ i ]; + passes[ i ] = passes[ i - 1 ]; + passes[ i - 1 ] = temp; + } + + void CDynMaterial::movePassDown( const std::string &name ) + { + std::string n; + uint32 i = 0; + for( i = 0; i < passes.size(); i++ ) + { + passes[ i ]->getName( n ); + if( n == name ) + break; + } + + if( i >= passes.size() ) + return; + + if( i == ( passes.size() - 1 ) ) + return; + + SRenderPass *temp = passes[ i ]; + passes[ i ] = passes[ i + 1 ]; + passes[ i + 1 ] = temp; + } + + SRenderPass* CDynMaterial::getPass( const std::string &name ) + { + std::string n; + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + (*itr)->getName( n ); + if( n == name ) + break; + ++itr; + } + if( itr == passes.end() ) + return NULL; + else + return *itr; + } + + SRenderPass* CDynMaterial::getPass( uint32 i ) + { + if( i >= passes.size() ) + return NULL; + else + return passes[ i ]; + } + + void CDynMaterial::getPassList( std::vector< std::string > &l ) + { + std::vector< SRenderPass* >::iterator itr = passes.begin(); + while( itr != passes.end() ) + { + std::string name; + + (*itr)->getName( name ); + l.push_back( name ); + + ++itr; + } + } +} + + diff --git a/code/nel/src/3d/shape_material_serializer.cpp b/code/nel/src/3d/shape_material_serializer.cpp index 251f66b80..691cdf39b 100644 --- a/code/nel/src/3d/shape_material_serializer.cpp +++ b/code/nel/src/3d/shape_material_serializer.cpp @@ -1,102 +1,102 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/3d/shape_material_serializer.h" -#include "nel/3d/shape.h" -#include "nel/3d/material.h" -#include "nel/3d/dynamic_material.h" -#include "nel/3d/mesh_base.h" -#include "nel/misc/file.h" -#include "nel/misc/o_xml.h" - -namespace NL3D -{ - ShapeMatSerial::ShapeMatSerial() - { - shape = NULL; - } - - ShapeMatSerial::~ShapeMatSerial() - { - shape = NULL; - } - - void ShapeMatSerial::serial( const char *sPath ) - { - if( shape == NULL ) - return; - - nlinfo( "Exporting materials of %s", sPath ); - - std::string path = sPath; - std::string fname; - std::string::size_type idx; - - idx = path.find_last_of( '.' ); - path = path.substr( 0, idx ); - - CMeshBase *mb = dynamic_cast< CMeshBase* >( shape ); - if( mb != NULL ) - { - uint n = mb->getNbMaterial(); - nlinfo( "exporting %u materials", n ); - - for( int i = 0; i < n; i++ ) - { - CMaterial &m = mb->getMaterial( i ); - CDynMaterial *dm = m.getDynMat(); - if( dm == NULL ) - { - m.createDynMat(); - dm = m.getDynMat(); - } - - fname = path + "_"; - fname += char( '0' + i ); - fname += ".nelmat"; - - nlinfo( "exporting to %s", fname.c_str() ); - - NLMISC::COFile o; - if( o.open( fname ) ) - { - NLMISC::COXml xml; - if( xml.init( &o ) ) - { - dm->serial( xml ); - xml.flush(); - } - else - { - nlerror( "Error initializing XML output stream for %s", fname.c_str() ); - } - o.close(); - } - else - { - nlerror( "Error creating file %s", fname.c_str() ); - } - - } - } - - } -} - - - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/3d/shape_material_serializer.h" +#include "nel/3d/shape.h" +#include "nel/3d/material.h" +#include "nel/3d/dynamic_material.h" +#include "nel/3d/mesh_base.h" +#include "nel/misc/file.h" +#include "nel/misc/o_xml.h" + +namespace NL3D +{ + ShapeMatSerial::ShapeMatSerial() + { + shape = NULL; + } + + ShapeMatSerial::~ShapeMatSerial() + { + shape = NULL; + } + + void ShapeMatSerial::serial( const char *sPath ) + { + if( shape == NULL ) + return; + + nlinfo( "Exporting materials of %s", sPath ); + + std::string path = sPath; + std::string fname; + std::string::size_type idx; + + idx = path.find_last_of( '.' ); + path = path.substr( 0, idx ); + + CMeshBase *mb = dynamic_cast< CMeshBase* >( shape ); + if( mb != NULL ) + { + uint n = mb->getNbMaterial(); + nlinfo( "exporting %u materials", n ); + + for( int i = 0; i < n; i++ ) + { + CMaterial &m = mb->getMaterial( i ); + CDynMaterial *dm = m.getDynMat(); + if( dm == NULL ) + { + m.createDynMat(); + dm = m.getDynMat(); + } + + fname = path + "_"; + fname += char( '0' + i ); + fname += ".nelmat"; + + nlinfo( "exporting to %s", fname.c_str() ); + + NLMISC::COFile o; + if( o.open( fname ) ) + { + NLMISC::COXml xml; + if( xml.init( &o ) ) + { + dm->serial( xml ); + xml.flush(); + } + else + { + nlerror( "Error initializing XML output stream for %s", fname.c_str() ); + } + o.close(); + } + else + { + nlerror( "Error creating file %s", fname.c_str() ); + } + + } + } + + } +} + + + + diff --git a/code/nel/src/3d/usr_shader_loader.cpp b/code/nel/src/3d/usr_shader_loader.cpp index 7cd492933..bf28dc1d3 100644 --- a/code/nel/src/3d/usr_shader_loader.cpp +++ b/code/nel/src/3d/usr_shader_loader.cpp @@ -1,74 +1,74 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/3d/usr_shader_loader.h" -#include "nel/3d/usr_shader_manager.h" -#include "nel/3d/usr_shader_program.h" -#include "nel/misc/file.h" -#include "nel/misc/path.h" -#include "nel/misc/i_xml.h" - -namespace NL3D -{ - CUsrShaderLoader::CUsrShaderLoader() - { - manager = NULL; - } - - CUsrShaderLoader::~CUsrShaderLoader() - { - } - - void CUsrShaderLoader::loadShaders( const std::string &directory ) - { - if( manager == NULL ) - return; - - std::vector< std::string > files; - - NLMISC::CPath::getPathContent( directory, true, false, true, files ); - - std::vector< std::string >::iterator itr = files.begin(); - while( itr != files.end() ) - { - if( NLMISC::CFile::getExtension( *itr ) == "nlshdr" ) - { - loadShader( *itr ); - } - - ++itr; - } - } - - void CUsrShaderLoader::loadShader( const std::string &file ) - { - NLMISC::CIFile f; - if( !f.open( file, true ) ) - return; - - NLMISC::CIXml xml; - if( !xml.init( f ) ) - return; - - CUsrShaderProgram *p = new CUsrShaderProgram(); - p->serial( xml ); - manager->addShader( p ); - - f.close(); - } -} - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/3d/usr_shader_loader.h" +#include "nel/3d/usr_shader_manager.h" +#include "nel/3d/usr_shader_program.h" +#include "nel/misc/file.h" +#include "nel/misc/path.h" +#include "nel/misc/i_xml.h" + +namespace NL3D +{ + CUsrShaderLoader::CUsrShaderLoader() + { + manager = NULL; + } + + CUsrShaderLoader::~CUsrShaderLoader() + { + } + + void CUsrShaderLoader::loadShaders( const std::string &directory ) + { + if( manager == NULL ) + return; + + std::vector< std::string > files; + + NLMISC::CPath::getPathContent( directory, true, false, true, files ); + + std::vector< std::string >::iterator itr = files.begin(); + while( itr != files.end() ) + { + if( NLMISC::CFile::getExtension( *itr ) == "nlshdr" ) + { + loadShader( *itr ); + } + + ++itr; + } + } + + void CUsrShaderLoader::loadShader( const std::string &file ) + { + NLMISC::CIFile f; + if( !f.open( file, true ) ) + return; + + NLMISC::CIXml xml; + if( !xml.init( f ) ) + return; + + CUsrShaderProgram *p = new CUsrShaderProgram(); + p->serial( xml ); + manager->addShader( p ); + + f.close(); + } +} + diff --git a/code/nel/src/3d/usr_shader_manager.cpp b/code/nel/src/3d/usr_shader_manager.cpp index f58e4b907..08b3eefc8 100644 --- a/code/nel/src/3d/usr_shader_manager.cpp +++ b/code/nel/src/3d/usr_shader_manager.cpp @@ -1,157 +1,157 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/3d/usr_shader_manager.h" -#include "nel/3d/usr_shader_program.h" -#include "nel/3d/usr_shader_visitor.h" - -namespace NL3D -{ - CUsrShaderManager::CUsrShaderManager() - { - } - - CUsrShaderManager::~CUsrShaderManager() - { - clear(); - } - - - void CUsrShaderManager::clear() - { - std::map< std::string, CUsrShaderProgram* >::iterator itr = programs.begin(); - while( itr != programs.end() ) - { - delete itr->second; - ++itr; - } - programs.clear(); - } - - void CUsrShaderManager::getShaderList( std::vector< std::string > &v ) - { - v.clear(); - - std::string n; - std::map< std::string, CUsrShaderProgram* >::iterator itr = programs.begin(); - while( itr != programs.end() ) - { - itr->second->getName( n ); - v.push_back( n ); - ++itr; - } - } - - bool CUsrShaderManager::addShader( CUsrShaderProgram *program ) - { - std::string n; - program->getName( n ); - - std::map< std::string, CUsrShaderProgram* >::iterator itr - = programs.find( n ); - if( itr != programs.end() ) - return false; - - programs[ n ] = program; - return true; - } - - bool CUsrShaderManager::removeShader( const std::string &name ) - { - std::map< std::string, CUsrShaderProgram* >::iterator itr - = programs.find( name ); - if( itr == programs.end() ) - return false; - - delete itr->second; - itr->second = NULL; - programs.erase( itr ); - - return true; - } - - bool CUsrShaderManager::changeShader( const std::string &name, CUsrShaderProgram *program ) - { - std::map< std::string, CUsrShaderProgram* >::iterator itr - = programs.find( name ); - if( itr == programs.end() ) - return false; - - CUsrShaderProgram *p = itr->second; - std::string s; - - program->getName( s ); - p->setName( s ); - - program->getDescription( s ); - p->setDescription( s ); - - program->getVP( s ); - p->setVP( s ); - - program->getFP( s ); - p->setFP( s ); - - return true; - } - - bool CUsrShaderManager::getShader( const std::string &name, CUsrShaderProgram *program ) - { - std::map< std::string, CUsrShaderProgram* >::iterator itr - = programs.find( name ); - if( itr == programs.end() ) - return false; - - CUsrShaderProgram *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 CUsrShaderManager::visitShaders( IUsrShaderVisitor *visitor ) - { - std::map< std::string, CUsrShaderProgram* >::iterator itr = programs.begin(); - while( itr != programs.end() ) - { - visitor->visit( itr->second ); - ++itr; - } - } - - void CUsrShaderManager::visitShader( const std::string &name, IUsrShaderVisitor *visitor ) - { - std::map< std::string, CUsrShaderProgram* >::iterator itr = - programs.find( name ); - if( itr == programs.end() ) - return; - - visitor->visit( itr->second ); - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/3d/usr_shader_manager.h" +#include "nel/3d/usr_shader_program.h" +#include "nel/3d/usr_shader_visitor.h" + +namespace NL3D +{ + CUsrShaderManager::CUsrShaderManager() + { + } + + CUsrShaderManager::~CUsrShaderManager() + { + clear(); + } + + + void CUsrShaderManager::clear() + { + std::map< std::string, CUsrShaderProgram* >::iterator itr = programs.begin(); + while( itr != programs.end() ) + { + delete itr->second; + ++itr; + } + programs.clear(); + } + + void CUsrShaderManager::getShaderList( std::vector< std::string > &v ) + { + v.clear(); + + std::string n; + std::map< std::string, CUsrShaderProgram* >::iterator itr = programs.begin(); + while( itr != programs.end() ) + { + itr->second->getName( n ); + v.push_back( n ); + ++itr; + } + } + + bool CUsrShaderManager::addShader( CUsrShaderProgram *program ) + { + std::string n; + program->getName( n ); + + std::map< std::string, CUsrShaderProgram* >::iterator itr + = programs.find( n ); + if( itr != programs.end() ) + return false; + + programs[ n ] = program; + return true; + } + + bool CUsrShaderManager::removeShader( const std::string &name ) + { + std::map< std::string, CUsrShaderProgram* >::iterator itr + = programs.find( name ); + if( itr == programs.end() ) + return false; + + delete itr->second; + itr->second = NULL; + programs.erase( itr ); + + return true; + } + + bool CUsrShaderManager::changeShader( const std::string &name, CUsrShaderProgram *program ) + { + std::map< std::string, CUsrShaderProgram* >::iterator itr + = programs.find( name ); + if( itr == programs.end() ) + return false; + + CUsrShaderProgram *p = itr->second; + std::string s; + + program->getName( s ); + p->setName( s ); + + program->getDescription( s ); + p->setDescription( s ); + + program->getVP( s ); + p->setVP( s ); + + program->getFP( s ); + p->setFP( s ); + + return true; + } + + bool CUsrShaderManager::getShader( const std::string &name, CUsrShaderProgram *program ) + { + std::map< std::string, CUsrShaderProgram* >::iterator itr + = programs.find( name ); + if( itr == programs.end() ) + return false; + + CUsrShaderProgram *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 CUsrShaderManager::visitShaders( IUsrShaderVisitor *visitor ) + { + std::map< std::string, CUsrShaderProgram* >::iterator itr = programs.begin(); + while( itr != programs.end() ) + { + visitor->visit( itr->second ); + ++itr; + } + } + + void CUsrShaderManager::visitShader( const std::string &name, IUsrShaderVisitor *visitor ) + { + std::map< std::string, CUsrShaderProgram* >::iterator itr = + programs.find( name ); + if( itr == programs.end() ) + return; + + visitor->visit( itr->second ); + } +} + + diff --git a/code/nel/src/3d/usr_shader_program.cpp b/code/nel/src/3d/usr_shader_program.cpp index 76dc1734e..fc48a94ad 100644 --- a/code/nel/src/3d/usr_shader_program.cpp +++ b/code/nel/src/3d/usr_shader_program.cpp @@ -1,59 +1,59 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/3d/usr_shader_program.h" - -namespace NL3D -{ - CUsrShaderProgram::CUsrShaderProgram() - { - vpId = 0; - fpId = 0; - pId = 0; - } - - CUsrShaderProgram::~CUsrShaderProgram() - { - } - - void CUsrShaderProgram::serial( NLMISC::IStream &f ) - { - f.xmlPush( "ShaderProgram" ); - - int version = f.serialVersion( 1 ); - - f.xmlPush( "Name" ); - f.serial( name ); - f.xmlPop(); - - f.xmlPush( "Description" ); - f.serial( description ); - f.xmlPop(); - - f.xmlPush( "VertexProgram" ); - f.serial( vertexProgram ); - f.xmlPop(); - - f.xmlPush( "FragmentProgram" ); - f.serial( fragmentProgram ); - f.xmlPop(); - - f.xmlPop(); - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/3d/usr_shader_program.h" + +namespace NL3D +{ + CUsrShaderProgram::CUsrShaderProgram() + { + vpId = 0; + fpId = 0; + pId = 0; + } + + CUsrShaderProgram::~CUsrShaderProgram() + { + } + + void CUsrShaderProgram::serial( NLMISC::IStream &f ) + { + f.xmlPush( "ShaderProgram" ); + + int version = f.serialVersion( 1 ); + + f.xmlPush( "Name" ); + f.serial( name ); + f.xmlPop(); + + f.xmlPush( "Description" ); + f.serial( description ); + f.xmlPop(); + + f.xmlPush( "VertexProgram" ); + f.serial( vertexProgram ); + f.xmlPop(); + + f.xmlPush( "FragmentProgram" ); + f.serial( fragmentProgram ); + f.xmlPop(); + + f.xmlPop(); + } +} + + diff --git a/code/nel/src/3d/usr_shader_saver.cpp b/code/nel/src/3d/usr_shader_saver.cpp index 95fafd132..d28a76ca7 100644 --- a/code/nel/src/3d/usr_shader_saver.cpp +++ b/code/nel/src/3d/usr_shader_saver.cpp @@ -1,71 +1,71 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/3d/usr_shader_saver.h" -#include "nel/3d/usr_shader_manager.h" -#include "nel/3d/usr_shader_program.h" -#include "nel/misc/file.h" -#include "nel/misc/o_xml.h" - -namespace NL3D -{ - CUsrShaderSaver::CUsrShaderSaver() - { - manager = NULL; - } - - CUsrShaderSaver::~CUsrShaderSaver() - { - } - - void CUsrShaderSaver::visit( CUsrShaderProgram *program ) - { - std::string fn; - program->getName( fn ); - fn += ".nlshdr"; - - fn = outputDir + "/" + fn; - - NLMISC::COFile of; - if( !of.open( fn, false, true ) ) - return; - - NLMISC::COXml xml; - if( !xml.init( &of ) ) - return; - - program->serial( xml ); - - xml.flush(); - of.close(); - - } - - void CUsrShaderSaver::saveShaders( const std::string &directory ) - { - outputDir = directory; - manager->visitShaders( this ); - } - - void CUsrShaderSaver::saveShader( const std::string &directory, const std::string &name ) - { - outputDir = directory; - manager->visitShader( name, this ); - } -} - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/3d/usr_shader_saver.h" +#include "nel/3d/usr_shader_manager.h" +#include "nel/3d/usr_shader_program.h" +#include "nel/misc/file.h" +#include "nel/misc/o_xml.h" + +namespace NL3D +{ + CUsrShaderSaver::CUsrShaderSaver() + { + manager = NULL; + } + + CUsrShaderSaver::~CUsrShaderSaver() + { + } + + void CUsrShaderSaver::visit( CUsrShaderProgram *program ) + { + std::string fn; + program->getName( fn ); + fn += ".nlshdr"; + + fn = outputDir + "/" + fn; + + NLMISC::COFile of; + if( !of.open( fn, false, true ) ) + return; + + NLMISC::COXml xml; + if( !xml.init( &of ) ) + return; + + program->serial( xml ); + + xml.flush(); + of.close(); + + } + + void CUsrShaderSaver::saveShaders( const std::string &directory ) + { + outputDir = directory; + manager->visitShaders( this ); + } + + void CUsrShaderSaver::saveShader( const std::string &directory, const std::string &name ) + { + outputDir = directory; + manager->visitShader( name, this ); + } +} + + diff --git a/code/nel/src/misc/variant.cpp b/code/nel/src/misc/variant.cpp index 7ab3a49d4..9a3e63e6d 100644 --- a/code/nel/src/misc/variant.cpp +++ b/code/nel/src/misc/variant.cpp @@ -1,320 +1,320 @@ -// NeL - MMORPG Framework -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "nel/misc/variant.h" -#include - - -namespace NLMISC -{ - CVariant::CVariant() - { - std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0f ); - type = String; - } - - CVariant::~CVariant() - { - } - - void CVariant::serial( IStream &f ) - { - uint v = f.serialVersion( 1 ); - - f.xmlPush( "type" ); - - if( !f.isReading() ) - { - uint t = type; - f.serial( t ); - } - else - { - uint t; - f.serial( t ); - type = EVarType( t ); - } - - f.xmlPop(); - - f.xmlPush( "value" ); - - switch( type ) - { - case Double: - { - f.serial( uvalue.dval ); - break; - } - - case Float: - { - f.serial( uvalue.fval ); - break; - } - - case Int: - { - f.serial( uvalue.ival ); - break; - } - - case UInt: - { - f.serial( uvalue.uval ); - break; - } - - case String: - { - f.serial( sval ); - break; - } - - } - - - if( !f.isReading() ) - { - switch( type ) - { - case Vector4: - { - float fval; - for( int i = 0; i < 4; i++ ) - { - fval = uvalue.vval[ i ]; - f.serial( fval ); - } - break; - } - - case Matrix4: - { - float fval; - for( int i = 0; i < 16; i++ ) - { - fval = uvalue.vval[ i ]; - f.serial( fval ); - } - break; - } - } - - } - else - { - switch( type ) - { - - case Vector4: - { - float fval; - for( int i = 0; i < 4; i++ ) - { - f.serial( fval ); - uvalue.vval[ i ] = fval; - } - break; - } - - case Matrix4: - { - float fval; - for( int i = 0; i < 16; i++ ) - { - f.serial( fval ); - uvalue.vval[ i ] = fval; - } - break; - } - } - } - - f.xmlPop(); - } - - void CVariant::setVector4( const float *v ) - { - for( int i = 0; i < 4; i++ ) - uvalue.vval[ i ] = v[ i ]; - type = Vector4; - } - - void CVariant::setMatrix4( const float *m ) - { - for( int i = 0; i < 16; i++ ) - uvalue.vval[ i ] = m[ i ]; - type = Matrix4; - } - - void CVariant::getVector4( float *v ) const - { - for( int i = 0; i < 4; i++ ) - v[ i ] = uvalue.vval[ i ]; - } - - void CVariant::getMatrix4( float *m ) const - { - for( int i = 0; i < 16; i++ ) - m[ i ] = uvalue.vval[ i ]; - } - - bool CVariant::valueAsString( std::string &s ) const - { - std::stringstream ss; - - switch( type ) - { - case Double: - { - ss << uvalue.dval; - s = ss.str(); - break; - } - - case Float: - { - ss << uvalue.fval; - s = ss.str(); - break; - } - - case Int: - { - ss << uvalue.ival; - s = ss.str(); - break; - } - - case UInt: - { - ss << uvalue.uval; - s = ss.str(); - break; - } - - case String: - { - s = sval; - break; - } - - case Vector4: - { - for( int i = 0; i < 4; i++ ) - ss << uvalue.vval[ i ] << " "; - s = ss.str(); - s.resize( s.size() - 1 ); - break; - } - - case Matrix4: - { - for( int i = 0; i < 16; i++ ) - ss << uvalue.vval[ i ] << " "; - s = ss.str(); - s.resize( s.size() - 1 ); - break; - } - - default: - { - return false; - break; - } - } - - return true; - } - - - void CVariant::fromString( const std::string &s, EVarType t ) - { - type = t; - sval = ""; - std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0 ); - - if( s.empty() ) - return; - - switch( t ) - { - case Double: - { - uvalue.dval = strtod( s.c_str(), NULL ); - break; - } - - case Float: - { - uvalue.fval = strtod( s.c_str(), NULL ); - break; - } - - case Int: - { - uvalue.ival = strtod( s.c_str(), NULL ); - break; - } - - case UInt: - { - uvalue.uval = strtod( s.c_str(), NULL ); - break; - } - - case String: - { - sval = s; - break; - } - - case Vector4: - { - std::stringstream ss( s ); - - for( int i = 0; i < 4; i++ ) - { - ss >> uvalue.vval[ i ]; - if( !ss.good() ) - break; - } - - break; - } - - case Matrix4: - { - std::stringstream ss( s ); - - for( int i = 0; i < 16; i++ ) - { - ss >> uvalue.vval[ i ]; - if( !ss.good() ) - break; - } - break; - } - } - - } - -} - - - +// NeL - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "nel/misc/variant.h" +#include + + +namespace NLMISC +{ + CVariant::CVariant() + { + std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0f ); + type = String; + } + + CVariant::~CVariant() + { + } + + void CVariant::serial( IStream &f ) + { + uint v = f.serialVersion( 1 ); + + f.xmlPush( "type" ); + + if( !f.isReading() ) + { + uint t = type; + f.serial( t ); + } + else + { + uint t; + f.serial( t ); + type = EVarType( t ); + } + + f.xmlPop(); + + f.xmlPush( "value" ); + + switch( type ) + { + case Double: + { + f.serial( uvalue.dval ); + break; + } + + case Float: + { + f.serial( uvalue.fval ); + break; + } + + case Int: + { + f.serial( uvalue.ival ); + break; + } + + case UInt: + { + f.serial( uvalue.uval ); + break; + } + + case String: + { + f.serial( sval ); + break; + } + + } + + + if( !f.isReading() ) + { + switch( type ) + { + case Vector4: + { + float fval; + for( int i = 0; i < 4; i++ ) + { + fval = uvalue.vval[ i ]; + f.serial( fval ); + } + break; + } + + case Matrix4: + { + float fval; + for( int i = 0; i < 16; i++ ) + { + fval = uvalue.vval[ i ]; + f.serial( fval ); + } + break; + } + } + + } + else + { + switch( type ) + { + + case Vector4: + { + float fval; + for( int i = 0; i < 4; i++ ) + { + f.serial( fval ); + uvalue.vval[ i ] = fval; + } + break; + } + + case Matrix4: + { + float fval; + for( int i = 0; i < 16; i++ ) + { + f.serial( fval ); + uvalue.vval[ i ] = fval; + } + break; + } + } + } + + f.xmlPop(); + } + + void CVariant::setVector4( const float *v ) + { + for( int i = 0; i < 4; i++ ) + uvalue.vval[ i ] = v[ i ]; + type = Vector4; + } + + void CVariant::setMatrix4( const float *m ) + { + for( int i = 0; i < 16; i++ ) + uvalue.vval[ i ] = m[ i ]; + type = Matrix4; + } + + void CVariant::getVector4( float *v ) const + { + for( int i = 0; i < 4; i++ ) + v[ i ] = uvalue.vval[ i ]; + } + + void CVariant::getMatrix4( float *m ) const + { + for( int i = 0; i < 16; i++ ) + m[ i ] = uvalue.vval[ i ]; + } + + bool CVariant::valueAsString( std::string &s ) const + { + std::stringstream ss; + + switch( type ) + { + case Double: + { + ss << uvalue.dval; + s = ss.str(); + break; + } + + case Float: + { + ss << uvalue.fval; + s = ss.str(); + break; + } + + case Int: + { + ss << uvalue.ival; + s = ss.str(); + break; + } + + case UInt: + { + ss << uvalue.uval; + s = ss.str(); + break; + } + + case String: + { + s = sval; + break; + } + + case Vector4: + { + for( int i = 0; i < 4; i++ ) + ss << uvalue.vval[ i ] << " "; + s = ss.str(); + s.resize( s.size() - 1 ); + break; + } + + case Matrix4: + { + for( int i = 0; i < 16; i++ ) + ss << uvalue.vval[ i ] << " "; + s = ss.str(); + s.resize( s.size() - 1 ); + break; + } + + default: + { + return false; + break; + } + } + + return true; + } + + + void CVariant::fromString( const std::string &s, EVarType t ) + { + type = t; + sval = ""; + std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0 ); + + if( s.empty() ) + return; + + switch( t ) + { + case Double: + { + uvalue.dval = strtod( s.c_str(), NULL ); + break; + } + + case Float: + { + uvalue.fval = strtod( s.c_str(), NULL ); + break; + } + + case Int: + { + uvalue.ival = strtod( s.c_str(), NULL ); + break; + } + + case UInt: + { + uvalue.uval = strtod( s.c_str(), NULL ); + break; + } + + case String: + { + sval = s; + break; + } + + case Vector4: + { + std::stringstream ss( s ); + + for( int i = 0; i < 4; i++ ) + { + ss >> uvalue.vval[ i ]; + if( !ss.good() ) + break; + } + + break; + } + + case Matrix4: + { + std::stringstream ss( s ); + + for( int i = 0; i < 16; i++ ) + { + ss >> uvalue.vval[ i ]; + if( !ss.good() ) + break; + } + break; + } + } + + } + +} + + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.cpp index 29ae3381c..7824d167c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.cpp @@ -1,116 +1,116 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "fog_widget.h" -#include "nel3d_interface.h" -#include - -namespace MaterialEditor -{ - FogWidget::FogWidget( QWidget *parent ) : - QWidget( parent ) - { - iface = NULL; - setupUi( this ); - setupConnections(); - } - - FogWidget::~FogWidget() - { - iface = NULL; - } - - void FogWidget::loadValues() - { - SFogSettings s; - iface->getFogSettings( s ); - - fogCB->setChecked( s.enable ); - startSB->setValue( s.start ); - endSB->setValue( s.end ); - setColorButtonColor( s.color[ 0 ], s.color[ 1 ], s.color[ 2 ] ); - } - - void FogWidget::setupConnections() - { - connect( fogCB, SIGNAL( clicked( bool ) ), this, SLOT( onFogCBClicked() ) ); - connect( startSB, SIGNAL( valueChanged( double ) ), this, SLOT( onStartSBChanged() ) ); - connect( endSB, SIGNAL( valueChanged( double ) ), this, SLOT( onEndSBChanged() ) ); - connect( colorButton, SIGNAL( clicked( bool ) ), this, SLOT( onColorButtonClicked() ) ); - } - - void FogWidget::onFogCBClicked() - { - SFogSettings s; - iface->getFogSettings( s ); - - s.enable = fogCB->isChecked(); - - iface->setFogSettings( s ); - - if( !s.enable ) - iface->setBGColor( 255, 255, 255, 255 ); - } - - void FogWidget::onStartSBChanged() - { - SFogSettings s; - iface->getFogSettings( s ); - - s.start = startSB->value(); - - iface->setFogSettings( s ); - } - - void FogWidget::onEndSBChanged() - { - SFogSettings s; - iface->getFogSettings( s ); - - s.end = endSB->value(); - - iface->setFogSettings( s ); - } - - void FogWidget::onColorButtonClicked() - { - QColor c = QColorDialog::getColor(); - - setColorButtonColor( c.red(), c.green(), c.blue() ); - - SFogSettings s; - iface->getFogSettings( s ); - - s.color[ 0 ] = c.red(); - s.color[ 1 ] = c.green(); - s.color[ 2 ] = c.blue(); - s.color[ 3 ] = 255; - - iface->setFogSettings( s ); - } - - void FogWidget::setColorButtonColor( int r, int g, int b ) - { - QString sh; - sh = QString( "background-color: rgb(%1, %2, %3);" ).arg( r ).arg( g ).arg( b ); - colorButton->setStyleSheet( sh ); - } - -} - - - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "fog_widget.h" +#include "nel3d_interface.h" +#include + +namespace MaterialEditor +{ + FogWidget::FogWidget( QWidget *parent ) : + QWidget( parent ) + { + iface = NULL; + setupUi( this ); + setupConnections(); + } + + FogWidget::~FogWidget() + { + iface = NULL; + } + + void FogWidget::loadValues() + { + SFogSettings s; + iface->getFogSettings( s ); + + fogCB->setChecked( s.enable ); + startSB->setValue( s.start ); + endSB->setValue( s.end ); + setColorButtonColor( s.color[ 0 ], s.color[ 1 ], s.color[ 2 ] ); + } + + void FogWidget::setupConnections() + { + connect( fogCB, SIGNAL( clicked( bool ) ), this, SLOT( onFogCBClicked() ) ); + connect( startSB, SIGNAL( valueChanged( double ) ), this, SLOT( onStartSBChanged() ) ); + connect( endSB, SIGNAL( valueChanged( double ) ), this, SLOT( onEndSBChanged() ) ); + connect( colorButton, SIGNAL( clicked( bool ) ), this, SLOT( onColorButtonClicked() ) ); + } + + void FogWidget::onFogCBClicked() + { + SFogSettings s; + iface->getFogSettings( s ); + + s.enable = fogCB->isChecked(); + + iface->setFogSettings( s ); + + if( !s.enable ) + iface->setBGColor( 255, 255, 255, 255 ); + } + + void FogWidget::onStartSBChanged() + { + SFogSettings s; + iface->getFogSettings( s ); + + s.start = startSB->value(); + + iface->setFogSettings( s ); + } + + void FogWidget::onEndSBChanged() + { + SFogSettings s; + iface->getFogSettings( s ); + + s.end = endSB->value(); + + iface->setFogSettings( s ); + } + + void FogWidget::onColorButtonClicked() + { + QColor c = QColorDialog::getColor(); + + setColorButtonColor( c.red(), c.green(), c.blue() ); + + SFogSettings s; + iface->getFogSettings( s ); + + s.color[ 0 ] = c.red(); + s.color[ 1 ] = c.green(); + s.color[ 2 ] = c.blue(); + s.color[ 3 ] = 255; + + iface->setFogSettings( s ); + } + + void FogWidget::setColorButtonColor( int r, int g, int b ) + { + QString sh; + sh = QString( "background-color: rgb(%1, %2, %3);" ).arg( r ).arg( g ).arg( b ); + colorButton->setStyleSheet( sh ); + } + +} + + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.h index cbd204cc3..e507c39cd 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/fog_widget.h @@ -1,56 +1,56 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef FOG_WIDGET_H -#define FOG_WIDGET_H - -#include "ui_fog_widget.h" - -namespace MaterialEditor -{ - class CNel3DInterface; - - class FogWidget : public QWidget, public Ui::FogWidget - { - Q_OBJECT - - public: - FogWidget( QWidget *parent = NULL ); - ~FogWidget(); - - void loadValues(); - - void setNl3DIface( CNel3DInterface *iface ){ this->iface = iface; } - - private Q_SLOTS: - void onFogCBClicked(); - void onStartSBChanged(); - void onEndSBChanged(); - void onColorButtonClicked(); - - private: - void setupConnections(); - void setColorButtonColor( int r, int g, int b ); - - - CNel3DInterface *iface; - - }; -} - -#endif - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef FOG_WIDGET_H +#define FOG_WIDGET_H + +#include "ui_fog_widget.h" + +namespace MaterialEditor +{ + class CNel3DInterface; + + class FogWidget : public QWidget, public Ui::FogWidget + { + Q_OBJECT + + public: + FogWidget( QWidget *parent = NULL ); + ~FogWidget(); + + void loadValues(); + + void setNl3DIface( CNel3DInterface *iface ){ this->iface = iface; } + + private Q_SLOTS: + void onFogCBClicked(); + void onStartSBChanged(); + void onEndSBChanged(); + void onColorButtonClicked(); + + private: + void setupConnections(); + void setColorButtonColor( int r, int g, int b ); + + + CNel3DInterface *iface; + + }; +} + +#endif + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.cpp index d7879d847..b75d63c7d 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.cpp @@ -1,270 +1,270 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "lights_widget.h" -#include -#include "nel3d_interface.h" - -namespace MaterialEditor -{ - enum LWLightTypes - { - DIRECTIONAL, - POINT, - SPOT - }; - - enum LWColorButton - { - AMBIENT, - DIFFUSE, - SPECULAR - }; - - void LightsWidget::setButtonColor( unsigned char butt, int r, int g, int b ) - { - QString sh; - QPushButton *button; - - if( butt > SPECULAR ) - return; - - switch( butt ) - { - case AMBIENT: - button = ambientButton; - break; - - case DIFFUSE: - button = diffuseButton; - break; - - case SPECULAR: - button = specularButton; - break; - } - - sh = QString( "background-color: rgb(%1, %2, %3);" ).arg( r ).arg( g ).arg( b ); - button->setStyleSheet( sh ); - - buttonColors[ butt ][ 0 ] = r; - buttonColors[ butt ][ 1 ] = g; - buttonColors[ butt ][ 2 ] = b; - } - - - - LightsWidget::LightsWidget( QWidget *parent ) : - QWidget( parent ) - { - setupUi( this ); - setupConnections(); - - typeCB->addItem( "Directional light" ); - typeCB->addItem( "Point light" ); - typeCB->addItem( "Spot light" ); - } - - LightsWidget::~LightsWidget() - { - } - - void LightsWidget::loadValues() - { - disableChangeConnections(); - - unsigned char c = iface->getMaxLights(); - - lightList->clear(); - - for( unsigned char i = 0; i < c; i++ ) - { - QString s = "light"; - s += QString::number( i ); - lightList->addItem( s ); - } - - lightList->setCurrentRow( 0 ); - loadLight( 0 ); - - // loadLight enables it anyways - //setupChangeConnections(); - } - - void LightsWidget::setupConnections() - { - } - - void LightsWidget::setupChangeConnections() - { - connect( enableCB, SIGNAL( toggled( bool ) ), this, SLOT( onChanges() ) ); - connect( ambientButton, SIGNAL( clicked( bool ) ), this, SLOT( onAmbButtonClicked() ) ); - connect( diffuseButton, SIGNAL( clicked( bool ) ), this, SLOT( onDiffButtonClicked() ) ); - connect( specularButton, SIGNAL( clicked( bool ) ), this, SLOT( onSpecButtonClicked() ) ); - connect( lightList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onLightChanged( int ) ) ); - - connect( typeCB, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onChanges() ) ); - connect( xSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - connect( ySB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - connect( zSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - connect( constAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - connect( linearAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - connect( quadAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - } - - void LightsWidget::disableChangeConnections() - { - disconnect( enableCB, SIGNAL( toggled( bool ) ), this, SLOT( onChanges() ) ); - disconnect( ambientButton, SIGNAL( clicked( bool ) ), this, SLOT( onAmbButtonClicked() ) ); - disconnect( diffuseButton, SIGNAL( clicked( bool ) ), this, SLOT( onDiffButtonClicked() ) ); - disconnect( specularButton, SIGNAL( clicked( bool ) ), this, SLOT( onSpecButtonClicked() ) ); - disconnect( lightList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onLightChanged( int ) ) ); - - disconnect( typeCB, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onChanges() ) ); - disconnect( xSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - disconnect( ySB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - disconnect( zSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - disconnect( constAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - disconnect( linearAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - disconnect( quadAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); - } - - void LightsWidget::onAmbButtonClicked() - { - QColor c = QColorDialog::getColor(); - setButtonColor( AMBIENT, c.red(), c.green(), c.blue() ); - applyChanges(); - } - - void LightsWidget::onDiffButtonClicked() - { - QColor c = QColorDialog::getColor(); - setButtonColor( DIFFUSE, c.red(), c.green(), c.blue() ); - applyChanges(); - } - - void LightsWidget::onSpecButtonClicked() - { - QColor c = QColorDialog::getColor(); - setButtonColor( SPECULAR, c.red(), c.green(), c.blue() ); - applyChanges(); - } - - void LightsWidget::onLightChanged( int light ) - { - loadLight( light ); - } - - void LightsWidget::onChanges() - { - applyChanges(); - } - - void LightsWidget::loadLight( unsigned char light ) - { - disableChangeConnections(); - - SLightInfo info; - iface->getLightInfo( light, info ); - - if( info.enabled ) - enableCB->setChecked( true ); - else - enableCB->setChecked( false ); - - switch( info.type ) - { - case SLightInfo::Directional: - typeCB->setCurrentIndex( DIRECTIONAL ); - break; - case SLightInfo::Point: - typeCB->setCurrentIndex( POINT ); - break; - case SLightInfo::Spot: - typeCB->setCurrentIndex( SPOT ); - break; - } - - xSB->setValue( info.posOrDir[ 0 ] ); - ySB->setValue( info.posOrDir[ 1 ] ); - zSB->setValue( info.posOrDir[ 2 ] ); - - constAttnButton->setValue( info.constAttn ); - linearAttnButton->setValue( info.linAttn ); - quadAttnButton->setValue( info.quadAttn ); - - setButtonColor( AMBIENT, info.ambColor[ 0 ] * 255.0f, - info.ambColor[ 1 ] * 255.0f, - info.ambColor[ 2 ] * 255.0f ); - - setButtonColor( DIFFUSE, info.diffColor[ 0 ] * 255.0f, - info.diffColor[ 1 ] * 255.0f, - info.diffColor[ 2 ] * 255.0f ); - - setButtonColor( SPECULAR, info.specColor[ 0 ] * 255.0f, - info.specColor[ 1 ] * 255.0f, - info.specColor[ 2 ] * 255.0f ); - - setupChangeConnections(); - } - - void LightsWidget::saveLight( unsigned char light ) - { - SLightInfo info; - - info.enabled = enableCB->isChecked(); - switch( typeCB->currentIndex() ) - { - case DIRECTIONAL: - info.type = SLightInfo::Directional; - break; - case POINT: - info.type = SLightInfo::Point; - break; - case SPOT: - info.type = SLightInfo::Spot; - break; - } - - info.posOrDir[ 0 ] = static_cast< float >( xSB->value() ); - info.posOrDir[ 1 ] = static_cast< float >( ySB->value() ); - info.posOrDir[ 2 ] = static_cast< float >( zSB->value() ); - - info.constAttn = static_cast< float >( constAttnButton->value() ); - info.linAttn = static_cast< float >( linearAttnButton->value() ); - info.quadAttn = static_cast< float >( quadAttnButton->value() ); - - info.ambColor[ 0 ] = buttonColors[ AMBIENT ][ 0 ] / 255.0f; - info.ambColor[ 1 ] = buttonColors[ AMBIENT ][ 1 ] / 255.0f; - info.ambColor[ 2 ] = buttonColors[ AMBIENT ][ 2 ] / 255.0f; - info.diffColor[ 0 ] = buttonColors[ DIFFUSE ][ 0 ] / 255.0f; - info.diffColor[ 1 ] = buttonColors[ DIFFUSE ][ 1 ] / 255.0f; - info.diffColor[ 2 ] = buttonColors[ DIFFUSE ][ 2 ] / 255.0f; - info.specColor[ 0 ] = buttonColors[ SPECULAR ][ 0 ] / 255.0f; - info.specColor[ 1 ] = buttonColors[ SPECULAR ][ 1 ] / 255.0f; - info.specColor[ 2 ] = buttonColors[ SPECULAR ][ 2 ] / 255.0f; - - iface->setLightInfo( light, info ); - } - - void LightsWidget::applyChanges() - { - int row = lightList->currentRow(); - saveLight( static_cast< unsigned char >( row ) ); - } - -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "lights_widget.h" +#include +#include "nel3d_interface.h" + +namespace MaterialEditor +{ + enum LWLightTypes + { + DIRECTIONAL, + POINT, + SPOT + }; + + enum LWColorButton + { + AMBIENT, + DIFFUSE, + SPECULAR + }; + + void LightsWidget::setButtonColor( unsigned char butt, int r, int g, int b ) + { + QString sh; + QPushButton *button; + + if( butt > SPECULAR ) + return; + + switch( butt ) + { + case AMBIENT: + button = ambientButton; + break; + + case DIFFUSE: + button = diffuseButton; + break; + + case SPECULAR: + button = specularButton; + break; + } + + sh = QString( "background-color: rgb(%1, %2, %3);" ).arg( r ).arg( g ).arg( b ); + button->setStyleSheet( sh ); + + buttonColors[ butt ][ 0 ] = r; + buttonColors[ butt ][ 1 ] = g; + buttonColors[ butt ][ 2 ] = b; + } + + + + LightsWidget::LightsWidget( QWidget *parent ) : + QWidget( parent ) + { + setupUi( this ); + setupConnections(); + + typeCB->addItem( "Directional light" ); + typeCB->addItem( "Point light" ); + typeCB->addItem( "Spot light" ); + } + + LightsWidget::~LightsWidget() + { + } + + void LightsWidget::loadValues() + { + disableChangeConnections(); + + unsigned char c = iface->getMaxLights(); + + lightList->clear(); + + for( unsigned char i = 0; i < c; i++ ) + { + QString s = "light"; + s += QString::number( i ); + lightList->addItem( s ); + } + + lightList->setCurrentRow( 0 ); + loadLight( 0 ); + + // loadLight enables it anyways + //setupChangeConnections(); + } + + void LightsWidget::setupConnections() + { + } + + void LightsWidget::setupChangeConnections() + { + connect( enableCB, SIGNAL( toggled( bool ) ), this, SLOT( onChanges() ) ); + connect( ambientButton, SIGNAL( clicked( bool ) ), this, SLOT( onAmbButtonClicked() ) ); + connect( diffuseButton, SIGNAL( clicked( bool ) ), this, SLOT( onDiffButtonClicked() ) ); + connect( specularButton, SIGNAL( clicked( bool ) ), this, SLOT( onSpecButtonClicked() ) ); + connect( lightList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onLightChanged( int ) ) ); + + connect( typeCB, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onChanges() ) ); + connect( xSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + connect( ySB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + connect( zSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + connect( constAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + connect( linearAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + connect( quadAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + } + + void LightsWidget::disableChangeConnections() + { + disconnect( enableCB, SIGNAL( toggled( bool ) ), this, SLOT( onChanges() ) ); + disconnect( ambientButton, SIGNAL( clicked( bool ) ), this, SLOT( onAmbButtonClicked() ) ); + disconnect( diffuseButton, SIGNAL( clicked( bool ) ), this, SLOT( onDiffButtonClicked() ) ); + disconnect( specularButton, SIGNAL( clicked( bool ) ), this, SLOT( onSpecButtonClicked() ) ); + disconnect( lightList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onLightChanged( int ) ) ); + + disconnect( typeCB, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onChanges() ) ); + disconnect( xSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + disconnect( ySB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + disconnect( zSB, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + disconnect( constAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + disconnect( linearAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + disconnect( quadAttnButton, SIGNAL( valueChanged( double ) ), this, SLOT( onChanges() ) ); + } + + void LightsWidget::onAmbButtonClicked() + { + QColor c = QColorDialog::getColor(); + setButtonColor( AMBIENT, c.red(), c.green(), c.blue() ); + applyChanges(); + } + + void LightsWidget::onDiffButtonClicked() + { + QColor c = QColorDialog::getColor(); + setButtonColor( DIFFUSE, c.red(), c.green(), c.blue() ); + applyChanges(); + } + + void LightsWidget::onSpecButtonClicked() + { + QColor c = QColorDialog::getColor(); + setButtonColor( SPECULAR, c.red(), c.green(), c.blue() ); + applyChanges(); + } + + void LightsWidget::onLightChanged( int light ) + { + loadLight( light ); + } + + void LightsWidget::onChanges() + { + applyChanges(); + } + + void LightsWidget::loadLight( unsigned char light ) + { + disableChangeConnections(); + + SLightInfo info; + iface->getLightInfo( light, info ); + + if( info.enabled ) + enableCB->setChecked( true ); + else + enableCB->setChecked( false ); + + switch( info.type ) + { + case SLightInfo::Directional: + typeCB->setCurrentIndex( DIRECTIONAL ); + break; + case SLightInfo::Point: + typeCB->setCurrentIndex( POINT ); + break; + case SLightInfo::Spot: + typeCB->setCurrentIndex( SPOT ); + break; + } + + xSB->setValue( info.posOrDir[ 0 ] ); + ySB->setValue( info.posOrDir[ 1 ] ); + zSB->setValue( info.posOrDir[ 2 ] ); + + constAttnButton->setValue( info.constAttn ); + linearAttnButton->setValue( info.linAttn ); + quadAttnButton->setValue( info.quadAttn ); + + setButtonColor( AMBIENT, info.ambColor[ 0 ] * 255.0f, + info.ambColor[ 1 ] * 255.0f, + info.ambColor[ 2 ] * 255.0f ); + + setButtonColor( DIFFUSE, info.diffColor[ 0 ] * 255.0f, + info.diffColor[ 1 ] * 255.0f, + info.diffColor[ 2 ] * 255.0f ); + + setButtonColor( SPECULAR, info.specColor[ 0 ] * 255.0f, + info.specColor[ 1 ] * 255.0f, + info.specColor[ 2 ] * 255.0f ); + + setupChangeConnections(); + } + + void LightsWidget::saveLight( unsigned char light ) + { + SLightInfo info; + + info.enabled = enableCB->isChecked(); + switch( typeCB->currentIndex() ) + { + case DIRECTIONAL: + info.type = SLightInfo::Directional; + break; + case POINT: + info.type = SLightInfo::Point; + break; + case SPOT: + info.type = SLightInfo::Spot; + break; + } + + info.posOrDir[ 0 ] = static_cast< float >( xSB->value() ); + info.posOrDir[ 1 ] = static_cast< float >( ySB->value() ); + info.posOrDir[ 2 ] = static_cast< float >( zSB->value() ); + + info.constAttn = static_cast< float >( constAttnButton->value() ); + info.linAttn = static_cast< float >( linearAttnButton->value() ); + info.quadAttn = static_cast< float >( quadAttnButton->value() ); + + info.ambColor[ 0 ] = buttonColors[ AMBIENT ][ 0 ] / 255.0f; + info.ambColor[ 1 ] = buttonColors[ AMBIENT ][ 1 ] / 255.0f; + info.ambColor[ 2 ] = buttonColors[ AMBIENT ][ 2 ] / 255.0f; + info.diffColor[ 0 ] = buttonColors[ DIFFUSE ][ 0 ] / 255.0f; + info.diffColor[ 1 ] = buttonColors[ DIFFUSE ][ 1 ] / 255.0f; + info.diffColor[ 2 ] = buttonColors[ DIFFUSE ][ 2 ] / 255.0f; + info.specColor[ 0 ] = buttonColors[ SPECULAR ][ 0 ] / 255.0f; + info.specColor[ 1 ] = buttonColors[ SPECULAR ][ 1 ] / 255.0f; + info.specColor[ 2 ] = buttonColors[ SPECULAR ][ 2 ] / 255.0f; + + iface->setLightInfo( light, info ); + } + + void LightsWidget::applyChanges() + { + int row = lightList->currentRow(); + saveLight( static_cast< unsigned char >( row ) ); + } + +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.h index 691ea9b7e..4a9cd0faf 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/lights_widget.h @@ -1,72 +1,72 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef LIGHTS_WIDGET_H -#define LIGHTS_WIDGET_H - -#include "ui_lights_widget.h" - -namespace MaterialEditor -{ - class CNel3DInterface; - - class LightsWidget : public QWidget, public Ui::LightsWidget - { - Q_OBJECT - - private: - void setButtonColor( unsigned char butt, int r, int g, int b ); - - public: - LightsWidget( QWidget *parent = NULL ); - ~LightsWidget(); - void setNL3DIface( CNel3DInterface *iface ){ this->iface = iface; } - void loadValues(); - - private Q_SLOTS: - void onAmbButtonClicked(); - void onDiffButtonClicked(); - void onSpecButtonClicked(); - void onLightChanged( int light ); - void onChanges(); - - private: - void setupConnections(); - void setupChangeConnections(); - void disableChangeConnections(); - void loadLight( unsigned char light ); - void saveLight( unsigned char light ); - void applyChanges(); - - CNel3DInterface *iface; - - enum LightType - { - Directional, - Point, - Spot - }; - - int buttonColors[ 3 ][ 3 ]; - }; - -} - - -#endif - - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef LIGHTS_WIDGET_H +#define LIGHTS_WIDGET_H + +#include "ui_lights_widget.h" + +namespace MaterialEditor +{ + class CNel3DInterface; + + class LightsWidget : public QWidget, public Ui::LightsWidget + { + Q_OBJECT + + private: + void setButtonColor( unsigned char butt, int r, int g, int b ); + + public: + LightsWidget( QWidget *parent = NULL ); + ~LightsWidget(); + void setNL3DIface( CNel3DInterface *iface ){ this->iface = iface; } + void loadValues(); + + private Q_SLOTS: + void onAmbButtonClicked(); + void onDiffButtonClicked(); + void onSpecButtonClicked(); + void onLightChanged( int light ); + void onChanges(); + + private: + void setupConnections(); + void setupChangeConnections(); + void disableChangeConnections(); + void loadLight( unsigned char light ); + void saveLight( unsigned char light ); + void applyChanges(); + + CNel3DInterface *iface; + + enum LightType + { + Directional, + Point, + Spot + }; + + int buttonColors[ 3 ][ 3 ]; + }; + +} + + +#endif + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_observer.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_observer.h index e73c40aab..4deb1dbfc 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_observer.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_observer.h @@ -1,43 +1,43 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef MATERIAL_OBSERVER_H -#define MATERIAL_OBSERVER_H - -namespace MaterialEditor -{ - /// Observes material changes - class CMaterialObserver - { - public: - CMaterialObserver(){} - virtual ~CMaterialObserver(){} - - virtual void onNewMaterial() = 0; - virtual void onMaterialLoaded() = 0; - - virtual void onPassAdded( const char *name ) = 0; - virtual void onPassRemoved( const char *name ) = 0; - virtual void onPassMovedUp( const char *name ) = 0; - virtual void onPassMovedDown( const char *name ) = 0; - virtual void onPassRenamed( const char *from, const char *to ) = 0; - }; - -} - - -#endif - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef MATERIAL_OBSERVER_H +#define MATERIAL_OBSERVER_H + +namespace MaterialEditor +{ + /// Observes material changes + class CMaterialObserver + { + public: + CMaterialObserver(){} + virtual ~CMaterialObserver(){} + + virtual void onNewMaterial() = 0; + virtual void onMaterialLoaded() = 0; + + virtual void onPassAdded( const char *name ) = 0; + virtual void onPassRemoved( const char *name ) = 0; + virtual void onPassMovedUp( const char *name ) = 0; + virtual void onPassMovedDown( const char *name ) = 0; + virtual void onPassRenamed( const char *from, const char *to ) = 0; + }; + +} + + +#endif + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.cpp index a01ae0739..b353ea9ec 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.cpp @@ -1,274 +1,274 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "material_properties.h" -#include "material_property_editor.h" -#include "nel3d_interface.h" - -#include - -namespace MaterialEditor -{ - - MatPropWidget::MatPropWidget( QWidget *parent ) : - QDialog( parent ) - { - setupUi( this ); - matPropEditWidget = new MatPropEditWidget(); - setupConnections(); - edit = false; - changed = false; - proxy = NULL; - } - - MatPropWidget::~MatPropWidget() - { - clear(); - - delete matPropEditWidget; - matPropEditWidget = NULL; - } - - void MatPropWidget::load( CRenderPassProxy *proxy ) - { - clear(); - changed = false; - this->proxy = new CRenderPassProxy( *proxy ); - - std::string n; - proxy->getName( n ); - nameEdit->setText( n.c_str() ); - - std::vector< SMatProp > v; - proxy->getProperties( v ); - - std::vector< SMatProp >::iterator itr = v.begin(); - while( itr != v.end() ) - { - SMatProp &mp = *itr; - QTreeWidgetItem *item = new QTreeWidgetItem(); - - item->setData( 0, Qt::DisplayRole, QString( mp.id.c_str() ) ); - item->setData( 1, Qt::DisplayRole, QString( mp.label.c_str() ) ); - - QString type = SMatProp::typeIdToString( mp.type ).c_str(); - item->setData( 2, Qt::DisplayRole, type ); - - item->setData( 3, Qt::DisplayRole, mp.value.c_str() ); - - treeWidget->addTopLevelItem( item ); - - ++itr; - } - } - - void MatPropWidget::clear() - { - treeWidget->clear(); - nameEdit->clear(); - if( this->proxy != NULL ) - { - delete this->proxy; - this->proxy = NULL; - } - } - - void MatPropWidget::onOKClicked() - { - if( proxy != NULL ) - { - std::vector< SMatProp > v; - SMatProp p; - QTreeWidgetItem *item = NULL; - std::string s; - - for( int i = 0; i < treeWidget->topLevelItemCount(); i++ ) - { - item = treeWidget->topLevelItem( i ); - p.id = item->text( 0 ).toUtf8().data(); - p.label = item->text( 1 ).toUtf8().data(); - - s = item->text( 2 ).toUtf8().data(); - p.type = SMatProp::typeStringToId( s ); - - p.value = item->text( 3 ).toUtf8().data(); - - v.push_back( p ); - } - - proxy->setProperties( v ); - } - - clear(); - setResult( QDialog::Accepted ); - close(); - } - - void MatPropWidget::onCancelClicked() - { - clear(); - setResult( QDialog::Rejected ); - close(); - } - - void MatPropWidget::onAddClicked() - { - edit = false; - changed = true; - matPropEditWidget->clear(); - matPropEditWidget->show(); - } - - void MatPropWidget::onEditClicked() - { - QTreeWidgetItem *item = treeWidget->currentItem(); - if( item == NULL ) - return; - - MaterialProperty prop; - prop.prop = item->data( 0, Qt::DisplayRole ).toString(); - prop.label = item->data( 1, Qt::DisplayRole ).toString(); - prop.type = item->data( 2, Qt::DisplayRole ).toString(); - - edit = true; - matPropEditWidget->setProperty( prop ); - matPropEditWidget->show(); - } - - void MatPropWidget::onRemoveClicked() - { - QTreeWidgetItem *item = treeWidget->currentItem(); - if( item == NULL ) - return; - - delete item; - changed = true; - } - - void MatPropWidget::onEditorOKClicked() - { - MaterialProperty prop; - matPropEditWidget->getProperty( prop ); - - if( edit ) - { - QTreeWidgetItem *item = treeWidget->currentItem(); - - MaterialProperty old; - old.prop = item->text( 0 ); - old.label = item->text( 1 ); - old.type = item->text( 2 ); - - if( old == prop ) - return; - - - if( idExists( prop.prop ) ) - { - QMessageBox::critical( - this, - tr( "Property Id" ), - tr( "A property with that Id already exists" ) - ); - - return; - } - - if( labelExists( prop.label ) ) - { - QMessageBox::critical( - this, - tr( "Property label" ), - tr( "A property with that label already exists" ) - ); - return; - } - - - item->setData( 0, Qt::DisplayRole, prop.prop ); - item->setData( 1, Qt::DisplayRole, prop.label ); - item->setData( 2, Qt::DisplayRole, prop.type ); - } - else - { - - if( idExists( prop.prop ) ) - { - QMessageBox::critical( - this, - tr( "Property Id" ), - tr( "A property with that Id already exists" ) - ); - - return; - } - - if( labelExists( prop.label ) ) - { - QMessageBox::critical( - this, - tr( "Property label" ), - tr( "A property with that label already exists" ) - ); - return; - } - - QTreeWidgetItem *item = new QTreeWidgetItem(); - item->setData( 0, Qt::DisplayRole, prop.prop ); - item->setData( 1, Qt::DisplayRole, prop.label ); - item->setData( 2, Qt::DisplayRole, prop.type ); - treeWidget->addTopLevelItem( item ); - } - changed = true; - - } - - void MatPropWidget::setupConnections() - { - connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); - connect( cancelButton, SIGNAL( clicked( bool ) ), this, SLOT( onCancelClicked() ) ); - connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); - connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); - connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); - connect( matPropEditWidget, SIGNAL( okClicked() ), this, SLOT( onEditorOKClicked() ) ); - } - - bool MatPropWidget::idExists( const QString &id ) - { - int c = treeWidget->topLevelItemCount(); - for( int i = 0; i < c; i++ ) - { - if( id == treeWidget->topLevelItem( i )->text( 0 ) ) - return true; - } - - return false; - } - - bool MatPropWidget::labelExists( const QString &label ) - { - int c = treeWidget->topLevelItemCount(); - for( int i = 0; i < c; i++ ) - { - if( label == treeWidget->topLevelItem( i )->text( 1 ) ) - return true; - } - - return false; - } - -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "material_properties.h" +#include "material_property_editor.h" +#include "nel3d_interface.h" + +#include + +namespace MaterialEditor +{ + + MatPropWidget::MatPropWidget( QWidget *parent ) : + QDialog( parent ) + { + setupUi( this ); + matPropEditWidget = new MatPropEditWidget(); + setupConnections(); + edit = false; + changed = false; + proxy = NULL; + } + + MatPropWidget::~MatPropWidget() + { + clear(); + + delete matPropEditWidget; + matPropEditWidget = NULL; + } + + void MatPropWidget::load( CRenderPassProxy *proxy ) + { + clear(); + changed = false; + this->proxy = new CRenderPassProxy( *proxy ); + + std::string n; + proxy->getName( n ); + nameEdit->setText( n.c_str() ); + + std::vector< SMatProp > v; + proxy->getProperties( v ); + + std::vector< SMatProp >::iterator itr = v.begin(); + while( itr != v.end() ) + { + SMatProp &mp = *itr; + QTreeWidgetItem *item = new QTreeWidgetItem(); + + item->setData( 0, Qt::DisplayRole, QString( mp.id.c_str() ) ); + item->setData( 1, Qt::DisplayRole, QString( mp.label.c_str() ) ); + + QString type = SMatProp::typeIdToString( mp.type ).c_str(); + item->setData( 2, Qt::DisplayRole, type ); + + item->setData( 3, Qt::DisplayRole, mp.value.c_str() ); + + treeWidget->addTopLevelItem( item ); + + ++itr; + } + } + + void MatPropWidget::clear() + { + treeWidget->clear(); + nameEdit->clear(); + if( this->proxy != NULL ) + { + delete this->proxy; + this->proxy = NULL; + } + } + + void MatPropWidget::onOKClicked() + { + if( proxy != NULL ) + { + std::vector< SMatProp > v; + SMatProp p; + QTreeWidgetItem *item = NULL; + std::string s; + + for( int i = 0; i < treeWidget->topLevelItemCount(); i++ ) + { + item = treeWidget->topLevelItem( i ); + p.id = item->text( 0 ).toUtf8().data(); + p.label = item->text( 1 ).toUtf8().data(); + + s = item->text( 2 ).toUtf8().data(); + p.type = SMatProp::typeStringToId( s ); + + p.value = item->text( 3 ).toUtf8().data(); + + v.push_back( p ); + } + + proxy->setProperties( v ); + } + + clear(); + setResult( QDialog::Accepted ); + close(); + } + + void MatPropWidget::onCancelClicked() + { + clear(); + setResult( QDialog::Rejected ); + close(); + } + + void MatPropWidget::onAddClicked() + { + edit = false; + changed = true; + matPropEditWidget->clear(); + matPropEditWidget->show(); + } + + void MatPropWidget::onEditClicked() + { + QTreeWidgetItem *item = treeWidget->currentItem(); + if( item == NULL ) + return; + + MaterialProperty prop; + prop.prop = item->data( 0, Qt::DisplayRole ).toString(); + prop.label = item->data( 1, Qt::DisplayRole ).toString(); + prop.type = item->data( 2, Qt::DisplayRole ).toString(); + + edit = true; + matPropEditWidget->setProperty( prop ); + matPropEditWidget->show(); + } + + void MatPropWidget::onRemoveClicked() + { + QTreeWidgetItem *item = treeWidget->currentItem(); + if( item == NULL ) + return; + + delete item; + changed = true; + } + + void MatPropWidget::onEditorOKClicked() + { + MaterialProperty prop; + matPropEditWidget->getProperty( prop ); + + if( edit ) + { + QTreeWidgetItem *item = treeWidget->currentItem(); + + MaterialProperty old; + old.prop = item->text( 0 ); + old.label = item->text( 1 ); + old.type = item->text( 2 ); + + if( old == prop ) + return; + + + if( idExists( prop.prop ) ) + { + QMessageBox::critical( + this, + tr( "Property Id" ), + tr( "A property with that Id already exists" ) + ); + + return; + } + + if( labelExists( prop.label ) ) + { + QMessageBox::critical( + this, + tr( "Property label" ), + tr( "A property with that label already exists" ) + ); + return; + } + + + item->setData( 0, Qt::DisplayRole, prop.prop ); + item->setData( 1, Qt::DisplayRole, prop.label ); + item->setData( 2, Qt::DisplayRole, prop.type ); + } + else + { + + if( idExists( prop.prop ) ) + { + QMessageBox::critical( + this, + tr( "Property Id" ), + tr( "A property with that Id already exists" ) + ); + + return; + } + + if( labelExists( prop.label ) ) + { + QMessageBox::critical( + this, + tr( "Property label" ), + tr( "A property with that label already exists" ) + ); + return; + } + + QTreeWidgetItem *item = new QTreeWidgetItem(); + item->setData( 0, Qt::DisplayRole, prop.prop ); + item->setData( 1, Qt::DisplayRole, prop.label ); + item->setData( 2, Qt::DisplayRole, prop.type ); + treeWidget->addTopLevelItem( item ); + } + changed = true; + + } + + void MatPropWidget::setupConnections() + { + connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); + connect( cancelButton, SIGNAL( clicked( bool ) ), this, SLOT( onCancelClicked() ) ); + connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); + connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); + connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); + connect( matPropEditWidget, SIGNAL( okClicked() ), this, SLOT( onEditorOKClicked() ) ); + } + + bool MatPropWidget::idExists( const QString &id ) + { + int c = treeWidget->topLevelItemCount(); + for( int i = 0; i < c; i++ ) + { + if( id == treeWidget->topLevelItem( i )->text( 0 ) ) + return true; + } + + return false; + } + + bool MatPropWidget::labelExists( const QString &label ) + { + int c = treeWidget->topLevelItemCount(); + for( int i = 0; i < c; i++ ) + { + if( label == treeWidget->topLevelItem( i )->text( 1 ) ) + return true; + } + + return false; + } + +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.h index 1021f1e94..d80c5acbd 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_properties.h @@ -1,60 +1,60 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef MATERIAL_PROPERTIES_H -#define MATERIAL_PROPERTIES_H - -#include "ui_material_properties.h" - -namespace MaterialEditor -{ - - class MatPropEditWidget; - class CRenderPassProxy; - - class MatPropWidget : public QDialog, public Ui::MatPropWidget - { - Q_OBJECT - public: - MatPropWidget( QWidget *parent = NULL ); - ~MatPropWidget(); - void load( CRenderPassProxy *proxy ); - void clear(); - bool getChanged() const{ return changed; } - - private Q_SLOTS: - void onOKClicked(); - void onCancelClicked(); - void onAddClicked(); - void onEditClicked(); - void onRemoveClicked(); - void onEditorOKClicked(); - - private: - void setupConnections(); - bool idExists( const QString &id ); - bool labelExists( const QString &id ); - - - bool edit; - bool changed; - MatPropEditWidget *matPropEditWidget; - CRenderPassProxy *proxy; - }; - -} - -#endif +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef MATERIAL_PROPERTIES_H +#define MATERIAL_PROPERTIES_H + +#include "ui_material_properties.h" + +namespace MaterialEditor +{ + + class MatPropEditWidget; + class CRenderPassProxy; + + class MatPropWidget : public QDialog, public Ui::MatPropWidget + { + Q_OBJECT + public: + MatPropWidget( QWidget *parent = NULL ); + ~MatPropWidget(); + void load( CRenderPassProxy *proxy ); + void clear(); + bool getChanged() const{ return changed; } + + private Q_SLOTS: + void onOKClicked(); + void onCancelClicked(); + void onAddClicked(); + void onEditClicked(); + void onRemoveClicked(); + void onEditorOKClicked(); + + private: + void setupConnections(); + bool idExists( const QString &id ); + bool labelExists( const QString &id ); + + + bool edit; + bool changed; + MatPropEditWidget *matPropEditWidget; + CRenderPassProxy *proxy; + }; + +} + +#endif diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.cpp index d16a80734..c3220db75 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.cpp @@ -1,73 +1,73 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "material_property_editor.h" - -namespace MaterialEditor -{ - MatPropEditWidget::MatPropEditWidget( QWidget *parent ) : - QWidget( parent ) - { - setupUi( this ); - setupConnections(); - } - - MatPropEditWidget::~MatPropEditWidget() - { - } - - void MatPropEditWidget::getProperty( MaterialProperty &prop ) - { - prop.prop = propertyEdit->text(); - prop.label = labelEdit->text(); - prop.type = typeCB->currentText(); - } - - void MatPropEditWidget::setProperty( const MaterialProperty &prop ) - { - propertyEdit->setText( prop.prop ); - labelEdit->setText( prop.label ); - int i = typeCB->findText( prop.type ); - if( i != -1 ) - typeCB->setCurrentIndex( i ); - - } - - void MatPropEditWidget::clear() - { - propertyEdit->clear(); - labelEdit->clear(); - typeCB->setCurrentIndex( 0 ); - } - - void MatPropEditWidget::onOKClicked() - { - close(); - Q_EMIT okClicked(); - } - - void MatPropEditWidget::onCancelClicked() - { - close(); - } - - void MatPropEditWidget::setupConnections() - { - connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); - connect( cancelButton, SIGNAL( clicked( bool ) ), this, SLOT( onCancelClicked() ) ); - } -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "material_property_editor.h" + +namespace MaterialEditor +{ + MatPropEditWidget::MatPropEditWidget( QWidget *parent ) : + QWidget( parent ) + { + setupUi( this ); + setupConnections(); + } + + MatPropEditWidget::~MatPropEditWidget() + { + } + + void MatPropEditWidget::getProperty( MaterialProperty &prop ) + { + prop.prop = propertyEdit->text(); + prop.label = labelEdit->text(); + prop.type = typeCB->currentText(); + } + + void MatPropEditWidget::setProperty( const MaterialProperty &prop ) + { + propertyEdit->setText( prop.prop ); + labelEdit->setText( prop.label ); + int i = typeCB->findText( prop.type ); + if( i != -1 ) + typeCB->setCurrentIndex( i ); + + } + + void MatPropEditWidget::clear() + { + propertyEdit->clear(); + labelEdit->clear(); + typeCB->setCurrentIndex( 0 ); + } + + void MatPropEditWidget::onOKClicked() + { + close(); + Q_EMIT okClicked(); + } + + void MatPropEditWidget::onCancelClicked() + { + close(); + } + + void MatPropEditWidget::setupConnections() + { + connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); + connect( cancelButton, SIGNAL( clicked( bool ) ), this, SLOT( onCancelClicked() ) ); + } +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.h index af9053191..7fc23e993 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_property_editor.h @@ -1,70 +1,70 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef MATERIAL_PROPERTY_EDITOR_H -#define MATERIAL_PROPERTY_EDITOR_H - -#include "ui_material_property_editor.h" - -namespace MaterialEditor -{ - - struct MaterialProperty - { - QString prop; - QString label; - QString type; - - bool operator==( const MaterialProperty &o ) - { - if( o.prop != prop ) - return false; - - if( o.label != label ) - return false; - - if( o.type != type ) - return false; - - return true; - } - }; - - class MatPropEditWidget : public QWidget, public Ui::MatPropEditWidget - { - Q_OBJECT - public: - MatPropEditWidget( QWidget *parent = NULL ); - ~MatPropEditWidget(); - void getProperty( MaterialProperty &prop ); - void setProperty( const MaterialProperty &prop ); - void clear(); - - Q_SIGNALS: - void okClicked(); - - private Q_SLOTS: - void onOKClicked(); - void onCancelClicked(); - - private: - void setupConnections(); - - }; -} - -#endif - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef MATERIAL_PROPERTY_EDITOR_H +#define MATERIAL_PROPERTY_EDITOR_H + +#include "ui_material_property_editor.h" + +namespace MaterialEditor +{ + + struct MaterialProperty + { + QString prop; + QString label; + QString type; + + bool operator==( const MaterialProperty &o ) + { + if( o.prop != prop ) + return false; + + if( o.label != label ) + return false; + + if( o.type != type ) + return false; + + return true; + } + }; + + class MatPropEditWidget : public QWidget, public Ui::MatPropEditWidget + { + Q_OBJECT + public: + MatPropEditWidget( QWidget *parent = NULL ); + ~MatPropEditWidget(); + void getProperty( MaterialProperty &prop ); + void setProperty( const MaterialProperty &prop ); + void clear(); + + Q_SIGNALS: + void okClicked(); + + private Q_SLOTS: + void onOKClicked(); + void onCancelClicked(); + + private: + void setupConnections(); + + }; +} + +#endif + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.cpp index 8647c7c55..566386c45 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.cpp @@ -1,141 +1,141 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "material_splitter.h" -#include "nel3d_interface.h" -#include "material_widget.h" -#include "prop_browser_ctrl.h" -#include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h" - -namespace MaterialEditor -{ - MaterialSplitter::MaterialSplitter( QWidget *parent ) : - QSplitter( parent ) - { - materialWidget = new MaterialWidget(); - browserCtrl = new CPropBrowserCtrl(); - browser = new QtTreePropertyBrowser(); - browserCtrl->setBrowser( browser ); - - setup(); - setupConnections(); - } - - MaterialSplitter::~MaterialSplitter() - { - delete browserCtrl; - browserCtrl = NULL; - nl3dIface = NULL; - materialWidget = NULL; - browser = NULL; - } - - void MaterialSplitter::setupConnections() - { - connect( materialWidget, SIGNAL( propsChanged() ), this, SLOT( onPropsChanged() ) ); - connect( materialWidget, SIGNAL( passChanged( const QString& ) ), this, SLOT( onPassChanged( const QString& ) ) ); - connect( materialWidget, SIGNAL( subMatChanged( int ) ), this, SLOT( onSubMatChanged( int ) ) ); - } - - void MaterialSplitter::setup() - { - setOrientation( Qt::Vertical ); - addWidget( materialWidget ); - addWidget( browser ); - } - - void MaterialSplitter::setNel3DIface( CNel3DInterface *iface ) - { - nl3dIface = iface; - materialWidget->setNel3DIface( iface ); - browserCtrl->setNel3DIface( iface ); - } - - void MaterialSplitter::onNewMaterial() - { - materialWidget->onNewMaterial(); - } - - void MaterialSplitter::onMaterialLoaded() - { - materialWidget->onMaterialLoaded(); - } - - void MaterialSplitter::onSceneCleared() - { - materialWidget->onSceneCleared(); - browserCtrl->onSceneCleared(); - } - - void MaterialSplitter::onShapeChanged() - { - materialWidget->onShapeChanged(); - } - - void MaterialSplitter::onPassAdded( const char *name ) - { - materialWidget->onPassAdded( name ); - } - - void MaterialSplitter::onPassRemoved( const char *name ) - { - materialWidget->onPassRemoved( name ); - } - - void MaterialSplitter::onPassMovedUp( const char *name ) - { - materialWidget->onPassMovedUp( name ); - } - - void MaterialSplitter::onPassMovedDown( const char *name ) - { - materialWidget->onPassMovedDown( name ); - } - - void MaterialSplitter::onPassRenamed( const char *from, const char *to ) - { - materialWidget->onPassRenamed( from, to ); - } - - void MaterialSplitter::onShaderAdded( const QString &name ) - { - materialWidget->onShaderAdded( name ); - } - - void MaterialSplitter::onShaderRemoved( const QString &name ) - { - materialWidget->onShaderRemoved( name ); - } - - void MaterialSplitter::onPropsChanged() - { - QString pass; - materialWidget->getCurrentPass( pass ); - browserCtrl->onPropsChanged(); - } - - void MaterialSplitter::onPassChanged( const QString &pass ) - { - browserCtrl->loadPropsForPass( pass ); - } - - void MaterialSplitter::onSubMatChanged( int i ) - { - browserCtrl->loadPropsForPass( 0 ); - } -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "material_splitter.h" +#include "nel3d_interface.h" +#include "material_widget.h" +#include "prop_browser_ctrl.h" +#include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h" + +namespace MaterialEditor +{ + MaterialSplitter::MaterialSplitter( QWidget *parent ) : + QSplitter( parent ) + { + materialWidget = new MaterialWidget(); + browserCtrl = new CPropBrowserCtrl(); + browser = new QtTreePropertyBrowser(); + browserCtrl->setBrowser( browser ); + + setup(); + setupConnections(); + } + + MaterialSplitter::~MaterialSplitter() + { + delete browserCtrl; + browserCtrl = NULL; + nl3dIface = NULL; + materialWidget = NULL; + browser = NULL; + } + + void MaterialSplitter::setupConnections() + { + connect( materialWidget, SIGNAL( propsChanged() ), this, SLOT( onPropsChanged() ) ); + connect( materialWidget, SIGNAL( passChanged( const QString& ) ), this, SLOT( onPassChanged( const QString& ) ) ); + connect( materialWidget, SIGNAL( subMatChanged( int ) ), this, SLOT( onSubMatChanged( int ) ) ); + } + + void MaterialSplitter::setup() + { + setOrientation( Qt::Vertical ); + addWidget( materialWidget ); + addWidget( browser ); + } + + void MaterialSplitter::setNel3DIface( CNel3DInterface *iface ) + { + nl3dIface = iface; + materialWidget->setNel3DIface( iface ); + browserCtrl->setNel3DIface( iface ); + } + + void MaterialSplitter::onNewMaterial() + { + materialWidget->onNewMaterial(); + } + + void MaterialSplitter::onMaterialLoaded() + { + materialWidget->onMaterialLoaded(); + } + + void MaterialSplitter::onSceneCleared() + { + materialWidget->onSceneCleared(); + browserCtrl->onSceneCleared(); + } + + void MaterialSplitter::onShapeChanged() + { + materialWidget->onShapeChanged(); + } + + void MaterialSplitter::onPassAdded( const char *name ) + { + materialWidget->onPassAdded( name ); + } + + void MaterialSplitter::onPassRemoved( const char *name ) + { + materialWidget->onPassRemoved( name ); + } + + void MaterialSplitter::onPassMovedUp( const char *name ) + { + materialWidget->onPassMovedUp( name ); + } + + void MaterialSplitter::onPassMovedDown( const char *name ) + { + materialWidget->onPassMovedDown( name ); + } + + void MaterialSplitter::onPassRenamed( const char *from, const char *to ) + { + materialWidget->onPassRenamed( from, to ); + } + + void MaterialSplitter::onShaderAdded( const QString &name ) + { + materialWidget->onShaderAdded( name ); + } + + void MaterialSplitter::onShaderRemoved( const QString &name ) + { + materialWidget->onShaderRemoved( name ); + } + + void MaterialSplitter::onPropsChanged() + { + QString pass; + materialWidget->getCurrentPass( pass ); + browserCtrl->onPropsChanged(); + } + + void MaterialSplitter::onPassChanged( const QString &pass ) + { + browserCtrl->loadPropsForPass( pass ); + } + + void MaterialSplitter::onSubMatChanged( int i ) + { + browserCtrl->loadPropsForPass( 0 ); + } +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.h index 784245fc2..a2edc48b2 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_splitter.h @@ -1,73 +1,73 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef MATERIAL_SPLITTER_H -#define MATERIAL_SPLITTER_H - -#include -#include "material_observer.h" - -class QtTreePropertyBrowser; - -namespace MaterialEditor -{ - class CNel3DInterface; - class MaterialWidget; - class CPropBrowserCtrl; - - class MaterialSplitter : public QSplitter, public CMaterialObserver - { - Q_OBJECT - public: - MaterialSplitter( QWidget *parent = NULL ); - ~MaterialSplitter(); - - void setupConnections(); - - void setup(); - - void setNel3DIface( CNel3DInterface *iface ); - - void onNewMaterial(); - void onMaterialLoaded(); - void onSceneCleared(); - void onShapeChanged(); - void onPassAdded( const char *name ); - void onPassRemoved( const char *name ); - void onPassMovedUp( const char *name ); - void onPassMovedDown( const char *name ); - void onPassRenamed( const char *from, const char *to ); - - public Q_SLOTS: - void onShaderAdded( const QString &name ); - void onShaderRemoved( const QString &name ); - - private: - CNel3DInterface *nl3dIface; - MaterialWidget *materialWidget; - CPropBrowserCtrl *browserCtrl; - QtTreePropertyBrowser *browser; - - private Q_SLOTS: - void onPropsChanged(); - void onPassChanged( const QString &pass ); - void onSubMatChanged( int i ); - - }; -} - -#endif +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef MATERIAL_SPLITTER_H +#define MATERIAL_SPLITTER_H + +#include +#include "material_observer.h" + +class QtTreePropertyBrowser; + +namespace MaterialEditor +{ + class CNel3DInterface; + class MaterialWidget; + class CPropBrowserCtrl; + + class MaterialSplitter : public QSplitter, public CMaterialObserver + { + Q_OBJECT + public: + MaterialSplitter( QWidget *parent = NULL ); + ~MaterialSplitter(); + + void setupConnections(); + + void setup(); + + void setNel3DIface( CNel3DInterface *iface ); + + void onNewMaterial(); + void onMaterialLoaded(); + void onSceneCleared(); + void onShapeChanged(); + void onPassAdded( const char *name ); + void onPassRemoved( const char *name ); + void onPassMovedUp( const char *name ); + void onPassMovedDown( const char *name ); + void onPassRenamed( const char *from, const char *to ); + + public Q_SLOTS: + void onShaderAdded( const QString &name ); + void onShaderRemoved( const QString &name ); + + private: + CNel3DInterface *nl3dIface; + MaterialWidget *materialWidget; + CPropBrowserCtrl *browserCtrl; + QtTreePropertyBrowser *browser; + + private Q_SLOTS: + void onPropsChanged(); + void onPassChanged( const QString &pass ); + void onSubMatChanged( int i ); + + }; +} + +#endif diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_widget.h index e8a9ba0d7..fd4464bd5 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_widget.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/material_widget.h @@ -1,76 +1,76 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef MATERIAL_WIDGET_H -#define MATERIAL_WIDGET_H - -#include "ui_material_widget.h" -#include "material_observer.h" - -namespace MaterialEditor -{ - class ShaderEditorWidget; - class MatPropWidget; - class CNel3DInterface; - - class MaterialWidget : public QWidget, public Ui::MaterialWidget, public CMaterialObserver - { - Q_OBJECT - public: - MaterialWidget( QWidget *parent = NULL ); - ~MaterialWidget(); - - void onSceneCleared(); - void onNewMaterial(); - void onMaterialLoaded(); - void onShapeChanged(); - - void onPassAdded( const char *name ); - void onPassRemoved( const char *name ); - void onPassMovedUp( const char *name ); - void onPassMovedDown( const char *name ); - void onPassRenamed( const char *from, const char *to ); - - void onShaderAdded( const QString &name ); - void onShaderRemoved( const QString &name ); - - void setNel3DIface( CNel3DInterface *iface ); - - void getCurrentPass( QString &pass ); - - Q_SIGNALS: - void propsChanged(); - void passChanged( const QString &pass ); - void subMatChanged( int i ); - - private: - void setupConnections(); - ShaderEditorWidget *shaderEditorWidget; - MatPropWidget *matPropWidget; - CNel3DInterface *nl3dIface; - - private Q_SLOTS: - void onPassEditClicked(); - void onShaderEditClicked(); - void onSubMatCBChanged( int i ); - void onPassCBChanged( const QString &text ); - void onShaderCBChanged( const QString &text ); - - }; - -} - -#endif +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef MATERIAL_WIDGET_H +#define MATERIAL_WIDGET_H + +#include "ui_material_widget.h" +#include "material_observer.h" + +namespace MaterialEditor +{ + class ShaderEditorWidget; + class MatPropWidget; + class CNel3DInterface; + + class MaterialWidget : public QWidget, public Ui::MaterialWidget, public CMaterialObserver + { + Q_OBJECT + public: + MaterialWidget( QWidget *parent = NULL ); + ~MaterialWidget(); + + void onSceneCleared(); + void onNewMaterial(); + void onMaterialLoaded(); + void onShapeChanged(); + + void onPassAdded( const char *name ); + void onPassRemoved( const char *name ); + void onPassMovedUp( const char *name ); + void onPassMovedDown( const char *name ); + void onPassRenamed( const char *from, const char *to ); + + void onShaderAdded( const QString &name ); + void onShaderRemoved( const QString &name ); + + void setNel3DIface( CNel3DInterface *iface ); + + void getCurrentPass( QString &pass ); + + Q_SIGNALS: + void propsChanged(); + void passChanged( const QString &pass ); + void subMatChanged( int i ); + + private: + void setupConnections(); + ShaderEditorWidget *shaderEditorWidget; + MatPropWidget *matPropWidget; + CNel3DInterface *nl3dIface; + + private Q_SLOTS: + void onPassEditClicked(); + void onShaderEditClicked(); + void onSubMatCBChanged( int i ); + void onPassCBChanged( const QString &text ); + void onShaderCBChanged( const QString &text ); + + }; + +} + +#endif diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp index 5112069f5..73eb6477b 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.cpp @@ -1,743 +1,743 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "nel3d_interface.h" -#include "nel/3d/dynamic_material.h" -#include "nel/3d/usr_shader_manager.h" -#include "nel/3d/usr_shader_program.h" -#include "nel/3d/usr_shader_loader.h" -#include "nel/3d/usr_shader_saver.h" -#include "nel/3d/driver_user.h" -#include "nel/3d/scene_user.h" -#include "nel/3d/u_camera.h" -#include "nel/3d/u_instance.h" -#include "nel/3d/u_light.h" -#include "nel/3d/u_3d_mouse_listener.h" -#include "nel/misc/i_xml.h" -#include "nel/misc/o_xml.h" -#include "nel/misc/file.h" -#include "nel/misc/path.h" - -namespace MaterialEditor -{ - - NL3D::UInstance currentShape; - - const char *SMatProp::idToString[] = - { - "Color", - "Vector4", - "Float", - "Double", - "Int", - "Uint", - "Matrix4", - "Texture" - }; - - const NLMISC::CVariant::EVarType evartypes[] = - { - NLMISC::CVariant::Vector4, - NLMISC::CVariant::Vector4, - NLMISC::CVariant::Float, - NLMISC::CVariant::Double, - NLMISC::CVariant::Int, - NLMISC::CVariant::UInt, - NLMISC::CVariant::Matrix4, - NLMISC::CVariant::String - }; - - NLMISC::CVariant::EVarType toEVarType( uint8 t ) - { - NLMISC::CVariant::EVarType rt = NLMISC::CVariant::Float; - - unsigned long s = sizeof( evartypes ) / sizeof( NLMISC::CVariant::EVarType ); - - if( t >= s ) - return rt; - else - rt = evartypes[ t ]; - - return rt; - - } - - std::string SMatProp::typeIdToString( unsigned char id ) - { - if( id >= EType_count ) - return std::string(); - else - return std::string( idToString[ id ] ); - } - - unsigned char SMatProp::typeStringToId( const std::string &s ) - { - for( unsigned char i = 0; i < EType_count; i++ ) - if( s == idToString[ i ] ) - return i; - return 0; - } - - void CRenderPassProxy::getProperties( std::vector< SMatProp > &v ) - { - uint32 count = pass->count(); - for( uint32 i = 0; i < count; i++ ) - { - const NL3D::SDynMaterialProp &p = *( pass->getProperty( i ) ); - - SMatProp prop; - prop.id = p.prop; - prop.label = p.label; - prop.type = p.type; - p.value.valueAsString( prop.value ); - - v.push_back( prop ); - } - } - - void CRenderPassProxy::setProperties( std::vector< SMatProp > &v ) - { - pass->clear(); - NL3D::SDynMaterialProp p; - - std::vector< SMatProp >::iterator itr = v.begin(); - while( itr != v.end() ) - { - p.prop = itr->id; - p.label = itr->label; - p.type = itr->type; - p.value.fromString( itr->value, toEVarType( p.type ) ); - - pass->addProperty( p ); - - ++itr; - } - } - - void CRenderPassProxy::getName( std::string &name ) - { - pass->getName( name ); - } - - void CRenderPassProxy::setName( const std::string &name ) - { - pass->setName( name ); - } - - void CRenderPassProxy::getShaderRef( std::string &s ) - { - pass->getShaderRef( s ); - } - - void CRenderPassProxy::setShaderRef( const std::string &s ) - { - pass->setShaderRef( s ); - } - - bool CRenderPassProxy::getProperty( const std::string &name, SMatProp &p ) - { - uint32 count = pass->count(); - uint32 i = 0; - - for( i = 0; i < count; i++ ) - { - if( pass->getProperty( i )->prop == name ) - break; - } - - if( i == count ) - return false; - - const NL3D::SDynMaterialProp *prop = pass->getProperty( i ); - p.id = prop->prop; - p.label = prop->label; - p.type = prop->type; - prop->value.valueAsString( p.value ); - - return true; - } - - bool CRenderPassProxy::changeProperty( const SMatProp &p ) - { - NL3D::SDynMaterialProp prop; - prop.prop = p.id; - prop.label = p.label; - prop.type = p.type; - prop.value.fromString( p.value, toEVarType( prop.type ) ); - - return pass->changeProperty( prop.prop, prop ); - } - - void CNelMaterialProxy::getPassList( std::vector< std::string > &l ) - { - material->getPassList( l ); - } - - void CNelMaterialProxy::addPass( const char *name ) - { - NL3D::SRenderPass pass; - pass.setName( name ); - material->addPass( pass ); - } - - void CNelMaterialProxy::removePass( const char *name ) - { - material->removePass( name ); - } - - void CNelMaterialProxy::movePassUp( const char *name ) - { - material->movePassUp( name ); - } - - void CNelMaterialProxy::movePassDown( const char *name ) - { - material->movePassDown( name ); - } - - void CNelMaterialProxy::renamePass( const char *from, const char *to ) - { - material->renamePass( from, to ); - } - - CRenderPassProxy CNelMaterialProxy::getPass( unsigned long i ) - { - if( i >= material->getPassCount() ) - return CRenderPassProxy( NULL ); - else - return CRenderPassProxy( material->getPass( i ) ); - } - - CRenderPassProxy CNelMaterialProxy::getPass( const char *name ) - { - return CRenderPassProxy( material->getPass( name ) ); - } - - - CNel3DInterface::CNel3DInterface() - { - shaderManager = new NL3D::CUsrShaderManager(); - driver = NULL; - scene = NULL; - mouseListener = NULL; - subMatId = 0; - std::fill( bgColor, bgColor + 4, 255 ); - } - - CNel3DInterface::~CNel3DInterface() - { - delete shaderManager; - shaderManager = NULL; - killViewPort(); - } - - bool CNel3DInterface::loadMaterial( const char *fname ) - { - if( currentShape.empty() ) - return false; - - NLMISC::CIFile file; - if( !file.open( fname, true ) ) - return false; - - NLMISC::CIXml xml; - if( !xml.init( file ) ) - return false; - - NL3D::CDynMaterial *mat = currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat(); - if( mat != NULL ) - mat->clear(); - else - { - currentShape.getMaterial( subMatId ).getObjectPtr()->createCleanDynMat(); - mat = currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat(); - } - - mat->serial( xml ); - file.close(); - - return true; - } - - bool CNel3DInterface::saveMaterial( const char *fname ) - { - if( currentShape.empty() ) - return false; - - NLMISC::COFile file; - if( !file.open( fname, false, true ) ) - return false; - - NLMISC::COXml xml; - if( !xml.init( &file ) ) - return false; - - currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat()->serial( xml ); - - xml.flush(); - file.close(); - - return true; - } - - void CNel3DInterface::genMaterials() - { - int c = currentShape.getNumMaterials(); - for( int i = 0; i < c; i++ ) - currentShape.getMaterial( i ).getObjectPtr()->createDynMat(); - } - - void CNel3DInterface::newMaterial() - { - if( currentShape.empty() ) - return; - - for( int i = 0; i < currentShape.getNumMaterials(); i++ ) - currentShape.getMaterial( i ).getObjectPtr()->createCleanDynMat(); - } - - bool CNel3DInterface::selectSubMaterial( int id ) - { - if( currentShape.empty() ) - return false; - - if( currentShape.getNumMaterials() < id ) - return false; - - subMatId = id; - - return true; - } - - CNelMaterialProxy CNel3DInterface::getMaterial() - { - NL3D::CDynMaterial *mat = NULL; - if( !currentShape.empty() && ( currentShape.getNumMaterials() > 0 ) ) - mat = currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat(); - - return CNelMaterialProxy( mat ); - } - - void CNel3DInterface::getShaderList( std::vector< std::string > &v ) - { - shaderManager->getShaderList( v ); - } - - bool CNel3DInterface::getShaderInfo( const std::string &name, SShaderInfo &info ) - { - NL3D::CUsrShaderProgram 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::CUsrShaderProgram 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::CUsrShaderProgram *program = new NL3D::CUsrShaderProgram(); - - 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 ); - } - - void CNel3DInterface::loadShaders() - { - NL3D::CUsrShaderLoader loader; - loader.setManager( shaderManager ); - loader.loadShaders( "./shaders" ); - } - - void CNel3DInterface::saveShader( const std::string &name ) - { - NL3D::CUsrShaderSaver saver; - saver.setManager( shaderManager ); - saver.saveShader( "./shaders", name ); - NL3D::CDriverUser *d = dynamic_cast< NL3D::CDriverUser* >( driver ); - if( d != NULL ) - d->getDriver()->reloadUserShaders(); - } - - void CNel3DInterface::deleteShader( const std::string &name ) - { - NLMISC::CFile::deleteFile( "./shaders/" + name + ".nlshdr" ); - } - - void CNel3DInterface::initViewPort( unsigned long wnd, unsigned long w, unsigned long h ) - { - //driver = NL3D::UDriver::createDriver( 0, false, 0 ); - driver = NL3D::UDriver::createDriver( 0, NL3D::UDriver::OpenGl3, 0 ); - nlassert( driver != NULL ); - driver->setDisplay( (nlWindow)wnd, NL3D::UDriver::CMode( w, h, 32 ) ); - - scene = driver->createScene( true ); - - - driver->enableFog( true ); - driver->setupFog( 5.0f, 15.0f, NLMISC::CRGBA::White ); - - - NL3D::ULight *ld; - - /* - // Let's add a directional light! - ld = NL3D::ULight::createLight(); - ld->setMode( NL3D::ULight::DirectionalLight ); - ld->setDirection( NLMISC::CVector( -100.0f, 100.0f, 100.0f ) ); - ld->setAmbiant( NLMISC::CRGBA::CRGBA( 0.1f, 0.1f, 0.1f, 0.1f ) ); - ld->setSpecular( NLMISC::CRGBA::White ); - ld->setDiffuse( NLMISC::CRGBA::White ); - driver->setLight( 0, *ld ); - driver->enableLight( 0, true ); - delete ld; - */ - - // Let's add a point light as well! - ld = NL3D::ULight::createLight(); - ld->setMode( NL3D::ULight::PointLight ); - ld->setPosition( NLMISC::CVector( 0.0f, 0.0f, 0.0f ) ); - ld->setAmbiant( NLMISC::CRGBA::CRGBA( 0.1f, 0.1f, 0.1f, 0.1f ) ); - ld->setSpecular( NLMISC::CRGBA::White ); - ld->setDiffuse( NLMISC::CRGBA::White ); - ld->setConstantAttenuation( 1.0f ); - ld->setLinearAttenuation( 0.0f ); - ld->setQuadraticAttenuation( 0.0f ); - driver->setLight( 0, *ld ); - driver->enableLight( 0, true ); - delete ld; - ld = NULL; - - scene->enableLightingSystem( false ); - - - mouseListener = driver->create3dMouseListener(); - mouseListener->setMouseMode( NL3D::U3dMouseListener::nelStyle ); - } - - void CNel3DInterface::killViewPort() - { - driver->deleteScene( scene ); - scene = NULL; - driver->delete3dMouseListener( mouseListener ); - mouseListener = NULL; - delete driver; - driver = NULL; - } - - void CNel3DInterface::resizeViewPort( unsigned long w, unsigned long h ) - { - if( scene != NULL ) - { - scene->getCam().setPerspective( 90.0f, w / (float)h, 0.1f, 1000.0f ); - } - } - - bool CNel3DInterface::addCube() - { - return loadShape( "primitives/cube.shape" ); - } - - bool CNel3DInterface::addSphere() - { - return loadShape( "primitives/sphere.shape" ); - } - - bool CNel3DInterface::addCylinder() - { - return loadShape( "primitives/cylinder.shape" ); - } - - bool CNel3DInterface::addTeaPot() - { - return loadShape( "primitives/teapot.shape" ); - } - - bool CNel3DInterface::loadShape( const std::string &fileName ) - { - NLMISC::CPath::addSearchPath( NLMISC::CFile::getPath( fileName ), false, false ); - NL3D::UInstance instance = scene->createInstance( fileName ); - if( instance.empty() ) - return false; - - clearScene(); - currentShape = instance; - - subMatId = 0; - - setupCamera(); - - return true; - } - - void CNel3DInterface::clearScene() - { - if( scene != NULL ) - { - if( currentShape.empty() ) - return; - scene->deleteInstance( currentShape ); - currentShape = NL3D::UInstance(); - subMatId = 0; - } - - if( driver == NULL ) - return; - - NLMISC::CRGBA c; - c.R = bgColor[ 0 ]; - c.G = bgColor[ 1 ]; - c.B = bgColor[ 2 ]; - c.A = bgColor[ 3 ]; - - driver->clearBuffers( c ); - driver->swapBuffers(); - } - - void CNel3DInterface::updateInput() - { - driver->EventServer.pump(); - } - - void CNel3DInterface::renderScene() - { - if( scene != NULL ) - { - scene->getCam().setTransformMode( NL3D::UTransformable::DirectMatrix ); - scene->getCam().setMatrix( mouseListener->getViewMatrix() ); - - NLMISC::CRGBA c; - c.R = bgColor[ 0 ]; - c.G = bgColor[ 1 ]; - c.B = bgColor[ 2 ]; - c.A = bgColor[ 3 ]; - - driver->clearBuffers( c ); - scene->render(); - driver->swapBuffers(); - } - } - - unsigned long CNel3DInterface::getShapeMatCount() const - { - if( currentShape.empty() ) - return 0; - - return currentShape.getNumMaterials(); - } - - void CNel3DInterface::getFogSettings( SFogSettings &s ) - { - s.enable = driver->fogEnabled(); - s.start = driver->getFogStart(); - s.end = driver->getFogEnd(); - - NLMISC::CRGBA c = driver->getFogColor(); - s.color[ 0 ] = c.R; - s.color[ 1 ] = c.G; - s.color[ 2 ] = c.B; - s.color[ 3 ] = c.A; - } - - void CNel3DInterface::setFogSettings( const SFogSettings &s ) - { - driver->enableFog( s.enable ); - NLMISC::CRGBA c; - c.R = s.color[ 0 ]; - c.G = s.color[ 1 ]; - c.B = s.color[ 2 ]; - c.A = 255; - driver->setupFog( s.start, s.end, c ); - setBGColor( c.R, c.G, c.B, c.A ); - } - - unsigned char CNel3DInterface::getMaxLights() const - { - return driver->getMaxDriverLights(); - } - - void CNel3DInterface::getLightInfo( unsigned char light, SLightInfo &info ) - { - info.enabled = driver->isLightEnabled( light ); - - NL3D::ULight *u = driver->getLight( light ); - info.ambColor[ 0 ] = u->getAmbiant().R / 255.0f; - info.ambColor[ 1 ] = u->getAmbiant().G / 255.0f; - info.ambColor[ 2 ] = u->getAmbiant().B / 255.0f; - info.ambColor[ 3 ] = u->getAmbiant().A / 255.0f; - - info.diffColor[ 0 ] = u->getDiffuse().R / 255.0f; - info.diffColor[ 1 ] = u->getDiffuse().G / 255.0f; - info.diffColor[ 2 ] = u->getDiffuse().B / 255.0f; - info.diffColor[ 3 ] = u->getDiffuse().A / 255.0f; - - info.specColor[ 0 ] = u->getSpecular().R / 255.0f; - info.specColor[ 1 ] = u->getSpecular().G / 255.0f; - info.specColor[ 2 ] = u->getSpecular().B / 255.0f; - info.specColor[ 3 ] = u->getSpecular().A / 255.0f; - - info.constAttn = u->getConstantAttenuation(); - info.linAttn = u->getLinearAttenuation(); - info.quadAttn = u->getQuadraticAttenuation(); - - switch( u->getMode() ) - { - case NL3D::ULight::DirectionalLight: - info.type = SLightInfo::Directional; - break; - - case NL3D::ULight::PointLight: - info.type = SLightInfo::Point; - break; - - case NL3D::ULight::SpotLight: - info.type = SLightInfo::Spot; - break; - } - - if( info.type == SLightInfo::Directional ) - { - info.posOrDir[ 0 ] = u->getDirection().x; - info.posOrDir[ 1 ] = u->getDirection().y; - info.posOrDir[ 2 ] = u->getDirection().z; - } - else - { - info.posOrDir[ 0 ] = u->getPosition().x; - info.posOrDir[ 1 ] = u->getPosition().y; - info.posOrDir[ 2 ] = u->getPosition().z; - } - - delete u; - } - - void CNel3DInterface::setLightInfo( unsigned char light, const SLightInfo &info ) - { - NL3D::ULight *u = NL3D::ULight::createLight(); - - NLMISC::CRGBA c; - c.R = info.ambColor[ 0 ] * 255.0f; - c.G = info.ambColor[ 1 ] * 255.0f; - c.B = info.ambColor[ 2 ] * 255.0f; - c.A = 255; - u->setAmbiant( c ); - - c.R = info.diffColor[ 0 ] * 255.0f; - c.G = info.diffColor[ 1 ] * 255.0f; - c.B = info.diffColor[ 2 ] * 255.0f; - u->setDiffuse( c ); - - c.R = info.specColor[ 0 ] * 255.0f; - c.G = info.specColor[ 1 ] * 255.0f; - c.B = info.specColor[ 2 ] * 255.0f; - u->setSpecular( c ); - - switch( info.type ) - { - case SLightInfo::Directional: - u->setMode( NL3D::ULight::DirectionalLight ); - break; - case SLightInfo::Point: - u->setMode( NL3D::ULight::PointLight ); - break; - case SLightInfo::Spot: - u->setMode( NL3D::ULight::SpotLight ); - break; - } - - if( info.type == SLightInfo::Directional ) - u->setDirection( NLMISC::CVector( info.posOrDir[ 0 ], info.posOrDir[ 1 ], info.posOrDir[ 2 ] ) ); - else - u->setPosition( NLMISC::CVector( info.posOrDir[ 0 ], info.posOrDir[ 1 ], info.posOrDir[ 2 ] ) ); - - u->setConstantAttenuation( info.constAttn ); - u->setLinearAttenuation( info.linAttn ); - u->setQuadraticAttenuation( info.quadAttn ); - - driver->setLight( light, *u ); - driver->enableLight( light, info.enabled ); - - delete u; - u = NULL; - } - - void CNel3DInterface::setupCamera() - { - NLMISC::CAABBox bbox; - currentShape.getShapeAABBox( bbox ); - - NLMISC::CVector center = bbox.getCenter(); - NLMISC::CQuat q( 0.0f, 0.0f, 0.0f, 0.0f ); - currentShape.getDefaultRotQuat( q ); - - NLMISC::CVector max_radius = bbox.getHalfSize(); - float radius = std::max( max_radius.x, std::max( max_radius.y, max_radius.z ) ); - float distance = radius / tan( 45.0f ); - - NLMISC::CVector axis = q.getAxis(); - if( axis.isNull() || ( axis == NLMISC::CVector::I ) ) - axis = NLMISC::CVector::J; - else - if( axis == -NLMISC::CVector::K ) - axis = -NLMISC::CVector::J; - - NLMISC::CVector eye = center - axis * ( radius + distance ); - scene->getCam().lookAt( eye, center ); - - mouseListener->setHotSpot( center ); - mouseListener->setMatrix( scene->getCam().getMatrix() ); - } - -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "nel3d_interface.h" +#include "nel/3d/dynamic_material.h" +#include "nel/3d/usr_shader_manager.h" +#include "nel/3d/usr_shader_program.h" +#include "nel/3d/usr_shader_loader.h" +#include "nel/3d/usr_shader_saver.h" +#include "nel/3d/driver_user.h" +#include "nel/3d/scene_user.h" +#include "nel/3d/u_camera.h" +#include "nel/3d/u_instance.h" +#include "nel/3d/u_light.h" +#include "nel/3d/u_3d_mouse_listener.h" +#include "nel/misc/i_xml.h" +#include "nel/misc/o_xml.h" +#include "nel/misc/file.h" +#include "nel/misc/path.h" + +namespace MaterialEditor +{ + + NL3D::UInstance currentShape; + + const char *SMatProp::idToString[] = + { + "Color", + "Vector4", + "Float", + "Double", + "Int", + "Uint", + "Matrix4", + "Texture" + }; + + const NLMISC::CVariant::EVarType evartypes[] = + { + NLMISC::CVariant::Vector4, + NLMISC::CVariant::Vector4, + NLMISC::CVariant::Float, + NLMISC::CVariant::Double, + NLMISC::CVariant::Int, + NLMISC::CVariant::UInt, + NLMISC::CVariant::Matrix4, + NLMISC::CVariant::String + }; + + NLMISC::CVariant::EVarType toEVarType( uint8 t ) + { + NLMISC::CVariant::EVarType rt = NLMISC::CVariant::Float; + + unsigned long s = sizeof( evartypes ) / sizeof( NLMISC::CVariant::EVarType ); + + if( t >= s ) + return rt; + else + rt = evartypes[ t ]; + + return rt; + + } + + std::string SMatProp::typeIdToString( unsigned char id ) + { + if( id >= EType_count ) + return std::string(); + else + return std::string( idToString[ id ] ); + } + + unsigned char SMatProp::typeStringToId( const std::string &s ) + { + for( unsigned char i = 0; i < EType_count; i++ ) + if( s == idToString[ i ] ) + return i; + return 0; + } + + void CRenderPassProxy::getProperties( std::vector< SMatProp > &v ) + { + uint32 count = pass->count(); + for( uint32 i = 0; i < count; i++ ) + { + const NL3D::SDynMaterialProp &p = *( pass->getProperty( i ) ); + + SMatProp prop; + prop.id = p.prop; + prop.label = p.label; + prop.type = p.type; + p.value.valueAsString( prop.value ); + + v.push_back( prop ); + } + } + + void CRenderPassProxy::setProperties( std::vector< SMatProp > &v ) + { + pass->clear(); + NL3D::SDynMaterialProp p; + + std::vector< SMatProp >::iterator itr = v.begin(); + while( itr != v.end() ) + { + p.prop = itr->id; + p.label = itr->label; + p.type = itr->type; + p.value.fromString( itr->value, toEVarType( p.type ) ); + + pass->addProperty( p ); + + ++itr; + } + } + + void CRenderPassProxy::getName( std::string &name ) + { + pass->getName( name ); + } + + void CRenderPassProxy::setName( const std::string &name ) + { + pass->setName( name ); + } + + void CRenderPassProxy::getShaderRef( std::string &s ) + { + pass->getShaderRef( s ); + } + + void CRenderPassProxy::setShaderRef( const std::string &s ) + { + pass->setShaderRef( s ); + } + + bool CRenderPassProxy::getProperty( const std::string &name, SMatProp &p ) + { + uint32 count = pass->count(); + uint32 i = 0; + + for( i = 0; i < count; i++ ) + { + if( pass->getProperty( i )->prop == name ) + break; + } + + if( i == count ) + return false; + + const NL3D::SDynMaterialProp *prop = pass->getProperty( i ); + p.id = prop->prop; + p.label = prop->label; + p.type = prop->type; + prop->value.valueAsString( p.value ); + + return true; + } + + bool CRenderPassProxy::changeProperty( const SMatProp &p ) + { + NL3D::SDynMaterialProp prop; + prop.prop = p.id; + prop.label = p.label; + prop.type = p.type; + prop.value.fromString( p.value, toEVarType( prop.type ) ); + + return pass->changeProperty( prop.prop, prop ); + } + + void CNelMaterialProxy::getPassList( std::vector< std::string > &l ) + { + material->getPassList( l ); + } + + void CNelMaterialProxy::addPass( const char *name ) + { + NL3D::SRenderPass pass; + pass.setName( name ); + material->addPass( pass ); + } + + void CNelMaterialProxy::removePass( const char *name ) + { + material->removePass( name ); + } + + void CNelMaterialProxy::movePassUp( const char *name ) + { + material->movePassUp( name ); + } + + void CNelMaterialProxy::movePassDown( const char *name ) + { + material->movePassDown( name ); + } + + void CNelMaterialProxy::renamePass( const char *from, const char *to ) + { + material->renamePass( from, to ); + } + + CRenderPassProxy CNelMaterialProxy::getPass( unsigned long i ) + { + if( i >= material->getPassCount() ) + return CRenderPassProxy( NULL ); + else + return CRenderPassProxy( material->getPass( i ) ); + } + + CRenderPassProxy CNelMaterialProxy::getPass( const char *name ) + { + return CRenderPassProxy( material->getPass( name ) ); + } + + + CNel3DInterface::CNel3DInterface() + { + shaderManager = new NL3D::CUsrShaderManager(); + driver = NULL; + scene = NULL; + mouseListener = NULL; + subMatId = 0; + std::fill( bgColor, bgColor + 4, 255 ); + } + + CNel3DInterface::~CNel3DInterface() + { + delete shaderManager; + shaderManager = NULL; + killViewPort(); + } + + bool CNel3DInterface::loadMaterial( const char *fname ) + { + if( currentShape.empty() ) + return false; + + NLMISC::CIFile file; + if( !file.open( fname, true ) ) + return false; + + NLMISC::CIXml xml; + if( !xml.init( file ) ) + return false; + + NL3D::CDynMaterial *mat = currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat(); + if( mat != NULL ) + mat->clear(); + else + { + currentShape.getMaterial( subMatId ).getObjectPtr()->createCleanDynMat(); + mat = currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat(); + } + + mat->serial( xml ); + file.close(); + + return true; + } + + bool CNel3DInterface::saveMaterial( const char *fname ) + { + if( currentShape.empty() ) + return false; + + NLMISC::COFile file; + if( !file.open( fname, false, true ) ) + return false; + + NLMISC::COXml xml; + if( !xml.init( &file ) ) + return false; + + currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat()->serial( xml ); + + xml.flush(); + file.close(); + + return true; + } + + void CNel3DInterface::genMaterials() + { + int c = currentShape.getNumMaterials(); + for( int i = 0; i < c; i++ ) + currentShape.getMaterial( i ).getObjectPtr()->createDynMat(); + } + + void CNel3DInterface::newMaterial() + { + if( currentShape.empty() ) + return; + + for( int i = 0; i < currentShape.getNumMaterials(); i++ ) + currentShape.getMaterial( i ).getObjectPtr()->createCleanDynMat(); + } + + bool CNel3DInterface::selectSubMaterial( int id ) + { + if( currentShape.empty() ) + return false; + + if( currentShape.getNumMaterials() < id ) + return false; + + subMatId = id; + + return true; + } + + CNelMaterialProxy CNel3DInterface::getMaterial() + { + NL3D::CDynMaterial *mat = NULL; + if( !currentShape.empty() && ( currentShape.getNumMaterials() > 0 ) ) + mat = currentShape.getMaterial( subMatId ).getObjectPtr()->getDynMat(); + + return CNelMaterialProxy( mat ); + } + + void CNel3DInterface::getShaderList( std::vector< std::string > &v ) + { + shaderManager->getShaderList( v ); + } + + bool CNel3DInterface::getShaderInfo( const std::string &name, SShaderInfo &info ) + { + NL3D::CUsrShaderProgram 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::CUsrShaderProgram 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::CUsrShaderProgram *program = new NL3D::CUsrShaderProgram(); + + 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 ); + } + + void CNel3DInterface::loadShaders() + { + NL3D::CUsrShaderLoader loader; + loader.setManager( shaderManager ); + loader.loadShaders( "./shaders" ); + } + + void CNel3DInterface::saveShader( const std::string &name ) + { + NL3D::CUsrShaderSaver saver; + saver.setManager( shaderManager ); + saver.saveShader( "./shaders", name ); + NL3D::CDriverUser *d = dynamic_cast< NL3D::CDriverUser* >( driver ); + if( d != NULL ) + d->getDriver()->reloadUserShaders(); + } + + void CNel3DInterface::deleteShader( const std::string &name ) + { + NLMISC::CFile::deleteFile( "./shaders/" + name + ".nlshdr" ); + } + + void CNel3DInterface::initViewPort( unsigned long wnd, unsigned long w, unsigned long h ) + { + //driver = NL3D::UDriver::createDriver( 0, false, 0 ); + driver = NL3D::UDriver::createDriver( 0, NL3D::UDriver::OpenGl3, 0 ); + nlassert( driver != NULL ); + driver->setDisplay( (nlWindow)wnd, NL3D::UDriver::CMode( w, h, 32 ) ); + + scene = driver->createScene( true ); + + + driver->enableFog( true ); + driver->setupFog( 5.0f, 15.0f, NLMISC::CRGBA::White ); + + + NL3D::ULight *ld; + + /* + // Let's add a directional light! + ld = NL3D::ULight::createLight(); + ld->setMode( NL3D::ULight::DirectionalLight ); + ld->setDirection( NLMISC::CVector( -100.0f, 100.0f, 100.0f ) ); + ld->setAmbiant( NLMISC::CRGBA::CRGBA( 0.1f, 0.1f, 0.1f, 0.1f ) ); + ld->setSpecular( NLMISC::CRGBA::White ); + ld->setDiffuse( NLMISC::CRGBA::White ); + driver->setLight( 0, *ld ); + driver->enableLight( 0, true ); + delete ld; + */ + + // Let's add a point light as well! + ld = NL3D::ULight::createLight(); + ld->setMode( NL3D::ULight::PointLight ); + ld->setPosition( NLMISC::CVector( 0.0f, 0.0f, 0.0f ) ); + ld->setAmbiant( NLMISC::CRGBA::CRGBA( 0.1f, 0.1f, 0.1f, 0.1f ) ); + ld->setSpecular( NLMISC::CRGBA::White ); + ld->setDiffuse( NLMISC::CRGBA::White ); + ld->setConstantAttenuation( 1.0f ); + ld->setLinearAttenuation( 0.0f ); + ld->setQuadraticAttenuation( 0.0f ); + driver->setLight( 0, *ld ); + driver->enableLight( 0, true ); + delete ld; + ld = NULL; + + scene->enableLightingSystem( false ); + + + mouseListener = driver->create3dMouseListener(); + mouseListener->setMouseMode( NL3D::U3dMouseListener::nelStyle ); + } + + void CNel3DInterface::killViewPort() + { + driver->deleteScene( scene ); + scene = NULL; + driver->delete3dMouseListener( mouseListener ); + mouseListener = NULL; + delete driver; + driver = NULL; + } + + void CNel3DInterface::resizeViewPort( unsigned long w, unsigned long h ) + { + if( scene != NULL ) + { + scene->getCam().setPerspective( 90.0f, w / (float)h, 0.1f, 1000.0f ); + } + } + + bool CNel3DInterface::addCube() + { + return loadShape( "primitives/cube.shape" ); + } + + bool CNel3DInterface::addSphere() + { + return loadShape( "primitives/sphere.shape" ); + } + + bool CNel3DInterface::addCylinder() + { + return loadShape( "primitives/cylinder.shape" ); + } + + bool CNel3DInterface::addTeaPot() + { + return loadShape( "primitives/teapot.shape" ); + } + + bool CNel3DInterface::loadShape( const std::string &fileName ) + { + NLMISC::CPath::addSearchPath( NLMISC::CFile::getPath( fileName ), false, false ); + NL3D::UInstance instance = scene->createInstance( fileName ); + if( instance.empty() ) + return false; + + clearScene(); + currentShape = instance; + + subMatId = 0; + + setupCamera(); + + return true; + } + + void CNel3DInterface::clearScene() + { + if( scene != NULL ) + { + if( currentShape.empty() ) + return; + scene->deleteInstance( currentShape ); + currentShape = NL3D::UInstance(); + subMatId = 0; + } + + if( driver == NULL ) + return; + + NLMISC::CRGBA c; + c.R = bgColor[ 0 ]; + c.G = bgColor[ 1 ]; + c.B = bgColor[ 2 ]; + c.A = bgColor[ 3 ]; + + driver->clearBuffers( c ); + driver->swapBuffers(); + } + + void CNel3DInterface::updateInput() + { + driver->EventServer.pump(); + } + + void CNel3DInterface::renderScene() + { + if( scene != NULL ) + { + scene->getCam().setTransformMode( NL3D::UTransformable::DirectMatrix ); + scene->getCam().setMatrix( mouseListener->getViewMatrix() ); + + NLMISC::CRGBA c; + c.R = bgColor[ 0 ]; + c.G = bgColor[ 1 ]; + c.B = bgColor[ 2 ]; + c.A = bgColor[ 3 ]; + + driver->clearBuffers( c ); + scene->render(); + driver->swapBuffers(); + } + } + + unsigned long CNel3DInterface::getShapeMatCount() const + { + if( currentShape.empty() ) + return 0; + + return currentShape.getNumMaterials(); + } + + void CNel3DInterface::getFogSettings( SFogSettings &s ) + { + s.enable = driver->fogEnabled(); + s.start = driver->getFogStart(); + s.end = driver->getFogEnd(); + + NLMISC::CRGBA c = driver->getFogColor(); + s.color[ 0 ] = c.R; + s.color[ 1 ] = c.G; + s.color[ 2 ] = c.B; + s.color[ 3 ] = c.A; + } + + void CNel3DInterface::setFogSettings( const SFogSettings &s ) + { + driver->enableFog( s.enable ); + NLMISC::CRGBA c; + c.R = s.color[ 0 ]; + c.G = s.color[ 1 ]; + c.B = s.color[ 2 ]; + c.A = 255; + driver->setupFog( s.start, s.end, c ); + setBGColor( c.R, c.G, c.B, c.A ); + } + + unsigned char CNel3DInterface::getMaxLights() const + { + return driver->getMaxDriverLights(); + } + + void CNel3DInterface::getLightInfo( unsigned char light, SLightInfo &info ) + { + info.enabled = driver->isLightEnabled( light ); + + NL3D::ULight *u = driver->getLight( light ); + info.ambColor[ 0 ] = u->getAmbiant().R / 255.0f; + info.ambColor[ 1 ] = u->getAmbiant().G / 255.0f; + info.ambColor[ 2 ] = u->getAmbiant().B / 255.0f; + info.ambColor[ 3 ] = u->getAmbiant().A / 255.0f; + + info.diffColor[ 0 ] = u->getDiffuse().R / 255.0f; + info.diffColor[ 1 ] = u->getDiffuse().G / 255.0f; + info.diffColor[ 2 ] = u->getDiffuse().B / 255.0f; + info.diffColor[ 3 ] = u->getDiffuse().A / 255.0f; + + info.specColor[ 0 ] = u->getSpecular().R / 255.0f; + info.specColor[ 1 ] = u->getSpecular().G / 255.0f; + info.specColor[ 2 ] = u->getSpecular().B / 255.0f; + info.specColor[ 3 ] = u->getSpecular().A / 255.0f; + + info.constAttn = u->getConstantAttenuation(); + info.linAttn = u->getLinearAttenuation(); + info.quadAttn = u->getQuadraticAttenuation(); + + switch( u->getMode() ) + { + case NL3D::ULight::DirectionalLight: + info.type = SLightInfo::Directional; + break; + + case NL3D::ULight::PointLight: + info.type = SLightInfo::Point; + break; + + case NL3D::ULight::SpotLight: + info.type = SLightInfo::Spot; + break; + } + + if( info.type == SLightInfo::Directional ) + { + info.posOrDir[ 0 ] = u->getDirection().x; + info.posOrDir[ 1 ] = u->getDirection().y; + info.posOrDir[ 2 ] = u->getDirection().z; + } + else + { + info.posOrDir[ 0 ] = u->getPosition().x; + info.posOrDir[ 1 ] = u->getPosition().y; + info.posOrDir[ 2 ] = u->getPosition().z; + } + + delete u; + } + + void CNel3DInterface::setLightInfo( unsigned char light, const SLightInfo &info ) + { + NL3D::ULight *u = NL3D::ULight::createLight(); + + NLMISC::CRGBA c; + c.R = info.ambColor[ 0 ] * 255.0f; + c.G = info.ambColor[ 1 ] * 255.0f; + c.B = info.ambColor[ 2 ] * 255.0f; + c.A = 255; + u->setAmbiant( c ); + + c.R = info.diffColor[ 0 ] * 255.0f; + c.G = info.diffColor[ 1 ] * 255.0f; + c.B = info.diffColor[ 2 ] * 255.0f; + u->setDiffuse( c ); + + c.R = info.specColor[ 0 ] * 255.0f; + c.G = info.specColor[ 1 ] * 255.0f; + c.B = info.specColor[ 2 ] * 255.0f; + u->setSpecular( c ); + + switch( info.type ) + { + case SLightInfo::Directional: + u->setMode( NL3D::ULight::DirectionalLight ); + break; + case SLightInfo::Point: + u->setMode( NL3D::ULight::PointLight ); + break; + case SLightInfo::Spot: + u->setMode( NL3D::ULight::SpotLight ); + break; + } + + if( info.type == SLightInfo::Directional ) + u->setDirection( NLMISC::CVector( info.posOrDir[ 0 ], info.posOrDir[ 1 ], info.posOrDir[ 2 ] ) ); + else + u->setPosition( NLMISC::CVector( info.posOrDir[ 0 ], info.posOrDir[ 1 ], info.posOrDir[ 2 ] ) ); + + u->setConstantAttenuation( info.constAttn ); + u->setLinearAttenuation( info.linAttn ); + u->setQuadraticAttenuation( info.quadAttn ); + + driver->setLight( light, *u ); + driver->enableLight( light, info.enabled ); + + delete u; + u = NULL; + } + + void CNel3DInterface::setupCamera() + { + NLMISC::CAABBox bbox; + currentShape.getShapeAABBox( bbox ); + + NLMISC::CVector center = bbox.getCenter(); + NLMISC::CQuat q( 0.0f, 0.0f, 0.0f, 0.0f ); + currentShape.getDefaultRotQuat( q ); + + NLMISC::CVector max_radius = bbox.getHalfSize(); + float radius = std::max( max_radius.x, std::max( max_radius.y, max_radius.z ) ); + float distance = radius / tan( 45.0f ); + + NLMISC::CVector axis = q.getAxis(); + if( axis.isNull() || ( axis == NLMISC::CVector::I ) ) + axis = NLMISC::CVector::J; + else + if( axis == -NLMISC::CVector::K ) + axis = -NLMISC::CVector::J; + + NLMISC::CVector eye = center - axis * ( radius + distance ); + scene->getCam().lookAt( eye, center ); + + mouseListener->setHotSpot( center ); + mouseListener->setMatrix( scene->getCam().getMatrix() ); + } + +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h index f76d29227..3f638cbda 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/nel3d_interface.h @@ -1,331 +1,331 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef NEL3D_INTERFACE_H -#define NEL3D_INTERFACE_H - -#include -#include - -namespace NL3D -{ - class CDynMaterial; - struct SRenderPass; - class CUsrShaderManager; - class UDriver; - class UScene; - class U3dMouseListener; -} - -namespace MaterialEditor -{ - /// Material Property, holds the user shader parameters as string ( for the editor ) - struct SMatProp - { - enum EType - { - Color, - Vector4, - Float, - Double, - Int, - Uint, - Matrix4, - Texture, - EType_count - }; - - /// Creates a string representation of the type id - static std::string typeIdToString( unsigned char type ); - - /// Turns the type id string back to Id - static unsigned char typeStringToId( const std::string &s ); - - std::string id; - std::string label; - unsigned char type; - std::string value; - - private: - static const char *idToString[]; - }; - - - /// Proxy class for the rendering pass - class CRenderPassProxy - { - public: - CRenderPassProxy( NL3D::SRenderPass *p ) - { - pass = p; - } - - ~CRenderPassProxy(){} - - /// Retrieves the rendering properties as a vector - void getProperties( std::vector< SMatProp > &v ); - - /// Clears the properties and then copies the ones from the vector specified - void setProperties( std::vector< SMatProp > &v ); - - /// Retrieves the name of the pass - void getName( std::string &name ); - - /// Sets the name of the pass - void setName( const std::string &name ); - - /// Returns the reference ( just a string ) to the user shader associated - void getShaderRef( std::string &s ); - - /// Sets the reference ( just a string ) to the user shader associated - void setShaderRef( const std::string &s ); - - /// Retrieves a single rendering property - bool getProperty( const std::string &name, SMatProp &p ); - - /// Changes a single rendering property - bool changeProperty( const SMatProp &p ); - - private: - NL3D::SRenderPass *pass; - }; - - /// Proxy class for the dynamic material - class CNelMaterialProxy - { - public: - CNelMaterialProxy( NL3D::CDynMaterial *mat ) - { - material = mat; - } - - ~CNelMaterialProxy(){} - - /// Retrieves the list of rendering passes - void getPassList( std::vector< std::string > &l ); - - /// Adds a new pass - void addPass( const char *name ); - - /// Removes the specified pass, if exists - void removePass( const char *name ); - - /// Moves the pass up by one position - void movePassUp( const char *name ); - - /// Moves the pass down by one position - void movePassDown( const char *name ); - - /// Renames the specified pass - void renamePass( const char *from, const char *to ); - - /// Retrieves the specified pass, by position - CRenderPassProxy getPass( unsigned long i ); - - /// Retrieves the specified pass, by name - CRenderPassProxy getPass( const char *name ); - - bool isEmpty() const{ - if( material == NULL ) - return true; - else - return false; - } - - private: - NL3D::CDynMaterial *material; - }; - - - struct SShaderInfo - { - std::string name; - std::string description; - std::string vp; - std::string fp; - }; - - struct SFogSettings - { - bool enable; - float start; - float end; - unsigned char color[ 4 ]; - - SFogSettings() - { - enable = false; - start = 0.0f; - end = 0.0f; - color[ 0 ] = 0.0f; - color[ 1 ] = 0.0f; - color[ 2 ] = 0.0f; - color[ 3 ] = 0.0f; - } - - }; - - struct SLightInfo - { - enum LightType - { - Directional, - Point, - Spot - }; - - bool enabled; - unsigned char type; - float posOrDir[ 3 ]; - float ambColor[ 3 ]; - float diffColor[ 3 ]; - float specColor[ 3 ]; - float constAttn; - float linAttn; - float quadAttn; - - SLightInfo() - { - enabled = true; - type = Directional; - posOrDir[ 0 ] = posOrDir[ 1 ] = posOrDir[ 2 ] = 0.0f; - ambColor[ 0 ] = ambColor[ 1 ] = ambColor[ 2 ] = 255; - diffColor[ 0 ] = diffColor[ 1 ] = diffColor[ 2 ] = 255; - specColor[ 0 ] = specColor[ 1 ] = specColor[ 2 ] = 255; - constAttn = 1.0f; - linAttn = quadAttn = 0.0f; - } - - }; - - /// Proxy class for Nel3D, so the material editor and Nel3D can interface - class CNel3DInterface - { - public: - CNel3DInterface(); - ~CNel3DInterface(); - - /// Load a material for the current (sub)object - bool loadMaterial( const char *fname ); - - /// Save the current (sub)object's material - bool saveMaterial( const char *fname ); - - /// Generate materials from the current (sub)object(s) "old" material(s) - void genMaterials(); - - /// Creates new material(s) for the current (sub)object(s) - void newMaterial(); - - /// Makes the specified sub-material current - bool selectSubMaterial( int id ); - - /// Returns a proxy object to the current sub-material - CNelMaterialProxy getMaterial(); - - /// Retrieves a list of user shaders loaded - void getShaderList( std::vector< std::string > &v ); - - /// Retrieves the specified user shader if exists - bool getShaderInfo( const std::string &name, SShaderInfo &info ); - - /// Updates a user shader - bool updateShaderInfo( const SShaderInfo &info ); - - /// Adds a new user shader - bool addShader( const SShaderInfo &info ); - - /// Removes a user shader - bool removeShader( const std::string &name ); - - /// Loads the user shaders - void loadShaders(); - - /// Saves the specified user shader - void saveShader( const std::string &name ); - - /// Deletes the specified user shader - void deleteShader( const std::string &name ); - - /// Sets up the viewport widget - void initViewPort( unsigned long wnd, unsigned long w, unsigned long h ); - - /// Shuts down the viewport widget - void killViewPort(); - - /// Resizes the viewport widget - void resizeViewPort( unsigned long w, unsigned long h ); - NL3D::UDriver* getDriver(){ return driver; } - - /// Clears the scene then adds a cube - bool addCube(); - - /// Clears the scene then adds a sphere - bool addSphere(); - - /// Clears the scene then add a cylinder - bool addCylinder(); - - /// Clears the scene the adds a teapot - bool addTeaPot(); - - - /// Clears the scene then loads a shape - bool loadShape( const std::string &fileName ); - - /// Clears the scene, as the name suggests - void clearScene(); - - /// Sends the input events to Nel3D - void updateInput(); - - /// Renders the scene - void renderScene(); - - unsigned long getShapeMatCount() const; - - void getFogSettings( SFogSettings &s ); - void setFogSettings( const SFogSettings &s ); - - unsigned char getMaxLights() const; - void getLightInfo( unsigned char light, SLightInfo &info ); - void setLightInfo( unsigned char light, const SLightInfo &info ); - - void setBGColor( unsigned char R, unsigned char G, unsigned char B, unsigned char A ){ - bgColor[ 0 ] = R; - bgColor[ 1 ] = G; - bgColor[ 2 ] = B; - bgColor[ 3 ] = A; - } - - private: - void setupCamera(); - - unsigned long subMatId; - - NL3D::CUsrShaderManager *shaderManager; - NL3D::UDriver *driver; - NL3D::UScene *scene; - NL3D::U3dMouseListener *mouseListener; - unsigned char bgColor[ 4 ]; - }; -} - - -#endif - - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef NEL3D_INTERFACE_H +#define NEL3D_INTERFACE_H + +#include +#include + +namespace NL3D +{ + class CDynMaterial; + struct SRenderPass; + class CUsrShaderManager; + class UDriver; + class UScene; + class U3dMouseListener; +} + +namespace MaterialEditor +{ + /// Material Property, holds the user shader parameters as string ( for the editor ) + struct SMatProp + { + enum EType + { + Color, + Vector4, + Float, + Double, + Int, + Uint, + Matrix4, + Texture, + EType_count + }; + + /// Creates a string representation of the type id + static std::string typeIdToString( unsigned char type ); + + /// Turns the type id string back to Id + static unsigned char typeStringToId( const std::string &s ); + + std::string id; + std::string label; + unsigned char type; + std::string value; + + private: + static const char *idToString[]; + }; + + + /// Proxy class for the rendering pass + class CRenderPassProxy + { + public: + CRenderPassProxy( NL3D::SRenderPass *p ) + { + pass = p; + } + + ~CRenderPassProxy(){} + + /// Retrieves the rendering properties as a vector + void getProperties( std::vector< SMatProp > &v ); + + /// Clears the properties and then copies the ones from the vector specified + void setProperties( std::vector< SMatProp > &v ); + + /// Retrieves the name of the pass + void getName( std::string &name ); + + /// Sets the name of the pass + void setName( const std::string &name ); + + /// Returns the reference ( just a string ) to the user shader associated + void getShaderRef( std::string &s ); + + /// Sets the reference ( just a string ) to the user shader associated + void setShaderRef( const std::string &s ); + + /// Retrieves a single rendering property + bool getProperty( const std::string &name, SMatProp &p ); + + /// Changes a single rendering property + bool changeProperty( const SMatProp &p ); + + private: + NL3D::SRenderPass *pass; + }; + + /// Proxy class for the dynamic material + class CNelMaterialProxy + { + public: + CNelMaterialProxy( NL3D::CDynMaterial *mat ) + { + material = mat; + } + + ~CNelMaterialProxy(){} + + /// Retrieves the list of rendering passes + void getPassList( std::vector< std::string > &l ); + + /// Adds a new pass + void addPass( const char *name ); + + /// Removes the specified pass, if exists + void removePass( const char *name ); + + /// Moves the pass up by one position + void movePassUp( const char *name ); + + /// Moves the pass down by one position + void movePassDown( const char *name ); + + /// Renames the specified pass + void renamePass( const char *from, const char *to ); + + /// Retrieves the specified pass, by position + CRenderPassProxy getPass( unsigned long i ); + + /// Retrieves the specified pass, by name + CRenderPassProxy getPass( const char *name ); + + bool isEmpty() const{ + if( material == NULL ) + return true; + else + return false; + } + + private: + NL3D::CDynMaterial *material; + }; + + + struct SShaderInfo + { + std::string name; + std::string description; + std::string vp; + std::string fp; + }; + + struct SFogSettings + { + bool enable; + float start; + float end; + unsigned char color[ 4 ]; + + SFogSettings() + { + enable = false; + start = 0.0f; + end = 0.0f; + color[ 0 ] = 0.0f; + color[ 1 ] = 0.0f; + color[ 2 ] = 0.0f; + color[ 3 ] = 0.0f; + } + + }; + + struct SLightInfo + { + enum LightType + { + Directional, + Point, + Spot + }; + + bool enabled; + unsigned char type; + float posOrDir[ 3 ]; + float ambColor[ 3 ]; + float diffColor[ 3 ]; + float specColor[ 3 ]; + float constAttn; + float linAttn; + float quadAttn; + + SLightInfo() + { + enabled = true; + type = Directional; + posOrDir[ 0 ] = posOrDir[ 1 ] = posOrDir[ 2 ] = 0.0f; + ambColor[ 0 ] = ambColor[ 1 ] = ambColor[ 2 ] = 255; + diffColor[ 0 ] = diffColor[ 1 ] = diffColor[ 2 ] = 255; + specColor[ 0 ] = specColor[ 1 ] = specColor[ 2 ] = 255; + constAttn = 1.0f; + linAttn = quadAttn = 0.0f; + } + + }; + + /// Proxy class for Nel3D, so the material editor and Nel3D can interface + class CNel3DInterface + { + public: + CNel3DInterface(); + ~CNel3DInterface(); + + /// Load a material for the current (sub)object + bool loadMaterial( const char *fname ); + + /// Save the current (sub)object's material + bool saveMaterial( const char *fname ); + + /// Generate materials from the current (sub)object(s) "old" material(s) + void genMaterials(); + + /// Creates new material(s) for the current (sub)object(s) + void newMaterial(); + + /// Makes the specified sub-material current + bool selectSubMaterial( int id ); + + /// Returns a proxy object to the current sub-material + CNelMaterialProxy getMaterial(); + + /// Retrieves a list of user shaders loaded + void getShaderList( std::vector< std::string > &v ); + + /// Retrieves the specified user shader if exists + bool getShaderInfo( const std::string &name, SShaderInfo &info ); + + /// Updates a user shader + bool updateShaderInfo( const SShaderInfo &info ); + + /// Adds a new user shader + bool addShader( const SShaderInfo &info ); + + /// Removes a user shader + bool removeShader( const std::string &name ); + + /// Loads the user shaders + void loadShaders(); + + /// Saves the specified user shader + void saveShader( const std::string &name ); + + /// Deletes the specified user shader + void deleteShader( const std::string &name ); + + /// Sets up the viewport widget + void initViewPort( unsigned long wnd, unsigned long w, unsigned long h ); + + /// Shuts down the viewport widget + void killViewPort(); + + /// Resizes the viewport widget + void resizeViewPort( unsigned long w, unsigned long h ); + NL3D::UDriver* getDriver(){ return driver; } + + /// Clears the scene then adds a cube + bool addCube(); + + /// Clears the scene then adds a sphere + bool addSphere(); + + /// Clears the scene then add a cylinder + bool addCylinder(); + + /// Clears the scene the adds a teapot + bool addTeaPot(); + + + /// Clears the scene then loads a shape + bool loadShape( const std::string &fileName ); + + /// Clears the scene, as the name suggests + void clearScene(); + + /// Sends the input events to Nel3D + void updateInput(); + + /// Renders the scene + void renderScene(); + + unsigned long getShapeMatCount() const; + + void getFogSettings( SFogSettings &s ); + void setFogSettings( const SFogSettings &s ); + + unsigned char getMaxLights() const; + void getLightInfo( unsigned char light, SLightInfo &info ); + void setLightInfo( unsigned char light, const SLightInfo &info ); + + void setBGColor( unsigned char R, unsigned char G, unsigned char B, unsigned char A ){ + bgColor[ 0 ] = R; + bgColor[ 1 ] = G; + bgColor[ 2 ] = B; + bgColor[ 3 ] = A; + } + + private: + void setupCamera(); + + unsigned long subMatId; + + NL3D::CUsrShaderManager *shaderManager; + NL3D::UDriver *driver; + NL3D::UScene *scene; + NL3D::U3dMouseListener *mouseListener; + unsigned char bgColor[ 4 ]; + }; +} + + +#endif + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.cpp index fbb61e255..acc7011df 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.cpp @@ -1,394 +1,394 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "prop_browser_ctrl.h" -#include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h" -#include "3rdparty/qtpropertybrowser/qtvariantproperty.h" -#include "nel3d_interface.h" - -#include - -#include - -namespace MaterialEditor -{ - bool QStringToQMatrix4x4( const QString &s, QMatrix4x4 &m ) - { - QString ms = s; - bool ok = false; - bool success = true; - double da[ 16 ]; - - QStringList sl = ms.split( " " ); - QStringListIterator it( sl ); - int i = 0; - - while( it.hasNext() ) - { - double d = it.next().toDouble( &ok ); - - if( ok ) - { - da[ i ] = d; - } - else - { - da[ i ] = 0.0; - success = false; - } - - i++; - } - - m = QMatrix4x4( da ); - - return success; - } - - - int propToQVariant( unsigned char t ) - { - int type = 0; - - switch( t ) - { - case SMatProp::Color: - type = QVariant::Color; - break; - - case SMatProp::Double: - type = QVariant::Double; - break; - - case SMatProp::Float: - type = QVariant::Double; - break; - - case SMatProp::Int: - type = QVariant::Int; - break; - - case SMatProp::Matrix4: - type = QVariant::String; - break; - - case SMatProp::Texture: - type = QVariant::String; - break; - - case SMatProp::Uint: - type = QVariant::Int; - break; - - case SMatProp::Vector4: - type = QVariant::String; - break; - - default: - type = QVariant::String; - break; - } - - return type; - } - - void propValToQVariant( const SMatProp &p, QVariant &v ) - { - bool ok = false; - QString s; - - switch( p.type ) - { - case SMatProp::Color: - { - std::stringstream ss = p.value; - float c[ 4 ]; - std::fill( c, c + 4, 0.0f ); - - for( int i = 0; i < 4; i++ ) - { - ss >> c[ i ]; - if( !ss.good() ) - break; - } - - QColor color; - color.setRedF( c[ 0 ] / 255.0f ); - color.setGreenF( c[ 1 ] / 255.0f ); - color.setBlueF( c[ 2 ] / 255.0f ); - color.setAlphaF( c[ 3 ] / 255.0f ); - - v = color; - - break; - } - - case SMatProp::Double: - double d; - - s = p.value.c_str(); - d = s.toDouble( &ok ); - if( ok ) - v = d; - else - v = 0.0; - - break; - - case SMatProp::Float: - float f; - - s = p.value.c_str(); - f = s.toFloat( &ok ); - if( ok ) - v = f; - else - v = 0.0f; - - break; - - case SMatProp::Int: - int i; - - s = p.value.c_str(); - i = s.toInt( &ok ); - if( ok ) - v = i; - else - v = 0; - - break; - - case SMatProp::Matrix4: - { - /* - QMatrix4x4 m; - m.fill( 0.0 ); - QStringToQMatrix4x4( p.value.c_str(), m ); - v = QVariant( m ); - */ - v = p.value.c_str(); - } - break; - - case SMatProp::Texture: - v = p.value.c_str(); - break; - - case SMatProp::Uint: - unsigned int u; - - s = p.value.c_str(); - u = s.toUInt( &ok ); - if( ok ) - v = u; - else - v = 0u; - - break; - - case SMatProp::Vector4: - v = p.value.c_str(); - break; - - default: - v = ""; - break; - } - } - - - - - - - - CPropBrowserCtrl::CPropBrowserCtrl( QObject *parent ) : - QObject( parent ) - { - browser = NULL; - nel3dIface = NULL; - manager = new QtVariantPropertyManager(); - factory = new QtVariantEditorFactory(); - - setupConnections(); - } - - CPropBrowserCtrl::~CPropBrowserCtrl() - { - browser = NULL; - nel3dIface = NULL; - delete manager; - manager = NULL; - delete factory; - factory = NULL; - } - - void CPropBrowserCtrl::setBrowser( QtTreePropertyBrowser *b ) - { - browser = b; - browser->setFactoryForManager( manager, factory ); - } - - void CPropBrowserCtrl::setNel3DIface( CNel3DInterface *iface ) - { - nel3dIface = iface; - } - - void CPropBrowserCtrl::setupConnections() - { - connect( manager, SIGNAL( valueChanged( QtProperty*, const QVariant& ) ), - this, SLOT( onValueChanged( QtProperty*, const QVariant& ) ) ); - } - - void CPropBrowserCtrl::onSceneCleared() - { - clearProps(); - } - - void CPropBrowserCtrl::onPropsChanged() - { - clearProps(); - loadPropsForPass( currentPass ); - } - - void CPropBrowserCtrl::clearProps() - { - browser->clear(); - propToId.clear(); - } - - void CPropBrowserCtrl::loadPropsForPass( const QString &pass ) - { - currentPass = pass; - clearProps(); - - if( pass.isEmpty() ) - return; - - CNelMaterialProxy m = nel3dIface->getMaterial(); - if( m.isEmpty() ) - return; - - CRenderPassProxy p = m.getPass( pass.toUtf8().data() ); - - std::vector< SMatProp > v; - p.getProperties( v ); - - QtVariantProperty *vp = NULL; - int type = 0; - QVariant qv; - - std::vector< SMatProp >::const_iterator itr = v.begin(); - while( itr != v.end() ) - { - const SMatProp &prop = *itr; - - type = propToQVariant( prop.type ); - - vp = manager->addProperty( type, prop.label.c_str() ); - - if( vp != NULL ) - { - propValToQVariant( prop, qv ); - vp->setValue( qv ); - browser->addProperty( vp ); - propToId[ vp ] = prop.id; - } - ++itr; - } - } - - void CPropBrowserCtrl::loadPropsForPass( int i ) - { - clearProps(); - - CNelMaterialProxy m = nel3dIface->getMaterial(); - if( m.isEmpty() ) - return; - - CRenderPassProxy p = m.getPass( i ); - - std::string n; - p.getName( n ); - currentPass = n.c_str(); - - std::vector< SMatProp > v; - p.getProperties( v ); - - QtVariantProperty *vp = NULL; - int type = 0; - QVariant qv; - - std::vector< SMatProp >::const_iterator itr = v.begin(); - while( itr != v.end() ) - { - const SMatProp &prop = *itr; - - type = propToQVariant( prop.type ); - - vp = manager->addProperty( type, prop.label.c_str() ); - - if( vp != NULL ) - { - propValToQVariant( prop, qv ); - vp->setValue( qv ); - browser->addProperty( vp ); - propToId[ vp ] = prop.id; - } - ++itr; - } - } - - void CPropBrowserCtrl::onValueChanged( QtProperty *p, const QVariant &v ) - { - QString label = p->propertyName(); - std::string value = p->valueText().toUtf8().data(); - - if( v.type() == QVariant::Color ) - { - QColor c = v.value< QColor >(); - value.clear(); - QString val = "%1 %2 %3 %4"; - val = val.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() ); - value = val.toUtf8().data(); - } - - CNelMaterialProxy m = nel3dIface->getMaterial(); - if( m.isEmpty() ) - return; - - CRenderPassProxy pass = m.getPass( currentPass.toUtf8().data() ); - - std::map< QtProperty*, std::string >::const_iterator itr - = propToId.find( p ); - if( itr == propToId.end() ) - return; - - SMatProp prop; - bool ok = pass.getProperty( itr->second, prop ); - if( !ok ) - return; - - prop.value = value; - pass.changeProperty( prop ); - - - } - -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "prop_browser_ctrl.h" +#include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h" +#include "3rdparty/qtpropertybrowser/qtvariantproperty.h" +#include "nel3d_interface.h" + +#include + +#include + +namespace MaterialEditor +{ + bool QStringToQMatrix4x4( const QString &s, QMatrix4x4 &m ) + { + QString ms = s; + bool ok = false; + bool success = true; + double da[ 16 ]; + + QStringList sl = ms.split( " " ); + QStringListIterator it( sl ); + int i = 0; + + while( it.hasNext() ) + { + double d = it.next().toDouble( &ok ); + + if( ok ) + { + da[ i ] = d; + } + else + { + da[ i ] = 0.0; + success = false; + } + + i++; + } + + m = QMatrix4x4( da ); + + return success; + } + + + int propToQVariant( unsigned char t ) + { + int type = 0; + + switch( t ) + { + case SMatProp::Color: + type = QVariant::Color; + break; + + case SMatProp::Double: + type = QVariant::Double; + break; + + case SMatProp::Float: + type = QVariant::Double; + break; + + case SMatProp::Int: + type = QVariant::Int; + break; + + case SMatProp::Matrix4: + type = QVariant::String; + break; + + case SMatProp::Texture: + type = QVariant::String; + break; + + case SMatProp::Uint: + type = QVariant::Int; + break; + + case SMatProp::Vector4: + type = QVariant::String; + break; + + default: + type = QVariant::String; + break; + } + + return type; + } + + void propValToQVariant( const SMatProp &p, QVariant &v ) + { + bool ok = false; + QString s; + + switch( p.type ) + { + case SMatProp::Color: + { + std::stringstream ss = p.value; + float c[ 4 ]; + std::fill( c, c + 4, 0.0f ); + + for( int i = 0; i < 4; i++ ) + { + ss >> c[ i ]; + if( !ss.good() ) + break; + } + + QColor color; + color.setRedF( c[ 0 ] / 255.0f ); + color.setGreenF( c[ 1 ] / 255.0f ); + color.setBlueF( c[ 2 ] / 255.0f ); + color.setAlphaF( c[ 3 ] / 255.0f ); + + v = color; + + break; + } + + case SMatProp::Double: + double d; + + s = p.value.c_str(); + d = s.toDouble( &ok ); + if( ok ) + v = d; + else + v = 0.0; + + break; + + case SMatProp::Float: + float f; + + s = p.value.c_str(); + f = s.toFloat( &ok ); + if( ok ) + v = f; + else + v = 0.0f; + + break; + + case SMatProp::Int: + int i; + + s = p.value.c_str(); + i = s.toInt( &ok ); + if( ok ) + v = i; + else + v = 0; + + break; + + case SMatProp::Matrix4: + { + /* + QMatrix4x4 m; + m.fill( 0.0 ); + QStringToQMatrix4x4( p.value.c_str(), m ); + v = QVariant( m ); + */ + v = p.value.c_str(); + } + break; + + case SMatProp::Texture: + v = p.value.c_str(); + break; + + case SMatProp::Uint: + unsigned int u; + + s = p.value.c_str(); + u = s.toUInt( &ok ); + if( ok ) + v = u; + else + v = 0u; + + break; + + case SMatProp::Vector4: + v = p.value.c_str(); + break; + + default: + v = ""; + break; + } + } + + + + + + + + CPropBrowserCtrl::CPropBrowserCtrl( QObject *parent ) : + QObject( parent ) + { + browser = NULL; + nel3dIface = NULL; + manager = new QtVariantPropertyManager(); + factory = new QtVariantEditorFactory(); + + setupConnections(); + } + + CPropBrowserCtrl::~CPropBrowserCtrl() + { + browser = NULL; + nel3dIface = NULL; + delete manager; + manager = NULL; + delete factory; + factory = NULL; + } + + void CPropBrowserCtrl::setBrowser( QtTreePropertyBrowser *b ) + { + browser = b; + browser->setFactoryForManager( manager, factory ); + } + + void CPropBrowserCtrl::setNel3DIface( CNel3DInterface *iface ) + { + nel3dIface = iface; + } + + void CPropBrowserCtrl::setupConnections() + { + connect( manager, SIGNAL( valueChanged( QtProperty*, const QVariant& ) ), + this, SLOT( onValueChanged( QtProperty*, const QVariant& ) ) ); + } + + void CPropBrowserCtrl::onSceneCleared() + { + clearProps(); + } + + void CPropBrowserCtrl::onPropsChanged() + { + clearProps(); + loadPropsForPass( currentPass ); + } + + void CPropBrowserCtrl::clearProps() + { + browser->clear(); + propToId.clear(); + } + + void CPropBrowserCtrl::loadPropsForPass( const QString &pass ) + { + currentPass = pass; + clearProps(); + + if( pass.isEmpty() ) + return; + + CNelMaterialProxy m = nel3dIface->getMaterial(); + if( m.isEmpty() ) + return; + + CRenderPassProxy p = m.getPass( pass.toUtf8().data() ); + + std::vector< SMatProp > v; + p.getProperties( v ); + + QtVariantProperty *vp = NULL; + int type = 0; + QVariant qv; + + std::vector< SMatProp >::const_iterator itr = v.begin(); + while( itr != v.end() ) + { + const SMatProp &prop = *itr; + + type = propToQVariant( prop.type ); + + vp = manager->addProperty( type, prop.label.c_str() ); + + if( vp != NULL ) + { + propValToQVariant( prop, qv ); + vp->setValue( qv ); + browser->addProperty( vp ); + propToId[ vp ] = prop.id; + } + ++itr; + } + } + + void CPropBrowserCtrl::loadPropsForPass( int i ) + { + clearProps(); + + CNelMaterialProxy m = nel3dIface->getMaterial(); + if( m.isEmpty() ) + return; + + CRenderPassProxy p = m.getPass( i ); + + std::string n; + p.getName( n ); + currentPass = n.c_str(); + + std::vector< SMatProp > v; + p.getProperties( v ); + + QtVariantProperty *vp = NULL; + int type = 0; + QVariant qv; + + std::vector< SMatProp >::const_iterator itr = v.begin(); + while( itr != v.end() ) + { + const SMatProp &prop = *itr; + + type = propToQVariant( prop.type ); + + vp = manager->addProperty( type, prop.label.c_str() ); + + if( vp != NULL ) + { + propValToQVariant( prop, qv ); + vp->setValue( qv ); + browser->addProperty( vp ); + propToId[ vp ] = prop.id; + } + ++itr; + } + } + + void CPropBrowserCtrl::onValueChanged( QtProperty *p, const QVariant &v ) + { + QString label = p->propertyName(); + std::string value = p->valueText().toUtf8().data(); + + if( v.type() == QVariant::Color ) + { + QColor c = v.value< QColor >(); + value.clear(); + QString val = "%1 %2 %3 %4"; + val = val.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() ); + value = val.toUtf8().data(); + } + + CNelMaterialProxy m = nel3dIface->getMaterial(); + if( m.isEmpty() ) + return; + + CRenderPassProxy pass = m.getPass( currentPass.toUtf8().data() ); + + std::map< QtProperty*, std::string >::const_iterator itr + = propToId.find( p ); + if( itr == propToId.end() ) + return; + + SMatProp prop; + bool ok = pass.getProperty( itr->second, prop ); + if( !ok ) + return; + + prop.value = value; + pass.changeProperty( prop ); + + + } + +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.h index b298a6079..87f70a67d 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/prop_browser_ctrl.h @@ -1,64 +1,64 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef PROP_BROWSER_CTRL_H -#define PROP_BROWSER_CTRL_H - -#include -#include -#include - -class QtTreePropertyBrowser; -class QtVariantPropertyManager; -class QtVariantEditorFactory; -class QtProperty; - -namespace MaterialEditor -{ - class CNel3DInterface; - - class CPropBrowserCtrl : public QObject - { - Q_OBJECT - public: - CPropBrowserCtrl( QObject *parent = NULL ); - ~CPropBrowserCtrl(); - void setBrowser( QtTreePropertyBrowser *b ); - void setNel3DIface( CNel3DInterface *iface ); - void setupConnections(); - void onSceneCleared(); - void onPropsChanged(); - void clearProps(); - void loadPropsForPass( const QString &pass ); - void loadPropsForPass( int i ); - - private Q_SLOTS: - void onValueChanged( QtProperty *p, const QVariant &v ); - - private: - QtTreePropertyBrowser *browser; - QtVariantPropertyManager *manager; - QtVariantEditorFactory *factory; - - CNel3DInterface *nel3dIface; - QString currentPass; - - std::map< QtProperty*, std::string > propToId; - }; -} - -#endif +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef PROP_BROWSER_CTRL_H +#define PROP_BROWSER_CTRL_H + +#include +#include +#include + +class QtTreePropertyBrowser; +class QtVariantPropertyManager; +class QtVariantEditorFactory; +class QtProperty; + +namespace MaterialEditor +{ + class CNel3DInterface; + + class CPropBrowserCtrl : public QObject + { + Q_OBJECT + public: + CPropBrowserCtrl( QObject *parent = NULL ); + ~CPropBrowserCtrl(); + void setBrowser( QtTreePropertyBrowser *b ); + void setNel3DIface( CNel3DInterface *iface ); + void setupConnections(); + void onSceneCleared(); + void onPropsChanged(); + void clearProps(); + void loadPropsForPass( const QString &pass ); + void loadPropsForPass( int i ); + + private Q_SLOTS: + void onValueChanged( QtProperty *p, const QVariant &v ); + + private: + QtTreePropertyBrowser *browser; + QtVariantPropertyManager *manager; + QtVariantEditorFactory *factory; + + CNel3DInterface *nel3dIface; + QString currentPass; + + std::map< QtProperty*, std::string > propToId; + }; +} + +#endif diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.cpp index 0c19e1b29..189066745 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.cpp @@ -1,257 +1,257 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "render_passes.h" -#include "nel3d_interface.h" -#include "material_observer.h" -#include -#include - -namespace MaterialEditor -{ - RenderPassesWidget::RenderPassesWidget( QWidget *parent ) : - QWidget( parent ) - { - setupUi( this ); - setupConnections(); - nl3dIface = NULL; - observer = NULL; - } - - RenderPassesWidget::~RenderPassesWidget() - { - } - - void RenderPassesWidget::fillList( const QStringList &list ) - { - listWidget->clear(); - - QStringListIterator itr( list ); - while( itr.hasNext() ) - { - listWidget->addItem( itr.next() ); - } - } - - void RenderPassesWidget::getList( QStringList &list ) - { - for( int i = 0; i < listWidget->count(); i++ ) - { - list.push_back( listWidget->item( i )->text() ); - } - } - - void RenderPassesWidget::clear() - { - listWidget->clear(); - } - - void RenderPassesWidget::onMaterialLoaded() - { - clear(); - CNelMaterialProxy m = nl3dIface->getMaterial(); - if( m.isEmpty() ) - return; - - std::vector< std::string > pl; - m.getPassList( pl ); - - std::vector< std::string >::const_iterator itr = pl.begin(); - while( itr != pl.end() ) - { - listWidget->addItem( QString( itr->c_str() ) ); - ++itr; - } - } - - void RenderPassesWidget::setupConnections() - { - connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); - connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); - connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); - connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); - connect( upButton, SIGNAL( clicked( bool ) ), this, SLOT( onUpClicked() ) ); - connect( downButton, SIGNAL( clicked( bool ) ), this, SLOT( onDownClicked() ) ); - } - - bool RenderPassesWidget::passExists( const QString &label ) - { - int c = listWidget->count(); - - for( int i = 0; i < c; i++ ) - { - if( label == listWidget->item( i )->text() ) - return true; - } - - return false; - } - - void RenderPassesWidget::onOKClicked() - { - close(); - } - - void RenderPassesWidget::onAddClicked() - { - QString label = - QInputDialog::getText( - NULL, - tr( "Pass label" ), - tr( "Please enter the new pass' label" ) - ); - - if( label.isEmpty() ) - return; - - if( passExists( label ) ) - { - QMessageBox::warning( - NULL, - tr( "Pass label" ), - tr( "Pass label already exists!" ) - ); - - return; - } - - listWidget->addItem( label ); - - CNelMaterialProxy material = nl3dIface->getMaterial(); - material.addPass( label.toUtf8().data() ); - - if( observer != NULL ) - observer->onPassAdded( label.toUtf8().data() ); - } - - void RenderPassesWidget::onRemoveClicked() - { - int row = listWidget->currentRow(); - if( row == -1 ) - return; - QString pass; - - QListWidgetItem *item = listWidget->takeItem( row ); - pass = item->text(); - delete item; - - CNelMaterialProxy material = nl3dIface->getMaterial(); - if( material.isEmpty() ) - return; - - material.removePass( pass.toUtf8().data() ); - - if( observer != NULL ) - observer->onPassRemoved( pass.toUtf8().data() ); - } - - void RenderPassesWidget::onEditClicked() - { - QListWidgetItem *item = listWidget->currentItem(); - if( item == NULL ) - return; - - QString from = item->text(); - QString to = - QInputDialog::getText( - NULL, - tr( "Editing pass label" ), - tr( "Please enter the new label" ), - QLineEdit::Normal, - from - ); - - if( to.isEmpty() ) - return; - - if( from == to ) - return; - - if( passExists( to ) ) - { - QMessageBox::warning( - NULL, - tr( "Pass label" ), - tr( "Pass label already exists!" ) - ); - return; - } - - item->setText( to ); - - CNelMaterialProxy material = nl3dIface->getMaterial(); - if( material.isEmpty() ) - return; - - material.renamePass( from.toUtf8().data(), to.toUtf8().data() ); - - if( observer != NULL ) - observer->onPassRenamed( from.toUtf8().data(), to.toUtf8().data() ); - } - - void RenderPassesWidget::onUpClicked() - { - QListWidgetItem *item = listWidget->currentItem(); - if( item == NULL ) - return; - - int row = listWidget->currentRow(); - if( row == 0 ) - return; - - item = listWidget->takeItem( row ); - listWidget->insertItem( row - 1, item ); - listWidget->setCurrentRow( row - 1 ); - - QString s = item->text(); - - CNelMaterialProxy material = nl3dIface->getMaterial(); - if( material.isEmpty() ) - return; - - material.movePassUp( s.toUtf8().data() ); - - if( observer != NULL ) - observer->onPassMovedUp( s.toUtf8().data() ); - } - - void RenderPassesWidget::onDownClicked() - { - QListWidgetItem *item = listWidget->currentItem(); - if( item == NULL ) - return; - - int row = listWidget->currentRow(); - if( row == ( listWidget->count() - 1 ) ) - return; - - item = listWidget->takeItem( row ); - listWidget->insertItem( row + 1, item ); - listWidget->setCurrentRow( row + 1 ); - - QString s = item->text(); - - CNelMaterialProxy material = nl3dIface->getMaterial(); - if( material.isEmpty() ) - return; - - material.movePassDown( s.toUtf8().data() ); - - if( observer != NULL ) - observer->onPassMovedDown( s.toUtf8().data() ); - } -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "render_passes.h" +#include "nel3d_interface.h" +#include "material_observer.h" +#include +#include + +namespace MaterialEditor +{ + RenderPassesWidget::RenderPassesWidget( QWidget *parent ) : + QWidget( parent ) + { + setupUi( this ); + setupConnections(); + nl3dIface = NULL; + observer = NULL; + } + + RenderPassesWidget::~RenderPassesWidget() + { + } + + void RenderPassesWidget::fillList( const QStringList &list ) + { + listWidget->clear(); + + QStringListIterator itr( list ); + while( itr.hasNext() ) + { + listWidget->addItem( itr.next() ); + } + } + + void RenderPassesWidget::getList( QStringList &list ) + { + for( int i = 0; i < listWidget->count(); i++ ) + { + list.push_back( listWidget->item( i )->text() ); + } + } + + void RenderPassesWidget::clear() + { + listWidget->clear(); + } + + void RenderPassesWidget::onMaterialLoaded() + { + clear(); + CNelMaterialProxy m = nl3dIface->getMaterial(); + if( m.isEmpty() ) + return; + + std::vector< std::string > pl; + m.getPassList( pl ); + + std::vector< std::string >::const_iterator itr = pl.begin(); + while( itr != pl.end() ) + { + listWidget->addItem( QString( itr->c_str() ) ); + ++itr; + } + } + + void RenderPassesWidget::setupConnections() + { + connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); + connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); + connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); + connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); + connect( upButton, SIGNAL( clicked( bool ) ), this, SLOT( onUpClicked() ) ); + connect( downButton, SIGNAL( clicked( bool ) ), this, SLOT( onDownClicked() ) ); + } + + bool RenderPassesWidget::passExists( const QString &label ) + { + int c = listWidget->count(); + + for( int i = 0; i < c; i++ ) + { + if( label == listWidget->item( i )->text() ) + return true; + } + + return false; + } + + void RenderPassesWidget::onOKClicked() + { + close(); + } + + void RenderPassesWidget::onAddClicked() + { + QString label = + QInputDialog::getText( + NULL, + tr( "Pass label" ), + tr( "Please enter the new pass' label" ) + ); + + if( label.isEmpty() ) + return; + + if( passExists( label ) ) + { + QMessageBox::warning( + NULL, + tr( "Pass label" ), + tr( "Pass label already exists!" ) + ); + + return; + } + + listWidget->addItem( label ); + + CNelMaterialProxy material = nl3dIface->getMaterial(); + material.addPass( label.toUtf8().data() ); + + if( observer != NULL ) + observer->onPassAdded( label.toUtf8().data() ); + } + + void RenderPassesWidget::onRemoveClicked() + { + int row = listWidget->currentRow(); + if( row == -1 ) + return; + QString pass; + + QListWidgetItem *item = listWidget->takeItem( row ); + pass = item->text(); + delete item; + + CNelMaterialProxy material = nl3dIface->getMaterial(); + if( material.isEmpty() ) + return; + + material.removePass( pass.toUtf8().data() ); + + if( observer != NULL ) + observer->onPassRemoved( pass.toUtf8().data() ); + } + + void RenderPassesWidget::onEditClicked() + { + QListWidgetItem *item = listWidget->currentItem(); + if( item == NULL ) + return; + + QString from = item->text(); + QString to = + QInputDialog::getText( + NULL, + tr( "Editing pass label" ), + tr( "Please enter the new label" ), + QLineEdit::Normal, + from + ); + + if( to.isEmpty() ) + return; + + if( from == to ) + return; + + if( passExists( to ) ) + { + QMessageBox::warning( + NULL, + tr( "Pass label" ), + tr( "Pass label already exists!" ) + ); + return; + } + + item->setText( to ); + + CNelMaterialProxy material = nl3dIface->getMaterial(); + if( material.isEmpty() ) + return; + + material.renamePass( from.toUtf8().data(), to.toUtf8().data() ); + + if( observer != NULL ) + observer->onPassRenamed( from.toUtf8().data(), to.toUtf8().data() ); + } + + void RenderPassesWidget::onUpClicked() + { + QListWidgetItem *item = listWidget->currentItem(); + if( item == NULL ) + return; + + int row = listWidget->currentRow(); + if( row == 0 ) + return; + + item = listWidget->takeItem( row ); + listWidget->insertItem( row - 1, item ); + listWidget->setCurrentRow( row - 1 ); + + QString s = item->text(); + + CNelMaterialProxy material = nl3dIface->getMaterial(); + if( material.isEmpty() ) + return; + + material.movePassUp( s.toUtf8().data() ); + + if( observer != NULL ) + observer->onPassMovedUp( s.toUtf8().data() ); + } + + void RenderPassesWidget::onDownClicked() + { + QListWidgetItem *item = listWidget->currentItem(); + if( item == NULL ) + return; + + int row = listWidget->currentRow(); + if( row == ( listWidget->count() - 1 ) ) + return; + + item = listWidget->takeItem( row ); + listWidget->insertItem( row + 1, item ); + listWidget->setCurrentRow( row + 1 ); + + QString s = item->text(); + + CNelMaterialProxy material = nl3dIface->getMaterial(); + if( material.isEmpty() ) + return; + + material.movePassDown( s.toUtf8().data() ); + + if( observer != NULL ) + observer->onPassMovedDown( s.toUtf8().data() ); + } +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.h index e4d4666f3..1fe891363 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/render_passes.h @@ -1,59 +1,59 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#ifndef RENDER_PASSES_H -#define RENDER_PASSES_H - -#include "ui_render_passes.h" -#include - -namespace MaterialEditor -{ - class CNel3DInterface; - class CMaterialObserver; - - class RenderPassesWidget : public QWidget, public Ui::RenderPassesWidget - { - Q_OBJECT - public: - RenderPassesWidget( QWidget *parent = NULL ); - ~RenderPassesWidget(); - void fillList( const QStringList &list ); - void getList( QStringList &list ); - void clear(); - void onMaterialLoaded(); - void setNel3dIface( CNel3DInterface *iface ){ nl3dIface = iface; } - void setMaterialObserver( CMaterialObserver *obs ){ observer = obs; } - - private: - void setupConnections(); - bool passExists( const QString &label ); - - CNel3DInterface *nl3dIface; - CMaterialObserver *observer; - - private Q_SLOTS: - void onOKClicked(); - void onAddClicked(); - void onRemoveClicked(); - void onEditClicked(); - void onUpClicked(); - void onDownClicked(); - }; -} - -#endif - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef RENDER_PASSES_H +#define RENDER_PASSES_H + +#include "ui_render_passes.h" +#include + +namespace MaterialEditor +{ + class CNel3DInterface; + class CMaterialObserver; + + class RenderPassesWidget : public QWidget, public Ui::RenderPassesWidget + { + Q_OBJECT + public: + RenderPassesWidget( QWidget *parent = NULL ); + ~RenderPassesWidget(); + void fillList( const QStringList &list ); + void getList( QStringList &list ); + void clear(); + void onMaterialLoaded(); + void setNel3dIface( CNel3DInterface *iface ){ nl3dIface = iface; } + void setMaterialObserver( CMaterialObserver *obs ){ observer = obs; } + + private: + void setupConnections(); + bool passExists( const QString &label ); + + CNel3DInterface *nl3dIface; + CMaterialObserver *observer; + + private Q_SLOTS: + void onOKClicked(); + void onAddClicked(); + void onRemoveClicked(); + void onEditClicked(); + void onUpClicked(); + void onDownClicked(); + }; +} + +#endif + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.cpp index fdee63fbd..cc4cdb69c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.cpp @@ -1,90 +1,90 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "shader_editor.h" -#include "nel3d_interface.h" - -namespace MaterialEditor -{ - ShaderEditorWidget::ShaderEditorWidget( QDialog *parent ) : - QDialog( parent ) - { - setupUi( this ); - nl3dIface = NULL; - setupConnections(); - } - - ShaderEditorWidget::~ShaderEditorWidget() - { - } - - void ShaderEditorWidget::getDescription( QString &desc ) - { - desc = descriptionEdit->toPlainText(); - } - - void ShaderEditorWidget::setupConnections() - { - connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ) ; - connect( cancelButton, SIGNAL( clicked( bool ) ), this, SLOT( onCancelClicked() ) ); - } - - void ShaderEditorWidget::onOKClicked() - { - SShaderInfo info; - info.name = nameEdit->text().toUtf8().data(); - info.description = descriptionEdit->toPlainText().toUtf8().data(); - info.vp = vsEdit->toPlainText().toUtf8().data(); - info.fp = fsEdit->toPlainText().toUtf8().data(); - - bool ok = nl3dIface->updateShaderInfo( info ); - if( ok ) - nl3dIface->saveShader( info.name ); - - accept(); - } - - void ShaderEditorWidget::onCancelClicked() - { - reject(); - } - - void ShaderEditorWidget::reset() - { - QString empty; - nameEdit->setText( empty ); - descriptionEdit->setPlainText( empty ); - vsEdit->setPlainText( empty ); - fsEdit->setPlainText( empty ); - setResult( QDialog::Rejected ); - } - - bool ShaderEditorWidget::load( const QString &name ) - { - SShaderInfo info; - if( !nl3dIface->getShaderInfo( name.toUtf8().data(), info ) ) - return false; - - nameEdit->setText( info.name.c_str() ); - descriptionEdit->setPlainText( info.description.c_str() ); - vsEdit->setPlainText( info.vp.c_str() ); - fsEdit->setPlainText( info.fp.c_str() ); - return true; - } -} - - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "shader_editor.h" +#include "nel3d_interface.h" + +namespace MaterialEditor +{ + ShaderEditorWidget::ShaderEditorWidget( QDialog *parent ) : + QDialog( parent ) + { + setupUi( this ); + nl3dIface = NULL; + setupConnections(); + } + + ShaderEditorWidget::~ShaderEditorWidget() + { + } + + void ShaderEditorWidget::getDescription( QString &desc ) + { + desc = descriptionEdit->toPlainText(); + } + + void ShaderEditorWidget::setupConnections() + { + connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ) ; + connect( cancelButton, SIGNAL( clicked( bool ) ), this, SLOT( onCancelClicked() ) ); + } + + void ShaderEditorWidget::onOKClicked() + { + SShaderInfo info; + info.name = nameEdit->text().toUtf8().data(); + info.description = descriptionEdit->toPlainText().toUtf8().data(); + info.vp = vsEdit->toPlainText().toUtf8().data(); + info.fp = fsEdit->toPlainText().toUtf8().data(); + + bool ok = nl3dIface->updateShaderInfo( info ); + if( ok ) + nl3dIface->saveShader( info.name ); + + accept(); + } + + void ShaderEditorWidget::onCancelClicked() + { + reject(); + } + + void ShaderEditorWidget::reset() + { + QString empty; + nameEdit->setText( empty ); + descriptionEdit->setPlainText( empty ); + vsEdit->setPlainText( empty ); + fsEdit->setPlainText( empty ); + setResult( QDialog::Rejected ); + } + + bool ShaderEditorWidget::load( const QString &name ) + { + SShaderInfo info; + if( !nl3dIface->getShaderInfo( name.toUtf8().data(), info ) ) + return false; + + nameEdit->setText( info.name.c_str() ); + descriptionEdit->setPlainText( info.description.c_str() ); + vsEdit->setPlainText( info.vp.c_str() ); + fsEdit->setPlainText( info.fp.c_str() ); + return true; + } +} + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.h index 7017c9212..7daa8bb55 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_editor.h @@ -1,52 +1,52 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef SHADER_EDITOR_H -#define SHADER_EDITOR_H - -#include "ui_shader_editor.h" - -namespace MaterialEditor -{ - class CNel3DInterface; - - class ShaderEditorWidget : public QDialog, public Ui::ShaderEditorWidget - { - Q_OBJECT - public: - ShaderEditorWidget( QDialog *parent = NULL ); - ~ShaderEditorWidget(); - - void getDescription( QString &desc ); - - void setNel3DInterface( CNel3DInterface *iface ){ nl3dIface = iface; } - - void reset(); - bool load( const QString &name ); - - private Q_SLOTS: - void onOKClicked(); - void onCancelClicked(); - - private: - void setupConnections(); - CNel3DInterface *nl3dIface; - }; -} - -#endif - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef SHADER_EDITOR_H +#define SHADER_EDITOR_H + +#include "ui_shader_editor.h" + +namespace MaterialEditor +{ + class CNel3DInterface; + + class ShaderEditorWidget : public QDialog, public Ui::ShaderEditorWidget + { + Q_OBJECT + public: + ShaderEditorWidget( QDialog *parent = NULL ); + ~ShaderEditorWidget(); + + void getDescription( QString &desc ); + + void setNel3DInterface( CNel3DInterface *iface ){ nl3dIface = iface; } + + void reset(); + bool load( const QString &name ); + + private Q_SLOTS: + void onOKClicked(); + void onCancelClicked(); + + private: + void setupConnections(); + CNel3DInterface *nl3dIface; + }; +} + +#endif + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.cpp index f019c38e0..5dde3ae70 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.cpp @@ -1,220 +1,220 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -#include "shader_widget.h" -#include "shader_editor.h" -#include "nel3d_interface.h" -#include -#include -#include - -namespace MaterialEditor -{ - ShaderWidget::ShaderWidget( QWidget *parent ) : - QWidget( parent ) - { - setupUi( this ); - setupConnections(); - - shaderEditorWidget = new ShaderEditorWidget(); - nl3dIface = NULL; - } - - ShaderWidget::~ShaderWidget() - { - delete shaderEditorWidget; - shaderEditorWidget = NULL; - } - - void ShaderWidget::setNel3DInterface( CNel3DInterface *iface ) - { - nl3dIface = iface; - shaderEditorWidget->setNel3DInterface( iface ); - } - - void ShaderWidget::load() - { - std::vector< std::string > v; - - nl3dIface->getShaderList( v ); - - shaderList->clear(); - - QString name; - - std::vector< std::string >::const_iterator itr = v.begin(); - while( itr != v.end() ) - { - name = itr->c_str(); - shaderList->addItem( name ); - Q_EMIT shaderAdded( name ); - - ++itr; - } - } - - void ShaderWidget::setupConnections() - { - connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); - - connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); - connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); - connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); - - connect( shaderList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onRowChanged( int ) ) ); - } - - void ShaderWidget::onOKClicked() - { - close(); - } - - bool ShaderWidget::nameExists( const QString &name ) - { - for( int i = 0; i < shaderList->count(); i++ ) - { - if( shaderList->item( i )->text() == name ) - return true; - } - - return false; - } - - void ShaderWidget::nameExistsMessage() - { - QMessageBox::critical( - this, - tr( "Shader already exists" ), - tr( "A shader with that name already exists!" ), - QMessageBox::Ok - ); - } - - void ShaderWidget::onAddClicked() - { - QString name = - QInputDialog::getText( - this, - tr( "New shader" ), - tr( "Enter the new shader's name" ) - ); - - if( name.isEmpty() ) - { - QMessageBox::critical( - this, - tr( "New shader" ), - tr( "You must enter a name for the new shader" ), - QMessageBox::Ok - ); - return; - } - - if( nameExists( name ) ) - { - nameExistsMessage(); - return; - } - - SShaderInfo info; - info.name = name.toUtf8().data(); - bool ok = nl3dIface->addShader( info ); - nl3dIface->saveShader( info.name ); - if( !ok ) - { - QMessageBox::critical( - this, - tr( "Error adding shader" ), - tr( "There was an error while trying to add the shader" ) - ); - - return; - } - - shaderList->addItem( name ); - - Q_EMIT shaderAdded( name ); - } - - void ShaderWidget::onRemoveClicked() - { - int i = shaderList->currentRow(); - if( i < 0 ) - return; - - int selection = - QMessageBox::question( - this, - tr( "Removing shader" ), - tr( "Are you sure you want to remove this shader?" ), - QMessageBox::Yes, - QMessageBox::Cancel - ); - - if( selection == QMessageBox::Yes ) - { - QListWidgetItem *item = shaderList->takeItem( i ); - QString name = item->text(); - std::string n = name.toUtf8().data(); - delete item; - - nl3dIface->removeShader( n ); - nl3dIface->deleteShader( n ); - - if( shaderList->count() == 0 ) - description->setPlainText( "" ); - - Q_EMIT shaderRemoved( name ); - } - - } - - void ShaderWidget::onEditClicked() - { - int i = shaderList->currentRow(); - if( i < 0 ) - return; - - QString name = shaderList->item( i )->text(); - - shaderEditorWidget->reset(); - shaderEditorWidget->load( name ); - - int res = shaderEditorWidget->exec(); - if( res == QDialog::Rejected ) - return; - - QString descr; - shaderEditorWidget->getDescription( descr ); - description->setPlainText( descr ); - } - - 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() ); - } -} - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#include "shader_widget.h" +#include "shader_editor.h" +#include "nel3d_interface.h" +#include +#include +#include + +namespace MaterialEditor +{ + ShaderWidget::ShaderWidget( QWidget *parent ) : + QWidget( parent ) + { + setupUi( this ); + setupConnections(); + + shaderEditorWidget = new ShaderEditorWidget(); + nl3dIface = NULL; + } + + ShaderWidget::~ShaderWidget() + { + delete shaderEditorWidget; + shaderEditorWidget = NULL; + } + + void ShaderWidget::setNel3DInterface( CNel3DInterface *iface ) + { + nl3dIface = iface; + shaderEditorWidget->setNel3DInterface( iface ); + } + + void ShaderWidget::load() + { + std::vector< std::string > v; + + nl3dIface->getShaderList( v ); + + shaderList->clear(); + + QString name; + + std::vector< std::string >::const_iterator itr = v.begin(); + while( itr != v.end() ) + { + name = itr->c_str(); + shaderList->addItem( name ); + Q_EMIT shaderAdded( name ); + + ++itr; + } + } + + void ShaderWidget::setupConnections() + { + connect( okButton, SIGNAL( clicked( bool ) ), this, SLOT( onOKClicked() ) ); + + connect( addButton, SIGNAL( clicked( bool ) ), this, SLOT( onAddClicked() ) ); + connect( removeButton, SIGNAL( clicked( bool ) ), this, SLOT( onRemoveClicked() ) ); + connect( editButton, SIGNAL( clicked( bool ) ), this, SLOT( onEditClicked() ) ); + + connect( shaderList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onRowChanged( int ) ) ); + } + + void ShaderWidget::onOKClicked() + { + close(); + } + + bool ShaderWidget::nameExists( const QString &name ) + { + for( int i = 0; i < shaderList->count(); i++ ) + { + if( shaderList->item( i )->text() == name ) + return true; + } + + return false; + } + + void ShaderWidget::nameExistsMessage() + { + QMessageBox::critical( + this, + tr( "Shader already exists" ), + tr( "A shader with that name already exists!" ), + QMessageBox::Ok + ); + } + + void ShaderWidget::onAddClicked() + { + QString name = + QInputDialog::getText( + this, + tr( "New shader" ), + tr( "Enter the new shader's name" ) + ); + + if( name.isEmpty() ) + { + QMessageBox::critical( + this, + tr( "New shader" ), + tr( "You must enter a name for the new shader" ), + QMessageBox::Ok + ); + return; + } + + if( nameExists( name ) ) + { + nameExistsMessage(); + return; + } + + SShaderInfo info; + info.name = name.toUtf8().data(); + bool ok = nl3dIface->addShader( info ); + nl3dIface->saveShader( info.name ); + if( !ok ) + { + QMessageBox::critical( + this, + tr( "Error adding shader" ), + tr( "There was an error while trying to add the shader" ) + ); + + return; + } + + shaderList->addItem( name ); + + Q_EMIT shaderAdded( name ); + } + + void ShaderWidget::onRemoveClicked() + { + int i = shaderList->currentRow(); + if( i < 0 ) + return; + + int selection = + QMessageBox::question( + this, + tr( "Removing shader" ), + tr( "Are you sure you want to remove this shader?" ), + QMessageBox::Yes, + QMessageBox::Cancel + ); + + if( selection == QMessageBox::Yes ) + { + QListWidgetItem *item = shaderList->takeItem( i ); + QString name = item->text(); + std::string n = name.toUtf8().data(); + delete item; + + nl3dIface->removeShader( n ); + nl3dIface->deleteShader( n ); + + if( shaderList->count() == 0 ) + description->setPlainText( "" ); + + Q_EMIT shaderRemoved( name ); + } + + } + + void ShaderWidget::onEditClicked() + { + int i = shaderList->currentRow(); + if( i < 0 ) + return; + + QString name = shaderList->item( i )->text(); + + shaderEditorWidget->reset(); + shaderEditorWidget->load( name ); + + int res = shaderEditorWidget->exec(); + if( res == QDialog::Rejected ) + return; + + QString descr; + shaderEditorWidget->getDescription( descr ); + description->setPlainText( descr ); + } + + 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() ); + } +} + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.h index d5941d253..236051d6c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/shader_widget.h @@ -1,61 +1,61 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef SHADER_WIDGET_H -#define SHADER_WIDGET_H - -#include "ui_shader_widget.h" - -namespace MaterialEditor -{ - class ShaderEditorWidget; - class CNel3DInterface; - - class ShaderWidget : public QWidget, public Ui::ShaderWidget - { - Q_OBJECT - - public: - ShaderWidget( QWidget *parent = NULL ); - ~ShaderWidget(); - - void setNel3DInterface( CNel3DInterface *iface ); - void load(); - - Q_SIGNALS: - void shaderAdded( const QString &name ); - void shaderRemoved( const QString &name ); - - private: - void setupConnections(); - bool nameExists( const QString &name ); - void nameExistsMessage(); - - ShaderEditorWidget *shaderEditorWidget; - CNel3DInterface *nl3dIface; - - private Q_SLOTS: - void onOKClicked(); - void onAddClicked(); - void onRemoveClicked(); - void onEditClicked(); - void onRowChanged( int i ); - }; -} - -#endif - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef SHADER_WIDGET_H +#define SHADER_WIDGET_H + +#include "ui_shader_widget.h" + +namespace MaterialEditor +{ + class ShaderEditorWidget; + class CNel3DInterface; + + class ShaderWidget : public QWidget, public Ui::ShaderWidget + { + Q_OBJECT + + public: + ShaderWidget( QWidget *parent = NULL ); + ~ShaderWidget(); + + void setNel3DInterface( CNel3DInterface *iface ); + void load(); + + Q_SIGNALS: + void shaderAdded( const QString &name ); + void shaderRemoved( const QString &name ); + + private: + void setupConnections(); + bool nameExists( const QString &name ); + void nameExistsMessage(); + + ShaderEditorWidget *shaderEditorWidget; + CNel3DInterface *nl3dIface; + + private Q_SLOTS: + void onOKClicked(); + void onAddClicked(); + void onRemoveClicked(); + void onEditClicked(); + void onRowChanged( int i ); + }; +} + +#endif + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp index e4d1f790a..97be13699 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.cpp @@ -1,154 +1,154 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#include "viewport_widget.h" -#include -#include "nel3d_interface.h" -#include "nel/3d/driver.h" -#include "nel/3d/driver_user.h" - -#ifdef NL_OS_WINDOWS -#include -#endif - - -namespace MaterialEditor -{ - ViewPortWidget::ViewPortWidget( QWidget *parent ) : - QWidget( parent ) - { - nl3dIface = NULL; - timerId = 0; - setAttribute( Qt::WA_PaintOnScreen ); - } - - ViewPortWidget::~ViewPortWidget() - { - } - - void ViewPortWidget::init() - { - nl3dIface->initViewPort( (unsigned long)winId(), width(), height() ); - - } - - void ViewPortWidget::resizeEvent( QResizeEvent *evnt ) - { - uint32 w = evnt->size().width(); - uint32 h = evnt->size().height(); - - nl3dIface->resizeViewPort( w, h ); - - QWidget::resizeEvent( evnt ); - } - - void ViewPortWidget::startTimedUpdates( int interval ) - { - if( interval == 0 ) - return; - - timerId = startTimer( interval ); - } - - void ViewPortWidget::stopTimedUpdates() - { - killTimer( timerId ); - timerId = 0; - } - - void ViewPortWidget::timerEvent( QTimerEvent *evnt ) - { - int id = evnt->timerId(); - if( id == timerId ) - update(); - } - -#if defined( NL_OS_WINDOWS ) - - typedef bool ( *winProc )( NL3D::IDriver *driver, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ); - - bool ViewPortWidget::winEvent( MSG *message, long *result ) - { - NL3D::UDriver *udriver = nl3dIface->getDriver(); - if( ( udriver != NULL ) && udriver->isActive() ) - { - NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); - if( driver != NULL ) - { - winProc proc = (winProc)driver->getWindowProc(); - return proc( driver, message->hwnd, message->message, message->wParam, message->lParam ); - } - } - - return false; - } - -#elif defined( NL_OS_MAC ) - - typedef bool ( *cocoaProc )( NL3D::IDriver *, const void *e ); - - bool ViewPortWidget::macEvent( EventHandlerCallRef caller, EventRef event ) - { - if( caller ) - nlerror("You are using QtCarbon! Only QtCocoa supported, please upgrade Qt"); - - NL3D::UDriver *udriver = nl3dIface->getDriver(); - if( ( udriver != NULL ) && udriver->isActive() ) - { - NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); - if( driver != NULL ) - { - cocoaProc proc = ( cocoaProc )driver->getWindowProc(); - proc( driver, event ); - } - } - - return false; - } - -#elif defined( NL_OS_UNIX ) - - typedef bool ( *x11Proc )( NL3D::IDriver *drv, XEvent *e ); - - bool ViewPortWidget::x11Event( XEvent *event ) - { - NL3D::UDriver *udriver = nl3dIface->getDriver(); - - if( ( udriver != NULL ) && udriver->isActive() ) - { - NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); - if( driver != NULL ) - { - x11Proc proc = ( x11Proc )driver->getWindowProc(); - proc( driver, event ); - } - - } - return false; - } - -#endif - - void ViewPortWidget::update() - { - nl3dIface->updateInput(); - nl3dIface->renderScene(); - } - -} - - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "viewport_widget.h" +#include +#include "nel3d_interface.h" +#include "nel/3d/driver.h" +#include "nel/3d/driver_user.h" + +#ifdef NL_OS_WINDOWS +#include +#endif + + +namespace MaterialEditor +{ + ViewPortWidget::ViewPortWidget( QWidget *parent ) : + QWidget( parent ) + { + nl3dIface = NULL; + timerId = 0; + setAttribute( Qt::WA_PaintOnScreen ); + } + + ViewPortWidget::~ViewPortWidget() + { + } + + void ViewPortWidget::init() + { + nl3dIface->initViewPort( (unsigned long)winId(), width(), height() ); + + } + + void ViewPortWidget::resizeEvent( QResizeEvent *evnt ) + { + uint32 w = evnt->size().width(); + uint32 h = evnt->size().height(); + + nl3dIface->resizeViewPort( w, h ); + + QWidget::resizeEvent( evnt ); + } + + void ViewPortWidget::startTimedUpdates( int interval ) + { + if( interval == 0 ) + return; + + timerId = startTimer( interval ); + } + + void ViewPortWidget::stopTimedUpdates() + { + killTimer( timerId ); + timerId = 0; + } + + void ViewPortWidget::timerEvent( QTimerEvent *evnt ) + { + int id = evnt->timerId(); + if( id == timerId ) + update(); + } + +#if defined( NL_OS_WINDOWS ) + + typedef bool ( *winProc )( NL3D::IDriver *driver, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ); + + bool ViewPortWidget::winEvent( MSG *message, long *result ) + { + NL3D::UDriver *udriver = nl3dIface->getDriver(); + if( ( udriver != NULL ) && udriver->isActive() ) + { + NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); + if( driver != NULL ) + { + winProc proc = (winProc)driver->getWindowProc(); + return proc( driver, message->hwnd, message->message, message->wParam, message->lParam ); + } + } + + return false; + } + +#elif defined( NL_OS_MAC ) + + typedef bool ( *cocoaProc )( NL3D::IDriver *, const void *e ); + + bool ViewPortWidget::macEvent( EventHandlerCallRef caller, EventRef event ) + { + if( caller ) + nlerror("You are using QtCarbon! Only QtCocoa supported, please upgrade Qt"); + + NL3D::UDriver *udriver = nl3dIface->getDriver(); + if( ( udriver != NULL ) && udriver->isActive() ) + { + NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); + if( driver != NULL ) + { + cocoaProc proc = ( cocoaProc )driver->getWindowProc(); + proc( driver, event ); + } + } + + return false; + } + +#elif defined( NL_OS_UNIX ) + + typedef bool ( *x11Proc )( NL3D::IDriver *drv, XEvent *e ); + + bool ViewPortWidget::x11Event( XEvent *event ) + { + NL3D::UDriver *udriver = nl3dIface->getDriver(); + + if( ( udriver != NULL ) && udriver->isActive() ) + { + NL3D::IDriver *driver = dynamic_cast< NL3D::CDriverUser* >( udriver )->getDriver(); + if( driver != NULL ) + { + x11Proc proc = ( x11Proc )driver->getWindowProc(); + proc( driver, event ); + } + + } + return false; + } + +#endif + + void ViewPortWidget::update() + { + nl3dIface->updateInput(); + nl3dIface->renderScene(); + } + +} + + diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h index 081cad53a..6a2578a9f 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/material_editor/viewport_widget.h @@ -1,68 +1,68 @@ -// Object Viewer Qt Material Editor plugin -// Copyright (C) 2010 Winch Gate Property Limited -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - - -#ifndef VIEWPORT_WIDGET_H -#define VIEWPORT_WIDGET_H - -#include -#include "nel/misc/types_nl.h" -#include "nel/misc/event_emitter.h" - -namespace MaterialEditor -{ - class CNel3DInterface; - - class ViewPortWidget : public QWidget - { - Q_OBJECT - public: - ViewPortWidget( QWidget *parent = NULL ); - ~ViewPortWidget(); - void setNel3DInterface( CNel3DInterface *iface ){ nl3dIface = iface; } - void init(); - - void resizeEvent( QResizeEvent *evnt ); - - QPaintEngine* paintEngine() const{ return NULL; } - - void startTimedUpdates( int interval ); - void stopTimedUpdates(); - - protected: - - void timerEvent( QTimerEvent *evnt ); - -#if defined ( NL_OS_WINDOWS ) - bool winEvent( MSG *message, long *result ); -#elif defined( NL_OS_MAC ) - bool macEvent( EventHandlerCallRef caller, EventRef event ) ; -#elif defined( NL_OS_UNIX ) - bool x11Event( XEvent *event ); -#endif - - private: - void update(); - - CNel3DInterface *nl3dIface; - int timerId; - }; -} - - -#endif - - +// Object Viewer Qt Material Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef VIEWPORT_WIDGET_H +#define VIEWPORT_WIDGET_H + +#include +#include "nel/misc/types_nl.h" +#include "nel/misc/event_emitter.h" + +namespace MaterialEditor +{ + class CNel3DInterface; + + class ViewPortWidget : public QWidget + { + Q_OBJECT + public: + ViewPortWidget( QWidget *parent = NULL ); + ~ViewPortWidget(); + void setNel3DInterface( CNel3DInterface *iface ){ nl3dIface = iface; } + void init(); + + void resizeEvent( QResizeEvent *evnt ); + + QPaintEngine* paintEngine() const{ return NULL; } + + void startTimedUpdates( int interval ); + void stopTimedUpdates(); + + protected: + + void timerEvent( QTimerEvent *evnt ); + +#if defined ( NL_OS_WINDOWS ) + bool winEvent( MSG *message, long *result ); +#elif defined( NL_OS_MAC ) + bool macEvent( EventHandlerCallRef caller, EventRef event ) ; +#elif defined( NL_OS_UNIX ) + bool x11Event( XEvent *event ); +#endif + + private: + void update(); + + CNel3DInterface *nl3dIface; + int timerId; + }; +} + + +#endif + +