texlit.c
5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "vert.h"
#include "obj.h"
#include <math.h>
#include <stdio.h>
int
fmin( int a, int b )
{
if( a < b )
return( a );
return(b);
}
void calc_lighting_texture4( Object *, Vertex, Vertex, Vertex, Vertex, unsigned char *, int, int );
void simple_closest_norm_obj( Vertex, Object *, Vertex );
void
calc_lighting_texture3( Object *obj, Vertex n0, Vertex ns, Vertex nt,
unsigned char *img, int smax, int tmax )
{
Vertex nst;
normalize( n0 );
normalize( ns );
normalize( nt );
vadd( ns, nt, nst );
vsub( nst, n0, nst );
normalize( nst );
simple_closest_norm_obj( nst, obj, nst );
calc_lighting_texture4( obj, n0, ns, nt, nst, img, smax, tmax );
}
int simple_closest_vert( Vertex );
void
calc_lighting_texture4( Object *obj, Vertex n0, Vertex ns, Vertex nt, Vertex nst,
unsigned char *img, int smax, int tmax )
{
int s,t;
double ft, fs;
int sbig;
int i;
Vertex ns0, ns1, n;
void apply_bumps( Vertex, Object * );
for(t=0; t<tmax; t++) {
ft = (((float)t)/tmax);
if( t == 0 )
sbig = fmin( smax, smax*((tmax - (float)t)/tmax) + 3 );
else
sbig = fmin( smax, smax*((tmax-1.0-t)/tmax) + 3 );
vinterp( nt, n0, ft, ns0 );
vinterp( nst, ns, ft, ns1 );
for(s=0; s<sbig; s++) {
fs = (((float)s)/smax);
vinterp( ns1, ns0, fs, n );
apply_bumps( n, obj );
i = simple_closest_vert( n );
img[s+t*smax] = i;
}
};
}
int num_vtab = 0;
Vertex vertex_table[512];
int
simple_closest_vert( Vertex n )
{
int closest = -1;
int i;
double d_max = -2;
double d;
for(i=0; i<num_vtab; i++) {
d = vdot( n, vertex_table[i] );
if( d > d_max ) {
closest = i;
d_max = d;
};
};
return( closest );
}
void
simple_closest_norm_obj( Vertex n, Object *obj, Vertex vbest )
{
int closest = -1;
int i;
double d_max = -2;
double d;
for(i=0; i<obj->nverts; i++) {
d = vdot( n, obj->verts[i].normal );
if( d > d_max ) {
closest = i;
d_max = d;
};
};
vbest[0] = obj->verts[closest].normal[0];
vbest[1] = obj->verts[closest].normal[1];
vbest[2] = obj->verts[closest].normal[2];
}
void
add_vertex_table( Vertex v )
{
int i;
i = num_vtab++;
vertex_table[i][0] = v[0];
vertex_table[i][1] = v[1];
vertex_table[i][2] = v[2];
}
#define RSCALE (1.0/0x7fffffff)
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
double
frand( void )
{
static int seed = -1;
if( seed == -1 ) {
time_t t_dum;
seed = getpid() + time( &t_dum );
(void) srandom( seed );
};
return( random() * RSCALE );
}
void
vrand( Vertex vout, Vertex vscale )
{
do {
vout[0] = 2*frand() - 1.0;
vout[1] = 2*frand() - 1.0;
vout[2] = 2*frand() - 1.0;
} while( vlen( vout ) > 1.0 );
vout[0] *= vscale[0];
vout[1] *= vscale[1];
vout[2] *= vscale[2];
}
int
vradial( Vertex vout, Vertex vcur, objBump *bump )
{
Vertex vdif;
double d, f;
int id;
signed char *data;
d = 1.0 - vdot( bump->direction, vcur );
if( d < bump->scale[0] ) {
d = d / bump->scale[0] * (bump->shape->xdim - 1); /* Index into bump_map */
id = d;
d = d - id;
data = (signed char *) bump->shape->data;
f = data[id] * (1-d) + d * data[id+1];
f /= 128; /* Normalize to -1...+1 */
f *= bump->scale[2];
vsub( bump->direction, vcur, vdif );
normalize( vdif );
vout[0] = vdif[0] * f;
vout[1] = vdif[1] * f;
vout[2] = vdif[2] * f;
return( 1 );
} else {
vout[0] = 0.0;
vout[1] = 0.0;
vout[2] = 0.0;
return( 0 );
}
}
void
apply_bumps( Vertex v, Object *obj )
{
int bnum;
objBump *bump;
Vertex vsum, bumpv;
vsum[0] = vsum[1] = vsum[2] = 0.0;
vadd( v, vsum, vsum );
bump = obj->mat.bumps;
for(bnum=0; bnum<obj->mat.nbumps; bnum++, bump++) {
switch( bump->type ) {
case BUMP_UNOISE:
vrand( bumpv, bump->scale );
vadd( vsum, bumpv, vsum ); /* Accumulate effects of bumps */
break;
case BUMP_DNOISE:
break;
case BUMP_RADIAL:
if( vradial( bumpv, v, bump ) )
vadd( v, bumpv, vsum ); /* Overwrite previous bumps */
break;
default:
break;
};
};
v[0] = vsum[0];
v[1] = vsum[1];
v[2] = vsum[2];
normalize( v );
}
#if 0
/* this Vertex list structure defines some information
about the adjacency of some vertecies.
The structure is hierarchical, with each level consisting
of a cloud of verticies (stored in a linked list defined
by the "next" field and terminated by NULL. For each
point in this set there is an associated "children" list
which consists of points close to the parent.
*/
typedef struct vert_list_struct {
int vnum;
Vertex *v;
struct vert_list_struct **children;
} Vert_list;
Vertex tetraherdron[] = {
{ 1.0/6, 1.0/6, -5.0/6 },
{ 1.0/6, -5.0/6, 1.0/6 },
{ -5.0/6, 1.0/6, 1.0/6 },
{ 1.0/2, 1.0/2, 1.0/2 } };
int
closest_vert( Vertex *n, Vert_list *vl )
{
Vert_list *vlclose = NULL;
Vertex *v;
double d_min = -2;
double d;
do {
/* find closest vertex */
for( v=vl->v; vl; vl = vl->next ) {
if( v && ((d=vdot( n, v )) > d_max )) {
vlclose = vl;
d_max = d;
}
};
vl = vlclose->children;
} while( vl );
return( vertex_table - vlclose->v );
}
#endif /* 0 */