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 ...@@ -26,7 +26,14 @@ class IGeometryCreator : public IReferenceCounted
{ {
public: 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 tileSize The size of each time.
\param tileCount The number of tiles in each dimension. \param tileCount The number of tiles in each dimension.
...@@ -34,6 +41,7 @@ public: ...@@ -34,6 +41,7 @@ public:
\param hillHeight The maximum height of the hills. \param hillHeight The maximum height of the hills.
\param countHills The number of hills along each dimension. \param countHills The number of hills along each dimension.
\param textureRepeatCount The number of times to repeat the material texture along each dimension. \param textureRepeatCount The number of times to repeat the material texture along each dimension.
\return Generated mesh.
*/ */
virtual IMesh* createHillPlaneMesh( virtual IMesh* createHillPlaneMesh(
const core::dimension2d<f32>& tileSize, const core::dimension2d<f32>& tileSize,
...@@ -56,6 +64,7 @@ public: ...@@ -56,6 +64,7 @@ public:
\param driver The current video driver. \param driver The current video driver.
\param defaultVertexBlockSize (to be documented) \param defaultVertexBlockSize (to be documented)
\param debugBorders (to be documented) \param debugBorders (to be documented)
\return Generated mesh.
*/ */
virtual IMesh* createTerrainMesh(video::IImage* texture, virtual IMesh* createTerrainMesh(video::IImage* texture,
video::IImage* heightmap, video::IImage* heightmap,
...@@ -76,6 +85,7 @@ public: ...@@ -76,6 +85,7 @@ public:
than the cylinder's diameter than the cylinder's diameter
\param colorCylinder color of the cylinder \param colorCylinder color of the cylinder
\param colorCone color of the cone \param colorCone color of the cone
\return Generated mesh.
*/ */
virtual IMesh* createArrowMesh(const u32 tesselationCylinder = 4, virtual IMesh* createArrowMesh(const u32 tesselationCylinder = 4,
const u32 tesselationCone = 8, const f32 height = 1.f, const u32 tesselationCone = 8, const f32 height = 1.f,
...@@ -89,6 +99,7 @@ public: ...@@ -89,6 +99,7 @@ public:
\param radius Radius of the sphere \param radius Radius of the sphere
\param polyCountX Number of quads used for the horizontal tiling \param polyCountX Number of quads used for the horizontal tiling
\param polyCountY Number of quads used for the vertical tiling \param polyCountY Number of quads used for the vertical tiling
\return Generated mesh.
*/ */
virtual IMesh* createSphereMesh(f32 radius = 5.f, virtual IMesh* createSphereMesh(f32 radius = 5.f,
u32 polyCountX = 16, u32 polyCountY = 16) const =0; u32 polyCountX = 16, u32 polyCountY = 16) const =0;
...@@ -101,6 +112,7 @@ public: ...@@ -101,6 +112,7 @@ public:
\param color The color of the cylinder. \param color The color of the cylinder.
\param closeTop If true, close the ends of the cylinder, otherwise leave them open. \param closeTop If true, close the ends of the cylinder, otherwise leave them open.
\param oblique (to be documented) \param oblique (to be documented)
\return Generated mesh.
*/ */
virtual IMesh* createCylinderMesh(f32 radius, f32 length, virtual IMesh* createCylinderMesh(f32 radius, f32 length,
u32 tesselation, u32 tesselation,
...@@ -115,6 +127,7 @@ public: ...@@ -115,6 +127,7 @@ public:
\param colorTop The color of the top of the cone. \param colorTop The color of the top of the cone.
\param colorBottom The color of the bottom of the cone. \param colorBottom The color of the bottom of the cone.
\param oblique (to be documented) \param oblique (to be documented)
\return Generated mesh.
*/ */
virtual IMesh* createConeMesh(f32 radius, f32 length, u32 tesselation, virtual IMesh* createConeMesh(f32 radius, f32 length, u32 tesselation,
const video::SColor& colorTop=video::SColor(0xffffffff), const video::SColor& colorTop=video::SColor(0xffffffff),
...@@ -129,6 +142,7 @@ public: ...@@ -129,6 +142,7 @@ public:
\param tailColor Color at the mid of the light. \param tailColor Color at the mid of the light.
\param lpDistance Virtual distance of the light point for normals. \param lpDistance Virtual distance of the light point for normals.
\param lightDim Dimensions of the light. \param lightDim Dimensions of the light.
\return Generated mesh.
*/ */
virtual IMesh* createVolumeLightMesh( virtual IMesh* createVolumeLightMesh(
const u32 subdivideU=32, const u32 subdivideV=32, const u32 subdivideU=32, const u32 subdivideV=32,
......
...@@ -31,61 +31,22 @@ namespace scene ...@@ -31,61 +31,22 @@ namespace scene
CCubeSceneNode::CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr, CCubeSceneNode::CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr,
s32 id, const core::vector3df& position, s32 id, const core::vector3df& position,
const core::vector3df& rotation, const core::vector3df& scale) 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 #ifdef _DEBUG
setDebugName("CCubeSceneNode"); setDebugName("CCubeSceneNode");
#endif #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(); setSize();
} }
void CCubeSceneNode::setSize() void CCubeSceneNode::setSize()
{ {
// we are creating the cube mesh here. if (Mesh)
Mesh->drop();
// nicer texture mapping sent in by Dr Andros C Bragianos Mesh = SceneManager->getGeometryCreator()->createCubeMesh(Size);
// .. 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);
}
} }
...@@ -93,16 +54,16 @@ void CCubeSceneNode::setSize() ...@@ -93,16 +54,16 @@ void CCubeSceneNode::setSize()
void CCubeSceneNode::render() void CCubeSceneNode::render()
{ {
video::IVideoDriver* driver = SceneManager->getVideoDriver(); video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setMaterial(Mesh.getMeshBuffer(0)->getMaterial()); driver->setMaterial(Mesh->getMeshBuffer(0)->getMaterial());
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation); 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 //! returns the axis aligned bounding box of this node
const core::aabbox3d<f32>& CCubeSceneNode::getBoundingBox() const const core::aabbox3d<f32>& CCubeSceneNode::getBoundingBox() const
{ {
return Mesh.getMeshBuffer(0)->getBoundingBox(); return Mesh->getMeshBuffer(0)->getBoundingBox();
} }
...@@ -114,14 +75,10 @@ void CCubeSceneNode::OnRegisterSceneNode() ...@@ -114,14 +75,10 @@ void CCubeSceneNode::OnRegisterSceneNode()
} }
//! returns the material based on the zero based index i. To get the amount //! returns the material based on the zero based index i.
//! 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) 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 ...@@ -144,9 +101,13 @@ void CCubeSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeRea
//! Reads attributes of the scene node. //! Reads attributes of the scene node.
void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{ {
Size = in->getAttributeAsFloat("Size"); f32 newSize = in->getAttributeAsFloat("Size");
Size = core::max_(Size, 0.0001f); newSize = core::max_(newSize, 0.0001f);
if (newSize != Size)
{
Size = newSize;
setSize(); setSize();
}
ISceneNode::deserializeAttributes(in, options); ISceneNode::deserializeAttributes(in, options);
} }
...@@ -155,8 +116,10 @@ void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeRe ...@@ -155,8 +116,10 @@ void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeRe
//! Creates a clone of this scene node and its children. //! Creates a clone of this scene node and its children.
ISceneNode* CCubeSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager) ISceneNode* CCubeSceneNode::clone(ISceneNode* newParent, ISceneManager* newManager)
{ {
if (!newParent) newParent = Parent; if (!newParent)
if (!newManager) newManager = SceneManager; newParent = Parent;
if (!newManager)
newManager = SceneManager;
CCubeSceneNode* nb = new CCubeSceneNode(Size, newParent, CCubeSceneNode* nb = new CCubeSceneNode(Size, newParent,
newManager, ID, RelativeTranslation); newManager, ID, RelativeTranslation);
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// This file is part of the "Irrlicht Engine". // This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h // For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_TEST_SCENE_NODE_H_INCLUDED__ #ifndef __C_CUBE_SCENE_NODE_H_INCLUDED__
#define __C_TEST_SCENE_NODE_H_INCLUDED__ #define __C_CUBE_SCENE_NODE_H_INCLUDED__
#include "IMeshSceneNode.h" #include "IMeshSceneNode.h"
#include "SMesh.h" #include "SMesh.h"
...@@ -56,7 +56,7 @@ namespace scene ...@@ -56,7 +56,7 @@ namespace scene
virtual void setMesh(IMesh* mesh) {} virtual void setMesh(IMesh* mesh) {}
//! Returns the current 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. //! 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 /* In this way it is possible to change the materials a mesh causing all mesh scene nodes
...@@ -69,7 +69,7 @@ namespace scene ...@@ -69,7 +69,7 @@ namespace scene
private: private:
void setSize(); void setSize();
SMesh Mesh; IMesh* Mesh;
f32 Size; f32 Size;
}; };
......
...@@ -16,6 +16,58 @@ namespace irr ...@@ -16,6 +16,58 @@ namespace irr
namespace scene 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 // creates a hill plane
IMesh* CGeometryCreator::createHillPlaneMesh( IMesh* CGeometryCreator::createHillPlaneMesh(
const core::dimension2d<f32>& tileSize, const core::dimension2d<f32>& tileSize,
......
...@@ -19,6 +19,7 @@ class CGeometryCreator : public IGeometryCreator ...@@ -19,6 +19,7 @@ class CGeometryCreator : public IGeometryCreator
{ {
void addToBuffer(const video::S3DVertex& v, SMeshBuffer* Buffer) const; void addToBuffer(const video::S3DVertex& v, SMeshBuffer* Buffer) const;
public: public:
IMesh* createCubeMesh(f32 size) const;
IMesh* createHillPlaneMesh( IMesh* createHillPlaneMesh(
const core::dimension2d<f32>& tileSize, const core::dimension2d<u32>& tileCount, 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