1 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | OgreDepthShadowMapRenderingRun::OgreDepthShadowMapRenderingRun(OgreSharedRuns* sharedRuns,
|
---|
5 | String name,
|
---|
6 | Light* light,
|
---|
7 | unsigned int resolutionX,
|
---|
8 | unsigned int resolutionY,
|
---|
9 | String materialName
|
---|
10 | )
|
---|
11 | :DepthShadowMapRenderingRun(resolutionX, resolutionY)
|
---|
12 | , OgreRenderingRun(1, 1)
|
---|
13 | , RenderingRun(1, 1)
|
---|
14 | {
|
---|
15 | this->light = light;
|
---|
16 | this->sharedRuns = sharedRuns;
|
---|
17 | this->name = name;
|
---|
18 | this->materialName = materialName;
|
---|
19 |
|
---|
20 | createDepthMap();
|
---|
21 | }
|
---|
22 |
|
---|
23 | void OgreDepthShadowMapRenderingRun::createDepthMap()
|
---|
24 | {
|
---|
25 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name,
|
---|
26 | "default",
|
---|
27 | TEX_TYPE_2D,
|
---|
28 | resolutionX,
|
---|
29 | resolutionY,
|
---|
30 | 0,
|
---|
31 | 0,
|
---|
32 | PF_FLOAT16_RGBA,
|
---|
33 | TU_RENDERTARGET);
|
---|
34 | depthMapTexture = texPtr.getPointer();
|
---|
35 | depthMapCamera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
|
---|
36 | //add viewport to rendertarget
|
---|
37 | HardwarePixelBuffer* hpb = (depthMapTexture->getBuffer()).getPointer();
|
---|
38 | RenderTarget* rt = hpb->getRenderTarget();
|
---|
39 | Viewport* v = rt->addViewport(depthMapCamera);
|
---|
40 | v->setOverlaysEnabled(false);
|
---|
41 | rt->setAutoUpdated(false);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum)
|
---|
45 | {
|
---|
46 | refreshLight();
|
---|
47 |
|
---|
48 | if(light->getType() == Light::LT_POINT)
|
---|
49 | {
|
---|
50 | updateDepthMap();
|
---|
51 | }
|
---|
52 | else
|
---|
53 | {
|
---|
54 | updateDepthMap();
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | void OgreDepthShadowMapRenderingRun::updateDepthCubeFace(int facenum)
|
---|
61 | {
|
---|
62 | }
|
---|
63 |
|
---|
64 | void OgreDepthShadowMapRenderingRun::updateDepthMap()
|
---|
65 | {
|
---|
66 | setMaterialForVisibles(materialName, depthMapCamera, true);
|
---|
67 |
|
---|
68 | RenderTarget* rt = depthMapTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
69 |
|
---|
70 | SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
|
---|
71 |
|
---|
72 | rt->update();
|
---|
73 |
|
---|
74 | //rt->writeContentsToFile("shadowmap.dds");
|
---|
75 |
|
---|
76 | restoreMaterials();
|
---|
77 | }
|
---|
78 |
|
---|
79 | void OgreDepthShadowMapRenderingRun::refreshLight()
|
---|
80 | {
|
---|
81 |
|
---|
82 | if(light!= 0)
|
---|
83 | {
|
---|
84 | if(light->getType() == Light::LT_DIRECTIONAL)
|
---|
85 | {
|
---|
86 | Vector3 dir = light->getDirection();
|
---|
87 | depthMapCamera->setDirection( dir );
|
---|
88 | //TODO: where to put light
|
---|
89 | depthMapCamera->setPosition( - dir * 100000 );
|
---|
90 | depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
91 | }
|
---|
92 | else if(light->getType() == Light::LT_SPOTLIGHT)
|
---|
93 | {
|
---|
94 | Vector3 dir = light->getDirection();
|
---|
95 | depthMapCamera->setDirection( dir );
|
---|
96 | Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
97 | depthMapCamera->setPosition(pos);
|
---|
98 | depthMapCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
99 | depthMapCamera->setFOVy(light->getSpotlightOuterAngle());
|
---|
100 | }
|
---|
101 | else//point light
|
---|
102 | {
|
---|
103 | Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
104 | Vector3 dir = -pos;
|
---|
105 | depthMapCamera->setDirection( dir );
|
---|
106 | depthMapCamera->setPosition(pos);
|
---|
107 | depthMapCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
108 | depthMapCamera->setFOVy(Radian(Degree(140)));
|
---|
109 | depthMapCamera->setNearClipDistance(1);
|
---|
110 | // depthMapCamera->setFarClipDistance(200);
|
---|
111 |
|
---|
112 | //OGRE_EXCEPT(0, "NOT implemented for Pointlight", "OgreDepthShadowMapRenderingRun::refreshLight");
|
---|
113 |
|
---|
114 | /*Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
115 | Vector3 dir = -pos;
|
---|
116 | depthMapCamera->setDirection( dir );
|
---|
117 | depthMapCamera->setPosition(pos);
|
---|
118 | depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
119 | depthMapCamera->*/
|
---|
120 |
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix()
|
---|
126 | {
|
---|
127 | return depthMapCamera->getViewMatrix();
|
---|
128 | }
|
---|
129 |
|
---|
130 | Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix()
|
---|
131 | {
|
---|
132 | return depthMapCamera->getProjectionMatrix() * depthMapCamera->getViewMatrix();
|
---|
133 | }
|
---|
134 |
|
---|