From: <spo...@us...> - 2009-02-14 19:11:58
|
Revision: 1032 http://opengate.svn.sourceforge.net/opengate/?rev=1032&view=rev Author: spom_spom Date: 2009-02-14 19:11:43 +0000 (Sat, 14 Feb 2009) Log Message: ----------- Forgot 2 files Added Paths: ----------- trunk/src/SectorEnvironmentObject.cpp trunk/src/SectorEnvironmentObject.h Added: trunk/src/SectorEnvironmentObject.cpp =================================================================== --- trunk/src/SectorEnvironmentObject.cpp (rev 0) +++ trunk/src/SectorEnvironmentObject.cpp 2009-02-14 19:11:43 UTC (rev 1032) @@ -0,0 +1,332 @@ +/*************************************************************************** + * Copyright (C) 2009 by OpenGate development team * + * spo...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "SectorEnvironmentObject.h" + +#include "LogManager.h" +#include "Sector.h" + +#include <OgreEntity.h> +#include <OgreLight.h> +#include <OgreManualObject.h> +#include <OgreMeshManager.h> +#include <OgreSceneManager.h> +#include <OgreSceneNode.h> +#include <OgreSubMesh.h> +#include <OgreVector3.h> + + +namespace OpenGate{ + +SectorEnvironmentObject::SectorEnvironmentObject( const std::string & name, Sector * sector, Ogre::SceneNode * parentNode ) + : SectorBaseObject( name, sector, parentNode ){ + +} + +SectorEnvironmentObject::~SectorEnvironmentObject( ){ + +} + +bool SectorEnvironmentObject::update( Ogre::Real elapsedTime ){ + return true; +} + + + + +SectorPlanetObject::SectorPlanetObject( const std::string & name, Sector * sector, Ogre::SceneNode * parentNode ) + : SectorEnvironmentObject( name, sector, parentNode ){ + + cloudNode_ = NULL; + cloudEntity_ = NULL; + + //planet_->lighting( true ); + createSphere_( name + "/Mesh", 15000.0f, 64, 64 ); + + entity_ = sector_->sceneManager()->createEntity( name, name + "/Mesh" ); + entity_->setMaterialName( "Planet" ); + + mainNode_->attachObject( entity_ ); + mainNode_->setPosition( Ogre::Vector3( 0000.0, 0000.0, 50000 ) ); + //planetNode_->pitch( Ogre::Degree(30) ); + + cloudNode_ = mainNode_->createChildSceneNode( mainNode_->getName() + "/cloud" ); + + cloudEntity_ = entity_->clone( cloudNode_->getName() ); + cloudEntity_->setMaterialName( "Planet/Cloud" ); + + cloudNode_->attachObject( cloudEntity_ ); + cloudNode_->scale(1.015, 1.015, 1.015); + + light_ = sector_->sceneManager()->createLight( name + "-light"); + light_->setDiffuseColour( 0.1, 0.1, 0.1 ); + light_->setSpecularColour( 0.4, 0.4, 0.4 ); + mainNode_->attachObject( light_ ); + light_->setVisible( true ); + +} + +SectorPlanetObject::~SectorPlanetObject( ){ + mainNode_->detachObject( light_ ); + sector_->sceneManager()->destroyLight( light_->getName() ); + + if ( cloudNode_ ){ + if ( cloudEntity_ ){ + cloudNode_->detachObject( cloudEntity_ ); + sector_->sceneManager()->destroyEntity( cloudEntity_ ); + } + mainNode_->removeAndDestroyChild( cloudNode_->getName() ); + } + + Ogre::MeshManager::getSingleton().remove( name_ + "/Mesh" ); + mainNode_->detachObject( entity_ ); + sector_->sceneManager()->destroyEntity( entity_ ); + +} + +bool SectorPlanetObject::update( Ogre::Real elapsedTime ){ + mainNode_->yaw( Ogre::Degree( 0.2 * elapsedTime ) ); + cloudNode_->yaw( Ogre::Degree( 0.1 * elapsedTime ) ); + return true; +} + +void SectorPlanetObject::createSphere_( const std::string & name, const float r, const int nRings, const int nSegments ){ + Ogre::MeshPtr pSphere = Ogre::MeshManager::getSingleton().createManual( name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME ); + Ogre::SubMesh *pSphereVertex = pSphere->createSubMesh(); + + pSphere->sharedVertexData = new Ogre::VertexData(); + Ogre::VertexData* vertexData = pSphere->sharedVertexData; + + // define the vertex format + Ogre::VertexDeclaration * vertexDecl = vertexData->vertexDeclaration; + size_t currOffset = 0; + // positions + vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); + currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); + // normals + vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL); + currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); + // two dimensional texture coordinates + vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0); + currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2); + + // allocate the vertex buffer + vertexData->vertexCount = (nRings + 1) * (nSegments+1); + Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( + vertexDecl->getVertexSize(0), vertexData->vertexCount, + Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false); + Ogre::VertexBufferBinding* binding = vertexData->vertexBufferBinding; + binding->setBinding(0, vBuf); + float * pVertex = static_cast<float*>(vBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); + + // allocate index buffer + pSphereVertex->indexData->indexCount = 6 * nRings * (nSegments + 1); + pSphereVertex->indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer( + Ogre::HardwareIndexBuffer::IT_16BIT, pSphereVertex->indexData->indexCount, + Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false); + Ogre::HardwareIndexBufferSharedPtr iBuf = pSphereVertex->indexData->indexBuffer; + unsigned short* pIndices = static_cast<unsigned short*>(iBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); + + float fDeltaRingAngle = (Ogre::Math::PI / nRings); + float fDeltaSegAngle = (2.0 * Ogre::Math::PI / nSegments); + unsigned short wVerticeIndex = 0 ; + + //! Generate the group of rings for the sphere + for ( int ring = 0; ring <= nRings; ring++ ) { + float r0 = r * sinf (ring * fDeltaRingAngle); + float y0 = r * cosf (ring * fDeltaRingAngle); + + //! Generate the group of segments for the current ring + for (int seg = 0; seg <= nSegments; seg++) { + float x0 = r0 * sinf(seg * fDeltaSegAngle); + float z0 = r0 * cosf(seg * fDeltaSegAngle); + + //! Add one vertex to the strip which makes up the sphere + *pVertex++ = x0; + *pVertex++ = y0; + *pVertex++ = z0; + + Ogre::Vector3 vNormal = Ogre::Vector3(x0, y0, z0).normalisedCopy(); + *pVertex++ = vNormal.x; + *pVertex++ = vNormal.y; + *pVertex++ = vNormal.z; + + *pVertex++ = (float) seg / (float) nSegments; + *pVertex++ = (float) ring / (float) nRings; + + if (ring != nRings) { + //! each vertex (except the last) has six indices pointing to it + *pIndices++ = wVerticeIndex + nSegments + 1; + *pIndices++ = wVerticeIndex; + *pIndices++ = wVerticeIndex + nSegments; + *pIndices++ = wVerticeIndex + nSegments + 1; + *pIndices++ = wVerticeIndex + 1; + *pIndices++ = wVerticeIndex; + wVerticeIndex ++; + } + } //! end for seg + } //! end for ring + + vBuf->unlock(); + iBuf->unlock(); + + //! Generate face list + pSphereVertex->useSharedVertices = true; + + //! the original code was missing this line: + pSphere->_setBounds( Ogre::AxisAlignedBox( Ogre::Vector3(-r, -r, -r), Ogre::Vector3(r, r, r) ), false ); + pSphere->_setBoundingSphereRadius( r ); + //! this line makes clear the mesh is loaded (avoids memory leaks) + pSphere->load(); + + Ogre::MeshPtr pMesh = Ogre::MeshManager::getSingleton().load( name, + Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, + Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, + Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, + true, true); + unsigned short src, dest; + + if ( !pMesh->suggestTangentVectorBuildParams( Ogre::VES_TANGENT, src, dest) ) { + pMesh->buildTangentVectors( Ogre::VES_TANGENT, src, dest ); + } +} + +SectorEclipticObject::SectorEclipticObject( Sector * sector, Ogre::SceneNode * parentNode ) + : SectorEnvironmentObject( "Ecliptic", sector, parentNode ){ + + createGrid_(); +} + +SectorEclipticObject::~SectorEclipticObject(){ + mainNode_->detachObject( entity_ ); + sector_->sceneManager()->destroyManualObject( entity_ ); +} + +bool SectorEclipticObject::update( Ogre::Real elapsedTime ){ + return true; +} + +void SectorEclipticObject::createGrid_(){ + entity_ = sector_->sceneManager()->createManualObject( name_ + "/ManualObject" ); + Ogre::Real thickness = 15.0; // Of course this must be less than the radius value. + Ogre::Real nx = 20, nz = 20; + Ogre::Real dx = 1000, dz = 1000; + + unsigned point_index = 0; + + entity_->begin( "shader/gradient", Ogre::RenderOperation::OT_TRIANGLE_LIST); + entity_->getSection( 0 )->setCustomParameter( 0, Ogre::Vector4( 0.0, 0.0, 1.0, 0.2 ) ); + entity_->getSection( 0 )->setCustomParameter( 1, Ogre::Vector4( nx * dx, 0.0, 0.0, 0.0 ) ); + + for ( int i = 0; i < nx+1; i ++ ){ + entity_->position( -nx * dx / 2, 0.0, -nx * dx / 2 + i * dx - thickness ); + entity_->position( nx * dx / 2, 0.0, -nx * dx / 2 + i * dx - thickness ); + entity_->position( nx * dx / 2, 0.0, -nx * dx / 2 + i * dx + thickness ); + entity_->position( -nx * dx / 2, 0.0, -nx * dx / 2 + i * dx + thickness ); + point_index = (i * 4); + entity_->quad( point_index + 0, point_index + 1, point_index + 2, point_index + 3 ); + } + entity_->end(); + + entity_->begin( "shader/gradient", Ogre::RenderOperation::OT_TRIANGLE_LIST); + entity_->getSection( 1 )->setCustomParameter( 0, Ogre::Vector4( 0.0, 0.0, 1.0, 0.2 ) ); + entity_->getSection( 1 )->setCustomParameter( 1, Ogre::Vector4( nz * dz, 0.0, 0.0, 0.0 ) ); + + for ( int i = 0; i < nz+1; i ++ ){ + entity_->position( -nz * dz / 2 + i * dx + thickness, 0.0, -nz * dz / 2 ); + entity_->position( -nz * dz / 2 + i * dx + thickness, 0.0, nz * dz / 2 ); + entity_->position( -nz * dz / 2 + i * dx - thickness, 0.0, nz * dz / 2 ); + entity_->position( -nz * dz / 2 + i * dx - thickness, 0.0, -nz * dz / 2 ); + point_index = (i * 4); + entity_->quad( point_index + 0, point_index + 1, point_index + 2, point_index + 3 ); + } + entity_->end(); + + entity_->begin( "shader/gradient", Ogre::RenderOperation::OT_TRIANGLE_LIST); + entity_->getSection( 2 )->setCustomParameter( 0, Ogre::Vector4( 0.0, 1.0, 0.0, 0.2 ) ); + entity_->getSection( 2 )->setCustomParameter( 1, Ogre::Vector4( nx * dx, 0.0, 0.0, 0.0 ) ); + + for ( int i = 0; i < nx+1; i ++ ){ + entity_->position( -nx * dx / 2, 0.0, -nx * dx / 2 + i * dx - thickness ); + entity_->position( nx * dx / 2, 0.0, -nx * dx / 2 + i * dx - thickness ); + entity_->position( nx * dx / 2, 0.0, -nx * dx / 2 + i * dx + thickness ); + entity_->position( -nx * dx / 2, 0.0, -nx * dx / 2 + i * dx + thickness ); + point_index = (i * 4); + entity_->quad( point_index + 3, point_index + 2, point_index + 1, point_index + 0 ); + } + entity_->end(); + + entity_->begin( "shader/gradient", Ogre::RenderOperation::OT_TRIANGLE_LIST); + entity_->getSection( 3 )->setCustomParameter( 0, Ogre::Vector4( 0.0, 1.0, 0.0, 0.2 ) ); + entity_->getSection( 3 )->setCustomParameter( 1, Ogre::Vector4( nz * dz, 0.0, 0.0, 0.0 ) ); + + for ( int i = 0; i < nz+1; i ++ ){ + entity_->position( -nz * dz / 2 + i * dx + thickness, 0.0, -nz * dz / 2 ); + entity_->position( -nz * dz / 2 + i * dx + thickness, 0.0, nz * dz / 2 ); + entity_->position( -nz * dz / 2 + i * dx - thickness, 0.0, nz * dz / 2 ); + entity_->position( -nz * dz / 2 + i * dx - thickness, 0.0, -nz * dz / 2 ); + point_index = (i * 4); + entity_->quad( point_index + 3, point_index + 2, point_index + 1, point_index + 0 ); + } + entity_->end(); + + mainNode_->attachObject( entity_ ); +} + + +// if ( !1 ) { +// planetNode_ = sceneMgr_->getRootSceneNode()->createChildSceneNode( "PlanetNode" ); +// +// Ogre::ManualObject * planet = sceneMgr_->createManualObject( "Planet" ); +// // planet->setUseIdentityProjection( true ); +// // planet->setUseIdentityView( true ); +// +// planet->begin( "OpenGate/RedPlanet", Ogre::RenderOperation::OT_TRIANGLE_LIST); +// planet->position( Ogre::Vector3( -1.0, -1.0, 200.0 ) ); +// planet->textureCoord( 0, 0 ); +// planet->position( Ogre::Vector3( 1.0, -1.0, 200.0 ) ); +// planet->textureCoord( 1, 0 ); +// planet->position( Ogre::Vector3( 1.0, 1.0, 200.0 ) ); +// planet->textureCoord( 1, 1 ); +// planet->position( Ogre::Vector3( -1.0, 1.0, 200.0 ) ); +// planet->textureCoord( 0, 1 ); +// planet->quad( 0, 1, 2, 3 ); +// planet->end(); +// Ogre::AxisAlignedBox aabInf; aabInf.setInfinite(); +// planet->setBoundingBox( aabInf ); +// planetNode_->attachObject( planet ); +// planetNode_->scale( 20000, 20000, 1 ); +// planetNode_->setPosition( Ogre::Vector3( 20000.0, 10000.0, 50000 ) ); +// } +// if ( planetNode_ ){ // updateLoop +// Ogre::Vector3 src = planetNode_->getWorldOrientation() * Ogre::Vector3::UNIT_Z; +// src.normalise(); +// +// Ogre::Vector3 target( avatar_->mainNode()->getPosition() - planetNode_->getWorldPosition() ); +// target.normalise(); +// +// Ogre::Quaternion rot = src.getRotationTo( target ); +// rot.normalise(); +// +// planetNode_->setPosition( Ogre::Vector3( 20000.0, 10000.0, 50000 ) + avatar_->mainNode()->getPosition() ); +// planetNode_->rotate( rot, Ogre::Node::TS_PARENT ); +// } + +} Added: trunk/src/SectorEnvironmentObject.h =================================================================== --- trunk/src/SectorEnvironmentObject.h (rev 0) +++ trunk/src/SectorEnvironmentObject.h 2009-02-14 19:11:43 UTC (rev 1032) @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2009 by OpenGate development team * + * spo...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _OPENGATE_SECTORENVIRONMENT__H +#define _OPENGATE_SECTORENVIRONMENT__H + +#include "Opengate.h" +#include "SectorBaseObject.h" + +namespace OpenGate{ + +/*! Baseclass for space environment elements without collision*/ +class SectorEnvironmentObject : public SectorBaseObject { +public: + SectorEnvironmentObject( const Ogre::String & name, Sector * sector, Ogre::SceneNode * parentNode = NULL ); + + virtual ~SectorEnvironmentObject( ); + + virtual int rtti( ) const { return SECTOR_ENVIRONMENT_OBJECT_RTTI; } + + /*! Update loop for the environment */ + virtual bool update( Ogre::Real elapsedTime ); + +protected: + +}; + +class SectorPlanetObject : public SectorEnvironmentObject{ +public: + SectorPlanetObject( const Ogre::String & name, Sector * sector, Ogre::SceneNode * parentNode = NULL ); + + virtual ~SectorPlanetObject(); + + /*! Update loop for the environment */ + virtual bool update( Ogre::Real elapsedTime ); + +protected: + //** taken from ogre wiki: http://www.ogre3d.org/wiki/index.php/ManualSphereMeshes + void createSphere_( const std::string & name, const float r, const int nRings, const int nSegments ); + + Ogre::SceneNode * cloudNode_; + Ogre::Entity * cloudEntity_; + Ogre::Entity * entity_; + Ogre::Light * light_; +}; + +class SectorEclipticObject : public SectorEnvironmentObject{ +public: + SectorEclipticObject( Sector * sector, Ogre::SceneNode * parentNode = NULL ); + + ~SectorEclipticObject(); + + virtual bool update( Ogre::Real elapsedTime ); + +protected: + void createGrid_(); + + Ogre::ManualObject * entity_; +}; +// class SectorStarfieldObject : public SectorEnvironmentObject{ +// }; +// class SectorSpaceDustObject : public SectorEnvironmentObject{ +// }; +// class SectorSpaceDecorationObject : public SectorEnvironmentObject{ +// }; + + + + +} //namespace OpenGate + +#endif //_OPENGATE_SECTORENVIRONMENT__H + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |