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 | Vector3 dir = mainCam->getDirection();
|
---|
77 | float d = dir.normalise();
|
---|
78 |
|
---|
79 | if (d <= r * 1.2)//too close
|
---|
80 | {
|
---|
81 | d = r * 1.2;
|
---|
82 | impostorCamera->setPosition(dir * -d);
|
---|
83 | }
|
---|
84 |
|
---|
85 | impostorCamera->setDirection(dir);
|
---|
86 |
|
---|
87 | //r = 200;
|
---|
88 |
|
---|
89 | if(perspectiveRendering)
|
---|
90 | {
|
---|
91 | impostorCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
92 | impostorCamera->setAspectRatio(1.0);
|
---|
93 | Radian a = Math::ASin(r / d) * 2.0;
|
---|
94 | impostorCamera->setFOVy( a );
|
---|
95 | impostorCamera->setNearClipDistance(d - r);
|
---|
96 | impostorCamera->setFarClipDistance(d + r);
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | impostorCamera->setPosition(Vector3(0,0,0));
|
---|
101 | impostorCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
102 | Matrix4 m;
|
---|
103 | m = Matrix4::IDENTITY;
|
---|
104 | float scale = 1.0 / r;
|
---|
105 | m.setScale(Vector3(scale, scale, scale));
|
---|
106 | impostorCamera->setCustomProjectionMatrix(true, m);
|
---|
107 | }
|
---|
108 |
|
---|
109 | //
|
---|
110 | psys->_notifyCurrentCamera(impostorCamera);
|
---|
111 | psys->_updateRenderQueue(rq);
|
---|
112 |
|
---|
113 | if(!useOwnMaterial)
|
---|
114 | setMaterialForRenderables(materialName,rq);
|
---|
115 |
|
---|
116 | RenderTarget* rt = impostorTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
117 | sm->setFindVisibleObjects(false);
|
---|
118 | rt->update();
|
---|
119 | sm->setFindVisibleObjects(true);
|
---|
120 |
|
---|
121 |
|
---|
122 | static int framecount = 0;
|
---|
123 | String filename = "impostor_";
|
---|
124 | filename.append(this->name);
|
---|
125 | //filename.append(StringConverter::toString(framecount));
|
---|
126 | filename.append(".bmp");
|
---|
127 | //rt->writeContentsToFile(filename);
|
---|
128 | framecount++;
|
---|
129 |
|
---|
130 | if(!useOwnMaterial)
|
---|
131 | restoreMaterials();
|
---|
132 | }
|
---|
133 |
|
---|