From 2704bdc68e51c39f5ea475ba092c2d21281ccd53 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 24 Apr 2011 21:15:50 +0300 Subject: [PATCH] Optimized smooth lighting calculation code --- CMakeLists.txt | 2 +- doc/changelog.txt | 3 + src/game.cpp | 11 ++ src/main.cpp | 6 + src/mapblock.cpp | 339 +++++++++++++++++----------------------------- 5 files changed, 147 insertions(+), 214 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bd5e776b..d5b8028f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ project(minetest) set(VERSION_MAJOR 0) set(VERSION_MINOR 2) -set(VERSION_PATCH 20110424_0) +set(VERSION_PATCH 20110424_1_dev) set(VERSION_STRING "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") # Configuration options diff --git a/doc/changelog.txt b/doc/changelog.txt index 574ea60b1..71cbd970d 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -3,6 +3,9 @@ Minetest-c55 changelog This should contain all the major changes. For minor stuff, refer to the commit log of the repository. +X: +- Optimized smooth lighting + 2011-04-24: - Smooth lighting with simple ambient occlusion - Updated main menu diff --git a/src/game.cpp b/src/game.cpp index 7eb847e6c..9054f00cc 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1229,6 +1229,17 @@ void the_game( s32 dx = input->getMousePos().X - displaycenter.X; s32 dy = input->getMousePos().Y - displaycenter.Y; //std::cout<<"window active, pos difference "<isKeyDown(irr::KEY_UP)) + dy -= dtime * keyspeed; + if(input->isKeyDown(irr::KEY_DOWN)) + dy += dtime * keyspeed; + if(input->isKeyDown(irr::KEY_LEFT)) + dx -= dtime * keyspeed; + if(input->isKeyDown(irr::KEY_RIGHT)) + dx += dtime * keyspeed; + camera_yaw -= dx*0.2; camera_pitch += dy*0.2; if(camera_pitch < -89.5) camera_pitch = -89.5; diff --git a/src/main.cpp b/src/main.cpp index 3bc4de777..51a407827 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -167,6 +167,10 @@ TODO: Better control of draw_control.wanted_max_blocks TODO: Get player texture (and some others) from the specified texture directory +SUGG: Simple light color information to air + +TODO: Block mesh generator to tile properly on smooth lighting + Configuration: -------------- @@ -189,6 +193,8 @@ TODO: Don't update all meshes always on single node changes, but - Tool/weapon visualization +FIXME: When disconnected to the menu, memory is not freed properly + Server: ------- diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 284c64300..c89cf46da 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -223,45 +223,6 @@ void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p, ); } - /*v3f vertex_pos[4]; - // If looking towards z+, this is the face that is behind - // the center point, facing towards z+. - vertex_pos[0] = v3f(-BS/2,-BS/2,BS/2); - vertex_pos[1] = v3f( BS/2,-BS/2,BS/2); - vertex_pos[2] = v3f( BS/2, BS/2,BS/2); - vertex_pos[3] = v3f(-BS/2, BS/2,BS/2); - - if(dir == v3s16(0,0,1)) - { - for(u16 i=0; i<4; i++) - vertex_pos[i].rotateXZBy(0); - } - else if(dir == v3s16(0,0,-1)) - { - for(u16 i=0; i<4; i++) - vertex_pos[i].rotateXZBy(180); - } - else if(dir == v3s16(1,0,0)) - { - for(u16 i=0; i<4; i++) - vertex_pos[i].rotateXZBy(-90); - } - else if(dir == v3s16(-1,0,0)) - { - for(u16 i=0; i<4; i++) - vertex_pos[i].rotateXZBy(90); - } - else if(dir == v3s16(0,1,0)) - { - for(u16 i=0; i<4; i++) - vertex_pos[i].rotateYZBy(-90); - } - else if(dir == v3s16(0,-1,0)) - { - for(u16 i=0; i<4; i++) - vertex_pos[i].rotateYZBy(90); - }*/ - for(u16 i=0; i<4; i++) { vertex_pos[i].X *= scale.X; @@ -472,6 +433,73 @@ u8 getSmoothLight(v3s16 p, v3s16 corner, return getSmoothLight(p, vmanip, daynight_ratio); } +void getTileInfo( + // Input: + v3s16 blockpos_nodes, + v3s16 p, + v3s16 face_dir, + u32 daynight_ratio, + VoxelManipulator &vmanip, + NodeModMap &temp_mods, + bool smooth_lighting, + // Output: + bool &makes_face, + v3s16 &p_corrected, + v3s16 &face_dir_corrected, + u8 *lights, + TileSpec &tile + ) +{ + MapNode n0 = vmanip.getNodeNoEx(blockpos_nodes + p); + MapNode n1 = vmanip.getNodeNoEx(blockpos_nodes + p + face_dir); + TileSpec tile0 = getNodeTile(n0, p, face_dir, temp_mods); + TileSpec tile1 = getNodeTile(n1, p + face_dir, -face_dir, temp_mods); + + // This is hackish + u8 content0 = getNodeContent(p, n0, temp_mods); + u8 content1 = getNodeContent(p + face_dir, n1, temp_mods); + u8 mf = face_contents(content0, content1); + + if(mf == 0) + { + makes_face = false; + return; + } + + makes_face = true; + + if(mf == 1) + { + tile = tile0; + p_corrected = p; + face_dir_corrected = face_dir; + } + else + { + tile = tile1; + p_corrected = p + face_dir; + face_dir_corrected = -face_dir; + } + + if(smooth_lighting == false) + { + lights[0] = lights[1] = lights[2] = lights[3] = + decode_light(getFaceLight(daynight_ratio, n0, n1, face_dir)); + } + else + { + v3s16 vertex_dirs[4]; + getNodeVertexDirs(face_dir_corrected, vertex_dirs); + for(u16 i=0; i<4; i++) + { + lights[i] = getSmoothLight(blockpos_nodes + p_corrected, + vertex_dirs[i], vmanip, daynight_ratio); + } + } + + return; +} + /* startpos: translate_dir: unit vector with only one of x, y or z @@ -496,11 +524,14 @@ void updateFastFaceRow( u16 continuous_tiles_count = 0; - MapNode n0 = vmanip.getNodeNoEx(blockpos_nodes + p); - MapNode n1 = vmanip.getNodeNoEx(blockpos_nodes + p + face_dir); - TileSpec tile0 = getNodeTile(n0, p, face_dir, temp_mods); - TileSpec tile1 = getNodeTile(n1, p + face_dir, -face_dir, temp_mods); - u8 light = getFaceLight(daynight_ratio, n0, n1, face_dir); + bool makes_face; + v3s16 p_corrected; + v3s16 face_dir_corrected; + u8 lights[4]; + TileSpec tile; + getTileInfo(blockpos_nodes, p, face_dir, daynight_ratio, + vmanip, temp_mods, smooth_lighting, + makes_face, p_corrected, face_dir_corrected, lights, tile); for(u16 j=0; j