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 |
|
---|
18 | namespace 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 | //-----------------------------------------------------------------------
|
---|
64 | void 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 | //-----------------------------------------------------------------------
|
---|
75 | void 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 | /*
|
---|
100 | // Left-top
|
---|
101 | // Positions
|
---|
102 | *mLockPtr++ = -50 + bb.mPosition.x;
|
---|
103 | *mLockPtr++ = 50 + bb.mPosition.y;
|
---|
104 | *mLockPtr++ = 0 + bb.mPosition.z;
|
---|
105 | // Colour
|
---|
106 | // Convert float* to RGBA*
|
---|
107 | pCol = static_cast<RGBA*>(static_cast<void*>(mLockPtr));
|
---|
108 | *pCol++ = colour;
|
---|
109 | // Update lock pointer
|
---|
110 | mLockPtr = static_cast<float*>(static_cast<void*>(pCol));
|
---|
111 | // Texture coords
|
---|
112 | *mLockPtr++ = r.left;
|
---|
113 | *mLockPtr++ = r.bottom;
|
---|
114 |
|
---|
115 | // Right-top
|
---|
116 | // Positions
|
---|
117 | *mLockPtr++ = 50 + bb.mPosition.x;
|
---|
118 | *mLockPtr++ = 50 + bb.mPosition.y;
|
---|
119 | *mLockPtr++ = 0 + bb.mPosition.z;
|
---|
120 | // Colour
|
---|
121 | // Convert float* to RGBA*
|
---|
122 | pCol = static_cast<RGBA*>(static_cast<void*>(mLockPtr));
|
---|
123 | *pCol++ = colour;
|
---|
124 | // Update lock pointer
|
---|
125 | mLockPtr = static_cast<float*>(static_cast<void*>(pCol));
|
---|
126 | // Texture coords
|
---|
127 | *mLockPtr++ = r.right;
|
---|
128 | *mLockPtr++ = r.bottom;
|
---|
129 |
|
---|
130 | // Left-bottom
|
---|
131 | // Positions
|
---|
132 | *mLockPtr++ = -50 + bb.mPosition.x;
|
---|
133 | *mLockPtr++ = -50 + bb.mPosition.y;
|
---|
134 | *mLockPtr++ = 0 + bb.mPosition.z;
|
---|
135 | // Colour
|
---|
136 | // Convert float* to RGBA*
|
---|
137 | pCol = static_cast<RGBA*>(static_cast<void*>(mLockPtr));
|
---|
138 | *pCol++ = colour;
|
---|
139 | // Update lock pointer
|
---|
140 | mLockPtr = static_cast<float*>(static_cast<void*>(pCol));
|
---|
141 | // Texture coords
|
---|
142 | *mLockPtr++ = r.left;
|
---|
143 | *mLockPtr++ = r.top;
|
---|
144 |
|
---|
145 | // Right-bottom
|
---|
146 | // Positions
|
---|
147 | *mLockPtr++ = 50 + bb.mPosition.x;
|
---|
148 | *mLockPtr++ = -50 + bb.mPosition.y;
|
---|
149 | *mLockPtr++ = 0 + bb.mPosition.z;
|
---|
150 | // Colour
|
---|
151 | // Convert float* to RGBA*
|
---|
152 | pCol = static_cast<RGBA*>(static_cast<void*>(mLockPtr));
|
---|
153 | *pCol++ = colour;
|
---|
154 | // Update lock pointer
|
---|
155 | mLockPtr = static_cast<float*>(static_cast<void*>(pCol));
|
---|
156 | // Texture coords
|
---|
157 | *mLockPtr++ = r.right;
|
---|
158 | *mLockPtr++ = r.top;*/
|
---|
159 |
|
---|
160 | float width;
|
---|
161 | if(bb.hasOwnDimensions())
|
---|
162 | width = bb.getOwnWidth() / 2.0;
|
---|
163 | else
|
---|
164 | width = getDefaultWidth() / 2.0;
|
---|
165 |
|
---|
166 | static float TexData[24] =
|
---|
167 | {
|
---|
168 | r.left, r.top, -width, width,
|
---|
169 | r.left, r.bottom, -width, -width,
|
---|
170 | r.right, r.bottom, width, -width,
|
---|
171 | r.right, r.top, width, width,
|
---|
172 | r.left, r.top, -width, width,
|
---|
173 | r.right, r.bottom, width, -width
|
---|
174 | };
|
---|
175 |
|
---|
176 | for(int i=0;i<6;i++)
|
---|
177 | {
|
---|
178 | *mLockPtr++ = bb.mPosition.x;
|
---|
179 | *mLockPtr++ = bb.mPosition.y;
|
---|
180 | *mLockPtr++ = bb.mPosition.z;
|
---|
181 |
|
---|
182 | pCol = static_cast<RGBA*>(static_cast<void*>(mLockPtr));
|
---|
183 | *pCol++ = colour;
|
---|
184 |
|
---|
185 | mLockPtr = static_cast<float*>(static_cast<void*>(pCol));
|
---|
186 | // Texture coords
|
---|
187 | *mLockPtr++ = TexData[i*4+0];
|
---|
188 | *mLockPtr++ = TexData[i*4+1];
|
---|
189 | *mLockPtr++ = TexData[i*4+2];
|
---|
190 | *mLockPtr++ = TexData[i*4+3];
|
---|
191 |
|
---|
192 | }
|
---|
193 | }
|
---|
194 | //-----------------------------------------------------------------------
|
---|
195 | const String& SpriteSet::getMovableType(void) const
|
---|
196 | {
|
---|
197 | return SpriteSetFactory::FACTORY_TYPE_NAME;
|
---|
198 | }
|
---|
199 | //-----------------------------------------------------------------------
|
---|
200 | //-----------------------------------------------------------------------
|
---|
201 | String SpriteSetFactory::FACTORY_TYPE_NAME = "SpriteSet";
|
---|
202 | //-----------------------------------------------------------------------
|
---|
203 | const String& SpriteSetFactory::getType(void) const
|
---|
204 | {
|
---|
205 | return FACTORY_TYPE_NAME;
|
---|
206 | }
|
---|
207 | //-----------------------------------------------------------------------
|
---|
208 | MovableObject* SpriteSetFactory::createInstanceImpl( const String& name,
|
---|
209 | const NameValuePairList* params)
|
---|
210 | {
|
---|
211 | // may have parameters
|
---|
212 | bool externalData = false;
|
---|
213 | unsigned int poolSize = 0;
|
---|
214 |
|
---|
215 | if (params != 0)
|
---|
216 | {
|
---|
217 | NameValuePairList::const_iterator ni = params->find("poolSize");
|
---|
218 | if (ni != params->end())
|
---|
219 | {
|
---|
220 | poolSize = StringConverter::parseUnsignedInt(ni->second);
|
---|
221 | }
|
---|
222 | ni = params->find("externalData");
|
---|
223 | if (ni != params->end())
|
---|
224 | {
|
---|
225 | externalData = StringConverter::parseBool(ni->second);
|
---|
226 | }
|
---|
227 |
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (poolSize > 0)
|
---|
231 | {
|
---|
232 | return new SpriteSet(name, poolSize, externalData);
|
---|
233 | }
|
---|
234 | else
|
---|
235 | {
|
---|
236 | return new SpriteSet(name);
|
---|
237 | }
|
---|
238 |
|
---|
239 | }
|
---|
240 | //-----------------------------------------------------------------------
|
---|
241 | void SpriteSetFactory::destroyInstance( MovableObject* obj)
|
---|
242 | {
|
---|
243 | delete obj;
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | #endif |
---|