Code: Select all
// add a viewport that displays the scene
IGUIViewport* viewport = env->addViewport(core::rect<s32>(10, 30, 290, 290));
if (viewport)
{
//
// create a scene for the viewport
//
ISceneManager* vsmgr = smgr->createNewSceneManager();
viewport->setSceneManager(vsmgr);
vsmgr->drop();
// set the viewport background color
//viewport->setOverrideColor(video::SColor(255, 160, 180, 255));
// add stuff to viewports scene manager
// add a camera for the viewport
vsmgr->addCameraSceneNodeMaya();
}
Code: Select all
#ifndef __I_GUI_VIEWPORT_H_INCLUDED__
#define __I_GUI_VIEWPORT_H_INCLUDED__
#include "IGUIElement.h"
#include "SColor.h"
namespace irr
{
namespace scene
{
class ISceneManager;
}
namespace gui
{
//! 3d scene GUI element.
class IGUIViewport : public IGUIElement
{
public:
//! constructor
IGUIViewport(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
: IGUIElement(EGUIET_MESH_VIEWER, environment, parent, id, rectangle)
{
}
//! sets the scene to be shown
virtual void setSceneManager(scene::ISceneManager* scene) = 0;
//! gets the scene to be shown
virtual scene::ISceneManager* getSceneManager() const = 0;
//! sets the viewport fill color
virtual void setOverrideColor(video::SColor color) = 0;
//! enable or disable the viewport fill color
virtual void enableOverrideColor(bool override) = 0;
};
} // end namespace gui
} // end namespace irr
#endif
Code: Select all
#ifndef __C_GUI_VIEWPORT_H_INCLUDED__
#define __C_GUI_VIEWPORT_H_INCLUDED__
#include "IGUIViewport.h"
namespace irr
{
namespace gui
{
class CGUIViewport : public IGUIViewport
{
public:
//! constructor
CGUIViewport(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle);
//! destructor
virtual ~CGUIViewport();
//! called if an event happened.
virtual bool OnEvent(SEvent event);
//! draws the element and its children
virtual void draw();
//! sets the scene to be shown
virtual void setSceneManager(scene::ISceneManager* scene);
//! gets the scene to be shown
virtual scene::ISceneManager* getSceneManager() const;
//! sets the viewport fill color
virtual void setOverrideColor(video::SColor color);
//! enable or disable the viewport fill color
virtual void enableOverrideColor(bool override);
private:
scene::ISceneManager* SceneManager;
bool OverrideColorEnabled;
video::SColor OverrideColor;
};
} // end namespace gui
} // end namespace irr
#endif
Code: Select all
#include "CGUIViewport.h"
#include "IGUIEnvironment.h"
#include "ISceneManager.h"
#include "ICameraSceneNode.h"
#include "IVideoDriver.h"
#include "IGUISkin.h"
namespace irr
{
namespace gui
{
//! constructor
CGUIViewport::CGUIViewport(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
: IGUIViewport(environment, parent, id, rectangle)
, SceneManager(0)
, OverrideColorEnabled(false)
, OverrideColor(255, 0, 0, 0)
{
#ifdef _DEBUG
setDebugName("CGUIViewport");
#endif
}
//! destructor
CGUIViewport::~CGUIViewport()
{
if (SceneManager)
SceneManager->drop();
}
//! called if an event happened.
bool CGUIViewport::OnEvent(SEvent event)
{
bool absorbed = false;
if (SceneManager)
absorbed = SceneManager->postEventFromUser(event);
if (!absorbed && Parent)
absorbed = Parent->OnEvent(event);
return absorbed;
}
//! draws the element and its children
void CGUIViewport::draw()
{
if (!IsVisible)
return;
video::IVideoDriver* driver = Environment->getVideoDriver();
// draw the background and frame
core::rect<s32> frameRect(AbsoluteRect);
if (OverrideColorEnabled)
driver->draw2DRectangle(OverrideColor, frameRect, &AbsoluteClippingRect);
IGUISkin* skin = Environment->getSkin();
frameRect.LowerRightCorner.Y = frameRect.UpperLeftCorner.Y + 1;
driver->draw2DRectangle(skin->getColor(EGDC_3D_SHADOW), frameRect, &AbsoluteClippingRect);
frameRect.LowerRightCorner.Y = AbsoluteRect.LowerRightCorner.Y;
frameRect.LowerRightCorner.X = frameRect.UpperLeftCorner.X + 1;
driver->draw2DRectangle(skin->getColor(EGDC_3D_SHADOW), frameRect, &AbsoluteClippingRect);
frameRect = AbsoluteRect;
frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X - 1;
driver->draw2DRectangle(skin->getColor(EGDC_3D_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
frameRect = AbsoluteRect;
frameRect.UpperLeftCorner.Y = AbsoluteRect.LowerRightCorner.Y - 1;
frameRect.LowerRightCorner.Y = AbsoluteRect.LowerRightCorner.Y;
driver->draw2DRectangle(skin->getColor(EGDC_3D_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
// draw the scene
if (SceneManager)
{
core::rect<s32> viewPort = AbsoluteRect;
viewPort.LowerRightCorner.X -= 1;
viewPort.LowerRightCorner.Y -= 1;
viewPort.UpperLeftCorner.X += 1;
viewPort.UpperLeftCorner.Y += 1;
viewPort.clipAgainst(AbsoluteClippingRect);
core::rect<s32> screenRect;
screenRect.UpperLeftCorner.X = 0;
screenRect.UpperLeftCorner.Y = 0;
screenRect.LowerRightCorner.X = driver->getScreenSize().Width;
screenRect.LowerRightCorner.Y = driver->getScreenSize().Height;
viewPort.clipAgainst(screenRect);
if ( !(viewPort.getWidth() < 1 || viewPort.getHeight() < 1))
{
scene::ICameraSceneNode* cam = SceneManager->getActiveCamera();
if (cam)
{
core::rect<s32> oldViewPort = driver->getViewPort();
driver->setViewPort(viewPort);
driver->clearZBuffer();
f32 oldAspectRatio = cam->getAspectRatio();
cam->setAspectRatio(1.f * viewPort.getWidth() / viewPort.getHeight());
f32 oldFieldOfView = cam->getFOV();
cam->setFOV(oldFieldOfView * viewPort.getHeight() / (AbsoluteRect.getHeight() - 2));
SceneManager->drawAll();
cam->setFOV(oldFieldOfView);
cam->setAspectRatio(oldAspectRatio);
driver->setViewPort(oldViewPort);
}
}
}
IGUIElement::draw();
}
//! sets the scene to be shown
void CGUIViewport::setSceneManager(scene::ISceneManager* scene)
{
if (SceneManager)
SceneManager->drop();
SceneManager = scene;
if (SceneManager)
SceneManager->grab();
}
//! gets the scene to be shown
scene::ISceneManager* CGUIViewport::getSceneManager() const
{
return SceneManager;
}
//! sets the viewport fill color
void CGUIViewport::setOverrideColor(video::SColor color)
{
OverrideColor = color;
enableOverrideColor(true);
}
//! enable or disable the viewport fill color
void CGUIViewport::enableOverrideColor(bool override)
{
OverrideColorEnabled = override;
}
} // end namespace gui
} // end namespace irr