cgizmoscenenode.cpp
Code: Select all
#include "cgizmoscenenode.h"
CGizmoSceneNode::CGizmoSceneNode(IrrlichtDevice *device, scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id)
: scene::ISceneNode(parent, mgr, id)
{
#ifdef _DEBUG
setDebugName("CGizmoSceneNode");
#endif
setAutomaticCulling(scene::EAC_OFF);
Device = device;
CollMan = SceneManager->getSceneCollisionManager();
IAnimatedMesh* arrowMeshRed = SceneManager->addArrowMesh( "x_axisArrow",video::SColor(255, 255, 0, 0),video::SColor(255, 255, 0, 0));
scene::ISceneNode* nodeX = SceneManager->addMeshSceneNode(arrowMeshRed, this, x_axis);
if(nodeX)
{
nodeX->setMaterialFlag(video::EMF_LIGHTING, false);
nodeX->setRotation(vector3df(0,0,-90));
nodeX->setScale(vector3df(10,30,10));
}
IAnimatedMesh* arrowMeshGreen = SceneManager->addArrowMesh( "y_axisArrow",video::SColor(255, 0, 255, 0),video::SColor(255, 0, 255, 0));
scene::ISceneNode* nodeY = SceneManager->addMeshSceneNode(arrowMeshGreen, this, y_axis);
if(nodeY)
{
nodeY->setMaterialFlag(video::EMF_LIGHTING, false);
nodeY->setScale(vector3df(10,30,10));
}
IAnimatedMesh* arrowMeshBlue = SceneManager->addArrowMesh( "z_axisArrow",video::SColor(255, 0, 0, 255),video::SColor(255, 0, 0, 255));
scene::ISceneNode* nodeZ = SceneManager->addMeshSceneNode(arrowMeshBlue, this, z_axis);
if(nodeZ)
{
nodeZ->setMaterialFlag(video::EMF_LIGHTING, false);
nodeZ->setRotation(vector3df(90,0,0));
nodeZ->setScale(vector3df(10,30,10));
}
setDebugDataVisible(EDS_BBOX);
}
CGizmoSceneNode::~CGizmoSceneNode(void)
{
}
void CGizmoSceneNode::OnRegisterSceneNode()
{
if (IsVisible)
{
SceneManager->registerNodeForRendering(this);
}
ISceneNode::OnRegisterSceneNode();
}
void CGizmoSceneNode::render()
{
currentAxix = CollMan->getSceneNodeFromScreenCoordinatesBB(Device->getCursorControl()->getPosition(), x_axis | y_axis | y_axis, true);
}
const core::aabbox3d<f32>& CGizmoSceneNode::getBoundingBox() const
{
return Box;
}
ISceneNode* CGizmoSceneNode::getCurrentAxis()
{
return currentAxix;
}
Code: Select all
#include "MyEventReceiver.h"
MyEventReceiver::MyEventReceiver(IrrlichtDevice *device)
{
Device = device;
CollMan = Device->getSceneManager()->getSceneCollisionManager();
LMouseButtonDown = 0;
SelectedNode = 0;
gizmo = new CGizmoSceneNode(Device, Device->getSceneManager()->getRootSceneNode(), Device->getSceneManager());
}
MyEventReceiver::~MyEventReceiver(void)
{
}
bool MyEventReceiver::OnEvent(const SEvent& event)
{
switch(event.EventType)
{
case EET_MOUSE_INPUT_EVENT :
{
switch (event.MouseInput.Event)
{
case EMIE_LMOUSE_PRESSED_DOWN :
{
LMouseButtonDown = true;
SelectedNode = CollMan->getSceneNodeFromScreenCoordinatesBB(Device->getCursorControl()->getPosition(), 0,true);
if(SelectedNode)
{
initialCursorPosition = Device->getCursorControl()->getPosition();
initialObjectPosition = CollMan->getScreenCoordinatesFrom3DPosition(SelectedNode->getAbsolutePosition(), Device->getSceneManager()->getActiveCamera());
}
}
break;
case EMIE_LMOUSE_LEFT_UP :
{
LMouseButtonDown = false;
return false;
}
break;
case EMIE_MOUSE_MOVED :
{
if (!LMouseButtonDown)
return false;
if (SelectedNode)
{
vector3df p = SelectedNode->getPosition();
plane3df const planeXZ(SelectedNode->getAbsolutePosition(), vector3df(0.f, 1.f, 0.f));
position2di currentCursorPosition(Device->getCursorControl()->getPosition());
position2di effectiveObjectPosition = initialObjectPosition + currentCursorPosition - initialCursorPosition;
line3df ray(CollMan->getRayFromScreenCoordinates(effectiveObjectPosition, Device->getSceneManager()->getActiveCamera()));
vector3df intersectWithPlane;
if(planeXZ.getIntersectionWithLine(ray.start, ray.getVector(), intersectWithPlane))
{
SelectedNode->setPosition(intersectWithPlane);
}
}
}
break;
default:
break;
}
}
break;
default:
break;
}
return 0;
}
Code: Select all
#include <irrlicht.h>
#include "MyEventReceiver.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
int main()
{
IrrlichtDevice *device = createDevice(video::EDT_OPENGL,core::dimension2d<u32>(800, 600), 32, false, false, false, 0);
if (!device)
return 1;
MyEventReceiver receiver(device);
device->setEventReceiver(&receiver);
IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
IGUIEnvironment *guienv = device->getGUIEnvironment();
device->setWindowCaption(L"Gizmo Test");
ICameraSceneNode *camera = smgr->addCameraSceneNode(0, vector3df(50,30,-40), vector3df(0,5,0));
ISceneNode* node = smgr->addCubeSceneNode(20, 0);
if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMaterialTexture( 0, driver->getTexture("wall.jpg") );
node->setMaterialFlag(EMF_WIREFRAME, true);
}
while(device->run())
{
driver->beginScene(true, true, SColor(255,100,101,140));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
I have a few questions.
1. How to attach the cube to the gizmo? (setParent method not working correctly)
2. How to restrict the movement of the arrows for only one axis?
3. Can this be done in scene node class? Not in EventReceiver class.
4. How to make so that if one moves arrow, then would move all arrows? All arrows it is childrens of empty scene node.
Thanks
Best regard
Digan