-
Notifications
You must be signed in to change notification settings - Fork 0
/
tplane.c
226 lines (184 loc) · 5.25 KB
/
tplane.c
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
/*
* tplane.c
*
* Initialize, dump, and hit functions for a tplane object.
*
* Chris Blades
*
* 31/3/2011
*/
#include <stdio.h>
#include "common.h"
#include "object.h"
#include "safe.h"
#include "plane.h"
#include "tplane.h"
#include "material.h"
#include "veclib3d.h"
/**
* Initialize a tplane object from a file.
*
* PARAMETERS:
* in - file to read from
* objtype - type of object to initalize
*
* RETURNS:
* the newly created object
*/
obj_t * tplane_init(FILE *in, int objtype) {
obj_t *obj = plane_init(in, objtype); // base object struct
plane_t *plane = (plane_t *)obj->priv; // base plane struct
tplane_t *tplane =
(tplane_t *)smalloc(sizeof(tplane_t)); // new tplane struct
plane->priv = tplane; // connect tplane to obj
obj->hits = hits_plane; // connect hits function
obj->getamb = tp_amb;
obj->getdif = tp_diff;
obj->getspec = tp_spec;
char buf[256]; // buffer for reading in values
int rc = 0; // read counter
// read in x direction
while (rc != 3) {
rc = fscanf(in, "%lf %lf %lf", tplane->xdir, (tplane->xdir + 1),
(tplane->xdir + 2));
fgets(buf, 256, in);
#ifdef DEBUG_OBJECTS
fprintf(stderr, "tplane read %s\n", buf);
#endif
}
rc = 0;
// read in normal
while (rc != 2) {
rc = fscanf(in, "%lf %lf", tplane->size,
(tplane->size + 1));
fgets(buf, 256, in);
#ifdef DEBUG_OBJECTS
fprintf(stderr, "tplane read %s\n", buf);
#endif
}
// initailize backgorund material
material_init(in, &tplane->background);
// construct rotation matrix
double normal[3];
double xdir[3];
//vec_project3(plane->normal, tplane->xdir, tplane->xdir);
vec_unit3(plane->normal, normal);
vec_unit3(tplane->xdir, xdir);
vec_unit3(xdir, tplane->rotmat[0]);
vec_unit3(normal, tplane->rotmat[2]);
vec_cross3(tplane->rotmat[2], tplane->rotmat[0], tplane->rotmat[1]);
return obj;
}
/**
* Prints information about a tplane object to a file.
*
* PARAMETERS:
* out - file to write to
* obj - object to dump
*/
void tplane_dump(FILE *out, obj_t *obj) {
plane_t *plane = (plane_t *)obj->priv;
tplane_t *tplane =
(tplane_t *)plane->priv; // get tplane struct out of obj
fprintf(out, "\tTYPE: TPlane\n");
plane_dump(out, obj);
fprintf(out, "\t\tXDIR: (%lf, %lf, %lf)\n", tplane->xdir[0],
tplane->xdir[1],
tplane->xdir[2]);
fprintf(out, "\t\tSIZE: (%lf, %lf)\n", tplane->size[0],
tplane->size[1]);
}
/**
* Retrieve diffuse values for the last hitpoint
*
* PARAMETERS:
* obj - tplane object
* value - intensity vector
*/
void tp_diff(obj_t *obj, double *value) {
#ifdef DEBUG_TPLANE
fprintf(stderr, "tp_diff\n");
#endif
plane_t *pln = (plane_t *)obj->priv;
tplane_t *tp = (tplane_t *)pln->priv;
material_t *mat;
if (tp_select(obj)) {
mat = &obj->material;
} else {
mat = &tp->background;
}
*(value + 0) = mat->diffuse[0];
*(value + 1) = mat->diffuse[1];
*(value + 2) = mat->diffuse[2];
}
/**
* Retrieve ambient values for the last hitpoint
*
* PARAMETERS:
* obj - tplane object
* value - intensity vector
*/
void tp_amb(obj_t *obj, double *value) {
#ifdef DEBUG_TPLANE
fprintf(stderr, "tp_amb\n");
#endif
plane_t *pln = (plane_t *)obj->priv;
tplane_t *tp = (tplane_t *)pln->priv;
material_t *mat;
if (tp_select(obj)) {
mat = &obj->material;
} else {
mat = &tp->background;
}
*(value + 0) = mat->ambient[0];
*(value + 1) = mat->ambient[1];
*(value + 2) = mat->ambient[2];
}
/**
* Retrieve specular values for the last hitpoint
*
* PARAMETERS:
* obj - tplane object
* value - intensity vector
*/
void tp_spec(obj_t *obj, double *value) {
#ifdef DEBUG_TPLANE
fprintf(stderr, "tp_spec\n");
#endif
plane_t *pln = (plane_t *)obj->priv;
tplane_t *tp = (tplane_t *)pln->priv;
material_t *mat;
if (tp_select(obj)) {
mat = &obj->material;
} else {
mat = &tp->background;
}
*(value + 0) = mat->specular[0];
*(value + 1) = mat->specular[1];
*(value + 2) = mat->specular[2];
}
/**
* Determine if the current hitpoint is on a foreground or background
* tile.
*
* PARAMETERS:
* obj - tplane object
*
* RETURNS:
* 0 for background and 1 for foreground
*/
int tp_select(obj_t *obj) {
plane_t *plane = (plane_t *)obj->priv; // plane struct
tplane_t *tplane = (tplane_t *)plane->priv; // tplane struct
double *newhit = (double *)alloca(sizeof(double) * 3);
vec_diff3(plane->point, obj->hitloc, newhit);
xform3(tplane->rotmat, newhit, newhit);
// HACK! the 1000 is to take care of rouding error near boundries
int relx = (int) (1000 + newhit[0] / tplane->size[0]);
int rely = (int) (1000 + newhit[1] / tplane->size[1]);
int total = relx + rely;
#ifdef DEBUG_TPLANE
fprintf(stderr, "relx = %d, rely = %d, mod = %d\n", relx, rely, total % 2);
#endif
return total % 2;
}