BRL-CAD
sh_rtrans.c
Go to the documentation of this file.
1 /* S H _ R T R A N S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1998-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_rtrans.c
21  *
22  * Random transparency shader. A random number from 0 to 1 is drawn
23  * for each pixel rendered. If the random number is less than the threshold
24  * value, the pixel is rendered as 100% transparent
25  *
26  */
27 
28 #include "common.h"
29 
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <math.h>
34 
35 #include "vmath.h"
36 #include "raytrace.h"
37 #include "optical.h"
38 
39 
40 #define RTRANS_MAGIC 0x4a6f686e
42  uint32_t magic;
43  double threshold;
44  int next_rand;
45 };
46 #define CK_RTRANS_SP(_p) BU_CKMAG(_p, RTRANS_MAGIC, "rtrans_specific")
47 
48 static struct rtrans_specific rtrans_defaults = {
50  0.5,
51  3
52 };
53 
54 
55 #define SHDR_NULL ((struct rtrans_specific *)0)
56 #define SHDR_O(m) bu_offsetof(struct rtrans_specific, m)
57 
59  {"%g", 1, "threshold", SHDR_O(threshold), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
60  {"%g", 1, "t", SHDR_O(threshold), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
61  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
62 };
63 
64 
65 HIDDEN int rtrans_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
66 HIDDEN int rtrans_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp);
67 HIDDEN void rtrans_print(register struct region *rp, void *dp);
68 HIDDEN void rtrans_free(void *cp);
69 
70 struct mfuncs rtrans_mfuncs[] = {
71  {MF_MAGIC, "rtrans", 0, 0, 0, rtrans_setup, rtrans_render, rtrans_print, rtrans_free },
72  {0, (char *)0, 0, 0, 0, 0, 0, 0, 0 }
73 };
74 
75 
76 /*
77  * This routine is called (at prep time)
78  * once for each region which uses this shader.
79  * Any shader-specific initialization should be done here.
80  */
81 HIDDEN int
82 rtrans_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *rtip)
83 
84 
85 /* pointer to reg_udata in *rp */
86 
87 /* New since 4.4 release */
88 {
89  register struct rtrans_specific *rtrans_sp;
90 
91  RT_CHECK_RTI(rtip);
92  BU_CK_VLS(matparm);
93  RT_CK_REGION(rp);
94  BU_GET(rtrans_sp, struct rtrans_specific);
95  *dpp = rtrans_sp;
96 
97  memcpy(rtrans_sp, &rtrans_defaults, sizeof(struct rtrans_specific));
98 
99  if (bu_struct_parse(matparm, rtrans_parse, (char *)rtrans_sp, NULL) < 0)
100  return -1;
101 
102  BN_RANDSEED(rtrans_sp->next_rand, 3);
103 
104  if (rdebug&RDEBUG_SHADE)
105  bu_struct_print(rp->reg_name, rtrans_parse, (char *)rtrans_sp);
106 
107  return 1;
108 }
109 
110 
111 HIDDEN void
112 rtrans_print(register struct region *rp, void *dp)
113 {
114  bu_struct_print(rp->reg_name, rtrans_parse, (char *)dp);
115 }
116 
117 
118 HIDDEN void
119 rtrans_free(void *cp)
120 {
121  BU_PUT(cp, struct rtrans_specific);
122 }
123 
124 
125 /*
126  * This is called (from viewshade() in shade.c)
127  * once for each hit point to be shaded.
128  */
129 int
130 rtrans_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
131 {
132  register struct rtrans_specific *rtrans_sp =
133  (struct rtrans_specific *)dp;
134 
135  RT_AP_CHECK(ap);
136  RT_CHECK_PT(pp);
137  CK_RTRANS_SP(rtrans_sp);
138 
139  if (rdebug&RDEBUG_SHADE)
140  bu_struct_print("random transparency", rtrans_parse, (char *)rtrans_sp);
141 
142  if (rtrans_sp->threshold >= 1.0 ||
143  BN_RANDOM(rtrans_sp->next_rand) < rtrans_sp->threshold)
144  {
145  swp->sw_transmit = 1.0;
146  swp->sw_reflect = 0.0;
147  swp->sw_refrac_index = 1.0;
148  VSETALL(swp->sw_basecolor, 1.0);
149 
150  if (swp->sw_reflect > 0 || swp->sw_transmit > 0)
151  (void)rr_render(ap, pp, swp);
152  }
153 
154  return 1;
155 }
156 
157 
158 /*
159  * Local Variables:
160  * mode: C
161  * tab-width: 8
162  * indent-tabs-mode: t
163  * c-file-style: "stroustrup"
164  * End:
165  * ex: shiftwidth=4 tabstop=8
166  */
uint32_t magic
Definition: sh_rtrans.c:42
HIDDEN void rtrans_free(void *cp)
Definition: sh_rtrans.c:119
HIDDEN void rtrans_print(register struct region *rp, void *dp)
Definition: sh_rtrans.c:112
HIDDEN int rtrans_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip)
Definition: sh_rtrans.c:82
#define CK_RTRANS_SP(_p)
Definition: sh_rtrans.c:46
#define MF_MAGIC
Definition: magic.h:205
struct bu_structparse rtrans_parse[]
Definition: sh_rtrans.c:58
#define VSETALL(a, s)
Definition: color.c:54
Header file for the BRL-CAD common definitions.
struct mfuncs rtrans_mfuncs[]
Definition: sh_rtrans.c:70
const char * reg_name
Identifying string.
Definition: raytrace.h:539
#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
HIDDEN int rtrans_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
Definition: sh_rtrans.c:130
#define BU_CK_VLS(_vp)
Definition: vls.h:69
#define RT_CHECK_RTI(_p)
Definition: raytrace.h:1832
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define BN_RANDSEED(_i, _seed)
Definition: rand.h:60
#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
#define RTRANS_MAGIC
Definition: sh_rtrans.c:40
#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 BN_RANDOM(_i)
Definition: rand.h:73
#define SHDR_O(m)
Definition: sh_rtrans.c:56
int rr_render(struct application *app, const struct partition *pp, struct shadework *swp)
double threshold
Definition: sh_rtrans.c:43
#define RT_CHECK_PT(_p)
compat
Definition: raytrace.h:588
Definition: vls.h:56
Header file for the BRL-CAD Optical Library, LIBOPTICAL.
int rdebug
Definition: init.c:39