I see any more article's and topic's: tombraider camera, 3rd camera, IrrCamera3D and more. I like gothic style camera.
This all find code and my code (not work):
main.cpp
Code: Select all
#include "IrrCamera3D.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main(int argc, char** argv)
{
IrrCamera3D cam_control;
MyEventReceiver receiver;
IrrlichtDevice *device =createDevice( video::EDT_OPENGL, dimension2d<u32>(1024, 768), 32, true, false, false, 0);
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
rect<int>(10,10,200,22), true);
device->getCursorControl()->setVisible(false);
// add terrain scene node
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
"media/terrain-heightmap.bmp",
0, // parent node
-1, // node id
core::vector3df(0.f, 0.f, 0.f), // position
core::vector3df(0.f, 0.f, 0.f), // rotation
core::vector3df(40.f, 4.4f, 40.f), // scale
video::SColor ( 255, 255, 255, 255 ), // vertexColor
5, // maxLOD
scene::ETPS_17, // patchSize
4 // smoothFactor
);
terrain->setMaterialFlag(video::EMF_LIGHTING, false);
terrain->setMaterialTexture(0,
driver->getTexture("media/terrain-texture.jpg"));
terrain->setMaterialTexture(1,
driver->getTexture("media/detailmap3.jpg"));
terrain->setMaterialType(video::EMT_DETAIL_MAP);
terrain->scaleTexture(1.0f, 20.0f);
IAnimatedMesh* mesh = smgr->getMesh("media/sydney.md2");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
vector3df(0,5,0));
cam_control.Create(smgr);
u32 then = device->getTimer()->getTime();
const f32 MOVEMENT_SPEED = 5.f;
while(device->run())
{
const u32 now = device->getTimer()->getTime();
const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
then = now;
if(receiver.IsKeyDown(irr::KEY_KEY_W))
cam_control.moveForward(MOVEMENT_SPEED * frameDeltaTime);
else if(receiver.IsKeyDown(irr::KEY_KEY_S))
cam_control.moveBack(MOVEMENT_SPEED * frameDeltaTime);
if(receiver.IsKeyDown(irr::KEY_KEY_A))
cam_control.moveLeft(MOVEMENT_SPEED * frameDeltaTime);
else if(receiver.IsKeyDown(irr::KEY_KEY_D))
cam_control.moveRight(MOVEMENT_SPEED * frameDeltaTime);
driver->beginScene(true, true, SColor(0,200,200,200));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
Code: Select all
#pragma once
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class IrrCamera3D
{
public:
f32 rotationX;
f32 rotationY;
core::vector3df direction;
scene::ICameraSceneNode* camera;
IrrCamera3D(void);
virtual ~IrrCamera3D(void);
virtual void Initialize();
virtual bool Create(ISceneManager* level);
virtual bool Cleanup();
void turnRight(f32 i);
void turnLeft(f32 i);
void turnUp(f32 i);
void turnDown(f32 i);
void moveForward(f32 i);
void moveBack(f32 i);
void moveRight(f32 i);
void moveLeft(f32 i);
void setHeight(f32 i);
void setPosition(core::vector3df pos);
core::vector3df getPosition();
core::vector3df getDirection();
core::vector3df getTarget();
f32 getHeading();
f32 getPitch();
f32 getFarValue();
void setFarValue(f32 f);
f32 getNearValue();
void setNearValue(f32 n);
f32 getFOV();
void setFOV(f32 v);
f32 getAspectRatio();
void setAspectRatio(f32 a);
};
Code: Select all
#include "IrrCamera3D.h"
IrrCamera3D::IrrCamera3D(void)
{
Initialize();
}
IrrCamera3D::~IrrCamera3D(void)
{
Cleanup();
}
void IrrCamera3D::Initialize()
{
}
bool IrrCamera3D::Create(scene::ISceneManager* level)
{
camera = level->addCameraSceneNode();
rotationX = 0.0f;
rotationY = 0.0f;
direction = core::vector3df(0,0,1);
return true;
}
bool IrrCamera3D::Cleanup()
{
// remove the camera
return false;
}
void IrrCamera3D::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);
camera->updateAbsolutePosition();
}
void IrrCamera3D::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);
camera->updateAbsolutePosition();
}
void IrrCamera3D::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);
camera->updateAbsolutePosition();
}
void IrrCamera3D::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);
camera->updateAbsolutePosition();
}
void IrrCamera3D::moveForward(f32 i)
{
core::vector3df step = core::vector3df(0,0,i);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
matrix.rotateVect(step);
camera->setPosition(camera->getPosition() + step);
camera->setTarget(camera->getPosition() + direction);
camera->updateAbsolutePosition();
}
void IrrCamera3D::moveBack(f32 i)
{
core::vector3df step = core::vector3df(0,0,-i);
core::matrix4 matrix;
matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
matrix.rotateVect(step);
camera->setPosition(camera->getPosition() + step);
camera->setTarget(camera->getPosition() + direction);
camera->updateAbsolutePosition();
}
void IrrCamera3D::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);
camera->updateAbsolutePosition();
}
void IrrCamera3D::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);
camera->updateAbsolutePosition();
}
void IrrCamera3D::setHeight(f32 i)
{
camera->setPosition(core::vector3df(camera->getPosition().X, i, camera->getPosition().Z));
camera->setTarget(camera->getPosition() + direction);
camera->updateAbsolutePosition();
}
void IrrCamera3D::setPosition(core::vector3df pos)
{
camera->setPosition(pos);
camera->updateAbsolutePosition();
}
core::vector3df IrrCamera3D::getPosition()
{
return camera->getPosition();
}
core::vector3df IrrCamera3D::getDirection()
{
return direction;
}
core::vector3df IrrCamera3D::getTarget()
{
return camera->getTarget();
}
f32 IrrCamera3D::getHeading()
{
return rotationY;
}
f32 IrrCamera3D::getPitch()
{
return rotationX;
}
f32 IrrCamera3D::getFarValue()
{
return camera->getFarValue();
}
void IrrCamera3D::setFarValue(f32 f)
{
//camera->setFarValue(f);
}
f32 IrrCamera3D::getNearValue()
{
return camera->getNearValue();
}
void IrrCamera3D::setNearValue(f32 n)
{
camera->setNearValue(n);
}
f32 IrrCamera3D::getFOV()
{
return camera->getFOV();
}
void IrrCamera3D::setFOV(f32 v)
{
camera->setFOV(v);
}
f32 IrrCamera3D::getAspectRatio()
{
return camera->getAspectRatio();
}
void IrrCamera3D::setAspectRatio(f32 a)
{
camera->setAspectRatio(a);
}
Code: Select all
/*Particular Zlib License /LIBRARY(C).
3DP.h
3DP.cpp
Licenзa ZLIB/LIBPNG
Disponнvel, no original, em: http://www.opensource.org/licenses/zlib-license.php
Versгo 1.01
Licenзa ZLIB/LIBPNG
Direitos autorais Copyright (C) 2008 Lucian. 2009 Lucian,cуdigo original encontrado em um forum.
Este programa de computador й fornecido "como estб",
sem garantias de qualquer espйcie, expressas ou tбcitas.
Em nenhuma hipуtese os autores serгo responsбveis por
quaisquer danos decorrentes do uso deste programa de computador.
Outorga-se permissгo para qualquer um usar este programa de
computador para quaisquer fins, incluindo utilizaзхes comerciais,
e para o modificar e redistribuir livremente, sujeita аs seguintes restriзхes:
1. A origem deste programa de computador nгo pode ser apresentada
de forma errфnea; vocк nгo pode reivindicar ter escrito o programa
de computador original. Se vocк usar esse programa de computador em
um produto, um reconhecimento de crйditos na documentaзгo do produto
seria apreciada, mas nгo й exigida.
2. Versхes alteradas do cуdigo-fonte devem estar marcadas como tais,
e nгo podem ser apresentadas forma errфnea, como sendo o programa de
computador original.
3. Este aviso nгo pode ser alterado ou removido de qualquer distribuiзгo de cуdigo fonte.*/
#ifndef _3DP_h_
#define _3DP_h_
#include <irrlicht.h>
#define EPSILON 0.001f
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class CScene3PCamera : public irr::scene::ISceneNodeAnimator,public irr::IEventReceiver
{
public:
//! Construtor
//! player: Objeto a Seguir
//! Distanciaancia: The initial Distanciaance from the player
//! initAngleY: The initial horizontal rotatation
//! initAngleZ: The initial vertical rotation
//! targetOffset: The offset from the object's center at which the camera looks
//! minDistancia/maxDistancia: Distanciaance bounding
//! minAngle/maxAngle: Rotation bounding. -89 ==> looking up at player, 89 ==> looking down
//! boxSize: The size of the box in which mouse movements do not result in camera movements
CScene3PCamera(
irr::scene::ISceneManager *manager,
irr::gui::ICursorControl *cursor,
irr::scene::ISceneNode *player,
irr::f32 Distanciaancia = 50.0f,
irr::f32 initAngleY = 180.0f,
irr::f32 initAngleZ = 10.0f,
bool RotatePlayer = false,
irr::core::vector3df targetOffset = irr::core::vector3df(0,0,0),
irr::f32 minDistancia = 20.0f,
irr::f32 maxDistancia = 200.0f,
irr::f32 minAngle = -45.0f,
irr::f32 maxAngle = 89.0f,
irr::f32 boxSize = 0.0f,
irr::f32 rotationSpeed = 60.0f);
//! Destructor
virtual ~CScene3PCamera(void);
//! animates the scene node
virtual void animateNode(ISceneNode* node, u32 timeMs);
//! Process an input event
virtual ISceneNodeAnimator* createClone (ISceneNode *node, ISceneManager *newManager=0)
{
return new CScene3PCamera(Manager,Cursor,Player,Distancia,AngleY,
AngleZ,false,TargetOffset,MinDistancia,MaxDistancia,MinAngle,
MaxAngle,BoxSize,RotationSpeed);
}
// virtual bool OnEvent(irr::SEvent event);
virtual bool OnEvent(const SEvent& event);
//! Get/set active status
int isActive();
void setActive(bool status);
virtual ESCENE_NODE_ANIMATOR_TYPE getType(void) const
{
return ESNAT_UNKNOWN;
}
virtual bool isEventReceiverEnabled(void) const
{
return false;
}
//! Get/set box size
irr::f32 getBoxSize();
void setBoxSize(irr::f32 newSize);
//! Map zoom in/zoom out buttons
//! Access the camera's current orientation
irr::f32 getOrientation();
bool XRotatePlayer;
// current states
irr::f32 Distancia;
irr::f32 AngleY;
irr::f32 AngleZ;
private:
int Active;//bool Active;
/*
// current states
irr::f32 Distancia;
irr::f32 AngleY;
irr::f32 AngleZ;
*/
// boundaries
irr::core::vector3df TargetOffset;
irr::f32 MinDistancia;
irr::f32 MaxDistancia;
irr::f32 MinAngle;
irr::f32 MaxAngle;
irr::f32 BoxSize;
// Motion
irr::f32 RotationSpeed;
//! Node to follow
irr::scene::ISceneNode *Player;
irr::scene::ISceneManager *Manager;
irr::gui::ICursorControl *Cursor;
//! Puts the cursor back in the box
void updateCursorPosition();
};
#endif
Code: Select all
#include "3DP.h"
CScene3PCamera::CScene3PCamera(
irr::scene::ISceneManager *manager,
irr::gui::ICursorControl *cursor,
irr::scene::ISceneNode *player,
irr::f32 Distanciaancia,irr::f32 initAngleY,
irr::f32 initAngleZ,
bool RotatePlayer,
irr::core::vector3df targetOffset,
irr::f32 minDistancia,
irr::f32 maxDistancia,
irr::f32 minAngle,
irr::f32 maxAngle,
irr::f32 boxSize,
irr::f32 rotationSpeed)
: Active(true), Manager(manager), Cursor(cursor), Player(player),
Distancia(Distanciaancia),AngleY(initAngleY),AngleZ(initAngleZ),XRotatePlayer(RotatePlayer),
TargetOffset(targetOffset),
MinDistancia(minDistancia), MaxDistancia(maxDistancia), MinAngle(-maxAngle), MaxAngle(-minAngle),
BoxSize(boxSize), RotationSpeed(rotationSpeed)
{
Cursor->setPosition(0.5f,0.5f);
// Ensure Distanciaance is bounded correctly
if(Distancia < MinDistancia) Distancia = MinDistancia;
else if(Distancia > MaxDistancia) Distancia = MaxDistancia;
// Bound MinAngle/MaxAngle to avoid problematic areas
if(MinAngle < -89.0f) MinAngle = -89.0f;
if(MinAngle > 89.0f) MinAngle = 89.0f;
if(MaxAngle < -89.0f) MaxAngle = -89.0f;
if(MaxAngle > 89.0f) MaxAngle = 89.0f;
if(minAngle > maxAngle) MaxAngle = MinAngle+1.0f;
// Ensure Vertical Rotation Angle is bounded correctly
if(AngleZ < MinAngle) AngleZ = MinAngle;
else if(AngleZ > MaxAngle) AngleZ = MaxAngle;
}
//! Destructor
CScene3PCamera::~CScene3PCamera(void)
{
}
//! Puts the cursor back in the box
void CScene3PCamera::updateCursorPosition()
{
irr::core::position2d<irr::f32> pos = Cursor->getRelativePosition();
if(pos.X < (0.5f-BoxSize)) pos.X = (0.5f-BoxSize);
if(pos.X > (0.5f+BoxSize)) pos.X = (0.5f+BoxSize);
if(pos.Y < (0.5f-BoxSize)) pos.Y = (0.5f-BoxSize);
if(pos.Y > (0.5f+BoxSize)) pos.Y = (0.5f+BoxSize);
Cursor->setPosition(pos);
}
//! Process an input event
bool CScene3PCamera::OnEvent(const irr::SEvent& event)
{
if(!Active)
{
return false;
}
if(event.EventType == irr::EET_KEY_INPUT_EVENT)
{
}
return false;
}
//! Get/set active status
int CScene3PCamera::isActive()
{
return Active;
}
void CScene3PCamera::setActive(bool status)
{
// reset the cursor only if we are switching back to active from inactive
if(!Active && status)
{
updateCursorPosition();
}
Active = status;
}
//! Get/set box size
irr::f32 CScene3PCamera::getBoxSize()
{
return BoxSize;
}
void CScene3PCamera::setBoxSize(irr::f32 newSize)
{
BoxSize = newSize;
updateCursorPosition();
}
//! Access the camera's current orientation
irr::f32 CScene3PCamera::getOrientation()
{
return AngleY;
}
//! animates the scene node
void CScene3PCamera::animateNode(irr::scene::ISceneNode *node, irr::u32 timeMs)
{
// make sure you don't go attaching this animator to anything other than a camera
irr::scene::ICameraSceneNode *camera = (irr::scene::ICameraSceneNode*)node;
if(Manager->getActiveCamera() != camera)
{
return;
}
if(Active)
{
// Camera is active, rotate as necessary
irr::core::position2d<irr::f32> pos = Cursor->getRelativePosition();
if(pos.X < 0.5f-BoxSize-EPSILON)
{
AngleY -= (pos.X-(0.5f-BoxSize))*RotationSpeed;//Decrement of this statement gives the correct movement to the mouse.... by smartwhiz
}
if(pos.X > 0.5f+BoxSize+EPSILON)
{
AngleY -= (pos.X-(0.5f+BoxSize))*RotationSpeed;//Decrement of this statement gives the correct movement to the mouse.... by smartwhiz
}
// So we don't get huge rotation numbers
if(AngleY > 360.0f)
{
AngleY += 360.0f;
}
if(AngleY < 0.0f)
{
AngleY -= 360.0f;
}
if(pos.Y < 0.5f-BoxSize-EPSILON)
{
AngleZ -= (pos.Y-(0.5f-BoxSize))*RotationSpeed;
}
if(pos.Y > 0.5f+BoxSize+EPSILON)
{
AngleZ -= (pos.Y-(0.5f+BoxSize))*RotationSpeed;
}
// Ensure Vertical Rotation Angle is bounded correctly
if(AngleZ < MinAngle) AngleZ = MinAngle;
else if(AngleZ > MaxAngle) AngleZ = MaxAngle;
//keep the player in the view angle that of the camera, this is the change made......... by smartwhiz
if (XRotatePlayer == true){
Player->setRotation(irr::core::vector3df(0,-(AngleY+180),0));
}
updateCursorPosition();
}
// Create translation vector
irr::core::vector3df translation(Distancia,0,0);
translation.rotateXYBy(-AngleZ,irr::core::vector3df(0,0,0));
translation.rotateXZBy(AngleY,irr::core::vector3df(0,0,0));
// Assumes the camera is *not* a child of the player node
camera->setTarget(Player->getPosition()+TargetOffset);
camera->setPosition(Player->getPosition()+translation+TargetOffset);
}
Please help newbie.