[1722] | 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,
|
---|
[1886] | 19 | String environmentMaterial,
|
---|
| 20 | bool getMinMax,
|
---|
[1725] | 21 | RenderingRunType cubemapRunType)
|
---|
[1722] | 22 | :CubeMapRenderingRun(startFrame, updateInterval, resolution, useDistCalc, useFaceAngleCalc, distTolerance, angleTolerance, updateAllFace, renderSelf, renderEnvironment)
|
---|
| 23 | , OgreRenderingRun(startFrame, updateInterval)
|
---|
| 24 | , RenderingRun(startFrame, updateInterval)
|
---|
| 25 |
|
---|
| 26 | {
|
---|
[1886] | 27 | this->getMinMax = getMinMax;
|
---|
[1725] | 28 | this->cubemapRunType = cubemapRunType;
|
---|
[1722] | 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 | {
|
---|
[1725] | 50 | ColourValue clearColor = ColourValue::Black;
|
---|
| 51 | clearColor.a = 0.0;
|
---|
[1722] | 52 | cubemapTexture = createCubeRenderTexture(name,
|
---|
| 53 | sharedRuns->getRootPosition(),
|
---|
| 54 | resolution,
|
---|
| 55 | PF_FLOAT16_RGBA,
|
---|
| 56 | //PF_R8G8B8A8,
|
---|
[1725] | 57 | 0,
|
---|
| 58 | clearColor);
|
---|
[1722] | 59 | }
|
---|
| 60 |
|
---|
| 61 | void OgreCubeMapRenderingRun::updateCubeFace(int facenum)
|
---|
| 62 | {
|
---|
[1725] | 63 | OgreSharedRuns* root = (OgreSharedRuns*) sharedRuns->getRoot(cubemapRunType);
|
---|
[1722] | 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();
|
---|
[1725] | 69 | Vector3 center = sharedRuns->getRootPosition(cubemapRunType);
|
---|
[1722] | 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 | setMaterialForVisibles(environmentMaterial, cam);
|
---|
| 86 | sm->_queueSkiesForRendering(cam);
|
---|
| 87 | }
|
---|
| 88 | else
|
---|
| 89 | {
|
---|
| 90 | sm->_findVisibleObjects(cam, false);
|
---|
| 91 | sm->_queueSkiesForRendering(cam);
|
---|
| 92 | sm->setFindVisibleObjects(false);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | else
|
---|
| 96 | {
|
---|
| 97 | rq->clear();
|
---|
| 98 | sm->setFindVisibleObjects(false);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | if(renderSelf)
|
---|
| 102 | {
|
---|
[1725] | 103 | root->restoreVisibility();
|
---|
[1722] | 104 | if(useSelfMaterial)
|
---|
| 105 | root->setMaterial(selfMaterial);
|
---|
| 106 | root->addRenderablesToQueue(rq);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | rt->update();
|
---|
| 113 |
|
---|
[1725] | 114 | //rt->writeContentsToFile( "cubeLayer" + StringConverter::toString(cubemapRunType)+ "face" + StringConverter::toString(facenum) + ".dds");
|
---|
[1722] | 115 |
|
---|
| 116 | if(!renderAllwithOwnColor)
|
---|
| 117 | {
|
---|
| 118 | if(renderEnvironment && useEnvMaterial)
|
---|
| 119 | restoreMaterials();
|
---|
| 120 | else
|
---|
| 121 | sm->setFindVisibleObjects(true);
|
---|
| 122 |
|
---|
| 123 | if(!renderSelf)
|
---|
| 124 | root->restoreVisibility();
|
---|
| 125 | else if(useSelfMaterial)
|
---|
| 126 | root->restoreMaterial();
|
---|
| 127 | }
|
---|
[1886] | 128 |
|
---|
| 129 | if(facenum == 5 && getMinMax)
|
---|
| 130 | getCubeMapMinMax();
|
---|
[1722] | 131 | }
|
---|
| 132 |
|
---|
[1886] | 133 | void OgreCubeMapRenderingRun::getCubeMapMinMax()
|
---|
| 134 | {
|
---|
| 135 | unsigned int buffersize = resolution * resolution * 4;
|
---|
| 136 | float* floatbuffer = new float[buffersize];
|
---|
| 137 | PixelBox lockBox(resolution, resolution, 1, PF_FLOAT32_RGBA, floatbuffer);
|
---|
| 138 |
|
---|
| 139 | float minX = 0;
|
---|
| 140 | float minY = 0;
|
---|
| 141 | float minZ = 0;
|
---|
| 142 | float minW = 0;
|
---|
| 143 | float maxX = 0;
|
---|
| 144 | float maxY = 0;
|
---|
| 145 | float maxZ = 0;
|
---|
| 146 | float maxW = 0;
|
---|
| 147 |
|
---|
| 148 | bool first = true;
|
---|
| 149 |
|
---|
| 150 | for(int iFace = 0; iFace <6 ; iFace++)
|
---|
| 151 | {
|
---|
| 152 | this->cubemapTexture->getBuffer(iFace,0)->blitToMemory(lockBox);
|
---|
| 153 |
|
---|
| 154 | for(unsigned int i = 0; i < buffersize; i+= 4)
|
---|
| 155 | {
|
---|
| 156 | float x = floatbuffer[i];
|
---|
| 157 | float y = floatbuffer[i + 1];
|
---|
| 158 | float z = floatbuffer[i + 2];
|
---|
| 159 | float w = floatbuffer[i + 3];
|
---|
| 160 |
|
---|
| 161 | if(first)
|
---|
| 162 | {
|
---|
| 163 | minX = x;
|
---|
| 164 | minY = y;
|
---|
| 165 | minZ = z;
|
---|
| 166 | minW = w;
|
---|
| 167 | maxX = x;
|
---|
| 168 | maxY = y;
|
---|
| 169 | maxZ = z;
|
---|
| 170 | maxW = w;
|
---|
| 171 |
|
---|
| 172 | first = false;
|
---|
| 173 | }
|
---|
| 174 | else if( (x * x + y * y + z * z + w * w) != 0)
|
---|
| 175 | {
|
---|
| 176 | if(x < minX)minX = x;
|
---|
| 177 | if(y < minY)minY = y;
|
---|
| 178 | if(z < minZ)minZ = z;
|
---|
| 179 | if(w < minW)minW = w;
|
---|
| 180 |
|
---|
| 181 | if(x > maxX)maxX = x;
|
---|
| 182 | if(y > maxY)maxY = y;
|
---|
| 183 | if(z > maxZ)maxZ = z;
|
---|
| 184 | if(w > maxW)maxW = w;
|
---|
| 185 |
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | min.x = minX; min.y = minY; min.z = minZ; min.w = minW;
|
---|
| 191 | max.x = maxX; max.y = maxY; max.z = maxZ; max.w = maxW;
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | delete[] floatbuffer;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[1722] | 197 | bool OgreCubeMapRenderingRun::faceNeedsUpdate(int facenum)
|
---|
| 198 | {
|
---|
| 199 | if(useDistCalc || useFaceAngleCalc)
|
---|
| 200 | {
|
---|
| 201 | float chance1 = 1.0;
|
---|
| 202 | float chance2 = 1.0;
|
---|
| 203 |
|
---|
| 204 | Camera* mainCamera = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
| 205 |
|
---|
| 206 | if(useDistCalc)
|
---|
| 207 | {
|
---|
[1725] | 208 | Vector3 cubemapPosition = sharedRuns->getRootPosition(cubemapRunType);
|
---|
| 209 | float objradius = sharedRuns->getRootBoundingSphere(cubemapRunType).getRadius();
|
---|
[1722] | 210 | float fov = mainCamera->getFOVy().valueRadians() / 2.0;
|
---|
| 211 | float dist = (cubemapPosition - mainCamera->getPosition()).length();
|
---|
| 212 | float vangle = Math::ASin( objradius / dist).valueRadians();
|
---|
| 213 | float angleratio = vangle / fov;
|
---|
| 214 | chance1 = Math::Pow(angleratio, 1.0 / distTolerance);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | if(useFaceAngleCalc)
|
---|
| 218 | {
|
---|
| 219 | Vector3 faceDir = getCubeMapFaceDirection(facenum);
|
---|
| 220 | Vector3 cameraDir = mainCamera->getDirection();
|
---|
| 221 | float angle = faceDir.dotProduct(-1 * cameraDir);
|
---|
| 222 | float facingforward = (angle + 1) / 2.0;
|
---|
| 223 | chance2 = Math::Pow(facingforward, 1.0 / angleTolerance);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | float dice = Math::UnitRandom();
|
---|
| 227 | float chance = chance1 * chance2;
|
---|
| 228 |
|
---|
| 229 | if(dice < chance)
|
---|
| 230 | {
|
---|
| 231 | return true;
|
---|
| 232 | }
|
---|
| 233 | else
|
---|
| 234 | {
|
---|
| 235 | return false;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | return true;
|
---|
| 240 | }
|
---|