summaryrefslogtreecommitdiffstats
path: root/libdimension/phong.c
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension/phong.c')
-rw-r--r--libdimension/phong.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/libdimension/phong.c b/libdimension/phong.c
index 2bfbe90..bbf521d 100644
--- a/libdimension/phong.c
+++ b/libdimension/phong.c
@@ -1,5 +1,5 @@
/*************************************************************************
- * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> *
+ * Copyright (C) 2010-2014 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Library. *
* *
@@ -26,6 +26,13 @@
#include "dimension.h"
#include <stdlib.h>
+/** Phone specular type. */
+typedef struct dmnsn_phong {
+ dmnsn_specular specular;
+ double coeff;
+ double exp;
+} dmnsn_phong;
+
/** Phong specular highlight callback. */
static dmnsn_color
dmnsn_phong_specular_fn(const dmnsn_specular *specular,
@@ -33,10 +40,7 @@ dmnsn_phong_specular_fn(const dmnsn_specular *specular,
dmnsn_vector ray, dmnsn_vector normal,
dmnsn_vector viewer)
{
- double *params = specular->ptr;
-
- double coeff = params[0];
- double exp = params[1];
+ const dmnsn_phong *phong = (const dmnsn_phong *)specular;
dmnsn_vector proj = dmnsn_vector_mul(2*dmnsn_vector_dot(ray, normal), normal);
dmnsn_vector reflected = dmnsn_vector_sub(proj, ray);
@@ -46,23 +50,20 @@ dmnsn_phong_specular_fn(const dmnsn_specular *specular,
return dmnsn_black;
}
- specular_factor = pow(specular_factor, exp);
- return dmnsn_color_mul(coeff*specular_factor, light);
+ specular_factor = pow(specular_factor, phong->exp);
+ return dmnsn_color_mul(phong->coeff*specular_factor, light);
}
/* A phong finish */
dmnsn_specular *
-dmnsn_new_phong(double specular, double exp)
+dmnsn_new_phong(double coeff, double exp)
{
- dmnsn_specular *phong = dmnsn_new_specular();
-
- double *params = dmnsn_malloc(2*sizeof(double));
- params[0] = specular;
- params[1] = exp;
-
- phong->specular_fn = dmnsn_phong_specular_fn;
- phong->free_fn = dmnsn_free;
- phong->ptr = params;
+ dmnsn_phong *phong = DMNSN_MALLOC(dmnsn_phong);
+ phong->coeff = coeff;
+ phong->exp = exp;
- return phong;
+ dmnsn_specular *specular = &phong->specular;
+ dmnsn_init_specular(specular);
+ specular->specular_fn = dmnsn_phong_specular_fn;
+ return specular;
}