From 43fcfbfe05578d7471d40c8c087fd04e24b264b5 Mon Sep 17 00:00:00 2001 From: RealBadAngel Date: Sun, 14 Jun 2015 21:13:36 +0200 Subject: Improved parallax mapping. Generate heightmaps on the fly. --- src/clientmap.cpp | 2 +- src/defaultsettings.cpp | 6 ++- src/mapblock_mesh.cpp | 126 +++++++++++++++++++++++------------------------- src/mapblock_mesh.h | 6 ++- src/mesh.cpp | 67 ++++++++++++------------- src/shader.cpp | 65 ++++++++++++++----------- 6 files changed, 138 insertions(+), 134 deletions(-) (limited to 'src') diff --git a/src/clientmap.cpp b/src/clientmap.cpp index 1a52ac4ab..288a12135 100644 --- a/src/clientmap.cpp +++ b/src/clientmap.cpp @@ -539,7 +539,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) MapBlockMesh *mapBlockMesh = block->mesh; assert(mapBlockMesh); - scene::SMesh *mesh = mapBlockMesh->getMesh(); + scene::IMesh *mesh = mapBlockMesh->getMesh(); assert(mesh); u32 c = mesh->getMeshBufferCount(); diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 0cf1dd770..eab81009f 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -160,8 +160,10 @@ void set_default_settings(Settings *settings) settings->setDefault("generate_normalmaps", "false"); settings->setDefault("normalmaps_strength", "0.6"); settings->setDefault("normalmaps_smooth", "1"); - settings->setDefault("parallax_occlusion_scale", "0.06"); - settings->setDefault("parallax_occlusion_bias", "0.03"); + settings->setDefault("parallax_occlusion_mode", "1"); + settings->setDefault("parallax_occlusion_iterations", "4"); + settings->setDefault("parallax_occlusion_scale", "0.08"); + settings->setDefault("parallax_occlusion_bias", "0.04"); settings->setDefault("enable_waving_water", "false"); settings->setDefault("water_wave_height", "1.0"); settings->setDefault("water_wave_length", "20.0"); diff --git a/src/mapblock_mesh.cpp b/src/mapblock_mesh.cpp index ab4b63f86..9c755b102 100644 --- a/src/mapblock_mesh.cpp +++ b/src/mapblock_mesh.cpp @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "shader.h" #include "settings.h" #include "util/directiontables.h" +#include static void applyFacesShading(video::SColor& color, float factor) { @@ -1028,6 +1029,8 @@ static void updateAllFastFaceRows(MeshMakeData *data, MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): m_mesh(new scene::SMesh()), m_gamedef(data->m_gamedef), + m_tsrc(m_gamedef->getTextureSource()), + m_shdrsrc(m_gamedef->getShaderSource()), m_animation_force_timer(0), // force initial animation m_last_crack(-1), m_crack_materials(), @@ -1110,8 +1113,6 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): /* Convert MeshCollector to SMesh */ - ITextureSource *tsrc = m_gamedef->tsrc(); - IShaderSource *shdrsrc = m_gamedef->getShaderSource(); for(u32 i = 0; i < collector.prebuffers.size(); i++) { @@ -1123,13 +1124,13 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): { // Find the texture name plus ^[crack:N: std::ostringstream os(std::ios::binary); - os<getTextureName(p.tile.texture_id)<<"^[crack"; + os<getTextureName(p.tile.texture_id)<<"^[crack"; if(p.tile.material_flags & MATERIAL_FLAG_CRACK_OVERLAY) os<<"o"; // use ^[cracko os<<":"<<(u32)p.tile.animation_frame_count<<":"; m_crack_materials.insert(std::make_pair(i, os.str())); // Replace tile texture with the cracked one - p.tile.texture = tsrc->getTextureForMesh( + p.tile.texture = m_tsrc->getTextureForMesh( os.str()+"0", &p.tile.texture_id); } @@ -1158,20 +1159,21 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): for(u32 j = 0; j < p.vertices.size(); j++) { + video::S3DVertexTangents *vertex = &p.vertices[j]; // Note applyFacesShading second parameter is precalculated sqrt // value for speed improvement // Skip it for lightsources and top faces. - video::SColor &vc = p.vertices[j].Color; + video::SColor &vc = vertex->Color; if (!vc.getBlue()) { - if (p.vertices[j].Normal.Y < -0.5) { + if (vertex->Normal.Y < -0.5) { applyFacesShading (vc, 0.447213); - } else if (p.vertices[j].Normal.X > 0.5) { + } else if (vertex->Normal.X > 0.5) { applyFacesShading (vc, 0.670820); - } else if (p.vertices[j].Normal.X < -0.5) { + } else if (vertex->Normal.X < -0.5) { applyFacesShading (vc, 0.670820); - } else if (p.vertices[j].Normal.Z > 0.5) { + } else if (vertex->Normal.Z > 0.5) { applyFacesShading (vc, 0.836660); - } else if (p.vertices[j].Normal.Z < -0.5) { + } else if (vertex->Normal.Z < -0.5) { applyFacesShading (vc, 0.836660); } } @@ -1199,33 +1201,30 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR; } else { if (m_enable_shaders) { - material.MaterialType = shdrsrc->getShaderInfo(p.tile.shader_id).material; + material.MaterialType = m_shdrsrc->getShaderInfo(p.tile.shader_id).material; p.tile.applyMaterialOptionsWithShaders(material); if (p.tile.normal_texture) { material.setTexture(1, p.tile.normal_texture); - material.setTexture(2, tsrc->getTextureForMesh("enable_img.png")); + material.setTexture(2, m_tsrc->getTextureForMesh("enable_img.png")); } else { - material.setTexture(2, tsrc->getTextureForMesh("disable_img.png")); + material.setTexture(2, m_tsrc->getTextureForMesh("disable_img.png")); } } else { p.tile.applyMaterialOptions(material); } } - - // Create meshbuffer - // This is a "Standard MeshBuffer", - // it's a typedeffed CMeshBuffer - scene::SMeshBuffer *buf = new scene::SMeshBuffer(); - // Set material - buf->Material = material; - // Add to mesh - m_mesh->addMeshBuffer(buf); - // Mesh grabbed it - buf->drop(); - buf->append(&p.vertices[0], p.vertices.size(), - &p.indices[0], p.indices.size()); - } - + + // Create meshbuffer + scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents(); + // Set material + buf->Material = material; + // Add to mesh + m_mesh->addMeshBuffer(buf); + // Mesh grabbed it + buf->drop(); + buf->append(&p.vertices[0], p.vertices.size(), + &p.indices[0], p.indices.size()); +} m_camera_offset = camera_offset; /* @@ -1234,6 +1233,11 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): translateMesh(m_mesh, intToFloat(data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS)); + if (m_enable_shaders) { + scene::IMeshManipulator* meshmanip = m_gamedef->getSceneManager()->getMeshManipulator(); + meshmanip->recalculateTangents(m_mesh, true, false, false); + } + if(m_mesh) { #if 0 @@ -1272,7 +1276,6 @@ MapBlockMesh::~MapBlockMesh() bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio) { - if(!m_has_animation) { m_animation_force_timer = 100000; @@ -1292,12 +1295,11 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat std::string basename = i->second; // Create new texture name from original - ITextureSource *tsrc = m_gamedef->getTextureSource(); std::ostringstream os; os<getTextureForMesh(os.str(), &new_texture_id); + m_tsrc->getTextureForMesh(os.str(), &new_texture_id); buf->getMaterial().setTexture(0, new_texture); // If the current material is also animated, @@ -1333,16 +1335,15 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat m_animation_frames[i->first] = frame; scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); - ITextureSource *tsrc = m_gamedef->getTextureSource(); FrameSpec animation_frame = tile.frames[frame]; buf->getMaterial().setTexture(0, animation_frame.texture); if (m_enable_shaders) { if (animation_frame.normal_texture) { buf->getMaterial().setTexture(1, animation_frame.normal_texture); - buf->getMaterial().setTexture(2, tsrc->getTextureForMesh("enable_img.png")); + buf->getMaterial().setTexture(2, m_tsrc->getTextureForMesh("enable_img.png")); } else { - buf->getMaterial().setTexture(2, tsrc->getTextureForMesh("disable_img.png")); + buf->getMaterial().setTexture(2, m_tsrc->getTextureForMesh("disable_img.png")); } } } @@ -1355,16 +1356,14 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat i != m_daynight_diffs.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); - video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); + video::S3DVertexTangents *vertices = (video::S3DVertexTangents *)buf->getVertices(); for(std::map >::iterator j = i->second.begin(); j != i->second.end(); j++) { - u32 vertexIndex = j->first; u8 day = j->second.first; u8 night = j->second.second; - finalColorBlend(vertices[vertexIndex].Color, - day, night, daynight_ratio); + finalColorBlend(vertices[j->first].Color, day, night, daynight_ratio); } } m_last_daynight_ratio = daynight_ratio; @@ -1388,7 +1387,7 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat i != m_highlighted_materials.end(); i++) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(*i); - video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices(); + video::S3DVertexTangents *vertices = (video::S3DVertexTangents*)buf->getVertices(); for (u32 j = 0; j < buf->getVertexCount() ;j++) vertices[j].Color = hc; } @@ -1413,42 +1412,40 @@ void MeshCollector::append(const TileSpec &tile, const video::S3DVertex *vertices, u32 numVertices, const u16 *indices, u32 numIndices) { - if(numIndices > 65535) - { + if (numIndices > 65535) { dstream<<"FIXME: MeshCollector::append() called with numIndices="< 65535) + if (pp.indices.size() + numIndices > 65535) continue; p = &pp; break; } - if(p == NULL) - { + if (p == NULL) { PreMeshBuffer pp; pp.tile = tile; prebuffers.push_back(pp); - p = &prebuffers[prebuffers.size()-1]; + p = &prebuffers[prebuffers.size() - 1]; } u32 vertex_count = p->vertices.size(); - for(u32 i=0; iindices.push_back(j); } - for(u32 i=0; ivertices.push_back(vertices[i]); + + for (u32 i = 0; i < numVertices; i++) { + video::S3DVertexTangents vert(vertices[i].Pos, vertices[i].Normal, + vertices[i].Color, vertices[i].TCoords); + p->vertices.push_back(vert); } } @@ -1461,15 +1458,13 @@ void MeshCollector::append(const TileSpec &tile, const u16 *indices, u32 numIndices, v3f pos, video::SColor c) { - if(numIndices > 65535) - { + if (numIndices > 65535) { dstream<<"FIXME: MeshCollector::append() called with numIndices="<vertices.size(); - for(u32 i=0; iindices.push_back(j); } - for(u32 i=0; ivertices.push_back(vert); } } diff --git a/src/mapblock_mesh.h b/src/mapblock_mesh.h index b334ce469..72c90c3e5 100644 --- a/src/mapblock_mesh.h +++ b/src/mapblock_mesh.h @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include class IGameDef; +class IShaderSource; /* Mesh making stuff @@ -123,6 +124,8 @@ public: private: scene::SMesh *m_mesh; IGameDef *m_gamedef; + ITextureSource *m_tsrc; + IShaderSource *m_shdrsrc; bool m_enable_shaders; bool m_enable_highlighting; @@ -165,13 +168,12 @@ struct PreMeshBuffer { TileSpec tile; std::vector indices; - std::vector vertices; + std::vector vertices; }; struct MeshCollector { std::vector prebuffers; - void append(const TileSpec &material, const video::S3DVertex *vertices, u32 numVertices, const u16 *indices, u32 numIndices); diff --git a/src/mesh.cpp b/src/mesh.cpp index e021e4c92..6c7c6d37b 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -94,26 +94,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 +122,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,21 +148,20 @@ 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; } } diff --git a/src/shader.cpp b/src/shader.cpp index a467c2ea9..a6d18e1be 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -680,42 +680,54 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype, shaders_header += itos(drawtype); shaders_header += "\n"; - if (g_settings->getBool("generate_normalmaps")){ - shaders_header += "#define GENERATE_NORMALMAPS\n"; - shaders_header += "#define NORMALMAPS_STRENGTH "; - shaders_header += ftos(g_settings->getFloat("normalmaps_strength")); - shaders_header += "\n"; - float sample_step; - int smooth = (int)g_settings->getFloat("normalmaps_smooth"); - switch (smooth){ - case 0: - sample_step = 0.0078125; // 1.0 / 128.0 - break; - case 1: - sample_step = 0.00390625; // 1.0 / 256.0 - break; - case 2: - sample_step = 0.001953125; // 1.0 / 512.0 - break; - default: - sample_step = 0.0078125; - break; - } - shaders_header += "#define SAMPLE_STEP "; - shaders_header += ftos(sample_step); - shaders_header += "\n"; + if (g_settings->getBool("generate_normalmaps")) { + shaders_header += "#define GENERATE_NORMALMAPS 1\n"; + } else { + shaders_header += "#define GENERATE_NORMALMAPS 0\n"; + } + shaders_header += "#define NORMALMAPS_STRENGTH "; + shaders_header += ftos(g_settings->getFloat("normalmaps_strength")); + shaders_header += "\n"; + float sample_step; + int smooth = (int)g_settings->getFloat("normalmaps_smooth"); + switch (smooth){ + case 0: + sample_step = 0.0078125; // 1.0 / 128.0 + break; + case 1: + sample_step = 0.00390625; // 1.0 / 256.0 + break; + case 2: + sample_step = 0.001953125; // 1.0 / 512.0 + break; + default: + sample_step = 0.0078125; + break; } + shaders_header += "#define SAMPLE_STEP "; + shaders_header += ftos(sample_step); + shaders_header += "\n"; if (g_settings->getBool("enable_bumpmapping")) shaders_header += "#define ENABLE_BUMPMAPPING\n"; if (g_settings->getBool("enable_parallax_occlusion")){ + int mode = g_settings->getFloat("parallax_occlusion_mode"); + float scale = g_settings->getFloat("parallax_occlusion_scale"); + float bias = g_settings->getFloat("parallax_occlusion_bias"); + int iterations = g_settings->getFloat("parallax_occlusion_iterations"); shaders_header += "#define ENABLE_PARALLAX_OCCLUSION\n"; + shaders_header += "#define PARALLAX_OCCLUSION_MODE "; + shaders_header += itos(mode); + shaders_header += "\n"; shaders_header += "#define PARALLAX_OCCLUSION_SCALE "; - shaders_header += ftos(g_settings->getFloat("parallax_occlusion_scale")); + shaders_header += ftos(scale); shaders_header += "\n"; shaders_header += "#define PARALLAX_OCCLUSION_BIAS "; - shaders_header += ftos(g_settings->getFloat("parallax_occlusion_bias")); + shaders_header += ftos(bias); + shaders_header += "\n"; + shaders_header += "#define PARALLAX_OCCLUSION_ITERATIONS "; + shaders_header += itos(iterations); shaders_header += "\n"; } @@ -755,7 +767,6 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype, vertex_program = shaders_header + vertex_program; if(geometry_program != "") geometry_program = shaders_header + geometry_program; - // Call addHighLevelShaderMaterial() or addShaderMaterial() const c8* vertex_program_ptr = 0; const c8* pixel_program_ptr = 0; -- cgit v1.2.3