]> git.lizzy.rs Git - minetest.git/blobdiff - src/mesh.cpp
Vary ore sheet y position by noise
[minetest.git] / src / mesh.cpp
index 1d347a09f7678cc153c786b92fdcebca7096aac6..c2e6bcaa4dd6256194f2f193274b80fe92e6afd1 100644 (file)
@@ -1,25 +1,29 @@
 /*
-Minetest-c55
-Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "mesh.h"
+#include "log.h"
+#include <cassert>
+#include <iostream>
 #include <IAnimatedMesh.h>
 #include <SAnimatedMesh.h>
+#include <ICameraSceneNode.h>
 
 // In Irrlicht 1.8 the signature of ITexture::lock was changed from
 // (bool, u32) to (E_TEXTURE_LOCK_MODE, u32).
@@ -73,9 +77,15 @@ scene::IAnimatedMesh* createCubeMesh(v3f scale)
        {
                scene::IMeshBuffer *buf = new scene::SMeshBuffer();
                buf->append(vertices + 4 * i, 4, indices, 6);
+               // Set default material
+               buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
+               buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+               buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+               // Add mesh buffer to mesh
                mesh->addMeshBuffer(buf);
                buf->drop();
        }
+
        scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh);
        mesh->drop();
        scaleMesh(anim_mesh, scale);  // also recalculates bounding box
@@ -280,6 +290,13 @@ scene::IAnimatedMesh* createExtrudedMesh(video::ITexture *texture,
                }
                img1->drop();
        }
+
+       // Set default material
+       mesh->getMeshBuffer(0)->getMaterial().setTexture(0, texture);
+       mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_LIGHTING, false);
+       mesh->getMeshBuffer(0)->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+       mesh->getMeshBuffer(0)->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+
        scaleMesh(mesh, scale);  // also recalculates bounding box
        return mesh;
 }
@@ -313,6 +330,35 @@ void scaleMesh(scene::IMesh *mesh, v3f scale)
        mesh->setBoundingBox(bbox);
 }
 
+void translateMesh(scene::IMesh *mesh, v3f vec)
+{
+       if(mesh == NULL)
+               return;
+
+       core::aabbox3d<f32> bbox;
+       bbox.reset(0,0,0);
+
+       u16 mc = mesh->getMeshBufferCount();
+       for(u16 j=0; j<mc; j++)
+       {
+               scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+               video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+               u16 vc = buf->getVertexCount();
+               for(u16 i=0; i<vc; i++)
+               {
+                       vertices[i].Pos += vec;
+               }
+               buf->recalculateBoundingBox();
+
+               // calculate total bounding box
+               if(j == 0)
+                       bbox = buf->getBoundingBox();
+               else
+                       bbox.addInternalBox(buf->getBoundingBox());
+       }
+       mesh->setBoundingBox(bbox);
+}
+
 void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
 {
        if(mesh == NULL)
@@ -360,3 +406,79 @@ void setMeshColorByNormalXYZ(scene::IMesh *mesh,
                }
        }
 }
+
+video::ITexture *generateTextureFromMesh(scene::IMesh *mesh,
+               IrrlichtDevice *device,
+               core::dimension2d<u32> dim,
+               std::string texture_name,
+               v3f camera_position,
+               v3f camera_lookat,
+               core::CMatrix4<f32> camera_projection_matrix,
+               video::SColorf ambient_light,
+               v3f light_position,
+               video::SColorf light_color,
+               f32 light_radius)
+{
+       video::IVideoDriver *driver = device->getVideoDriver();
+       if(driver->queryFeature(video::EVDF_RENDER_TO_TARGET) == false)
+       {
+               static bool warned = false;
+               if(!warned)
+               {
+                       errorstream<<"generateTextureFromMesh(): EVDF_RENDER_TO_TARGET"
+                                       " not supported."<<std::endl;
+                       warned = true;
+               }
+               return NULL;
+       }
+
+       // Create render target texture
+       video::ITexture *rtt = driver->addRenderTargetTexture(
+                       dim, texture_name.c_str(), video::ECF_A8R8G8B8);
+       if(rtt == NULL)
+       {
+               errorstream<<"generateTextureFromMesh(): addRenderTargetTexture"
+                               " returned NULL."<<std::endl;
+               return NULL;
+       }
+
+       // Set render target
+       driver->setRenderTarget(rtt, false, true, video::SColor(0,0,0,0));
+
+       // Get a scene manager
+       scene::ISceneManager *smgr_main = device->getSceneManager();
+       assert(smgr_main);
+       scene::ISceneManager *smgr = smgr_main->createNewSceneManager();
+       assert(smgr);
+
+       scene::IMeshSceneNode* meshnode = smgr->addMeshSceneNode(mesh, NULL, -1, v3f(0,0,0), v3f(0,0,0), v3f(1,1,1), true);
+       meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
+       meshnode->setMaterialFlag(video::EMF_ANTI_ALIASING, true);
+       meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, true);
+
+       scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(0,
+                       camera_position, camera_lookat);
+       // second parameter of setProjectionMatrix (isOrthogonal) is ignored
+       camera->setProjectionMatrix(camera_projection_matrix, false);
+
+       smgr->setAmbientLight(ambient_light);
+       smgr->addLightSceneNode(0, light_position, light_color, light_radius);
+
+       // Render scene
+       driver->beginScene(true, true, video::SColor(0,0,0,0));
+       smgr->drawAll();
+       driver->endScene();
+
+       // NOTE: The scene nodes should not be dropped, otherwise
+       //       smgr->drop() segfaults
+       /*cube->drop();
+       camera->drop();
+       light->drop();*/
+       // Drop scene manager
+       smgr->drop();
+
+       // Unset render target
+       driver->setRenderTarget(0, false, true, 0);
+
+       return rtt;
+}