0

separated iterate and rendermode code, rendermodes are now defined by interface structs

This commit is contained in:
Aaron Griffith
2011-03-21 07:40:14 -04:00
parent 7b7f97d6c5
commit b908c6e07c
5 changed files with 204 additions and 107 deletions

View File

@@ -44,7 +44,7 @@ except AttributeError:
numpy_include = numpy.get_numpy_include() 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 [])) 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 # tell build_ext to build the extension in-place
# (NOT in build/) # (NOT in build/)

View File

@@ -17,10 +17,8 @@
#include "overviewer.h" #include "overviewer.h"
#include <numpy/arrayobject.h> /* available render modes */
extern RenderModeInterface rendermode_normal;
/* 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))))
static PyObject *textures = NULL; static PyObject *textures = NULL;
static PyObject *chunk_mod = NULL; static PyObject *chunk_mod = NULL;
@@ -55,61 +53,14 @@ int init_chunk_render(void) {
} }
inline int inline int
is_transparent(PyObject* tup, unsigned char b) { is_transparent(unsigned char b) {
PyObject *block = PyInt_FromLong(b); PyObject *block = PyInt_FromLong(b);
int ret = PySequence_Contains(tup, block); int ret = PySequence_Contains(transparent_blocks, block);
Py_DECREF(block); Py_DECREF(block);
return ret; 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 */ /* TODO triple check this to make sure reference counting is correct */
PyObject* PyObject*
chunk_render(PyObject *self, PyObject *args) { chunk_render(PyObject *self, PyObject *args) {
@@ -130,7 +81,18 @@ chunk_render(PyObject *self, PyObject *args) {
state.textures = textures; state.textures = textures;
state.chunk = chunk_mod; state.chunk = chunk_mod;
/* tuple */ /* 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");
}
}
/* get the image size */
imgsize = PyObject_GetAttrString(state.img, "size"); imgsize = PyObject_GetAttrString(state.img, "size");
imgsize0_py = PySequence_GetItem(imgsize, 0); imgsize0_py = PySequence_GetItem(imgsize, 0);
@@ -145,36 +107,26 @@ chunk_render(PyObject *self, PyObject *args) {
/* get the block data directly from numpy: */ /* get the block data directly from numpy: */
blocks_py = PyObject_GetAttrString(state.self, "blocks"); blocks_py = PyObject_GetAttrString(state.self, "blocks");
state.blocks = blocks_py;
/* /*
PyObject *left_blocks = PyObject_GetAttrString(chunk, "left_blocks"); PyObject *left_blocks = PyObject_GetAttrString(chunk, "left_blocks");
PyObject *right_blocks = PyObject_GetAttrString(chunk, "right_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.x = 15; state.x > -1; state.x--) {
for (state.y = 0; state.y < 16; state.y++) { for (state.y = 0; state.y < 16; state.y++) {
PyObject *blockid = NULL; PyObject *blockid = NULL;
/* set up the render coordinates */
state.imgx = xoff + state.x*12 + state.y*12; state.imgx = xoff + state.x*12 + state.y*12;
/* 128*12 -- offset for z direction, 15*6 -- offset for x */ /* 128*12 -- offset for z direction, 15*6 -- offset for x */
state.imgy = yoff - state.x*6 + state.y*6 + 128*12 + 15*6; state.imgy = yoff - state.x*6 + state.y*6 + 128*12 + 15*6;
for (state.z = 0; state.z < 128; state.z++) { for (state.z = 0; state.z < 128; state.z++) {
state.imgy -= 12; state.imgy -= 12;
/* make sure we're rendering inside the image boundaries */
if ((state.imgx >= imgsize0 + 24) || (state.imgx <= -24)) { if ((state.imgx >= imgsize0 + 24) || (state.imgx <= -24)) {
continue; continue;
} }
@@ -194,28 +146,21 @@ chunk_render(PyObject *self, PyObject *args) {
} }
blockid = PyInt_FromLong(state.block); blockid = PyInt_FromLong(state.block);
// check for occlusion
if ( (state.x != 0) && (state.y != 15) && (state.z != 127) && if (rendermode->occluded) {
!is_transparent(transparent_blocks, getArrayByte3D(blocks_py, state.x-1, state.y, state.z)) && if (rendermode->occluded(rm_data, &state)) {
!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) {
continue; continue;
} }
}
/* note that this version of alpha_over has a different signature than the // everything stored here will be a borrowed ref
version in _composite.c */ PyObject *t = NULL;
texture_alpha_over(state.img, t, state.imgx, state.imgy ); /* 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 { } else {
PyObject *tmp, *t; PyObject *tmp;
/* this should be a pointer to a unsigned char */ /* this should be a pointer to a unsigned char */
void* ancilData_p = PyArray_GETPTR3(blockdata_expanded, state.x, state.y, state.z); void* ancilData_p = PyArray_GETPTR3(blockdata_expanded, state.x, state.y, state.z);
@@ -234,18 +179,20 @@ chunk_render(PyObject *self, PyObject *args) {
/* this is a borrowed reference. no need to decref */ /* this is a borrowed reference. no need to decref */
t = PyDict_GetItem(specialblockmap, tmp); t = PyDict_GetItem(specialblockmap, tmp);
Py_DECREF(tmp); Py_DECREF(tmp);
if (t != NULL)
texture_alpha_over(state.img, t, state.imgx, state.imgy );
} }
if (lighting) { /* if we found a proper texture, render it! */
// FIXME whole-block shading for transparent blocks if (t != NULL && t != Py_None)
do_shading_for_face(state.self, state.x, state.y, state.z+1, facemasks[0], black_color, {
state.img, state.imgx, state.imgy); PyObject *src, *mask;
do_shading_for_face(state.self, state.x-1, state.y, state.z, facemasks[1], black_color, src = PyTuple_GetItem(t, 0);
state.img, state.imgx, state.imgy); mask = PyTuple_GetItem(t, 1);
do_shading_for_face(state.self, state.x, state.y+1, state.z, facemasks[2], black_color,
state.img, state.imgx, state.imgy); if (mask == Py_None)
mask = src;
if (rendermode->draw)
rendermode->draw(rm_data, &state, src, mask);
} }
} }
@@ -256,8 +203,11 @@ chunk_render(PyObject *self, PyObject *args) {
} }
} }
Py_DECREF(black_color); /* free up the rendermode info */
Py_DECREF(facemasks_py); if (rendermode->finish)
rendermode->finish(rm_data, &state);
free(rm_data);
Py_DECREF(blocks_py); Py_DECREF(blocks_py);
return Py_BuildValue("i",2); return Py_BuildValue("i",2);

View File

@@ -17,8 +17,6 @@
#include "overviewer.h" #include "overviewer.h"
#include <numpy/arrayobject.h>
static PyMethodDef COverviewerMethods[] = { static PyMethodDef COverviewerMethods[] = {
{"alpha_over", alpha_over_wrap, METH_VARARGS, {"alpha_over", alpha_over_wrap, METH_VARARGS,
"alpha over composite function"}, "alpha over composite function"},

View File

@@ -24,9 +24,13 @@
#ifndef __OVERVIEWER_H_INCLUDED__ #ifndef __OVERVIEWER_H_INCLUDED__
#define __OVERVIEWER_H_INCLUDED__ #define __OVERVIEWER_H_INCLUDED__
/* Python and PIL headers */ /* Python PIL, and numpy headers */
#include <Python.h> #include <Python.h>
#include <Imaging.h> #include <Imaging.h>
#include <numpy/arrayobject.h>
/* 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 */ /* in composite.c */
Imaging imaging_python_to_c(PyObject *obj); Imaging imaging_python_to_c(PyObject *obj);
@@ -44,16 +48,31 @@ typedef struct {
PyObject *textures; PyObject *textures;
PyObject *chunk; PyObject *chunk;
/* the rest only make sense for occluded() and draw() !! */
/* the tile image and destination */ /* the tile image and destination */
PyObject *img; PyObject *img;
int imgx, imgy; int imgx, imgy;
/* the block position and type */ /* the block position and type, and the block array */
int x, y, z; int x, y, z;
unsigned char block; unsigned char block;
PyObject *blocks;
} RenderState; } RenderState;
int is_transparent(PyObject* tup, unsigned char b); typedef struct {
PyObject *chunk_render(PyObject *self, PyObject *args); /* 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 init_chunk_render(void);
int is_transparent(unsigned char b);
PyObject *chunk_render(PyObject *self, PyObject *args);
#endif /* __OVERVIEWER_H_INCLUDED__ */ #endif /* __OVERVIEWER_H_INCLUDED__ */

130
src/rendermodes.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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,
};