From 5138e55d3eb41255b1436d40ceba134876c3c5cd Mon Sep 17 00:00:00 2001
From: Tavian Barnes <tavianator@gmail.com>
Date: Mon, 9 Nov 2009 15:20:58 -0500
Subject: Add support for finishes (BRDFs).

---
 libdimension/Makefile.am          |  2 ++
 libdimension/dimension/finishes.h | 31 +++++++++++++++++++++++++++++
 libdimension/dimension/texture.h  | 27 ++++++++++++++++++++++++++
 libdimension/finishes.c           | 41 +++++++++++++++++++++++++++++++++++++++
 libdimension/scene.c              |  2 +-
 libdimension/texture.c            | 29 +++++++++++++++++++++++++--
 6 files changed, 129 insertions(+), 3 deletions(-)
 create mode 100644 libdimension/dimension/finishes.h
 create mode 100644 libdimension/finishes.c

diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am
index 15151d6..fb15f44 100644
--- a/libdimension/Makefile.am
+++ b/libdimension/Makefile.am
@@ -24,6 +24,7 @@ nobase_include_HEADERS = dimension.h                                           \
                          dimension/canvas.h                                    \
                          dimension/color.h                                     \
                          dimension/error.h                                     \
+                         dimension/finishes.h                                  \
                          dimension/geometry.h                                  \
                          dimension/gl.h                                        \
                          dimension/light.h                                     \
@@ -46,6 +47,7 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS)                            \
                           color.c                                              \
                           dimension_impl.h                                     \
                           error.c                                              \
+                          finishes.c                                           \
                           geometry.c                                           \
                           gl.c                                                 \
                           inlines.c                                            \
diff --git a/libdimension/dimension/finishes.h b/libdimension/dimension/finishes.h
new file mode 100644
index 0000000..da18a6f
--- /dev/null
+++ b/libdimension/dimension/finishes.h
@@ -0,0 +1,31 @@
+/*************************************************************************
+ * Copyright (C) 2009 Tavian Barnes <tavianator@gmail.com>               *
+ *                                                                       *
+ * 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                         *
+ * <http://www.gnu.org/licenses/>.                                       *
+ *************************************************************************/
+
+/*
+ * Custom finishes.
+ */
+
+#ifndef DIMENSION_FINISHES_H
+#define DIMENSION_FINISHES_H
+
+/* A specular finish */
+dmnsn_finish *dmnsn_new_specular_finish();
+
+#endif /* DIMENSION_FINISHES_H */
diff --git a/libdimension/dimension/texture.h b/libdimension/dimension/texture.h
index c26e840..4d0e2d7 100644
--- a/libdimension/dimension/texture.h
+++ b/libdimension/dimension/texture.h
@@ -49,12 +49,39 @@ struct dmnsn_pigment {
 dmnsn_pigment *dmnsn_new_pigment();
 void dmnsn_delete_pigment(dmnsn_pigment *pigment);
 
+/*
+ * Finishes
+ */
+
+/* Forward-declare dmnsn_finish */
+typedef struct dmnsn_finish dmnsn_finish;
+
+/* Finish callback */
+typedef dmnsn_color dmnsn_finish_fn(const dmnsn_finish *finish,
+                                    dmnsn_color color, dmnsn_vector x0,
+                                    dmnsn_vector normal,
+                                    dmnsn_vector reflected);
+
+/* dmnsn_finish definition */
+struct dmnsn_finish {
+  /* Callbacks */
+  dmnsn_finish_fn *finish_fn;
+  dmnsn_free_fn *free_fn;
+
+  /* Generic pointer */
+  void *ptr;
+};
+
+dmnsn_finish *dmnsn_new_finish();
+void dmnsn_delete_finish(dmnsn_finish *finish);
+
 /*
  * A complete texture
  */
 
 typedef struct {
   dmnsn_pigment *pigment;
+  dmnsn_finish  *finish;
 } dmnsn_texture;
 
 dmnsn_texture *dmnsn_new_texture();
diff --git a/libdimension/finishes.c b/libdimension/finishes.c
new file mode 100644
index 0000000..d78bc96
--- /dev/null
+++ b/libdimension/finishes.c
@@ -0,0 +1,41 @@
+/*************************************************************************
+ * Copyright (C) 2009 Tavian Barnes <tavianator@gmail.com>               *
+ *                                                                       *
+ * 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                         *
+ * <http://www.gnu.org/licenses/>.                                       *
+ *************************************************************************/
+
+#include "dimension.h"
+#include <stdlib.h> /* For malloc */
+
+static dmnsn_color
+dmnsn_specular_finish_fn(const dmnsn_finish *finish,
+                         dmnsn_color color, dmnsn_vector x0,
+                         dmnsn_vector normal, dmnsn_vector reflected)
+{
+  return dmnsn_color_mul(dmnsn_vector_dot(normal, reflected), color);
+}
+
+/* A specular finish */
+dmnsn_finish *
+dmnsn_new_specular_finish()
+{
+  dmnsn_finish *finish = dmnsn_new_finish();
+  if (finish) {
+    finish->finish_fn = &dmnsn_specular_finish_fn;
+  }
+  return finish;
+}
diff --git a/libdimension/scene.c b/libdimension/scene.c
index 65362a5..38b1d19 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -27,7 +27,7 @@ dmnsn_new_scene()
 {
   dmnsn_scene *scene = malloc(sizeof(dmnsn_scene));
   if (scene) {
-    scene->default_texture = NULL;
+    scene->default_texture = dmnsn_new_texture();
 
     scene->camera  = NULL;
     scene->canvas  = NULL;
diff --git a/libdimension/texture.c b/libdimension/texture.c
index 3867628..a9d7de1 100644
--- a/libdimension/texture.c
+++ b/libdimension/texture.c
@@ -21,7 +21,7 @@
 #include "dimension.h"
 #include <stdlib.h> /* For malloc */
 
-/* Allocate an dummy pigment */
+/* Allocate a dummy pigment */
 dmnsn_pigment *
 dmnsn_new_pigment()
 {
@@ -44,6 +44,29 @@ dmnsn_delete_pigment(dmnsn_pigment *pigment)
   }
 }
 
+/* Allocate a dummy finish */
+dmnsn_finish *
+dmnsn_new_finish()
+{
+  dmnsn_finish *finish = malloc(sizeof(dmnsn_finish));
+  if (finish) {
+    finish->free_fn = NULL;
+  }
+  return finish;
+}
+
+/* Free a finish */
+void
+dmnsn_delete_finish(dmnsn_finish *finish)
+{
+  if (finish) {
+    if (finish->free_fn) {
+      (*finish->free_fn)(finish->ptr);
+    }
+    free(finish);
+  }
+}
+
 /* Allocate a dummy texture */
 dmnsn_texture *
 dmnsn_new_texture()
@@ -51,15 +74,17 @@ dmnsn_new_texture()
   dmnsn_texture *texture = malloc(sizeof(dmnsn_texture));
   if (texture) {
     texture->pigment = NULL;
+    texture->finish = NULL;
   }
   return texture;
 }
 
-/* Free a dummy texture */
+/* Free a texture */
 void
 dmnsn_delete_texture(dmnsn_texture *texture)
 {
   if (texture) {
+    dmnsn_delete_finish(texture->finish);
     dmnsn_delete_pigment(texture->pigment);
     free(texture);
   }
-- 
cgit v1.2.3