Commit a98c3319 authored by hybrid's avatar hybrid

Move cube creation to geometry creator. Submitted by wITTus.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2345 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 225d51e2
......@@ -26,7 +26,14 @@ class IGeometryCreator : public IReferenceCounted
{
public:
//! Create a psuedo-random mesh representing a hilly terrain.
//! Creates a simple cube mesh.
/**
\param size Size of the cube.
\return Generated mesh.
*/
virtual IMesh* createCubeMesh(f32 size=5.f) const =0;
//! Create a pseudo-random mesh representing a hilly terrain.
/**
\param tileSize The size of each time.
\param tileCount The number of tiles in each dimension.
......@@ -34,6 +41,7 @@ public:
\param hillHeight The maximum height of the hills.
\param countHills The number of hills along each dimension.
\param textureRepeatCount The number of times to repeat the material texture along each dimension.
\return Generated mesh.
*/
virtual IMesh* createHillPlaneMesh(
const core::dimension2d<f32>& tileSize,
......@@ -56,6 +64,7 @@ public:
\param driver The current video driver.
\param defaultVertexBlockSize (to be documented)
\param debugBorders (to be documented)
\return Generated mesh.
*/
virtual IMesh* createTerrainMesh(video::IImage* texture,
video::IImage* heightmap,
......@@ -76,6 +85,7 @@ public:
than the cylinder's diameter
\param colorCylinder color of the cylinder
\param colorCone color of the cone
\return Generated mesh.
*/
virtual IMesh* createArrowMesh(const u32 tesselationCylinder = 4,
const u32 tesselationCone = 8, const f32 height = 1.f,
......@@ -89,6 +99,7 @@ public:
\param radius Radius of the sphere
\param polyCountX Number of quads used for the horizontal tiling
\param polyCountY Number of quads used for the vertical tiling
\return Generated mesh.
*/
virtual IMesh* createSphereMesh(f32 radius = 5.f,
u32 polyCountX = 16, u32 polyCountY = 16) const =0;
......@@ -101,6 +112,7 @@ public:
\param color The color of the cylinder.
\param closeTop If true, close the ends of the cylinder, otherwise leave them open.
\param oblique (to be documented)
\return Generated mesh.
*/
virtual IMesh* createCylinderMesh(f32 radius, f32 length,
u32 tesselation,
......@@ -115,6 +127,7 @@ public:
\param colorTop The color of the top of the cone.
\param colorBottom The color of the bottom of the cone.
\param oblique (to be documented)
\return Generated mesh.
*/
virtual IMesh* createConeMesh(f32 radius, f32 length, u32 tesselation,
const video::SColor& colorTop=video::SColor(0xffffffff),
......@@ -129,6 +142,7 @@ public:
\param tailColor Color at the mid of the light.
\param lpDistance Virtual distance of the light point for normals.
\param lightDim Dimensions of the light.
\return Generated mesh.
*/
virtual IMesh* createVolumeLightMesh(
const u32 subdivideU=32, const u32 subdivideV=32,
......
......@@ -31,61 +31,22 @@ namespace scene
CCubeSceneNode::CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr,
s32 id, const core::vector3df& position,
const core::vector3df& rotation, const core::vector3df& scale)
: IMeshSceneNode(parent, mgr, id, position, rotation, scale), Size(size)
: IMeshSceneNode(parent, mgr, id, position, rotation, scale),
Mesh(0), Size(size)
{
#ifdef _DEBUG
setDebugName("CCubeSceneNode");
#endif
const u16 u[36] = { 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};
SMeshBuffer* buf = new SMeshBuffer();
Mesh.addMeshBuffer(buf);
buf->Indices.set_used(36);
for (u32 i=0; i<36; ++i)
buf->Indices[i] = u[i];
buf->drop();
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);
SMeshBuffer* buf = (SMeshBuffer*)Mesh.getMeshBuffer(0);
buf->Vertices.reallocate(12);
// Start setting vertices from index 0 to deal with this method being called multiple times.
buf->Vertices.set_used(0);
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);
for (u32 i=0; i<12; ++i)
{
buf->Vertices[i].Pos -= core::vector3df(0.5f, 0.5f, 0.5f);
buf->Vertices[i].Pos *= Size;
buf->BoundingBox.addInternalPoint(buf->Vertices[i].Pos);
}
if (Mesh)
Mesh->drop();
Mesh = SceneManager->getGeometryCreator()->createCubeMesh(Size);
}
......@@ -93,16 +54,16 @@ void CCubeSceneNode::setSize()
void CCubeSceneNode::render()
{
video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setMaterial(Mesh.getMeshBuffer(0)->getMaterial());
driver->setMaterial(Mesh->getMeshBuffer(0)->getMaterial());
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
driver->drawMeshBuffer(Mesh.getMeshBuffer(0));
driver->drawMeshBuffer(Mesh->getMeshBuffer(0));
}
//! returns the axis aligned bounding box of this node
const core::aabbox3d<f32>& CCubeSceneNode::getBoundingBox() const
{
return Mesh.getMeshBuffer(0)->getBoundingBox();
return Mesh->getMeshBuffer(0)->getBoundingBox();
}
......@@ -114,14 +75,10 @@ void CCubeSceneNode::OnRegisterSceneNode()
}
//! 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.
//! returns the material based on the zero based index i.
video::SMaterial& CCubeSceneNode::getMaterial(u32 i)
{
return Mesh.getMeshBuffer(0)->getMaterial();
return Mesh->getMeshBuffer(0)->getMaterial();
}
......@@ -144,9 +101,13 @@ void CCubeSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeRea
//! Reads attributes of the scene node.
void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
Size = in->getAttributeAsFloat("Size");
Size = core::max_(Size, 0.0001f);
setSize();
f32 newSize = in->getAttributeAsFloat("Size");
newSize = core::max_(newSize, 0.0001f);
if (newSize != Size)
{
Size = newSize;
setSize();
}
ISceneNode::deserializeAttributes(in, options);
}
......@@ -155,8 +116,10 @@ void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeRe
//! 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;
if (!newParent)
newParent = Parent;
if (!newManager)
newManager = SceneManager;
CCubeSceneNode* nb = new CCubeSceneNode(Size, newParent,
newManager, ID, RelativeTranslation);
......
......@@ -2,8 +2,8 @@
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_TEST_SCENE_NODE_H_INCLUDED__
#define __C_TEST_SCENE_NODE_H_INCLUDED__
#ifndef __C_CUBE_SCENE_NODE_H_INCLUDED__
#define __C_CUBE_SCENE_NODE_H_INCLUDED__
#include "IMeshSceneNode.h"
#include "SMesh.h"
......@@ -56,7 +56,7 @@ namespace scene
virtual void setMesh(IMesh* mesh) {}
//! Returns the current mesh
virtual IMesh* getMesh(void) { return &Mesh; }
virtual IMesh* getMesh(void) { return Mesh; }
//! Sets if the scene node should not copy the materials of the mesh but use them in a read only style.
/* In this way it is possible to change the materials a mesh causing all mesh scene nodes
......@@ -69,7 +69,7 @@ namespace scene
private:
void setSize();
SMesh Mesh;
IMesh* Mesh;
f32 Size;
};
......
......@@ -16,6 +16,58 @@ namespace irr
namespace scene
{
IMesh* CGeometryCreator::createCubeMesh(f32 size) const
{
SMeshBuffer* buffer = new SMeshBuffer();
// Create indices
const u16 u[36] = { 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};
buffer->Indices.set_used(36);
for (u32 i=0; i<36; ++i)
buffer->Indices[i] = u[i];
// Create vertices
video::SColor clr(255,255,255,255);
buffer->Vertices.reallocate(12);
buffer->Vertices.set_used(0);
buffer->Vertices.push_back(video::S3DVertex(0,0,0, -1,-1,-1, clr, 0, 1));
buffer->Vertices.push_back(video::S3DVertex(1,0,0, 1,-1,-1, clr, 1, 1));
buffer->Vertices.push_back(video::S3DVertex(1,1,0, 1, 1,-1, clr, 1, 0));
buffer->Vertices.push_back(video::S3DVertex(0,1,0, -1, 1,-1, clr, 0, 0));
buffer->Vertices.push_back(video::S3DVertex(1,0,1, 1,-1, 1, clr, 0, 1));
buffer->Vertices.push_back(video::S3DVertex(1,1,1, 1, 1, 1, clr, 0, 0));
buffer->Vertices.push_back(video::S3DVertex(0,1,1, -1, 1, 1, clr, 1, 0));
buffer->Vertices.push_back(video::S3DVertex(0,0,1, -1,-1, 1, clr, 1, 1));
buffer->Vertices.push_back(video::S3DVertex(0,1,1, -1, 1, 1, clr, 0, 1));
buffer->Vertices.push_back(video::S3DVertex(0,1,0, -1, 1,-1, clr, 1, 1));
buffer->Vertices.push_back(video::S3DVertex(1,0,1, 1,-1, 1, clr, 1, 0));
buffer->Vertices.push_back(video::S3DVertex(1,0,0, 1,-1,-1, clr, 0, 0));
// Recalculate bounding box
buffer->BoundingBox.reset(0,0,0);
for (u32 i=0; i<12; ++i)
{
buffer->Vertices[i].Pos -= core::vector3df(0.5f, 0.5f, 0.5f);
buffer->Vertices[i].Pos *= size;
buffer->BoundingBox.addInternalPoint(buffer->Vertices[i].Pos);
}
SMesh* mesh = new SMesh;
mesh->addMeshBuffer(buffer);
buffer->drop();
mesh->recalculateBoundingBox();
return mesh;
}
// creates a hill plane
IMesh* CGeometryCreator::createHillPlaneMesh(
const core::dimension2d<f32>& tileSize,
......
......@@ -19,6 +19,7 @@ class CGeometryCreator : public IGeometryCreator
{
void addToBuffer(const video::S3DVertex& v, SMeshBuffer* Buffer) const;
public:
IMesh* createCubeMesh(f32 size) const;
IMesh* createHillPlaneMesh(
const core::dimension2d<f32>& tileSize, const core::dimension2d<u32>& tileCount,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment