laser.h
Code: Select all
#include <irrlicht.h>
#include <iostream>
#include <ITexture.h>
#include <SMaterial.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
// Just a simple wrapper :D
struct IrrQuad {
video::S3DVertex verts[4];
};
class CBeamNode : public scene::ISceneNode {
private:
// The beam material.
video::SMaterial material;
// Start/End Points
core::vector3df vStart;
core::vector3df vEnd;
// Bounding Box
core::aabbox3d<f32> Box;
// Size of the beam
float flScale;
// Beam color
video::SColor beamColor;
void DrawQuad( IrrQuad& quad ) {
u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setMaterial(material);
driver->drawIndexedTriangleList( &quad.verts[0], 4, &indices[0], 4 );
}
// Thanks to whoever wrote this little function :)
core::vector3df getTargetAngle( core::vector3df v, core::vector3df r) {
//v -current node position
//r -target node position
core::vector3df angle;
float x,y,z;
x = r.X - v.X;
y = r.Y - v.Y;
z = r.Z - v.Z;
//angle in X-Z plane
angle.Y = atan2 (x, z);
angle.Y *= (180 / 3.14); //converting from rad to degrees
//just making sure angle is somewhere between 0-360 degrees
if(angle.Y < 0) angle.Y += 360;
if(angle.Y >= 360) angle.Y -= 360;
//angle in Y-Z plane while Z and X axes are already rotated around Y
float z1 = sqrt(x*x + z*z);
angle.X = atan2 (z1, y);
angle.X *= (180 / 3.14); //converting from rad to degrees
angle.X -= 90;
//just making sure angle is somewhere between 0-360 degrees
if(angle.X < 0) angle.X += 360;
if(angle.X >= 360) angle.X -= 360;
return angle;
}
public:
CBeamNode( scene::ISceneNode* parent, scene::ISceneManager *mgr, s32 id, char* szBeam ) : scene::ISceneNode( parent, mgr, id ) {
// Setup the beam material
material.Wireframe = false;
material.Lighting = false;
material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
video::ITexture* texture = mgr->getVideoDriver( )->getTexture( szBeam );
material.setTexture(0, texture);
// Default to 32 units for the scale
flScale = 32.0;
// Default to white
beamColor.set( 255, 255, 255, 255 );
}
virtual void OnPreRender( ) {
if( IsVisible ) {
SceneManager->registerNodeForRendering( this );
}
}
virtual void render( ) {
SceneManager->getVideoDriver()->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);
// Figure out quads based on start/end points.
core::matrix4 m;
m.setRotationDegrees( getTargetAngle( vStart, vEnd ) );
core::vector3df vUp( 0, 1, 0 );
core::vector3df vRight( -1, 0, 0 );
m.transformVect( vRight );
m.transformVect( vUp );
// Draw the first cross
IrrQuad beam;
beam.verts[0] = video::S3DVertex( vStart + vUp * flScale, core::vector3df( 1, 1, 0 ), beamColor, core::vector2df( 0, 1 ) );
beam.verts[1] = video::S3DVertex( vStart + vUp * -flScale, core::vector3df( 1, 0, 0 ), beamColor, core::vector2df( 1, 1 ) );
beam.verts[2] = video::S3DVertex( vEnd + vUp * -flScale, core::vector3df( 0, 1, 1 ), beamColor, core::vector2df( 1, 0 ) );
beam.verts[3] = video::S3DVertex( vEnd + vUp * flScale, core::vector3df( 0, 0, 1 ), beamColor, core::vector2df( 0, 0 ) );
DrawQuad( beam );
// Draw the second cross.
beam.verts[0] = video::S3DVertex( vStart + vRight * flScale, core::vector3df( 1, 1, 0 ), beamColor, core::vector2df( 0, 1 ) );
beam.verts[1] = video::S3DVertex( vStart + vRight * -flScale, core::vector3df( 1, 0, 0 ), beamColor, core::vector2df( 1, 1 ) );
beam.verts[2] = video::S3DVertex( vEnd + vRight * -flScale, core::vector3df( 0, 1, 1 ), beamColor, core::vector2df( 1, 0 ) );
beam.verts[3] = video::S3DVertex( vEnd + vRight * flScale, core::vector3df( 0, 0, 1 ), beamColor, core::vector2df( 0, 0 ) );
DrawQuad( beam );
}
virtual const core::aabbox3d<f32>& getBoundingBox() const {
return Box;
}
virtual s32 getMaterialCount() {
return 1;
}
virtual video::SMaterial& getMaterial(s32 i) {
return material;
}
void SetStartPoint( core::vector3df pos ) {
vStart = pos;
}
void SetEndPoint( core::vector3df pos ) {
vEnd = pos;
}
void SetBeamScale( float size ) {
flScale = size;
}
void SetBeamColor( video::SColor color ) {
beamColor = color;
}
};
Code: Select all
/*
This Tutorial shows how to move and animate SceneNodes. The
basic concept of SceneNodeAnimators is shown as well as manual
movement of nodes using the keyboard.
As always, I include the header files, use the irr namespace,
and tell the linker to link with the .lib file.
*/
#include <irrlicht.h>
[b]#include "laser.h"[/b]
#include "flightfunctions.h"
#include "shipcontrols.h"
#include <iostream>
using namespace irr;
double playerspeed = 0.0;
#pragma comment(lib, "Irrlicht.lib")
int main()
{
// let user select driver type
video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
printf("Please select the driver you want for this example:\n"\
" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
" (f) NullDevice\n (otherKey) exit\n\n");
char i;
std::cin >> i;
switch(i)
{
case 'a': driverType = video::EDT_DIRECT3D9;break;
case 'b': driverType = video::EDT_DIRECT3D8;break;
case 'c': driverType = video::EDT_OPENGL; break;
case 'd': driverType = video::EDT_SOFTWARE; break;
case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
case 'f': driverType = video::EDT_NULL; break;
default: return 0;
}
// create device
MyEventReceiver receiver;
IrrlichtDevice* device = createDevice( driverType, core::dimension2d<s32>(1000, 800),
16, false, false, false, &receiver);
if (device == 0)
return 1; // could not create selected driver.
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
smgr->addSkyDomeSceneNode(driver->getTexture("media/Textures/sky.bmp"),32,32,1.0f,2.0f);
smgr->loadScene("Media/Levels/space.irr");
scene::ISceneNode* node1 = smgr->addSphereSceneNode(); //replace with what ever you want
if (node1)
{
node1->setMaterialFlag(EMF_LIGHTING, false);
node1->setMaterialTexture( 0, driver->getTexture("media/Textures/borg.psd") );
node1->setPosition(core::vector3df(-550,0,0));
}
scene::ISceneNode* node2 = smgr->addCubeSceneNode(); //just for reference
if (node2)
node2->setMaterialFlag(video::EMF_LIGHTING, false);
node2->setPosition(core::vector3df(500,0,100));
scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();
[b]CBeamNode* beam = new CBeamNode( smgr->getRootSceneNode( ), smgr, 200, "laserbeam.bmp" );[/b]
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, SColor(255,100,101,140));
smgr->drawAll();
// direction control
if(keys[irr::KEY_LEFT])
{
turn(node1, 0.5);
}
if(keys[irr::KEY_RIGHT])
{
turn(node1, -0.5);
}
if(keys[irr::KEY_UP])
{
pitch(node1, 0.5);
}
if(keys[irr::KEY_DOWN])
{
pitch(node1, -0.5);
}
if(keys[irr::KEY_COMMA])
{
roll(node1, 0.01);
}
if(keys[irr::KEY_PERIOD])
{
roll(node1, -0.01);
}
// movement control
if(keys[irr::KEY_KEY_W])
{
playerspeed = playerspeed + 0.5;
}
if(keys[irr::KEY_KEY_S])
{
playerspeed = playerspeed - 0.0001;
if (playerspeed = -0.01)
playerspeed = -.009;
move(node1, core::vector3df(0,0,playerspeed));
}
if(keys[irr::KEY_KEY_Q])
{
playerspeed = 0;
}
[b]if(keys[irr::KEY_KEY_F])
{
beam->SetStartPoint( core::vector3df( -500, 0, 0 ) );
beam->SetEndPoint( core::vector3df( 500, 0, 0 ) );
beam->SetBeamColor( video::SColor( 200, 0, 255, 0 ) );
beam->SetBeamScale(1.5);
}[/b]
move(node1, core::vector3df(0,0,playerspeed));
makeCockpit(camera, node1, core::vector3df(0,7,-30));
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw tmp(L"Movement Example - Irrlicht Engine [");
tmp += driver->getName();
tmp += L"] fps: ";
tmp += fps;
device->setWindowCaption(tmp.c_str());
lastFPS = fps;
}
}
device->drop();
return 1;
}