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