1 | #include "OgreRenderingrun.h"
|
---|
2 |
|
---|
3 | MovablePlane* OgreRenderingRun::fullScreenQuad = NULL;
|
---|
4 | Entity* OgreRenderingRun::fullScreenQuadEntity = NULL;
|
---|
5 | SceneNode* OgreRenderingRun::fullScreenQuadNode = NULL;
|
---|
6 |
|
---|
7 | class MaterialSetVisitor : public QueuedRenderableVisitor
|
---|
8 | {
|
---|
9 | private:
|
---|
10 | String materialName;
|
---|
11 | std::map<Renderable*, String>* visibleObjects;
|
---|
12 | public:
|
---|
13 |
|
---|
14 | MaterialSetVisitor(String& materialName, std::map<Renderable*, String>* visibleObjects)
|
---|
15 | {
|
---|
16 | this->materialName = materialName;
|
---|
17 | this->visibleObjects = visibleObjects;
|
---|
18 | }
|
---|
19 |
|
---|
20 | void visit(const Renderable* r)
|
---|
21 | {
|
---|
22 | Renderable* rend = (Renderable*) r; //sorry for removing the const
|
---|
23 |
|
---|
24 | if( visibleObjects->count(rend) == 0)
|
---|
25 | {
|
---|
26 | (*visibleObjects)[rend] = rend->getMaterial().getPointer()->getName();
|
---|
27 | rend->setMaterialName(materialName);
|
---|
28 | }
|
---|
29 | }
|
---|
30 | bool visit(const Pass* p){return true;}
|
---|
31 | void visit(const RenderablePass* rp){}
|
---|
32 |
|
---|
33 | };
|
---|
34 |
|
---|
35 | Vector3 OgreRenderingRun::getCubeMapFaceDirection(unsigned char faceId)
|
---|
36 | {
|
---|
37 | switch(faceId)
|
---|
38 | {
|
---|
39 | case 0: //positive X
|
---|
40 | return Vector3(1,0,0);
|
---|
41 | case 1: //negative X
|
---|
42 | return Vector3(-1,0,0);
|
---|
43 | case 2: //positiove Y
|
---|
44 | return Vector3(0,1,0);
|
---|
45 | case 3: //negative Y
|
---|
46 | return Vector3(0,-1,0);
|
---|
47 | case 4: //positiove Z
|
---|
48 | return Vector3(0,0,-1);
|
---|
49 | case 5: //negative Z
|
---|
50 | return Vector3(0,0,1);
|
---|
51 | default:
|
---|
52 | return Vector3(0,0,0);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | Texture* OgreRenderingRun::createCubeRenderTexture(String name, const Vector3 position, unsigned int resolution, PixelFormat format, int numMips, ColourValue clearColor)
|
---|
57 | {
|
---|
58 | //create texture
|
---|
59 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual( name,
|
---|
60 | "default",
|
---|
61 | TEX_TYPE_CUBE_MAP,
|
---|
62 | resolution,
|
---|
63 | resolution,
|
---|
64 | 0,
|
---|
65 | numMips,
|
---|
66 | format,
|
---|
67 | TU_RENDERTARGET);
|
---|
68 | Texture* result = texPtr.getPointer();
|
---|
69 |
|
---|
70 | //add viewports to faces
|
---|
71 | for(unsigned int i = 0; i < 6; i++)
|
---|
72 | {
|
---|
73 | Camera* cam = Root::getSingleton()._getCurrentSceneManager()
|
---|
74 | ->createCamera(name + "CUBEMAPFACE_" + StringConverter::toString(i) + "_CAMERA");
|
---|
75 |
|
---|
76 | cam->setPosition(position);
|
---|
77 | cam->setAspectRatio(1);
|
---|
78 | cam->setFOVy(Radian(Degree(90)));
|
---|
79 | cam->setNearClipDistance(0.1);
|
---|
80 |
|
---|
81 | //set camera orientation
|
---|
82 | switch(i)
|
---|
83 | {
|
---|
84 | case 2:
|
---|
85 | cam->setOrientation(Quaternion(Radian(Degree(90)),Vector3(1,0,0)));
|
---|
86 | break;
|
---|
87 | case 3:
|
---|
88 | cam->setOrientation(Quaternion(Radian(Degree(90)),Vector3(-1,0,0)));
|
---|
89 | break;
|
---|
90 | default:
|
---|
91 | cam->setDirection(getCubeMapFaceDirection(i));
|
---|
92 | break;
|
---|
93 | }
|
---|
94 |
|
---|
95 | HardwarePixelBuffer* hpb = (result->getBuffer(i, 0)).getPointer();
|
---|
96 | RenderTarget* rt = hpb->getRenderTarget(0);
|
---|
97 | Viewport* v = rt->addViewport(cam);
|
---|
98 | v->setOverlaysEnabled(false);
|
---|
99 | rt->setAutoUpdated(false);
|
---|
100 |
|
---|
101 | Root::getSingleton().getRenderSystem()->_setViewport(v);
|
---|
102 | Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, clearColor);
|
---|
103 | }
|
---|
104 |
|
---|
105 | return result;
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | void OgreRenderingRun::setMaterialForRenderables(String& materialName, RenderQueue* rq)
|
---|
110 | {
|
---|
111 | RenderQueue::QueueGroupIterator queueIt = rq->_getQueueGroupIterator();
|
---|
112 |
|
---|
113 | while (queueIt.hasMoreElements())
|
---|
114 | {
|
---|
115 | RenderQueueGroup* pGroup = queueIt.getNext();
|
---|
116 |
|
---|
117 | RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
|
---|
118 |
|
---|
119 | while (groupIt.hasMoreElements())
|
---|
120 | {
|
---|
121 | RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
|
---|
122 | const QueuedRenderableCollection& solids = pPriorityGrp->getSolidsBasic();
|
---|
123 | const QueuedRenderableCollection& transparents = pPriorityGrp->getTransparents();
|
---|
124 | MaterialSetVisitor visitor(materialName, &visibleObjects);
|
---|
125 | solids.acceptVisitor(&visitor, QueuedRenderableCollection::OM_PASS_GROUP);
|
---|
126 | transparents.acceptVisitor(&visitor, QueuedRenderableCollection::OM_SORT_ASCENDING);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | //refill renderqueue
|
---|
131 | rq->clear();
|
---|
132 |
|
---|
133 | std::map<Renderable*, String>::iterator it = visibleObjects.begin();
|
---|
134 | std::map<Renderable*, String>::iterator itend = visibleObjects.end();
|
---|
135 |
|
---|
136 | while(it != itend)
|
---|
137 | {
|
---|
138 | rq->addRenderable((*it).first);
|
---|
139 | it++;
|
---|
140 | }
|
---|
141 |
|
---|
142 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
143 | sm->setFindVisibleObjects(false);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void OgreRenderingRun::setMaterialForVisibles(String& materialName, Camera* cam, bool shadowcastersonly)
|
---|
147 | {
|
---|
148 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
149 |
|
---|
150 | RenderQueue* rq = sm->getRenderQueue();
|
---|
151 | rq->clear();
|
---|
152 |
|
---|
153 | RenderQueue::QueueGroupIterator groupIter = rq->_getQueueGroupIterator();
|
---|
154 | while (groupIter.hasMoreElements())
|
---|
155 | {
|
---|
156 | RenderQueueGroup* g = groupIter.getNext();
|
---|
157 | g->defaultOrganisationMode();
|
---|
158 | }
|
---|
159 |
|
---|
160 | sm->_findVisibleObjects (cam, shadowcastersonly);
|
---|
161 |
|
---|
162 | setMaterialForRenderables(materialName, rq);
|
---|
163 | }
|
---|
164 |
|
---|
165 | void OgreRenderingRun::restoreMaterials()
|
---|
166 | {
|
---|
167 | std::map<Renderable*, String>::iterator it = visibleObjects.begin();
|
---|
168 | std::map<Renderable*, String>::iterator itend = visibleObjects.end();
|
---|
169 |
|
---|
170 | while(it != itend)
|
---|
171 | {
|
---|
172 | ((*it).first)->setMaterialName((*it).second);
|
---|
173 | it++;
|
---|
174 | }
|
---|
175 |
|
---|
176 | visibleObjects.clear();
|
---|
177 |
|
---|
178 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
179 | sm->setFindVisibleObjects(true);
|
---|
180 | }
|
---|
181 |
|
---|
182 | void OgreRenderingRun::renderFullscreenQuad(String materialName, RenderTarget* target)
|
---|
183 | {
|
---|
184 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
185 |
|
---|
186 | if(fullScreenQuad == NULL)
|
---|
187 | {
|
---|
188 | fullScreenQuad = new MovablePlane("PASS_FULLSCREENQUAD_PLANE");
|
---|
189 | fullScreenQuad->d = 0;
|
---|
190 | fullScreenQuad->normal = Vector3::UNIT_Z;
|
---|
191 | MeshManager::getSingleton().createPlane("PASS_FULLSCREENQUAD_MESH",
|
---|
192 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
193 | *fullScreenQuad, 2, 2,
|
---|
194 | 1, 1, true, 1, 1, 1, Vector3::UNIT_Y);
|
---|
195 | fullScreenQuadEntity = sm->createEntity( "PASS_FULLSCREENQUAD_ENTITY", "PASS_FULLSCREENQUAD_MESH" );
|
---|
196 | sm->getRootSceneNode()->attachObject(fullScreenQuadEntity);
|
---|
197 | fullScreenQuadEntity->setVisible(false);
|
---|
198 | }
|
---|
199 |
|
---|
200 | fullScreenQuadEntity->setMaterialName(materialName);
|
---|
201 | sm->getRenderQueue()->clear();
|
---|
202 | sm->getRenderQueue()->addRenderable(fullScreenQuadEntity->getSubEntity(0));
|
---|
203 | sm->setFindVisibleObjects(false);
|
---|
204 | target->update();
|
---|
205 | sm->setFindVisibleObjects(true);
|
---|
206 | }
|
---|
207 |
|
---|
208 | void OgreRenderingRun::renderPixelSprites(String& materialName, RenderTarget* rt, int width, int height)
|
---|
209 | {
|
---|
210 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
211 |
|
---|
212 | if(pixelSprites == 0)
|
---|
213 | {
|
---|
214 | int resolutionX = width;
|
---|
215 | int resolutionY = height;
|
---|
216 | pixelSprites = new SpriteSet(spriteSetName, resolutionX * resolutionY, true);
|
---|
217 | //pixelSprites->setBillboardsInWorldSpace(true);
|
---|
218 | pixelSprites->setCullIndividually(false);
|
---|
219 | //pixelSprites->setDefaultDimensions(1.0 / (float) resolutionX, 1.0 / resolutionY);
|
---|
220 | pixelSprites->beginBillboards();
|
---|
221 | for(int i = 0; i < resolutionX; i++)
|
---|
222 | {
|
---|
223 | for(int j = 0; j < resolutionY; j++)
|
---|
224 | {
|
---|
225 | Billboard bb( Vector3(
|
---|
226 | ( (float)i + 0.5 )/ (float) resolutionX, //position x
|
---|
227 | ( (float)j + 0.5 )/ (float) resolutionY, //position y
|
---|
228 | 0),
|
---|
229 | pixelSprites);
|
---|
230 | bb.setDimensions(1.0 / (float) resolutionX, 1.0 / resolutionY);
|
---|
231 | pixelSprites->injectBillboard(bb);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | pixelSprites->endBillboards();
|
---|
235 |
|
---|
236 | sm->getRootSceneNode()->attachObject(pixelSprites);
|
---|
237 |
|
---|
238 | pixelSprites->setVisible(false);
|
---|
239 | }
|
---|
240 |
|
---|
241 | pixelSprites->setMaterialName(materialName);
|
---|
242 |
|
---|
243 | sm->getRenderQueue()->clear();
|
---|
244 | sm->getRenderQueue()->addRenderable(pixelSprites);
|
---|
245 | sm->setFindVisibleObjects(false);
|
---|
246 | rt->update();
|
---|
247 | sm->setFindVisibleObjects(true);
|
---|
248 |
|
---|
249 | }
|
---|
250 |
|
---|