source: OGRE/trunk/ogre_changes/Ogre1.2/OgreMain/src/SpriteSet.cpp @ 1459

Revision 1459, 5.6 KB checked in by szirmay, 18 years ago (diff)
Line 
1#include "OgreStableHeaders.h"
2
3#include "SpriteSet.h"
4
5#include "OgreBillboard.h"
6#include "OgreMaterialManager.h"
7#include "OgreHardwareBufferManager.h"
8#include "OgreCamera.h"
9#include "OgreMath.h"
10#include "OgreSphere.h"
11#include "OgreRoot.h"
12#include "OgreException.h"
13#include <algorithm>
14
15#ifdef GAMETOOLS_ILLUMINATION_MODULE
16
17
18namespace Ogre
19{
20        void SpriteSet::beginBillboards(void)
21    {     
22        // create vertex and index buffers if they haven't already been
23        if(!mBuffersCreated)
24            _createBuffers();           
25
26        // Init num visible
27        mNumVisibleBillboards = 0;
28
29        // Lock the buffer
30        mLockPtr = static_cast<float*>(
31            mMainBuf->lock(HardwareBuffer::HBL_DISCARD) );
32    }
33
34        void SpriteSet::_createBuffers(void)
35    {
36        mVertexData = new VertexData();
37       
38        mVertexData->vertexCount = mPoolSize * 6;
39        mVertexData->vertexStart = 0;
40
41        // Vertex declaration
42        VertexDeclaration* decl = mVertexData->vertexDeclaration;
43        VertexBufferBinding* binding = mVertexData->vertexBufferBinding;
44
45        size_t offset = 0;
46        decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
47        offset += VertexElement::getTypeSize(VET_FLOAT3);
48        decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
49        offset += VertexElement::getTypeSize(VET_COLOUR);
50        decl->addElement(0, offset, VET_FLOAT4, VES_TEXTURE_COORDINATES, 0);
51
52        mMainBuf =
53            HardwareBufferManager::getSingleton().createVertexBuffer(
54                decl->getVertexSize(0),
55                mVertexData->vertexCount,
56                HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);
57        // bind position and diffuses
58        binding->setBinding(0, mMainBuf);
59     
60        mBuffersCreated = true;
61        }
62
63//-----------------------------------------------------------------------
64void SpriteSet::getRenderOperation(RenderOperation &op)
65{
66    op.operationType = RenderOperation::OT_TRIANGLE_LIST;
67    op.useIndexes = false;
68        op.indexData = 0;
69    op.vertexData = mVertexData;
70    op.vertexData->vertexCount = mNumVisibleBillboards * 6;
71    op.vertexData->vertexStart = 0;
72 }
73
74//-----------------------------------------------------------------------
75void SpriteSet::injectBillboard(const Billboard& bb)
76{
77      // Skip if not visible (NB always true if not bounds checking individual billboards)
78        if (!billboardVisible(mCurrentCamera, bb)) return;
79
80     
81        genVertices( bb);
82       
83        // Increment visibles
84        mNumVisibleBillboards++;
85 }
86
87 //-----------------------------------------------------------------------
88    void SpriteSet::genVertices(  const Billboard& bb)
89    {
90                RGBA colour;
91        Root::getSingleton().convertColourValue(bb.mColour, &colour);
92                RGBA* pCol;
93
94        // Texcoords
95        assert( bb.mUseTexcoordRect || bb.mTexcoordIndex < mTextureCoords.size() );
96        const Ogre::FloatRect & r =
97            bb.mUseTexcoordRect ? bb.mTexcoordRect : mTextureCoords[bb.mTexcoordIndex];
98               
99                float width;
100                float height;
101                if(bb.hasOwnDimensions())
102                {
103                        width = bb.getOwnWidth() / 2.0;
104                        height = bb.getOwnHeight() / 2.0;
105                }
106                else
107                {
108                        width = getDefaultWidth() / 2.0;
109                        height = getDefaultHeight() / 2.0;
110                }
111
112                float TexData[24] =
113                {
114                        r.left,         r.top,                  -width,  height,
115                        r.left,         r.bottom,               -width, -height,
116            r.right,    r.bottom,                width, -height,
117                        r.right,        r.top,                   width,  height,
118            r.left,             r.top,                  -width,  height,
119                        r.right,        r.bottom,                width, -height
120                };
121
122           for(int i=0;i<6;i++)
123           {
124                   *mLockPtr++ =  bb.mPosition.x;
125            *mLockPtr++ =  bb.mPosition.y;
126            *mLockPtr++ = bb.mPosition.z;
127
128                         pCol = static_cast<RGBA*>(static_cast<void*>(mLockPtr));
129            *pCol++ = colour;
130
131                         mLockPtr = static_cast<float*>(static_cast<void*>(pCol));
132            // Texture coords
133            *mLockPtr++ = TexData[i*4+0];
134            *mLockPtr++ = TexData[i*4+1];
135                        *mLockPtr++ = TexData[i*4+2];
136                        *mLockPtr++ = TexData[i*4+3];
137           
138           }
139    }
140        //-----------------------------------------------------------------------
141    const String& SpriteSet::getMovableType(void) const
142    {
143                return SpriteSetFactory::FACTORY_TYPE_NAME;
144    }
145        //-----------------------------------------------------------------------
146        //-----------------------------------------------------------------------
147        String SpriteSetFactory::FACTORY_TYPE_NAME = "SpriteSet";
148        //-----------------------------------------------------------------------
149        const String& SpriteSetFactory::getType(void) const
150        {
151                return FACTORY_TYPE_NAME;
152        }
153        //-----------------------------------------------------------------------
154        MovableObject* SpriteSetFactory::createInstanceImpl( const String& name,
155                const NameValuePairList* params)
156        {
157                // may have parameters
158                bool externalData = false;
159                unsigned int poolSize = 0;
160
161                if (params != 0)
162                {
163                        NameValuePairList::const_iterator ni = params->find("poolSize");
164                        if (ni != params->end())
165                        {
166                                poolSize = StringConverter::parseUnsignedInt(ni->second);
167                        }
168                        ni = params->find("externalData");
169                        if (ni != params->end())
170                        {
171                                externalData = StringConverter::parseBool(ni->second);
172                        }
173
174                }
175
176                if (poolSize > 0)
177                {
178                        return new SpriteSet(name, poolSize, externalData);
179                }
180                else
181                {
182                        return new SpriteSet(name);
183                }
184
185        }
186        //-----------------------------------------------------------------------
187        void SpriteSetFactory::destroyInstance( MovableObject* obj)
188        {
189                delete obj;
190        }
191}
192
193#endif
Note: See TracBrowser for help on using the repository browser.