#include "OgreStableHeaders.h" #include "SpriteSet.h" #include "OgreBillboard.h" #include "OgreMaterialManager.h" #include "OgreHardwareBufferManager.h" #include "OgreCamera.h" #include "OgreMath.h" #include "OgreSphere.h" #include "OgreRoot.h" #include "OgreException.h" #include #ifdef GAMETOOLS_ILLUMINATION_MODULE namespace Ogre { void SpriteSet::beginBillboards(void) { // create vertex and index buffers if they haven't already been if(!mBuffersCreated) _createBuffers(); // Init num visible mNumVisibleBillboards = 0; // Lock the buffer mLockPtr = static_cast( mMainBuf->lock(HardwareBuffer::HBL_DISCARD) ); } void SpriteSet::_createBuffers(void) { mVertexData = new VertexData(); mVertexData->vertexCount = mPoolSize * 6; mVertexData->vertexStart = 0; // Vertex declaration VertexDeclaration* decl = mVertexData->vertexDeclaration; VertexBufferBinding* binding = mVertexData->vertexBufferBinding; size_t offset = 0; decl->addElement(0, offset, VET_FLOAT3, VES_POSITION); offset += VertexElement::getTypeSize(VET_FLOAT3); decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE); offset += VertexElement::getTypeSize(VET_COLOUR); decl->addElement(0, offset, VET_FLOAT4, VES_TEXTURE_COORDINATES, 0); mMainBuf = HardwareBufferManager::getSingleton().createVertexBuffer( decl->getVertexSize(0), mVertexData->vertexCount, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE); // bind position and diffuses binding->setBinding(0, mMainBuf); mBuffersCreated = true; } //----------------------------------------------------------------------- void SpriteSet::getRenderOperation(RenderOperation &op) { op.operationType = RenderOperation::OT_TRIANGLE_LIST; op.useIndexes = false; op.indexData = 0; op.vertexData = mVertexData; op.vertexData->vertexCount = mNumVisibleBillboards * 6; op.vertexData->vertexStart = 0; } //----------------------------------------------------------------------- void SpriteSet::injectBillboard(const Billboard& bb) { // Skip if not visible (NB always true if not bounds checking individual billboards) if (!billboardVisible(mCurrentCamera, bb)) return; genVertices( bb); // Increment visibles mNumVisibleBillboards++; } //----------------------------------------------------------------------- void SpriteSet::genVertices( const Billboard& bb) { RGBA colour; Root::getSingleton().convertColourValue(bb.mColour, &colour); RGBA* pCol; // Texcoords assert( bb.mUseTexcoordRect || bb.mTexcoordIndex < mTextureCoords.size() ); const Ogre::FloatRect & r = bb.mUseTexcoordRect ? bb.mTexcoordRect : mTextureCoords[bb.mTexcoordIndex]; float width; float height; if(bb.hasOwnDimensions()) { width = bb.getOwnWidth() / 2.0; height = bb.getOwnHeight() / 2.0; } else { width = getDefaultWidth() / 2.0; height = getDefaultHeight() / 2.0; } float TexData[24] = { r.left, r.top, -width, height, r.left, r.bottom, -width, -height, r.right, r.bottom, width, -height, r.right, r.top, width, height, r.left, r.top, -width, height, r.right, r.bottom, width, -height }; for(int i=0;i<6;i++) { *mLockPtr++ = bb.mPosition.x; *mLockPtr++ = bb.mPosition.y; *mLockPtr++ = bb.mPosition.z; pCol = static_cast(static_cast(mLockPtr)); *pCol++ = colour; mLockPtr = static_cast(static_cast(pCol)); // Texture coords *mLockPtr++ = TexData[i*4+0]; *mLockPtr++ = TexData[i*4+1]; *mLockPtr++ = TexData[i*4+2]; *mLockPtr++ = TexData[i*4+3]; } } //----------------------------------------------------------------------- const String& SpriteSet::getMovableType(void) const { return SpriteSetFactory::FACTORY_TYPE_NAME; } //----------------------------------------------------------------------- //----------------------------------------------------------------------- String SpriteSetFactory::FACTORY_TYPE_NAME = "SpriteSet"; //----------------------------------------------------------------------- const String& SpriteSetFactory::getType(void) const { return FACTORY_TYPE_NAME; } //----------------------------------------------------------------------- MovableObject* SpriteSetFactory::createInstanceImpl( const String& name, const NameValuePairList* params) { // may have parameters bool externalData = false; unsigned int poolSize = 0; if (params != 0) { NameValuePairList::const_iterator ni = params->find("poolSize"); if (ni != params->end()) { poolSize = StringConverter::parseUnsignedInt(ni->second); } ni = params->find("externalData"); if (ni != params->end()) { externalData = StringConverter::parseBool(ni->second); } } if (poolSize > 0) { return new SpriteSet(name, poolSize, externalData); } else { return new SpriteSet(name); } } //----------------------------------------------------------------------- void SpriteSetFactory::destroyInstance( MovableObject* obj) { delete obj; } } #endif