507
|
1 #include <iostream>
|
|
2 #include <SDL.h>
|
|
3 #include <SDL_opengl.h>
|
|
4 #include <SDL_image.h>
|
|
5 #include "polygon.h"
|
|
6 #include "xml.h"
|
|
7 #include "sys.h"
|
|
8 #include "triangle.h"
|
|
9 #include "vertex.h"
|
|
10 #include "Span.h"
|
|
11 #include "SpanC.h"
|
|
12 #include "scene_graph_pack.h"
|
|
13 #include "error.h"
|
|
14 #include "viewer_types.h"
|
|
15 using namespace std;
|
|
16
|
|
17 //extern int decode(char *cont, char *file_name);
|
|
18 extern int decode(char *cont, FILE *outfile);
|
|
19
|
|
20
|
|
21 Polygon::Polygon(void)
|
|
22 {
|
|
23 xyz[0] = 0;
|
|
24 xyz[1] = 0;
|
|
25 xyz[2] = 0;
|
|
26 xyz[3] = 1;
|
|
27 c_xyz[0] = 0;
|
|
28 c_xyz[1] = 0;
|
|
29 c_xyz[2] = 0;
|
|
30 c_xyz[3] = 1;
|
|
31 angle[0] = 0;
|
|
32 angle[1] = 0;
|
|
33 angle[2] = 0;
|
|
34 angle[3] = 1;
|
|
35
|
861
|
36
|
|
37 #if !SPE_CREATE_POLYGON
|
|
38
|
507
|
39 for (int i = 0; i < 16; i++) {
|
|
40 matrix[i] = 0;
|
|
41 }
|
861
|
42
|
|
43 #endif
|
|
44
|
507
|
45 }
|
|
46
|
|
47 void
|
|
48 Polygon::position_init(void)
|
|
49 {
|
|
50 xyz[0] = 0;
|
|
51 xyz[1] = 0;
|
|
52 xyz[2] = 0;
|
|
53 xyz[3] = 1;
|
|
54 c_xyz[0] = 0;
|
|
55 c_xyz[1] = 0;
|
|
56 c_xyz[2] = 0;
|
|
57 c_xyz[3] = 1;
|
|
58 angle[0] = 0;
|
|
59 angle[1] = 0;
|
|
60 angle[2] = 0;
|
|
61 angle[3] = 1;
|
|
62
|
861
|
63
|
|
64 #if !SPE_CREATE_POLYGON
|
|
65
|
507
|
66 for (int i = 0; i < 16; i++) {
|
|
67 matrix[i] = 0;
|
|
68 }
|
861
|
69
|
|
70 #endif
|
|
71
|
507
|
72 }
|
|
73
|
|
74 #if 0
|
|
75 void Polygon::draw(SceneGraphPack *sgp)
|
|
76 {
|
|
77 float xyz1[4],xyz2[4],xyz3[4];
|
|
78
|
|
79 /***SceneGraphUpdate***/
|
|
80 //sgp_update();
|
|
81 for (int i = 0; i < sgp->info.size; i++) {
|
|
82 SceneGraphNode node = sgp->node[i];
|
|
83
|
|
84 /***draw***/
|
|
85 int n,nt;
|
|
86 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) {
|
|
87 xyz1[0] = node.vertex[n];
|
|
88 xyz1[1] = node.vertex[n+1];
|
|
89 xyz1[2] = node.vertex[n+2]*-1;
|
|
90 xyz1[3] = 1;
|
|
91 xyz2[0] = node.vertex[n+3];
|
|
92 xyz2[1] = node.vertex[n+3+1];
|
|
93 xyz2[2] = node.vertex[n+3+2]*-1;
|
|
94 xyz2[3] = 1;
|
|
95 xyz3[0] = node.vertex[n+6];
|
|
96 xyz3[1] = node.vertex[n+6+1];
|
|
97 xyz3[2] = node.vertex[n+6+2]*-1;
|
|
98 xyz3[3] = 1;
|
|
99
|
|
100 rotate(xyz1, node.translation);
|
|
101 rotate(xyz2, node.translation);
|
|
102 rotate(xyz3, node.translation);
|
|
103
|
|
104 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]);
|
|
105 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]);
|
|
106 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]);
|
|
107
|
|
108 Triangle *tri = new Triangle(ver1,ver2,ver3);
|
|
109 Span_c *span = new Span_c();
|
|
110 span->viewer = viewer;
|
|
111 span->p = this;
|
|
112 span->create_span(tri,texture_image);
|
|
113 delete ver1;
|
|
114 delete ver2;
|
|
115 delete ver3;
|
|
116 delete tri;
|
|
117 delete span;
|
|
118 }
|
|
119 }
|
|
120 }
|
|
121
|
|
122
|
|
123 void Polygon::draw(PolygonPack *pp)
|
|
124 {
|
|
125 for(int n=0; n<pp->info.size; n++)
|
|
126 {
|
|
127 Vertex *ver1 = new Vertex(pp->tri[n].ver1.x,pp->tri[n].ver1.y,pp->tri[n].ver1.z,pp->tri[n].ver1.tex_x,pp->tri[n].ver1.tex_y);
|
|
128 Vertex *ver2 = new Vertex(pp->tri[n].ver2.x,pp->tri[n].ver2.y,pp->tri[n].ver2.z,pp->tri[n].ver2.tex_x,pp->tri[n].ver2.tex_y);
|
|
129 Vertex *ver3 = new Vertex(pp->tri[n].ver3.x,pp->tri[n].ver3.y,pp->tri[n].ver3.z,pp->tri[n].ver3.tex_x,pp->tri[n].ver3.tex_y);
|
|
130
|
|
131 Triangle *tri = new Triangle(ver1,ver2,ver3);
|
|
132 Span_c *span = new Span_c();
|
|
133 span->viewer = viewer;
|
|
134 span->p = this;
|
|
135 span->create_span(tri,texture_image);
|
|
136 delete ver1;
|
|
137 delete ver2;
|
|
138 delete ver3;
|
|
139 delete tri;
|
|
140 delete span;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 void Polygon::draw(SpanPack *sp)
|
|
145 {
|
|
146 Span *span;
|
|
147
|
|
148 for (int n = 0; n < sp->info.size; n++) {
|
|
149 span = &sp->span[n];
|
|
150
|
|
151 //int x = span->x;
|
|
152 //int y = span->y;
|
|
153 float z = span->start_z;
|
|
154 int end = span->length_x;
|
|
155 float zpos = span->end_z;
|
|
156 float tex1 = span->tex_x1;
|
|
157 float tex2 = span->tex_x2;
|
|
158 float tey1 = span->tex_y1;
|
|
159 float tey2 = span->tex_y2;
|
|
160 Uint32 rgb;
|
|
161 int tex_xpos;
|
|
162 int tex_ypos;
|
|
163 int tex_zpos;
|
|
164 float tex_x, tex_y, tex_z;
|
|
165
|
|
166 if (end == 1) {
|
|
167 tex_xpos = (int)((span->tex_height-1) * tex1);
|
|
168 tex_ypos = (int)((span->tex_width-1) * tey1);
|
|
169 tex_zpos = (int)z;
|
|
170 rgb = get_rgb(tex_xpos, tex_ypos);
|
|
171 //viewer->write_pixel(x, y, zpos, rgb);
|
|
172 } else {
|
|
173 for (int j = 0; j < end; j++) {
|
|
174 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
|
|
175 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
|
|
176 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
|
|
177 if (tex_x > 1) tex_x = 1;
|
|
178 if (tex_y > 1) tex_y = 1;
|
|
179 tex_xpos = (int)((span->tex_height-1) * tex_x);
|
|
180 tex_ypos = (int)((span->tex_width-1) * tex_y);
|
|
181 rgb = get_rgb(tex_xpos,tex_ypos);
|
|
182 //viewer->write_pixel(j + x, y, tex_z, rgb);
|
|
183 }
|
|
184 }
|
|
185 }
|
|
186 }
|
|
187
|
|
188 #endif
|
|
189
|
|
190 void Polygon::pickup_coordinate(char *cont)
|
|
191 {
|
860
|
192
|
|
193 #if SPE_CREATE_POLYGON
|
|
194
|
|
195 TrianglePackPtr cur = tri_pack;
|
|
196
|
|
197 for(int n=0; n<size; n+=3)
|
|
198 {
|
|
199
|
|
200 cont = pickup_float(cont, &cur->ver1.x);
|
|
201 cont = pickup_float(cont, &cur->ver1.y);
|
|
202 cont = pickup_float(cont, &cur->ver1.z);
|
|
203
|
|
204 cont = pickup_float(cont, &cur->ver2.x);
|
|
205 cont = pickup_float(cont, &cur->ver2.y);
|
|
206 cont = pickup_float(cont, &cur->ver2.z);
|
|
207
|
|
208 cont = pickup_float(cont, &cur->ver3.x);
|
|
209 cont = pickup_float(cont, &cur->ver3.y);
|
|
210 cont = pickup_float(cont, &cur->ver3.z);
|
|
211
|
|
212 cur = cur + 1;
|
|
213
|
|
214 if (cont == NULL)
|
|
215 {
|
|
216 cout << "Analyzing obj data failed coordinate\n";
|
|
217 }
|
|
218 }
|
|
219
|
|
220 #else
|
|
221
|
507
|
222 for(int n=0; n<size*3; n+=3)
|
|
223 {
|
|
224 cont = pickup_float(cont, coord_xyz+n);
|
|
225 cont = pickup_float(cont, coord_xyz+n+1);
|
|
226 cont = pickup_float(cont, coord_xyz+n+2);
|
|
227
|
|
228 if (cont == NULL)
|
|
229 {
|
|
230 cout << "Analyzing obj data failed coordinate\n";
|
|
231 }
|
|
232 }
|
860
|
233
|
|
234 #endif
|
|
235
|
507
|
236 }
|
|
237
|
|
238 void Polygon::pickup_normal(char *cont)
|
|
239 {
|
860
|
240
|
|
241 #if SPE_CREATE_POLYGON
|
|
242
|
|
243 TrianglePackPtr cur = tri_pack;
|
|
244
|
|
245 for(int n=0; n<size; n+=3)
|
|
246 {
|
|
247
|
|
248 cont = pickup_float(cont, &cur->normal1.x);
|
|
249 cont = pickup_float(cont, &cur->normal1.y);
|
|
250 cont = pickup_float(cont, &cur->normal1.z);
|
|
251
|
|
252 cont = pickup_float(cont, &cur->normal2.x);
|
|
253 cont = pickup_float(cont, &cur->normal2.y);
|
|
254 cont = pickup_float(cont, &cur->normal2.z);
|
|
255
|
|
256 cont = pickup_float(cont, &cur->normal3.x);
|
|
257 cont = pickup_float(cont, &cur->normal3.y);
|
|
258 cont = pickup_float(cont, &cur->normal3.z);
|
|
259
|
|
260 cur = cur + 1;
|
|
261
|
|
262 if (cont == NULL)
|
|
263 {
|
|
264 cout << "Analyzing obj data failed coordinate\n";
|
|
265 }
|
|
266 }
|
|
267
|
|
268 #else
|
|
269
|
|
270
|
507
|
271 for (int n = 0; n<size*3; n += 3)
|
|
272 {
|
|
273 cont = pickup_float(cont, normal+n);
|
|
274 cont = pickup_float(cont, normal+n+1);
|
|
275 cont = pickup_float(cont, normal+n+2);
|
|
276
|
|
277 if (cont == NULL)
|
|
278 {
|
|
279 cout << "Analyzing obj data failed normal\n";
|
|
280 }
|
|
281 }
|
860
|
282
|
|
283 #endif
|
|
284
|
507
|
285 }
|
|
286
|
|
287 void Polygon::pickup_model(char *cont)
|
|
288 {
|
|
289 cont = pickup_float(cont,c_xyz);
|
|
290 cont = pickup_float(cont,c_xyz+1);
|
|
291 cont = pickup_float(cont,c_xyz+2);
|
|
292
|
|
293 if (cont == NULL)
|
|
294 {
|
|
295 cout << "Analyzing obj data failed model\n";
|
|
296 }
|
|
297 }
|
|
298
|
|
299 void Polygon::pickup_texture(char *cont)
|
|
300 {
|
860
|
301
|
|
302 #if SPE_CREATE_POLYGON
|
|
303
|
|
304 TrianglePackPtr cur = tri_pack;
|
|
305
|
|
306 for(int n=0; n<size; n+=3)
|
|
307 {
|
|
308
|
|
309 cont = pickup_float(cont, &cur->ver1.tex_x);
|
|
310 cont = pickup_float(cont, &cur->ver1.tex_y);
|
|
311
|
|
312 cont = pickup_float(cont, &cur->ver2.tex_x);
|
|
313 cont = pickup_float(cont, &cur->ver2.tex_y);
|
|
314
|
|
315 cont = pickup_float(cont, &cur->ver3.tex_x);
|
|
316 cont = pickup_float(cont, &cur->ver3.tex_y);
|
|
317
|
|
318 cur = cur + 1;
|
|
319
|
|
320 if (cont == NULL)
|
|
321 {
|
|
322 cout << "Analyzing obj data failed coordinate\n";
|
|
323 }
|
|
324 }
|
|
325
|
|
326 #else
|
|
327
|
507
|
328 for (int n = 0; n < size*3; n += 3)
|
|
329 {
|
|
330 cont = pickup_float(cont, coord_tex+n);
|
|
331 cont = pickup_float(cont, coord_tex+n+1);
|
|
332 coord_tex[n+2] = 1.0;
|
|
333
|
|
334 if (cont == NULL)
|
|
335 {
|
|
336 cout << "Analyzing obj data failed texture\n";
|
|
337 }
|
|
338 }
|
860
|
339
|
|
340 #endif
|
|
341
|
507
|
342 }
|
|
343
|
|
344 char *get_pixel(int tx, int ty, SDL_Surface *texture_image)
|
|
345 {
|
|
346 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx));
|
|
347 }
|
|
348
|
|
349 unsigned my_ntohl(unsigned u) {
|
|
350 // rr gg bb 00
|
|
351 // rr
|
|
352 // bb gg rr
|
|
353 //unsigned u1 = ((u&0xff)<<24) +
|
|
354 // ((u&0xff00)<<8) +
|
|
355 // ((u&0xff0000)>>8) +
|
|
356 // ((u&0xff000000)>>24);
|
|
357 unsigned u1;
|
|
358 unsigned b = (u&0xff000000)>>24;
|
|
359 unsigned g = (u&0xff0000)>>16;
|
|
360 unsigned r = (u&0xff00)>>8;
|
|
361 u1 = r + (g<<8) + (b<<16);
|
|
362 //printf("pixel %x->%x\n",u,u1);
|
|
363 return u1;
|
|
364 }
|
|
365
|
|
366 Uint32 Polygon::get_rgb(int tx, int ty)
|
|
367 {
|
|
368 SDL_PixelFormat *fmt;
|
|
369 //Uint32 temp, pixel;
|
|
370 Uint8 red, green, blue;
|
|
371
|
|
372 fmt = texture_image->format;
|
|
373
|
|
374 if (tx<0) tx = 0;
|
|
375 if (texture_image->w-1< tx) tx = texture_image->w-1 ;
|
|
376 if (ty<0) ty = 0;
|
|
377 if (texture_image->h-1< ty) ty = texture_image->h-1 ;
|
|
378
|
|
379
|
|
380
|
|
381 //SDL_LockSurface(texture_image);
|
|
382 char *p = get_pixel(tx,ty,texture_image);
|
|
383 #if 0
|
|
384 pixel = my_ntohl(*(Uint32*)p);
|
|
385 //printf("pixel = %d\n", pixel);
|
|
386 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel);
|
|
387 //SDL_UnlockSurface(texture_image);
|
|
388
|
|
389 temp = pixel&fmt->Rmask;
|
|
390 temp = temp>>fmt->Rshift;
|
|
391 temp = temp<<fmt->Rloss;
|
|
392 red = (Uint8)temp;
|
|
393
|
|
394 temp = pixel&fmt->Gmask;
|
|
395 temp = temp>>fmt->Gshift;
|
|
396 temp = temp<<fmt->Gloss;
|
|
397 green = (Uint8)temp;
|
|
398
|
|
399 temp = pixel&fmt->Bmask;
|
|
400 temp = temp>>fmt->Bshift;
|
|
401 temp = temp<<fmt->Bloss;
|
|
402 blue = (Uint8)temp;
|
|
403 #endif
|
|
404 blue = (Uint8) p[0];
|
|
405 green = (Uint8) p[1];
|
|
406 red = (Uint8) p[2];
|
|
407
|
|
408 //printf("tx = %d ty = %d ", tx,ty);
|
|
409 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue);
|
|
410
|
|
411 SDL_PixelFormat *pf = NULL;
|
|
412 //pf = viewer->screen->format;
|
|
413
|
|
414 //cout << SDL_MapRGB(pf, red, green, blue) << endl;
|
|
415 return SDL_MapRGB(pf, red, green, blue);
|
|
416 }
|