BRL-CAD
sh_fbm.c
Go to the documentation of this file.
1 /* S H _ F B M . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1997-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_fbm.c
21  *
22  */
23 
24 #include "common.h"
25 
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <math.h>
30 #include <string.h>
31 
32 #include "vmath.h"
33 #include "raytrace.h"
34 #include "optical.h"
35 
36 
37 struct fbm_specific {
38  double lacunarity;
39  double h_val;
40  double octaves;
41  double offset;
42  double gain;
43  double distortion;
44  point_t scale; /* scale coordinate space */
45 };
46 
47 
48 static struct fbm_specific fbm_defaults = {
49  2.1753974, /* lacunarity */
50  1.0, /* h_val */
51  4, /* octaves */
52  0.0, /* offset */
53  0.0, /* gain */
54  1.0, /* distortion */
55  VINITALL(1.0) /* scale */
56 };
57 
58 
59 #define FBM_NULL ((struct fbm_specific *)0)
60 #define FBM_O(m) bu_offsetof(struct fbm_specific, m)
61 
63  {"%g", 1, "lacunarity", FBM_O(lacunarity), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
64  {"%g", 1, "H", FBM_O(h_val), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
65  {"%g", 1, "octaves", FBM_O(octaves), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
66  {"%g", 1, "gain", FBM_O(gain), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
67  {"%g", 1, "distortion", FBM_O(distortion), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
68  {"%g", 1, "l", FBM_O(lacunarity), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
69  {"%d", 1, "o", FBM_O(octaves), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
70  {"%g", 1, "g", FBM_O(gain), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
71  {"%g", 1, "d", FBM_O(distortion), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
72  {"%f", 3, "scale", FBM_O(scale), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
73  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
74 };
75 
76 
77 HIDDEN int fbm_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
78 HIDDEN int fbm_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp);
79 HIDDEN void fbm_print(register struct region *rp, void *dp);
80 HIDDEN void fbm_free(void *cp);
81 
82 struct mfuncs fbm_mfuncs[] = {
83  {MF_MAGIC, "bump_fbm", 0, MFI_NORMAL|MFI_HIT|MFI_UV, 0,
84  fbm_setup, fbm_render, fbm_print, fbm_free },
85 
86  {0, (char*)0, 0, 0, 0,
87  0, 0, 0, 0}
88 };
89 
90 
91 HIDDEN int
92 fbm_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *UNUSED(rtip))
93 {
94  register struct fbm_specific *fbm;
95 
96  BU_CK_VLS(matparm);
97  BU_GET(fbm, struct fbm_specific);
98  *dpp = fbm;
99 
100  memcpy(fbm, &fbm_defaults, sizeof(struct fbm_specific));
101  if (rdebug&RDEBUG_SHADE)
102  bu_log("fbm_setup\n");
103 
104  if (bu_struct_parse(matparm, fbm_parse, (char *)fbm, NULL) < 0)
105  return -1;
106 
107  if (rdebug&RDEBUG_SHADE)
108  bu_struct_print(rp->reg_name, fbm_parse, (char *)fbm);
109 
110  return 1;
111 }
112 
113 
114 HIDDEN void
115 fbm_print(register struct region *rp, void *dp)
116 {
117  bu_struct_print(rp->reg_name, fbm_parse, (char *)dp);
118 }
119 
120 
121 HIDDEN void
122 fbm_free(void *cp)
123 {
124  BU_PUT(cp, struct fbm_specific);
125 }
126 
127 
128 int
129 fbm_render(struct application *UNUSED(ap), const struct partition *UNUSED(pp), struct shadework *swp, void *dp)
130 {
131  register struct fbm_specific *fbm_sp =
132  (struct fbm_specific *)dp;
133  vect_t v_noise;
134  point_t pt;
135 
136  if (rdebug&RDEBUG_SHADE)
137  bu_struct_print("foo", fbm_parse, (char *)fbm_sp);
138 
139  pt[0] = swp->sw_hit.hit_point[0] * fbm_sp->scale[0];
140  pt[1] = swp->sw_hit.hit_point[1] * fbm_sp->scale[1];
141  pt[2] = swp->sw_hit.hit_point[2] * fbm_sp->scale[2];
142 
143  bn_noise_vec(pt, v_noise);
144 
145  VSCALE(v_noise, v_noise, fbm_sp->distortion);
146 
147  if (rdebug&RDEBUG_SHADE)
148  bu_log("fbm_render: point (%g %g %g) becomes (%g %g %g)\n\tv_noise (%g %g %g)\n",
149  V3ARGS(swp->sw_hit.hit_point),
150  V3ARGS(pt),
151  V3ARGS(v_noise));
152 
153  VADD2(swp->sw_hit.hit_normal, swp->sw_hit.hit_normal, v_noise);
154  VUNITIZE(swp->sw_hit.hit_normal);
155 
156  return 1;
157 }
158 
159 
160 /*
161  * Local Variables:
162  * mode: C
163  * tab-width: 8
164  * indent-tabs-mode: t
165  * c-file-style: "stroustrup"
166  * End:
167  * ex: shiftwidth=4 tabstop=8
168  */
HIDDEN void fbm_free(void *cp)
Definition: sh_fbm.c:122
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define MF_MAGIC
Definition: magic.h:205
HIDDEN void fbm_print(register struct region *rp, void *dp)
Definition: sh_fbm.c:115
double distortion
Definition: sh_fbm.c:43
Header file for the BRL-CAD common definitions.
double offset
Definition: sh_fbm.c:41
const char * reg_name
Identifying string.
Definition: raytrace.h:539
struct bu_structparse fbm_parse[]
Definition: sh_fbm.c:62
#define HIDDEN
Definition: common.h:86
HIDDEN int fbm_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip)
Definition: sh_fbm.c:92
void bu_struct_print(const char *title, const struct bu_structparse *parsetab, const char *base)
Definition: parse.c:1221
HIDDEN int fbm_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
Definition: sh_fbm.c:129
#define BU_CK_VLS(_vp)
Definition: vls.h:69
double gain
Definition: sh_fbm.c:42
struct mfuncs fbm_mfuncs[]
Definition: sh_fbm.c:82
#define V3ARGS(a)
Definition: color.c:56
double octaves
Definition: sh_fbm.c:40
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define FBM_O(m)
Definition: sh_fbm.c:60
#define UNUSED(parameter)
Definition: common.h:239
double lacunarity
Definition: sh_fbm.c:38
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
#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
double h_val
Definition: sh_fbm.c:39
void bn_noise_vec(point_t point, point_t result)
Definition: vls.h:56
point_t scale
Definition: sh_fbm.c:44
Header file for the BRL-CAD Optical Library, LIBOPTICAL.
int rdebug
Definition: init.c:39