From 275c2542c4e414ffe01558cfca2445dcad6bbd29 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 28 Jul 2011 12:47:10 -0600 Subject: Remove color_maps. --- dimension/tests/demo.dmnsn | 36 +++++----- libdimension-python/dimension.pxd | 4 -- libdimension-python/dimension.pyx | 33 --------- libdimension/Makefile.am | 1 - libdimension/color_map.c | 98 --------------------------- libdimension/dimension/map.h | 6 +- libdimension/dimension/pigments.h | 28 +++----- libdimension/pigment_map.c | 7 ++ libdimension/tests/render.c | 138 ++++++++++++++++++++++++-------------- 9 files changed, 127 insertions(+), 224 deletions(-) delete mode 100644 libdimension/color_map.c diff --git a/dimension/tests/demo.dmnsn b/dimension/tests/demo.dmnsn index 550c57a..f20be39 100644 --- a/dimension/tests/demo.dmnsn +++ b/dimension/tests/demo.dmnsn @@ -17,14 +17,27 @@ # along with this program. If not, see . # ######################################################################### -# Background -background = Clear - # Camera camera = PerspectiveCamera(location = (0, 0.25, -4), look_at = 0) camera.transform(rotate(53*Y)) +# Background +background = Clear + +# Sky sphere +sky_sphere = SkySphere( + [ + PigmentMap( + pattern = Gradient(Y), + map = { + 0: Orange, + 0.35: Color(0, 0.1, 0.2, trans = 0.1, filter = 0.0), + }, + ), + ] +) + # Lights lights = [ PointLight(location = (-15, 20, 10), color = White), @@ -66,7 +79,7 @@ arrow = Union( open = True ), ], - pigment = ColorMap( + pigment = PigmentMap( Gradient(Y), { 0/6: Red, @@ -125,7 +138,7 @@ ground = Plane( Checker(), [ White, - ColorMap(Checker(), [Black, White]).transform(scale(1/3)) + PigmentMap(Checker(), [Black, White]).transform(scale(1/3)) ], ), ) @@ -136,16 +149,3 @@ objects = [ strip, ground, ] - -# Sky sphere -sky_sphere = SkySphere( - [ - ColorMap( - pattern = Gradient(Y), - map = { - 0: Orange, - 0.35: Color(0, 0.1, 0.2, trans = 0.1, filter = 0.0), - }, - ), - ] -) diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd index 8b0ccbb..da4ac12 100644 --- a/libdimension-python/dimension.pxd +++ b/libdimension-python/dimension.pxd @@ -217,7 +217,6 @@ cdef extern from "../libdimension/dimension.h": void dmnsn_add_map_entry(dmnsn_map *map, double n, void *obj) size_t dmnsn_map_size(dmnsn_map *map) - dmnsn_map *dmnsn_new_color_map() dmnsn_map *dmnsn_new_pigment_map() ############ @@ -236,9 +235,6 @@ cdef extern from "../libdimension/dimension.h": dmnsn_pigment *dmnsn_new_solid_pigment(dmnsn_color color) dmnsn_pigment *dmnsn_new_canvas_pigment(dmnsn_canvas *canvas) - dmnsn_pigment *dmnsn_new_color_map_pigment(dmnsn_pattern *pattern, - dmnsn_map *map, - dmnsn_pigment_map_flags flags) dmnsn_pigment *dmnsn_new_pigment_map_pigment(dmnsn_pattern *pattern, dmnsn_map *map, dmnsn_pigment_map_flags flags) diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx index dd91b0e..2cde396 100644 --- a/libdimension-python/dimension.pyx +++ b/libdimension-python/dimension.pyx @@ -617,39 +617,6 @@ cdef _Pigment(dmnsn_pigment *pigment): DMNSN_INCREF(self._pigment) return self -cdef class ColorMap(Pigment): - """A color map""" - def __init__(self, Pattern pattern not None, map, bool sRGB not None = True, - *args, **kwargs): - """ - Create a ColorMap. - - Keyword arguments: - pattern -- the pattern to use for the mapping - map -- a dictionary of the form { val1: color1, val2: color2, ... }, - or a list of the form [color1, color2, ...] - sRGB -- whether the gradients should be in sRGB or linear space - (default True) - """ - cdef dmnsn_map *color_map = dmnsn_new_color_map() - if hasattr(map, "items"): - for i, color in map.items(): - dmnsn_add_map_entry(color_map, i, &Color(color)._c) - else: - for i, color in enumerate(map): - dmnsn_add_map_entry(color_map, i/len(map), &Color(color)._c) - - cdef dmnsn_pigment_map_flags flags - if sRGB: - flags = DMNSN_PIGMENT_MAP_SRGB - else: - flags = DMNSN_PIGMENT_MAP_REGULAR - - DMNSN_INCREF(pattern._pattern) - self._pigment = dmnsn_new_color_map_pigment(pattern._pattern, color_map, - flags) - Pigment.__init__(self, *args, **kwargs) - cdef class PigmentMap(Pigment): """A pigment map.""" def __init__(self, Pattern pattern not None, map, bool sRGB not None = True, diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index b042fc9..31db52a 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -68,7 +68,6 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \ canvas_pigment.c \ checker.c \ color.c \ - color_map.c \ compiler.h \ cone.c \ cube.c \ diff --git a/libdimension/color_map.c b/libdimension/color_map.c deleted file mode 100644 index 654fe97..0000000 --- a/libdimension/color_map.c +++ /dev/null @@ -1,98 +0,0 @@ -/************************************************************************* - * Copyright (C) 2010 Tavian Barnes * - * * - * This file is part of The Dimension Library. * - * * - * The Dimension Library is free software; you can redistribute it and/ * - * or modify it under the terms of the GNU Lesser General Public License * - * as published by the Free Software Foundation; either version 3 of the * - * License, or (at your option) any later version. * - * * - * The Dimension Library 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 * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this program. If not, see * - * . * - *************************************************************************/ - -/** - * @file - * Color-mapped pigment patterns. - */ - -#include "dimension.h" - -dmnsn_map * -dmnsn_new_color_map(void) -{ - return dmnsn_new_map(sizeof(dmnsn_color)); -} - -/** Payload for a color_map pigment. */ -typedef struct dmnsn_color_map_payload { - dmnsn_pattern *pattern; - dmnsn_map *map; - dmnsn_pigment_map_flags flags; -} dmnsn_color_map_payload; - -/** Free a color_map payload. */ -static void -dmnsn_delete_color_map_payload(void *ptr) -{ - dmnsn_color_map_payload *payload = ptr; - dmnsn_delete_map(payload->map); - dmnsn_delete_pattern(payload->pattern); - dmnsn_free(payload); -} - -/** color_map pigment callback. */ -static dmnsn_color -dmnsn_color_map_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v) -{ - const dmnsn_color_map_payload *payload = pigment->ptr; - double n; - dmnsn_color color1, color2; - dmnsn_evaluate_map(payload->map, dmnsn_pattern_value(payload->pattern, v), - &n, &color1, &color2); - - if (payload->flags == DMNSN_PIGMENT_MAP_SRGB) { - color1 = dmnsn_color_to_sRGB(color1); - color2 = dmnsn_color_to_sRGB(color2); - } - dmnsn_color ret = dmnsn_color_gradient(color1, color2, n); - if (payload->flags == DMNSN_PIGMENT_MAP_SRGB) { - ret = dmnsn_color_from_sRGB(ret); - } - - return ret; -} - -/** color_map initialization callback. */ -static void -dmnsn_color_map_initialize_fn(dmnsn_pigment *pigment) -{ - dmnsn_color_map_payload *payload = pigment->ptr; - dmnsn_initialize_pattern(payload->pattern); -} - -dmnsn_pigment * -dmnsn_new_color_map_pigment(dmnsn_pattern *pattern, dmnsn_map *map, - dmnsn_pigment_map_flags flags) -{ - dmnsn_pigment *pigment = dmnsn_new_pigment(); - - dmnsn_color_map_payload *payload - = dmnsn_malloc(sizeof(dmnsn_color_map_payload)); - payload->pattern = pattern; - payload->map = map; - payload->flags = flags; - - pigment->pigment_fn = dmnsn_color_map_pigment_fn; - pigment->initialize_fn = dmnsn_color_map_initialize_fn; - pigment->free_fn = dmnsn_delete_color_map_payload; - pigment->ptr = payload; - return pigment; -} diff --git a/libdimension/dimension/map.h b/libdimension/dimension/map.h index 80b48b6..b515518 100644 --- a/libdimension/dimension/map.h +++ b/libdimension/dimension/map.h @@ -20,7 +20,7 @@ /** * @file - * Generic maps (backend for color_maps, pigment_maps, etc.). + * Generic maps (backend for pigment_maps, etc.). */ /** A map. */ @@ -44,8 +44,8 @@ dmnsn_map *dmnsn_new_map(size_t size); void dmnsn_delete_map(dmnsn_map *map); /** - * Add an entry (a scalar-object pair) to a color map. - * @param[in,out] map The color map to add to. + * Add an entry (a scalar-object pair) to a map. + * @param[in,out] map The map to add to. * @param[in] n The index of the entry. * @param[in] obj The value of the entry. */ diff --git a/libdimension/dimension/pigments.h b/libdimension/dimension/pigments.h index 60dc658..56bba92 100644 --- a/libdimension/dimension/pigments.h +++ b/libdimension/dimension/pigments.h @@ -39,36 +39,28 @@ dmnsn_pigment *dmnsn_new_solid_pigment(dmnsn_color color); dmnsn_pigment *dmnsn_new_canvas_pigment(dmnsn_canvas *canvas); /** - * Construct a color map. - * @return An empty color map. - */ -dmnsn_map *dmnsn_new_color_map(void); - -/** - * pigment_map flags. + * Pigment map flags. */ typedef enum dmnsn_pigment_map_flags { DMNSN_PIGMENT_MAP_REGULAR, /**< Calculate linear color gradients. */ DMNSN_PIGMENT_MAP_SRGB /**< Calculate sRGB color gradients. */ } dmnsn_pigment_map_flags; -/** - * A color-mapped pigment. - * @param[in,out] pattern The pattern of the pigment. - * @param[in,out] map The color map to apply to the pattern. - * @param[in] flags Gradient flags - * @return A pigment mapping the pattern to color values. - */ -dmnsn_pigment *dmnsn_new_color_map_pigment(dmnsn_pattern *pattern, - dmnsn_map *map, - dmnsn_pigment_map_flags flags); - /** * Construct a pigment map. * @return An empty pigment map. */ dmnsn_map *dmnsn_new_pigment_map(void); +/** + * Add a raw color to a pigment map. + * Shorthand for creating a solid pigment and adding it manually. + * @param[in,out] map The pigment map to add to. + * @param[in] n The index of the entry. + * @param[in] color The value of the entry. + */ +void dmnsn_pigment_map_add_color(dmnsn_map *map, double n, dmnsn_color color); + /** * A pigment-mapped pigment. * @param[in,out] pattern The pattern of the pigment. diff --git a/libdimension/pigment_map.c b/libdimension/pigment_map.c index cf49649..4ffb129 100644 --- a/libdimension/pigment_map.c +++ b/libdimension/pigment_map.c @@ -56,6 +56,13 @@ typedef struct dmnsn_pigment_map_payload { dmnsn_pigment_map_flags flags; } dmnsn_pigment_map_payload; +void +dmnsn_pigment_map_add_color(dmnsn_map *map, double n, dmnsn_color color) +{ + dmnsn_pigment *pigment = dmnsn_new_solid_pigment(color); + dmnsn_add_map_entry(map, n, &pigment); +} + /** Free a pigment_map payload. */ static void dmnsn_delete_pigment_map_payload(void *ptr) diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c index a9d20d7..e9dff2a 100644 --- a/libdimension/tests/render.c +++ b/libdimension/tests/render.c @@ -22,15 +22,9 @@ #include #include -/* - * Test scene - */ -static dmnsn_scene * -dmnsn_new_test_scene(void) +static void +dmnsn_test_scene_set_defaults(dmnsn_scene *scene) { - /* Allocate a new scene */ - dmnsn_scene *scene = dmnsn_new_scene(); - /* Default texture */ scene->default_texture->pigment = dmnsn_new_solid_pigment(dmnsn_black); dmnsn_finish *default_finish = &scene->default_texture->finish; @@ -43,9 +37,19 @@ dmnsn_new_test_scene(void) ) ); - /* Allocate a canvas */ + /* Background color */ + scene->background = dmnsn_clear; +} + +static void +dmnsn_test_scene_add_canvas(dmnsn_scene *scene) +{ scene->canvas = dmnsn_new_canvas(768, 480); +} +static void +dmnsn_test_scene_add_camera(dmnsn_scene *scene) +{ /* Set up the transformation matrix for the perspective camera */ dmnsn_matrix trans = dmnsn_scale_matrix( dmnsn_new_vector( @@ -68,33 +72,38 @@ dmnsn_new_test_scene(void) /* Create a perspective camera */ scene->camera = dmnsn_new_perspective_camera(); scene->camera->trans = trans; +} - /* Background color */ - scene->background = dmnsn_clear; - - /* Sky sphere */ +static void +dmnsn_test_scene_add_sky_sphere(dmnsn_scene *scene) +{ scene->sky_sphere = dmnsn_new_sky_sphere(); dmnsn_pattern *sky_gradient = dmnsn_new_gradient_pattern(dmnsn_y); - dmnsn_map *sky_gradient_color_map = dmnsn_new_color_map(); - dmnsn_add_map_entry(sky_gradient_color_map, 0.0, &dmnsn_orange); + dmnsn_map *sky_gradient_pigment_map = dmnsn_new_pigment_map(); + dmnsn_pigment_map_add_color(sky_gradient_pigment_map, 0.0, dmnsn_orange); dmnsn_color background = dmnsn_color_from_sRGB( dmnsn_new_color5(0.0, 0.1, 0.2, 0.1, 0.0) ); - dmnsn_add_map_entry(sky_gradient_color_map, 0.35, &background); - dmnsn_pigment *sky_pigment - = dmnsn_new_color_map_pigment(sky_gradient, sky_gradient_color_map, + dmnsn_pigment_map_add_color(sky_gradient_pigment_map, 0.35, background); + dmnsn_pigment *sky_pigment = + dmnsn_new_pigment_map_pigment(sky_gradient, sky_gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB); dmnsn_array_push(scene->sky_sphere->pigments, &sky_pigment); +} - /* Light source */ +static void +dmnsn_test_scene_add_lights(dmnsn_scene *scene) +{ dmnsn_light *light = dmnsn_new_point_light( dmnsn_new_vector(-15.0, 20.0, 10.0), dmnsn_white ); dmnsn_array_push(scene->lights, &light); +} - /* Now make our objects */ - +static void +dmnsn_test_scene_add_hollow_cube(dmnsn_scene *scene) +{ dmnsn_object *cube = dmnsn_new_cube(); cube->trans = dmnsn_rotation_matrix( dmnsn_new_vector(dmnsn_radians(45.0), 0.0, 0.0) @@ -121,9 +130,13 @@ dmnsn_new_test_scene(void) sphere->texture->finish.specular = dmnsn_new_phong(0.2, 40.0); sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); - dmnsn_object *csg = dmnsn_new_csg_difference(cube, sphere); - dmnsn_array_push(scene->objects, &csg); + dmnsn_object *hollow_cube = dmnsn_new_csg_difference(cube, sphere); + dmnsn_array_push(scene->objects, &hollow_cube); +} +static void +dmnsn_test_scene_add_spike(dmnsn_scene *scene) +{ dmnsn_array *arrow_array = dmnsn_new_array(sizeof(dmnsn_object *)); dmnsn_object *cylinder = dmnsn_new_cone(0.1, 0.1, false); @@ -141,17 +154,17 @@ dmnsn_new_test_scene(void) dmnsn_object *arrow = dmnsn_new_csg_union(arrow_array); dmnsn_delete_array(arrow_array); dmnsn_pattern *gradient = dmnsn_new_gradient_pattern(dmnsn_y); - dmnsn_map *gradient_color_map = dmnsn_new_color_map(); - dmnsn_add_map_entry(gradient_color_map, 0.0, &dmnsn_red); - dmnsn_add_map_entry(gradient_color_map, 1.0/6.0, &dmnsn_orange); - dmnsn_add_map_entry(gradient_color_map, 2.0/6.0, &dmnsn_yellow); - dmnsn_add_map_entry(gradient_color_map, 3.0/6.0, &dmnsn_green); - dmnsn_add_map_entry(gradient_color_map, 4.0/6.0, &dmnsn_blue); - dmnsn_add_map_entry(gradient_color_map, 5.0/6.0, &dmnsn_magenta); - dmnsn_add_map_entry(gradient_color_map, 1.0, &dmnsn_red); + dmnsn_map *gradient_pigment_map = dmnsn_new_pigment_map(); + dmnsn_pigment_map_add_color(gradient_pigment_map, 0.0, dmnsn_red); + dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0/6.0, dmnsn_orange); + dmnsn_pigment_map_add_color(gradient_pigment_map, 2.0/6.0, dmnsn_yellow); + dmnsn_pigment_map_add_color(gradient_pigment_map, 3.0/6.0, dmnsn_green); + dmnsn_pigment_map_add_color(gradient_pigment_map, 4.0/6.0, dmnsn_blue); + dmnsn_pigment_map_add_color(gradient_pigment_map, 5.0/6.0, dmnsn_magenta); + dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0, dmnsn_red); arrow->texture = dmnsn_new_texture(); - arrow->texture->pigment - = dmnsn_new_color_map_pigment(gradient, gradient_color_map, + arrow->texture->pigment = + dmnsn_new_pigment_map_pigment(gradient, gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB); arrow->texture->trans = @@ -189,9 +202,11 @@ dmnsn_new_test_scene(void) dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0) ); dmnsn_array_push(scene->objects, &spike); +} - /* Triangle strip */ - +static void +dmnsn_test_scene_add_triangle_strip(dmnsn_scene *scene) +{ dmnsn_array *strip_array = dmnsn_new_array(sizeof(dmnsn_object *)); dmnsn_vector a = dmnsn_zero; dmnsn_vector b = dmnsn_new_vector(0.0, sqrt(3.0)/2.0, 0.5); @@ -222,32 +237,57 @@ dmnsn_new_test_scene(void) dmnsn_delete_array(strip_array); strip->trans = dmnsn_translation_matrix(dmnsn_new_vector(5.0, -2.0, -4.0)); dmnsn_array_push(scene->objects, &strip); +} +static void +dmnsn_test_scene_add_ground(dmnsn_scene *scene) +{ dmnsn_object *plane = dmnsn_new_plane(dmnsn_new_vector(0.0, 1.0, 0.0)); plane->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -2.0, 0.0)); dmnsn_pattern *checker1 = dmnsn_new_checker_pattern(); dmnsn_pattern *checker2 = dmnsn_new_checker_pattern(); - dmnsn_map *checker_color_map = dmnsn_new_color_map(); - dmnsn_add_map_entry(checker_color_map, 0.0, &dmnsn_black); - dmnsn_add_map_entry(checker_color_map, 1.0, &dmnsn_white); - dmnsn_pigment *pigment1 = dmnsn_new_solid_pigment(dmnsn_white); - dmnsn_pigment *pigment2 - = dmnsn_new_color_map_pigment(checker1, checker_color_map, + dmnsn_map *small_map = dmnsn_new_pigment_map(); + dmnsn_pigment_map_add_color(small_map, 0.0, dmnsn_black); + dmnsn_pigment_map_add_color(small_map, 1.0, dmnsn_white); + dmnsn_pigment *small_pigment = + dmnsn_new_pigment_map_pigment(checker1, small_map, DMNSN_PIGMENT_MAP_REGULAR); - pigment2->trans - = dmnsn_scale_matrix(dmnsn_new_vector(1.0/3.0, 1.0/3.0, 1.0/3.0)); - dmnsn_map *checker_pigment_map = dmnsn_new_pigment_map(); - dmnsn_add_map_entry(checker_pigment_map, 0.0, &pigment1); - dmnsn_add_map_entry(checker_pigment_map, 1.0, &pigment2); + small_pigment->trans = + dmnsn_scale_matrix(dmnsn_new_vector(1.0/3.0, 1.0/3.0, 1.0/3.0)); + dmnsn_map *big_map = dmnsn_new_pigment_map(); + dmnsn_pigment_map_add_color(big_map, 0.0, dmnsn_white); + dmnsn_add_map_entry(big_map, 1.0, &small_pigment); plane->texture = dmnsn_new_texture(); - plane->texture->pigment - = dmnsn_new_pigment_map_pigment(checker2, checker_pigment_map, - DMNSN_PIGMENT_MAP_REGULAR); + plane->texture->pigment = + dmnsn_new_pigment_map_pigment(checker2, big_map, DMNSN_PIGMENT_MAP_REGULAR); plane->texture->pigment->quick_color = dmnsn_color_from_sRGB( dmnsn_new_color(1.0, 0.5, 0.75) ); dmnsn_array_push(scene->objects, &plane); +} +static void +dmnsn_test_scene_add_objects(dmnsn_scene *scene) +{ + dmnsn_test_scene_add_hollow_cube(scene); + dmnsn_test_scene_add_spike(scene); + dmnsn_test_scene_add_triangle_strip(scene); + dmnsn_test_scene_add_ground(scene); +} + +/* + * Test scene + */ +static dmnsn_scene * +dmnsn_new_test_scene(void) +{ + dmnsn_scene *scene = dmnsn_new_scene(); + dmnsn_test_scene_set_defaults(scene); + dmnsn_test_scene_add_canvas(scene); + dmnsn_test_scene_add_camera(scene); + dmnsn_test_scene_add_sky_sphere(scene); + dmnsn_test_scene_add_lights(scene); + dmnsn_test_scene_add_objects(scene); return scene; } -- cgit v1.2.3