1 | #include "OgreCubeMapRenderingRun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 | OgreCubeMapRenderingRun::OgreCubeMapRenderingRun(OgreSharedRuns* sharedRuns,
|
---|
7 | String name,
|
---|
8 | unsigned long startFrame,
|
---|
9 | unsigned long updateInterval,
|
---|
10 | unsigned int resolution,
|
---|
11 | bool useDistCalc,
|
---|
12 | bool useFaceAngleCalc,
|
---|
13 | float distTolerance,
|
---|
14 | float angleTolerance,
|
---|
15 | bool updateAllFace,
|
---|
16 | bool renderSelf,
|
---|
17 | bool renderEnvironment,
|
---|
18 | String selfMaterial,
|
---|
19 | String environmentMaterial,
|
---|
20 | String envTriggerName,
|
---|
21 | bool getMinMax,
|
---|
22 | RenderingRunType cubemapRunType)
|
---|
23 | :CubeMapRenderingRun(startFrame, updateInterval, resolution, useDistCalc, useFaceAngleCalc, distTolerance, angleTolerance, updateAllFace, renderSelf, renderEnvironment)
|
---|
24 | , OgreRenderingRun(startFrame, updateInterval)
|
---|
25 | , RenderingRun(startFrame, updateInterval)
|
---|
26 |
|
---|
27 | {
|
---|
28 | this->envTriggerName = envTriggerName;
|
---|
29 | this->getMinMax = getMinMax;
|
---|
30 | this->cubemapRunType = cubemapRunType;
|
---|
31 | this->sharedRuns = sharedRuns;
|
---|
32 | this->name = name;
|
---|
33 | this->selfMaterial = selfMaterial;
|
---|
34 | this->environmentMaterial = environmentMaterial;
|
---|
35 |
|
---|
36 | if(environmentMaterial == "")
|
---|
37 | useEnvMaterial = false;
|
---|
38 | else
|
---|
39 | useEnvMaterial = true;
|
---|
40 |
|
---|
41 | if(selfMaterial == "")
|
---|
42 | useSelfMaterial = false;
|
---|
43 | else
|
---|
44 | useSelfMaterial = true;
|
---|
45 |
|
---|
46 |
|
---|
47 | createCubeMap();
|
---|
48 | }
|
---|
49 |
|
---|
50 | void OgreCubeMapRenderingRun::createCubeMap()
|
---|
51 | {
|
---|
52 | ColourValue clearColor = ColourValue::Black;
|
---|
53 | clearColor.a = 0.0;
|
---|
54 | cubemapTexture = createCubeRenderTexture(name,
|
---|
55 | sharedRuns->getRootPosition(),
|
---|
56 | resolution,
|
---|
57 | PF_FLOAT16_RGBA,
|
---|
58 | //PF_R8G8B8A8,
|
---|
59 | 0,
|
---|
60 | clearColor);
|
---|
61 | }
|
---|
62 |
|
---|
63 | void OgreCubeMapRenderingRun::updateCubeFace(int facenum)
|
---|
64 | {
|
---|
65 | OgreSharedRuns* root = (OgreSharedRuns*) sharedRuns->getRoot(cubemapRunType);
|
---|
66 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
67 | RenderQueue* rq = sm->getRenderQueue();
|
---|
68 |
|
---|
69 | RenderTarget* rt = cubemapTexture->getBuffer(facenum, 0).getPointer()->getRenderTarget();
|
---|
70 | Camera* cam = rt->getViewport(0)->getCamera();
|
---|
71 | Vector3 center = sharedRuns->getRootPosition(cubemapRunType);
|
---|
72 | cam->setPosition(center);
|
---|
73 |
|
---|
74 | bool renderAllwithOwnColor = false;
|
---|
75 |
|
---|
76 | if(renderSelf && renderEnvironment && !useSelfMaterial && !useEnvMaterial)
|
---|
77 | renderAllwithOwnColor = true;
|
---|
78 |
|
---|
79 | if(!renderAllwithOwnColor)
|
---|
80 | {
|
---|
81 | root->hide();
|
---|
82 |
|
---|
83 | if(renderEnvironment)
|
---|
84 | {
|
---|
85 | if(useEnvMaterial)
|
---|
86 | {
|
---|
87 | setMaterialForVisibles(environmentMaterial, cam, false, false, envTriggerName);
|
---|
88 | //sm->_queueSkiesForRendering(cam);
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | //sm->_findVisibleObjectsOC(cam, rt->getViewport(0), false, false, false);
|
---|
93 | sm->_findVisibleObjects(cam, false);
|
---|
94 | sm->_queueSkiesForRendering(cam);
|
---|
95 | sm->setFindVisibleObjects(false);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | rq->clear();
|
---|
101 | sm->setFindVisibleObjects(false);
|
---|
102 | }
|
---|
103 |
|
---|
104 | if(renderSelf)
|
---|
105 | {
|
---|
106 | root->restoreVisibility();
|
---|
107 | if(useSelfMaterial)
|
---|
108 | root->setMaterial(selfMaterial);
|
---|
109 | root->addRenderablesToQueue(rq);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 | rt->update();
|
---|
116 |
|
---|
117 | //rt->writeContentsToFile( "cubeLayer" + StringConverter::toString(cubemapRunType)+ "face" + StringConverter::toString(facenum) + ".dds");
|
---|
118 |
|
---|
119 | if(!renderAllwithOwnColor)
|
---|
120 | {
|
---|
121 | if(renderEnvironment && useEnvMaterial)
|
---|
122 | restoreMaterials();
|
---|
123 | else
|
---|
124 | sm->setFindVisibleObjects(true);
|
---|
125 |
|
---|
126 | if(!renderSelf)
|
---|
127 | root->restoreVisibility();
|
---|
128 | else if(useSelfMaterial)
|
---|
129 | root->restoreMaterial();
|
---|
130 | }
|
---|
131 |
|
---|
132 | if(facenum == 5 && getMinMax)
|
---|
133 | {
|
---|
134 | getCubeMapMinMax();
|
---|
135 | this->sharedRuns->runUpdated(cubemapRunType, this);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | void OgreCubeMapRenderingRun::getCubeMapMinMax()
|
---|
140 | {
|
---|
141 | unsigned int buffersize = resolution * resolution * 4;
|
---|
142 | float* floatbuffer = new float[buffersize];
|
---|
143 | PixelBox lockBox(resolution, resolution, 1, PF_FLOAT32_RGBA, floatbuffer);
|
---|
144 |
|
---|
145 | float minX = 0;
|
---|
146 | float minY = 0;
|
---|
147 | float minZ = 0;
|
---|
148 | float minW = 0;
|
---|
149 | float maxX = 0;
|
---|
150 | float maxY = 0;
|
---|
151 | float maxZ = 0;
|
---|
152 | float maxW = 0;
|
---|
153 |
|
---|
154 | bool first = true;
|
---|
155 |
|
---|
156 | for(int iFace = 0; iFace <6 ; iFace++)
|
---|
157 | {
|
---|
158 | this->cubemapTexture->getBuffer(iFace,0)->blitToMemory(lockBox);
|
---|
159 |
|
---|
160 | for(unsigned int i = 0; i < buffersize; i+= 4)
|
---|
161 | {
|
---|
162 | float x = floatbuffer[i];
|
---|
163 | float y = floatbuffer[i + 1];
|
---|
164 | float z = floatbuffer[i + 2];
|
---|
165 | float w = floatbuffer[i + 3];
|
---|
166 |
|
---|
167 | if(first && w != 0)
|
---|
168 | {
|
---|
169 | minX = x;
|
---|
170 | minY = y;
|
---|
171 | minZ = z;
|
---|
172 | minW = w;
|
---|
173 | maxX = x;
|
---|
174 | maxY = y;
|
---|
175 | maxZ = z;
|
---|
176 | maxW = w;
|
---|
177 |
|
---|
178 | first = false;
|
---|
179 | }
|
---|
180 | else if( /*(x * x + y * y + z * z + w * w) != 0*/ w != 0)
|
---|
181 | {
|
---|
182 | if(x < minX)minX = x;
|
---|
183 | if(y < minY)minY = y;
|
---|
184 | if(z < minZ)minZ = z;
|
---|
185 | if(w < minW)minW = w;
|
---|
186 |
|
---|
187 | if(x > maxX)maxX = x;
|
---|
188 | if(y > maxY)maxY = y;
|
---|
189 | if(z > maxZ)maxZ = z;
|
---|
190 | if(w > maxW)maxW = w;
|
---|
191 |
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | min.x = minX; min.y = minY; min.z = minZ; min.w = minW;
|
---|
197 | max.x = maxX; max.y = maxY; max.z = maxZ; max.w = maxW;
|
---|
198 |
|
---|
199 |
|
---|
200 | delete[] floatbuffer;
|
---|
201 | }
|
---|
202 |
|
---|
203 | bool OgreCubeMapRenderingRun::faceNeedsUpdate(int facenum)
|
---|
204 | {
|
---|
205 | if(useDistCalc || useFaceAngleCalc)
|
---|
206 | {
|
---|
207 | float chance1 = 1.0;
|
---|
208 | float chance2 = 1.0;
|
---|
209 |
|
---|
210 | Camera* mainCamera = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
211 |
|
---|
212 | if(useDistCalc)
|
---|
213 | {
|
---|
214 | Vector3 cubemapPosition = sharedRuns->getRootPosition(cubemapRunType);
|
---|
215 | float objradius = sharedRuns->getRootBoundingSphere(cubemapRunType).getRadius();
|
---|
216 | float fov = mainCamera->getFOVy().valueRadians() / 2.0;
|
---|
217 | float dist = (cubemapPosition - mainCamera->getPosition()).length();
|
---|
218 | float vangle = Math::ASin( objradius / dist).valueRadians();
|
---|
219 | float angleratio = vangle / fov;
|
---|
220 | chance1 = Math::Pow(angleratio, 1.0 / distTolerance);
|
---|
221 | }
|
---|
222 |
|
---|
223 | if(useFaceAngleCalc)
|
---|
224 | {
|
---|
225 | Vector3 faceDir = getCubeMapFaceDirection(facenum);
|
---|
226 | Vector3 cameraDir = mainCamera->getDirection();
|
---|
227 | float angle = faceDir.dotProduct(-1 * cameraDir);
|
---|
228 | float facingforward = (angle + 1) / 2.0;
|
---|
229 | chance2 = Math::Pow(facingforward, 1.0 / angleTolerance);
|
---|
230 | }
|
---|
231 |
|
---|
232 | float dice = Math::UnitRandom();
|
---|
233 | float chance = chance1 * chance2;
|
---|
234 |
|
---|
235 | if(dice < chance)
|
---|
236 | {
|
---|
237 | return true;
|
---|
238 | }
|
---|
239 | else
|
---|
240 | {
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | return true;
|
---|
246 | }
|
---|
247 |
|
---|
248 | void OgreCubeMapRenderingRun::freeAllResources()
|
---|
249 | {
|
---|
250 | this->cubemapTexture = 0;
|
---|
251 | TextureManager::getSingleton().remove(name);
|
---|
252 | Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA");
|
---|
253 | } |
---|