source: OGRE/trunk/ogrenew/Samples/RenderToTexture/src/RenderToTexture.cpp @ 692

Revision 692, 7.0 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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