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