1 | #include "OgreDistanceCubeMapRenderingRun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 | OgreDistanceCubeMapRenderingRun::OgreDistanceCubeMapRenderingRun(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 | :DistanceCubeMapRenderingRun(startFrame, updateInterval, resolution, useDistCalc, useFaceAngleCalc, distTolerance, angleTolerance, updateAllFace)
|
---|
17 | , OgreRenderingRun(startFrame, updateInterval)
|
---|
18 | , RenderingRun(startFrame, updateInterval)
|
---|
19 | {
|
---|
20 | this->sharedRuns = sharedRuns;
|
---|
21 | this->name = name;
|
---|
22 |
|
---|
23 | createDistanceCubeMap();
|
---|
24 | }
|
---|
25 |
|
---|
26 | void OgreDistanceCubeMapRenderingRun::createDistanceCubeMap()
|
---|
27 | {
|
---|
28 | distanceCubemapTexture = createCubeRenderTexture(name,
|
---|
29 | sharedRuns->getRootPosition(),
|
---|
30 | resolution,
|
---|
31 | PF_FLOAT32_RGBA);
|
---|
32 | }
|
---|
33 |
|
---|
34 | void OgreDistanceCubeMapRenderingRun::updateCubeFace(int facenum)
|
---|
35 | {
|
---|
36 | sharedRuns->getRoot(ILLUMRUN_DISTANCE_CUBEMAP)->hide();
|
---|
37 |
|
---|
38 | RenderTarget* rt = distanceCubemapTexture->getBuffer(facenum, 0).getPointer()
|
---|
39 | ->getRenderTarget();
|
---|
40 |
|
---|
41 | Vector3 center = sharedRuns->getRootPosition(ILLUMRUN_DISTANCE_CUBEMAP);
|
---|
42 | Camera* cam = rt->getViewport(0)->getCamera();
|
---|
43 | cam->setPosition(center);
|
---|
44 |
|
---|
45 |
|
---|
46 | setMaterialForVisibles(String("GameTools/DistanceShader"), cam);
|
---|
47 |
|
---|
48 | rt->update();
|
---|
49 |
|
---|
50 | restoreMaterials();
|
---|
51 |
|
---|
52 | //rt->writeContentsToFile("cubbe" + StringConverter::toString(facenum) + ".dds");
|
---|
53 |
|
---|
54 | sharedRuns->getRoot(ILLUMRUN_DISTANCE_CUBEMAP)->restoreVisibility();
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool OgreDistanceCubeMapRenderingRun::faceNeedsUpdate(int facenum)
|
---|
58 | {
|
---|
59 | if(useDistCalc || useFaceAngleCalc)
|
---|
60 | {
|
---|
61 | float chance1 = 1.0;
|
---|
62 | float chance2 = 1.0;
|
---|
63 |
|
---|
64 | Camera* mainCamera = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
65 |
|
---|
66 | if(useDistCalc)
|
---|
67 | {
|
---|
68 | Vector3 cubemapPosition = sharedRuns->getRootPosition(ILLUMRUN_DISTANCE_CUBEMAP);
|
---|
69 | float objradius = sharedRuns->getRootBoundingSphere(ILLUMRUN_DISTANCE_CUBEMAP).getRadius();
|
---|
70 | float fov = mainCamera->getFOVy().valueRadians() / 2.0;
|
---|
71 | float dist = (cubemapPosition - mainCamera->getPosition()).length();
|
---|
72 | float vangle = Math::ASin( objradius / dist).valueRadians();
|
---|
73 | float angleratio = vangle / fov;
|
---|
74 | chance1 = Math::Pow(angleratio, 1.0 / distTolerance);
|
---|
75 | }
|
---|
76 |
|
---|
77 | if(useFaceAngleCalc)
|
---|
78 | {
|
---|
79 | Vector3 faceDir = getCubeMapFaceDirection(facenum);
|
---|
80 | Vector3 cameraDir = mainCamera->getDirection();
|
---|
81 | float angle = faceDir.dotProduct(-1 * cameraDir);
|
---|
82 | float facingforward = (angle + 1) / 2.0;
|
---|
83 | chance2 = Math::Pow(facingforward, 1.0 / angleTolerance);
|
---|
84 | }
|
---|
85 |
|
---|
86 | float dice = Math::UnitRandom();
|
---|
87 | float chance = chance1 * chance2;
|
---|
88 |
|
---|
89 | if(dice < chance)
|
---|
90 | {
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | return true;
|
---|
100 | }
|
---|