109
|
1 #include <iostream>
|
|
2 #include "SpanC.h"
|
|
3 #include "triangle.h"
|
|
4 #include "polygon.h"
|
|
5 using namespace std;
|
|
6
|
|
7
|
|
8 // f1:x$B$NA}2CNL(B f2:y$B$NA}2CNL(B i:....... base:$B4pK\$H$J$kD:E@$N(By$B:BI8(B
|
|
9 static float calc(float f1, float f2,int i, float base){
|
|
10 float ans;
|
|
11 ans = f1/f2*i + base;
|
|
12 return ans;
|
|
13 }
|
|
14
|
|
15
|
|
16 static Vertex *vMid1(Vertex *vMid1,Vertex *vMin, Vertex *vMid, Vertex *vMax) {
|
|
17 float d,d1;
|
|
18
|
|
19 vMid1->y = vMid->y;
|
|
20 //vMid1->tex_y = vMid->tex_y;
|
|
21 d = (int)vMax->y-(int)vMin->y;
|
|
22 d1 = (int)vMid->y - (int)vMin->y;
|
|
23 /*
|
|
24
|
|
25 if (-1.0<=vMax->y&&vMax->y<=1.0) {
|
|
26 a = 0.5;
|
|
27 } else {
|
|
28 a = (vMax->x-vMin->x)/d;
|
|
29 }
|
|
30 */
|
|
31 //vMid1->tex_x = vMin->tex_x + a * (vMax->tex_x-vMin->tex_x);
|
|
32 vMid1->tex_x = calc(vMax->tex_x - vMin->tex_x, d, (int)d1 , vMin->tex_x);
|
|
33 vMid1->tex_y = calc(vMax->tex_y - vMin->tex_y, d, (int)d1 , vMin->tex_y);
|
|
34 //vMid1->x = vMin->x + a * (vMax->y -vMin->y);
|
|
35 vMid1->x = calc(vMax->x - vMin->x, d, (int)d1 , vMin->x);
|
|
36 //vMid1->z = vMin->z + a * (vMax->y -vMin->y);
|
|
37 vMid1->z = calc(vMax->z - vMin->z, d, (int)d1 , vMin->z);
|
|
38 //printf("x:%f y:%f z:%f tex_x:%f tex_y:%f\n",vMid1->x,vMid1->y,vMid1->z,vMid1->tex_x, vMid1->tex_y);
|
|
39 return vMid1;
|
|
40 }
|
|
41
|
|
42
|
|
43
|
|
44 void
|
|
45 Span_c::half_triangle(Vertex *vMin, Vertex *vMid, Vertex *vMid1,
|
|
46 SDL_Surface *image)
|
|
47 {
|
|
48
|
|
49 float tmp_z,tmp_tex1, tmp_tex2 ,tmp_tey1,tmp_tey2;
|
|
50 //tmp_x,tmp_y,tmp_ypos ......unused
|
|
51 float tex_x, tex_y,tex_z;
|
|
52 float tmp_xpos,tmp_end,tmp_zpos;
|
|
53 int tex_xpos,tex_ypos,tex_zpos;
|
|
54 int i,j;
|
|
55 float div_y;
|
|
56 Uint32 rgb;
|
|
57 float z,zpos;
|
|
58 int x,y;
|
|
59 int k =0;
|
|
60 int l = 1;
|
|
61 //float incli_x1, incli_x2;
|
|
62 //float incli_z1, incli_z2;
|
|
63 //float base_x1, base_x2 , base_z1, base_z2;
|
|
64 //int base_y;
|
|
65
|
|
66 /*
|
|
67 incli_x1 = vMid1->x - vMin->x;
|
|
68 incli_x2 = vMid->x - vMin->x;
|
|
69 incli_z1 = vMid1->z - vMin->z;
|
|
70 incli_z2 = vMid->z - vMin->z;
|
|
71 base_x1 = vMin->x;
|
|
72 base_x2 = vMin->x;
|
|
73 base_z1 = vMin->z;
|
|
74 base_z2 = vMin->z;
|
|
75 */
|
|
76 //cout << vMin->x << vMin->y << vMin->z << endl;
|
|
77 int start_y = (int)vMid->y;
|
|
78 int end_y = (int)vMin->y;
|
|
79
|
|
80 if (start_y<end_y) {
|
|
81 int i; i=end_y; end_y=start_y; start_y = i; //y$B$NF~$lBX$((B
|
|
82 //Vertex *v; v = vMin; vMin = vMid; vMid = v;
|
|
83 /*
|
|
84 incli_x1 = vMin->x - vMid->x; incli_x2 = vMin->x - vMid1->x;
|
|
85 incli_z1 = vMin->z - vMid->z; incli_z2 = vMin->z - vMid1->z;
|
|
86 base_z1 = vMid->z; base_z2 = vMid1->z;
|
|
87 base_y = (int)vMid->y;
|
|
88 */
|
|
89 k = 1;
|
|
90 l = -1;
|
|
91 }
|
|
92
|
|
93
|
|
94
|
|
95 div_y = start_y - end_y; // > 0
|
|
96
|
|
97 //if(div_y < 1 && div_y > -1) div_y = 1;
|
|
98 for(i = k; i < div_y+1; i++) {
|
|
99 //$B$3$3$G(Bspan$B$N:8C<$H1&C<$N(Bx,z$B$r5a$a$F$k(B
|
|
100 tmp_xpos = calc(vMid1->x - vMin->x ,div_y, i, vMin->x);
|
|
101 tmp_end = calc(vMid->x - vMin->x ,div_y, i, vMin->x);
|
|
102 tmp_z = calc(vMid1->z - vMin->z ,div_y, i, vMin->z);
|
|
103 tmp_zpos = calc(vMid->z - vMin->z ,div_y, i, vMin->z);
|
|
104 //printf("x:%f end:%f z:%f zpos:%f\n",tmp_xpos,tmp_end,tmp_z,tmp_zpos);
|
|
105 //$B$3$3$+$i(Bspan$B$N:8C<$H1&C<$KBP1~$9$k%F%/%9%A%c$r7W;;$9$k(B
|
|
106 tmp_tex1 =((i/(div_y)) * vMid1->tex_x) + \
|
|
107 ( ((div_y - i)/(div_y)) * vMin->tex_x);
|
|
108 tmp_tex2 =( (i/(div_y)) * vMid->tex_x) + \
|
|
109 ( ((div_y - i)/(div_y)) * vMin->tex_x);
|
|
110
|
|
111 tmp_tey1 =( (i/(div_y)) * vMid1->tex_y) + \
|
|
112 ( ((div_y - i)/(div_y)) * vMin->tex_y);
|
|
113 tmp_tey2 =( (i/(div_y)) * vMid->tex_y) + \
|
|
114 ( ((div_y - i)/(div_y)) * vMin->tex_y);
|
|
115
|
|
116 //$B$3$3$G:8C<$,(Bxpos$B$+(Bend$B$rH=CG$7!"IA2h$9$k(Bspan$B$r:n$k(B
|
|
117 if(tmp_xpos > tmp_end) {
|
|
118 x = (int)tmp_end;
|
|
119 y = (int)vMin->y + i*l;
|
|
120 end = (int)(tmp_xpos)-(int)(tmp_end)+1;
|
|
121 z = tmp_zpos;
|
|
122 zpos = tmp_z;
|
|
123 tex1 = tmp_tex2;
|
|
124 tex2 = tmp_tex1;
|
|
125 tey1 = tmp_tey2;
|
|
126 tey2 = tmp_tey1;
|
|
127 } else {
|
|
128 x = (int)tmp_xpos;
|
|
129 y = (int)vMin->y + i*l;
|
|
130 end = (int)(tmp_end)-(int)(tmp_xpos)+1;
|
|
131 z = tmp_z;
|
|
132 zpos = tmp_zpos;
|
|
133 tex1 = tmp_tex1;
|
|
134 tex2 = tmp_tex2;
|
|
135 tey1 = tmp_tey1;
|
|
136 tey2 = tmp_tey2;
|
|
137 }
|
|
138 //printf("%d:%f,%f ",(int)vMin->y + i,xpos,end);
|
|
139 //printf("%d:%d:%d:x:%d,end:%d\n",x, y , end,(int)(tmp_end),(int)(tmp_xpos));
|
|
140 //printf("x:%d y:%d end:%d z:%f zpos:%f\n",x,y,end,z , zpos);
|
|
141 //printf("tex1:%f tex2:%f tey1:%f tey2:%f\n",tex1,tex2,tey1,tey2);
|
|
142 if(end == 1) {
|
|
143 //printf("tex_x:%f tex_y:%f\n",tex1,tex2);
|
|
144 if(tex1 > 1) tex1 = 1;
|
|
145 if(tey1 > 1) tey1 = 1;
|
|
146 tex_xpos = (int)((image->h-1) * tex1);
|
|
147 tex_ypos = (int)((image->w-1) * tey1);
|
|
148 tex_zpos = (int)z;
|
|
149 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
150 //printf("image->h:%d tex_x:%f\n",image->h,tex1);
|
|
151 rgb = p->get_rgb(tex_xpos,tex_ypos);
|
|
152 //viewer->write_pixel(x,y,zpos,rgb);
|
|
153 }else {
|
|
154 for(j = 0; j < end; j++) {
|
|
155 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
156 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
157 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
158 if(tex_x > 1) tex_x = 1;
|
|
159 if(tex_y > 1) tex_y = 1;
|
|
160 tex_xpos = (int)((image->h-1) * tex_x);
|
|
161 tex_ypos = (int)((image->w-1) * tex_y);
|
|
162 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
163 //printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
|
|
164 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
165 rgb = p->get_rgb(tex_xpos,tex_ypos);
|
|
166 //viewer->write_pixel(j+x,y,tex_z,rgb);
|
|
167 }
|
|
168 }
|
|
169 }
|
|
170 }
|
|
171
|
|
172 void Span_c::create_span(Triangle *tri,SDL_Surface *image) {
|
|
173 Vertex *vMin , *vMid, *vMax;
|
|
174 /*
|
|
175 float tmp_z,tmp_tex1, tmp_tex2 ,tmp_tey1,tmp_tey2;
|
|
176 //tmp_x,tmp_y,tmp_ypos ......unused
|
|
177 float tex_x, tex_y,tex_z;
|
|
178 float tmp_xpos,tmp_end,tmp_zpos;
|
|
179 int top_triangle;
|
|
180 int tex_xpos,tex_ypos,tex_zpos;
|
|
181 int i,j;
|
|
182 float div_x,div_y;
|
|
183 float z,zpos;
|
|
184 Uint32 rgb;
|
|
185 */
|
|
186
|
|
187 //cout << "x = " << tri->vertex1->x << endl;
|
|
188 if(tri->vertex1->y <= tri->vertex2->y) {
|
|
189 if(tri->vertex2->y <= tri->vertex3->y) {
|
|
190 //printf("condition 1\n");
|
|
191 vMin = tri->vertex1;
|
|
192 vMid = tri->vertex2;
|
|
193 vMax = tri->vertex3;
|
|
194 } else if(tri->vertex3->y <= tri->vertex1->y) {
|
|
195 //printf("condition 2\n");
|
|
196 vMin = tri->vertex3;
|
|
197 vMid = tri->vertex1;
|
|
198 vMax = tri->vertex2;
|
|
199 } else {
|
|
200 //printf("condition 3\n");
|
|
201 vMin = tri->vertex1;
|
|
202 vMid = tri->vertex3;
|
|
203 vMax = tri->vertex2;
|
|
204 }
|
|
205 } else {
|
|
206 if(tri->vertex1->y <= tri->vertex3->y) {
|
|
207 //printf("condition 4\n");
|
|
208 vMin = tri->vertex2;
|
|
209 vMid = tri->vertex1;
|
|
210 vMax = tri->vertex3;
|
|
211 } else if(tri->vertex3->y <= tri->vertex2->y) {
|
|
212 //printf("condition 5\n");
|
|
213 vMin = tri->vertex3;
|
|
214 vMid = tri->vertex2;
|
|
215 vMax = tri->vertex1;
|
|
216 } else {
|
|
217 //printf("condition 6\n");
|
|
218 vMin = tri->vertex2;
|
|
219 vMid = tri->vertex3;
|
|
220 vMax = tri->vertex1;
|
|
221 }
|
|
222 }
|
|
223
|
|
224 Vertex *vMid10 = new Vertex(0,0,0,0,0);
|
|
225 vMid1(vMid10,vMin,vMid,vMax);
|
|
226
|
|
227 half_triangle(vMin,vMid,vMid10,image);
|
|
228 half_triangle(vMax,vMid,vMid10,image);
|
|
229
|
|
230 free(vMid10);
|
|
231 #if 0
|
|
232 //cout << vMin->x << vMin->y << vMin->z << endl;
|
|
233 div_x = (int)vMid->y - (int)vMin->y;
|
|
234 if(div_x < 1 && div_x > -1) div_x = 1;
|
|
235 div_y = (int)vMax->y - (int)vMin->y;
|
|
236 if(div_y < 1 && div_y > -1) div_y = 1;
|
|
237
|
|
238 for(i = 0; i < div_x+1; i++) {
|
|
239 //$B$3$3$G(Bspan$B$N:8C<$H1&C<$N(Bx,z$B$r5a$a$F$k(B
|
|
240 tmp_xpos = calc(vMax->x - vMin->x,div_y, i, vMin->x);
|
|
241 tmp_end = calc(vMid->x - vMin->x,div_x, i, vMin->x);
|
|
242 tmp_z = calc(vMax->z - vMin->z,div_y, i, vMin->z);
|
|
243 tmp_zpos = calc(vMid->z - vMin->z,div_x, i, vMin->z);
|
|
244
|
|
245 //$B$3$3$+$i(Bspan$B$N:8C<$H1&C<$KBP1~$9$k%F%/%9%A%c$r7W;;$9$k(B
|
|
246 tmp_tex1 =( (i/(div_y)) * vMax->tex_x) + \
|
|
247 ( ((div_y - i)/(div_y)) * vMin->tex_x);
|
|
248 tmp_tex2 =( (i/(div_x)) * vMid->tex_x) + \
|
|
249 ( ((div_x - i)/(div_x)) * vMin->tex_x);
|
|
250
|
|
251 tmp_tey1 =( (i/(div_y)) * vMax->tex_y) + \
|
|
252 ( ((div_y - i)/(div_y)) * vMin->tex_y);
|
|
253 tmp_tey2 =( (i/(div_x)) * vMid->tex_y) + \
|
|
254 ( ((div_x - i)/(div_x)) * vMin->tex_y);
|
|
255
|
|
256 //$B$3$3$G:8C<$,(Bxpos$B$+(Bend$B$rH=CG$7!"IA2h$9$k(Bspan$B$r:n$k(B
|
|
257 if(tmp_xpos > tmp_end) {
|
|
258 x = (int)tmp_end;
|
|
259 y = (int)vMin->y + i;
|
|
260 end = (int)(tmp_xpos)-(int)(tmp_end)+1;
|
|
261 z = tmp_z;
|
|
262 zpos = tmp_zpos;
|
|
263 tex1 = tmp_tex2;
|
|
264 tex2 = tmp_tex1;
|
|
265 tey1 = tmp_tey2;
|
|
266 tey2 = tmp_tey1;
|
|
267 } else {
|
|
268 x = (int)tmp_xpos;
|
|
269 y = (int)vMin->y + i;
|
|
270 end = (int)(tmp_end)-(int)(tmp_xpos)+1;
|
|
271 z = tmp_zpos;
|
|
272 zpos = tmp_z;
|
|
273 tex1 = tmp_tex1;
|
|
274 tex2 = tmp_tex2;
|
|
275 tey1 = tmp_tey1;
|
|
276 tey2 = tmp_tey2;
|
|
277 }
|
|
278 //printf("%d:%f,%f ",(int)vMin->y + i,xpos,end);
|
|
279 //printf("%d:%d:%d:x:%d,end:%d\n",x, y , end,(int)(tmp_end),(int)(tmp_xpos));
|
|
280 //printf("x:%d y:%d end:%d z:%f zpos:%f\n",x,y,end,z , zpos);
|
|
281 //printf("tex1:%f tex2:%f tey1:%f tey2:%f\n",tex1,tex2,tey1,tey2);
|
|
282 if(end == 1) {
|
|
283 //printf("tex_x:%f tex_y:%f\n",tex1,tex2);
|
|
284 if(tex1 > 1) tex1 = 1;
|
|
285 if(tey1 > 1) tey1 = 1;
|
|
286 tex_xpos = (int)(image->h-1) * tex1;
|
|
287 tex_ypos = (int)(image->w-1) * tey1;
|
|
288 tex_zpos = z;
|
|
289 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
290 //printf("image->h:%d tex_x:%f\n",image->h,tex1);
|
|
291 rgb = p->get_rgb(tex_xpos,tex_ypos);
|
|
292 //viewer->write_pixel(x,y,zpos,rgb);
|
|
293 }else {
|
|
294 for(j = 0; j < end; j++) {
|
|
295 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
296 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
297 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
298 if(tex_x > 1) tex_x = 1;
|
|
299 if(tex_y > 1) tex_y = 1;
|
|
300 tex_xpos = (int)(image->h-1) * tex_x;
|
|
301 tex_ypos = (int)(image->w-1) * tex_y;
|
|
302 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
303 //printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
|
|
304 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
305 rgb = p->get_rgb(tex_xpos,tex_ypos);
|
|
306 //viewer->write_pixel(j+x,y,tex_z,rgb);
|
|
307 }
|
|
308 }
|
|
309 }
|
|
310
|
|
311 top_triangle = (int)vMid->y - (int)vMin->y;
|
|
312 div_x = (int)vMax->y - (int)vMid->y;
|
|
313 if(div_x < 1 && div_x > -1) div_x = 1;
|
|
314 div_y = (int)vMax->y - (int)vMin->y;
|
|
315 if(div_y < 1 && div_y > -1) div_y = 1;
|
|
316
|
|
317 for(i = 0; i < div_x; i++) {
|
|
318 //$B$3$3$G(Bspan$B$N:8C<$H1&C<$N(Bx,z$B$r5a$a$F$k(B
|
|
319 tmp_xpos = calc(vMax->x-vMin->x, div_y, (i+1+top_triangle), vMin->x);
|
|
320 tmp_end = calc(vMax->x-vMid->x, div_x, (i+1), vMid->x);
|
|
321 tmp_z = calc(vMax->z-vMin->z, div_y, (i+1+top_triangle), vMin->z);
|
|
322 tmp_zpos = calc(vMax->z-vMid->z, div_x, (i+1), vMid->z);
|
|
323
|
|
324 tmp_tex1 =( ((i + 1 +top_triangle)/(div_y)) * vMax->tex_x) + \
|
|
325 ( ((div_y - i - 1 - top_triangle)/(div_y)) * vMin->tex_x);
|
|
326 tmp_tex2 =( ((i+1)/(div_x)) * vMax->tex_x) + \
|
|
327 ( ((div_x - i -1)/(div_x)) * vMid->tex_x);
|
|
328
|
|
329 tmp_tey1 =( ((i+1+top_triangle)/(div_y)) * vMax->tex_y) + \
|
|
330 ( ((div_y - i - 1 - top_triangle)/(div_y)) * vMin->tex_y);
|
|
331 tmp_tey2 =( ((i+1)/(div_x)) * vMax->tex_y) + \
|
|
332 ( ((div_x - i -1)/(div_x)) * vMid->tex_y);
|
|
333 if(tmp_xpos > tmp_end) {
|
|
334 x = (int)tmp_end;
|
|
335 y = (int)vMid->y+i+1;
|
|
336 end = (int)(tmp_xpos)-(int)(tmp_end)+1;
|
|
337 z = tmp_z;
|
|
338 zpos = tmp_zpos;
|
|
339 tex1 = tmp_tex2;
|
|
340 tex2 = tmp_tex1;
|
|
341 tey1 = tmp_tey2;
|
|
342 tey2 = tmp_tey1;
|
|
343 } else {
|
|
344 x = (int)tmp_xpos;
|
|
345 y = (int)vMid->y+i+1;
|
|
346 end = (int)(tmp_end)-(int)(tmp_xpos)+1;
|
|
347 z = tmp_zpos;
|
|
348 zpos = tmp_z;
|
|
349 tex1 = tmp_tex1;
|
|
350 tex2 = tmp_tex2;
|
|
351 tey1 = tmp_tey1;
|
|
352 tey2 = tmp_tey2;
|
|
353 }
|
|
354 if(end == 0) end = 1;
|
|
355 //printf("%d:%d:%d\n",x, y , end);
|
|
356 //printf("x:%d y:%d end:%d z:%f zpos:%f\n",x,y,end,z,zpos);
|
|
357 //printf("tex1:%f tex2:%f tey1:%f tey2:%f\n",tex1,tex2,tey1,tey2);
|
|
358 if(end == 1) {
|
|
359 //printf("tex_x:%f tex_y:%f\n",tex1,tex2);
|
|
360 if(tex1 > 1) tex1 = 1;
|
|
361 if(tey1 > 1) tey1 = 1;
|
|
362 tex_xpos = (int)(image->h-1) * tex1;
|
|
363 tex_ypos = (int)(image->w-1) * tey1;
|
|
364 tex_zpos = z;
|
|
365 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
366 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
367 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
368 rgb = p->get_rgb(tex_xpos,tex_ypos);
|
|
369 //viewer->write_pixel(x,y,tex_zpos,rgb);
|
|
370 }else {
|
|
371 for(j = 0; j < end; j++) {
|
|
372 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
373 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
374 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
375 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
376 if(tex_x > 1) tex_x = 1;
|
|
377 if(tex_y > 1) tex_y = 1;
|
|
378 tex_xpos = (int)(image->h-2) * tex_x;
|
|
379 tex_ypos = (int)(image->w-1) * tex_y;
|
|
380 //printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
|
|
381 //printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);
|
|
382 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
383 //printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
|
|
384 rgb = p->get_rgb(tex_xpos,tex_ypos);
|
|
385 //viewer->write_pixel(j+x,y,tex_z,rgb);
|
|
386 }
|
|
387 }
|
|
388 }
|
|
389 #endif
|
|
390 }
|
|
391
|
|
392 // i:$BJ,;R!"(Bf1:$BD9$5(B f2:$BBP1~$9$kD:E@(B1 f3:$BBP1~$9$kD:E@(B2
|
|
393 /*
|
|
394 float Span::calc2(int i, float f1, float f2, float f3) {
|
|
395 float ans;
|
|
396 ans = i/f1 * f2 + (f1-i)/f1 * f3;
|
|
397
|
|
398 */
|