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"
21 #include "main.h" // for g_settings
24 #include "clientmap.h"
25 #include "util/timetaker.h"
34 void draw_selectionbox(video::IVideoDriver* driver, Hud& hud,
35 std::vector<aabb3f>& hilightboxes, bool show_hud)
37 static const s16 selectionbox_width = rangelim(g_settings->getS16("selectionbox_width"), 1, 5);
42 video::SMaterial oldmaterial = driver->getMaterial2D();
44 m.Thickness = selectionbox_width;
46 driver->setMaterial(m);
47 hud.drawSelectionBoxes(hilightboxes);
48 driver->setMaterial(oldmaterial);
51 void draw_anaglyph_3d_mode(Camera& camera, bool show_hud, Hud& hud,
52 std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
53 scene::ISceneManager* smgr, bool draw_wield_tool, Client& client,
54 gui::IGUIEnvironment* guienv )
57 /* preserve old setup*/
58 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
59 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
61 irr::core::matrix4 startMatrix =
62 camera.getCameraNode()->getAbsoluteTransformation();
63 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
64 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
65 + camera.getCameraNode()->getAbsolutePosition();
69 irr::core::vector3df leftEye;
70 irr::core::matrix4 leftMove;
71 leftMove.setTranslation(
72 irr::core::vector3df(-g_settings->getFloat("3d_paralax_strength"),
74 leftEye = (startMatrix * leftMove).getTranslation();
76 //clear the depth buffer, and color
77 driver->beginScene( true, true, irr::video::SColor(0, 200, 200, 255));
78 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_RED;
79 driver->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
80 driver->getOverrideMaterial().EnablePasses = irr::scene::ESNRP_SKY_BOX
81 + irr::scene::ESNRP_SOLID + irr::scene::ESNRP_TRANSPARENT
82 + irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
83 camera.getCameraNode()->setPosition(leftEye);
84 camera.getCameraNode()->setTarget(focusPoint);
86 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
89 draw_selectionbox(driver, hud, hilightboxes, show_hud);
92 camera.drawWieldedTool(&leftMove);
98 irr::core::vector3df rightEye;
99 irr::core::matrix4 rightMove;
100 rightMove.setTranslation(
101 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
103 rightEye = (startMatrix * rightMove).getTranslation();
105 //clear the depth buffer
106 driver->clearZBuffer();
107 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_GREEN
108 + irr::video::ECP_BLUE;
109 driver->getOverrideMaterial().EnableFlags = irr::video::EMF_COLOR_MASK;
110 driver->getOverrideMaterial().EnablePasses = irr::scene::ESNRP_SKY_BOX
111 + irr::scene::ESNRP_SOLID + irr::scene::ESNRP_TRANSPARENT
112 + irr::scene::ESNRP_TRANSPARENT_EFFECT + irr::scene::ESNRP_SHADOW;
113 camera.getCameraNode()->setPosition(rightEye);
114 camera.getCameraNode()->setTarget(focusPoint);
116 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
119 draw_selectionbox(driver, hud, hilightboxes, show_hud);
122 camera.drawWieldedTool(&rightMove);
127 driver->getOverrideMaterial().Material.ColorMask = irr::video::ECP_ALL;
128 driver->getOverrideMaterial().EnableFlags = 0;
129 driver->getOverrideMaterial().EnablePasses = 0;
130 camera.getCameraNode()->setPosition(oldPosition);
131 camera.getCameraNode()->setTarget(oldTarget);
134 void init_texture(video::IVideoDriver* driver, const v2u32& screensize,
135 video::ITexture** texture, const char* name)
137 if (*texture != NULL)
139 driver->removeTexture(*texture);
141 *texture = driver->addRenderTargetTexture(
142 core::dimension2d<u32>(screensize.X, screensize.Y), name,
143 irr::video::ECF_A8R8G8B8);
146 video::ITexture* draw_image(const v2u32& screensize,
147 paralax_sign psign, const irr::core::matrix4& startMatrix,
148 const irr::core::vector3df& focusPoint, bool show_hud,
149 video::IVideoDriver* driver, Camera& camera, scene::ISceneManager* smgr,
150 Hud& hud, std::vector<aabb3f>& hilightboxes,
151 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
152 video::SColor skycolor )
154 static video::ITexture* images[2] = { NULL, NULL };
155 static v2u32 last_screensize = v2u32(0,0);
157 video::ITexture* image = NULL;
159 if (screensize != last_screensize) {
160 init_texture(driver, screensize, &images[1], "mt_drawimage_img1");
161 init_texture(driver, screensize, &images[0], "mt_drawimage_img2");
162 last_screensize = screensize;
170 driver->setRenderTarget(image, true, true,
171 irr::video::SColor(255,
172 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
174 irr::core::vector3df eye_pos;
175 irr::core::matrix4 movement;
176 movement.setTranslation(
177 irr::core::vector3df((int) psign *
178 g_settings->getFloat("3d_paralax_strength"), 0.0f, 0.0f));
179 eye_pos = (startMatrix * movement).getTranslation();
181 //clear the depth buffer
182 driver->clearZBuffer();
183 camera.getCameraNode()->setPosition(eye_pos);
184 camera.getCameraNode()->setTarget(focusPoint);
187 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
191 draw_selectionbox(driver, hud, hilightboxes, show_hud);
194 camera.drawWieldedTool(&movement);
199 /* switch back to real renderer */
200 driver->setRenderTarget(0, true, true,
201 irr::video::SColor(0,
202 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
207 video::ITexture* draw_hud(video::IVideoDriver* driver, const v2u32& screensize,
208 bool show_hud, Hud& hud, Client& client, bool draw_crosshair,
209 video::SColor skycolor, gui::IGUIEnvironment* guienv, Camera& camera )
211 static video::ITexture* image = NULL;
212 init_texture(driver, screensize, &image, "mt_drawimage_hud");
213 driver->setRenderTarget(image, true, true,
214 irr::video::SColor(255,0,0,0));
220 hud.drawHotbar(client.getPlayerItem());
221 hud.drawLuaElements(camera.getOffset());
226 driver->setRenderTarget(0, true, true,
227 irr::video::SColor(0,
228 skycolor.getRed(), skycolor.getGreen(), skycolor.getBlue()));
233 void draw_interlaced_3d_mode(Camera& camera, bool show_hud,
234 Hud& hud, std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
235 scene::ISceneManager* smgr, const v2u32& screensize,
236 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
237 video::SColor skycolor )
239 /* save current info */
240 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
241 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
242 irr::core::matrix4 startMatrix =
243 camera.getCameraNode()->getAbsoluteTransformation();
244 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
245 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
246 + camera.getCameraNode()->getAbsolutePosition();
248 /* create left view */
249 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
250 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
251 draw_wield_tool, client, guienv, skycolor);
254 irr::core::vector3df rightEye;
255 irr::core::matrix4 rightMove;
256 rightMove.setTranslation(
257 irr::core::vector3df(g_settings->getFloat("3d_paralax_strength"),
259 rightEye = (startMatrix * rightMove).getTranslation();
261 //clear the depth buffer
262 driver->clearZBuffer();
263 camera.getCameraNode()->setPosition(rightEye);
264 camera.getCameraNode()->setTarget(focusPoint);
267 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
271 draw_selectionbox(driver, hud, hilightboxes, show_hud);
274 camera.drawWieldedTool(&rightMove);
278 for (unsigned int i = 0; i < screensize.Y; i+=2 ) {
279 #if (IRRLICHT_VERSION_MAJOR >= 1) && (IRRLICHT_VERSION_MINOR >= 8)
280 driver->draw2DImage(left_image, irr::core::position2d<s32>(0, i),
282 driver->draw2DImage(left_image, irr::core::position2d<s32>(0, screensize.Y-i),
284 irr::core::rect<s32>(0, i,screensize.X, i+1), 0,
285 irr::video::SColor(255, 255, 255, 255),
290 camera.getCameraNode()->setPosition(oldPosition);
291 camera.getCameraNode()->setTarget(oldTarget);
294 void draw_sidebyside_3d_mode(Camera& camera, bool show_hud,
295 Hud& hud, std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
296 scene::ISceneManager* smgr, const v2u32& screensize,
297 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
298 video::SColor skycolor )
300 /* save current info */
301 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
302 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
303 irr::core::matrix4 startMatrix =
304 camera.getCameraNode()->getAbsoluteTransformation();
305 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
306 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
307 + camera.getCameraNode()->getAbsolutePosition();
309 /* create left view */
310 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
311 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
312 draw_wield_tool, client, guienv, skycolor);
314 /* create right view */
315 video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix,
316 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
317 draw_wield_tool, client, guienv, skycolor);
319 /* create hud overlay */
320 video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client,
321 false, skycolor, guienv, camera );
322 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
323 //makeColorKeyTexture mirrors texture so we do it twice to get it right again
324 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
326 driver->draw2DImage(left_image,
327 irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y),
328 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
330 driver->draw2DImage(hudtexture,
331 irr::core::rect<s32>(0, 0, screensize.X/2, screensize.Y),
332 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
334 driver->draw2DImage(right_image,
335 irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y),
336 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
338 driver->draw2DImage(hudtexture,
339 irr::core::rect<s32>(screensize.X/2, 0, screensize.X, screensize.Y),
340 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
346 camera.getCameraNode()->setPosition(oldPosition);
347 camera.getCameraNode()->setTarget(oldTarget);
350 void draw_top_bottom_3d_mode(Camera& camera, bool show_hud,
351 Hud& hud, std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
352 scene::ISceneManager* smgr, const v2u32& screensize,
353 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv,
354 video::SColor skycolor )
356 /* save current info */
357 irr::core::vector3df oldPosition = camera.getCameraNode()->getPosition();
358 irr::core::vector3df oldTarget = camera.getCameraNode()->getTarget();
359 irr::core::matrix4 startMatrix =
360 camera.getCameraNode()->getAbsoluteTransformation();
361 irr::core::vector3df focusPoint = (camera.getCameraNode()->getTarget()
362 - camera.getCameraNode()->getAbsolutePosition()).setLength(1)
363 + camera.getCameraNode()->getAbsolutePosition();
365 /* create left view */
366 video::ITexture* left_image = draw_image(screensize, LEFT, startMatrix,
367 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
368 draw_wield_tool, client, guienv, skycolor);
370 /* create right view */
371 video::ITexture* right_image = draw_image(screensize, RIGHT, startMatrix,
372 focusPoint, show_hud, driver, camera, smgr, hud, hilightboxes,
373 draw_wield_tool, client, guienv, skycolor);
375 /* create hud overlay */
376 video::ITexture* hudtexture = draw_hud(driver, screensize, show_hud, hud, client,
377 false, skycolor, guienv, camera );
378 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
379 //makeColorKeyTexture mirrors texture so we do it twice to get it right again
380 driver->makeColorKeyTexture(hudtexture, irr::video::SColor(255, 0, 0, 0));
382 driver->draw2DImage(left_image,
383 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2),
384 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
386 driver->draw2DImage(hudtexture,
387 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y/2),
388 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
390 driver->draw2DImage(right_image,
391 irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y),
392 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, false);
394 driver->draw2DImage(hudtexture,
395 irr::core::rect<s32>(0, screensize.Y/2, screensize.X, screensize.Y),
396 irr::core::rect<s32>(0, 0, screensize.X, screensize.Y), 0, 0, true);
402 camera.getCameraNode()->setPosition(oldPosition);
403 camera.getCameraNode()->setTarget(oldTarget);
406 void draw_plain(Camera& camera, bool show_hud, Hud& hud,
407 std::vector<aabb3f> hilightboxes, video::IVideoDriver* driver,
408 bool draw_wield_tool, Client& client, gui::IGUIEnvironment* guienv)
410 driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
412 draw_selectionbox(driver, hud, hilightboxes, show_hud);
415 camera.drawWieldedTool();
418 void draw_scene(video::IVideoDriver* driver, scene::ISceneManager* smgr,
419 Camera& camera, Client& client, LocalPlayer* player, Hud& hud,
420 gui::IGUIEnvironment* guienv, std::vector<aabb3f> hilightboxes,
421 const v2u32& screensize, video::SColor skycolor, bool show_hud)
423 //TODO check if usefull
426 TimeTaker timer("smgr");
428 bool draw_wield_tool = (show_hud &&
429 (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) &&
430 camera.getCameraMode() < CAMERA_MODE_THIRD );
432 bool draw_crosshair = ((player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) &&
433 (camera.getCameraMode() != CAMERA_MODE_THIRD_FRONT));
435 #ifdef HAVE_TOUCHSCREENGUI
437 draw_crosshair = !g_settings->getBool("touchtarget");
439 catch(SettingNotFoundException) {}
442 std::string draw_mode = g_settings->get("3d_mode");
446 if (draw_mode == "anaglyph")
448 draw_anaglyph_3d_mode(camera, show_hud, hud, hilightboxes, driver,
449 smgr, draw_wield_tool, client, guienv);
450 draw_crosshair = false;
452 else if (draw_mode == "interlaced")
454 draw_interlaced_3d_mode(camera, show_hud, hud, hilightboxes, driver,
455 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
456 draw_crosshair = false;
458 else if (draw_mode == "sidebyside")
460 draw_sidebyside_3d_mode(camera, show_hud, hud, hilightboxes, driver,
461 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
464 else if (draw_mode == "topbottom")
466 draw_top_bottom_3d_mode(camera, show_hud, hud, hilightboxes, driver,
467 smgr, screensize, draw_wield_tool, client, guienv, skycolor);
471 draw_plain(camera, show_hud, hud, hilightboxes, driver,
472 draw_wield_tool, client, guienv);
479 client.getEnv().getClientMap().renderPostFx(camera.getCameraMode());
482 //TODO how to make those 3d too
487 hud.drawHotbar(client.getPlayerItem());
488 hud.drawLuaElements(camera.getOffset());
493 scenetime = timer.stop(true);
499 Draws a screen with a single text on it.
500 Text will be removed when the screen is drawn the next time.
501 Additionally, a progressbar can be drawn when percent is set between 0 and 100.
503 /*gui::IGUIStaticText **/
504 void draw_load_screen(const std::wstring &text, IrrlichtDevice* device,
505 gui::IGUIEnvironment* guienv, gui::IGUIFont* font, float dtime,
506 int percent, bool clouds )
508 video::IVideoDriver* driver = device->getVideoDriver();
509 v2u32 screensize = driver->getScreenSize();
510 const wchar_t *loadingtext = text.c_str();
511 core::vector2d<u32> textsize_u = font->getDimension(loadingtext);
512 core::vector2d<s32> textsize(textsize_u.X,textsize_u.Y);
513 core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
514 core::rect<s32> textrect(center - textsize/2, center + textsize/2);
516 gui::IGUIStaticText *guitext = guienv->addStaticText(
517 loadingtext, textrect, false, false);
518 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
520 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
521 if (cloud_menu_background)
523 g_menuclouds->step(dtime*3);
524 g_menuclouds->render();
525 driver->beginScene(true, true, video::SColor(255,140,186,250));
526 g_menucloudsmgr->drawAll();
529 driver->beginScene(true, true, video::SColor(255,0,0,0));
531 if (percent >= 0 && percent <= 100) // draw progress bar
533 core::vector2d<s32> barsize(256,32);
534 core::rect<s32> barrect(center-barsize/2, center+barsize/2);
535 driver->draw2DRectangle(video::SColor(255,255,255,255),barrect, NULL); // border
536 driver->draw2DRectangle(video::SColor(255,64,64,64), core::rect<s32> (
537 barrect.UpperLeftCorner+1,
538 barrect.LowerRightCorner-1), NULL); // black inside the bar
539 driver->draw2DRectangle(video::SColor(255,128,128,128), core::rect<s32> (
540 barrect.UpperLeftCorner+1,
542 barrect.LowerRightCorner.X-(barsize.X-1)+percent*(barsize.X-2)/100,
543 barrect.LowerRightCorner.Y-1)), NULL); // the actual progress