0

rewrote get_lighting_coefficient in C

This commit is contained in:
Aaron Griffith
2011-03-23 00:03:26 -04:00
parent 24950f6024
commit f5264e9306
4 changed files with 124 additions and 140 deletions

102
chunk.py
View File

@@ -398,108 +398,6 @@ class ChunkRenderer(object):
return pseudo_data
def calculate_darkness(self, skylight, blocklight):
"""Takes a raw blocklight and skylight, and returns a value
between 0.0 (fully lit) and 1.0 (fully black) that can be used as
an alpha value for a blend with a black source image. It mimics
Minecraft lighting calculations."""
if not self.quadtree.night:
# Daytime
return 1.0 - pow(0.8, 15 - max(blocklight, skylight))
else:
# Nighttime
return 1.0 - pow(0.8, 15 - max(blocklight, skylight - 11))
def get_lighting_coefficient(self, x, y, z, norecurse=False):
"""Calculates the lighting coefficient for the given
coordinate, using default lighting and peeking into
neighboring chunks, if needed. A lighting coefficient of 1.0
means fully black.
Returns a tuple (coefficient, occluded), where occluded is
True if the given coordinate is filled with a solid block, and
therefore the returned coefficient is just the default."""
# placeholders for later data arrays, coordinates
blocks = None
skylight = None
blocklight = None
local_x = x
local_y = y
local_z = z
is_local_chunk = False
# find out what chunk we're in, and translate accordingly
if x >= 0 and y < 16:
blocks = self.blocks
skylight = self.skylight
blocklight = self.blocklight
is_local_chunk = True
elif x < 0:
local_x += 16
blocks = self.left_blocks
skylight = self.left_skylight
blocklight = self.left_blocklight
elif y >= 16:
local_y -= 16
blocks = self.right_blocks
skylight = self.right_skylight
blocklight = self.right_blocklight
# make sure we have a correctly-ranged coordinates and enough
# info about the chunk
if not (blocks is not None and skylight is not None and blocklight is not None and
local_x >= 0 and local_x < 16 and local_y >= 0 and local_y < 16 and
local_z >= 0 and local_z < 128):
# we have no useful info, return default
return (self.calculate_darkness(15, 0), False)
blocktype = blocks[local_x, local_y, local_z]
# special handling for half-blocks
# (don't recurse more than once!)
if blocktype == 44 and not norecurse:
# average gathering variables
averagegather = 0.0
averagecount = 0
# how bright we need before we consider a side "lit"
threshold = self.calculate_darkness(0, 0)
# iterate through all the sides of the block
sides = [(x-1, y, z), (x+1, y, z), (x, y, z-1), (x, y, z+1), (x, y-1, z), (x, y+1, z)]
for side in sides:
val, occ = self.get_lighting_coefficient(*side, norecurse=True)
if (not occ) and (val < threshold):
averagegather += val
averagecount += 1
# if at least one side was lit, return the average
if averagecount > 0:
return (averagegather / averagecount, False)
# calculate the return...
occluded = not (blocktype in transparent_blocks)
# only calculate the non-default coefficient if we're not occluded
if (blocktype == 10) or (blocktype == 11):
# lava blocks should always be lit!
coefficient = 0.0
elif occluded:
coefficient = self.calculate_darkness(15, 0)
else:
coefficient = self.calculate_darkness(skylight[local_x, local_y, local_z], blocklight[local_x, local_y, local_z])
# only say we're occluded if the point is in the CURRENT
# chunk, so that we don't get obvious inter-chunk dependencies
# (we want this here so we still have the default coefficient
# for occluded blocks, even when we don't report them as
# occluded)
if not is_local_chunk:
occluded = False
return (coefficient, occluded)
def chunk_render(self, img=None, xoff=0, yoff=0, cave=False):
"""Renders a chunk with the given parameters, and returns the image.
If img is given, the chunk is rendered to that image object. Otherwise,