]> git.lizzy.rs Git - minetest.git/blob - src/client/renderingengine.cpp
Initial Haiku support (#6568)
[minetest.git] / src / client / renderingengine.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <IrrlichtDevice.h>
22 #include <irrlicht.h>
23 #include "fontengine.h"
24 #include "client.h"
25 #include "clouds.h"
26 #include "util/numeric.h"
27 #include "guiscalingfilter.h"
28 #include "hud.h"
29 #include "localplayer.h"
30 #include "camera.h"
31 #include "minimap.h"
32 #include "clientmap.h"
33 #include "renderingengine.h"
34 #include "inputhandler.h"
35 #include "gettext.h"
36
37 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__) && \
38                 !defined(SERVER) && !defined(__HAIKU__)
39 #define XORG_USED
40 #endif
41 #ifdef XORG_USED
42 #include <X11/Xlib.h>
43 #include <X11/Xutil.h>
44 #endif
45
46 RenderingEngine *RenderingEngine::s_singleton = nullptr;
47
48 RenderingEngine::RenderingEngine(IEventReceiver *receiver)
49 {
50         sanity_check(!s_singleton);
51
52         // Resolution selection
53         bool fullscreen = g_settings->getBool("fullscreen");
54         u16 screen_w = g_settings->getU16("screen_w");
55         u16 screen_h = g_settings->getU16("screen_h");
56
57         // bpp, fsaa, vsync
58         bool vsync = g_settings->getBool("vsync");
59         u16 bits = g_settings->getU16("fullscreen_bpp");
60         u16 fsaa = g_settings->getU16("fsaa");
61
62         // stereo buffer required for pageflip stereo
63         bool stereo_buffer = g_settings->get("3d_mode") == "pageflip";
64
65         // Determine driver
66         video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
67         const std::string &driverstring = g_settings->get("video_driver");
68         std::vector<video::E_DRIVER_TYPE> drivers =
69                         RenderingEngine::getSupportedVideoDrivers();
70         u32 i;
71         for (i = 0; i != drivers.size(); i++) {
72                 if (!strcasecmp(driverstring.c_str(),
73                                     RenderingEngine::getVideoDriverName(drivers[i]))) {
74                         driverType = drivers[i];
75                         break;
76                 }
77         }
78         if (i == drivers.size()) {
79                 errorstream << "Invalid video_driver specified; "
80                                "defaulting to opengl"
81                             << std::endl;
82         }
83
84         SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
85         params.DriverType = driverType;
86         params.WindowSize = core::dimension2d<u32>(screen_w, screen_h);
87         params.Bits = bits;
88         params.AntiAlias = fsaa;
89         params.Fullscreen = fullscreen;
90         params.Stencilbuffer = false;
91         params.Stereobuffer = stereo_buffer;
92         params.Vsync = vsync;
93         params.EventReceiver = receiver;
94         params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
95         params.ZBufferBits = 24;
96 #ifdef __ANDROID__
97         // clang-format off
98         params.PrivateData = porting::app_global;
99         params.OGLES2ShaderPath = std::string(porting::path_user + DIR_DELIM + "media" +
100                 DIR_DELIM + "Shaders" + DIR_DELIM).c_str();
101         // clang-format on
102 #endif
103
104         m_device = createDeviceEx(params);
105         s_singleton = this;
106 }
107
108 RenderingEngine::~RenderingEngine()
109 {
110         m_device->drop();
111         s_singleton = nullptr;
112 }
113
114 v2u32 RenderingEngine::getWindowSize() const
115 {
116         return m_device->getVideoDriver()->getScreenSize();
117 }
118
119 void RenderingEngine::setResizable(bool resize)
120 {
121         m_device->setResizable(resize);
122 }
123
124 video::IVideoDriver *RenderingEngine::getVideoDriver()
125 {
126         return m_device->getVideoDriver();
127 }
128
129 bool RenderingEngine::print_video_modes()
130 {
131         IrrlichtDevice *nulldevice;
132
133         bool vsync = g_settings->getBool("vsync");
134         u16 fsaa = g_settings->getU16("fsaa");
135         MyEventReceiver *receiver = new MyEventReceiver();
136
137         SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
138         params.DriverType = video::EDT_NULL;
139         params.WindowSize = core::dimension2d<u32>(640, 480);
140         params.Bits = 24;
141         params.AntiAlias = fsaa;
142         params.Fullscreen = false;
143         params.Stencilbuffer = false;
144         params.Vsync = vsync;
145         params.EventReceiver = receiver;
146         params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
147
148         nulldevice = createDeviceEx(params);
149
150         if (!nulldevice) {
151                 delete receiver;
152                 return false;
153         }
154
155         std::cout << _("Available video modes (WxHxD):") << std::endl;
156
157         video::IVideoModeList *videomode_list = nulldevice->getVideoModeList();
158
159         if (videomode_list != NULL) {
160                 s32 videomode_count = videomode_list->getVideoModeCount();
161                 core::dimension2d<u32> videomode_res;
162                 s32 videomode_depth;
163                 for (s32 i = 0; i < videomode_count; ++i) {
164                         videomode_res = videomode_list->getVideoModeResolution(i);
165                         videomode_depth = videomode_list->getVideoModeDepth(i);
166                         std::cout << videomode_res.Width << "x" << videomode_res.Height
167                                   << "x" << videomode_depth << std::endl;
168                 }
169
170                 std::cout << _("Active video mode (WxHxD):") << std::endl;
171                 videomode_res = videomode_list->getDesktopResolution();
172                 videomode_depth = videomode_list->getDesktopDepth();
173                 std::cout << videomode_res.Width << "x" << videomode_res.Height << "x"
174                           << videomode_depth << std::endl;
175         }
176
177         nulldevice->drop();
178         delete receiver;
179
180         return videomode_list != NULL;
181 }
182
183 void RenderingEngine::setXorgClassHint(
184                 const video::SExposedVideoData &video_data, const std::string &name)
185 {
186 #ifdef XORG_USED
187         if (video_data.OpenGLLinux.X11Display == NULL)
188                 return;
189
190         XClassHint *classhint = XAllocClassHint();
191         classhint->res_name = (char *)name.c_str();
192         classhint->res_class = (char *)name.c_str();
193
194         XSetClassHint((Display *)video_data.OpenGLLinux.X11Display,
195                         video_data.OpenGLLinux.X11Window, classhint);
196         XFree(classhint);
197 #endif
198 }
199
200 bool RenderingEngine::setWindowIcon()
201 {
202 #if defined(XORG_USED)
203 #if RUN_IN_PLACE
204         return setXorgWindowIconFromPath(
205                         porting::path_share + "/misc/" PROJECT_NAME "-xorg-icon-128.png");
206 #else
207         // We have semi-support for reading in-place data if we are
208         // compiled with RUN_IN_PLACE. Don't break with this and
209         // also try the path_share location.
210         return setXorgWindowIconFromPath(
211                                ICON_DIR "/hicolor/128x128/apps/" PROJECT_NAME ".png") ||
212                setXorgWindowIconFromPath(porting::path_share + "/misc/" PROJECT_NAME
213                                                                "-xorg-icon-128.png");
214 #endif
215 #elif defined(_WIN32)
216         const video::SExposedVideoData exposedData =
217                         m_device->getVideoDriver()->getExposedVideoData();
218         HWND hWnd; // Window handle
219
220         switch (m_device->getVideoDriver()->getDriverType()) {
221         case video::EDT_DIRECT3D8:
222                 hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
223                 break;
224         case video::EDT_DIRECT3D9:
225                 hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
226                 break;
227         case video::EDT_OPENGL:
228                 hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
229                 break;
230         default:
231                 return false;
232         }
233
234         // Load the ICON from resource file
235         const HICON hicon = LoadIcon(GetModuleHandle(NULL),
236                         MAKEINTRESOURCE(130) // The ID of the ICON defined in
237                                              // winresource.rc
238         );
239
240         if (hicon) {
241                 SendMessage(hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hicon));
242                 SendMessage(hWnd, WM_SETICON, ICON_SMALL,
243                                 reinterpret_cast<LPARAM>(hicon));
244                 return true;
245         }
246         return false;
247 #else
248         return false;
249 #endif
250 }
251
252 bool RenderingEngine::setXorgWindowIconFromPath(const std::string &icon_file)
253 {
254 #ifdef XORG_USED
255
256         video::IVideoDriver *v_driver = m_device->getVideoDriver();
257
258         video::IImageLoader *image_loader = NULL;
259         u32 cnt = v_driver->getImageLoaderCount();
260         for (u32 i = 0; i < cnt; i++) {
261                 if (v_driver->getImageLoader(i)->isALoadableFileExtension(
262                                     icon_file.c_str())) {
263                         image_loader = v_driver->getImageLoader(i);
264                         break;
265                 }
266         }
267
268         if (!image_loader) {
269                 warningstream << "Could not find image loader for file '" << icon_file
270                               << "'" << std::endl;
271                 return false;
272         }
273
274         io::IReadFile *icon_f =
275                         m_device->getFileSystem()->createAndOpenFile(icon_file.c_str());
276
277         if (!icon_f) {
278                 warningstream << "Could not load icon file '" << icon_file << "'"
279                               << std::endl;
280                 return false;
281         }
282
283         video::IImage *img = image_loader->loadImage(icon_f);
284
285         if (!img) {
286                 warningstream << "Could not load icon file '" << icon_file << "'"
287                               << std::endl;
288                 icon_f->drop();
289                 return false;
290         }
291
292         u32 height = img->getDimension().Height;
293         u32 width = img->getDimension().Width;
294
295         size_t icon_buffer_len = 2 + height * width;
296         long *icon_buffer = new long[icon_buffer_len];
297
298         icon_buffer[0] = width;
299         icon_buffer[1] = height;
300
301         for (u32 x = 0; x < width; x++) {
302                 for (u32 y = 0; y < height; y++) {
303                         video::SColor col = img->getPixel(x, y);
304                         long pixel_val = 0;
305                         pixel_val |= (u8)col.getAlpha() << 24;
306                         pixel_val |= (u8)col.getRed() << 16;
307                         pixel_val |= (u8)col.getGreen() << 8;
308                         pixel_val |= (u8)col.getBlue();
309                         icon_buffer[2 + x + y * width] = pixel_val;
310                 }
311         }
312
313         img->drop();
314         icon_f->drop();
315
316         const video::SExposedVideoData &video_data = v_driver->getExposedVideoData();
317
318         Display *x11_dpl = (Display *)video_data.OpenGLLinux.X11Display;
319
320         if (x11_dpl == NULL) {
321                 warningstream << "Could not find x11 display for setting its icon."
322                               << std::endl;
323                 delete[] icon_buffer;
324                 return false;
325         }
326
327         Window x11_win = (Window)video_data.OpenGLLinux.X11Window;
328
329         Atom net_wm_icon = XInternAtom(x11_dpl, "_NET_WM_ICON", False);
330         Atom cardinal = XInternAtom(x11_dpl, "CARDINAL", False);
331         XChangeProperty(x11_dpl, x11_win, net_wm_icon, cardinal, 32, PropModeReplace,
332                         (const unsigned char *)icon_buffer, icon_buffer_len);
333
334         delete[] icon_buffer;
335
336 #endif
337         return true;
338 }
339
340 /*
341         Draws a screen with a single text on it.
342         Text will be removed when the screen is drawn the next time.
343         Additionally, a progressbar can be drawn when percent is set between 0 and 100.
344 */
345 void RenderingEngine::_draw_load_screen(const std::wstring &text,
346                 gui::IGUIEnvironment *guienv, ITextureSource *tsrc, float dtime,
347                 int percent, bool clouds)
348 {
349         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
350
351         v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
352         v2s32 center(screensize.X / 2, screensize.Y / 2);
353         core::rect<s32> textrect(center - textsize / 2, center + textsize / 2);
354
355         gui::IGUIStaticText *guitext =
356                         guienv->addStaticText(text.c_str(), textrect, false, false);
357         guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
358
359         bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
360         if (cloud_menu_background) {
361                 g_menuclouds->step(dtime * 3);
362                 g_menuclouds->render();
363                 get_video_driver()->beginScene(
364                                 true, true, video::SColor(255, 140, 186, 250));
365                 g_menucloudsmgr->drawAll();
366         } else
367                 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
368
369         // draw progress bar
370         if ((percent >= 0) && (percent <= 100)) {
371                 video::ITexture *progress_img = tsrc->getTexture("progress_bar.png");
372                 video::ITexture *progress_img_bg =
373                                 tsrc->getTexture("progress_bar_bg.png");
374
375                 if (progress_img && progress_img_bg) {
376 #ifndef __ANDROID__
377                         const core::dimension2d<u32> &img_size =
378                                         progress_img_bg->getSize();
379                         u32 imgW = rangelim(img_size.Width, 200, 600);
380                         u32 imgH = rangelim(img_size.Height, 24, 72);
381 #else
382                         const core::dimension2d<u32> img_size(256, 48);
383                         float imgRatio = (float)img_size.Height / img_size.Width;
384                         u32 imgW = screensize.X / 2.2f;
385                         u32 imgH = floor(imgW * imgRatio);
386 #endif
387                         v2s32 img_pos((screensize.X - imgW) / 2,
388                                         (screensize.Y - imgH) / 2);
389
390                         draw2DImageFilterScaled(get_video_driver(), progress_img_bg,
391                                         core::rect<s32>(img_pos.X, img_pos.Y,
392                                                         img_pos.X + imgW,
393                                                         img_pos.Y + imgH),
394                                         core::rect<s32>(0, 0, img_size.Width,
395                                                         img_size.Height),
396                                         0, 0, true);
397
398                         draw2DImageFilterScaled(get_video_driver(), progress_img,
399                                         core::rect<s32>(img_pos.X, img_pos.Y,
400                                                         img_pos.X + (percent * imgW) / 100,
401                                                         img_pos.Y + imgH),
402                                         core::rect<s32>(0, 0,
403                                                         (percent * img_size.Width) / 100,
404                                                         img_size.Height),
405                                         0, 0, true);
406                 }
407         }
408
409         guienv->drawAll();
410         get_video_driver()->endScene();
411         guitext->remove();
412 }
413
414 std::vector<core::vector3d<u32>> RenderingEngine::getSupportedVideoModes()
415 {
416         IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
417         sanity_check(nulldevice);
418
419         std::vector<core::vector3d<u32>> mlist;
420         video::IVideoModeList *modelist = nulldevice->getVideoModeList();
421
422         s32 num_modes = modelist->getVideoModeCount();
423         for (s32 i = 0; i != num_modes; i++) {
424                 core::dimension2d<u32> mode_res = modelist->getVideoModeResolution(i);
425                 u32 mode_depth = (u32)modelist->getVideoModeDepth(i);
426                 mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth);
427         }
428
429         nulldevice->drop();
430         return mlist;
431 }
432
433 std::vector<irr::video::E_DRIVER_TYPE> RenderingEngine::getSupportedVideoDrivers()
434 {
435         std::vector<irr::video::E_DRIVER_TYPE> drivers;
436
437         for (int i = 0; i != irr::video::EDT_COUNT; i++) {
438                 if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i))
439                         drivers.push_back((irr::video::E_DRIVER_TYPE)i);
440         }
441
442         return drivers;
443 }
444
445 void RenderingEngine::_draw_scene(Camera *camera, Client *client, LocalPlayer *player,
446                 Hud *hud, Minimap *mapper, gui::IGUIEnvironment *guienv,
447                 const v2u32 &screensize, const video::SColor &skycolor, bool show_hud,
448                 bool show_minimap)
449 {
450         bool draw_wield_tool =
451                         (show_hud && (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) &&
452                                         camera->getCameraMode() < CAMERA_MODE_THIRD);
453
454         bool draw_crosshair = ((player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) &&
455                                (camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT));
456
457 #ifdef HAVE_TOUCHSCREENGUI
458         try {
459                 draw_crosshair = !g_settings->getBool("touchtarget");
460         } catch (SettingNotFoundException) {
461         }
462 #endif
463
464         const std::string &draw_mode = g_settings->get("3d_mode");
465
466         if (draw_mode == "anaglyph") {
467                 draw_anaglyph_3d_mode(
468                                 camera, show_hud, hud, draw_wield_tool, client, guienv);
469                 draw_crosshair = false;
470         } else if (draw_mode == "interlaced") {
471                 draw_interlaced_3d_mode(camera, show_hud, hud, screensize,
472                                 draw_wield_tool, client, guienv, skycolor);
473                 draw_crosshair = false;
474         } else if (draw_mode == "sidebyside") {
475                 draw_sidebyside_3d_mode(camera, show_hud, hud, screensize,
476                                 draw_wield_tool, client, guienv, skycolor);
477                 show_hud = false;
478         } else if (draw_mode == "topbottom") {
479                 draw_top_bottom_3d_mode(camera, show_hud, hud, screensize,
480                                 draw_wield_tool, client, guienv, skycolor);
481                 show_hud = false;
482         } else if (draw_mode == "pageflip") {
483                 draw_pageflip_3d_mode(camera, show_hud, hud, screensize, draw_wield_tool,
484                                 client, guienv, skycolor);
485                 draw_crosshair = false;
486                 show_hud = false;
487         } else {
488                 draw_plain(camera, show_hud, hud, screensize, draw_wield_tool, client,
489                                 guienv, skycolor);
490         }
491
492         /*
493                 Post effects
494         */
495         client->getEnv().getClientMap().renderPostFx(camera->getCameraMode());
496
497         // TODO how to make those 3d too
498         if (show_hud) {
499                 if (draw_crosshair)
500                         hud->drawCrosshair();
501
502                 hud->drawHotbar(client->getPlayerItem());
503                 hud->drawLuaElements(camera->getOffset());
504                 camera->drawNametags();
505
506                 if (mapper && show_minimap)
507                         mapper->drawMinimap();
508         }
509
510         guienv->drawAll();
511 }
512
513 void RenderingEngine::draw_anaglyph_3d_mode(Camera *camera, bool show_hud, Hud *hud,
514                 bool draw_wield_tool, Client *client, gui::IGUIEnvironment *guienv)
515 {
516
517         /* preserve old setup*/
518         irr::core::vector3df oldPosition = camera->getCameraNode()->getPosition();
519         irr::core::vector3df oldTarget = camera->getCameraNode()->getTarget();
520
521         irr::core::matrix4 startMatrix =
522                         camera->getCameraNode()->getAbsoluteTransformation();
523         irr::core::vector3df focusPoint =
524                         (camera->getCameraNode()->getTarget() -
525                                         camera->getCameraNode()->getAbsolutePosition())
526                                         .setLength(1) +
527                         camera->getCameraNode()->getAbsolutePosition();
528
529         // Left eye...
530         irr::core::vector3df leftEye;
531         irr::core::matrix4 leftMove;
532         leftMove.setTranslation(irr::core::vector3df(
533                         -g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
534         leftEye = (startMatrix * leftMove).getTranslation();
535
536         // clear the depth buffer, and color
537         getVideoDriver()->beginScene(true, true, irr::video::SColor(0, 200, 200, 255));
538         getVideoDriver()->getOverrideMaterial().Material.ColorMask = irr::video::ECP_RED;
539         getVideoDriver()->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
540         getVideoDriver()->getOverrideMaterial().EnablePasses =
541                         irr::scene::ESNRP_SKY_BOX + irr::scene::ESNRP_SOLID +
542                         irr::scene::ESNRP_TRANSPARENT +
543                         irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
544         camera->getCameraNode()->setPosition(leftEye);
545         camera->getCameraNode()->setTarget(focusPoint);
546         get_scene_manager()->drawAll();
547         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
548         if (show_hud) {
549                 hud->drawSelectionMesh();
550                 if (draw_wield_tool)
551                         camera->drawWieldedTool(&leftMove);
552         }
553
554         guienv->drawAll();
555
556         // Right eye...
557         irr::core::vector3df rightEye;
558         irr::core::matrix4 rightMove;
559         rightMove.setTranslation(irr::core::vector3df(
560                         g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
561         rightEye = (startMatrix * rightMove).getTranslation();
562
563         // clear the depth buffer
564         getVideoDriver()->clearZBuffer();
565         getVideoDriver()->getOverrideMaterial().Material.ColorMask =
566                         irr::video::ECP_GREEN + irr::video::ECP_BLUE;
567         getVideoDriver()->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
568         getVideoDriver()->getOverrideMaterial().EnablePasses =
569                         irr::scene::ESNRP_SKY_BOX + irr::scene::ESNRP_SOLID +
570                         irr::scene::ESNRP_TRANSPARENT +
571                         irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
572         camera->getCameraNode()->setPosition(rightEye);
573         camera->getCameraNode()->setTarget(focusPoint);
574         get_scene_manager()->drawAll();
575         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
576         if (show_hud) {
577                 hud->drawSelectionMesh();
578                 if (draw_wield_tool)
579                         camera->drawWieldedTool(&rightMove);
580         }
581
582         guienv->drawAll();
583
584         getVideoDriver()->getOverrideMaterial().Material.ColorMask = irr::video::ECP_ALL;
585         getVideoDriver()->getOverrideMaterial().EnableFlags = 0;
586         getVideoDriver()->getOverrideMaterial().EnablePasses = 0;
587         camera->getCameraNode()->setPosition(oldPosition);
588         camera->getCameraNode()->setTarget(oldTarget);
589 }
590
591 void RenderingEngine::init_texture(
592                 const v2u32 &screensize, video::ITexture **texture, const char *name)
593 {
594         if (*texture) {
595                 getVideoDriver()->removeTexture(*texture);
596         }
597         *texture = getVideoDriver()->addRenderTargetTexture(
598                         core::dimension2d<u32>(screensize.X, screensize.Y), name,
599                         irr::video::ECF_A8R8G8B8);
600 }
601
602 video::ITexture *RenderingEngine::draw_image(const v2u32 &screensize, parallax_sign psign,
603                 const irr::core::matrix4 &startMatrix,
604                 const irr::core::vector3df &focusPoint, bool show_hud, Camera *camera,
605                 Hud *hud, bool draw_wield_tool, Client *client,
606                 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
607 {
608         static video::ITexture *images[2] = {NULL, NULL};
609         static v2u32 last_screensize = v2u32(0, 0);
610
611         video::ITexture *image = NULL;
612
613         if (screensize != last_screensize) {
614                 init_texture(screensize, &images[1], "mt_drawimage_img1");
615                 init_texture(screensize, &images[0], "mt_drawimage_img2");
616                 last_screensize = screensize;
617         }
618
619         if (psign == RIGHT)
620                 image = images[1];
621         else
622                 image = images[0];
623
624         getVideoDriver()->setRenderTarget(image, true, true,
625                         irr::video::SColor(255, skycolor.getRed(), skycolor.getGreen(),
626                                         skycolor.getBlue()));
627
628         irr::core::vector3df eye_pos;
629         irr::core::matrix4 movement;
630         movement.setTranslation(irr::core::vector3df(
631                         (int)psign * g_settings->getFloat("3d_paralax_strength"), 0.0f,
632                         0.0f));
633         eye_pos = (startMatrix * movement).getTranslation();
634
635         // clear the depth buffer
636         getVideoDriver()->clearZBuffer();
637         camera->getCameraNode()->setPosition(eye_pos);
638         camera->getCameraNode()->setTarget(focusPoint);
639         get_scene_manager()->drawAll();
640
641         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
642
643         if (show_hud) {
644                 hud->drawSelectionMesh();
645                 if (draw_wield_tool)
646                         camera->drawWieldedTool(&movement);
647         }
648
649         guienv->drawAll();
650
651         /* switch back to real renderer */
652         getVideoDriver()->setRenderTarget(0, true, true,
653                         irr::video::SColor(0, skycolor.getRed(), skycolor.getGreen(),
654                                         skycolor.getBlue()));
655
656         return image;
657 }
658
659 video::ITexture *RenderingEngine::draw_hud(const v2u32 &screensize, bool show_hud,
660                 Hud *hud, Client *client, bool draw_crosshair,
661                 const video::SColor &skycolor, gui::IGUIEnvironment *guienv,
662                 Camera *camera)
663 {
664         static video::ITexture *image = nullptr;
665         init_texture(screensize, &image, "mt_drawimage_hud");
666         getVideoDriver()->setRenderTarget(
667                         image, true, true, irr::video::SColor(255, 0, 0, 0));
668
669         if (show_hud) {
670                 if (draw_crosshair)
671                         hud->drawCrosshair();
672                 hud->drawHotbar(client->getPlayerItem());
673                 hud->drawLuaElements(camera->getOffset());
674                 camera->drawNametags();
675                 guienv->drawAll();
676         }
677
678         getVideoDriver()->setRenderTarget(0, true, true,
679                         irr::video::SColor(0, skycolor.getRed(), skycolor.getGreen(),
680                                         skycolor.getBlue()));
681
682         return image;
683 }
684
685 void RenderingEngine::draw_interlaced_3d_mode(Camera *camera, bool show_hud, Hud *hud,
686                 const v2u32 &screensize, bool draw_wield_tool, Client *client,
687                 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
688 {
689         /* save current info */
690         irr::core::vector3df oldPosition = camera->getCameraNode()->getPosition();
691         irr::core::vector3df oldTarget = camera->getCameraNode()->getTarget();
692         irr::core::matrix4 startMatrix =
693                         camera->getCameraNode()->getAbsoluteTransformation();
694         irr::core::vector3df focusPoint =
695                         (camera->getCameraNode()->getTarget() -
696                                         camera->getCameraNode()->getAbsolutePosition())
697                                         .setLength(1) +
698                         camera->getCameraNode()->getAbsolutePosition();
699
700         /* create left view */
701         video::ITexture *left_image = draw_image(screensize, LEFT, startMatrix,
702                         focusPoint, show_hud, camera, hud, draw_wield_tool, client,
703                         guienv, skycolor);
704
705         // Right eye...
706         irr::core::vector3df rightEye;
707         irr::core::matrix4 rightMove;
708         rightMove.setTranslation(irr::core::vector3df(
709                         g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
710         rightEye = (startMatrix * rightMove).getTranslation();
711
712         // clear the depth buffer
713         getVideoDriver()->clearZBuffer();
714         camera->getCameraNode()->setPosition(rightEye);
715         camera->getCameraNode()->setTarget(focusPoint);
716         get_scene_manager()->drawAll();
717
718         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
719
720         if (show_hud) {
721                 hud->drawSelectionMesh();
722                 if (draw_wield_tool)
723                         camera->drawWieldedTool(&rightMove);
724         }
725         guienv->drawAll();
726
727         for (unsigned int i = 0; i < screensize.Y; i += 2) {
728 #if (IRRLICHT_VERSION_MAJOR >= 1) && (IRRLICHT_VERSION_MINOR >= 8)
729                 getVideoDriver()->draw2DImage(left_image,
730                                 irr::core::position2d<s32>(0, i),
731 #else
732                 getVideoDriver()->draw2DImage(left_image,
733                                 irr::core::position2d<s32>(0, screensize.Y - i),
734 #endif
735                                 irr::core::rect<s32>(0, i, screensize.X, i + 1), 0,
736                                 irr::video::SColor(255, 255, 255, 255), false);
737         }
738
739         /* cleanup */
740         camera->getCameraNode()->setPosition(oldPosition);
741         camera->getCameraNode()->setTarget(oldTarget);
742 }
743
744 void RenderingEngine::draw_sidebyside_3d_mode(Camera *camera, bool show_hud, Hud *hud,
745                 const v2u32 &screensize, bool draw_wield_tool, Client *client,
746                 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
747 {
748         /* save current info */
749         irr::core::vector3df oldPosition = camera->getCameraNode()->getPosition();
750         irr::core::vector3df oldTarget = camera->getCameraNode()->getTarget();
751         irr::core::matrix4 startMatrix =
752                         camera->getCameraNode()->getAbsoluteTransformation();
753         irr::core::vector3df focusPoint =
754                         (camera->getCameraNode()->getTarget() -
755                                         camera->getCameraNode()->getAbsolutePosition())
756                                         .setLength(1) +
757                         camera->getCameraNode()->getAbsolutePosition();
758
759         /* create left view */
760         video::ITexture *left_image = draw_image(screensize, LEFT, startMatrix,
761                         focusPoint, show_hud, camera, hud, draw_wield_tool, client,
762                         guienv, skycolor);
763
764         /* create right view */
765         video::ITexture *right_image = draw_image(screensize, RIGHT, startMatrix,
766                         focusPoint, show_hud, camera, hud, draw_wield_tool, client,
767                         guienv, skycolor);
768
769         /* create hud overlay */
770         video::ITexture *hudtexture = draw_hud(screensize, show_hud, hud, client, false,
771                         skycolor, guienv, camera);
772         getVideoDriver()->makeColorKeyTexture(
773                         hudtexture, irr::video::SColor(255, 0, 0, 0));
774         // makeColorKeyTexture mirrors texture so we do it twice to get it right again
775         getVideoDriver()->makeColorKeyTexture(
776                         hudtexture, irr::video::SColor(255, 0, 0, 0));
777
778         draw2DImageFilterScaled(getVideoDriver(), left_image,
779                         irr::core::rect<s32>(0, 0, screensize.X / 2, screensize.Y),
780                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
781                         false);
782
783         draw2DImageFilterScaled(getVideoDriver(), hudtexture,
784                         irr::core::rect<s32>(0, 0, screensize.X / 2, screensize.Y),
785                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
786                         true);
787
788         draw2DImageFilterScaled(getVideoDriver(), right_image,
789                         irr::core::rect<s32>(
790                                         screensize.X / 2, 0, screensize.X, screensize.Y),
791                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
792                         false);
793
794         draw2DImageFilterScaled(getVideoDriver(), hudtexture,
795                         irr::core::rect<s32>(
796                                         screensize.X / 2, 0, screensize.X, screensize.Y),
797                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
798                         true);
799
800         left_image = nullptr;
801         right_image = nullptr;
802
803         /* cleanup */
804         camera->getCameraNode()->setPosition(oldPosition);
805         camera->getCameraNode()->setTarget(oldTarget);
806 }
807
808 void RenderingEngine::draw_top_bottom_3d_mode(Camera *camera, bool show_hud, Hud *hud,
809                 const v2u32 &screensize, bool draw_wield_tool, Client *client,
810                 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
811 {
812         /* save current info */
813         irr::core::vector3df oldPosition = camera->getCameraNode()->getPosition();
814         irr::core::vector3df oldTarget = camera->getCameraNode()->getTarget();
815         irr::core::matrix4 startMatrix =
816                         camera->getCameraNode()->getAbsoluteTransformation();
817         irr::core::vector3df focusPoint =
818                         (camera->getCameraNode()->getTarget() -
819                                         camera->getCameraNode()->getAbsolutePosition())
820                                         .setLength(1) +
821                         camera->getCameraNode()->getAbsolutePosition();
822
823         /* create left view */
824         video::ITexture *left_image = draw_image(screensize, LEFT, startMatrix,
825                         focusPoint, show_hud, camera, hud, draw_wield_tool, client,
826                         guienv, skycolor);
827
828         /* create right view */
829         video::ITexture *right_image = draw_image(screensize, RIGHT, startMatrix,
830                         focusPoint, show_hud, camera, hud, draw_wield_tool, client,
831                         guienv, skycolor);
832
833         /* create hud overlay */
834         video::ITexture *hudtexture = draw_hud(screensize, show_hud, hud, client, false,
835                         skycolor, guienv, camera);
836         getVideoDriver()->makeColorKeyTexture(
837                         hudtexture, irr::video::SColor(255, 0, 0, 0));
838         // makeColorKeyTexture mirrors texture so we do it twice to get it right again
839         getVideoDriver()->makeColorKeyTexture(
840                         hudtexture, irr::video::SColor(255, 0, 0, 0));
841
842         draw2DImageFilterScaled(getVideoDriver(), left_image,
843                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y / 2),
844                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
845                         false);
846
847         draw2DImageFilterScaled(getVideoDriver(), hudtexture,
848                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y / 2),
849                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
850                         true);
851
852         draw2DImageFilterScaled(getVideoDriver(), right_image,
853                         irr::core::rect<s32>(
854                                         0, screensize.Y / 2, screensize.X, screensize.Y),
855                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
856                         false);
857
858         draw2DImageFilterScaled(getVideoDriver(), hudtexture,
859                         irr::core::rect<s32>(
860                                         0, screensize.Y / 2, screensize.X, screensize.Y),
861                         irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0,
862                         true);
863
864         left_image = NULL;
865         right_image = NULL;
866
867         /* cleanup */
868         camera->getCameraNode()->setPosition(oldPosition);
869         camera->getCameraNode()->setTarget(oldTarget);
870 }
871
872 void RenderingEngine::draw_pageflip_3d_mode(Camera *camera, bool show_hud, Hud *hud,
873                 const v2u32 &screensize, bool draw_wield_tool, Client *client,
874                 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
875 {
876 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
877         errorstream << "Pageflip 3D mode is not supported"
878                     << " with your Irrlicht version!" << std::endl;
879 #else
880         /* preserve old setup*/
881         irr::core::vector3df oldPosition = camera->getCameraNode()->getPosition();
882         irr::core::vector3df oldTarget = camera->getCameraNode()->getTarget();
883
884         irr::core::matrix4 startMatrix =
885                         camera->getCameraNode()->getAbsoluteTransformation();
886         irr::core::vector3df focusPoint =
887                         (camera->getCameraNode()->getTarget() -
888                                         camera->getCameraNode()->getAbsolutePosition())
889                                         .setLength(1) +
890                         camera->getCameraNode()->getAbsolutePosition();
891
892         // Left eye...
893         getVideoDriver()->setRenderTarget(irr::video::ERT_STEREO_LEFT_BUFFER);
894
895         irr::core::vector3df leftEye;
896         irr::core::matrix4 leftMove;
897         leftMove.setTranslation(irr::core::vector3df(
898                         -g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
899         leftEye = (startMatrix * leftMove).getTranslation();
900
901         // clear the depth buffer, and color
902         getVideoDriver()->beginScene(true, true, irr::video::SColor(200, 200, 200, 255));
903         camera->getCameraNode()->setPosition(leftEye);
904         camera->getCameraNode()->setTarget(focusPoint);
905         get_scene_manager()->drawAll();
906         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
907
908         if (show_hud) {
909                 hud->drawSelectionMesh();
910                 if (draw_wield_tool)
911                         camera->drawWieldedTool(&leftMove);
912                 hud->drawHotbar(client->getPlayerItem());
913                 hud->drawLuaElements(camera->getOffset());
914                 camera->drawNametags();
915         }
916
917         guienv->drawAll();
918
919         // Right eye...
920         getVideoDriver()->setRenderTarget(irr::video::ERT_STEREO_RIGHT_BUFFER);
921
922         irr::core::vector3df rightEye;
923         irr::core::matrix4 rightMove;
924         rightMove.setTranslation(irr::core::vector3df(
925                         g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
926         rightEye = (startMatrix * rightMove).getTranslation();
927
928         // clear the depth buffer, and color
929         getVideoDriver()->beginScene(true, true, irr::video::SColor(200, 200, 200, 255));
930         camera->getCameraNode()->setPosition(rightEye);
931         camera->getCameraNode()->setTarget(focusPoint);
932         get_scene_manager()->drawAll();
933         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
934
935         if (show_hud) {
936                 hud->drawSelectionMesh();
937                 if (draw_wield_tool)
938                         camera->drawWieldedTool(&rightMove);
939                 hud->drawHotbar(client->getPlayerItem());
940                 hud->drawLuaElements(camera->getOffset());
941                 camera->drawNametags();
942         }
943
944         guienv->drawAll();
945
946         camera->getCameraNode()->setPosition(oldPosition);
947         camera->getCameraNode()->setTarget(oldTarget);
948 #endif
949 }
950
951 // returns (size / coef), rounded upwards
952 inline int scaledown(int coef, int size)
953 {
954         return (size + coef - 1) / coef;
955 }
956
957 void RenderingEngine::draw_plain(Camera *camera, bool show_hud, Hud *hud,
958                 const v2u32 &screensize, bool draw_wield_tool, Client *client,
959                 gui::IGUIEnvironment *guienv, const video::SColor &skycolor)
960 {
961         // Undersampling-specific stuff
962         static video::ITexture *image = NULL;
963         static v2u32 last_pixelated_size = v2u32(0, 0);
964         static thread_local int undersampling = g_settings->getU16("undersampling");
965         v2u32 pixelated_size;
966         v2u32 dest_size;
967         if (undersampling > 0) {
968                 pixelated_size = v2u32(scaledown(undersampling, screensize.X),
969                                 scaledown(undersampling, screensize.Y));
970                 dest_size = v2u32(undersampling * pixelated_size.X,
971                                 undersampling * pixelated_size.Y);
972                 if (pixelated_size != last_pixelated_size) {
973                         init_texture(pixelated_size, &image, "mt_drawimage_img1");
974                         last_pixelated_size = pixelated_size;
975                 }
976                 getVideoDriver()->setRenderTarget(image, true, true, skycolor);
977         }
978
979         // Render
980         get_scene_manager()->drawAll();
981         getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
982         if (show_hud) {
983                 hud->drawSelectionMesh();
984                 if (draw_wield_tool) {
985                         camera->drawWieldedTool();
986                 }
987         }
988
989         // Upscale lowres render
990         if (undersampling > 0) {
991                 getVideoDriver()->setRenderTarget(0, true, true);
992                 getVideoDriver()->draw2DImage(image,
993                                 irr::core::rect<s32>(0, 0, dest_size.X, dest_size.Y),
994                                 irr::core::rect<s32>(0, 0, pixelated_size.X,
995                                                 pixelated_size.Y));
996         }
997 }
998
999 const char *RenderingEngine::getVideoDriverName(irr::video::E_DRIVER_TYPE type)
1000 {
1001         static const char *driver_ids[] = {
1002                         "null",
1003                         "software",
1004                         "burningsvideo",
1005                         "direct3d8",
1006                         "direct3d9",
1007                         "opengl",
1008                         "ogles1",
1009                         "ogles2",
1010         };
1011
1012         return driver_ids[type];
1013 }
1014
1015 const char *RenderingEngine::getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
1016 {
1017         static const char *driver_names[] = {
1018                         "NULL Driver",
1019                         "Software Renderer",
1020                         "Burning's Video",
1021                         "Direct3D 8",
1022                         "Direct3D 9",
1023                         "OpenGL",
1024                         "OpenGL ES1",
1025                         "OpenGL ES2",
1026         };
1027
1028         return driver_names[type];
1029 }
1030
1031 #ifndef __ANDROID__
1032 #ifdef XORG_USED
1033
1034 static float calcDisplayDensity()
1035 {
1036         const char *current_display = getenv("DISPLAY");
1037
1038         if (current_display != NULL) {
1039                 Display *x11display = XOpenDisplay(current_display);
1040
1041                 if (x11display != NULL) {
1042                         /* try x direct */
1043                         float dpi_height = floor(
1044                                         DisplayHeight(x11display, 0) /
1045                                                         (DisplayHeightMM(x11display, 0) *
1046                                                                         0.039370) +
1047                                         0.5);
1048                         float dpi_width = floor(
1049                                         DisplayWidth(x11display, 0) /
1050                                                         (DisplayWidthMM(x11display, 0) *
1051                                                                         0.039370) +
1052                                         0.5);
1053
1054                         XCloseDisplay(x11display);
1055
1056                         return std::max(dpi_height, dpi_width) / 96.0;
1057                 }
1058         }
1059
1060         /* return manually specified dpi */
1061         return g_settings->getFloat("screen_dpi") / 96.0;
1062 }
1063
1064 float RenderingEngine::getDisplayDensity()
1065 {
1066         static float cached_display_density = calcDisplayDensity();
1067         return cached_display_density;
1068 }
1069
1070 #else  // XORG_USED
1071 float RenderingEngine::getDisplayDensity()
1072 {
1073         return g_settings->getFloat("screen_dpi") / 96.0;
1074 }
1075 #endif // XORG_USED
1076
1077 v2u32 RenderingEngine::getDisplaySize()
1078 {
1079         IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
1080
1081         core::dimension2d<u32> deskres =
1082                         nulldevice->getVideoModeList()->getDesktopResolution();
1083         nulldevice->drop();
1084
1085         return deskres;
1086 }
1087 #endif // __ANDROID__