comparison Renderer/Engine/polygon.cc @ 507:735f76483bb2

Reorganization..
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 12 Oct 2009 09:39:35 +0900
parents
children 649e4cb84683
comparison
equal deleted inserted replaced
506:1d4a8a86f26b 507:735f76483bb2
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
36 for (int i = 0; i < 16; i++) {
37 matrix[i] = 0;
38 }
39 }
40
41 void
42 Polygon::position_init(void)
43 {
44 xyz[0] = 0;
45 xyz[1] = 0;
46 xyz[2] = 0;
47 xyz[3] = 1;
48 c_xyz[0] = 0;
49 c_xyz[1] = 0;
50 c_xyz[2] = 0;
51 c_xyz[3] = 1;
52 angle[0] = 0;
53 angle[1] = 0;
54 angle[2] = 0;
55 angle[3] = 1;
56
57 for (int i = 0; i < 16; i++) {
58 matrix[i] = 0;
59 }
60 }
61
62 #if 0
63 void Polygon::draw(SceneGraphPack *sgp)
64 {
65 float xyz1[4],xyz2[4],xyz3[4];
66
67 /***SceneGraphUpdate***/
68 //sgp_update();
69 for (int i = 0; i < sgp->info.size; i++) {
70 SceneGraphNode node = sgp->node[i];
71
72 /***draw***/
73 int n,nt;
74 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) {
75 xyz1[0] = node.vertex[n];
76 xyz1[1] = node.vertex[n+1];
77 xyz1[2] = node.vertex[n+2]*-1;
78 xyz1[3] = 1;
79 xyz2[0] = node.vertex[n+3];
80 xyz2[1] = node.vertex[n+3+1];
81 xyz2[2] = node.vertex[n+3+2]*-1;
82 xyz2[3] = 1;
83 xyz3[0] = node.vertex[n+6];
84 xyz3[1] = node.vertex[n+6+1];
85 xyz3[2] = node.vertex[n+6+2]*-1;
86 xyz3[3] = 1;
87
88 rotate(xyz1, node.translation);
89 rotate(xyz2, node.translation);
90 rotate(xyz3, node.translation);
91
92 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]);
93 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]);
94 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]);
95
96 Triangle *tri = new Triangle(ver1,ver2,ver3);
97 Span_c *span = new Span_c();
98 span->viewer = viewer;
99 span->p = this;
100 span->create_span(tri,texture_image);
101 delete ver1;
102 delete ver2;
103 delete ver3;
104 delete tri;
105 delete span;
106 }
107 }
108 }
109
110
111 void Polygon::draw(PolygonPack *pp)
112 {
113 for(int n=0; n<pp->info.size; n++)
114 {
115 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);
116 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);
117 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);
118
119 Triangle *tri = new Triangle(ver1,ver2,ver3);
120 Span_c *span = new Span_c();
121 span->viewer = viewer;
122 span->p = this;
123 span->create_span(tri,texture_image);
124 delete ver1;
125 delete ver2;
126 delete ver3;
127 delete tri;
128 delete span;
129 }
130 }
131
132 void Polygon::draw(SpanPack *sp)
133 {
134 Span *span;
135
136 for (int n = 0; n < sp->info.size; n++) {
137 span = &sp->span[n];
138
139 //int x = span->x;
140 //int y = span->y;
141 float z = span->start_z;
142 int end = span->length_x;
143 float zpos = span->end_z;
144 float tex1 = span->tex_x1;
145 float tex2 = span->tex_x2;
146 float tey1 = span->tex_y1;
147 float tey2 = span->tex_y2;
148 Uint32 rgb;
149 int tex_xpos;
150 int tex_ypos;
151 int tex_zpos;
152 float tex_x, tex_y, tex_z;
153
154 if (end == 1) {
155 tex_xpos = (int)((span->tex_height-1) * tex1);
156 tex_ypos = (int)((span->tex_width-1) * tey1);
157 tex_zpos = (int)z;
158 rgb = get_rgb(tex_xpos, tex_ypos);
159 //viewer->write_pixel(x, y, zpos, rgb);
160 } else {
161 for (int j = 0; j < end; j++) {
162 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
163 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
164 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
165 if (tex_x > 1) tex_x = 1;
166 if (tex_y > 1) tex_y = 1;
167 tex_xpos = (int)((span->tex_height-1) * tex_x);
168 tex_ypos = (int)((span->tex_width-1) * tex_y);
169 rgb = get_rgb(tex_xpos,tex_ypos);
170 //viewer->write_pixel(j + x, y, tex_z, rgb);
171 }
172 }
173 }
174 }
175
176 #endif
177
178 void Polygon::pickup_coordinate(char *cont)
179 {
180 for(int n=0; n<size*3; n+=3)
181 {
182 cont = pickup_float(cont, coord_xyz+n);
183 cont = pickup_float(cont, coord_xyz+n+1);
184 cont = pickup_float(cont, coord_xyz+n+2);
185
186 if (cont == NULL)
187 {
188 cout << "Analyzing obj data failed coordinate\n";
189 }
190 }
191 }
192
193 void Polygon::pickup_normal(char *cont)
194 {
195 for (int n = 0; n<size*3; n += 3)
196 {
197 cont = pickup_float(cont, normal+n);
198 cont = pickup_float(cont, normal+n+1);
199 cont = pickup_float(cont, normal+n+2);
200
201 if (cont == NULL)
202 {
203 cout << "Analyzing obj data failed normal\n";
204 }
205 }
206 }
207
208 void Polygon::pickup_model(char *cont)
209 {
210 cont = pickup_float(cont,c_xyz);
211 cont = pickup_float(cont,c_xyz+1);
212 cont = pickup_float(cont,c_xyz+2);
213
214 if (cont == NULL)
215 {
216 cout << "Analyzing obj data failed model\n";
217 }
218 }
219
220 void Polygon::pickup_texture(char *cont)
221 {
222 for (int n = 0; n < size*3; n += 3)
223 {
224 cont = pickup_float(cont, coord_tex+n);
225 cont = pickup_float(cont, coord_tex+n+1);
226 coord_tex[n+2] = 1.0;
227
228 if (cont == NULL)
229 {
230 cout << "Analyzing obj data failed texture\n";
231 }
232 }
233 }
234
235 char *get_pixel(int tx, int ty, SDL_Surface *texture_image)
236 {
237 return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx));
238 }
239
240 unsigned my_ntohl(unsigned u) {
241 // rr gg bb 00
242 // rr
243 // bb gg rr
244 //unsigned u1 = ((u&0xff)<<24) +
245 // ((u&0xff00)<<8) +
246 // ((u&0xff0000)>>8) +
247 // ((u&0xff000000)>>24);
248 unsigned u1;
249 unsigned b = (u&0xff000000)>>24;
250 unsigned g = (u&0xff0000)>>16;
251 unsigned r = (u&0xff00)>>8;
252 u1 = r + (g<<8) + (b<<16);
253 //printf("pixel %x->%x\n",u,u1);
254 return u1;
255 }
256
257 Uint32 Polygon::get_rgb(int tx, int ty)
258 {
259 SDL_PixelFormat *fmt;
260 //Uint32 temp, pixel;
261 Uint8 red, green, blue;
262
263 fmt = texture_image->format;
264
265 if (tx<0) tx = 0;
266 if (texture_image->w-1< tx) tx = texture_image->w-1 ;
267 if (ty<0) ty = 0;
268 if (texture_image->h-1< ty) ty = texture_image->h-1 ;
269
270
271
272 //SDL_LockSurface(texture_image);
273 char *p = get_pixel(tx,ty,texture_image);
274 #if 0
275 pixel = my_ntohl(*(Uint32*)p);
276 //printf("pixel = %d\n", pixel);
277 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel);
278 //SDL_UnlockSurface(texture_image);
279
280 temp = pixel&fmt->Rmask;
281 temp = temp>>fmt->Rshift;
282 temp = temp<<fmt->Rloss;
283 red = (Uint8)temp;
284
285 temp = pixel&fmt->Gmask;
286 temp = temp>>fmt->Gshift;
287 temp = temp<<fmt->Gloss;
288 green = (Uint8)temp;
289
290 temp = pixel&fmt->Bmask;
291 temp = temp>>fmt->Bshift;
292 temp = temp<<fmt->Bloss;
293 blue = (Uint8)temp;
294 #endif
295 blue = (Uint8) p[0];
296 green = (Uint8) p[1];
297 red = (Uint8) p[2];
298
299 //printf("tx = %d ty = %d ", tx,ty);
300 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue);
301
302 SDL_PixelFormat *pf = NULL;
303 //pf = viewer->screen->format;
304
305 //cout << SDL_MapRGB(pf, red, green, blue) << endl;
306 return SDL_MapRGB(pf, red, green, blue);
307 }