3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "drawscene.h"
23 #include "clientmap.h"
24 #include "util/timetaker.h"
25 #include "fontengine.h"
26 #include "guiscalingfilter.h"
35 void draw_selectionbox(video::IVideoDriver* driver, Hud& hud,
36 std::vector<aabb3f>& hilightboxes, bool show_hud)
38 static const s16 selectionbox_width = rangelim(g_settings->getS16("selectionbox_width"), 1, 5);
43 video::SMaterial oldmaterial = driver->getMaterial2D();
45 m.Thickness = selectionbox_width;
47 driver->setMaterial(m);
48 hud.drawSelectionBoxes(hilightboxes);
49 driver->setMaterial(oldmaterial);
52 void draw_anaglyph_3d_mode(Camera& camera, bool show_hud, Hud& hud,
53 std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
54 scene::ISceneManager* smgr, bool draw_wield_tool, Client& client,
55 gui::IGUIEnvironment* guienv )
58 /* preserve old setup*/
59 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
60 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
62 irr::core::matrix4 startMatrix =
63 camera.getCameraNode()->getAbsoluteTransformation();
64 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
65 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
66 + camera.getCameraNode()->getAbsolutePosition();
70 irr::core::vector3df leftEye;
71 irr::core::matrix4 leftMove;
72 leftMove.setTranslation(
73 irr::core::vector3df(-g_settings->getFloat("3d_paralax_strength"),
75 leftEye = (startMatrix * leftMove).getTranslation();
77 //clear the depth buffer, and color
78 driver->beginScene( true, true, irr::video::SColor(0, 200, 200, 255));
79 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_RED;
80 driver->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
81 driver->getOverrideMaterial().EnablePasses = irr::scene::ESNRP_SKY_BOX
82 + irr::scene::ESNRP_SOLID + irr::scene::ESNRP_TRANSPARENT
83 + irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
84 camera.getCameraNode()->setPosition(leftEye);
85 camera.getCameraNode()->setTarget(focusPoint);
87 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
90 draw_selectionbox(driver, hud, hilightboxes, show_hud);
93 camera.drawWieldedTool(&leftMove);
99 irr::core::vector3df rightEye;
100 irr::core::matrix4 rightMove;
101 rightMove.setTranslation(
102 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
104 rightEye = (startMatrix * rightMove).getTranslation();
106 //clear the depth buffer
107 driver->clearZBuffer();
108 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_GREEN
109 + irr::video::ECP_BLUE;
110 driver->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
111 driver->getOverrideMaterial().EnablePasses = irr::scene::ESNRP_SKY_BOX
112 + irr::scene::ESNRP_SOLID + irr::scene::ESNRP_TRANSPARENT
113 + irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
114 camera.getCameraNode()->setPosition(rightEye);
115 camera.getCameraNode()->setTarget(focusPoint);
117 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
120 draw_selectionbox(driver, hud, hilightboxes, show_hud);
123 camera.drawWieldedTool(&rightMove);
128 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_ALL;
129 driver->getOverrideMaterial().EnableFlags = 0;
130 driver->getOverrideMaterial().EnablePasses = 0;
131 camera.getCameraNode()->setPosition(oldPosition);
132 camera.getCameraNode()->setTarget(oldTarget);
135 void init_texture(video::IVideoDriver* driver, const v2u32& screensize,
136 video::ITexture** texture, const char* name)
138 if (*texture != NULL)
140 driver->removeTexture(*texture);
142 *texture = driver->addRenderTargetTexture(
143 core::dimension2d<u32>(screensize.X, screensize.Y), name,
144 irr::video::ECF_A8R8G8B8);
147 video::ITexture* draw_image(const v2u32& screensize,
148 paralax_sign psign, const irr::core::matrix4& startMatrix,
149 const irr::core::vector3df& focusPoint, bool show_hud,
150 video::IVideoDriver* driver, Camera& camera, scene::ISceneManager* smgr,
151 Hud& hud, std::vector<aabb3f>& hilightboxes,
152 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
153 video::SColor skycolor )
155 static video::ITexture* images[2] = { NULL, NULL };
156 static v2u32 last_screensize = v2u32(0,0);
158 video::ITexture* image = NULL;
160 if (screensize != last_screensize) {
161 init_texture(driver, screensize, &images[1], "mt_drawimage_img1");
162 init_texture(driver, screensize, &images[0], "mt_drawimage_img2");
163 last_screensize = screensize;
171 driver->setRenderTarget(image, true, true,
172 irr::video::SColor(255,
173 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
175 irr::core::vector3df eye_pos;
176 irr::core::matrix4 movement;
177 movement.setTranslation(
178 irr::core::vector3df((int) psign *
179 g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
180 eye_pos = (startMatrix * movement).getTranslation();
182 //clear the depth buffer
183 driver->clearZBuffer();
184 camera.getCameraNode()->setPosition(eye_pos);
185 camera.getCameraNode()->setTarget(focusPoint);
188 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
192 draw_selectionbox(driver, hud, hilightboxes, show_hud);
195 camera.drawWieldedTool(&movement);
200 /* switch back to real renderer */
201 driver->setRenderTarget(0, true, true,
202 irr::video::SColor(0,
203 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
208 video::ITexture* draw_hud(video::IVideoDriver* driver, const v2u32& screensize,
209 bool show_hud, Hud& hud, Client& client, bool draw_crosshair,
210 video::SColor skycolor, gui::IGUIEnvironment* guienv, Camera& camera )
212 static video::ITexture* image = NULL;
213 init_texture(driver, screensize, &image, "mt_drawimage_hud");
214 driver->setRenderTarget(image, true, true,
215 irr::video::SColor(255,0,0,0));
221 hud.drawHotbar(client.getPlayerItem());
222 hud.drawLuaElements(camera.getOffset());
227 driver->setRenderTarget(0, true, true,
228 irr::video::SColor(0,
229 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
234 void draw_interlaced_3d_mode(Camera& camera, bool show_hud,
235 Hud& hud, std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
236 scene::ISceneManager* smgr, const v2u32& screensize,
237 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
238 video::SColor skycolor )
240 /* save current info */
241 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
242 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
243 irr::core::matrix4 startMatrix =
244 camera.getCameraNode()->getAbsoluteTransformation();
245 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
246 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
247 + camera.getCameraNode()->getAbsolutePosition();
249 /* create left view */
250 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
251 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
252 draw_wield_tool, client, guienv, skycolor);
255 irr::core::vector3df rightEye;
256 irr::core::matrix4 rightMove;
257 rightMove.setTranslation(
258 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
260 rightEye = (startMatrix * rightMove).getTranslation();
262 //clear the depth buffer
263 driver->clearZBuffer();
264 camera.getCameraNode()->setPosition(rightEye);
265 camera.getCameraNode()->setTarget(focusPoint);
268 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
272 draw_selectionbox(driver, hud, hilightboxes, show_hud);
275 camera.drawWieldedTool(&rightMove);
279 for (unsigned int i = 0; i < screensize.Y; i+=2 ) {
280 #if (IRRLICHT_VERSION_MAJOR >= 1) && (IRRLICHT_VERSION_MINOR >= 8)
281 driver->draw2DImage(left_image, irr::core::position2d<s32>(0, i),
283 driver->draw2DImage(left_image, irr::core::position2d<s32>(0, screensize.Y-i),
285 irr::core::rect<s32>(0, i,screensize.X, i+1), 0,
286 irr::video::SColor(255, 255, 255, 255),
291 camera.getCameraNode()->setPosition(oldPosition);
292 camera.getCameraNode()->setTarget(oldTarget);
295 void draw_sidebyside_3d_mode(Camera& camera, bool show_hud,
296 Hud& hud, std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
297 scene::ISceneManager* smgr, const v2u32& screensize,
298 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
299 video::SColor skycolor )
301 /* save current info */
302 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
303 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
304 irr::core::matrix4 startMatrix =
305 camera.getCameraNode()->getAbsoluteTransformation();
306 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
307 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
308 + camera.getCameraNode()->getAbsolutePosition();
310 /* create left view */
311 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
312 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
313 draw_wield_tool, client, guienv, skycolor);
315 /* create right view */
316 video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix,
317 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
318 draw_wield_tool, client, guienv, skycolor);
320 /* create hud overlay */
321 video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client,
322 false, skycolor, guienv, camera );
323 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
324 //makeColorKeyTexture mirrors texture so we do it twice to get it right again
325 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
327 draw2DImageFilterScaled(driver, left_image,
328 irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y),
329 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
331 draw2DImageFilterScaled(driver, hudtexture,
332 irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y),
333 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
335 draw2DImageFilterScaled(driver, right_image,
336 irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y),
337 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
339 draw2DImageFilterScaled(driver, hudtexture,
340 irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y),
341 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
347 camera.getCameraNode()->setPosition(oldPosition);
348 camera.getCameraNode()->setTarget(oldTarget);
351 void draw_top_bottom_3d_mode(Camera& camera, bool show_hud,
352 Hud& hud, std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
353 scene::ISceneManager* smgr, const v2u32& screensize,
354 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
355 video::SColor skycolor )
357 /* save current info */
358 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
359 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
360 irr::core::matrix4 startMatrix =
361 camera.getCameraNode()->getAbsoluteTransformation();
362 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
363 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
364 + camera.getCameraNode()->getAbsolutePosition();
366 /* create left view */
367 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
368 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
369 draw_wield_tool, client, guienv, skycolor);
371 /* create right view */
372 video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix,
373 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
374 draw_wield_tool, client, guienv, skycolor);
376 /* create hud overlay */
377 video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client,
378 false, skycolor, guienv, camera );
379 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
380 //makeColorKeyTexture mirrors texture so we do it twice to get it right again
381 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
383 draw2DImageFilterScaled(driver, left_image,
384 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2),
385 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
387 draw2DImageFilterScaled(driver, hudtexture,
388 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2),
389 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
391 draw2DImageFilterScaled(driver, right_image,
392 irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y),
393 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
395 draw2DImageFilterScaled(driver, hudtexture,
396 irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y),
397 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
403 camera.getCameraNode()->setPosition(oldPosition);
404 camera.getCameraNode()->setTarget(oldTarget);
407 void draw_plain(Camera& camera, bool show_hud, Hud& hud,
408 std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
409 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv)
411 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
413 draw_selectionbox(driver, hud, hilightboxes, show_hud);
416 camera.drawWieldedTool();
419 void draw_scene(video::IVideoDriver *driver, scene::ISceneManager *smgr,
420 Camera &camera, Client& client, LocalPlayer *player, Hud &hud,
421 Mapper &mapper, gui::IGUIEnvironment *guienv,
422 std::vector<aabb3f> hilightboxes, const v2u32 &screensize,
423 video::SColor skycolor, bool show_hud, bool show_minimap)
425 TimeTaker timer("smgr");
427 bool draw_wield_tool = (show_hud &&
428 (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) &&
429 camera.getCameraMode() < CAMERA_MODE_THIRD );
431 bool draw_crosshair = ((player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) &&
432 (camera.getCameraMode() != CAMERA_MODE_THIRD_FRONT));
434 #ifdef HAVE_TOUCHSCREENGUI
436 draw_crosshair = !g_settings->getBool("touchtarget");
438 catch(SettingNotFoundException) {}
441 std::string draw_mode = g_settings->get("3d_mode");
445 if (draw_mode == "anaglyph")
447 draw_anaglyph_3d_mode(camera, show_hud, hud, hilightboxes, driver,
448 smgr, draw_wield_tool, client, guienv);
449 draw_crosshair = false;
451 else if (draw_mode == "interlaced")
453 draw_interlaced_3d_mode(camera, show_hud, hud, hilightboxes, driver,
454 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
455 draw_crosshair = false;
457 else if (draw_mode == "sidebyside")
459 draw_sidebyside_3d_mode(camera, show_hud, hud, hilightboxes, driver,
460 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
463 else if (draw_mode == "topbottom")
465 draw_top_bottom_3d_mode(camera, show_hud, hud, hilightboxes, driver,
466 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
470 draw_plain(camera, show_hud, hud, hilightboxes, driver,
471 draw_wield_tool, client, guienv);
478 client.getEnv().getClientMap().renderPostFx(camera.getCameraMode());
481 //TODO how to make those 3d too
486 hud.drawHotbar(client.getPlayerItem());
487 hud.drawLuaElements(camera.getOffset());
489 mapper.drawMinimap();
498 Draws a screen with a single text on it.
499 Text will be removed when the screen is drawn the next time.
500 Additionally, a progressbar can be drawn when percent is set between 0 and 100.
502 void draw_load_screen(const std::wstring &text, IrrlichtDevice* device,
503 gui::IGUIEnvironment* guienv, float dtime, int percent, bool clouds )
505 video::IVideoDriver* driver = device->getVideoDriver();
506 v2u32 screensize = porting::getWindowSize();
508 v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
509 v2s32 center(screensize.X / 2, screensize.Y / 2);
510 core::rect<s32> textrect(center - textsize / 2, center + textsize / 2);
512 gui::IGUIStaticText *guitext = guienv->addStaticText(
513 text.c_str(), textrect, false, false);
514 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
516 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
517 if (cloud_menu_background)
519 g_menuclouds->step(dtime*3);
520 g_menuclouds->render();
521 driver->beginScene(true, true, video::SColor(255, 140, 186, 250));
522 g_menucloudsmgr->drawAll();
525 driver->beginScene(true, true, video::SColor(255, 0, 0, 0));
528 if ((percent >= 0) && (percent <= 100))
531 // 342 is (approximately) 256/0.75 to keep bar on same size as
532 // before with default settings
533 342 * porting::getDisplayDensity() *
534 g_settings->getFloat("gui_scaling"),
535 g_fontengine->getTextHeight() * 2);
537 core::rect<s32> barrect(center - barsize / 2, center + barsize / 2);
538 driver->draw2DRectangle(video::SColor(255, 255, 255, 255),barrect, NULL); // border
539 driver->draw2DRectangle(video::SColor(255, 64, 64, 64), core::rect<s32> (
540 barrect.UpperLeftCorner + 1,
541 barrect.LowerRightCorner-1), NULL); // black inside the bar
542 driver->draw2DRectangle(video::SColor(255, 128, 128, 128), core::rect<s32> (
543 barrect.UpperLeftCorner + 1,
545 barrect.LowerRightCorner.X -
546 (barsize.X - 1) + percent * (barsize.X - 2) / 100,
547 barrect.LowerRightCorner.Y - 1)), NULL); // the actual progress