(C++) CFreeBoxSceneNode

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
jimon
Posts: 20
Joined: Sun Nov 20, 2005 7:46 pm
Location: Ukraine, Kiev

(C++) CFreeBoxSceneNode

Post by jimon »

Code: Select all

class CFreeBoxSceneNode : public scene::ISceneNode
{

	core::aabbox3d<f32> Box;
	video::S3DVertex Vertices[12];
	video::SMaterial Material;
	u16 indices[36];

public:

	CFreeBoxSceneNode(vector3df point1,vector3df point2,vector3df point3,vector3df point4,
		vector3df point5,vector3df point6,vector3df point7,vector3df point8,
		scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,SColor color,float X = 1,float Y = 1);
		

	virtual void OnPreRender();
	virtual void render();
	virtual const core::aabbox3d<f32>& getBoundingBox() const;
	virtual s32 getMaterialCount();
	virtual video::SMaterial& getMaterial(s32 i);
};

CFreeBoxSceneNode::CFreeBoxSceneNode(vector3df point1,vector3df point2,vector3df point3,vector3df point4,
	vector3df point5,vector3df point6,vector3df point7,vector3df point8,
	scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,SColor color,float X,float Y)
	: scene::ISceneNode(parent, mgr, id)
{
	Material.Wireframe = false;
	Material.Lighting = false;

	SColor F = color;

	/*
	vector3df point1(0,0,0);
	vector3df point2(1,0,0);
	vector3df point3(1,1,0);
	vector3df point4(0,1,0);
	vector3df point5(1,0,1);
	vector3df point6(1,1,1);
	vector3df point7(0,1,1);
	vector3df point8(0,0,1);
	*/

	Vertices[0]  = video::S3DVertex(point1.X,point1.Y,point1.Z,0,0,0,F,0,Y); 
	Vertices[1]  = video::S3DVertex(point2.X,point2.Y,point2.Z,0,0,0,F,X,Y); 
	Vertices[2]  = video::S3DVertex(point3.X,point3.Y,point3.Z,0,0,0,F,X,0); 
	Vertices[3]  = video::S3DVertex(point4.X,point4.Y,point4.Z,0,0,0,F,0,0); 
	Vertices[4]  = video::S3DVertex(point5.X,point5.Y,point5.Z,0,0,0,F,0,Y); 
	Vertices[5]  = video::S3DVertex(point6.X,point6.Y,point6.Z,0,0,0,F,0,0); 
	Vertices[6]  = video::S3DVertex(point7.X,point7.Y,point7.Z,0,0,0,F,X,0); 
	Vertices[7]  = video::S3DVertex(point8.X,point8.Y,point8.Z,0,0,0,F,X,Y); 
	Vertices[8]  = video::S3DVertex(point7.X,point7.Y,point7.Z,0,0,0,F,0,Y); 
	Vertices[9]  = video::S3DVertex(point4.X,point4.Y,point4.Z,0,0,0,F,X,Y); 
	Vertices[10] = video::S3DVertex(point5.X,point5.Y,point5.Z,0,0,0,F,X,0); 
	Vertices[11] = video::S3DVertex(point2.X,point2.Y,point2.Z,0,0,0,F,0,0); 

	Box.reset(Vertices[0].Pos);
	for (s32 i=1; i<12; ++i)
		Box.addInternalPoint(Vertices[i].Pos);

	u16 u[] = {	0,2,1,   0,3,2,   1,5,4,   1,2,5,   4,6,7,   4,5,6, 
           7,3,0,   7,6,3,   9,5,2,   9,8,5,   0,11,10,   0,10,7 };

	for (s32 i=0; i<36; ++i)
		indices[i] = u[i];

}

void CFreeBoxSceneNode::OnPreRender()
{
	if (IsVisible)
		SceneManager->registerNodeForRendering(this);

	ISceneNode::OnPreRender();
}

void CFreeBoxSceneNode::render()
{
	video::IVideoDriver* driver = SceneManager->getVideoDriver();
	driver->setMaterial(Material);
	driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
	driver->drawIndexedTriangleList(&Vertices[0], 12, &indices[0], 12);
}

const core::aabbox3d<f32>& CFreeBoxSceneNode::getBoundingBox() const
{
	return Box;
}

s32 CFreeBoxSceneNode::getMaterialCount()
{
	return 1;
}

video::SMaterial& CFreeBoxSceneNode::getMaterial(s32 i)
{
	return Material;
}	

use :

Code: Select all

//box points
vector3df p1(-1,-1,-1);
vector3df p2(1,-1,-1);
vector3df p3(1,1,-1);
vector3df p4(-1,1,-1);
vector3df p5(1,-1,1);
vector3df p6(1,1,1);
vector3df p7(-1,1,1);
vector3df p8(-1,-1,1);

SColor color(255,127,127,255); //default color to all vertex
float t_x=1; //texturing scale
float t_y=1;

ISceneNode * test = new CFreeBoxSceneNode(p1,p2,p3,p4,p5,p6,p7,p8,smgr->getRootSceneNode(),smgr,-1,color,t_x,t_y);
Last edited by jimon on Sun Feb 05, 2006 11:42 am, edited 1 time in total.
my english is very bad,sorry
Guest

Post by Guest »

you did not respect the forum rules:

making the code snippet title very clear, what it does and the type of user it's aimed at.

thank you :)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It claims to be a box (one that is not axis aligned). Unfortunately, you can make something that is not a box at all...
Post Reply