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