From b908c6e07ce3a267f75a1944d80c0e25a3da7dc6 Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Mon, 21 Mar 2011 07:40:14 -0400 Subject: [PATCH] separated iterate and rendermode code, rendermodes are now defined by interface structs --- setup.py | 2 +- src/iterate.c | 150 ++++++++++++++++------------------------------ src/main.c | 2 - src/overviewer.h | 27 +++++++-- src/rendermodes.c | 130 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 204 insertions(+), 107 deletions(-) create mode 100644 src/rendermodes.c diff --git a/setup.py b/setup.py index 48a776d..d918bd7 100644 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ except AttributeError: numpy_include = numpy.get_numpy_include() -c_overviewer_files = ['src/main.c', 'src/composite.c', 'src/iterate.c'] +c_overviewer_files = ['src/main.c', 'src/composite.c', 'src/iterate.c', 'src/rendermodes.c'] setup_kwargs['ext_modules'].append(Extension('c_overviewer', c_overviewer_files, include_dirs=['.', numpy_include], extra_link_args=["/MANIFEST"] if platform.system() == "Windows" else [])) # tell build_ext to build the extension in-place # (NOT in build/) diff --git a/src/iterate.c b/src/iterate.c index 5054a08..f0e962f 100644 --- a/src/iterate.c +++ b/src/iterate.c @@ -17,10 +17,8 @@ #include "overviewer.h" -#include - -/* macro for getting a value out of a 3D numpy byte array */ -#define getArrayByte3D(array, x,y,z) (*(unsigned char *)(PyArray_GETPTR3((array), (x), (y), (z)))) +/* available render modes */ +extern RenderModeInterface rendermode_normal; static PyObject *textures = NULL; static PyObject *chunk_mod = NULL; @@ -55,61 +53,14 @@ int init_chunk_render(void) { } inline int -is_transparent(PyObject* tup, unsigned char b) { +is_transparent(unsigned char b) { PyObject *block = PyInt_FromLong(b); - int ret = PySequence_Contains(tup, block); + int ret = PySequence_Contains(transparent_blocks, block); Py_DECREF(block); return ret; } -/* helper to handle alpha_over calls involving a texture tuple */ -static inline PyObject * -texture_alpha_over(PyObject *dest, PyObject *t, int imgx, int imgy) -{ - PyObject* src, * mask; - - src = PyTuple_GET_ITEM(t, 0); - mask = PyTuple_GET_ITEM(t, 1); - if (mask == Py_None) { - mask = src; - } - - return alpha_over(dest, src, mask, imgx, imgy, 0, 0); -} - -/* shades the drawn block with the given facemask/black_color, based on the - lighting results from (x, y, z) */ -static inline void -do_shading_for_face(PyObject *chunk, int x, int y, int z, PyObject *facemask, PyObject *black_color, - PyObject *img, int imgx, int imgy) -{ - // returns new references - PyObject* light_tup = PyObject_CallMethod(chunk, "get_lighting_coefficient", "iii", x, y, z); - PyObject *black_coeff_py = PySequence_GetItem(light_tup, 0); - double black_coeff = PyFloat_AsDouble(black_coeff_py); - Py_DECREF(black_coeff_py); - - PyObject *face_occlude_py = PySequence_GetItem(light_tup, 1); - int face_occlude = PyInt_AsLong(face_occlude_py); - Py_DECREF(face_occlude_py); - - - if (!face_occlude) { - //#composite.alpha_over(img, over_color, (imgx, imgy), ImageEnhance.Brightness(facemasks[0]).enhance(black_coeff)) - - PyObject *mask = PyObject_CallMethod(facemask, "copy", NULL); // new ref - //printf("black_coeff: %f\n", black_coeff); - brightness(mask, black_coeff); - //printf("done with brightness\n"); - alpha_over(img, black_color, mask, imgx, imgy, 0, 0); - //printf("done with alpha_over\n"); - Py_DECREF(mask); - - } -} - - /* TODO triple check this to make sure reference counting is correct */ PyObject* chunk_render(PyObject *self, PyObject *args) { @@ -129,8 +80,19 @@ chunk_render(PyObject *self, PyObject *args) { /* fill in important modules */ state.textures = textures; state.chunk = chunk_mod; + + /* set up the render mode */ + /* FIXME deciding on correct rendermode */ + RenderModeInterface *rendermode = &rendermode_normal; + void* rm_data = malloc(rendermode->data_size); + if (rendermode->start) { + if (rendermode->start(rm_data, &state)) { + free(rm_data); + return Py_BuildValue("i", "-1"); + } + } - /* tuple */ + /* get the image size */ imgsize = PyObject_GetAttrString(state.img, "size"); imgsize0_py = PySequence_GetItem(imgsize, 0); @@ -145,36 +107,26 @@ chunk_render(PyObject *self, PyObject *args) { /* get the block data directly from numpy: */ blocks_py = PyObject_GetAttrString(state.self, "blocks"); + state.blocks = blocks_py; /* PyObject *left_blocks = PyObject_GetAttrString(chunk, "left_blocks"); PyObject *right_blocks = PyObject_GetAttrString(chunk, "right_blocks"); */ - PyObject *quadtree = PyObject_GetAttrString(state.self, "quadtree"); - PyObject *lighting_py = PyObject_GetAttrString(quadtree, "lighting"); - int lighting = PyObject_IsTrue(lighting_py); - Py_DECREF(lighting_py); - Py_DECREF(quadtree); - - PyObject *black_color = PyObject_GetAttrString(state.chunk, "black_color"); - PyObject *facemasks_py = PyObject_GetAttrString(state.chunk, "facemasks"); - PyObject *facemasks[3]; - // borrowed references, don't need to be decref'd - facemasks[0] = PyTuple_GetItem(facemasks_py, 0); - facemasks[1] = PyTuple_GetItem(facemasks_py, 1); - facemasks[2] = PyTuple_GetItem(facemasks_py, 2); - for (state.x = 15; state.x > -1; state.x--) { for (state.y = 0; state.y < 16; state.y++) { PyObject *blockid = NULL; + /* set up the render coordinates */ state.imgx = xoff + state.x*12 + state.y*12; /* 128*12 -- offset for z direction, 15*6 -- offset for x */ state.imgy = yoff - state.x*6 + state.y*6 + 128*12 + 15*6; + for (state.z = 0; state.z < 128; state.z++) { state.imgy -= 12; + /* make sure we're rendering inside the image boundaries */ if ((state.imgx >= imgsize0 + 24) || (state.imgx <= -24)) { continue; } @@ -194,28 +146,21 @@ chunk_render(PyObject *self, PyObject *args) { } blockid = PyInt_FromLong(state.block); - - if ( (state.x != 0) && (state.y != 15) && (state.z != 127) && - !is_transparent(transparent_blocks, getArrayByte3D(blocks_py, state.x-1, state.y, state.z)) && - !is_transparent(transparent_blocks, getArrayByte3D(blocks_py, state.x, state.y, state.z+1)) && - !is_transparent(transparent_blocks, getArrayByte3D(blocks_py, state.x, state.y+1, state.z))) { - continue; - } - - - if (!PySequence_Contains(special_blocks, blockid)) { - /* t = textures.blockmap[blockid] */ - PyObject *t = PyList_GetItem(blockmap, state.block); - /* PyList_GetItem returns borrowed ref */ - if (t == Py_None) { + // check for occlusion + if (rendermode->occluded) { + if (rendermode->occluded(rm_data, &state)) { continue; } - - /* note that this version of alpha_over has a different signature than the - version in _composite.c */ - texture_alpha_over(state.img, t, state.imgx, state.imgy ); + } + + // everything stored here will be a borrowed ref + PyObject *t = NULL; + /* get the texture and mask from block type / ancil. data */ + if (!PySequence_Contains(special_blocks, blockid)) { + /* t = textures.blockmap[blockid] */ + t = PyList_GetItem(blockmap, state.block); } else { - PyObject *tmp, *t; + PyObject *tmp; /* this should be a pointer to a unsigned char */ void* ancilData_p = PyArray_GETPTR3(blockdata_expanded, state.x, state.y, state.z); @@ -234,19 +179,21 @@ chunk_render(PyObject *self, PyObject *args) { /* this is a borrowed reference. no need to decref */ t = PyDict_GetItem(specialblockmap, tmp); Py_DECREF(tmp); - if (t != NULL) - texture_alpha_over(state.img, t, state.imgx, state.imgy ); } - if (lighting) { - // FIXME whole-block shading for transparent blocks - do_shading_for_face(state.self, state.x, state.y, state.z+1, facemasks[0], black_color, - state.img, state.imgx, state.imgy); - do_shading_for_face(state.self, state.x-1, state.y, state.z, facemasks[1], black_color, - state.img, state.imgx, state.imgy); - do_shading_for_face(state.self, state.x, state.y+1, state.z, facemasks[2], black_color, - state.img, state.imgx, state.imgy); - } + /* if we found a proper texture, render it! */ + if (t != NULL && t != Py_None) + { + PyObject *src, *mask; + src = PyTuple_GetItem(t, 0); + mask = PyTuple_GetItem(t, 1); + + if (mask == Py_None) + mask = src; + + if (rendermode->draw) + rendermode->draw(rm_data, &state, src, mask); + } } if (blockid) { @@ -256,8 +203,11 @@ chunk_render(PyObject *self, PyObject *args) { } } - Py_DECREF(black_color); - Py_DECREF(facemasks_py); + /* free up the rendermode info */ + if (rendermode->finish) + rendermode->finish(rm_data, &state); + free(rm_data); + Py_DECREF(blocks_py); return Py_BuildValue("i",2); diff --git a/src/main.c b/src/main.c index 8bdd91d..32ca491 100644 --- a/src/main.c +++ b/src/main.c @@ -17,8 +17,6 @@ #include "overviewer.h" -#include - static PyMethodDef COverviewerMethods[] = { {"alpha_over", alpha_over_wrap, METH_VARARGS, "alpha over composite function"}, diff --git a/src/overviewer.h b/src/overviewer.h index 00dc0ae..6c6fe02 100644 --- a/src/overviewer.h +++ b/src/overviewer.h @@ -24,9 +24,13 @@ #ifndef __OVERVIEWER_H_INCLUDED__ #define __OVERVIEWER_H_INCLUDED__ -/* Python and PIL headers */ +/* Python PIL, and numpy headers */ #include #include +#include + +/* macro for getting a value out of a 3D numpy byte array */ +#define getArrayByte3D(array, x,y,z) (*(unsigned char *)(PyArray_GETPTR3((array), (x), (y), (z)))) /* in composite.c */ Imaging imaging_python_to_c(PyObject *obj); @@ -44,16 +48,31 @@ typedef struct { PyObject *textures; PyObject *chunk; + /* the rest only make sense for occluded() and draw() !! */ + /* the tile image and destination */ PyObject *img; int imgx, imgy; - /* the block position and type */ + /* the block position and type, and the block array */ int x, y, z; unsigned char block; + PyObject *blocks; } RenderState; -int is_transparent(PyObject* tup, unsigned char b); -PyObject *chunk_render(PyObject *self, PyObject *args); +typedef struct { + /* the size of the local storage for this rendermode */ + unsigned int data_size; + + /* may return non-zero on error */ + int (*start)(void *, RenderState *); + void (*finish)(void *, RenderState *); + /* returns non-zero to skip rendering this block */ + int (*occluded)(void *, RenderState *); + /* last two arguments are img and mask, from texture lookup */ + void (*draw)(void *, RenderState *, PyObject *, PyObject *); +} RenderModeInterface; int init_chunk_render(void); +int is_transparent(unsigned char b); +PyObject *chunk_render(PyObject *self, PyObject *args); #endif /* __OVERVIEWER_H_INCLUDED__ */ diff --git a/src/rendermodes.c b/src/rendermodes.c new file mode 100644 index 0000000..dd08ed0 --- /dev/null +++ b/src/rendermodes.c @@ -0,0 +1,130 @@ +/* + * This file is part of the Minecraft Overviewer. + * + * Minecraft Overviewer is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Minecraft Overviewer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with the Overviewer. If not, see . + */ + +#include "overviewer.h" + +/* shades the drawn block with the given facemask/black_color, based on the + lighting results from (x, y, z) */ +static inline void +do_shading_for_face(PyObject *chunk, int x, int y, int z, PyObject *facemask, PyObject *black_color, + PyObject *img, int imgx, int imgy) +{ + // returns new references + PyObject* light_tup = PyObject_CallMethod(chunk, "get_lighting_coefficient", "iii", x, y, z); + PyObject *black_coeff_py = PySequence_GetItem(light_tup, 0); + double black_coeff = PyFloat_AsDouble(black_coeff_py); + Py_DECREF(black_coeff_py); + + PyObject *face_occlude_py = PySequence_GetItem(light_tup, 1); + int face_occlude = PyInt_AsLong(face_occlude_py); + Py_DECREF(face_occlude_py); + + + if (!face_occlude) { + //#composite.alpha_over(img, over_color, (imgx, imgy), ImageEnhance.Brightness(facemasks[0]).enhance(black_coeff)) + + PyObject *mask = PyObject_CallMethod(facemask, "copy", NULL); // new ref + //printf("black_coeff: %f\n", black_coeff); + brightness(mask, black_coeff); + //printf("done with brightness\n"); + alpha_over(img, black_color, mask, imgx, imgy, 0, 0); + //printf("done with alpha_over\n"); + Py_DECREF(mask); + + } +} + +typedef struct { + int lighting; + PyObject *black_color, *facemasks_py; + PyObject *facemasks[3]; +} RenderModeNormal; + +static int +rendermode_normal_start(void *data, RenderState *state) { + RenderModeNormal* self = (RenderModeNormal *)data; + + PyObject *quadtree = PyObject_GetAttrString(state->self, "quadtree"); + PyObject *lighting_py = PyObject_GetAttrString(quadtree, "lighting"); + self->lighting = PyObject_IsTrue(lighting_py); + Py_DECREF(lighting_py); + Py_DECREF(quadtree); + + self->black_color = PyObject_GetAttrString(state->chunk, "black_color"); + self->facemasks_py = PyObject_GetAttrString(state->chunk, "facemasks"); + // borrowed references, don't need to be decref'd + self->facemasks[0] = PyTuple_GetItem(self->facemasks_py, 0); + self->facemasks[1] = PyTuple_GetItem(self->facemasks_py, 1); + self->facemasks[2] = PyTuple_GetItem(self->facemasks_py, 2); + + return 0; +} + +static void +rendermode_normal_finish(void *data, RenderState *state) { + RenderModeNormal *self = (RenderModeNormal *)data; + + Py_DECREF(self->black_color); + Py_DECREF(self->facemasks_py); +} + +static int +rendermode_normal_occluded(void *data, RenderState *state) { + int x = state->x, y = state->y, z = state->z; + + if ( (x != 0) && (y != 15) && (z != 127) && + !is_transparent(getArrayByte3D(state->blocks, x-1, y, z)) && + !is_transparent(getArrayByte3D(state->blocks, x, y, z+1)) && + !is_transparent(getArrayByte3D(state->blocks, x, y+1, z))) { + return 1; + } + + return 0; +} + +static void +rendermode_normal_draw(void *data, RenderState *state, PyObject *src, PyObject *mask) +{ + RenderModeNormal* self = (RenderModeNormal *)data; + + PyObject *chunk = state->self; + int x = state->x, y = state->y, z = state->z; + PyObject **facemasks = self->facemasks; + PyObject *black_color = self->black_color, *img = state->img; + int imgx = state->imgx, imgy = state->imgy; + + alpha_over(img, src, mask, imgx, imgy, 0, 0); + + if (self->lighting) { + // FIXME whole-block shading for transparent blocks + do_shading_for_face(chunk, x, y, z+1, facemasks[0], black_color, + img, imgx, imgy); + do_shading_for_face(chunk, x-1, y, z, facemasks[1], black_color, + img, imgx, imgy); + do_shading_for_face(chunk, x, y+1, z, facemasks[2], black_color, + img, imgx, imgy); + } + +} + +RenderModeInterface rendermode_normal = { + sizeof(RenderModeNormal), + rendermode_normal_start, + rendermode_normal_finish, + rendermode_normal_occluded, + rendermode_normal_draw, +};