diff options
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/finish.h | 64 | ||||
-rw-r--r-- | libdimension/dimension/finishes.h | 18 | ||||
-rw-r--r-- | libdimension/finish.c | 122 | ||||
-rw-r--r-- | libdimension/lambertian.c | 4 | ||||
-rw-r--r-- | libdimension/phong.c | 4 | ||||
-rw-r--r-- | libdimension/reflection.c | 4 | ||||
-rw-r--r-- | libdimension/tests/render.c | 15 | ||||
-rw-r--r-- | libdimension/texture.c | 1 |
8 files changed, 48 insertions, 184 deletions
diff --git a/libdimension/dimension/finish.h b/libdimension/dimension/finish.h index 5f2443b..d975877 100644 --- a/libdimension/dimension/finish.h +++ b/libdimension/dimension/finish.h @@ -28,13 +28,10 @@ /** Ambient finish component. */ typedef struct dmnsn_ambient { dmnsn_color ambient; /**< Ambient light. */ - DMNSN_REFCOUNT; /**< Reference count. */ } dmnsn_ambient; -/** Allocate a dummy ambient component. */ -dmnsn_ambient *dmnsn_new_ambient(dmnsn_color ambient); -/** Delete an ambient component. */ -void dmnsn_delete_ambient(dmnsn_ambient *ambient); +/** Allocate an ambient component. */ +dmnsn_ambient *dmnsn_new_ambient(dmnsn_pool *pool, dmnsn_color ambient); /* Diffuse component */ @@ -53,26 +50,15 @@ typedef dmnsn_color dmnsn_diffuse_fn(const dmnsn_diffuse *diffuse, dmnsn_color light, dmnsn_color color, dmnsn_vector ray, dmnsn_vector normal); -/** - * Diffuse destruction callback. - * @param[in,out] diffuse The diffuse object to destroy. - */ -typedef void dmnsn_diffuse_free_fn(dmnsn_diffuse *diffuse); - /** Diffuse finish component. */ struct dmnsn_diffuse { dmnsn_diffuse_fn *diffuse_fn; /**< Diffuse callback. */ - dmnsn_diffuse_free_fn *free_fn; /**< Destructor callback. */ - - DMNSN_REFCOUNT; /**< Reference count. */ }; /** Allocate a dummy diffuse component. */ -dmnsn_diffuse *dmnsn_new_diffuse(void); +dmnsn_diffuse *dmnsn_new_diffuse(dmnsn_pool *pool); /** Initialize a dmnsn_diffuse field. */ void dmnsn_init_diffuse(dmnsn_diffuse *diffuse); -/** Delete a diffuse component. */ -void dmnsn_delete_diffuse(dmnsn_diffuse *diffuse); /* Specular component */ @@ -93,26 +79,15 @@ typedef dmnsn_color dmnsn_specular_fn(const dmnsn_specular *specular, dmnsn_vector ray, dmnsn_vector normal, dmnsn_vector viewer); -/** - * Specular destruction callback. - * @param[in,out] specular The specular object to destroy. - */ -typedef void dmnsn_specular_free_fn(dmnsn_specular *specular); - /** Specular finish component. */ struct dmnsn_specular { dmnsn_specular_fn *specular_fn; /**< Specular callback. */ - dmnsn_specular_free_fn *free_fn; /**< Destructor callback. */ - - DMNSN_REFCOUNT; /**< Reference count. */ }; /** Allocate a dummy specular component. */ -dmnsn_specular *dmnsn_new_specular(void); +dmnsn_specular *dmnsn_new_specular(dmnsn_pool *pool); /** Initialize a dmnsn_specular field. */ void dmnsn_init_specular(dmnsn_specular *specular); -/** Delete a specular component. */ -void dmnsn_delete_specular(dmnsn_specular *specular); /* Reflection component */ @@ -131,34 +106,23 @@ typedef dmnsn_color dmnsn_reflection_fn(const dmnsn_reflection *reflection, dmnsn_color reflect, dmnsn_color color, dmnsn_vector ray, dmnsn_vector normal); -/** - * Reflection destruction callback. - * @param[in,out] reflection The reflection object to destroy. - */ -typedef void dmnsn_reflection_free_fn(dmnsn_reflection *reflection); - /** The reflection component. */ struct dmnsn_reflection { dmnsn_reflection_fn *reflection_fn; /**< Reflection callback. */ - dmnsn_reflection_free_fn *free_fn; /**< Destructor callback. */ - - DMNSN_REFCOUNT; /**< Reference count. */ }; /** Allocate a dummy reflection component. */ -dmnsn_reflection *dmnsn_new_reflection(void); +dmnsn_reflection *dmnsn_new_reflection(dmnsn_pool *pool); /** Initialize a dmnsn_reflection field. */ void dmnsn_init_reflection(dmnsn_reflection *reflection); -/** Delete a reflection component. */ -void dmnsn_delete_reflection(dmnsn_reflection *reflection); /* Entire finishes */ /** A finish. */ typedef struct dmnsn_finish { - dmnsn_ambient *ambient; /**< Ambient component. */ - dmnsn_diffuse *diffuse; /**< Diffuse component. */ - dmnsn_specular *specular; /**< Specular component. */ + dmnsn_ambient *ambient; /**< Ambient component. */ + dmnsn_diffuse *diffuse; /**< Diffuse component. */ + dmnsn_specular *specular; /**< Specular component. */ dmnsn_reflection *reflection; /**< Reflection component. */ } dmnsn_finish; @@ -169,18 +133,6 @@ typedef struct dmnsn_finish { dmnsn_finish dmnsn_new_finish(void); /** - * Delete a finish. - * @param[in,out] finish The finish to delete. - */ -void dmnsn_delete_finish(dmnsn_finish finish); - -/** - * Increment a finish's reference count. - * @param[in,out] finish The finish to acquire. - */ -void dmnsn_finish_incref(dmnsn_finish *finish); - -/** * Fill missing finish properties from a default finish. * @param[in] default_finish The default finish. * @param[in,out] finish The finish to fill. diff --git a/libdimension/dimension/finishes.h b/libdimension/dimension/finishes.h index 7ed66d3..e1f7b44 100644 --- a/libdimension/dimension/finishes.h +++ b/libdimension/dimension/finishes.h @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2009-2014 Tavian Barnes <tavianator@tavianator.com> * * * * This file is part of The Dimension Library. * * * @@ -25,25 +25,27 @@ /** * Regular diffuse finish. + * @param[in] pool The memory pool to allocate from. * @param[in] diffuse The diffuse reflection coefficient. * @return A diffuse finish component. */ -dmnsn_diffuse *dmnsn_new_lambertian(double diffuse); +dmnsn_diffuse *dmnsn_new_lambertian(dmnsn_pool *pool, double diffuse); /** * A phong specular highlight. + * @param[in] pool The memory pool to allocate from. * @param[in] specular The specular reflection coefficient. - * @param[in] exp The exponent (roughly the highlight size). + * @param[in] exp The exponent (roughly the highlight size). * @return A phong specular finish component. */ -dmnsn_specular *dmnsn_new_phong(double specular, double exp); +dmnsn_specular *dmnsn_new_phong(dmnsn_pool *pool, double specular, double exp); /** * Specular (mirror) reflection. - * @param[in] min Reflection at paralell angles. - * @param[in] max Reflection at perpendicular angles (often == \p min). + * @param[in] pool The memory pool to allocate from. + * @param[in] min Reflection at paralell angles. + * @param[in] max Reflection at perpendicular angles (often == \p min). * @param[in] falloff Degree of exponential falloff (usually 1). * @return A reflective finish component. */ -dmnsn_reflection *dmnsn_new_basic_reflection(dmnsn_color min, dmnsn_color max, - double falloff); +dmnsn_reflection *dmnsn_new_basic_reflection(dmnsn_pool *pool, dmnsn_color min, dmnsn_color max, double falloff); diff --git a/libdimension/finish.c b/libdimension/finish.c index 276969d..13adefb 100644 --- a/libdimension/finish.c +++ b/libdimension/finish.c @@ -26,32 +26,17 @@ #include "dimension-internal.h" dmnsn_ambient * -dmnsn_new_ambient(dmnsn_color ambient_light) +dmnsn_new_ambient(dmnsn_pool *pool, dmnsn_color ambient_light) { - dmnsn_ambient *ambient = DMNSN_MALLOC(dmnsn_ambient); + dmnsn_ambient *ambient = DMNSN_PALLOC(pool, dmnsn_ambient); ambient->ambient = ambient_light; - DMNSN_REFCOUNT_INIT(ambient); return ambient; } -void -dmnsn_delete_ambient(dmnsn_ambient *ambient) -{ - if (DMNSN_DECREF(ambient)) { - dmnsn_free(ambient); - } -} - -static void -dmnsn_default_diffuse_free_fn(dmnsn_diffuse *diffuse) -{ - dmnsn_free(diffuse); -} - dmnsn_diffuse * -dmnsn_new_diffuse(void) +dmnsn_new_diffuse(dmnsn_pool *pool) { - dmnsn_diffuse *diffuse = DMNSN_MALLOC(dmnsn_diffuse); + dmnsn_diffuse *diffuse = DMNSN_PALLOC(pool, dmnsn_diffuse); dmnsn_init_diffuse(diffuse); return diffuse; } @@ -59,28 +44,12 @@ dmnsn_new_diffuse(void) void dmnsn_init_diffuse(dmnsn_diffuse *diffuse) { - diffuse->free_fn = dmnsn_default_diffuse_free_fn; - DMNSN_REFCOUNT_INIT(diffuse); -} - -void -dmnsn_delete_diffuse(dmnsn_diffuse *diffuse) -{ - if (DMNSN_DECREF(diffuse)) { - diffuse->free_fn(diffuse); - } -} - -static void -dmnsn_default_specular_free_fn(dmnsn_specular *specular) -{ - dmnsn_free(specular); } dmnsn_specular * -dmnsn_new_specular(void) +dmnsn_new_specular(dmnsn_pool *pool) { - dmnsn_specular *specular = DMNSN_MALLOC(dmnsn_specular); + dmnsn_specular *specular = DMNSN_PALLOC(pool, dmnsn_specular); dmnsn_init_specular(specular); return specular; } @@ -88,28 +57,12 @@ dmnsn_new_specular(void) void dmnsn_init_specular(dmnsn_specular *specular) { - specular->free_fn = dmnsn_default_specular_free_fn; - DMNSN_REFCOUNT_INIT(specular); -} - -void -dmnsn_delete_specular(dmnsn_specular *specular) -{ - if (DMNSN_DECREF(specular)) { - specular->free_fn(specular); - } -} - -static void -dmnsn_default_reflection_free_fn(dmnsn_reflection *reflection) -{ - dmnsn_free(reflection); } dmnsn_reflection * -dmnsn_new_reflection(void) +dmnsn_new_reflection(dmnsn_pool *pool) { - dmnsn_reflection *reflection = DMNSN_MALLOC(dmnsn_reflection); + dmnsn_reflection *reflection = DMNSN_PALLOC(pool, dmnsn_reflection); dmnsn_init_reflection(reflection); return reflection; } @@ -117,72 +70,33 @@ dmnsn_new_reflection(void) void dmnsn_init_reflection(dmnsn_reflection *reflection) { - reflection->free_fn = dmnsn_default_reflection_free_fn; - DMNSN_REFCOUNT_INIT(reflection); -} - -void -dmnsn_delete_reflection(dmnsn_reflection *reflection) -{ - if (DMNSN_DECREF(reflection)) { - reflection->free_fn(reflection); - } } dmnsn_finish dmnsn_new_finish(void) { - dmnsn_finish finish; - finish.ambient = NULL; - finish.diffuse = NULL; - finish.specular = NULL; - finish.reflection = NULL; + dmnsn_finish finish = { + .ambient = NULL, + .diffuse = NULL, + .specular = NULL, + .reflection = NULL, + }; return finish; } void -dmnsn_delete_finish(dmnsn_finish finish) -{ - dmnsn_delete_reflection(finish.reflection); - dmnsn_delete_specular(finish.specular); - dmnsn_delete_diffuse(finish.diffuse); - dmnsn_delete_ambient(finish.ambient); -} - -void -dmnsn_finish_incref(dmnsn_finish *finish) -{ - if (finish->ambient) { - DMNSN_INCREF(finish->ambient); - } - if (finish->diffuse) { - DMNSN_INCREF(finish->diffuse); - } - if (finish->specular) { - DMNSN_INCREF(finish->specular); - } - if (finish->reflection) { - DMNSN_INCREF(finish->reflection); - } -} - -void dmnsn_finish_cascade(const dmnsn_finish *default_finish, dmnsn_finish *finish) { - if (!finish->ambient && default_finish->ambient) { + if (!finish->ambient) { finish->ambient = default_finish->ambient; - DMNSN_INCREF(finish->ambient); } - if (!finish->diffuse && default_finish->diffuse) { + if (!finish->diffuse) { finish->diffuse = default_finish->diffuse; - DMNSN_INCREF(finish->diffuse); } - if (!finish->specular && default_finish->specular) { + if (!finish->specular) { finish->specular = default_finish->specular; - DMNSN_INCREF(finish->specular); } - if (!finish->reflection && default_finish->reflection) { + if (!finish->reflection) { finish->reflection = default_finish->reflection; - DMNSN_INCREF(finish->reflection); } } diff --git a/libdimension/lambertian.c b/libdimension/lambertian.c index 9b305df..9360872 100644 --- a/libdimension/lambertian.c +++ b/libdimension/lambertian.c @@ -45,9 +45,9 @@ dmnsn_lambertian_diffuse_fn(const dmnsn_diffuse *diffuse, } dmnsn_diffuse * -dmnsn_new_lambertian(double coeff) +dmnsn_new_lambertian(dmnsn_pool *pool, double coeff) { - dmnsn_lambertian *lambertian = DMNSN_MALLOC(dmnsn_lambertian); + dmnsn_lambertian *lambertian = DMNSN_PALLOC(pool, dmnsn_lambertian); lambertian->coeff = coeff; dmnsn_diffuse *diffuse = &lambertian->diffuse; diff --git a/libdimension/phong.c b/libdimension/phong.c index bbf521d..3536f45 100644 --- a/libdimension/phong.c +++ b/libdimension/phong.c @@ -56,9 +56,9 @@ dmnsn_phong_specular_fn(const dmnsn_specular *specular, /* A phong finish */ dmnsn_specular * -dmnsn_new_phong(double coeff, double exp) +dmnsn_new_phong(dmnsn_pool *pool, double coeff, double exp) { - dmnsn_phong *phong = DMNSN_MALLOC(dmnsn_phong); + dmnsn_phong *phong = DMNSN_PALLOC(pool, dmnsn_phong); phong->coeff = coeff; phong->exp = exp; diff --git a/libdimension/reflection.c b/libdimension/reflection.c index ca279b8..3b2aeeb 100644 --- a/libdimension/reflection.c +++ b/libdimension/reflection.c @@ -50,9 +50,9 @@ dmnsn_basic_reflection_fn(const dmnsn_reflection *reflection, } dmnsn_reflection * -dmnsn_new_basic_reflection(dmnsn_color min, dmnsn_color max, double falloff) +dmnsn_new_basic_reflection(dmnsn_pool *pool, dmnsn_color min, dmnsn_color max, double falloff) { - dmnsn_basic_reflection *basic = DMNSN_MALLOC(dmnsn_basic_reflection); + dmnsn_basic_reflection *basic = DMNSN_PALLOC(pool, dmnsn_basic_reflection); basic->min = min; basic->max = max; basic->falloff = falloff; diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c index b5e2e41..38451f7 100644 --- a/libdimension/tests/render.c +++ b/libdimension/tests/render.c @@ -29,9 +29,9 @@ dmnsn_test_scene_set_defaults(dmnsn_pool *pool, dmnsn_scene *scene) scene->default_texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_black)); dmnsn_finish *default_finish = &scene->default_texture->finish; default_finish->ambient = dmnsn_new_ambient( - dmnsn_color_from_sRGB(dmnsn_color_mul(0.1, dmnsn_white)) + pool, dmnsn_color_from_sRGB(dmnsn_color_mul(0.1, dmnsn_white)) ); - default_finish->diffuse = dmnsn_new_lambertian(dmnsn_sRGB_inverse_gamma(0.7)); + default_finish->diffuse = dmnsn_new_lambertian(pool, dmnsn_sRGB_inverse_gamma(0.7)); } static void @@ -127,10 +127,8 @@ dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_tcolor cube_color = dmnsn_new_tcolor(dmnsn_blue, 0.75, 1.0/3.0); cube->texture->pigment = dmnsn_new_solid_pigment(pool, cube_color); - dmnsn_color reflect = - dmnsn_color_from_sRGB(dmnsn_color_mul(0.5, dmnsn_white)); - cube->texture->finish.reflection = - dmnsn_new_basic_reflection(dmnsn_black, reflect, 1.0); + dmnsn_color reflect = dmnsn_color_from_sRGB(dmnsn_color_mul(0.5, dmnsn_white)); + cube->texture->finish.reflection = dmnsn_new_basic_reflection(pool, dmnsn_black, reflect, 1.0); cube->interior = dmnsn_new_interior(pool); cube->interior->ior = 1.1; @@ -138,8 +136,7 @@ dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_object *sphere = dmnsn_new_sphere(); sphere->texture = dmnsn_new_texture(); sphere->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_green)); - sphere->texture->finish.specular = - dmnsn_new_phong(dmnsn_sRGB_inverse_gamma(0.2), 40.0); + sphere->texture->finish.specular = dmnsn_new_phong(pool, dmnsn_sRGB_inverse_gamma(0.2), 40.0); sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); dmnsn_object *hollow_cube = dmnsn_new_csg_difference(cube, sphere); @@ -208,7 +205,7 @@ dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_delete_array(torus_array); torii->texture = dmnsn_new_texture(); torii->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_blue)); - torii->texture->finish.ambient = dmnsn_new_ambient(dmnsn_white); + torii->texture->finish.ambient = dmnsn_new_ambient(pool, dmnsn_white); dmnsn_array *spike_array = DMNSN_NEW_ARRAY(dmnsn_object *); dmnsn_array_push(spike_array, &arrow); diff --git a/libdimension/texture.c b/libdimension/texture.c index 37a937b..0727ea1 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -43,7 +43,6 @@ void dmnsn_delete_texture(dmnsn_texture *texture) { if (DMNSN_DECREF(texture)) { - dmnsn_delete_finish(texture->finish); dmnsn_free(texture); } } |