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 | RenderingRunType cubemapRunType)
|
---|
21 | :CubeMapRenderingRun(startFrame, updateInterval, resolution, useDistCalc, useFaceAngleCalc, distTolerance, angleTolerance, updateAllFace, renderSelf, renderEnvironment)
|
---|
22 | , OgreRenderingRun(startFrame, updateInterval)
|
---|
23 | , RenderingRun(startFrame, updateInterval)
|
---|
24 |
|
---|
25 | {
|
---|
26 | this->cubemapRunType = cubemapRunType;
|
---|
27 | this->sharedRuns = sharedRuns;
|
---|
28 | this->name = name;
|
---|
29 | this->selfMaterial = selfMaterial;
|
---|
30 | this->environmentMaterial = environmentMaterial;
|
---|
31 |
|
---|
32 | if(environmentMaterial == "")
|
---|
33 | useEnvMaterial = false;
|
---|
34 | else
|
---|
35 | useEnvMaterial = true;
|
---|
36 |
|
---|
37 | if(selfMaterial == "")
|
---|
38 | useSelfMaterial = false;
|
---|
39 | else
|
---|
40 | useSelfMaterial = true;
|
---|
41 |
|
---|
42 |
|
---|
43 | createCubeMap();
|
---|
44 | }
|
---|
45 |
|
---|
46 | void OgreCubeMapRenderingRun::createCubeMap()
|
---|
47 | {
|
---|
48 | ColourValue clearColor = ColourValue::Black;
|
---|
49 | clearColor.a = 0.0;
|
---|
50 | cubemapTexture = createCubeRenderTexture(name,
|
---|
51 | sharedRuns->getRootPosition(),
|
---|
52 | resolution,
|
---|
53 | PF_FLOAT16_RGBA,
|
---|
54 | //PF_R8G8B8A8,
|
---|
55 | 0,
|
---|
56 | clearColor);
|
---|
57 | }
|
---|
58 |
|
---|
59 | void OgreCubeMapRenderingRun::updateCubeFace(int facenum)
|
---|
60 | {
|
---|
61 | OgreSharedRuns* root = (OgreSharedRuns*) sharedRuns->getRoot(cubemapRunType);
|
---|
62 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
63 | RenderQueue* rq = sm->getRenderQueue();
|
---|
64 |
|
---|
65 | RenderTarget* rt = cubemapTexture->getBuffer(facenum, 0).getPointer()->getRenderTarget();
|
---|
66 | Camera* cam = rt->getViewport(0)->getCamera();
|
---|
67 | Vector3 center = sharedRuns->getRootPosition(cubemapRunType);
|
---|
68 | cam->setPosition(center);
|
---|
69 |
|
---|
70 | bool renderAllwithOwnColor = false;
|
---|
71 |
|
---|
72 | if(renderSelf && renderEnvironment && !useSelfMaterial && !useEnvMaterial)
|
---|
73 | renderAllwithOwnColor = true;
|
---|
74 |
|
---|
75 | if(!renderAllwithOwnColor)
|
---|
76 | {
|
---|
77 | root->hide();
|
---|
78 |
|
---|
79 | if(renderEnvironment)
|
---|
80 | {
|
---|
81 | if(useEnvMaterial)
|
---|
82 | {
|
---|
83 | setMaterialForVisibles(environmentMaterial, cam);
|
---|
84 | sm->_queueSkiesForRendering(cam);
|
---|
85 | }
|
---|
86 | else
|
---|
87 | {
|
---|
88 | sm->_findVisibleObjects(cam, false);
|
---|
89 | sm->_queueSkiesForRendering(cam);
|
---|
90 | sm->setFindVisibleObjects(false);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | rq->clear();
|
---|
96 | sm->setFindVisibleObjects(false);
|
---|
97 | }
|
---|
98 |
|
---|
99 | if(renderSelf)
|
---|
100 | {
|
---|
101 | root->restoreVisibility();
|
---|
102 | if(useSelfMaterial)
|
---|
103 | root->setMaterial(selfMaterial);
|
---|
104 | root->addRenderablesToQueue(rq);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | rt->update();
|
---|
111 |
|
---|
112 | //rt->writeContentsToFile( "cubeLayer" + StringConverter::toString(cubemapRunType)+ "face" + StringConverter::toString(facenum) + ".dds");
|
---|
113 |
|
---|
114 | if(!renderAllwithOwnColor)
|
---|
115 | {
|
---|
116 | if(renderEnvironment && useEnvMaterial)
|
---|
117 | restoreMaterials();
|
---|
118 | else
|
---|
119 | sm->setFindVisibleObjects(true);
|
---|
120 |
|
---|
121 | if(!renderSelf)
|
---|
122 | root->restoreVisibility();
|
---|
123 | else if(useSelfMaterial)
|
---|
124 | root->restoreMaterial();
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool OgreCubeMapRenderingRun::faceNeedsUpdate(int facenum)
|
---|
129 | {
|
---|
130 | if(useDistCalc || useFaceAngleCalc)
|
---|
131 | {
|
---|
132 | float chance1 = 1.0;
|
---|
133 | float chance2 = 1.0;
|
---|
134 |
|
---|
135 | Camera* mainCamera = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
136 |
|
---|
137 | if(useDistCalc)
|
---|
138 | {
|
---|
139 | Vector3 cubemapPosition = sharedRuns->getRootPosition(cubemapRunType);
|
---|
140 | float objradius = sharedRuns->getRootBoundingSphere(cubemapRunType).getRadius();
|
---|
141 | float fov = mainCamera->getFOVy().valueRadians() / 2.0;
|
---|
142 | float dist = (cubemapPosition - mainCamera->getPosition()).length();
|
---|
143 | float vangle = Math::ASin( objradius / dist).valueRadians();
|
---|
144 | float angleratio = vangle / fov;
|
---|
145 | chance1 = Math::Pow(angleratio, 1.0 / distTolerance);
|
---|
146 | }
|
---|
147 |
|
---|
148 | if(useFaceAngleCalc)
|
---|
149 | {
|
---|
150 | Vector3 faceDir = getCubeMapFaceDirection(facenum);
|
---|
151 | Vector3 cameraDir = mainCamera->getDirection();
|
---|
152 | float angle = faceDir.dotProduct(-1 * cameraDir);
|
---|
153 | float facingforward = (angle + 1) / 2.0;
|
---|
154 | chance2 = Math::Pow(facingforward, 1.0 / angleTolerance);
|
---|
155 | }
|
---|
156 |
|
---|
157 | float dice = Math::UnitRandom();
|
---|
158 | float chance = chance1 * chance2;
|
---|
159 |
|
---|
160 | if(dice < chance)
|
---|
161 | {
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return true;
|
---|
171 | }
|
---|