From 43fcfbfe05578d7471d40c8c087fd04e24b264b5 Mon Sep 17 00:00:00 2001 From: RealBadAngel Date: Sun, 14 Jun 2015 21:13:36 +0200 Subject: [PATCH] Improved parallax mapping. Generate heightmaps on the fly. --- .../shaders/nodes_shader/opengl_fragment.glsl | 134 +++++++++++++----- .../shaders/nodes_shader/opengl_vertex.glsl | 79 ++++++----- .../water_surface_shader/opengl_fragment.glsl | 24 ++-- .../water_surface_shader/opengl_vertex.glsl | 17 ++- minetest.conf.example | 25 ++-- 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 +++++---- 11 files changed, 310 insertions(+), 241 deletions(-) diff --git a/client/shaders/nodes_shader/opengl_fragment.glsl b/client/shaders/nodes_shader/opengl_fragment.glsl index 6dc96eb48..066e49658 100644 --- a/client/shaders/nodes_shader/opengl_fragment.glsl +++ b/client/shaders/nodes_shader/opengl_fragment.glsl @@ -8,6 +8,7 @@ uniform vec3 eyePosition; varying vec3 vPosition; varying vec3 worldPosition; +varying float generate_heightmaps; varying vec3 eyeVec; varying vec3 tsEyeVec; @@ -19,80 +20,135 @@ bool normalTexturePresent = false; const float e = 2.718281828459; const float BS = 10.0; -float intensity (vec3 color){ +float intensity (vec3 color) +{ return (color.r + color.g + color.b) / 3.0; } -float get_rgb_height (vec2 uv){ +float get_rgb_height (vec2 uv) +{ return intensity(texture2D(baseTexture,uv).rgb); } -vec4 get_normal_map(vec2 uv){ +vec4 get_normal_map(vec2 uv) +{ vec4 bump = texture2D(normalTexture, uv).rgba; - bump.xyz = normalize(bump.xyz * 2.0 -1.0); - bump.y = -bump.y; + bump.xyz = normalize(bump.xyz * 2.0 - 1.0); return bump; } +float find_intersection(vec2 dp, vec2 ds) +{ + const int linear_steps = 10; + const int binary_steps = 5; + const float depth_step = 1.0 / linear_steps; + float size = depth_step; + float depth = 1.0; + float best_depth = 1.0; + for (int i = 0 ; i < linear_steps - 1 ; ++i) { + vec4 t = texture2D(normalTexture, dp + ds * depth); + if (best_depth > 0.05) + if (depth >= t.a) + best_depth = depth; + depth -= size; + } + depth = best_depth - size; + for (int i = 0 ; i < binary_steps ; ++i) { + size *= 0.5; + vec4 t = texture2D(normalTexture, dp + ds * depth); + if (depth >= t.a) { + best_depth = depth; + depth -= 2 * size; + } + depth += size; + } + return best_depth; +} + +float find_intersectionRGB(vec2 dp, vec2 ds) { + const float iterations = 24; + const float depth_step = 1.0 / iterations; + float depth = 1.0; + for (int i = 0 ; i < iterations ; i++) { + float h = get_rgb_height(dp + ds * depth); + if (h >= depth) + break; + depth -= depth_step; + } + return depth; +} + void main (void) { vec3 color; vec4 bump; vec2 uv = gl_TexCoord[0].st; bool use_normalmap = false; - + #ifdef USE_NORMALMAPS - if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0){ + if (texture2D(useNormalmap,vec2(1.0, 1.0)).r > 0.0) { normalTexturePresent = true; } #endif #ifdef ENABLE_PARALLAX_OCCLUSION - if (normalTexturePresent){ - vec3 tsEye = normalize(tsEyeVec); - float height = PARALLAX_OCCLUSION_SCALE * texture2D(normalTexture, uv).a - PARALLAX_OCCLUSION_BIAS; - uv = uv + texture2D(normalTexture, uv).z * height * vec2(tsEye.x,-tsEye.y); + vec3 eyeRay = normalize(tsEyeVec); +#if PARALLAX_OCCLUSION_MODE == 0 + // Parallax occlusion with slope information + if (normalTexturePresent) { + const float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS; + const float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS; + for(int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) { + vec4 normal = texture2D(normalTexture, uv.xy); + float h = normal.a * scale - bias; + uv += h * normal.z * eyeRay.xy; + } +#endif +#if PARALLAX_OCCLUSION_MODE == 1 + // Relief mapping + if (normalTexturePresent) { + vec2 ds = eyeRay.xy * PARALLAX_OCCLUSION_SCALE; + float dist = find_intersection(uv, ds); + uv += dist * ds; +#endif + } else if (generate_heightmaps > 0.0) { + vec2 ds = eyeRay.xy * PARALLAX_OCCLUSION_SCALE; + float dist = find_intersectionRGB(uv, ds); + uv += dist * ds; } #endif #ifdef USE_NORMALMAPS - if (normalTexturePresent){ + if (normalTexturePresent) { bump = get_normal_map(uv); use_normalmap = true; } #endif - -#ifdef GENERATE_NORMALMAPS - if (use_normalmap == false){ - float tl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y+SAMPLE_STEP)); - float t = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP)); - float tr = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y+SAMPLE_STEP)); - float r = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y)); - float br = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y-SAMPLE_STEP)); - float b = get_rgb_height (vec2(uv.x,uv.y-SAMPLE_STEP)); - float bl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP)); - float l = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y)); + + if (GENERATE_NORMALMAPS == 1 && use_normalmap == false) { + float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP)); + float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP)); + float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP)); + float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y)); + float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP)); + float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP)); + float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP)); + float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y)); float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl); float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr); - bump = vec4 (normalize(vec3 (dX, -dY, NORMALMAPS_STRENGTH)),1.0); + bump = vec4(normalize(vec3 (-dX, -dY, NORMALMAPS_STRENGTH)), 1.0); use_normalmap = true; } -#endif -vec4 base = texture2D(baseTexture, uv).rgba; - + vec4 base = texture2D(baseTexture, uv).rgba; + #ifdef ENABLE_BUMPMAPPING - if (use_normalmap){ + if (use_normalmap) { vec3 L = normalize(lightVec); - vec3 E = normalize(eyeVec); - float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5); - float diffuse = dot(E,bump.xyz); - /* Mathematic optimization - * Original: color = 0.05*base.rgb + diffuse*base.rgb + 0.2*specular*base.rgb; - * This optimization save 2 multiplications (orig: 4 multiplications + 3 additions - * end: 2 multiplications + 3 additions) - */ - color = (0.05 + diffuse + 0.2 * specular) * base.rgb; + vec3 E = normalize(-eyeVec); + float specular = pow(clamp(dot(reflect(L, bump.xyz), -E), 0.0, 1.0), 1.0); + float diffuse = dot(E,bump.xyz); + color = (diffuse + 0.1 * specular) * base.rgb; } else { color = base.rgb; } @@ -104,7 +160,7 @@ vec4 base = texture2D(baseTexture, uv).rgba; float alpha = gl_Color.a; vec4 col = vec4(color.rgb, alpha); col *= gl_Color; - if(fogDistance != 0.0){ + if (fogDistance != 0.0) { float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0)); alpha = mix(alpha, 0.0, d); } @@ -112,7 +168,7 @@ vec4 base = texture2D(baseTexture, uv).rgba; #else vec4 col = vec4(color.rgb, base.a); col *= gl_Color; - if(fogDistance != 0.0){ + if (fogDistance != 0.0) { float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0)); col = mix(col, skyBgColor, d); } diff --git a/client/shaders/nodes_shader/opengl_vertex.glsl b/client/shaders/nodes_shader/opengl_vertex.glsl index 07684f616..e5644b8b2 100644 --- a/client/shaders/nodes_shader/opengl_vertex.glsl +++ b/client/shaders/nodes_shader/opengl_vertex.glsl @@ -14,25 +14,36 @@ varying vec3 eyeVec; varying vec3 lightVec; varying vec3 tsEyeVec; varying vec3 tsLightVec; +varying float generate_heightmaps; const float e = 2.718281828459; const float BS = 10.0; -float smoothCurve( float x ) { - return x * x *( 3.0 - 2.0 * x ); +float smoothCurve(float x) +{ + return x * x * (3.0 - 2.0 * x); } -float triangleWave( float x ) { - return abs( fract( x + 0.5 ) * 2.0 - 1.0 ); +float triangleWave(float x) +{ + return abs(fract(x + 0.5) * 2.0 - 1.0); } -float smoothTriangleWave( float x ) { - return smoothCurve( triangleWave( x ) ) * 2.0 - 1.0; +float smoothTriangleWave(float x) +{ + return smoothCurve(triangleWave(x)) * 2.0 - 1.0; } void main(void) { gl_TexCoord[0] = gl_MultiTexCoord0; - -#if (MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) && ENABLE_WAVING_WATER + gl_TexCoord[0].y += 0.008; + +#if ((DRAW_TYPE == NDT_NORMAL || DRAW_TYPE == NDT_LIQUID || DRAW_TYPE == NDT_FLOWINGLIQUID) && GENERATE_NORMALMAPS) + generate_heightmaps = 1.0; +#else + generate_heightmaps = 0.0; +#endif + +#if ((MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) && ENABLE_WAVING_WATER) vec4 pos = gl_Vertex; pos.y -= 2.0; @@ -76,43 +87,33 @@ void main(void) vPosition = gl_Position.xyz; worldPosition = (mWorld * gl_Vertex).xyz; + + // Don't generate heightmaps when too far from the eye + float dist = distance (worldPosition, eyePosition); + if (dist > 100.00) { + generate_heightmaps = 0.0; + } + vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0); vec3 normal, tangent, binormal; normal = normalize(gl_NormalMatrix * gl_Normal); - if (gl_Normal.x > 0.5) { - // 1.0, 0.0, 0.0 - tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, -1.0)); - binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0)); - } else if (gl_Normal.x < -0.5) { - // -1.0, 0.0, 0.0 - tangent = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0)); - binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0)); - } else if (gl_Normal.y > 0.5) { - // 0.0, 1.0, 0.0 - tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0)); - binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0)); - } else if (gl_Normal.y < -0.5) { - // 0.0, -1.0, 0.0 - tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0)); - binormal = normalize(gl_NormalMatrix * vec3( 0.0, 0.0, 1.0)); - } else if (gl_Normal.z > 0.5) { - // 0.0, 0.0, 1.0 - tangent = normalize(gl_NormalMatrix * vec3( 1.0, 0.0, 0.0)); - binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0)); - } else if (gl_Normal.z < -0.5) { - // 0.0, 0.0, -1.0 - tangent = normalize(gl_NormalMatrix * vec3(-1.0, 0.0, 0.0)); - binormal = normalize(gl_NormalMatrix * vec3( 0.0, -1.0, 0.0)); - } - mat3 tbnMatrix = mat3( tangent.x, binormal.x, normal.x, - tangent.y, binormal.y, normal.y, - tangent.z, binormal.z, normal.z); + tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz); + binormal = normalize(gl_NormalMatrix * -gl_MultiTexCoord2.xyz); + + vec3 v; lightVec = sunPosition - worldPosition; - tsLightVec = lightVec * tbnMatrix; - eyeVec = (gl_ModelViewMatrix * gl_Vertex).xyz; - tsEyeVec = eyeVec * tbnMatrix; + v.x = dot(lightVec, tangent); + v.y = dot(lightVec, binormal); + v.z = dot(lightVec, normal); + tsLightVec = v; + + eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz; + v.x = dot(eyeVec, tangent); + v.y = dot(eyeVec, binormal); + v.z = dot(eyeVec, normal); + tsEyeVec = v; vec4 color; float day = gl_Color.r; diff --git a/client/shaders/water_surface_shader/opengl_fragment.glsl b/client/shaders/water_surface_shader/opengl_fragment.glsl index 6dc96eb48..386f77486 100644 --- a/client/shaders/water_surface_shader/opengl_fragment.glsl +++ b/client/shaders/water_surface_shader/opengl_fragment.glsl @@ -14,7 +14,7 @@ varying vec3 tsEyeVec; varying vec3 lightVec; varying vec3 tsLightVec; -bool normalTexturePresent = false; +bool normalTexturePresent = false; const float e = 2.718281828459; const float BS = 10.0; @@ -40,15 +40,15 @@ void main (void) vec4 bump; vec2 uv = gl_TexCoord[0].st; bool use_normalmap = false; - + #ifdef USE_NORMALMAPS - if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0){ + if (texture2D(useNormalmap,vec2(1.0,1.0)).r > 0.0) { normalTexturePresent = true; } #endif #ifdef ENABLE_PARALLAX_OCCLUSION - if (normalTexturePresent){ + if (normalTexturePresent) { vec3 tsEye = normalize(tsEyeVec); float height = PARALLAX_OCCLUSION_SCALE * texture2D(normalTexture, uv).a - PARALLAX_OCCLUSION_BIAS; uv = uv + texture2D(normalTexture, uv).z * height * vec2(tsEye.x,-tsEye.y); @@ -56,14 +56,13 @@ void main (void) #endif #ifdef USE_NORMALMAPS - if (normalTexturePresent){ + if (normalTexturePresent) { bump = get_normal_map(uv); use_normalmap = true; } #endif - -#ifdef GENERATE_NORMALMAPS - if (use_normalmap == false){ + + if (GENERATE_NORMALMAPS == 1 && use_normalmap == false) { float tl = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y+SAMPLE_STEP)); float t = get_rgb_height (vec2(uv.x-SAMPLE_STEP,uv.y-SAMPLE_STEP)); float tr = get_rgb_height (vec2(uv.x+SAMPLE_STEP,uv.y+SAMPLE_STEP)); @@ -77,16 +76,15 @@ void main (void) bump = vec4 (normalize(vec3 (dX, -dY, NORMALMAPS_STRENGTH)),1.0); use_normalmap = true; } -#endif vec4 base = texture2D(baseTexture, uv).rgba; - + #ifdef ENABLE_BUMPMAPPING - if (use_normalmap){ + if (use_normalmap) { vec3 L = normalize(lightVec); vec3 E = normalize(eyeVec); - float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5); - float diffuse = dot(E,bump.xyz); + float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0),0.5); + float diffuse = dot(E,bump.xyz); /* Mathematic optimization * Original: color = 0.05*base.rgb + diffuse*base.rgb + 0.2*specular*base.rgb; * This optimization save 2 multiplications (orig: 4 multiplications + 3 additions diff --git a/client/shaders/water_surface_shader/opengl_vertex.glsl b/client/shaders/water_surface_shader/opengl_vertex.glsl index 6e70bbc36..53678800d 100644 --- a/client/shaders/water_surface_shader/opengl_vertex.glsl +++ b/client/shaders/water_surface_shader/opengl_vertex.glsl @@ -18,20 +18,23 @@ varying vec3 tsLightVec; const float e = 2.718281828459; const float BS = 10.0; -float smoothCurve( float x ) { - return x * x *( 3.0 - 2.0 * x ); +float smoothCurve(float x) +{ + return x * x * (3.0 - 2.0 * x); } -float triangleWave( float x ) { - return abs( fract( x + 0.5 ) * 2.0 - 1.0 ); +float triangleWave(float x) +{ + return abs(fract( x + 0.5 ) * 2.0 - 1.0); } -float smoothTriangleWave( float x ) { - return smoothCurve( triangleWave( x ) ) * 2.0 - 1.0; +float smoothTriangleWave(float x) +{ + return smoothCurve(triangleWave( x )) * 2.0 - 1.0; } void main(void) { gl_TexCoord[0] = gl_MultiTexCoord0; - + #if (MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) && ENABLE_WAVING_WATER vec4 pos = gl_Vertex; pos.y -= 2.0; diff --git a/minetest.conf.example b/minetest.conf.example index 76f01bcfc..f73dc3e27 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -225,40 +225,47 @@ # Set to true to pre-generate all item visuals #preload_item_visuals = false # Set to true to enable shaders. Disable them if video_driver = direct3d9/8. + #enable_shaders = true -# Set to true to enable textures bumpmapping. Requires shaders enabled. #enable_bumpmapping = false -# Set to true enables parallax occlusion mapping. Requires shaders enabled. +# Set to true to enable textures bumpmapping. Requires shaders enabled. #generate_normalmaps = false # Set to true enables on the fly normalmap generation (Emboss effect). # Requires bumpmapping enabled. #normalmaps_strength = 0.6 # Strength of generated normalmaps -#normalmaps_smooth = 1 +#normalmaps_smooth = 0 # Defines sampling step of texture (0 - 2). # A higher value results in smoother normal maps. +#parallax_occlusion_mode = 1 +# 0 = parallax occlusion with slope information (faster) +# 1 = relief mapping (slower, more accurate) #enable_parallax_occlusion = false -# Scale of parallax occlusion effect +# Set to true enables parallax occlusion mapping. Requires shaders enabled. +#parallax_occlusion_iterations = 4 +# Number of parallax occlusion iterations #parallax_occlusion_scale = 0.08 -# Bias of parallax occlusion effect, usually scale/2 +# Overall scale of parallax occlusion effect #parallax_occlusion_bias = 0.04 -# Set to true enables waving water. Requires shaders enabled. +# Overall bias of parallax occlusion effect, usually scale/2 #enable_waving_water = false +# Set to true enables waving water. Requires shaders enabled. # Parameters for waving water: #water_wave_height = 1.0 #water_wave_length = 20.0 #water_wave_speed = 5.0 -# Set to true enables waving leaves. Requires shaders enabled. #enable_waving_leaves = false -# Set to true enables waving plants. Requires shaders enabled. +# Set to true enables waving leaves. Requires shaders enabled. #enable_waving_plants = false -# Enables caching of facedir rotated meshes +# Set to true enables waving plants. Requires shaders enabled. #ambient_occlusion_gamma = 2.2 # The strength (darkness) of node ambient-occlusion shading. # Lower is darker, Higher is lighter. The valid range of values for this # setting is 0.25 to 4.0 inclusive. If the value is out of range it will be # set to the nearest valid value. #enable_mesh_cache = true +# Enables caching of facedir rotated meshes + # The time in seconds it takes between repeated # right clicks when holding the right mouse button. #repeat_rightclick_time = 0.25 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;