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_anaglyph_3d_mode(Camera& camera, bool show_hud, Hud& hud,
36 video::IVideoDriver* driver, scene::ISceneManager* smgr,
37 bool draw_wield_tool, Client& client,
38 gui::IGUIEnvironment* guienv )
41 /* preserve old setup*/
42 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
43 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
45 irr::core::matrix4 startMatrix =
46 camera.getCameraNode()->getAbsoluteTransformation();
47 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
48 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
49 + camera.getCameraNode()->getAbsolutePosition();
53 irr::core::vector3df leftEye;
54 irr::core::matrix4 leftMove;
55 leftMove.setTranslation(
56 irr::core::vector3df(-g_settings->getFloat("3d_paralax_strength"),
58 leftEye = (startMatrix * leftMove).getTranslation();
60 //clear the depth buffer, and color
61 driver->beginScene( true, true, irr::video::SColor(0, 200, 200, 255));
62 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_RED;
63 driver->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
64 driver->getOverrideMaterial().EnablePasses = irr::scene::ESNRP_SKY_BOX
65 + irr::scene::ESNRP_SOLID + irr::scene::ESNRP_TRANSPARENT
66 + irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
67 camera.getCameraNode()->setPosition(leftEye);
68 camera.getCameraNode()->setTarget(focusPoint);
70 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
72 hud.drawSelectionMesh();
74 camera.drawWieldedTool(&leftMove);
80 irr::core::vector3df rightEye;
81 irr::core::matrix4 rightMove;
82 rightMove.setTranslation(
83 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
85 rightEye = (startMatrix * rightMove).getTranslation();
87 //clear the depth buffer
88 driver->clearZBuffer();
89 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_GREEN
90 + irr::video::ECP_BLUE;
91 driver->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
92 driver->getOverrideMaterial().EnablePasses = irr::scene::ESNRP_SKY_BOX
93 + irr::scene::ESNRP_SOLID + irr::scene::ESNRP_TRANSPARENT
94 + irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
95 camera.getCameraNode()->setPosition(rightEye);
96 camera.getCameraNode()->setTarget(focusPoint);
98 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
100 hud.drawSelectionMesh();
102 camera.drawWieldedTool(&rightMove);
107 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_ALL;
108 driver->getOverrideMaterial().EnableFlags = 0;
109 driver->getOverrideMaterial().EnablePasses = 0;
110 camera.getCameraNode()->setPosition(oldPosition);
111 camera.getCameraNode()->setTarget(oldTarget);
114 void init_texture(video::IVideoDriver* driver, const v2u32& screensize,
115 video::ITexture** texture, const char* name)
117 if (*texture != NULL)
119 driver->removeTexture(*texture);
121 *texture = driver->addRenderTargetTexture(
122 core::dimension2d<u32>(screensize.X, screensize.Y), name,
123 irr::video::ECF_A8R8G8B8);
126 video::ITexture* draw_image(const v2u32 &screensize,
127 paralax_sign psign, const irr::core::matrix4 &startMatrix,
128 const irr::core::vector3df &focusPoint, bool show_hud,
129 video::IVideoDriver *driver, Camera &camera, scene::ISceneManager *smgr,
130 Hud &hud, bool draw_wield_tool, Client &client,
131 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
133 static video::ITexture* images[2] = { NULL, NULL };
134 static v2u32 last_screensize = v2u32(0, 0);
136 video::ITexture* image = NULL;
138 if (screensize != last_screensize) {
139 init_texture(driver, screensize, &images[1], "mt_drawimage_img1");
140 init_texture(driver, screensize, &images[0], "mt_drawimage_img2");
141 last_screensize = screensize;
149 driver->setRenderTarget(image, true, true,
150 irr::video::SColor(255,
151 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
153 irr::core::vector3df eye_pos;
154 irr::core::matrix4 movement;
155 movement.setTranslation(
156 irr::core::vector3df((int) psign *
157 g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
158 eye_pos = (startMatrix * movement).getTranslation();
160 //clear the depth buffer
161 driver->clearZBuffer();
162 camera.getCameraNode()->setPosition(eye_pos);
163 camera.getCameraNode()->setTarget(focusPoint);
166 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
169 hud.drawSelectionMesh();
171 camera.drawWieldedTool(&movement);
176 /* switch back to real renderer */
177 driver->setRenderTarget(0, true, true,
178 irr::video::SColor(0,
179 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
184 video::ITexture* draw_hud(video::IVideoDriver* driver, const v2u32& screensize,
185 bool show_hud, Hud& hud, Client& client, bool draw_crosshair,
186 video::SColor skycolor, gui::IGUIEnvironment* guienv, Camera& camera )
188 static video::ITexture* image = NULL;
189 init_texture(driver, screensize, &image, "mt_drawimage_hud");
190 driver->setRenderTarget(image, true, true,
191 irr::video::SColor(255,0,0,0));
197 hud.drawHotbar(client.getPlayerItem());
198 hud.drawLuaElements(camera.getOffset());
199 camera.drawNametags();
203 driver->setRenderTarget(0, true, true,
204 irr::video::SColor(0,
205 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
210 void draw_interlaced_3d_mode(Camera& camera, bool show_hud,
211 Hud& hud, video::IVideoDriver* driver,
212 scene::ISceneManager* smgr, const v2u32& screensize,
213 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
214 video::SColor skycolor )
216 /* save current info */
217 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
218 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
219 irr::core::matrix4 startMatrix =
220 camera.getCameraNode()->getAbsoluteTransformation();
221 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
222 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
223 + camera.getCameraNode()->getAbsolutePosition();
225 /* create left view */
226 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
227 focusPoint, show_hud, driver, camera, smgr, hud,
228 draw_wield_tool, client, guienv, skycolor);
231 irr::core::vector3df rightEye;
232 irr::core::matrix4 rightMove;
233 rightMove.setTranslation(
234 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
236 rightEye = (startMatrix * rightMove).getTranslation();
238 //clear the depth buffer
239 driver->clearZBuffer();
240 camera.getCameraNode()->setPosition(rightEye);
241 camera.getCameraNode()->setTarget(focusPoint);
244 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
247 hud.drawSelectionMesh();
249 camera.drawWieldedTool(&rightMove);
253 for (unsigned int i = 0; i < screensize.Y; i+=2 ) {
254 #if (IRRLICHT_VERSION_MAJOR >= 1) && (IRRLICHT_VERSION_MINOR >= 8)
255 driver->draw2DImage(left_image, irr::core::position2d<s32>(0, i),
257 driver->draw2DImage(left_image, irr::core::position2d<s32>(0, screensize.Y-i),
259 irr::core::rect<s32>(0, i,screensize.X, i+1), 0,
260 irr::video::SColor(255, 255, 255, 255),
265 camera.getCameraNode()->setPosition(oldPosition);
266 camera.getCameraNode()->setTarget(oldTarget);
269 void draw_sidebyside_3d_mode(Camera& camera, bool show_hud,
270 Hud& hud, video::IVideoDriver* driver,
271 scene::ISceneManager* smgr, const v2u32& screensize,
272 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
273 video::SColor skycolor )
275 /* save current info */
276 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
277 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
278 irr::core::matrix4 startMatrix =
279 camera.getCameraNode()->getAbsoluteTransformation();
280 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
281 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
282 + camera.getCameraNode()->getAbsolutePosition();
284 /* create left view */
285 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
286 focusPoint, show_hud, driver, camera, smgr, hud,
287 draw_wield_tool, client, guienv, skycolor);
289 /* create right view */
290 video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix,
291 focusPoint, show_hud, driver, camera, smgr, hud,
292 draw_wield_tool, client, guienv, skycolor);
294 /* create hud overlay */
295 video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client,
296 false, skycolor, guienv, camera );
297 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
298 //makeColorKeyTexture mirrors texture so we do it twice to get it right again
299 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
301 draw2DImageFilterScaled(driver, left_image,
302 irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y),
303 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
305 draw2DImageFilterScaled(driver, hudtexture,
306 irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y),
307 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
309 draw2DImageFilterScaled(driver, right_image,
310 irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y),
311 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
313 draw2DImageFilterScaled(driver, hudtexture,
314 irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y),
315 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
321 camera.getCameraNode()->setPosition(oldPosition);
322 camera.getCameraNode()->setTarget(oldTarget);
325 void draw_top_bottom_3d_mode(Camera& camera, bool show_hud,
326 Hud& hud, video::IVideoDriver* driver,
327 scene::ISceneManager* smgr, const v2u32& screensize,
328 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
329 video::SColor skycolor )
331 /* save current info */
332 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
333 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
334 irr::core::matrix4 startMatrix =
335 camera.getCameraNode()->getAbsoluteTransformation();
336 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
337 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
338 + camera.getCameraNode()->getAbsolutePosition();
340 /* create left view */
341 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
342 focusPoint, show_hud, driver, camera, smgr, hud,
343 draw_wield_tool, client, guienv, skycolor);
345 /* create right view */
346 video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix,
347 focusPoint, show_hud, driver, camera, smgr, hud,
348 draw_wield_tool, client, guienv, skycolor);
350 /* create hud overlay */
351 video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client,
352 false, skycolor, guienv, camera );
353 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
354 //makeColorKeyTexture mirrors texture so we do it twice to get it right again
355 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
357 draw2DImageFilterScaled(driver, left_image,
358 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2),
359 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
361 draw2DImageFilterScaled(driver, hudtexture,
362 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2),
363 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
365 draw2DImageFilterScaled(driver, right_image,
366 irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y),
367 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
369 draw2DImageFilterScaled(driver, hudtexture,
370 irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y),
371 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
377 camera.getCameraNode()->setPosition(oldPosition);
378 camera.getCameraNode()->setTarget(oldTarget);
381 void draw_pageflip_3d_mode(Camera& camera, bool show_hud,
382 Hud& hud, video::IVideoDriver* driver,
383 scene::ISceneManager* smgr, const v2u32& screensize,
384 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
385 video::SColor skycolor)
387 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
388 errorstream << "Pageflip 3D mode is not supported"
389 << " with your Irrlicht version!" << std::endl;
391 /* preserve old setup*/
392 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
393 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
395 irr::core::matrix4 startMatrix =
396 camera.getCameraNode()->getAbsoluteTransformation();
397 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
398 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
399 + camera.getCameraNode()->getAbsolutePosition();
402 driver->setRenderTarget(irr::video::ERT_STEREO_LEFT_BUFFER);
404 irr::core::vector3df leftEye;
405 irr::core::matrix4 leftMove;
406 leftMove.setTranslation(
407 irr::core::vector3df(-g_settings->getFloat("3d_paralax_strength"),
409 leftEye = (startMatrix * leftMove).getTranslation();
411 //clear the depth buffer, and color
412 driver->beginScene(true, true, irr::video::SColor(200, 200, 200, 255));
413 camera.getCameraNode()->setPosition(leftEye);
414 camera.getCameraNode()->setTarget(focusPoint);
416 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
419 hud.drawSelectionMesh();
421 camera.drawWieldedTool(&leftMove);
422 hud.drawHotbar(client.getPlayerItem());
423 hud.drawLuaElements(camera.getOffset());
424 camera.drawNametags();
430 driver->setRenderTarget(irr::video::ERT_STEREO_RIGHT_BUFFER);
432 irr::core::vector3df rightEye;
433 irr::core::matrix4 rightMove;
434 rightMove.setTranslation(
435 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
437 rightEye = (startMatrix * rightMove).getTranslation();
439 //clear the depth buffer, and color
440 driver->beginScene(true, true, irr::video::SColor(200, 200, 200, 255));
441 camera.getCameraNode()->setPosition(rightEye);
442 camera.getCameraNode()->setTarget(focusPoint);
444 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
447 hud.drawSelectionMesh();
449 camera.drawWieldedTool(&rightMove);
450 hud.drawHotbar(client.getPlayerItem());
451 hud.drawLuaElements(camera.getOffset());
452 camera.drawNametags();
457 camera.getCameraNode()->setPosition(oldPosition);
458 camera.getCameraNode()->setTarget(oldTarget);
462 void draw_plain(Camera &camera, bool show_hud, Hud &hud,
463 video::IVideoDriver *driver, bool draw_wield_tool,
464 Client &client, gui::IGUIEnvironment *guienv)
466 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
468 hud.drawSelectionMesh();
469 if (draw_wield_tool) {
470 camera.drawWieldedTool();
475 void draw_scene(video::IVideoDriver *driver, scene::ISceneManager *smgr,
476 Camera &camera, Client& client, LocalPlayer *player, Hud &hud,
477 Minimap &mapper, gui::IGUIEnvironment *guienv,
478 const v2u32 &screensize, const video::SColor &skycolor,
479 bool show_hud, bool show_minimap)
481 TimeTaker timer("smgr");
483 bool draw_wield_tool = (show_hud &&
484 (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) &&
485 camera.getCameraMode() < CAMERA_MODE_THIRD );
487 bool draw_crosshair = ((player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) &&
488 (camera.getCameraMode() != CAMERA_MODE_THIRD_FRONT));
490 #ifdef HAVE_TOUCHSCREENGUI
492 draw_crosshair = !g_settings->getBool("touchtarget");
494 catch(SettingNotFoundException) {}
497 const std::string &draw_mode = g_settings->get("3d_mode");
501 if (draw_mode == "anaglyph")
503 draw_anaglyph_3d_mode(camera, show_hud, hud, driver,
504 smgr, draw_wield_tool, client, guienv);
505 draw_crosshair = false;
507 else if (draw_mode == "interlaced")
509 draw_interlaced_3d_mode(camera, show_hud, hud, driver,
510 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
511 draw_crosshair = false;
513 else if (draw_mode == "sidebyside")
515 draw_sidebyside_3d_mode(camera, show_hud, hud, driver,
516 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
519 else if (draw_mode == "topbottom")
521 draw_top_bottom_3d_mode(camera, show_hud, hud, driver,
522 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
525 else if (draw_mode == "pageflip")
527 draw_pageflip_3d_mode(camera, show_hud, hud, driver,
528 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
529 draw_crosshair = false;
533 draw_plain(camera, show_hud, hud, driver,
534 draw_wield_tool, client, guienv);
541 client.getEnv().getClientMap().renderPostFx(camera.getCameraMode());
544 //TODO how to make those 3d too
550 hud.drawHotbar(client.getPlayerItem());
551 hud.drawLuaElements(camera.getOffset());
552 camera.drawNametags();
555 mapper.drawMinimap();
564 Draws a screen with a single text on it.
565 Text will be removed when the screen is drawn the next time.
566 Additionally, a progressbar can be drawn when percent is set between 0 and 100.
568 void draw_load_screen(const std::wstring &text, IrrlichtDevice* device,
569 gui::IGUIEnvironment* guienv, float dtime, int percent, bool clouds )
571 video::IVideoDriver* driver = device->getVideoDriver();
572 v2u32 screensize = porting::getWindowSize();
574 v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
575 v2s32 center(screensize.X / 2, screensize.Y / 2);
576 core::rect<s32> textrect(center - textsize / 2, center + textsize / 2);
578 gui::IGUIStaticText *guitext = guienv->addStaticText(
579 text.c_str(), textrect, false, false);
580 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
582 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
583 if (cloud_menu_background)
585 g_menuclouds->step(dtime*3);
586 g_menuclouds->render();
587 driver->beginScene(true, true, video::SColor(255, 140, 186, 250));
588 g_menucloudsmgr->drawAll();
591 driver->beginScene(true, true, video::SColor(255, 0, 0, 0));
594 if ((percent >= 0) && (percent <= 100)) {
595 const std::string &texture_path = g_settings->get("texture_path");
596 std::string tp_progress_bar = texture_path + "/progress_bar.png";
597 std::string tp_progress_bar_bg = texture_path + "/progress_bar_bg.png";
599 if (!(fs::PathExists(tp_progress_bar) &&
600 fs::PathExists(tp_progress_bar_bg))) {
601 std::string gamepath = fs::RemoveRelativePathComponents(
602 porting::path_share + DIR_DELIM + "textures");
603 tp_progress_bar = gamepath + "/base/pack/progress_bar.png";
604 tp_progress_bar_bg = gamepath + "/base/pack/progress_bar_bg.png";
607 video::ITexture *progress_img =
608 driver->getTexture(tp_progress_bar.c_str());
609 video::ITexture *progress_img_bg =
610 driver->getTexture(tp_progress_bar_bg.c_str());
612 if (progress_img && progress_img_bg) {
613 const core::dimension2d<u32> &img_size = progress_img_bg->getSize();
614 u32 imgW = rangelim(img_size.Width, 200, 600);
615 u32 imgH = rangelim(img_size.Height, 24, 72);
616 v2s32 img_pos((screensize.X - imgW) / 2, (screensize.Y - imgH) / 2);
618 draw2DImageFilterScaled(
619 driver, progress_img_bg,
620 core::rect<s32>(img_pos.X,
624 core::rect<s32>(0, 0,
629 draw2DImageFilterScaled(
630 driver, progress_img,
631 core::rect<s32>(img_pos.X,
633 img_pos.X + (percent * imgW) / 100,
635 core::rect<s32>(0, 0,
636 (percent * img_size.Width) / 100,