2009-01-14 04:37:51 -08:00
|
|
|
// Copyright (C) 2002-2009 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CCubeSceneNode.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
#include "ISceneManager.h"
|
|
|
|
#include "S3DVertex.h"
|
2008-03-19 02:29:57 -07:00
|
|
|
#include "SMeshBuffer.h"
|
2007-05-20 11:03:49 -07:00
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
2007-07-30 10:14:55 -07:00
|
|
|
/*
|
|
|
|
011 111
|
|
|
|
/6,8------/5 y
|
|
|
|
/ | / | ^ z
|
|
|
|
/ | / | | /
|
|
|
|
010 3,9-------2 | |/
|
|
|
|
| 7- - -10,4 101 *---->x
|
|
|
|
| / | /
|
|
|
|
|/ | /
|
|
|
|
0------11,1/
|
|
|
|
000 100
|
|
|
|
*/
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! constructor
|
2007-07-30 10:14:55 -07:00
|
|
|
CCubeSceneNode::CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr,
|
|
|
|
s32 id, const core::vector3df& position,
|
|
|
|
const core::vector3df& rotation, const core::vector3df& scale)
|
2008-03-19 02:29:57 -07:00
|
|
|
: IMeshSceneNode(parent, mgr, id, position, rotation, scale), Size(size)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("CCubeSceneNode");
|
|
|
|
#endif
|
|
|
|
|
2007-08-13 16:28:44 -07:00
|
|
|
const u16 u[36] = { 0,2,1, 0,3,2, 1,5,4, 1,2,5, 4,6,7, 4,5,6,
|
2007-05-20 11:03:49 -07:00
|
|
|
7,3,0, 7,6,3, 9,5,2, 9,8,5, 0,11,10, 0,10,7};
|
|
|
|
|
2008-03-19 02:29:57 -07:00
|
|
|
SMeshBuffer* buf = new SMeshBuffer();
|
|
|
|
Mesh.addMeshBuffer(buf);
|
|
|
|
buf->Indices.set_used(36);
|
2007-07-30 10:14:55 -07:00
|
|
|
for (u32 i=0; i<36; ++i)
|
2008-03-19 02:29:57 -07:00
|
|
|
buf->Indices[i] = u[i];
|
2008-04-29 13:55:30 -07:00
|
|
|
buf->drop();
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
setSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCubeSceneNode::setSize()
|
|
|
|
{
|
|
|
|
// we are creating the cube mesh here.
|
|
|
|
|
|
|
|
// nicer texture mapping sent in by Dr Andros C Bragianos
|
|
|
|
// .. and then improved by jox.
|
|
|
|
|
|
|
|
video::SColor clr(255,255,255,255);
|
|
|
|
|
2008-03-19 02:29:57 -07:00
|
|
|
SMeshBuffer* buf = (SMeshBuffer*)Mesh.getMeshBuffer(0);
|
2008-11-22 13:32:39 -08:00
|
|
|
|
2008-03-19 02:29:57 -07:00
|
|
|
buf->Vertices.reallocate(12);
|
2008-11-22 13:32:39 -08:00
|
|
|
// Start setting vertices from index 0 to deal with this method being called multiple times.
|
|
|
|
buf->Vertices.set_used(0);
|
2008-03-19 02:29:57 -07:00
|
|
|
buf->Vertices.push_back(video::S3DVertex(0,0,0, -1,-1,-1, clr, 0, 1));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(1,0,0, 1,-1,-1, clr, 1, 1));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(1,1,0, 1, 1,-1, clr, 1, 0));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(0,1,0, -1, 1,-1, clr, 0, 0));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(1,0,1, 1,-1, 1, clr, 0, 1));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(1,1,1, 1, 1, 1, clr, 0, 0));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(0,1,1, -1, 1, 1, clr, 1, 0));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(0,0,1, -1,-1, 1, clr, 1, 1));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(0,1,1, -1, 1, 1, clr, 0, 1));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(0,1,0, -1, 1,-1, clr, 1, 1));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(1,0,1, 1,-1, 1, clr, 1, 0));
|
|
|
|
buf->Vertices.push_back(video::S3DVertex(1,0,0, 1,-1,-1, clr, 0, 0));
|
|
|
|
|
|
|
|
buf->BoundingBox.reset(0,0,0);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2007-07-30 10:14:55 -07:00
|
|
|
for (u32 i=0; i<12; ++i)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-03-19 02:29:57 -07:00
|
|
|
buf->Vertices[i].Pos -= core::vector3df(0.5f, 0.5f, 0.5f);
|
|
|
|
buf->Vertices[i].Pos *= Size;
|
|
|
|
buf->BoundingBox.addInternalPoint(buf->Vertices[i].Pos);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! renders the node.
|
|
|
|
void CCubeSceneNode::render()
|
|
|
|
{
|
|
|
|
video::IVideoDriver* driver = SceneManager->getVideoDriver();
|
2008-03-19 02:29:57 -07:00
|
|
|
driver->setMaterial(Mesh.getMeshBuffer(0)->getMaterial());
|
2007-05-20 11:03:49 -07:00
|
|
|
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
|
2008-03-19 02:29:57 -07:00
|
|
|
driver->drawMeshBuffer(Mesh.getMeshBuffer(0));
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns the axis aligned bounding box of this node
|
|
|
|
const core::aabbox3d<f32>& CCubeSceneNode::getBoundingBox() const
|
|
|
|
{
|
2008-03-19 02:29:57 -07:00
|
|
|
return Mesh.getMeshBuffer(0)->getBoundingBox();
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCubeSceneNode::OnRegisterSceneNode()
|
|
|
|
{
|
|
|
|
if (IsVisible)
|
|
|
|
SceneManager->registerNodeForRendering(this);
|
2007-11-13 06:36:17 -08:00
|
|
|
ISceneNode::OnRegisterSceneNode();
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns the material based on the zero based index i. To get the amount
|
|
|
|
//! of materials used by this scene node, use getMaterialCount().
|
|
|
|
//! This function is needed for inserting the node into the scene hirachy on a
|
|
|
|
//! optimal position for minimizing renderstate changes, but can also be used
|
|
|
|
//! to directly modify the material of a scene node.
|
|
|
|
video::SMaterial& CCubeSceneNode::getMaterial(u32 i)
|
|
|
|
{
|
2008-03-19 02:29:57 -07:00
|
|
|
return Mesh.getMeshBuffer(0)->getMaterial();
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns amount of materials used by this scene node.
|
2007-09-16 16:41:55 -07:00
|
|
|
u32 CCubeSceneNode::getMaterialCount() const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Writes attributes of the scene node.
|
2007-09-14 15:25:59 -07:00
|
|
|
void CCubeSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
ISceneNode::serializeAttributes(out, options);
|
|
|
|
|
|
|
|
out->addFloat("Size", Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Reads attributes of the scene node.
|
|
|
|
void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
|
|
|
|
{
|
|
|
|
Size = in->getAttributeAsFloat("Size");
|
2007-09-19 07:08:28 -07:00
|
|
|
Size = core::max_(Size, 0.0001f);
|
2007-05-20 11:03:49 -07:00
|
|
|
setSize();
|
|
|
|
|
|
|
|
ISceneNode::deserializeAttributes(in, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Creates a clone of this scene node and its children.
|
|
|
|
ISceneNode* CCubeSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
|
|
|
|
{
|
|
|
|
if (!newParent) newParent = Parent;
|
|
|
|
if (!newManager) newManager = SceneManager;
|
|
|
|
|
|
|
|
CCubeSceneNode* nb = new CCubeSceneNode(Size, newParent,
|
|
|
|
newManager, ID, RelativeTranslation);
|
|
|
|
|
|
|
|
nb->cloneMembers(this, newManager);
|
2008-03-19 02:29:57 -07:00
|
|
|
nb->getMaterial(0) = getMaterial(0);
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
nb->drop();
|
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|
|
|
|
|