From e7c3459f8eba5db5704632f927a4168467521b88 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 9 Nov 2009 10:53:41 -0500 Subject: Add support for lights. --- libdimension/Makefile.am | 6 ++++- libdimension/dimension.h | 2 ++ libdimension/dimension/light.h | 47 +++++++++++++++++++++++++++++++++++++ libdimension/dimension/lights.h | 30 ++++++++++++++++++++++++ libdimension/light.c | 47 +++++++++++++++++++++++++++++++++++++ libdimension/lights.c | 52 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 libdimension/dimension/light.h create mode 100644 libdimension/dimension/lights.h create mode 100644 libdimension/light.c create mode 100644 libdimension/lights.c diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index f11f119..15151d6 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -26,6 +26,8 @@ nobase_include_HEADERS = dimension.h \ dimension/error.h \ dimension/geometry.h \ dimension/gl.h \ + dimension/light.h \ + dimension/lights.h \ dimension/object.h \ dimension/objects.h \ dimension/pigments.h \ @@ -38,17 +40,19 @@ nobase_include_HEADERS = dimension.h \ lib_LTLIBRARIES = libdimension.la libdimension_la_SOURCES = $(nobase_include_HEADERS) \ - dimension_impl.h \ camera.c \ cameras.c \ canvas.c \ color.c \ + dimension_impl.h \ error.c \ geometry.c \ gl.c \ inlines.c \ kD_splay_tree.c \ kD_splay_tree.h \ + light.c \ + lights.c \ object.c \ objects.c \ pigments.c \ diff --git a/libdimension/dimension.h b/libdimension/dimension.h index 4cc29ab..7fc25c5 100644 --- a/libdimension/dimension.h +++ b/libdimension/dimension.h @@ -72,6 +72,8 @@ typedef void dmnsn_free_fn(void *ptr); #include #include #include +#include +#include #include #include #include diff --git a/libdimension/dimension/light.h b/libdimension/dimension/light.h new file mode 100644 index 0000000..089af89 --- /dev/null +++ b/libdimension/dimension/light.h @@ -0,0 +1,47 @@ +/************************************************************************* + * Copyright (C) 2009 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 * + * . * + *************************************************************************/ + +/* + * Lights. + */ + +#ifndef DIMENSION_LIGHT_H +#define DIMENSION_LIGHT_H + +typedef struct dmnsn_light dmnsn_light; + +typedef dmnsn_color dmnsn_light_fn(const dmnsn_light *light, dmnsn_vector v); + +struct dmnsn_light { + /* Origin of light rays */ + dmnsn_vector x0; + + /* Callbacks */ + dmnsn_light_fn *light_fn; + dmnsn_free_fn *free_fn; + + /* Generic pointer for light info */ + void *ptr; +}; + +dmnsn_light *dmnsn_new_light(); +void dmnsn_delete_light(dmnsn_light *light); + +#endif /* DIMENSION_LIGHT_H */ diff --git a/libdimension/dimension/lights.h b/libdimension/dimension/lights.h new file mode 100644 index 0000000..d12179a --- /dev/null +++ b/libdimension/dimension/lights.h @@ -0,0 +1,30 @@ +/************************************************************************* + * Copyright (C) 2009 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 * + * . * + *************************************************************************/ + +/* + * Types of lights. + */ + +#ifndef DIMENSION_LIGHTS_H +#define DIMENSION_LIGHTS_H + +dmnsn_light *dmnsn_new_point_light(dmnsn_vector x0, dmnsn_color color); + +#endif /* DIMENSION_LIGHTS_H */ diff --git a/libdimension/light.c b/libdimension/light.c new file mode 100644 index 0000000..5f0bcfb --- /dev/null +++ b/libdimension/light.c @@ -0,0 +1,47 @@ +/************************************************************************* + * Copyright (C) 2009 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 * + * . * + *************************************************************************/ + +#include "dimension.h" +#include /* For malloc */ + +/* Allocate a new dummy light */ +dmnsn_light * +dmnsn_new_light() +{ + dmnsn_light *light = malloc(sizeof(dmnsn_light)); + if (light) { + light->light_fn = NULL; + light->free_fn = NULL; + light->ptr = NULL; + } + return light; +} + +/* Free a dummy light */ +void +dmnsn_delete_light(dmnsn_light *light) +{ + if (light) { + if (light->free_fn) { + (*light->free_fn)(light->ptr); + } + free(light); + } +} diff --git a/libdimension/lights.c b/libdimension/lights.c new file mode 100644 index 0000000..9862506 --- /dev/null +++ b/libdimension/lights.c @@ -0,0 +1,52 @@ +/************************************************************************* + * Copyright (C) 2009 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 * + * . * + *************************************************************************/ + +#include "dimension.h" + +/* + * Point light source + */ + +static dmnsn_color +dmnsn_point_light_fn(const dmnsn_light *light, dmnsn_vector v) +{ + return *(dmnsn_color *)light->ptr; +} + +dmnsn_light * +dmnsn_new_point_light(dmnsn_vector x0, dmnsn_color color) +{ + dmnsn_light *light = dmnsn_new_light(); + if (light) { + /* Allocate room for the transformation matrix */ + dmnsn_color *ptr = malloc(sizeof(dmnsn_color)); + if (!ptr) { + dmnsn_delete_light(light); + return NULL; + } + *ptr = color; + + light->x0 = x0; + light->light_fn = &dmnsn_point_light_fn; + light->free_fn = &free; + light->ptr = ptr; + } + return light; +} -- cgit v1.2.3