I'm doing this for a project, so please help:!:
I need to know how to convert a simple matrix rotating camera class into an irrlicht like lookat camera.
i'll could probably work it out on my own, but i don't want to reinvent that again
(general) cameras
Re: (general) cameras
maybe this program can help you!
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
/*
If the key 'W' or 'S' was left up, we get the position of the scene node,
and modify the Y coordinate a little bit. So if you press 'W', the node
moves up, and if you press 'S' it moves down.
*/
if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
case KEY_KEY_S:
{
core::vector3df v = node->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
node->setPosition(v);
}
return true;
}
}
return false;
}
};
/*
The event receiver for moving a scene node is ready. So lets just create
an Irrlicht Device and the scene node we want to move. We also create some
other additional scene nodes, to show that there are also some different
possibilities to move and animate scene nodes.
*/
int main()
{
MyEventReceiver receiver;
device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480),
16, false, false, false, &receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
/*
Create the node for moving it with the 'W' and 'S' key. We create a
sphere node, which is a built in geometry primitive. We place the node
at (0,0,30) and assign a texture to it to let it look a little bit more
interesting. Because we have no dynamic lights in this scene we disable
lighting for each model (otherwise the models would be black).
*/
node = smgr->addSphereSceneNode();
node->setPosition(core::vector3df(0,0,30));
node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
/*
Now we create another node, moving using a scene node animator. Scene node
animators modify scene nodes and can be attached to any scene node like
mesh scene nodes, billboards, lights and even camera scene nodes. Scene node
animators are not only able to modify the position of a scene node, they can
also animate the textures of an object for example.
We create a cube scene node and attach a 'fly circle' scene node to it, letting
this node fly around our sphere scene node.
*/
scene::ISceneNode* n = smgr->addCubeSceneNode();
if (n)
{
n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
n->setMaterialFlag(video::EMF_LIGHTING, false);
scene::ISceneNodeAnimator* anim =
smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
n->addAnimator(anim);
anim->drop();
}
scene::ICameraSceneNode* camera=smgr->addCameraSceneNodeFPS();//0, 100.0f, 200.0f);
if (camera)
{
scene::ISceneNodeAnimator* cameraanim =
smgr->createFlyStraightAnimator(core::vector3df(100,0,60),
core::vector3df(-100,0,60), 2500, true);
camera->addAnimator(cameraanim);
cameraanim->drop();
camera->setRotation(core::vector3df(0,180.0f,0));
}
device->getCursorControl()->setVisible(false);
/*
Add a colorful irrlicht logo
*/
device->getGUIEnvironment()->addImage(
driver->getTexture("../../media/irrlichtlogoalpha2.tga"),
core::position2d<s32>(10,10));
/*
We have done everything, so lets draw it. We also write the current
frames per second and the name of the driver to the caption of the
window.
*/
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine [%ls] fps: %d",
driver->getName(), fps);
device->setWindowCaption(tmp);
lastFPS = fps;
}
}
/*
In the end, delete the Irrlicht device.
*/
device->drop();
return 0;
}
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
/*
If the key 'W' or 'S' was left up, we get the position of the scene node,
and modify the Y coordinate a little bit. So if you press 'W', the node
moves up, and if you press 'S' it moves down.
*/
if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
case KEY_KEY_S:
{
core::vector3df v = node->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
node->setPosition(v);
}
return true;
}
}
return false;
}
};
/*
The event receiver for moving a scene node is ready. So lets just create
an Irrlicht Device and the scene node we want to move. We also create some
other additional scene nodes, to show that there are also some different
possibilities to move and animate scene nodes.
*/
int main()
{
MyEventReceiver receiver;
device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480),
16, false, false, false, &receiver);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
/*
Create the node for moving it with the 'W' and 'S' key. We create a
sphere node, which is a built in geometry primitive. We place the node
at (0,0,30) and assign a texture to it to let it look a little bit more
interesting. Because we have no dynamic lights in this scene we disable
lighting for each model (otherwise the models would be black).
*/
node = smgr->addSphereSceneNode();
node->setPosition(core::vector3df(0,0,30));
node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
/*
Now we create another node, moving using a scene node animator. Scene node
animators modify scene nodes and can be attached to any scene node like
mesh scene nodes, billboards, lights and even camera scene nodes. Scene node
animators are not only able to modify the position of a scene node, they can
also animate the textures of an object for example.
We create a cube scene node and attach a 'fly circle' scene node to it, letting
this node fly around our sphere scene node.
*/
scene::ISceneNode* n = smgr->addCubeSceneNode();
if (n)
{
n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
n->setMaterialFlag(video::EMF_LIGHTING, false);
scene::ISceneNodeAnimator* anim =
smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
n->addAnimator(anim);
anim->drop();
}
scene::ICameraSceneNode* camera=smgr->addCameraSceneNodeFPS();//0, 100.0f, 200.0f);
if (camera)
{
scene::ISceneNodeAnimator* cameraanim =
smgr->createFlyStraightAnimator(core::vector3df(100,0,60),
core::vector3df(-100,0,60), 2500, true);
camera->addAnimator(cameraanim);
cameraanim->drop();
camera->setRotation(core::vector3df(0,180.0f,0));
}
device->getCursorControl()->setVisible(false);
/*
Add a colorful irrlicht logo
*/
device->getGUIEnvironment()->addImage(
driver->getTexture("../../media/irrlichtlogoalpha2.tga"),
core::position2d<s32>(10,10));
/*
We have done everything, so lets draw it. We also write the current
frames per second and the name of the driver to the caption of the
window.
*/
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine [%ls] fps: %d",
driver->getName(), fps);
device->setWindowCaption(tmp);
lastFPS = fps;
}
}
/*
In the end, delete the Irrlicht device.
*/
device->drop();
return 0;
}
I am a students of china !weicame to china!
this is code I am sometimes using. You can extract what you need may be:
Code: Select all
#ifndef MCAMERA_H
#define MCAMERA_H
#include <irrlicht.h>
using namespace irr;
class MCamera
{
scene::ICameraSceneNode* camera;
f32 rotationX;
f32 rotationY;
core::vector3df direction;
public:
MCamera(scene::ISceneManager* smgr)
{
camera = smgr->addCameraSceneNode();
rotationX = 0.0f;
rotationY = 0.0f;
direction = core::vector3df(0,0,1);
}
~MCamera(){}
void turnRight(f32 i)
{
rotationY += i;
if(rotationY>=360)rotationY-=360;
if(rotationY<0)rotationY+=360;
direction = core::vector3df(0,0,1);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
matrix.rotateVect(direction);
camera->setTarget(camera->getPosition() + direction);
}
void turnLeft(f32 i)
{
rotationY -= i;
if(rotationY>=360)rotationY-=360;
if(rotationY<0)rotationY+=360;
direction = core::vector3df(0,0,1);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
matrix.rotateVect(direction);
camera->setTarget(camera->getPosition() + direction);
}
void turnUp(f32 i)
{
rotationX += i;
if(rotationX>=360)rotationX-=360;
if(rotationX<0)rotationX+=360;
direction = core::vector3df(0,0,1);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
matrix.rotateVect(direction);
camera->setTarget(camera->getPosition() + direction);
}
void turnDown(f32 i)
{
rotationX -= i;
if(rotationX>=360)rotationX-=360;
if(rotationX<0)rotationX+=360;
direction = core::vector3df(0,0,1);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
matrix.rotateVect(direction);
camera->setTarget(camera->getPosition() + direction);
}
void moveForward(f32 i)
{
core::vector3df step = core::vector3df(0,0,i);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
matrix.rotateVect(step);
camera->setPosition(camera->getPosition() + step);
camera->setTarget(camera->getPosition() + direction);
}
void moveBack(f32 i)
{
core::vector3df step = core::vector3df(0,0,-i);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
matrix.rotateVect(step);
camera->setPosition(camera->getPosition() + step);
camera->setTarget(camera->getPosition() + direction);
}
void moveRight(f32 i)
{
core::vector3df step = core::vector3df(i,0,0);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
matrix.rotateVect(step);
camera->setPosition(camera->getPosition() + step);
camera->setTarget(camera->getPosition() + direction);
}
void moveLeft(f32 i)
{
core::vector3df step = core::vector3df(-i,0,0);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
matrix.rotateVect(step);
camera->setPosition(camera->getPosition() + step);
camera->setTarget(camera->getPosition() + direction);
}
void setHeight(f32 i)
{
camera->setPosition(core::vector3df(camera->getPosition().X, i, camera->getPosition().Z));
camera->setTarget(camera->getPosition() + direction);
}
void setPosition(core::vector3df pos)
{
camera->setPosition(pos);
}
core::vector3df getPosition()
{
return camera->getPosition();
}
core::vector3df getDirection()
{
return direction;
}
core::vector3df getTarget()
{
return camera->getTarget();
}
f32 getHeading()
{
return rotationY;
}
f32 getPitch()
{
return rotationX;
}
f32 getFarValue()
{
return camera->getFarValue()
}
f32 setFarValue(f32 f)
{
camera->setFarValue(f);
}
f32 getNearValue()
{
return camera->getNearValue()
}
f32 setNearValue(f32 n)
{
camera->setNearValue(n);
}
f32 getFOV()
{
return camera->getFOV()
}
f32 setFOV(f32 v)
{
camera->setFOV(v);
}
f32 getAspectRatio()
{
return camera->getAspectRatio()
}
f32 setAspectRatio(f32 a)
{
camera->setAspectRatio(a);
}
};
#endif