-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEE.cpp
314 lines (273 loc) · 7.49 KB
/
LEE.cpp
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
304
305
306
307
308
309
310
311
312
313
#include "stdafx.h"
#include "LEE.h"
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
LEE::LEE(){
for (int i = 0; i < 3; i++) {
edges[i] = new LineEq<QType>(SCALEFACTOR);
}
triPl = new TriPlanar();
shader = new Shader();
texture = NULL;
aa = NULL;
drawed = NULL;
}
LEE::~LEE(){
for (int i = 0; i < 3; i++) {
if (edges[i] != NULL)
delete edges[i];
}
if (triPl != NULL)
delete triPl;
if (shader != NULL)
delete shader;
if (texture != NULL)
delete texture;
if (aa != NULL)
delete aa;
#ifdef _DEBUG
if (drawed != NULL)
free(drawed);
#endif
}
void LEE::boundBox(GzCoord triVertices[]) {
/*
find bounding box -> find min/max among 4 points for x/y -> 3 vertices and window
*/
float xmin, xmax, ymin, ymax;
minMax3<float>(triVertices[0][X], triVertices[1][X], triVertices[2][X], xmin, xmax);
minMax3<float>(triVertices[0][Y], triVertices[1][Y], triVertices[2][Y], ymin, ymax);
l = (int)(xmin < 0.f ? 0.f : xmin);
u = (int)(ymin < 0.f ? 0.f : ymin);
int ixmax = (int)(xmax + 1.f),
iymax = (int)(ymax + 1.f);
r = ixmax >= xres ? xres - 1 : ixmax;
b = iymax >= yres ? yres - 1 : iymax;
}
QType LEE::buildTriangle(GzCoord triVertices[]) {
// TODO: adjust integer vertices
/*
compute edges
*/
edges[0]->calLineEq(triVertices[0], triVertices[1]);
edges[1]->calLineEq(triVertices[1], triVertices[2]);
edges[2]->calLineEq(triVertices[2], triVertices[0]);
/*
compute area of the triangle, make sure the vertices is given in clockwise
*/
QType area = edges[0]->getC() + edges[1]->getC() + edges[2]->getC();
if (area == 0)
return area;
if (area < 0) {
edges[0]->reverse();
edges[1]->reverse();
edges[2]->reverse();
area = -area;
}
boundBox(triVertices);
return area;
}
bool LEE::coveredEdge(QType A, QType B) {
if (A > 0)
return true;
else if (A == 0 && B > 0)
return true;
else
return false;
}
void LEE::putTexPixel(int i, int j, int z, bool& drawn) {
drawn = true;
if ((display->fbuf + ARRAY(i, j))->z >= z) {
#ifdef _DEBUG
if (*(drawed + ARRAY(i, j)) && (display->fbuf + ARRAY(i, j))->z == z) {
printf("duplicated rendering detected at x=%d, y=%d", i, j);
}
*(drawed + ARRAY(i, j)) = true;
#endif
(display->fbuf + ARRAY(i, j))->z = z;
GzCoord position = { i, j, 0 };
GzColor color;
texture->getColor(i, j, z, color);
if (shader->getInterpStyle() == GZ_COLOR)
shader->setKt(color);
else if(shader->getInterpStyle() == GZ_NORMALS){
shader->setKa(color);
shader->setKd(color);
}
shader->getColor(position, color);
short scolor[3];
for (int i = 0; i < 3; i++) {
scolor[i] = ctoi(color[i]);
clampVal<short>(&scolor[i], 0, MAXRGBA);
}
putBuffer(i, j, scolor);
}
}
void LEE::putShaderPixel(int i, int j, int z, bool& drawn) {
drawn = true;
if ((display->fbuf + ARRAY(i, j))->z >= z) {
#ifdef _DEBUG
if (*(drawed + ARRAY(i, j)) && (display->fbuf + ARRAY(i, j))->z == z) {
printf("duplicated rendering detected at x=%d, y=%d", i, j);
}
*(drawed + ARRAY(i, j)) = true;
#endif
(display->fbuf + ARRAY(i, j))->z = z;
GzCoord position = { i, j, 0 };
GzColor color;
shader->getColor(position, color);
short scolor[3];
for (int i = 0; i < 3; i++) {
scolor[i] = ctoi(color[i]);
clampVal<short>(&scolor[i], 0, MAXRGBA);
}
putBuffer(i, j, scolor);
}
}
void LEE::putPixel(int i, int j, int z, bool& drawn) {
drawn = true;
if ((display->fbuf + ARRAY(i, j))->z >= z) {
#ifdef _DEBUG
if (*(drawed + ARRAY(i, j)) && (display->fbuf + ARRAY(i, j))->z == z) {
printf("duplicated rendering detected at x=%d, y=%d", i, j);
}
*(drawed + ARRAY(i, j)) = true;
#endif
(display->fbuf + ARRAY(i, j))->z = z;
putBuffer(i, j, srgb);
}
}
void LEE::putBuffer(int i, int j, short color[]) {
(display->fbuf + ARRAY(i, j))->blue = color[BLUE];
(display->fbuf + ARRAY(i, j))->green = color[GREEN];
(display->fbuf + ARRAY(i, j))->red = color[RED];
}
void LEE::draw(GzCoord triVertices[]) {
QType area = buildTriangle(triVertices);
if (area == 0)
return;
for (int i = 0; i < 3; i++) {
srgb[i] = ctoi(rgb[i]);
clampVal<short>(&srgb[i], 0, MAXRGBA);
}
drawPixel(triVertices, area, &LEE::putPixel);
}
void LEE::draw(GzCoord triVertices[], GzCoord normal[]) {
//TODO integer coordinate adjust
if (shader->getInterpStyle() != GZ_FLAT) {
QType area = buildTriangle(triVertices);
if (area==0)
return;
shader->init(triVertices, normal, rgb);
drawPixel(triVertices, area, &LEE::putShaderPixel);
}
else {
draw(triVertices);
}
}
void LEE::draw(GzCoord triVertices[], GzCoord normal[], GzTextureIndex t[]) {
//TODO integer coordinate adjust
if (shader->getInterpStyle() != GZ_FLAT && texture != NULL) {
QType area = buildTriangle(triVertices);
if (area == 0)
return;
shader->init(triVertices, normal, rgb);
texture->buildTexTri(triVertices, t);
drawPixel(triVertices, area, &LEE::putTexPixel);
}
else if (shader->getInterpStyle() != GZ_FLAT) {
draw(triVertices, normal);
}
else{
draw(triVertices);
}
}
void LEE::drawPixel(GzCoord triVertices[], QType area, pf p) {
if ((int)triVertices[0][Z] == (int)triVertices[1][Z] && (int)triVertices[1][Z] == (int)triVertices[2][Z]) {
//if it is flat, we don't need z interpolation
drawFlatPixel((int)triVertices[0][Z], p);
}
else {
triPl->calPlanar(edges, triVertices[0][Z], triVertices[1][Z], triVertices[2][Z], area);
int x = l, y = u;
QType A0 = edges[0]->getA(), A1 = edges[1]->getA(), A2 = edges[2]->getA(),
B0 = edges[0]->getB(), B1 = edges[1]->getB(), B2 = edges[2]->getB(),
Ap = triPl->getAp(), Bp = triPl->getBp(), Cp = triPl->getCp();
QType e0 = edges[0]->evaluate(l, u),
e1 = edges[1]->evaluate(l, u),
e2 = edges[2]->evaluate(l, u);
QType z = triPl->evaluate(l, u);
for (; y <= b; y++) {
QType xe0 = e0,
xe1 = e1,
xe2 = e2;
QType xz = z;
bool drawn = false;
for (x = l; x <= r; x++) {
if (((LONGLONG)xe0 | (LONGLONG)xe1 | (LONGLONG)xe2) > 0) {
CALL_MEMBER_FN(*this, p)(x, y, xz, drawn);
}
else if (((LONGLONG)xe0 | (LONGLONG)xe1 | (LONGLONG)xe2) < 0) {
if (drawn) {
break;
}
}
else if ((LONGLONG)xe0 == 0 && coveredEdge(A0, B0)) {
CALL_MEMBER_FN(*this, p)(x, y, xz, drawn);
}
else if ((LONGLONG)xe1 == 0 && coveredEdge(A1, B1)) {
CALL_MEMBER_FN(*this, p)(x, y, xz, drawn);
}
else if ((LONGLONG)xe2 == 0 && coveredEdge(A2, B2)) {
CALL_MEMBER_FN(*this, p)(x, y, xz, drawn);
}
xe0 += A0;
xe1 += A1;
xe2 += A2;
xz += Ap;
}
e0 += B0;
e1 += B1;
e2 += B2;
z += Bp;
}
}
}
void LEE::drawFlatPixel(int z, pf p) {
int x = l, y = u;
QType A0 = edges[0]->getA(), A1 = edges[1]->getA(), A2 = edges[2]->getA(),
B0 = edges[0]->getB(), B1 = edges[1]->getB(), B2 = edges[2]->getB();
QType e0 = edges[0]->evaluate(l, u),
e1 = edges[1]->evaluate(l, u),
e2 = edges[2]->evaluate(l, u);
for (; y <= b; y++) {
QType xe0 = e0,
xe1 = e1,
xe2 = e2;
bool drawn = false;
for (x = l; x <= r; x++) {
if (((LONGLONG)xe0 | (LONGLONG)xe1 | (LONGLONG)xe2) >= 0) {
CALL_MEMBER_FN(*this, p)(x, y, z, drawn);
}
else if (((LONGLONG)xe0 | (LONGLONG)xe1 | (LONGLONG)xe2) < 0) {
if (drawn) {
break;
}
}
else if ((LONGLONG)xe0 == 0 && coveredEdge(A0, B0)) {
CALL_MEMBER_FN(*this, p)(x, y, z, drawn);
}
else if ((LONGLONG)xe1 == 0 && coveredEdge(A1, B1)) {
CALL_MEMBER_FN(*this, p)(x, y, z, drawn);
}
else if ((LONGLONG)xe2 == 0 && coveredEdge(A2, B2)) {
CALL_MEMBER_FN(*this, p)(x, y, z, drawn);
}
xe0 += A0;
xe1 += A1;
xe2 += A2;
}
e0 += B0;
e1 += B1;
e2 += B2;
}
}