diff --git a/examples/22.MaterialViewer/main.cpp b/examples/22.MaterialViewer/main.cpp index 1f8a27d2..12c7a05f 100755 --- a/examples/22.MaterialViewer/main.cpp +++ b/examples/22.MaterialViewer/main.cpp @@ -693,7 +693,7 @@ bool CApp::init(int argc, char *argv[]) subMenuFile->addItem(L"Quit", GUI_ID_QUIT); // a static camera - Camera = smgr->addCameraSceneNode (0, core::vector3df(0, 40, -40), + Camera = smgr->addCameraSceneNode (0, core::vector3df(0, 30, -50), core::vector3df(0, 10, 0), -1); @@ -706,11 +706,19 @@ bool CApp::init(int argc, char *argv[]) SceneNode = smgr->addCubeSceneNode (30.0f, 0, -1, core::vector3df(0, 0, 0), core::vector3df(0.f, 45.f, 0.f), - core::vector3df(1.0f, 1.0f, 1.0f)); + core::vector3df(1.0f, 1.0f, 1.0f), + scene::ECMT_1BUF_24VTX_NP); + // avoid wrong colored lines at cube-borders (uv's go from 0-1 currently, which does not work well with interpolation) + for ( int i=0; i < irr::video::MATERIAL_MAX_TEXTURES_USED; ++i) + { + defaultMaterial.TextureLayer[i].TextureWrapU = irr::video::ETC_CLAMP_TO_EDGE; + defaultMaterial.TextureLayer[i].TextureWrapV = irr::video::ETC_CLAMP_TO_EDGE; + } #else SceneNode = smgr->addSphereSceneNode(30.f); #endif SceneNode->getMaterial(0) = defaultMaterial; + // SceneNode->setDebugDataVisible(scene::EDS_NORMALS); // showing normals can sometimes be useful to understand what's going on const s32 controlsTop = 20; MeshMaterialControl = new CMaterialControl(); @@ -730,7 +738,7 @@ bool CApp::init(int argc, char *argv[]) // add one light const f32 lightRadius = 80.f; - NodeLight = smgr->addLightSceneNode(0, core::vector3df(0, 0, -70), + NodeLight = smgr->addLightSceneNode(0, core::vector3df(0, 30, -70), video::SColorf(1.0f, 1.0f, 1.0f), lightRadius); LightControl = new CLightNodeControl(); diff --git a/include/IGeometryCreator.h b/include/IGeometryCreator.h index 7a4bdf87..b9871478 100644 --- a/include/IGeometryCreator.h +++ b/include/IGeometryCreator.h @@ -24,15 +24,31 @@ namespace scene { //! Single buffer with 12 different vertices, normals are average of adjacent planes //! Order for outgoing (front-face) normals of planes would be: NEG_Z, POS_X, POS_Z, NEG_X, POS_Y, NEG_Y + //! This was the only available type before Irrlicht 1.9, so it's still the default in some functions. + //! It has the least vertices, but is pretty much unusable if you have dynamic light ECMT_1BUF_12VTX_NA, //! One buffer per side, each with 4 vertices, normals are perpendicular to sides - //! Note: You probably will have to scale down your texture uv's to avoid white lines at borders - // as this mesh sets them to 0,1 values. We can't do that when creating the mesh as it - // depends on texture resolution which we don't know at that point. - ECMT_6BUF_4VTX_NP + //! You can use this one if you need different texture on each cube side + ECMT_6BUF_4VTX_NP, + + //! Single buffer with 24 different vertices, normals are perpendicular to sides + ECMT_1BUF_24VTX_NP, + + //! not used, counts the number of enumerated types + ECMT_COUNT }; + //! Names for ECUBE_MESH_TYPE + const c8* const CubeMeshTypeNames[ECMT_COUNT+1] = + { + "1BUF_12VTX_NA", + "ECMT_6BUF_4VTX_NP", + "1BUF_24VTX_NP", + 0 + }; + + //! Helper class for creating geometry on the fly. /** You can get an instance of this class through ISceneManager::getGeometryCreator() */ class IGeometryCreator : public IReferenceCounted @@ -44,6 +60,9 @@ public: \param size Dimensions of the cube. \param type One of ECUBE_MESH_TYPE. So you can chose between cubes with single material or independent materials per side. \return Generated mesh. + Note: UV's go always from 0 to 1. Which can produce wrong colors at edges with texture filtering. + Fixing UV's depends on texture-size (have to be moved half a pixel towards the inside, so 0.5f/texure_size as then the pixel center is exactly on the border) + Easier solution is usually to set TextureWrapU and TextureWrapV to ETC_CLAMP_TO_EDGE in the Material. */ virtual IMesh* createCubeMesh(const core::vector3df& size=core::vector3df(5.f,5.f,5.f), ECUBE_MESH_TYPE type = ECMT_1BUF_12VTX_NA) const =0; diff --git a/include/ISceneManager.h b/include/ISceneManager.h index 3df4678c..8c4cd32d 100644 --- a/include/ISceneManager.h +++ b/include/ISceneManager.h @@ -453,13 +453,15 @@ namespace scene where the scene node will be placed. \param rotation: Initial rotation of the scene node. \param scale: Initial scale of the scene node. + \param type: Type of cube-mesh to create. Check ECUBE_MESH_TYPE documentation for more info \return Pointer to the created test scene node. This pointer should not be dropped. See IReferenceCounted::drop() for more information. */ virtual IMeshSceneNode* addCubeSceneNode(f32 size=10.0f, ISceneNode* parent=0, s32 id=-1, const core::vector3df& position = core::vector3df(0,0,0), const core::vector3df& rotation = core::vector3df(0,0,0), - const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f)) = 0; + const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f), + ECUBE_MESH_TYPE type=ECMT_1BUF_12VTX_NA) = 0; //! Adds a sphere scene node of the given radius and detail /** \param radius: Radius of the sphere. diff --git a/source/Irrlicht/CCubeSceneNode.cpp b/source/Irrlicht/CCubeSceneNode.cpp index 0400c116..1e882ea6 100644 --- a/source/Irrlicht/CCubeSceneNode.cpp +++ b/source/Irrlicht/CCubeSceneNode.cpp @@ -37,9 +37,10 @@ namespace scene //! constructor CCubeSceneNode::CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr, s32 id, const core::vector3df& position, - const core::vector3df& rotation, const core::vector3df& scale) + const core::vector3df& rotation, const core::vector3df& scale, + ECUBE_MESH_TYPE type) : IMeshSceneNode(parent, mgr, id, position, rotation, scale), - Mesh(0), Shadow(0), Size(size) + Mesh(0), Shadow(0), Size(size), MeshType(type) { #ifdef _DEBUG setDebugName("CCubeSceneNode"); @@ -62,7 +63,7 @@ void CCubeSceneNode::setSize() { if (Mesh) Mesh->drop(); - Mesh = SceneManager->getGeometryCreator()->createCubeMesh(core::vector3df(Size)); + Mesh = SceneManager->getGeometryCreator()->createCubeMesh(core::vector3df(Size), MeshType); } @@ -135,7 +136,7 @@ const core::aabbox3d& CCubeSceneNode::getBoundingBox() const //! Removes a child from this scene node. //! Implemented here, to be able to remove the shadow properly, if there is one, -//! or to remove attached childs. +//! or to remove attached child. bool CCubeSceneNode::removeChild(ISceneNode* child) { if (child && Shadow == child) @@ -199,17 +200,20 @@ void CCubeSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeRea ISceneNode::serializeAttributes(out, options); out->addFloat("Size", Size); + out->addEnum("MeshType", (irr::s32)MeshType, CubeMeshTypeNames); } //! Reads attributes of the scene node. void CCubeSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) { - f32 newSize = in->getAttributeAsFloat("Size"); + f32 newSize = in->getAttributeAsFloat("Size", Size); + ECUBE_MESH_TYPE newMeshType = (ECUBE_MESH_TYPE)in->getAttributeAsEnumeration("MeshType", CubeMeshTypeNames, (irr::s32)MeshType); newSize = core::max_(newSize, 0.0001f); - if (newSize != Size) + if (newSize != Size || newMeshType != MeshType) { Size = newSize; + MeshType = newMeshType; setSize(); } @@ -226,7 +230,7 @@ ISceneNode* CCubeSceneNode::clone(ISceneNode* newParent, ISceneManager* newManag newManager = SceneManager; CCubeSceneNode* nb = new CCubeSceneNode(Size, newParent, - newManager, ID, RelativeTranslation); + newManager, ID, RelativeTranslation, RelativeRotation, RelativeScale, MeshType); nb->cloneMembers(this, newManager); nb->getMaterial(0) = getMaterial(0); diff --git a/source/Irrlicht/CCubeSceneNode.h b/source/Irrlicht/CCubeSceneNode.h index d1719e23..fc276c2e 100644 --- a/source/Irrlicht/CCubeSceneNode.h +++ b/source/Irrlicht/CCubeSceneNode.h @@ -7,6 +7,7 @@ #include "IMeshSceneNode.h" #include "SMesh.h" +#include "IGeometryCreator.h" namespace irr { @@ -20,7 +21,8 @@ namespace scene CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr, s32 id, const core::vector3df& position = core::vector3df(0,0,0), const core::vector3df& rotation = core::vector3df(0,0,0), - const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f)); + const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f), + ECUBE_MESH_TYPE type=ECMT_1BUF_12VTX_NA); virtual ~CCubeSceneNode(); @@ -84,6 +86,7 @@ namespace scene IMesh* Mesh; IShadowVolumeSceneNode* Shadow; f32 Size; + ECUBE_MESH_TYPE MeshType; }; } // end namespace scene diff --git a/source/Irrlicht/CGeometryCreator.cpp b/source/Irrlicht/CGeometryCreator.cpp index 235163d1..989d24e4 100644 --- a/source/Irrlicht/CGeometryCreator.cpp +++ b/source/Irrlicht/CGeometryCreator.cpp @@ -140,6 +140,69 @@ IMesh* CGeometryCreator::createCubeMesh(const core::vector3df& size, ECUBE_MESH_ buffer->drop(); } } + else if ( type == ECMT_1BUF_24VTX_NP ) + { + SMeshBuffer* buffer = new SMeshBuffer(); + + // Create indices (pos, neg describes normal direction of front-face) + const u16 u[36] = { 0,2,1, 0,3,2, // NEG_Z + 4,7,6, 4,5,7, // POS_X + 8,10,11, 8,9,10, // POS_Z + 15,13,12, 15,14,13, // NEG_X + 19,17,16, 19,18,17, // POS_Y + 20,23,22, 20,22,21}; // NEG_Y + + buffer->Indices.set_used(36); + + for (u32 i=0; i<36; ++i) + buffer->Indices[i] = u[i]; + + // Create vertices + buffer->Vertices.reallocate(24); + + buffer->Vertices.push_back(video::S3DVertex(0,0,0, 0, 0,-1, clr, 0, 1)); // 0 + buffer->Vertices.push_back(video::S3DVertex(1,0,0, 0, 0,-1, clr, 1, 1)); // 1 + buffer->Vertices.push_back(video::S3DVertex(1,1,0, 0, 0,-1, clr, 1, 0)); // 2 + buffer->Vertices.push_back(video::S3DVertex(0,1,0, 0, 0,-1, clr, 0, 0)); // 3 + + buffer->Vertices.push_back(video::S3DVertex(1,0,0, 1, 0, 0, clr, 1, 1)); // 4 (1) + buffer->Vertices.push_back(video::S3DVertex(1,1,0, 1, 0, 0, clr, 1, 0)); // 5 (2) + buffer->Vertices.push_back(video::S3DVertex(1,0,1, 1, 0, 0, clr, 0, 1)); // 6 (4) + buffer->Vertices.push_back(video::S3DVertex(1,1,1, 1, 0, 0, clr, 0, 0)); // 7 (5) + + buffer->Vertices.push_back(video::S3DVertex(1,0,1, 0, 0, 1, clr, 0, 1)); // 8 (4) + buffer->Vertices.push_back(video::S3DVertex(1,1,1, 0, 0, 1, clr, 0, 0)); // 9 (5) + buffer->Vertices.push_back(video::S3DVertex(0,1,1, 0, 0, 1, clr, 1, 0)); // 10 (6) + buffer->Vertices.push_back(video::S3DVertex(0,0,1, 0, 0, 1, clr, 1, 1)); // 11 (7) + + buffer->Vertices.push_back(video::S3DVertex(0,0,0, -1, 0, 0, clr, 0, 1)); // 12 (0) + buffer->Vertices.push_back(video::S3DVertex(0,1,0, -1, 0, 0, clr, 0, 0)); // 13 (3) + buffer->Vertices.push_back(video::S3DVertex(0,1,1, -1, 0, 0, clr, 1, 0)); // 14 (6) + buffer->Vertices.push_back(video::S3DVertex(0,0,1, -1, 0, 0, clr, 1, 1)); // 15 (7) + + buffer->Vertices.push_back(video::S3DVertex(1,1,0, 0, 1, 0, clr, 1, 0)); // 16 (2) + buffer->Vertices.push_back(video::S3DVertex(1,1,1, 0, 1, 0, clr, 0, 0)); // 17 (5) + buffer->Vertices.push_back(video::S3DVertex(0,1,1, 0, 1, 0, clr, 0, 1)); // 18 (8) + buffer->Vertices.push_back(video::S3DVertex(0,1,0, 0, 1, 0, clr, 1, 1)); // 19 (9) + + buffer->Vertices.push_back(video::S3DVertex(0,0,0, 0,-1, 0, clr, 0, 1)); // 20 (0) + buffer->Vertices.push_back(video::S3DVertex(0,0,1, 0,-1, 0, clr, 1, 1)); // 21 (7) + buffer->Vertices.push_back(video::S3DVertex(1,0,1, 0,-1, 0, clr, 1, 0)); // 22 (10) + buffer->Vertices.push_back(video::S3DVertex(1,0,0, 0,-1, 0, clr, 0, 0)); // 23 (11) + + // Recalculate bounding box and set cube size + buffer->BoundingBox.reset(0,0,0); + + for (u32 i=0; i<24; ++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); + } + + mesh->addMeshBuffer(buffer); + buffer->drop(); + } mesh->recalculateBoundingBox(); return mesh; diff --git a/source/Irrlicht/CSceneManager.cpp b/source/Irrlicht/CSceneManager.cpp index 8f727c11..b860182d 100644 --- a/source/Irrlicht/CSceneManager.cpp +++ b/source/Irrlicht/CSceneManager.cpp @@ -609,13 +609,14 @@ IVolumeLightSceneNode* CSceneManager::addVolumeLightSceneNode( //! the returned pointer must not be dropped. IMeshSceneNode* CSceneManager::addCubeSceneNode(f32 size, ISceneNode* parent, s32 id, const core::vector3df& position, - const core::vector3df& rotation, const core::vector3df& scale) + const core::vector3df& rotation, const core::vector3df& scale, + ECUBE_MESH_TYPE type) { #ifdef _IRR_COMPILE_WITH_CUBE_SCENENODE_ if (!parent) parent = this; - IMeshSceneNode* node = new CCubeSceneNode(size, parent, this, id, position, rotation, scale); + IMeshSceneNode* node = new CCubeSceneNode(size, parent, this, id, position, rotation, scale, type); node->drop(); return node; diff --git a/source/Irrlicht/CSceneManager.h b/source/Irrlicht/CSceneManager.h index ab9c975e..5051ecce 100644 --- a/source/Irrlicht/CSceneManager.h +++ b/source/Irrlicht/CSceneManager.h @@ -73,7 +73,8 @@ namespace scene virtual IMeshSceneNode* addCubeSceneNode(f32 size=10.0f, ISceneNode* parent=0, s32 id=-1, const core::vector3df& position = core::vector3df(0,0,0), const core::vector3df& rotation = core::vector3df(0,0,0), - const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f)) IRR_OVERRIDE; + const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f), + ECUBE_MESH_TYPE type=ECMT_1BUF_12VTX_NA) IRR_OVERRIDE; //! Adds a sphere scene node to the scene. virtual IMeshSceneNode* addSphereSceneNode(f32 radius=5.0f, s32 polyCount=16, ISceneNode* parent=0, s32 id=-1,