~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to examples/13.RenderToTexture/main.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** Example 013 Render To Texture
 
2
 
 
3
This tutorial shows how to render to a texture using Irrlicht. Render to
 
4
texture is a feature with which it is possible to create nice special effects.
 
5
In addition, this tutorial shows how to enable specular highlights.
 
6
 
 
7
In the beginning, everything as usual. Include the needed headers, ask the user
 
8
for the rendering driver, create the Irrlicht Device:
 
9
*/
 
10
 
 
11
#include <irrlicht.h>
 
12
#include "driverChoice.h"
 
13
 
 
14
using namespace irr;
 
15
 
 
16
#ifdef _MSC_VER
 
17
#pragma comment(lib, "Irrlicht.lib")
 
18
#endif
 
19
 
 
20
int main()
 
21
{
 
22
        // ask user for driver
 
23
        video::E_DRIVER_TYPE driverType=driverChoiceConsole();
 
24
        if (driverType==video::EDT_COUNT)
 
25
                return 1;
 
26
 
 
27
        // create device and exit if creation failed
 
28
 
 
29
        IrrlichtDevice *device =
 
30
                createDevice(driverType, core::dimension2d<u32>(640, 480),
 
31
                16, false, false);
 
32
 
 
33
        if (device == 0)
 
34
                return 1; // could not create selected driver.
 
35
 
 
36
        video::IVideoDriver* driver = device->getVideoDriver();
 
37
        scene::ISceneManager* smgr = device->getSceneManager();
 
38
        gui::IGUIEnvironment* env = device->getGUIEnvironment();
 
39
        
 
40
        /*
 
41
        Now, we load an animated mesh to be displayed. As in most examples,
 
42
        we'll take the fairy md2 model. The difference here: We set the
 
43
        shininess of the model to a value other than 0 which is the default
 
44
        value. This enables specular highlights on the model if dynamic
 
45
        lighting is on. The value influences the size of the highlights.
 
46
        */
 
47
 
 
48
        // load and display animated fairy mesh
 
49
 
 
50
        scene::IAnimatedMeshSceneNode* fairy = smgr->addAnimatedMeshSceneNode(
 
51
                smgr->getMesh("../../media/faerie.md2"));
 
52
 
 
53
        if (fairy)
 
54
        {
 
55
                fairy->setMaterialTexture(0,
 
56
                                driver->getTexture("../../media/faerie2.bmp")); // set diffuse texture
 
57
                fairy->setMaterialFlag(video::EMF_LIGHTING, true); // enable dynamic lighting
 
58
                fairy->getMaterial(0).Shininess = 20.0f; // set size of specular highlights
 
59
                fairy->setPosition(core::vector3df(-10,0,-100));
 
60
                fairy->setMD2Animation ( scene::EMAT_STAND );
 
61
        }
 
62
        
 
63
        /*
 
64
        To make specular highlights appear on the model, we need a dynamic
 
65
        light in the scene. We add one directly in vicinity of the model. In
 
66
        addition, to make the model not that dark, we set the ambient light to
 
67
        gray.
 
68
        */
 
69
 
 
70
        // add white light
 
71
        smgr->addLightSceneNode(0, core::vector3df(-15,5,-105),
 
72
                        video::SColorf(1.0f, 1.0f, 1.0f));
 
73
 
 
74
        // set ambient light
 
75
        smgr->setAmbientLight(video::SColor(0,60,60,60));
 
76
        
 
77
        /*
 
78
        The next is just some standard stuff: Add a test cube and let it rotate
 
79
        to make the scene more interesting. The user defined camera and cursor
 
80
        setup is made later on, right before the render loop.
 
81
        */
 
82
 
 
83
        // create test cube
 
84
        scene::ISceneNode* test = smgr->addCubeSceneNode(60);
 
85
 
 
86
        // let the cube rotate and set some light settings
 
87
        scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(
 
88
                core::vector3df(0.3f, 0.3f,0));
 
89
 
 
90
        test->setPosition(core::vector3df(-100,0,-100));
 
91
        test->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting
 
92
        test->addAnimator(anim);
 
93
        anim->drop();
 
94
 
 
95
        // set window caption
 
96
        device->setWindowCaption(L"Irrlicht Engine - Render to Texture and Specular Highlights example");
 
97
        
 
98
        /*
 
99
        To test out the render to texture feature, we need a render target
 
100
        texture. These are not like standard textures, but need to be created
 
101
        first. To create one, we call IVideoDriver::addRenderTargetTexture()
 
102
        and specify the size of the texture. Please don't use sizes bigger than
 
103
        the frame buffer for this, because the render target shares the zbuffer
 
104
        with the frame buffer.
 
105
        Because we want to render the scene not from the user camera into the
 
106
        texture, we add another fixed camera to the scene. But before we do all
 
107
        this, we check if the current running driver is able to render to
 
108
        textures. If it is not, we simply display a warning text.
 
109
        */
 
110
 
 
111
        // create render target
 
112
        video::ITexture* rt = 0;
 
113
        scene::ICameraSceneNode* fixedCam = 0;
 
114
        
 
115
 
 
116
        if (driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
 
117
        {
 
118
                rt = driver->addRenderTargetTexture(core::dimension2d<u32>(256,256), "RTT1");
 
119
                test->setMaterialTexture(0, rt); // set material of cube to render target
 
120
 
 
121
                // add fixed camera
 
122
                fixedCam = smgr->addCameraSceneNode(0, core::vector3df(10,10,-80),
 
123
                        core::vector3df(-10,10,-100));
 
124
        }
 
125
        else
 
126
        {
 
127
                // create problem text
 
128
                gui::IGUISkin* skin = env->getSkin();
 
129
                gui::IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
 
130
                if (font)
 
131
                        skin->setFont(font);
 
132
 
 
133
                gui::IGUIStaticText* text = env->addStaticText(
 
134
                        L"Your hardware or this renderer is not able to use the "\
 
135
                        L"render to texture feature. RTT Disabled.",
 
136
                        core::rect<s32>(150,20,470,60));
 
137
 
 
138
                text->setOverrideColor(video::SColor(100,255,255,255));
 
139
        }
 
140
        
 
141
        // add fps camera
 
142
        scene::ICameraSceneNode* fpsCamera = smgr->addCameraSceneNodeFPS();
 
143
        fpsCamera->setPosition(core::vector3df(-50,50,-150));
 
144
 
 
145
        // disable mouse cursor
 
146
        device->getCursorControl()->setVisible(false);
 
147
 
 
148
        /*
 
149
        Nearly finished. Now we need to draw everything. Every frame, we draw
 
150
        the scene twice. Once from the fixed camera into the render target
 
151
        texture and once as usual. When rendering into the render target, we
 
152
        need to disable the visibilty of the test cube, because it has the
 
153
        render target texture applied to it. That's it, wasn't too complicated
 
154
        I hope. :)
 
155
        */
 
156
 
 
157
        int lastFPS = -1;
 
158
 
 
159
        while(device->run())
 
160
        if (device->isWindowActive())
 
161
        {
 
162
                driver->beginScene(true, true, 0);
 
163
 
 
164
                if (rt)
 
165
                {
 
166
                        // draw scene into render target
 
167
                        
 
168
                        // set render target texture
 
169
                        driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));
 
170
 
 
171
                        // make cube invisible and set fixed camera as active camera
 
172
                        test->setVisible(false);
 
173
                        smgr->setActiveCamera(fixedCam);
 
174
 
 
175
                        // draw whole scene into render buffer
 
176
                        smgr->drawAll();
 
177
 
 
178
                        // set back old render target
 
179
                        // The buffer might have been distorted, so clear it
 
180
                        driver->setRenderTarget(0, true, true, 0);
 
181
 
 
182
                        // make the cube visible and set the user controlled camera as active one
 
183
                        test->setVisible(true);
 
184
                        smgr->setActiveCamera(fpsCamera);
 
185
                }
 
186
                
 
187
                // draw scene normally
 
188
                smgr->drawAll();
 
189
                env->drawAll();
 
190
 
 
191
                driver->endScene();
 
192
 
 
193
                // display frames per second in window title
 
194
                int fps = driver->getFPS();
 
195
                if (lastFPS != fps)
 
196
                {
 
197
                        core::stringw str = L"Irrlicht Engine - Render to Texture and Specular Highlights example";
 
198
                        str += " FPS:";
 
199
                        str += fps;
 
200
 
 
201
                        device->setWindowCaption(str.c_str());
 
202
                        lastFPS = fps;
 
203
                }
 
204
        }
 
205
 
 
206
        device->drop(); // drop device
 
207
        return 0;
 
208
}
 
209
 
 
210
/*
 
211
**/