ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglmat.c
Revision: 3.1
Committed: Tue Jun 9 11:18:35 1998 UTC (25 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 gwlarson 3.1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Routines for Radiance -> OpenGL materials.
9     */
10    
11     #include "radogl.h"
12    
13     extern int free(), freemtl();
14    
15     int domats = 1; /* are we doing materials? */
16    
17     LUTAB mtab = LU_SINIT(free,freemtl);
18    
19    
20     rgl_matclear() /* clean up materials */
21     {
22     lu_done(&mtab);
23     domats = 1;
24     }
25    
26    
27     MATREC *
28     getmatp(nam) /* find material record for modifier name */
29     char *nam;
30     {
31     register LUENT *lup;
32    
33     if (nam == NULL)
34     return(NULL);
35     if ((lup = lu_find(&mtab, nam)) == NULL)
36     return(NULL);
37     return((MATREC *)lup->data);
38     }
39    
40    
41     o_default(o) /* default object is non-material modifier */
42     register OBJREC *o;
43     {
44     register LUENT *lup;
45     #ifdef DEBUG
46     if (o->otype >= 0 && !ismodifier(o->otype))
47     error(CONSISTENCY, "o_default handed non-modifier");
48     #endif
49     /* find name in lookup table */
50     if ((lup = lu_find(&mtab, o->oname)) == NULL)
51     goto memerr;
52     if (lup->key == NULL) { /* new entry? */
53     lup->key = (char *)malloc(strlen(o->oname)+1);
54     if (lup->key == NULL)
55     goto memerr;
56     strcpy(lup->key, o->oname);
57     } else if (lup->data != NULL)
58     freemtl(lup->data);
59     if ((lup->data = o->os) != NULL) /* make material reference */
60     ((MATREC *)lup->data)->nlinks++;
61     return;
62     memerr:
63     error(SYSTEM, "out of memory in o_default");
64     }
65    
66    
67     MATREC *
68     newmaterial(nam) /* get an entry for a new material */
69     char *nam;
70     {
71     register LUENT *lup;
72     /* look it up (assign entry) */
73     if ((lup = lu_find(&mtab, nam)) == NULL)
74     goto memerr;
75     if (lup->key == NULL) { /* new entry? */
76     lup->key = (char *)malloc(strlen(nam)+1);
77     if (lup->key == NULL)
78     goto memerr;
79     strcpy(lup->key, nam);
80     } else if (lup->data != NULL)
81     freemtl(lup->data);
82     lup->data = (char *)malloc(sizeof(MATREC));
83     if (lup->data == NULL)
84     goto memerr;
85     ((MATREC *)lup->data)->nlinks = 1;
86     return((MATREC *)lup->data);
87     memerr:
88     error(SYSTEM, "out of memory in newmaterial");
89     }
90    
91    
92     freemtl(mp) /* free a material */
93     register MATREC *mp;
94     {
95     if (!--mp->nlinks)
96     free((char *)mp);
97     }
98    
99    
100     m_normal(o) /* compute normal material parameters */
101     register OBJREC *o;
102     {
103     register MATREC *m;
104     /* check arguments */
105     if (o->oargs.nfargs != (o->otype == MAT_TRANS ? 7 : 5))
106     objerror(o, USER, "bad # of real arguments");
107     /* allocate/insert material */
108     m = newmaterial(o->oname);
109     /* assign parameters */
110     setcolor(m->u.m.ambdiff, o->oargs.farg[0],
111     o->oargs.farg[1], o->oargs.farg[2]);
112     if ((m->type = o->otype) == MAT_METAL)
113     copycolor(m->u.m.specular, m->u.m.ambdiff);
114     else
115     setcolor(m->u.m.specular, 1., 1., 1.);
116     scalecolor(m->u.m.specular, o->oargs.farg[3]);
117     scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[3]);
118     if (m->type == MAT_TRANS) {
119     scalecolor(m->u.m.specular, 1.-o->oargs.farg[5]);
120     scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[5]);
121     }
122     if (o->oargs.farg[4] <= FTINY)
123     m->u.m.specexp = MAXSPECEXP;
124     else
125     m->u.m.specexp = 2./(o->oargs.farg[4]*o->oargs.farg[4]);
126     if (m->u.m.specexp > MAXSPECEXP)
127     m->u.m.specexp = MAXSPECEXP;
128     }
129    
130    
131     m_aniso(o) /* anisotropic material */
132     register OBJREC *o;
133     {
134     register MATREC *m;
135     /* check arguments */
136     if (o->oargs.nfargs < (o->otype == MAT_TRANS2 ? 8 : 6))
137     objerror(o, USER, "bad # of real arguments");
138     /* allocate/insert material */
139     m = newmaterial(o->oname);
140     /* assign parameters */
141     setcolor(m->u.m.ambdiff, o->oargs.farg[0],
142     o->oargs.farg[1], o->oargs.farg[2]);
143     if ((m->type = o->otype) == MAT_METAL2)
144     copycolor(m->u.m.specular, m->u.m.ambdiff);
145     else
146     setcolor(m->u.m.specular, 1., 1., 1.);
147     scalecolor(m->u.m.specular, o->oargs.farg[3]);
148     scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[3]);
149     if (m->type == MAT_TRANS2) {
150     scalecolor(m->u.m.specular, 1.-o->oargs.farg[6]);
151     scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[6]);
152     }
153     if (o->oargs.farg[4]*o->oargs.farg[5] <= FTINY*FTINY)
154     m->u.m.specexp = MAXSPECEXP;
155     else
156     m->u.m.specexp = 2./(o->oargs.farg[4]*o->oargs.farg[5]);
157     if (m->u.m.specexp > MAXSPECEXP)
158     m->u.m.specexp = MAXSPECEXP;
159     }
160    
161    
162     m_glass(o) /* glass material (hopeless) */
163     OBJREC *o;
164     {
165     register MATREC *m;
166    
167     m = newmaterial(o->oname);
168     m->type = o->otype;
169     setcolor(m->u.m.ambdiff, 0., 0., 0.);
170     setcolor(m->u.m.specular, .08, .08, .08);
171     m->u.m.specexp = MAXSPECEXP;
172     }
173    
174    
175     m_brdf(o) /* convert functional material */
176     register OBJREC *o;
177     {
178     register MATREC *m;
179     /* check arguments */
180     if (o->oargs.nfargs < (o->otype == MAT_TFUNC ? 6 : 4))
181     objerror(o, USER, "bad # of real arguments");
182     /* allocate/insert material */
183     m = newmaterial(o->oname);
184     /* assign parameters */
185     setcolor(m->u.m.ambdiff, o->oargs.farg[0],
186     o->oargs.farg[1], o->oargs.farg[2]);
187     if ((m->type = o->otype) == MAT_MFUNC)
188     copycolor(m->u.m.specular, m->u.m.ambdiff);
189     else
190     setcolor(m->u.m.specular, 1., 1., 1.);
191     scalecolor(m->u.m.specular, o->oargs.farg[3]);
192     scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[3]);
193     if (m->type == MAT_TFUNC) {
194     scalecolor(m->u.m.specular, 1.-o->oargs.farg[4]);
195     scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[4]);
196     }
197     m->u.m.specexp = UNKSPECEXP;
198     }
199    
200    
201     m_brdf2(o) /* convert advanced functional material */
202     register OBJREC *o;
203     {
204     register MATREC *m;
205    
206     if (o->oargs.nfargs < 9)
207     objerror(o, USER, "bad # of real arguments");
208     m = newmaterial(o->oname);
209     m->type = o->otype;
210     /* assign average diffuse front+back */
211     setcolor(m->u.m.ambdiff, (o->oargs.farg[0]+o->oargs.farg[3])*.5,
212     (o->oargs.farg[1]+o->oargs.farg[4])*.5,
213     (o->oargs.farg[2]+o->oargs.farg[5])*.5);
214     /* guess the rest */
215     setcolor(m->u.m.specular, .1, .1, .1);
216     m->u.m.specexp = UNKSPECEXP;
217     }
218    
219    
220     m_light(o) /* convert light type */
221     register OBJREC *o;
222     {
223     FVECT v;
224     register MATREC *m;
225    
226     if (o->oargs.nfargs < (o->otype == MAT_SPOT ? 7 : 3))
227     objerror(o, USER, "bad # of real arguments");
228     m = newmaterial(o->oname);
229     setcolor(m->u.l.emission, o->oargs.farg[0],
230     o->oargs.farg[1], o->oargs.farg[2]);
231     if ((m->type = o->otype) == MAT_SPOT) {
232     if ((m->u.l.spotang = o->oargs.farg[3]/2.) > 90.)
233     m->u.l.spotang = 180.;
234     v[0] = o->oargs.farg[4];
235     v[1] = o->oargs.farg[5];
236     v[2] = o->oargs.farg[6];
237     if (normalize(v) == 0.)
238     objerror(o, USER, "illegal direction");
239     VCOPY(m->u.l.spotdir, v);
240     } else {
241     m->u.l.spotang = 180.;
242     m->u.l.spotdir[0] = m->u.l.spotdir[1] = 0.;
243     m->u.l.spotdir[2] = -1.;
244     }
245     }
246    
247    
248     m_mirror(o) /* convert mirror type */
249     register OBJREC *o;
250     {
251     register MATREC *m;
252    
253     if (o->oargs.nfargs != 3)
254     objerror(o, USER, "bad # real arguments");
255     m = newmaterial(o->oname);
256     m->type = o->otype;
257     setcolor(m->u.m.ambdiff, 0., 0., 0.);
258     setcolor(m->u.m.specular, o->oargs.farg[0],
259     o->oargs.farg[1], o->oargs.farg[2]);
260     m->u.m.specexp = MAXSPECEXP;
261     }
262    
263    
264     m_prism(o) /* convert prism type */
265     register OBJREC *o;
266     {
267     register MATREC *m;
268     /* can't really deal with this type */
269     m = newmaterial(o->oname);
270     m->type = o->otype;
271     setcolor(m->u.m.ambdiff, 0.2, 0.2, 0.2);
272     setcolor(m->u.m.specular, 0.1, 0.1, 0.1);
273     m->u.m.specexp = UNKSPECEXP;
274     }