1 | #include "Ogre.h"
|
---|
2 | #include <vector>
|
---|
3 | #include <cmath>
|
---|
4 | #include "OgreEffectWrapper.h"
|
---|
5 | #include "ManagedOgreRenderTexturePass.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | const char* ManagedOgreRenderTexturePass::noRenderTargetName="NO_RENDER_TEXTURE";
|
---|
9 | MovablePlane* ManagedOgreRenderTexturePass::fullScreenQuad=NULL;
|
---|
10 | Entity* ManagedOgreRenderTexturePass::fullScreenQuadEntity=NULL;
|
---|
11 | SceneNode* ManagedOgreRenderTexturePass::fullScreenQuadSceneNode=NULL;
|
---|
12 |
|
---|
13 | ManagedOgreRenderTexturePass::ManagedOgreRenderTexturePass(
|
---|
14 |
|
---|
15 | Root* mRoot, const String& renderTextureName,unsigned int width,unsigned int height,
|
---|
16 | TextureType texType , PixelFormat internalFormat,
|
---|
17 | const NameValuePairList *miscParams,bool fullScreenQuadRenderer
|
---|
18 |
|
---|
19 | ){
|
---|
20 |
|
---|
21 | this->mRoot=mRoot;
|
---|
22 | renderSystem=mRoot->getRenderSystem();
|
---|
23 | mSceneMgr=mRoot->getSceneManager(ST_GENERIC);
|
---|
24 | if(renderTextureName!=noRenderTargetName){
|
---|
25 | renderTexture=renderSystem->createRenderTexture(renderTextureName,width,height,texType,internalFormat,miscParams);
|
---|
26 | this->addRenderTarget(renderTexture);
|
---|
27 | }
|
---|
28 | if(!fullScreenQuad){
|
---|
29 | createFullScreenQuad();
|
---|
30 | }
|
---|
31 | this->fullScreenQuadRenderer=fullScreenQuadRenderer;
|
---|
32 | effectWrapper=new EffectWrapper();
|
---|
33 | }
|
---|
34 |
|
---|
35 | void ManagedOgreRenderTexturePass::createFullScreenQuad(){
|
---|
36 | fullScreenQuad=new MovablePlane(Vector3::UNIT_Y,0.0f);
|
---|
37 | MeshManager::getSingleton().createPlane("FullScreenQuad",
|
---|
38 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
39 | *fullScreenQuad, 1, 1,
|
---|
40 | 1, 1, true, 1, 1, 1, Vector3::UNIT_Z);
|
---|
41 | this->fullScreenQuadEntity=mSceneMgr->createEntity("FullScreenQuad","FullScreenQuadEntity");
|
---|
42 | SceneNode* rootNode = mSceneMgr->getRootSceneNode();
|
---|
43 | fullScreenQuadSceneNode=rootNode->createChildSceneNode();
|
---|
44 | mPlaneNode->attachObject(fullScreenQuadEntity);
|
---|
45 | mPlaneNode->attachObject(fullScreenQuad);
|
---|
46 | //camera setup.
|
---|
47 | }
|
---|
48 |
|
---|
49 | void ManagedOgreRenderTexturePass::entityMaterialSetup(){
|
---|
50 | SceneManager::EntityIterator entityIterator=mSceneMgr->getEntityIterator();
|
---|
51 | while(entityIterator.hasMoreElements()){
|
---|
52 | Entity* entity=entityIterator.getNext();
|
---|
53 | entity->setMaterialName(this->effectWrapper->getMaterial()->getName());
|
---|
54 | if(fullScreenQuadRenderer){
|
---|
55 | entityVisibilityList.push_back(entity->isVisible());
|
---|
56 | entity->setVisible(false);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | if(fullScreenQuadRenderer){
|
---|
60 | this->fullScreenQuadSceneNode->setVisible(true);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | void ManagedOgreRenderTexturePass::entityMaterialCleanup(){
|
---|
65 | if(fullScreenQuadRenderer){
|
---|
66 | SceneManager::EntityIterator entityIterator=mSceneMgr->getEntityIterator();
|
---|
67 | int elementNumber=0;
|
---|
68 | while(entityIterator.hasMoreElements()){
|
---|
69 | Entity* entity=entityIterator.getNext();
|
---|
70 | entity->setVisible(entityVisibilityList.at(elementNumber));
|
---|
71 | elementNumber++;
|
---|
72 | }
|
---|
73 | entityVisibilityList.clear();
|
---|
74 | this->fullScreenQuadSceneNode->setVisible(false);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void ManagedOgreRenderTexturePass::update(NameValuePairList* startParams,NameValuePairList* endParams){
|
---|
79 | onRenderStart(startParams);
|
---|
80 | entityMaterialSetup();
|
---|
81 | RenderTarget* rt=renderTargets.at(0);
|
---|
82 | Matrix4* worldMatrices=new Matrix4[rt->getViewport(0)->getCamera()->getNumWorldTransforms()];
|
---|
83 | rt->getViewport(0)->getCamera()->getWorldTransforms(worldMatrices);
|
---|
84 | Matrix4 viewMatrix=renderTexture->getViewport(0)->getCamera()->getViewMatrix();
|
---|
85 | Matrix4 projMatrix=renderTexture->getViewport(0)->getCamera()->getProjectionMatrix();
|
---|
86 | std::vector<Viewport*> viewports;
|
---|
87 | std::vector<RenderTarget*>::iterator rtIterator;
|
---|
88 | for(rtIterator=renderTargets.begin();rtIterator!=renderTargets.end();rtIterator++){
|
---|
89 | viewports.push_back((*rtIterator)->getViewport(0));
|
---|
90 | }
|
---|
91 | Matrix4 worldMatrix = Matrix4::IDENTITY;
|
---|
92 | for(int i=0;i<rt->getViewport(0)->getCamera()->getNumWorldTransforms();i++){
|
---|
93 | worldMatrix = worldMatrix * worldMatrices[i];
|
---|
94 | }
|
---|
95 | delete[] worldMatrices;
|
---|
96 | mRoot->getSceneManager(ST_GENERIC)->manualRender(effectWrapper->getMaterial()->getTechnique(0)->getPass(0),viewports,this->cubeMapFaces,worldMatrix,viewMatrix,projMatrix,true);
|
---|
97 | entityMaterialCleanup();
|
---|
98 | onRenderEnd(endParams);
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool ManagedOgreRenderTexturePass::changeTexture(const String& textureName, unsigned short textureUnit){
|
---|
102 | this->effectWrapper->SetTexture(textureUnit,textureName);
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool ManagedOgreRenderTexturePass::changeMaterial(String& materialName, StringVector& newTextureNames,bool useOldTextureNames){
|
---|
107 | effectWrapper->createMaterial(materialName);
|
---|
108 | this->effectWrapper->getMaterial()->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
|
---|
109 | std::vector<TextureUnitState*>::iterator iter;
|
---|
110 | for(iter=usedTextureUnitStates.begin();iter<usedTextureUnitStates.end();iter++){
|
---|
111 | usedTextureUnitStates.erase(iter);
|
---|
112 | }
|
---|
113 | if(!useOldTextureNames){
|
---|
114 | textureNames=newTextureNames;
|
---|
115 | }
|
---|
116 | std::vector<String>::iterator iter2;
|
---|
117 | int i=0;
|
---|
118 | for(iter2=textureNames.begin();iter2<textureNames.end();iter2++){
|
---|
119 | TextureUnitState* t = this->effectWrapper->getMaterial()->getTechnique(0)->getPass(0)->createTextureUnitState((*iter2),i);
|
---|
120 | i++;
|
---|
121 | usedTextureUnitStates.push_back(t);
|
---|
122 | }
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void ManagedOgreRenderTexturePass::oneTimeRenderToTextureSetup(String& materialName,StringVector& textureNames){
|
---|
127 | std::vector<String>::iterator iter;
|
---|
128 | this->effectWrapper->getMaterial()->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
|
---|
129 | int i=0;
|
---|
130 | for(iter=textureNames.begin();iter<textureNames.end();iter++){
|
---|
131 | TextureUnitState* t = this->effectWrapper->getMaterial()->getTechnique(0)->getPass(0)->createTextureUnitState((*iter),i);
|
---|
132 | i++;
|
---|
133 | usedTextureUnitStates.push_back(t);
|
---|
134 | }
|
---|
135 | effectWrapper->createMaterial(materialName);
|
---|
136 | }
|
---|
137 |
|
---|