Mercurial > hg > Game > Cerium
comparison TaskManager/Test/test_render/polygon.cpp @ 349:bd0cc480a60a draft
fix hash_regist
author | admin@mcbk.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Fri, 10 Jul 2009 16:06:15 +0900 |
parents | a046c5dac580 |
children | 5b995810b86a |
comparison
equal
deleted
inserted
replaced
348:a046c5dac580 | 349:bd0cc480a60a |
---|---|
16 | 16 |
17 //extern int decode(char *cont, char *file_name); | 17 //extern int decode(char *cont, char *file_name); |
18 extern int decode(char *cont, FILE *outfile); | 18 extern int decode(char *cont, FILE *outfile); |
19 | 19 |
20 | 20 |
21 //SDL_Surface* Polygon::texture_image; | |
22 | |
23 Polygon::Polygon(void) | 21 Polygon::Polygon(void) |
24 { | 22 { |
25 xyz[0] = 0; | 23 xyz[0] = 0; |
26 xyz[1] = 0; | 24 xyz[1] = 0; |
27 xyz[2] = 0; | 25 xyz[2] = 0; |
34 angle[1] = 0; | 32 angle[1] = 0; |
35 angle[2] = 0; | 33 angle[2] = 0; |
36 angle[3] = 1; | 34 angle[3] = 1; |
37 | 35 |
38 for (int i = 0; i < 16; i++) { | 36 for (int i = 0; i < 16; i++) { |
39 matrix[i] = 0; | 37 matrix[i] = 0; |
40 } | 38 } |
41 } | 39 } |
42 | 40 |
43 void | 41 void |
44 Polygon::position_init(void) | 42 Polygon::position_init(void) |
55 angle[1] = 0; | 53 angle[1] = 0; |
56 angle[2] = 0; | 54 angle[2] = 0; |
57 angle[3] = 1; | 55 angle[3] = 1; |
58 | 56 |
59 for (int i = 0; i < 16; i++) { | 57 for (int i = 0; i < 16; i++) { |
60 matrix[i] = 0; | 58 matrix[i] = 0; |
61 } | 59 } |
62 } | 60 } |
63 | 61 |
64 #if 0 | 62 #if 0 |
65 void Polygon::draw(SceneGraphPack *sgp) | 63 void Polygon::draw(SceneGraphPack *sgp) |
67 float xyz1[4],xyz2[4],xyz3[4]; | 65 float xyz1[4],xyz2[4],xyz3[4]; |
68 | 66 |
69 /***SceneGraphUpdate***/ | 67 /***SceneGraphUpdate***/ |
70 //sgp_update(); | 68 //sgp_update(); |
71 for (int i = 0; i < sgp->info.size; i++) { | 69 for (int i = 0; i < sgp->info.size; i++) { |
72 SceneGraphNode node = sgp->node[i]; | 70 SceneGraphNode node = sgp->node[i]; |
73 | 71 |
74 /***draw***/ | 72 /***draw***/ |
75 int n,nt; | 73 int n,nt; |
76 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) { | 74 for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) { |
77 xyz1[0] = node.vertex[n]; | 75 xyz1[0] = node.vertex[n]; |
78 xyz1[1] = node.vertex[n+1]; | 76 xyz1[1] = node.vertex[n+1]; |
79 xyz1[2] = node.vertex[n+2]*-1; | 77 xyz1[2] = node.vertex[n+2]*-1; |
80 xyz1[3] = 1; | 78 xyz1[3] = 1; |
81 xyz2[0] = node.vertex[n+3]; | 79 xyz2[0] = node.vertex[n+3]; |
82 xyz2[1] = node.vertex[n+3+1]; | 80 xyz2[1] = node.vertex[n+3+1]; |
83 xyz2[2] = node.vertex[n+3+2]*-1; | 81 xyz2[2] = node.vertex[n+3+2]*-1; |
84 xyz2[3] = 1; | 82 xyz2[3] = 1; |
85 xyz3[0] = node.vertex[n+6]; | 83 xyz3[0] = node.vertex[n+6]; |
86 xyz3[1] = node.vertex[n+6+1]; | 84 xyz3[1] = node.vertex[n+6+1]; |
87 xyz3[2] = node.vertex[n+6+2]*-1; | 85 xyz3[2] = node.vertex[n+6+2]*-1; |
88 xyz3[3] = 1; | 86 xyz3[3] = 1; |
89 | 87 |
90 rotate(xyz1, node.translation); | 88 rotate(xyz1, node.translation); |
91 rotate(xyz2, node.translation); | 89 rotate(xyz2, node.translation); |
92 rotate(xyz3, node.translation); | 90 rotate(xyz3, node.translation); |
93 | 91 |
94 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]); | 92 Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]); |
95 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]); | 93 Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]); |
96 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]); | 94 Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]); |
97 | 95 |
98 Triangle *tri = new Triangle(ver1,ver2,ver3); | 96 Triangle *tri = new Triangle(ver1,ver2,ver3); |
99 Span_c *span = new Span_c(); | 97 Span_c *span = new Span_c(); |
100 span->viewer = viewer; | 98 span->viewer = viewer; |
101 span->p = this; | 99 span->p = this; |
102 span->create_span(tri,texture_image); | 100 span->create_span(tri,texture_image); |
103 delete ver1; | 101 delete ver1; |
104 delete ver2; | 102 delete ver2; |
105 delete ver3; | 103 delete ver3; |
106 delete tri; | 104 delete tri; |
107 delete span; | 105 delete span; |
108 } | 106 } |
109 } | 107 } |
110 } | 108 } |
111 | 109 |
112 | 110 |
113 void Polygon::draw(PolygonPack *pp) | 111 void Polygon::draw(PolygonPack *pp) |
114 { | 112 { |
115 for(int n=0; n<pp->info.size; n++) | 113 for(int n=0; n<pp->info.size; n++) |
116 { | 114 { |
117 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); | 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); |
118 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); | 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); |
119 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); | 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); |
120 | 118 |
121 Triangle *tri = new Triangle(ver1,ver2,ver3); | 119 Triangle *tri = new Triangle(ver1,ver2,ver3); |
122 Span_c *span = new Span_c(); | 120 Span_c *span = new Span_c(); |
123 span->viewer = viewer; | 121 span->viewer = viewer; |
124 span->p = this; | 122 span->p = this; |
125 span->create_span(tri,texture_image); | 123 span->create_span(tri,texture_image); |
126 delete ver1; | 124 delete ver1; |
127 delete ver2; | 125 delete ver2; |
128 delete ver3; | 126 delete ver3; |
129 delete tri; | 127 delete tri; |
130 delete span; | 128 delete span; |
131 } | 129 } |
132 } | 130 } |
133 | 131 |
134 void Polygon::draw(SpanPack *sp) | 132 void Polygon::draw(SpanPack *sp) |
135 { | 133 { |
136 Span *span; | 134 Span *span; |
137 | 135 |
138 for (int n = 0; n < sp->info.size; n++) { | 136 for (int n = 0; n < sp->info.size; n++) { |
139 span = &sp->span[n]; | 137 span = &sp->span[n]; |
140 | 138 |
141 //int x = span->x; | 139 //int x = span->x; |
142 //int y = span->y; | 140 //int y = span->y; |
143 float z = span->start_z; | 141 float z = span->start_z; |
144 int end = span->length_x; | 142 int end = span->length_x; |
145 float zpos = span->end_z; | 143 float zpos = span->end_z; |
146 float tex1 = span->tex_x1; | 144 float tex1 = span->tex_x1; |
147 float tex2 = span->tex_x2; | 145 float tex2 = span->tex_x2; |
148 float tey1 = span->tex_y1; | 146 float tey1 = span->tex_y1; |
149 float tey2 = span->tex_y2; | 147 float tey2 = span->tex_y2; |
150 Uint32 rgb; | 148 Uint32 rgb; |
151 int tex_xpos; | 149 int tex_xpos; |
152 int tex_ypos; | 150 int tex_ypos; |
153 int tex_zpos; | 151 int tex_zpos; |
154 float tex_x, tex_y, tex_z; | 152 float tex_x, tex_y, tex_z; |
155 | 153 |
156 if (end == 1) { | 154 if (end == 1) { |
157 tex_xpos = (int)((span->tex_height-1) * tex1); | 155 tex_xpos = (int)((span->tex_height-1) * tex1); |
158 tex_ypos = (int)((span->tex_width-1) * tey1); | 156 tex_ypos = (int)((span->tex_width-1) * tey1); |
159 tex_zpos = (int)z; | 157 tex_zpos = (int)z; |
160 rgb = get_rgb(tex_xpos, tex_ypos); | 158 rgb = get_rgb(tex_xpos, tex_ypos); |
161 //viewer->write_pixel(x, y, zpos, rgb); | 159 //viewer->write_pixel(x, y, zpos, rgb); |
162 } else { | 160 } else { |
163 for (int j = 0; j < end; j++) { | 161 for (int j = 0; j < end; j++) { |
164 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); | 162 tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); |
165 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); | 163 tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); |
166 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); | 164 tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); |
167 if (tex_x > 1) tex_x = 1; | 165 if (tex_x > 1) tex_x = 1; |
168 if (tex_y > 1) tex_y = 1; | 166 if (tex_y > 1) tex_y = 1; |
169 tex_xpos = (int)((span->tex_height-1) * tex_x); | 167 tex_xpos = (int)((span->tex_height-1) * tex_x); |
170 tex_ypos = (int)((span->tex_width-1) * tex_y); | 168 tex_ypos = (int)((span->tex_width-1) * tex_y); |
171 rgb = get_rgb(tex_xpos,tex_ypos); | 169 rgb = get_rgb(tex_xpos,tex_ypos); |
172 //viewer->write_pixel(j + x, y, tex_z, rgb); | 170 //viewer->write_pixel(j + x, y, tex_z, rgb); |
173 } | 171 } |
174 } | 172 } |
175 } | 173 } |
176 } | 174 } |
177 | 175 |
178 #endif | 176 #endif |
179 | 177 |
180 void Polygon::pickup_coordinate(char *cont) | 178 void Polygon::pickup_coordinate(char *cont) |
181 { | 179 { |
182 for(int n=0; n<size*3; n+=3) | 180 for(int n=0; n<size*3; n+=3) |
183 { | 181 { |
184 cont = pickup_float(cont, coord_xyz+n); | 182 cont = pickup_float(cont, coord_xyz+n); |
185 cont = pickup_float(cont, coord_xyz+n+1); | 183 cont = pickup_float(cont, coord_xyz+n+1); |
186 cont = pickup_float(cont, coord_xyz+n+2); | 184 cont = pickup_float(cont, coord_xyz+n+2); |
187 | 185 |
188 if (cont == NULL) | 186 if (cont == NULL) |
189 { | 187 { |
190 cout << "Analyzing obj data failed coordinate\n"; | 188 cout << "Analyzing obj data failed coordinate\n"; |
191 } | 189 } |
192 } | 190 } |
193 } | 191 } |
194 | 192 |
195 void Polygon::pickup_normal(char *cont) | 193 void Polygon::pickup_normal(char *cont) |
196 { | 194 { |
197 for (int n = 0; n<size*3; n += 3) | 195 for (int n = 0; n<size*3; n += 3) |
198 { | 196 { |
199 cont = pickup_float(cont, normal+n); | 197 cont = pickup_float(cont, normal+n); |
200 cont = pickup_float(cont, normal+n+1); | 198 cont = pickup_float(cont, normal+n+1); |
201 cont = pickup_float(cont, normal+n+2); | 199 cont = pickup_float(cont, normal+n+2); |
202 | 200 |
203 if (cont == NULL) | 201 if (cont == NULL) |
204 { | 202 { |
205 cout << "Analyzing obj data failed normal\n"; | 203 cout << "Analyzing obj data failed normal\n"; |
206 } | 204 } |
207 } | 205 } |
208 } | 206 } |
209 | 207 |
210 void Polygon::pickup_model(char *cont) | 208 void Polygon::pickup_model(char *cont) |
211 { | 209 { |
212 cont = pickup_float(cont,c_xyz); | 210 cont = pickup_float(cont,c_xyz); |
213 cont = pickup_float(cont,c_xyz+1); | 211 cont = pickup_float(cont,c_xyz+1); |
214 cont = pickup_float(cont,c_xyz+2); | 212 cont = pickup_float(cont,c_xyz+2); |
215 | 213 |
216 if (cont == NULL) | 214 if (cont == NULL) |
217 { | 215 { |
218 cout << "Analyzing obj data failed model\n"; | 216 cout << "Analyzing obj data failed model\n"; |
219 } | 217 } |
220 } | 218 } |
221 | 219 |
222 void Polygon::pickup_texture(char *cont) | 220 void Polygon::pickup_texture(char *cont) |
223 { | 221 { |
224 for (int n = 0; n < size*3; n += 3) | 222 for (int n = 0; n < size*3; n += 3) |
225 { | 223 { |
226 cont = pickup_float(cont, coord_tex+n); | 224 cont = pickup_float(cont, coord_tex+n); |
227 cont = pickup_float(cont, coord_tex+n+1); | 225 cont = pickup_float(cont, coord_tex+n+1); |
228 coord_tex[n+2] = 1.0; | 226 coord_tex[n+2] = 1.0; |
229 | 227 |
230 if (cont == NULL) | 228 if (cont == NULL) |
231 { | 229 { |
232 cout << "Analyzing obj data failed texture\n"; | 230 cout << "Analyzing obj data failed texture\n"; |
233 } | 231 } |
234 } | 232 } |
235 } | 233 } |
236 | 234 |
237 char *get_pixel(int tx, int ty, SDL_Surface *texture_image) | 235 char *get_pixel(int tx, int ty, SDL_Surface *texture_image) |
238 { | 236 { |
259 Uint32 Polygon::get_rgb(int tx, int ty) | 257 Uint32 Polygon::get_rgb(int tx, int ty) |
260 { | 258 { |
261 SDL_PixelFormat *fmt; | 259 SDL_PixelFormat *fmt; |
262 //Uint32 temp, pixel; | 260 //Uint32 temp, pixel; |
263 Uint8 red, green, blue; | 261 Uint8 red, green, blue; |
264 | 262 |
265 fmt = texture_image->format; | 263 fmt = texture_image->format; |
266 | 264 |
267 if (tx<0) tx = 0; | 265 if (tx<0) tx = 0; |
268 if (texture_image->w-1< tx) tx = texture_image->w-1 ; | 266 if (texture_image->w-1< tx) tx = texture_image->w-1 ; |
269 if (ty<0) ty = 0; | 267 if (ty<0) ty = 0; |
276 #if 0 | 274 #if 0 |
277 pixel = my_ntohl(*(Uint32*)p); | 275 pixel = my_ntohl(*(Uint32*)p); |
278 //printf("pixel = %d\n", pixel); | 276 //printf("pixel = %d\n", pixel); |
279 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); | 277 //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); |
280 //SDL_UnlockSurface(texture_image); | 278 //SDL_UnlockSurface(texture_image); |
281 | 279 |
282 temp = pixel&fmt->Rmask; | 280 temp = pixel&fmt->Rmask; |
283 temp = temp>>fmt->Rshift; | 281 temp = temp>>fmt->Rshift; |
284 temp = temp<<fmt->Rloss; | 282 temp = temp<<fmt->Rloss; |
285 red = (Uint8)temp; | 283 red = (Uint8)temp; |
286 | 284 |
287 temp = pixel&fmt->Gmask; | 285 temp = pixel&fmt->Gmask; |
288 temp = temp>>fmt->Gshift; | 286 temp = temp>>fmt->Gshift; |
289 temp = temp<<fmt->Gloss; | 287 temp = temp<<fmt->Gloss; |
290 green = (Uint8)temp; | 288 green = (Uint8)temp; |
291 | 289 |
292 temp = pixel&fmt->Bmask; | 290 temp = pixel&fmt->Bmask; |
293 temp = temp>>fmt->Bshift; | 291 temp = temp>>fmt->Bshift; |
294 temp = temp<<fmt->Bloss; | 292 temp = temp<<fmt->Bloss; |
295 blue = (Uint8)temp; | 293 blue = (Uint8)temp; |
296 #endif | 294 #endif |
297 blue = (Uint8) p[0]; | 295 blue = (Uint8) p[0]; |
298 green = (Uint8) p[1]; | 296 green = (Uint8) p[1]; |
299 red = (Uint8) p[2]; | 297 red = (Uint8) p[2]; |
300 | 298 |
301 //printf("tx = %d ty = %d ", tx,ty); | 299 //printf("tx = %d ty = %d ", tx,ty); |
302 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue); | 300 //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue); |
303 | 301 |
304 SDL_PixelFormat *pf = NULL; | 302 SDL_PixelFormat *pf = NULL; |
305 //pf = viewer->screen->format; | 303 //pf = viewer->screen->format; |
306 | 304 |