From: Perttu Ahola Date: Sun, 24 Apr 2011 11:19:35 +0000 (+0300) Subject: cleaned the smooth lighting code a bit X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=ba88685fab339acd471ade642d24d9ca7e794f04;p=zefram%2Fminetest%2Fminetest_engine.git cleaned the smooth lighting code a bit --- diff --git a/src/mapblock.cpp b/src/mapblock.cpp index e98ec6e9..284c6430 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -133,6 +133,69 @@ u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2, #ifndef SERVER +/* + vertex_dirs: v3s16[4] +*/ +void getNodeVertexDirs(v3s16 dir, v3s16 *vertex_dirs) +{ + /* + If looked from outside the node towards the face, the corners are: + 0: bottom-right + 1: bottom-left + 2: top-left + 3: top-right + */ + if(dir == v3s16(0,0,1)) + { + // If looking towards z+, this is the face that is behind + // the center point, facing towards z+. + vertex_dirs[0] = v3s16(-1,-1, 1); + vertex_dirs[1] = v3s16( 1,-1, 1); + vertex_dirs[2] = v3s16( 1, 1, 1); + vertex_dirs[3] = v3s16(-1, 1, 1); + } + else if(dir == v3s16(0,0,-1)) + { + // faces towards Z- + vertex_dirs[0] = v3s16( 1,-1,-1); + vertex_dirs[1] = v3s16(-1,-1,-1); + vertex_dirs[2] = v3s16(-1, 1,-1); + vertex_dirs[3] = v3s16( 1, 1,-1); + } + else if(dir == v3s16(1,0,0)) + { + // faces towards X+ + vertex_dirs[0] = v3s16( 1,-1, 1); + vertex_dirs[1] = v3s16( 1,-1,-1); + vertex_dirs[2] = v3s16( 1, 1,-1); + vertex_dirs[3] = v3s16( 1, 1, 1); + } + else if(dir == v3s16(-1,0,0)) + { + // faces towards X- + vertex_dirs[0] = v3s16(-1,-1,-1); + vertex_dirs[1] = v3s16(-1,-1, 1); + vertex_dirs[2] = v3s16(-1, 1, 1); + vertex_dirs[3] = v3s16(-1, 1,-1); + } + else if(dir == v3s16(0,1,0)) + { + // faces towards Y+ (assume Z- as "down" in texture) + vertex_dirs[0] = v3s16( 1, 1,-1); + vertex_dirs[1] = v3s16(-1, 1,-1); + vertex_dirs[2] = v3s16(-1, 1, 1); + vertex_dirs[3] = v3s16( 1, 1, 1); + } + else if(dir == v3s16(0,-1,0)) + { + // faces towards Y- (assume Z+ as "down" in texture) + vertex_dirs[0] = v3s16( 1,-1, 1); + vertex_dirs[1] = v3s16(-1,-1, 1); + vertex_dirs[2] = v3s16(-1,-1,-1); + vertex_dirs[3] = v3s16( 1,-1,-1); + } +} + inline video::SColor lightColor(u8 alpha, u8 light) { return video::SColor(alpha,light,light,light); @@ -149,6 +212,18 @@ void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p, posRelative_f *= BS; v3f vertex_pos[4]; + v3s16 vertex_dirs[4]; + getNodeVertexDirs(dir, vertex_dirs); + for(u16 i=0; i<4; i++) + { + vertex_pos[i] = v3f( + BS/2*vertex_dirs[i].X, + BS/2*vertex_dirs[i].Y, + BS/2*vertex_dirs[i].Z + ); + } + + /*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); @@ -185,7 +260,7 @@ void makeFastFace(TileSpec tile, u8 li0, u8 li1, u8 li2, u8 li3, v3f p, { for(u16 i=0; i<4; i++) vertex_pos[i].rotateYZBy(90); - } + }*/ for(u16 i=0; i<4; i++) { @@ -383,6 +458,20 @@ u8 getSmoothLight(v3s16 p, VoxelManipulator &vmanip, u32 daynight_ratio) return light; } +// Calculate lighting at the given corner of p +u8 getSmoothLight(v3s16 p, v3s16 corner, + VoxelManipulator &vmanip, u32 daynight_ratio) +{ + if(corner.X == 1) p.X += 1; + else assert(corner.X == -1); + if(corner.Y == 1) p.Y += 1; + else assert(corner.Y == -1); + if(corner.Z == 1) p.Z += 1; + else assert(corner.Z == -1); + + return getSmoothLight(p, vmanip, daynight_ratio); +} + /* startpos: translate_dir: unit vector with only one of x, y or z @@ -415,6 +504,7 @@ void updateFastFaceRow( for(u16 j=0; j