BRL-CAD
sh_spm.c
Go to the documentation of this file.
1 /* S H _ S P M . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1986-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_spm.c
21  *
22  * Spherical Data Structures/Texture Maps
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <math.h>
31 
32 #include "vmath.h"
33 #include "raytrace.h"
34 #include "fb.h"
35 #include "spm.h"
36 #include "optical.h"
37 
38 
39 #define SPM_NAME_LEN 128
40 struct spm_specific {
41  char sp_file[SPM_NAME_LEN]; /* Filename */
42  int sp_w; /* Width: number of pixels around equator */
43  bn_spm_map_t *sp_map; /* stuff */
44 };
45 #define SP_NULL ((struct spm_specific *)0)
46 #define SP_O(m) bu_offsetof(struct spm_specific, m)
47 
49  {"%s", SPM_NAME_LEN, "file", SP_O(sp_file), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
50  {"%d", 1, "w", SP_O(sp_w), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
51  {"%d", 1, "n", SP_O(sp_w), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }, /*compat*/
52  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
53 };
54 
55 
56 HIDDEN int spm_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
57 HIDDEN int spm_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp);
58 HIDDEN void spm_print(register struct region *rp, void *dp);
59 HIDDEN void spm_mfree(void *cp);
60 
61 struct mfuncs spm_mfuncs[] = {
62  {MF_MAGIC, "spm", 0, MFI_UV, 0, spm_setup, spm_render, spm_print, spm_mfree },
63  {0, (char *)0, 0, 0, 0, 0, 0, 0, 0 }
64 };
65 
66 
67 /*
68  * Given a u, v coordinate within the texture (0 <= u, v <= 1.0),
69  * return a pointer to the relevant pixel.
70  */
71 HIDDEN int
72 spm_render(struct application *UNUSED(ap), const struct partition *UNUSED(pp), struct shadework *swp, void *dp)
73 {
74  register struct spm_specific *spp =
75  (struct spm_specific *)dp;
76  int x, y;
77  register unsigned char *cp;
78 
79  /** bn_spm_read(spp->sp_map, xxx); **/
80  /* Limits checking? */
81  y = swp->sw_uv.uv_v * spp->sp_map->ny;
82  x = swp->sw_uv.uv_u * spp->sp_map->nx[y];
83  cp = &(spp->sp_map->xbin[y][x*3]);
84  VSET(swp->sw_color,
85  ((double)cp[RED])/256.,
86  ((double)cp[GRN])/256.,
87  ((double)cp[BLU])/256.0);
88  return 1;
89 }
90 
91 
92 HIDDEN void
93 spm_mfree(void *cp)
94 {
95  struct spm_specific *spm;
96 
97  spm = (struct spm_specific *)cp;
98 
99  if (spm->sp_map)
100  bn_spm_free(spm->sp_map);
101  spm->sp_map = BN_SPM_MAP_NULL;
102  BU_PUT(cp, struct spm_specific);
103 }
104 
105 
106 /*
107  * Returns -
108  * <0 failed
109  * >0 success
110  */
111 HIDDEN int
112 spm_setup(register struct region *UNUSED(rp), struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *UNUSED(rtip))
113 
114 
115 /* New since 4.4 release */
116 {
117  register struct spm_specific *spp;
118 
119  BU_CK_VLS(matparm);
120  BU_GET(spp, struct spm_specific);
121  *dpp = spp;
122 
123  spp->sp_file[0] = '\0';
124  spp->sp_w = -1;
125  if (bu_struct_parse(matparm, spm_parse, (char *)spp, NULL) < 0) {
126  BU_PUT(spp, struct spm_specific);
127  return -1;
128  }
129  if (spp->sp_w < 0) spp->sp_w = 512;
130  if (spp->sp_file[0] == '\0')
131  goto fail;
132  if ((spp->sp_map = bn_spm_init(spp->sp_w, sizeof(RGBpixel))) == BN_SPM_MAP_NULL)
133  goto fail;
134  if (bn_spm_load(spp->sp_map, spp->sp_file) < 0)
135  goto fail;
136  return 1;
137 fail:
138  spm_mfree((void *)spp);
139  return -1;
140 }
141 
142 
143 HIDDEN void
144 spm_print(register struct region *rp, void *dp)
145 {
146  struct spm_specific *spm;
147 
148  spm = (struct spm_specific *)dp;
149 
150  bu_log("spm_print(rp=%p, dp = %p)\n", (void *)rp, dp);
151  (void)bu_struct_print("spm_print", spm_parse, (char *)dp);
152  if (spm->sp_map) bn_spm_dump(spm->sp_map, 0);
153 }
154 
155 
156 /*
157  * Local Variables:
158  * mode: C
159  * tab-width: 8
160  * indent-tabs-mode: t
161  * c-file-style: "stroustrup"
162  * End:
163  * ex: shiftwidth=4 tabstop=8
164  */
HIDDEN void spm_print(register struct region *rp, void *dp)
Definition: sh_spm.c:144
void bn_spm_dump(bn_spm_map_t *mp, int verbose)
Definition: sphmap.c:356
unsigned char RGBpixel[3]
Definition: fb.h:73
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int sp_w
Definition: sh_spm.c:42
#define MF_MAGIC
Definition: magic.h:205
#define SP_O(m)
Definition: sh_spm.c:46
#define VSET(a, b, c, d)
Definition: color.c:53
#define RED
Definition: color.h:39
bn_spm_map_t * sp_map
Definition: sh_spm.c:43
HIDDEN void spm_mfree(void *cp)
Definition: sh_spm.c:93
Header file for the BRL-CAD common definitions.
#define BLU
Definition: color.h:41
#define SPM_NAME_LEN
Definition: sh_spm.c:39
#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 BU_CK_VLS(_vp)
Definition: vls.h:69
struct mfuncs spm_mfuncs[]
Definition: sh_spm.c:61
HIDDEN int spm_render(struct application *ap, const struct partition *pp, struct shadework *swp, void *dp)
Definition: sh_spm.c:72
int bn_spm_load(bn_spm_map_t *mapp, char *filename)
Definition: sphmap.c:169
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
struct bu_structparse spm_parse[]
Definition: sh_spm.c:48
HIDDEN int spm_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip)
Definition: sh_spm.c:112
#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
bn_spm_map_t * bn_spm_init(int N, int elsize)
Definition: sphmap.c:62
int bu_struct_parse(const struct bu_vls *in_vls, const struct bu_structparse *desc, const char *base, void *data)
Definition: parse.c:878
char sp_file[SPM_NAME_LEN]
Definition: sh_spm.c:41
void bn_spm_free(bn_spm_map_t *mp)
Definition: sphmap.c:36
#define GRN
Definition: color.h:40
Definition: vls.h:56
Header file for the BRL-CAD Optical Library, LIBOPTICAL.