BRL-CAD
sh_toon.c
Go to the documentation of this file.
1 /* S H _ T O O N . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2010-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 /** @file liboptical/sh_toon.c
22  *
23  * uses simple binning based on the cosine between the ray and the vector from
24  * the hitpoint to the light. Might be improved by using hit location curvature
25  * and/or cosine between ray dir and hit point normal?
26  */
27 
28 #include "common.h"
29 
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <math.h>
34 #include <string.h>
35 
36 #include "vmath.h"
37 #include "raytrace.h"
38 #include "optical.h"
39 #include "light.h"
40 
41 
42 #define TOON_MAGIC 0x746F6F6E /* make this a unique number for each shader */
43 #define CK_TOON_SP(_p) BU_CKMAG(_p, TOON_MAGIC, "toon_specific")
44 
45 /*
46  * the shader specific structure contains all variables which are unique
47  * to any particular use of the shader.
48  */
49 struct toon_specific {
50  uint32_t magic; /* magic # for memory validity check, must come 1st */
51 };
52 
53 
54 /* The default values for the variables in the shader specific structure */
55 static const
56 struct toon_specific toon_defaults = {
58 };
59 
60 
61 #define SHDR_NULL ((struct toon_specific *)0)
62 #define SHDR_O(m) bu_offsetof(struct toon_specific, m)
63 
64 /* description of how to parse/print the arguments to the shader
65  * There is at least one line here for each variable in the shader specific
66  * structure above
67  */
69  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
70 
71 };
73  {"%p", 1, "toon_print_tab", bu_byteoffset(toon_print_tab[0]), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
74  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
75 };
76 
77 
78 HIDDEN int toon_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
79 HIDDEN int toon_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp);
80 HIDDEN void toon_print(register struct region *rp, void *dp);
81 HIDDEN void toon_free(void *cp);
82 
83 /* The "mfuncs" structure defines the external interface to the shader.
84  * Note that more than one shader "name" can be associated with a given
85  * shader by defining more than one mfuncs struct in this array.
86  * See sh_phong.c for an example of building more than one shader "name"
87  * from a set of source functions. There you will find that "glass" "mirror"
88  * and "plastic" are all names for the same shader with different default
89  * values for the parameters.
90  */
91 struct mfuncs toon_mfuncs[] = {
92  {MF_MAGIC, "toon", 0, MFI_NORMAL|MFI_HIT, 0,
93  toon_setup, toon_render, toon_print, toon_free },
94 
95  {0, (char *)0, 0, 0, 0,
96  0, 0, 0, 0 }
97 };
98 
99 
100 /*
101  * This routine is called (at prep time)
102  * once for each region which uses this shader.
103  * Any shader-specific initialization should be done here.
104  *
105  * Returns:
106  * 1 success
107  * 0 success, but delete region
108  * -1 failure
109  */
110 HIDDEN int
111 toon_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *rtip)
112 
113 
114 /* pointer to reg_udata in *rp */
115 
116 /* New since 4.4 release */
117 {
118  register struct toon_specific *toon_sp;
119 
120  /* check the arguments */
121  RT_CHECK_RTI(rtip);
122  BU_CK_VLS(matparm);
123  RT_CK_REGION(rp);
124 
125  if (rdebug&RDEBUG_SHADE)
126  bu_log("toon_setup(%s)\n", rp->reg_name);
127 
128  /* Get memory for the shader parameters and shader-specific data */
129  BU_GET(toon_sp, struct toon_specific);
130  *dpp = toon_sp;
131 
132  /* initialize the default values for the shader */
133  memcpy(toon_sp, &toon_defaults, sizeof(struct toon_specific));
134 
135  /* parse the user's arguments for this use of the shader. */
136  if (bu_struct_parse(matparm, toon_parse_tab, (char *)toon_sp, NULL) < 0)
137  return -1;
138 
139  if (rdebug&RDEBUG_SHADE) {
140  bu_struct_print(" Parameters:", toon_print_tab, (char *)toon_sp);
141  }
142 
143  return 1;
144 }
145 
146 
147 HIDDEN void
148 toon_print(register struct region *rp, void *dp)
149 {
150  bu_struct_print(rp->reg_name, toon_print_tab, (char *)dp);
151 }
152 
153 
154 HIDDEN void
155 toon_free(void *cp)
156 {
157  BU_PUT(cp, struct toon_specific);
158 }
159 
160 
161 /*
162  * This is called (from viewshade() in shade.c) once for each hit point
163  * to be shaded. The purpose here is to fill in values in the shadework
164  * structure.
165  */
166 int
167 toon_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
168 {
169  int i;
170  struct toon_specific *toon_sp = (struct toon_specific *)dp;
171  struct light_specific *lp;
172  fastf_t cosi, scale;
173 
174  /* check the validity of the arguments we got */
175  RT_AP_CHECK(ap);
176  RT_CHECK_PT(pp);
177  CK_TOON_SP(toon_sp);
178 
179  if (rdebug&RDEBUG_SHADE)
180  bu_struct_print("toon_render Parameters:", toon_print_tab, (char *)toon_sp);
181 
182  /* if surface normal is nearly orthogonal to the ray, make a black line */
183  if (VDOT(swp->sw_hit.hit_normal, ap->a_inv_dir) >= 0.8) {
184  VSETALL(swp->sw_color, 0);
185  return 1;
186  }
187 
188  /* probably need to set some swp values here to avoid the infinite recursion
189  * if specified lights exist. */
190  light_obs(ap, swp, MFI_HIT);
191 
192  /* Consider effects of each light source */
193  for (i=ap->a_rt_i->rti_nlights-1; i>=0; i--) {
194  if ((lp = (struct light_specific *)swp->sw_visible[i]) == LIGHT_NULL)
195  continue;
196 
197  cosi = VDOT(swp->sw_hit.hit_normal, swp->sw_tolight);
198  if (cosi <= 0.0) scale = 0.0;
199  else if (cosi <= 0.5) scale = 0.5;
200  else if (cosi <= 0.8) scale = 0.8;
201  else scale = 1.0;
202  VSCALE(swp->sw_color, swp->sw_color, scale);
203  return 1;
204  }
205 
206  /* no paths to light source, so just paint it black */
207  VSETALL(swp->sw_color, 0);
208 
209  if (swp->sw_reflect > 0 || swp->sw_transmit > 0)
210  (void)rr_render(ap, pp, swp);
211  return 1;
212 }
213 
214 
215 /*
216  * Local Variables:
217  * mode: C
218  * tab-width: 8
219  * indent-tabs-mode: t
220  * c-file-style: "stroustrup"
221  * End:
222  * ex: shiftwidth=4 tabstop=8
223  */
struct bu_structparse toon_print_tab[]
Definition: sh_toon.c:68
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define bu_byteoffset(_i)
Definition: parse.h:81
#define MF_MAGIC
Definition: magic.h:205
#define VSETALL(a, s)
Definition: color.c:54
HIDDEN int toon_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
Definition: sh_toon.c:167
HIDDEN void toon_print(register struct region *rp, void *dp)
Definition: sh_toon.c:148
#define TOON_MAGIC
Definition: sh_toon.c:42
HIDDEN void toon_free(void *cp)
Definition: sh_toon.c:155
Header file for the BRL-CAD common definitions.
const char * reg_name
Identifying string.
Definition: raytrace.h:539
struct mfuncs toon_mfuncs[]
Definition: sh_toon.c:91
#define RT_CK_REGION(_p)
Definition: raytrace.h:559
#define HIDDEN
Definition: common.h:86
void light_obs(struct application *ap, struct shadework *swp, int have)
Definition: sh_light.c:1621
void bu_struct_print(const char *title, const struct bu_structparse *parsetab, const char *base)
Definition: parse.c:1221
#define RT_AP_CHECK(_ap)
Definition: raytrace.h:1685
struct rt_i * a_rt_i
this librt instance
Definition: raytrace.h:1588
#define BU_CK_VLS(_vp)
Definition: vls.h:69
vect_t a_inv_dir
filled in by rt_shootray(), inverse of ray direction cosines
Definition: raytrace.h:1614
int rti_nlights
number of light sources
Definition: raytrace.h:1760
#define RT_CHECK_RTI(_p)
Definition: raytrace.h:1832
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define UNUSED(parameter)
Definition: common.h:239
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
HIDDEN int toon_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip)
Definition: sh_toon.c:111
uint32_t magic
Definition: sh_toon.c:50
#define CK_TOON_SP(_p)
Definition: sh_toon.c:43
#define BU_STRUCTPARSE_FUNC_NULL
Definition: parse.h:153
#define RDEBUG_SHADE
Definition: optical.h:130
int bu_struct_parse(const struct bu_vls *in_vls, const struct bu_structparse *desc, const char *base, void *data)
Definition: parse.c:878
struct bu_structparse toon_parse_tab[]
Definition: sh_toon.c:72
int rr_render(struct application *app, const struct partition *pp, struct shadework *swp)
#define RT_CHECK_PT(_p)
compat
Definition: raytrace.h:588
Definition: vls.h:56
double fastf_t
Definition: defines.h:300
Header file for the BRL-CAD Optical Library, LIBOPTICAL.
int rdebug
Definition: init.c:39