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