Revision: 8534 http://playerstage.svn.sourceforge.net/playerstage/?rev=8534&view=rev Author: natepak Date: 2010-01-28 17:56:15 +0000 (Thu, 28 Jan 2010)
Log Message: ----------- Added ability to turn on and off per-pixel lighting. Added a function to calculate normals for a mesh Modified Paths: -------------- code/gazebo/trunk/cmake/SearchForStuff.cmake code/gazebo/trunk/server/Mesh.cc code/gazebo/trunk/server/Mesh.hh code/gazebo/trunk/server/MeshManager.cc code/gazebo/trunk/server/Vector3.cc code/gazebo/trunk/server/Vector3.hh code/gazebo/trunk/server/World.cc code/gazebo/trunk/server/World.hh code/gazebo/trunk/server/gui/MainMenu.cc code/gazebo/trunk/server/gui/MainMenu.hh code/gazebo/trunk/server/rendering/OgreVisual.cc code/gazebo/trunk/server/rendering/RTShaderSystem.cc code/gazebo/trunk/server/rendering/RTShaderSystem.hh code/gazebo/trunk/worlds/pioneer2dx.world Modified: code/gazebo/trunk/cmake/SearchForStuff.cmake =================================================================== --- code/gazebo/trunk/cmake/SearchForStuff.cmake 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/cmake/SearchForStuff.cmake 2010-01-28 17:56:15 UTC (rev 8534) @@ -59,7 +59,7 @@ ${OGRE_LDFLAGS}) # Try to find the OGRE RTShaderSystem library - find_library(ogre_rtshader_lib OgreRTShaderSystem ENV LD_LIBRARY_PATH) + find_library(ogre_rtshader_lib OgreRTShaderSystem ${OGRE_LIBRARY_DIRS} ENV LD_LIBRARY_PATH) if (ogre_rtshader_lib) APPEND_TO_CACHED_LIST(gazeboserver_link_libs ${gazeboserver_link_libs_desc} Modified: code/gazebo/trunk/server/Mesh.cc =================================================================== --- code/gazebo/trunk/server/Mesh.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/Mesh.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -261,7 +261,17 @@ } +//////////////////////////////////////////////////////////////////////////////// +/// Recalculate all the normals. +void Mesh::RecalculateNormals() +{ + std::vector<SubMesh*>::iterator iter; + for (iter = this->submeshes.begin(); iter != this->submeshes.begin(); iter++) + (*iter)->RecalculateNormals(); +} + + //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // SUBMESH @@ -550,3 +560,30 @@ iiter != this->indices.end(); iiter++) (*indArr)[i++] = (*iiter); } + +//////////////////////////////////////////////////////////////////////////////// +/// Recalculate all the normals. +void SubMesh::RecalculateNormals() +{ + unsigned int i; + + // Reset all the normals + for (i=0; i < this->normals.size(); i++) + this->normals[i].Set(0,0,0); + + // For each face, which is defined by three indices, calculate the normals + for (i=0; i < this->indices.size(); i+=3) + { + Vector3 v1 = this->vertices[this->indices[i]]; + Vector3 v2 = this->vertices[this->indices[i+1]]; + Vector3 v3 = this->vertices[this->indices[i+2]]; + Vector3 n = Vector3::GetNormal(v1, v2, v3); + this->normals[this->indices[i]] += n; + this->normals[this->indices[i+1]] += n; + this->normals[this->indices[i+2]] += n; + } + + // Normalize the results + for (i=0; i < this->normals.size(); i++) + this->normals[i].Normalize(); +} Modified: code/gazebo/trunk/server/Mesh.hh =================================================================== --- code/gazebo/trunk/server/Mesh.hh 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/Mesh.hh 2010-01-28 17:56:15 UTC (rev 8534) @@ -64,6 +64,9 @@ /// \brief Put all the data into flat arrays public: void FillArrays(float **vertArr, unsigned int **indArr) const; + /// \brief Recalculate all the normals. + public: void RecalculateNormals(); + private: std::string name; private: std::vector<SubMesh *> submeshes; private: std::vector<Material *> materials; @@ -152,6 +155,9 @@ /// \brief Put all the data into flat arrays public: void FillArrays(float **vertArr, unsigned int **indArr) const; + /// \brief Recalculate all the normals. + public: void RecalculateNormals(); + private: std::vector< Vector3 > vertices; private: std::vector< Vector3 > normals; private: std::vector< Vector2<double> > texCoords; Modified: code/gazebo/trunk/server/MeshManager.cc =================================================================== --- code/gazebo/trunk/server/MeshManager.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/MeshManager.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -225,6 +225,8 @@ } } } + + mesh->RecalculateNormals(); } @@ -323,6 +325,8 @@ // Set the indices for (i=0;i<36; i++) subMesh->AddIndex(ind[i]); + + subMesh->RecalculateNormals(); } //////////////////////////////////////////////////////////////////////////////// @@ -419,6 +423,8 @@ // Set the indices for (i=0;i<36; i++) subMesh->AddIndex(ind[i]); + + mesh->RecalculateNormals(); } //////////////////////////////////////////////////////////////////////////////// @@ -522,6 +528,8 @@ for (j=0; j<3; j++) subMesh->SetNormal(subMesh->GetIndex(i+j), norm ); } + + mesh->RecalculateNormals(); } //////////////////////////////////////////////////////////////////////////////// @@ -632,6 +640,8 @@ for (j=0; j<3; j++) subMesh->SetNormal(subMesh->GetIndex(i+j), norm ); } + + mesh->RecalculateNormals(); } //////////////////////////////////////////////////////////////////////////////// @@ -755,5 +765,7 @@ verticeIndex++; } } + + mesh->RecalculateNormals(); } Modified: code/gazebo/trunk/server/Vector3.cc =================================================================== --- code/gazebo/trunk/server/Vector3.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/Vector3.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -130,6 +130,17 @@ return perp; } +//////////////////////////////////////////////////////////////////////////////// +/// Get a normal vector to a triangle +Vector3 Vector3::GetNormal(const Vector3 &v1, const Vector3 &v2, + const Vector3 &v3) +{ + Vector3 a = v2 - v1; + Vector3 b = v3 - v1; + Vector3 n = a.GetCrossProd(b); + n.Normalize(); + return n; +} //////////////////////////////////////////////////////////////////////////////// // Equals operator Modified: code/gazebo/trunk/server/Vector3.hh =================================================================== --- code/gazebo/trunk/server/Vector3.hh 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/Vector3.hh 2010-01-28 17:56:15 UTC (rev 8534) @@ -72,6 +72,10 @@ /// \brief Return a vector that is perpendicular to this one. public: Vector3 GetPerpendicular() const; + /// \brief Get a normal vector to a triangle + public: static Vector3 GetNormal(const Vector3 &v1, const Vector3 &v2, + const Vector3 &v3); + /// \brief Equal operator public: const Vector3 &operator=( const Vector3 &pt ); Modified: code/gazebo/trunk/server/World.cc =================================================================== --- code/gazebo/trunk/server/World.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/World.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -64,6 +64,7 @@ this->showCameras = false; this->wireframe = false; this->showPhysics = false; + this->perPixelLighting = true; this->physicsEngine = NULL; this->server = NULL; this->graphics = NULL; @@ -770,7 +771,6 @@ return this->showPhysics; } - //////////////////////////////////////////////////////////////////////////////// /// Set whether to show the joints void World::SetShowPhysics(bool show) @@ -779,8 +779,22 @@ this->showPhysicsSignal(this->showPhysics); } +//////////////////////////////////////////////////////////////////////////////// +/// Set to use perpixel lighting or pervertex lighting +void World::SetPerPixelLighting( bool pp ) +{ + this->perPixelLighting = pp; + this->perPixelLightingSignal(pp); +} //////////////////////////////////////////////////////////////////////////////// +/// Get to use perpixel lighting or pervertex lighting +bool World::GetPerPixelLighting() +{ + return this->perPixelLighting; +} + +//////////////////////////////////////////////////////////////////////////////// // Update the simulation interface void World::UpdateSimulationIface() { Modified: code/gazebo/trunk/server/World.hh =================================================================== --- code/gazebo/trunk/server/World.hh 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/World.hh 2010-01-28 17:56:15 UTC (rev 8534) @@ -218,6 +218,12 @@ /// \brief Get whether to view as wireframe public: bool GetShowPhysics(); + /// \brief Set to use perpixel lighting or pervertex lighting + public: void SetPerPixelLighting( bool pp ); + + /// \brief Get use perpixel lighting or pervertex lighting + public: bool GetPerPixelLighting(); + /// \brief Goto a position in time public: void GotoTime(double pos); @@ -246,7 +252,9 @@ private: bool wireframe; + private: bool perPixelLighting; + /// \brief Load a model /// \param node Pointer to the XMLConfig node /// \param parent The parent model @@ -318,6 +326,12 @@ showBoundingBoxesSignal.connect(subscriber); } + /// \brief Connect a boost::slot the use per pixel lighting signal + public: template<typename T> + void ConnectPerPixelLightingSignal( T subscriber ) + { + perPixelLightingSignal.connect(subscriber); + } /// \brief Get the names of interfaces defined in the tree of a model private: void GetInterfaceNames(Entity* m, std::vector<std::string>& list); @@ -374,6 +388,7 @@ private: boost::signal<void (bool)> showPhysicsSignal; private: boost::signal<void (bool)> showJointsSignal; private: boost::signal<void (bool)> showBoundingBoxesSignal; + private: boost::signal<void (bool)> perPixelLightingSignal; private: std::deque<WorldState> worldStates; private: std::deque<WorldState>::iterator worldStatesInsertIter; Modified: code/gazebo/trunk/server/gui/MainMenu.cc =================================================================== --- code/gazebo/trunk/server/gui/MainMenu.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/gui/MainMenu.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -65,6 +65,7 @@ { "Show Contacts", 0, &gazebo::MainMenu::ShowContactsCB,0, FL_MENU_TOGGLE, FL_NORMAL_LABEL, 0, 14, 0}, { "Show Lights", 0, &gazebo::MainMenu::ShowLightsCB,0, FL_MENU_TOGGLE, FL_NORMAL_LABEL, 0, 14, 0}, { "Show Cameras", 0, &gazebo::MainMenu::ShowCamerasCB,0, FL_MENU_TOGGLE, FL_NORMAL_LABEL, 0, 14, 0}, + { "Per-Pixel Lighting", 0, &gazebo::MainMenu::PerPixelLightingCB,0, FL_MENU_TOGGLE|FL_MENU_VALUE, FL_NORMAL_LABEL, 0, 14, 0}, { 0 }, { 0 } @@ -175,3 +176,10 @@ { World::Instance()->SetShowCameras( !World::Instance()->GetShowCameras() ); } + +//////////////////////////////////////////////////////////////////////////////// +// Use per-pixel lighting +void MainMenu::PerPixelLightingCB(Fl_Widget * /*w*/, void * /*data*/) +{ + World::Instance()->SetPerPixelLighting( !World::Instance()->GetPerPixelLighting() ); +} Modified: code/gazebo/trunk/server/gui/MainMenu.hh =================================================================== --- code/gazebo/trunk/server/gui/MainMenu.hh 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/gui/MainMenu.hh 2010-01-28 17:56:15 UTC (rev 8534) @@ -61,6 +61,8 @@ public: static void ShowCamerasCB(Fl_Widget * /*w*/, void * /*data*/); + public: static void PerPixelLightingCB(Fl_Widget * /*w*/, void * /*data*/); + }; } Modified: code/gazebo/trunk/server/rendering/OgreVisual.cc =================================================================== --- code/gazebo/trunk/server/rendering/OgreVisual.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/rendering/OgreVisual.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -160,10 +160,15 @@ this->sceneNode->removeAndDestroyAllChildren(); */ + for (int i=0; i < this->sceneNode->numAttachedObjects(); i++) + RTShaderSystem::Instance()->DetachEntity( + (Ogre::Entity*)(this->sceneNode->getAttachedObject(i)) ); + if (this->sceneNode) OgreAdaptor::Instance()->sceneMgr->destroySceneNode(this->sceneNode); if (this->boundingBoxNode) OgreAdaptor::Instance()->sceneMgr->destroySceneNode(this->boundingBoxNode); + } //////////////////////////////////////////////////////////////////////////////// @@ -220,7 +225,6 @@ obj = (Ogre::MovableObject*)this->sceneNode->getCreator()->createEntity( stream.str(), meshName); - RTShaderSystem::Instance()->GenerateShaders((Ogre::Entity*)(obj)); } catch (Ogre::Exception e) { @@ -302,6 +306,9 @@ return; this->sceneNode->attachObject(obj); + Ogre::Entity *ent = dynamic_cast<Ogre::Entity*>(obj); + if (ent) + RTShaderSystem::Instance()->AttachEntity(ent); obj->setUserAny( Ogre::Any(this) ); } Modified: code/gazebo/trunk/server/rendering/RTShaderSystem.cc =================================================================== --- code/gazebo/trunk/server/rendering/RTShaderSystem.cc 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/rendering/RTShaderSystem.cc 2010-01-28 17:56:15 UTC (rev 8534) @@ -24,6 +24,8 @@ * SVN: $Id:$ */ +#include <boost/bind.hpp> +#include "World.hh" #include "GazeboError.hh" #include "GazeboMessage.hh" #include "OgreAdaptor.hh" @@ -35,6 +37,10 @@ /// Constructor RTShaderSystem::RTShaderSystem() { +#if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 + World::Instance()->ConnectPerPixelLightingSignal( boost::bind(&RTShaderSystem::SetPerPixelLighting, this, _1) ); + this->curLightingModel = RTShaderSystem::SSLM_PerPixelLighting; +#endif } //////////////////////////////////////////////////////////////////////////////// @@ -146,10 +152,60 @@ } //////////////////////////////////////////////////////////////////////////////// +// Set an Ogre::Entity to use RT shaders +void RTShaderSystem::AttachEntity(Ogre::Entity *entity) +{ +#if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 + this->GenerateShaders(entity); + this->entities.push_back(entity); +#endif +} + +//////////////////////////////////////////////////////////////////////////////// +// Remove and entity +void RTShaderSystem::DetachEntity(Ogre::Entity *entity) +{ +#if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 + this->entities.remove(entity); +#endif +} + +//////////////////////////////////////////////////////////////////////////////// +/// Set the lighting model +void RTShaderSystem::SetLightingModel(LightingModel model) +{ +#if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 + if (model == this->curLightingModel) + return; + + this->curLightingModel = model; + + std::list<Ogre::Entity*>::iterator iter; + + // Update all the shaders + for (iter = this->entities.begin(); iter != this->entities.end(); iter++) + { + this->GenerateShaders(*iter); + } +#endif +} + +//////////////////////////////////////////////////////////////////////////////// +// Set the lighting model to per pixel or per vertex +void RTShaderSystem::SetPerPixelLighting( bool s) +{ + if (s) + this->SetLightingModel( SSLM_PerPixelLighting ); + else + this->SetLightingModel( SSLM_PerVertexLighting ); +} + +//////////////////////////////////////////////////////////////////////////////// /// Generate shaders for an entity void RTShaderSystem::GenerateShaders(Ogre::Entity *entity) { #if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 + for (unsigned int i=0; i < entity->getNumSubEntities(); ++i) { Ogre::SubEntity* curSubEntity = entity->getSubEntity(i); @@ -188,25 +244,33 @@ // Remove all sub render states. renderState->reset(); -/*#ifdef RTSHADER_SYSTEM_BUILD_CORE_SHADERS - if (mCurLightingModel == SSLM_PerVertexLighting) +#ifdef RTSHADER_SYSTEM_BUILD_CORE_SHADERS + if (this->curLightingModel == SSLM_PerVertexLighting) { - RTShader::SubRenderState* perPerVertexLightModel = mShaderGenerator->createSubRenderState(RTShader::FFPLighting::Type); + Ogre::RTShader::SubRenderState* perPerVertexLightModel = this->shaderGenerator->createSubRenderState(Ogre::RTShader::FFPLighting::Type); renderState->addSubRenderState(perPerVertexLightModel); } #endif #ifdef RTSHADER_SYSTEM_BUILD_EXT_SHADERS - else if (mCurLightingModel == SSLM_PerPixelLighting) + else if (this->curLightingModel == SSLM_PerPixelLighting) { - */ - Ogre::RTShader::SubRenderState* perPixelLightModel = this->shaderGenerator->createSubRenderState(Ogre::RTShader::PerPixelLighting::Type); + Ogre::RTShader::SubRenderState* perPixelLightModel = this->shaderGenerator->createSubRenderState(Ogre::RTShader::PerPixelLighting::Type); renderState->addSubRenderState(perPixelLightModel); - //} + } } + + // Invalidate this material in order to re-generate its shaders. + this->shaderGenerator->invalidateMaterial(Ogre::RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME, curMaterialName); + + } #endif + + +#endif } - + + Modified: code/gazebo/trunk/server/rendering/RTShaderSystem.hh =================================================================== --- code/gazebo/trunk/server/rendering/RTShaderSystem.hh 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/server/rendering/RTShaderSystem.hh 2010-01-28 17:56:15 UTC (rev 8534) @@ -28,6 +28,7 @@ #define RTSHADERSYSTEM_HH #include <Ogre.h> +#include <list> #include "config.h" #if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 @@ -42,6 +43,14 @@ class RTShaderSystem : public SingletonT<RTShaderSystem> { + public: enum LightingModel + { + SSLM_PerVertexLighting, + SSLM_PerPixelLighting, + SSLM_NormalMapLightingTangentSpace, + SSLM_NormalMapLightingObjectSpace, + }; + /// \brief Constructor private: RTShaderSystem(); @@ -51,9 +60,16 @@ /// \brief Init the run time shader system public: void Init(); - /// \brief Generate shaders for an entity - public: void GenerateShaders(Ogre::Entity *entity); - + /// \brief Set the lighting model + public: void SetLightingModel(LightingModel model); + + /// \brief Set an Ogre::Entity to use RT shaders + public: void AttachEntity(Ogre::Entity *entity); + + /// \brief Remove and entity + public: void DetachEntity(Ogre::Entity *entity); + + /// \brief Set a viewport to use shaders public: static void AttachViewport(Ogre::Viewport *vp) { #if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 @@ -62,9 +78,18 @@ #endif } + /// Set the lighting model to per pixel or per vertex + public: void SetPerPixelLighting( bool s); + + /// \brief Generate shaders for an entity + private: void GenerateShaders(Ogre::Entity *entity); + + #if OGRE_VERSION_MAJOR == 1 && OGRE_VERSION_MINOR >= 7 private: Ogre::RTShader::ShaderGenerator *shaderGenerator; private: ShaderGeneratorTechniqueResolverListener *materialMgrListener; + private: LightingModel curLightingModel; + private: std::list<Ogre::Entity*> entities; #endif private: friend class DestroyerT<RTShaderSystem>; Modified: code/gazebo/trunk/worlds/pioneer2dx.world =================================================================== --- code/gazebo/trunk/worlds/pioneer2dx.world 2010-01-27 20:08:57 UTC (rev 8533) +++ code/gazebo/trunk/worlds/pioneer2dx.world 2010-01-28 17:56:15 UTC (rev 8534) @@ -38,7 +38,7 @@ </rendering:gui> <rendering:ogre> - <ambient>0.8 0.8 0.8 1.0</ambient> + <ambient>0.1 0.1 0.1 1.0</ambient> <sky> <material>Gazebo/CloudySky</material> </sky> @@ -67,7 +67,7 @@ <size>2000 2000</size> <segments>10 10</segments> <uvTile>2000 2000</uvTile> - <material>Gazebo/GrayGrid</material> + <material>Gazebo/Grey</material> </geom:plane> </body:plane> </model:physical> @@ -86,7 +86,7 @@ <visual> <scale>0.2 0.2 0.2</scale> <mesh>unit_sphere</mesh> - <material>Gazebo/Rocky</material> + <material>Gazebo/Grey</material> </visual> </geom:sphere> </body:sphere> @@ -200,17 +200,17 @@ <!-- White Point light --> <model:renderable name="point_white"> - <xyz>-3 0 10</xyz> + <xyz>0 0 5</xyz> <static>true</static> <light> <type>point</type> <diffuseColor>0.8 0.8 0.8</diffuseColor> <specularColor>0.1 0.1 0.1</specularColor> - <range>20</range> + <range>10</range> <!-- Constant(0-1) Linear(0-1) Quadratic --> - <attenuation>0.8 0.02 0</attenuation> + <attenuation>0.5 0.01 0</attenuation> </light> </model:renderable> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ The Planet: dedicated and managed hosting, cloud storage, colocation Stay online with enterprise data centers and the best network in the business Choose flexible plans and management services without long-term contracts Personal 24x7 support from experience hosting pros just a phone call away. http://p.sf.net/sfu/theplanet-com _______________________________________________ Playerstage-commit mailing list Playerstage-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/playerstage-commit