1 | #include "OgreChildPSystemRenderingRun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 | #include "OgreDistanceCubeMapRenderingRun.h"
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 | OgreChildPSystemRenderingRun::OgreChildPSystemRenderingRun(OgreSharedRuns* sharedRuns,
|
---|
8 | String name,
|
---|
9 | unsigned long startFrame,
|
---|
10 | unsigned long updateInterval,
|
---|
11 | unsigned int resolution,
|
---|
12 | bool perspectiveRendering,
|
---|
13 | String childParticleSystemName,
|
---|
14 | String particleScriptName,
|
---|
15 | bool useOwnMaterial,
|
---|
16 | String materialName)
|
---|
17 | :ChildPsystemRenderingRun(resolution, perspectiveRendering, startFrame, updateInterval)
|
---|
18 | , OgreRenderingRun(startFrame, updateInterval)
|
---|
19 | , RenderingRun(startFrame, updateInterval)
|
---|
20 | {
|
---|
21 | this->sharedRuns = sharedRuns;
|
---|
22 | this->name = name;
|
---|
23 | this->resolution = resolution;
|
---|
24 | this->materialName = materialName;
|
---|
25 | this->particleScriptName = particleScriptName;
|
---|
26 | this->perspectiveRendering = perspectiveRendering;
|
---|
27 | this->childParticleSystemName = childParticleSystemName;
|
---|
28 | this->useOwnMaterial = useOwnMaterial;
|
---|
29 | createImpostorTexture();
|
---|
30 | }
|
---|
31 |
|
---|
32 | void OgreChildPSystemRenderingRun::createImpostorTexture()
|
---|
33 | {
|
---|
34 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name,
|
---|
35 | "default",
|
---|
36 | TEX_TYPE_2D,
|
---|
37 | resolution,
|
---|
38 | resolution,
|
---|
39 | 0,
|
---|
40 | 0,
|
---|
41 | PF_FLOAT16_RGBA,
|
---|
42 | TU_RENDERTARGET);
|
---|
43 | impostorTexture = texPtr.getPointer();
|
---|
44 | impostorCamera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
|
---|
45 | //add viewport to rendertarget
|
---|
46 | HardwarePixelBuffer* hpb = (impostorTexture->getBuffer()).getPointer();
|
---|
47 | RenderTarget* rt = hpb->getRenderTarget();
|
---|
48 | Viewport* v = rt->addViewport(impostorCamera);
|
---|
49 | v->setBackgroundColour(ColourValue(0,1,0,0));
|
---|
50 | v->setOverlaysEnabled(false);
|
---|
51 | rt->setAutoUpdated(false);
|
---|
52 | }
|
---|
53 |
|
---|
54 | void OgreChildPSystemRenderingRun::updateFrame(unsigned long frameNum)
|
---|
55 | {
|
---|
56 |
|
---|
57 | Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
|
---|
58 |
|
---|
59 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
60 | RenderQueue* rq = sm->getRenderQueue();
|
---|
61 | rq->clear();
|
---|
62 |
|
---|
63 | sharedRuns->getRoot()->updateBounds();
|
---|
64 | Sphere boundSphere = ((OgreSharedRuns*)sharedRuns->getRoot(ILLUMRUN_HPP_IMPOSTOR))->getBoundingSphere();
|
---|
65 | Camera* mainCam = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
66 | impostorCamera->setPosition(mainCam->getPosition());
|
---|
67 | Vector3 dir = boundSphere.getCenter() - mainCam->getPosition();
|
---|
68 | float r = boundSphere.getRadius();
|
---|
69 | r = 200;
|
---|
70 | float d = dir.normalise();
|
---|
71 | impostorCamera->setDirection(dir);
|
---|
72 |
|
---|
73 | if(perspectiveRendering)
|
---|
74 | {
|
---|
75 | impostorCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
76 | impostorCamera->setAspectRatio(1.0);
|
---|
77 | Radian a = Math::ASin(r / d) * 2.0;
|
---|
78 | impostorCamera->setFOVy(/*a*/ Radian(Degree(90)));
|
---|
79 | impostorCamera->setNearClipDistance(/*std::max(0.01f, d - r)*/0.1);
|
---|
80 | impostorCamera->setFarClipDistance(/*r + d*/1000);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | impostorCamera->setPosition(boundSphere.getCenter());
|
---|
85 | impostorCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
86 | Matrix4 m;
|
---|
87 | m = Matrix4::IDENTITY;
|
---|
88 | float scale = 1.0 / boundSphere.getRadius();
|
---|
89 | m.setScale(Vector3(scale, scale, scale));
|
---|
90 | impostorCamera->setCustomProjectionMatrix(true, m);
|
---|
91 | }
|
---|
92 |
|
---|
93 | ParticleSystem* psys = Root::getSingleton()._getCurrentSceneManager()->getParticleSystem(childParticleSystemName);
|
---|
94 | psys->getParentSceneNode()->setPosition(boundSphere.getCenter());
|
---|
95 | psys->_notifyCurrentCamera(impostorCamera);
|
---|
96 | psys->_updateRenderQueue(rq);
|
---|
97 |
|
---|
98 | if(!useOwnMaterial)
|
---|
99 | setMaterialForRenderables(materialName,rq);
|
---|
100 |
|
---|
101 | RenderTarget* rt = impostorTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
102 |
|
---|
103 | rt->update();
|
---|
104 |
|
---|
105 |
|
---|
106 | static int framecount = 0;
|
---|
107 | String filename = "impostor";
|
---|
108 | filename.append(this->name);
|
---|
109 | //filename.append(StringConverter::toString(framecount));
|
---|
110 | filename.append(".dds");
|
---|
111 | rt->writeContentsToFile(filename);
|
---|
112 | framecount++;
|
---|
113 |
|
---|
114 | if(!useOwnMaterial)
|
---|
115 | restoreMaterials();
|
---|
116 | }
|
---|
117 |
|
---|