1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | You may use this sample code for anything you like, it is not covered by the |
---|
11 | LGPL like the rest of the engine. |
---|
12 | ----------------------------------------------------------------------------- |
---|
13 | */ |
---|
14 | |
---|
15 | /** |
---|
16 | \file |
---|
17 | RenderToTexture.cpp |
---|
18 | \brief |
---|
19 | Shows OGRE's RenderToTexture feature, and using it to drive a reflection. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "ExampleApplication.h" |
---|
23 | #include "ExampleFrameListener.h" |
---|
24 | |
---|
25 | class RenderToTextureFrameListener : public ExampleFrameListener |
---|
26 | { |
---|
27 | protected: |
---|
28 | Camera* mReflectCam; |
---|
29 | SceneNode* mPlaneNode; |
---|
30 | public: |
---|
31 | RenderToTextureFrameListener(RenderWindow* window, Camera* maincam, Camera* reflectCam, |
---|
32 | SceneNode* planeSceneNode) |
---|
33 | :ExampleFrameListener(window, maincam), |
---|
34 | mReflectCam(reflectCam), mPlaneNode(planeSceneNode) |
---|
35 | { |
---|
36 | |
---|
37 | } |
---|
38 | bool frameStarted(const FrameEvent& evt) |
---|
39 | { |
---|
40 | if( ExampleFrameListener::frameStarted(evt) == false ) |
---|
41 | return false; |
---|
42 | |
---|
43 | // Make sure reflection camera is updated too |
---|
44 | mReflectCam->setOrientation(mCamera->getOrientation()); |
---|
45 | mReflectCam->setPosition(mCamera->getPosition()); |
---|
46 | |
---|
47 | // Rotate plane |
---|
48 | mPlaneNode->yaw(Degree(30 * evt.timeSinceLastFrame), Node::TS_PARENT); |
---|
49 | |
---|
50 | return true; |
---|
51 | } |
---|
52 | }; |
---|
53 | |
---|
54 | class RenderToTextureApplication : public ExampleApplication, public RenderTargetListener |
---|
55 | { |
---|
56 | public: |
---|
57 | RenderToTextureApplication() : mPlane(0) {} |
---|
58 | ~RenderToTextureApplication() |
---|
59 | { |
---|
60 | delete mPlane; |
---|
61 | } |
---|
62 | |
---|
63 | protected: |
---|
64 | |
---|
65 | MovablePlane* mPlane; |
---|
66 | Entity* mPlaneEnt; |
---|
67 | Camera* mReflectCam; |
---|
68 | SceneNode* mPlaneNode; |
---|
69 | // render target events |
---|
70 | void preRenderTargetUpdate(const RenderTargetEvent& evt) |
---|
71 | { |
---|
72 | // Hide plane |
---|
73 | mPlaneEnt->setVisible(false); |
---|
74 | |
---|
75 | } |
---|
76 | void postRenderTargetUpdate(const RenderTargetEvent& evt) |
---|
77 | { |
---|
78 | // Show plane |
---|
79 | mPlaneEnt->setVisible(true); |
---|
80 | } |
---|
81 | |
---|
82 | // Just override the mandatory create scene method |
---|
83 | void createScene(void) |
---|
84 | { |
---|
85 | // Set ambient light |
---|
86 | mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2)); |
---|
87 | // Skybox |
---|
88 | mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox"); |
---|
89 | |
---|
90 | // Create a light |
---|
91 | Light* l = mSceneMgr->createLight("MainLight"); |
---|
92 | l->setType(Light::LT_DIRECTIONAL); |
---|
93 | Vector3 dir(0.5, -1, 0); |
---|
94 | dir.normalise(); |
---|
95 | l->setDirection(dir); |
---|
96 | l->setDiffuseColour(1.0f, 1.0f, 0.8f); |
---|
97 | l->setSpecularColour(1.0f, 1.0f, 1.0f); |
---|
98 | |
---|
99 | |
---|
100 | // Create a prefab plane |
---|
101 | mPlane = new MovablePlane("ReflectPlane"); |
---|
102 | mPlane->d = 0; |
---|
103 | mPlane->normal = Vector3::UNIT_Y; |
---|
104 | MeshManager::getSingleton().createPlane("ReflectionPlane", |
---|
105 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, |
---|
106 | *mPlane, 2000, 2000, |
---|
107 | 1, 1, true, 1, 1, 1, Vector3::UNIT_Z); |
---|
108 | mPlaneEnt = mSceneMgr->createEntity( "Plane", "ReflectionPlane" ); |
---|
109 | |
---|
110 | // Create an entity from a model (will be loaded automatically) |
---|
111 | Entity* knotEnt = mSceneMgr->createEntity("Knot", "knot.mesh"); |
---|
112 | |
---|
113 | // Create an entity from a model (will be loaded automatically) |
---|
114 | Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh"); |
---|
115 | |
---|
116 | knotEnt->setMaterialName("Examples/TextureEffect2"); |
---|
117 | |
---|
118 | // Attach the rtt entity to the root of the scene |
---|
119 | SceneNode* rootNode = mSceneMgr->getRootSceneNode(); |
---|
120 | mPlaneNode = rootNode->createChildSceneNode(); |
---|
121 | |
---|
122 | // Attach both the plane entity, and the plane definition |
---|
123 | mPlaneNode->attachObject(mPlaneEnt); |
---|
124 | mPlaneNode->attachObject(mPlane); |
---|
125 | mPlaneNode->translate(0, -10, 0); |
---|
126 | // Tilt it a little to make it interesting |
---|
127 | mPlaneNode->roll(Degree(5)); |
---|
128 | |
---|
129 | rootNode->createChildSceneNode( "Head" )->attachObject( ogreHead ); |
---|
130 | |
---|
131 | TexturePtr texture = TextureManager::getSingleton().createManual( "RttTex", |
---|
132 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, |
---|
133 | 512, 512, 0, PF_R8G8B8, TU_RENDERTARGET ); |
---|
134 | RenderTarget *rttTex = texture->getBuffer()->getRenderTarget(); |
---|
135 | { |
---|
136 | mReflectCam = mSceneMgr->createCamera("ReflectCam"); |
---|
137 | mReflectCam->setNearClipDistance(mCamera->getNearClipDistance()); |
---|
138 | mReflectCam->setFarClipDistance(mCamera->getFarClipDistance()); |
---|
139 | mReflectCam->setAspectRatio( |
---|
140 | (Real)mWindow->getViewport(0)->getActualWidth() / |
---|
141 | (Real)mWindow->getViewport(0)->getActualHeight()); |
---|
142 | |
---|
143 | Viewport *v = rttTex->addViewport( mReflectCam ); |
---|
144 | v->setClearEveryFrame( true ); |
---|
145 | v->setBackgroundColour( ColourValue::Black ); |
---|
146 | |
---|
147 | MaterialPtr mat = MaterialManager::getSingleton().create("RttMat", |
---|
148 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
---|
149 | TextureUnitState* t = mat->getTechnique(0)->getPass(0)->createTextureUnitState("RustedMetal.jpg"); |
---|
150 | t = mat->getTechnique(0)->getPass(0)->createTextureUnitState("RttTex"); |
---|
151 | // Blend with base texture |
---|
152 | t->setColourOperationEx(LBX_BLEND_MANUAL, LBS_TEXTURE, LBS_CURRENT, ColourValue::White, |
---|
153 | ColourValue::White, 0.25); |
---|
154 | t->setTextureAddressingMode(TextureUnitState::TAM_CLAMP); |
---|
155 | t->setProjectiveTexturing(true, mReflectCam); |
---|
156 | rttTex->addListener(this); |
---|
157 | |
---|
158 | // set up linked reflection |
---|
159 | mReflectCam->enableReflection(mPlane); |
---|
160 | // Also clip |
---|
161 | mReflectCam->enableCustomNearClipPlane(mPlane); |
---|
162 | } |
---|
163 | |
---|
164 | // Give the plane a texture |
---|
165 | mPlaneEnt->setMaterialName("RttMat"); |
---|
166 | |
---|
167 | |
---|
168 | // Add a whole bunch of extra transparent entities |
---|
169 | Entity *cloneEnt; |
---|
170 | for (int n = 0; n < 10; ++n) |
---|
171 | { |
---|
172 | // Create a new node under the root |
---|
173 | SceneNode* node = mSceneMgr->createSceneNode(); |
---|
174 | // Random translate |
---|
175 | Vector3 nodePos; |
---|
176 | nodePos.x = Math::SymmetricRandom() * 750.0; |
---|
177 | nodePos.y = Math::SymmetricRandom() * 100.0 + 25; |
---|
178 | nodePos.z = Math::SymmetricRandom() * 750.0; |
---|
179 | node->setPosition(nodePos); |
---|
180 | rootNode->addChild(node); |
---|
181 | // Clone knot |
---|
182 | char cloneName[12]; |
---|
183 | sprintf(cloneName, "Knot%d", n); |
---|
184 | cloneEnt = knotEnt->clone(cloneName); |
---|
185 | // Attach to new node |
---|
186 | node->attachObject(cloneEnt); |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | mCamera->setPosition(-50, 100, 500); |
---|
191 | mCamera->lookAt(0,0,0); |
---|
192 | } |
---|
193 | |
---|
194 | void createFrameListener(void) |
---|
195 | { |
---|
196 | mFrameListener= new RenderToTextureFrameListener(mWindow, mCamera, mReflectCam, mPlaneNode); |
---|
197 | mRoot->addFrameListener(mFrameListener); |
---|
198 | |
---|
199 | } |
---|
200 | |
---|
201 | }; |
---|
202 | |
---|
203 | #ifdef __cplusplus |
---|
204 | extern "C" { |
---|
205 | #endif |
---|
206 | |
---|
207 | int main(int argc, char ** argv) |
---|
208 | { |
---|
209 | |
---|
210 | // Create application object |
---|
211 | RenderToTextureApplication app; |
---|
212 | |
---|
213 | app.go(); |
---|
214 | |
---|
215 | return 0; |
---|
216 | } |
---|
217 | |
---|
218 | #ifdef __cplusplus |
---|
219 | } |
---|
220 | #endif |
---|