X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmesh.cpp;h=dab1575f38b8a42d65cbc0e42ca1fa92bd596405;hb=79e2647556773d46cc0299e73ca23c41a153ff2a;hp=e021e4c923718d8e16ef4a075e408663e8bdf11b;hpb=016448331061f87a63b8f9ef33671d81e8922ad1;p=minetest.git diff --git a/src/mesh.cpp b/src/mesh.cpp index e021e4c92..dab1575f3 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -33,6 +33,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY #endif +static void applyFacesShading(video::SColor& color, float factor) +{ + color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255)); + color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255)); + color.setBlue(core::clamp(core::round32(color.getBlue()*factor), 0, 255)); +} + scene::IAnimatedMesh* createCubeMesh(v3f scale) { video::SColor c(255,255,255,255); @@ -94,26 +101,25 @@ scene::IAnimatedMesh* createCubeMesh(v3f scale) void scaleMesh(scene::IMesh *mesh, v3f scale) { - if(mesh == NULL) + if (mesh == NULL) return; core::aabbox3d bbox; - bbox.reset(0,0,0); + bbox.reset(0, 0, 0); - u16 mc = mesh->getMeshBufferCount(); - for(u16 j=0; jgetMeshBufferCount(); + for (u32 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; igetVertexType()); + u32 vertex_count = buf->getVertexCount(); + u8 *vertices = (u8 *)buf->getVertices(); + for (u32 i = 0; i < vertex_count; i++) + ((video::S3DVertex *)(vertices + i * stride))->Pos *= scale; + buf->recalculateBoundingBox(); // calculate total bounding box - if(j == 0) + if (j == 0) bbox = buf->getBoundingBox(); else bbox.addInternalBox(buf->getBoundingBox()); @@ -123,26 +129,25 @@ void scaleMesh(scene::IMesh *mesh, v3f scale) void translateMesh(scene::IMesh *mesh, v3f vec) { - if(mesh == NULL) + if (mesh == NULL) return; core::aabbox3d bbox; - bbox.reset(0,0,0); + bbox.reset(0, 0, 0); - u16 mc = mesh->getMeshBufferCount(); - for(u16 j=0; jgetMeshBufferCount(); + for (u32 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; igetVertexType()); + u32 vertex_count = buf->getVertexCount(); + u8 *vertices = (u8 *)buf->getVertices(); + for (u32 i = 0; i < vertex_count; i++) + ((video::S3DVertex *)(vertices + i * stride))->Pos += vec; + buf->recalculateBoundingBox(); // calculate total bounding box - if(j == 0) + if (j == 0) bbox = buf->getBoundingBox(); else bbox.addInternalBox(buf->getBoundingBox()); @@ -150,20 +155,48 @@ void translateMesh(scene::IMesh *mesh, v3f vec) mesh->setBoundingBox(bbox); } + void setMeshColor(scene::IMesh *mesh, const video::SColor &color) { - if(mesh == NULL) + if (mesh == NULL) return; - - u16 mc = mesh->getMeshBufferCount(); - for(u16 j=0; jgetMeshBufferCount(); + for (u32 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; igetVertexType()); + u32 vertex_count = buf->getVertexCount(); + u8 *vertices = (u8 *)buf->getVertices(); + for (u32 i = 0; i < vertex_count; i++) + ((video::S3DVertex *)(vertices + i * stride))->Color = color; + } +} + +void shadeMeshFaces(scene::IMesh *mesh) +{ + if (mesh == NULL) + return; + + u32 mc = mesh->getMeshBufferCount(); + for (u32 j = 0; j < mc; j++) { + scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); + const u32 stride = getVertexPitchFromType(buf->getVertexType()); + u32 vertex_count = buf->getVertexCount(); + u8 *vertices = (u8 *)buf->getVertices(); + for (u32 i = 0; i < vertex_count; i++) { + video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride); + video::SColor &vc = vertex->Color; + if (vertex->Normal.Y < -0.5) { + applyFacesShading (vc, 0.447213); + } else if (vertex->Normal.Z > 0.5) { + applyFacesShading (vc, 0.670820); + } else if (vertex->Normal.Z < -0.5) { + applyFacesShading (vc, 0.670820); + } else if (vertex->Normal.X > 0.5) { + applyFacesShading (vc, 0.836660); + } else if (vertex->Normal.X < -0.5) { + applyFacesShading (vc, 0.836660); + } } } }