BRL-CAD
sh_xxx.c
Go to the documentation of this file.
1 /* S H _ X X X . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-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 /** @file liboptical/sh_xxx.c
21  *
22  * To add a new shader to the "rt" program's LIBOPTICAL library:
23  *
24  * 1) Copy this file to sh_shadername.c
25  *
26  * 2) edit sh_shadername.c:
27  * change "X X X" to "S H A D E R N A M E"
28  * change "xxx" to "shadername"
29  * Set a new number for the xxx_MAGIC define
30  * define shader specific structure and defaults
31  * edit/build parse table for bu_structparse from xxx_parse
32  * edit/build shader_mfuncs tables from xxx_mfuncs for
33  * each shader name being built.
34  * edit the xxx_setup function to do shader-specific setup
35  * edit the xxx_render function to do the actual rendering
36  *
37  * If you are building a dynamically loaded shader, compile this into a
38  * shared library called "shadername.so". If you have a number of shaders
39  * for you are adding, you can create a single library called "shaders.so"
40  * which contains all of your DSO shaders.
41  *
42  * RT will look in the following locations for DSO shaders:
43  * ./
44  * $prefix/lib/
45  * $LD_LIBRARY_PATH
46  *
47  * If you are adding the shader to "rt" as a permanent shader, then the
48  * following steps are necessary:
49  *
50  * 3) Edit init.c to add extern for xxx_mfuncs and a call to
51  * mlib_add_shader().
52  *
53  * 4) Edit CMakeLists.txt to add shader file to the compilation
54  *
55  * 5) replace this list with a description of the shader, its
56  * parameters and use.
57  *
58  * 6) Edit shaders.tcl and comb.tcl in the ../tclscripts/mged
59  * directory to add a new gui for this shader.
60  */
61 
62 #include "common.h"
63 
64 #include <stdlib.h>
65 #include <stddef.h>
66 #include <stdio.h>
67 #include <math.h>
68 #include <string.h>
69 
70 #include "bu/units.h"
71 #include "vmath.h"
72 #include "raytrace.h"
73 #include "optical.h"
74 
75 
76 #define xxx_MAGIC 0x1834 /* make this a unique number for each shader */
77 #define CK_xxx_SP(_p) BU_CKMAG(_p, xxx_MAGIC, "xxx_specific")
78 
79 /*
80  * the shader specific structure contains all variables which are unique
81  * to any particular use of the shader.
82  */
83 struct xxx_specific {
84  uint32_t magic; /* magic # for memory validity check, must come 1st */
85  double xxx_val; /* variables for shader ... */
86  double xxx_dist;
87  vect_t xxx_delta;
88  point_t xxx_min;
89  point_t xxx_max;
90  mat_t xxx_m_to_sh; /* model to shader space matrix */
91  mat_t xxx_m_to_r; /* model to shader space matrix */
92 };
93 
94 
95 /* The default values for the variables in the shader specific structure */
96 static const
97 struct xxx_specific xxx_defaults = {
98  xxx_MAGIC,
99  1.0, /* xxx_val */
100  0.0, /* xxx_dist */
101  VINITALL(1.0), /* xxx_delta */
102  VINIT_ZERO, /* xxx_min */
103  VINIT_ZERO, /* xxx_max */
104  MAT_INIT_ZERO, /* xxx_m_to_sh */
105  MAT_INIT_ZERO /* xxx_m_to_r */
106 };
107 
108 
109 #define SHDR_NULL ((struct xxx_specific *)0)
110 #define SHDR_O(m) bu_offsetof(struct xxx_specific, m)
111 
112 
113 /* description of how to parse/print the arguments to the shader
114  * There is at least one line here for each variable in the shader specific
115  * structure above
116  */
118  {"%g", 1, "val", SHDR_O(xxx_val), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
119  {"%g", 1, "dist", SHDR_O(xxx_dist), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
120  {"%f", 3, "delta", SHDR_O(xxx_delta), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
121  {"%f", 3, "max", SHDR_O(xxx_max), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
122  {"%f", 3, "min", SHDR_O(xxx_min), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
123  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
124 
125 };
127  {"%p", 1, "xxx_print_tab", bu_byteoffset(xxx_print_tab[0]), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
128  {"%g", 1, "v", SHDR_O(xxx_val), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
129  {"%g", 1, "dist", SHDR_O(xxx_dist), bu_mm_cvt, NULL, NULL },
130  {"%f", 3, "d", SHDR_O(xxx_delta), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
131  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
132 };
133 
134 
135 HIDDEN int xxx_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
136 HIDDEN int xxx_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp);
137 HIDDEN void xxx_print(register struct region *rp, void *dp);
138 HIDDEN void xxx_free(void *cp);
139 
140 /* The "mfuncs" structure defines the external interface to the shader.
141  * Note that more than one shader "name" can be associated with a given
142  * shader by defining more than one mfuncs struct in this array.
143  * See sh_phong.c for an example of building more than one shader "name"
144  * from a set of source functions. There you will find that "glass" "mirror"
145  * and "plastic" are all names for the same shader with different default
146  * values for the parameters.
147  */
148 struct mfuncs xxx_mfuncs[] = {
149  {MF_MAGIC, "xxx", 0, MFI_NORMAL|MFI_HIT|MFI_UV, 0, xxx_setup, xxx_render, xxx_print, xxx_free },
150  {0, (char *)0, 0, 0, 0, 0, 0, 0, 0 }
151 };
152 
153 
154 /*
155  * This routine is called (at prep time)
156  * once for each region which uses this shader.
157  * Any shader-specific initialization should be done here.
158  *
159  * Returns:
160  * 1 success
161  * 0 success, but delete region
162  * -1 failure
163  */
164 HIDDEN int
165 xxx_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *rtip)
166 /* pointer to reg_udata in *rp */
167 /* New since 4.4 release */
168 {
169  register struct xxx_specific *xxx_sp;
170 
171  /* check the arguments */
172  RT_CHECK_RTI(rtip);
173  BU_CK_VLS(matparm);
174  RT_CK_REGION(rp);
175 
176 
177  if (rdebug&RDEBUG_SHADE)
178  bu_log("xxx_setup(%s)\n", rp->reg_name);
179 
180  /* Get memory for the shader parameters and shader-specific data */
181  BU_GET(xxx_sp, struct xxx_specific);
182  *dpp = xxx_sp;
183 
184  /* initialize the default values for the shader */
185  memcpy(xxx_sp, &xxx_defaults, sizeof(struct xxx_specific));
186 
187  /* parse the user's arguments for this use of the shader. */
188  if (bu_struct_parse(matparm, xxx_parse_tab, (char *)xxx_sp, NULL) < 0)
189  return -1;
190 
191  /* Optional:
192  *
193  * If the shader needs to operate in a coordinate system which stays
194  * fixed on the region when the region is moved (as in animation)
195  * we need to get a matrix to perform the appropriate transform(s).
196  *
197  * rt_shader_mat() returns a matrix which maps points on/in the
198  * region into the unit cube. This unit cube is formed by first
199  * mapping from world coordinates into "region coordinates" (the
200  * coordinate system in which the region is defined). Then the
201  * bounding box of the region is used to establish a mapping to
202  * the unit cube
203  *
204  * rt_shader_mat(xxx_sp->xxx_m_to_sh, rtip, rp, xxx_sp->xxx_min,
205  * xxx_sp->xxx_max);
206  *
207  * Alternatively, shading may be done in "region coordinates"
208  * if desired:
209  *
210  * db_region_mat(xxx_sp->xxx_m_to_r, rtip->rti_dbip, rp->reg_name, &rt_uniresource);
211  *
212  */
213 
214  if (rdebug&RDEBUG_SHADE) {
215  bu_struct_print(" Parameters:", xxx_print_tab, (char *)xxx_sp);
216  bn_mat_print("m_to_sh", xxx_sp->xxx_m_to_sh);
217  }
218 
219  return 1;
220 }
221 
222 
223 HIDDEN void
224 xxx_print(register struct region *rp, void *dp)
225 {
226  bu_struct_print(rp->reg_name, xxx_print_tab, (char *)dp);
227 }
228 
229 
230 HIDDEN void
231 xxx_free(void *cp)
232 {
233  BU_PUT(cp, struct xxx_specific);
234 }
235 
236 
237 /*
238  * This is called (from viewshade() in shade.c) once for each hit point
239  * to be shaded. The purpose here is to fill in values in the shadework
240  * structure.
241  */
242 int
243 xxx_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
244 /* defined in ../h/shadework.h */
245 /* ptr to the shader-specific struct */
246 {
247  register struct xxx_specific *xxx_sp =
248  (struct xxx_specific *)dp;
249  point_t pt;
250  VSETALL(pt, 0);
251 
252  /* check the validity of the arguments we got */
253  RT_AP_CHECK(ap);
254  RT_CHECK_PT(pp);
255  CK_xxx_SP(xxx_sp);
256 
257  if (rdebug&RDEBUG_SHADE)
258  bu_struct_print("xxx_render Parameters:", xxx_print_tab, (char *)xxx_sp);
259 
260  /* If we are performing the shading in "region" space, we must
261  * transform the hit point from "model" space to "region" space.
262  * See the call to db_region_mat in xxx_setup().
263  MAT4X3PNT(pt, xxx_sp->xxx_m_to_sh, swp->sw_hit.hit_point);
264  MAT4X3PNT(pt, xxx_sp->xxx_m_to_r, swp->sw_hit.hit_point);
265 
266  if (rdebug&RDEBUG_SHADE) {
267  bu_log("xxx_render() model:(%g %g %g) shader:(%g %g %g)\n",
268  V3ARGS(swp->sw_hit.hit_point),
269  V3ARGS(pt));
270  }
271  */
272 
273  /* XXX perform shading operations here */
274  VMOVE(swp->sw_color, pt);
275 
276  /* shader must perform transmission/reflection calculations
277  *
278  * 0 < swp->sw_transmit <= 1 causes transmission computations
279  * 0 < swp->sw_reflect <= 1 causes reflection computations
280  */
281  if (swp->sw_reflect > 0 || swp->sw_transmit > 0)
282  (void)rr_render(ap, pp, swp);
283 
284  return 1;
285 }
286 
287 
288 /*
289  * Local Variables:
290  * mode: C
291  * tab-width: 8
292  * indent-tabs-mode: t
293  * c-file-style: "stroustrup"
294  * End:
295  * ex: shiftwidth=4 tabstop=8
296  */
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
struct bu_structparse xxx_print_tab[]
Definition: sh_xxx.c:117
double xxx_dist
Definition: sh_xxx.c:86
#define CK_xxx_SP(_p)
Definition: sh_xxx.c:77
#define VSETALL(a, s)
Definition: color.c:54
HIDDEN void xxx_print(register struct region *rp, void *dp)
Definition: sh_xxx.c:224
HIDDEN void xxx_free(void *cp)
Definition: sh_xxx.c:231
Header file for the BRL-CAD common definitions.
const char * reg_name
Identifying string.
Definition: raytrace.h:539
void bu_mm_cvt(const struct bu_structparse *sdp, const char *name, void *base, const char *value, void *data)
Definition: units.c:386
#define RT_CK_REGION(_p)
Definition: raytrace.h:559
#define HIDDEN
Definition: common.h:86
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 mfuncs xxx_mfuncs[]
Definition: sh_xxx.c:148
#define BU_CK_VLS(_vp)
Definition: vls.h:69
void bn_mat_print(const char *title, const mat_t m)
Definition: mat.c:81
#define RT_CHECK_RTI(_p)
Definition: raytrace.h:1832
point_t xxx_max
Definition: sh_xxx.c:89
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
vect_t xxx_delta
Definition: sh_xxx.c:87
#define UNUSED(parameter)
Definition: common.h:239
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
#define BU_STRUCTPARSE_FUNC_NULL
Definition: parse.h:153
mat_t xxx_m_to_r
Definition: sh_xxx.c:91
mat_t xxx_m_to_sh
Definition: sh_xxx.c:90
uint32_t magic
Definition: sh_xxx.c:84
#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
#define xxx_MAGIC
Definition: sh_xxx.c:76
point_t xxx_min
Definition: sh_xxx.c:88
int rr_render(struct application *app, const struct partition *pp, struct shadework *swp)
struct bu_structparse xxx_parse_tab[]
Definition: sh_xxx.c:126
HIDDEN int xxx_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
Definition: sh_xxx.c:243
#define RT_CHECK_PT(_p)
compat
Definition: raytrace.h:588
Definition: vls.h:56
#define SHDR_O(m)
Definition: sh_xxx.c:110
Header file for the BRL-CAD Optical Library, LIBOPTICAL.
HIDDEN int xxx_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip)
Definition: sh_xxx.c:165
int rdebug
Definition: init.c:39
double xxx_val
Definition: sh_xxx.c:85