Mercurial > hg > Members > kono > Cerium
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Renderer/Engine/polygon.cc Mon Oct 12 09:39:35 2009 +0900 @@ -0,0 +1,307 @@ +#include <iostream> +#include <SDL.h> +#include <SDL_opengl.h> +#include <SDL_image.h> +#include "polygon.h" +#include "xml.h" +#include "sys.h" +#include "triangle.h" +#include "vertex.h" +#include "Span.h" +#include "SpanC.h" +#include "scene_graph_pack.h" +#include "error.h" +#include "viewer_types.h" +using namespace std; + +//extern int decode(char *cont, char *file_name); +extern int decode(char *cont, FILE *outfile); + + +Polygon::Polygon(void) +{ + xyz[0] = 0; + xyz[1] = 0; + xyz[2] = 0; + xyz[3] = 1; + c_xyz[0] = 0; + c_xyz[1] = 0; + c_xyz[2] = 0; + c_xyz[3] = 1; + angle[0] = 0; + angle[1] = 0; + angle[2] = 0; + angle[3] = 1; + + for (int i = 0; i < 16; i++) { + matrix[i] = 0; + } +} + +void +Polygon::position_init(void) +{ + xyz[0] = 0; + xyz[1] = 0; + xyz[2] = 0; + xyz[3] = 1; + c_xyz[0] = 0; + c_xyz[1] = 0; + c_xyz[2] = 0; + c_xyz[3] = 1; + angle[0] = 0; + angle[1] = 0; + angle[2] = 0; + angle[3] = 1; + + for (int i = 0; i < 16; i++) { + matrix[i] = 0; + } +} + +#if 0 +void Polygon::draw(SceneGraphPack *sgp) +{ + float xyz1[4],xyz2[4],xyz3[4]; + + /***SceneGraphUpdate***/ + //sgp_update(); + for (int i = 0; i < sgp->info.size; i++) { + SceneGraphNode node = sgp->node[i]; + + /***draw***/ + int n,nt; + for(n=0,nt=0; n<node.size*3; n+=9,nt+=6) { + xyz1[0] = node.vertex[n]; + xyz1[1] = node.vertex[n+1]; + xyz1[2] = node.vertex[n+2]*-1; + xyz1[3] = 1; + xyz2[0] = node.vertex[n+3]; + xyz2[1] = node.vertex[n+3+1]; + xyz2[2] = node.vertex[n+3+2]*-1; + xyz2[3] = 1; + xyz3[0] = node.vertex[n+6]; + xyz3[1] = node.vertex[n+6+1]; + xyz3[2] = node.vertex[n+6+2]*-1; + xyz3[3] = 1; + + rotate(xyz1, node.translation); + rotate(xyz2, node.translation); + rotate(xyz3, node.translation); + + Vertex *ver1 = new Vertex(xyz1[0],xyz1[1],xyz1[2],node.texture[nt],node.texture[nt+1]); + Vertex *ver2 = new Vertex(xyz2[0],xyz2[1],xyz2[2],node.texture[nt+2],node.texture[nt+2+1]); + Vertex *ver3 = new Vertex(xyz3[0],xyz3[1],xyz3[2],node.texture[nt+4],node.texture[nt+4+1]); + + Triangle *tri = new Triangle(ver1,ver2,ver3); + Span_c *span = new Span_c(); + span->viewer = viewer; + span->p = this; + span->create_span(tri,texture_image); + delete ver1; + delete ver2; + delete ver3; + delete tri; + delete span; + } + } +} + + +void Polygon::draw(PolygonPack *pp) +{ + for(int n=0; n<pp->info.size; n++) + { + 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); + 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); + 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); + + Triangle *tri = new Triangle(ver1,ver2,ver3); + Span_c *span = new Span_c(); + span->viewer = viewer; + span->p = this; + span->create_span(tri,texture_image); + delete ver1; + delete ver2; + delete ver3; + delete tri; + delete span; + } +} + +void Polygon::draw(SpanPack *sp) +{ + Span *span; + + for (int n = 0; n < sp->info.size; n++) { + span = &sp->span[n]; + + //int x = span->x; + //int y = span->y; + float z = span->start_z; + int end = span->length_x; + float zpos = span->end_z; + float tex1 = span->tex_x1; + float tex2 = span->tex_x2; + float tey1 = span->tex_y1; + float tey2 = span->tex_y2; + Uint32 rgb; + int tex_xpos; + int tex_ypos; + int tex_zpos; + float tex_x, tex_y, tex_z; + + if (end == 1) { + tex_xpos = (int)((span->tex_height-1) * tex1); + tex_ypos = (int)((span->tex_width-1) * tey1); + tex_zpos = (int)z; + rgb = get_rgb(tex_xpos, tex_ypos); + //viewer->write_pixel(x, y, zpos, rgb); + } else { + for (int j = 0; j < end; j++) { + tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1); + tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1); + tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1); + if (tex_x > 1) tex_x = 1; + if (tex_y > 1) tex_y = 1; + tex_xpos = (int)((span->tex_height-1) * tex_x); + tex_ypos = (int)((span->tex_width-1) * tex_y); + rgb = get_rgb(tex_xpos,tex_ypos); + //viewer->write_pixel(j + x, y, tex_z, rgb); + } + } + } +} + +#endif + +void Polygon::pickup_coordinate(char *cont) +{ + for(int n=0; n<size*3; n+=3) + { + cont = pickup_float(cont, coord_xyz+n); + cont = pickup_float(cont, coord_xyz+n+1); + cont = pickup_float(cont, coord_xyz+n+2); + + if (cont == NULL) + { + cout << "Analyzing obj data failed coordinate\n"; + } + } +} + +void Polygon::pickup_normal(char *cont) +{ + for (int n = 0; n<size*3; n += 3) + { + cont = pickup_float(cont, normal+n); + cont = pickup_float(cont, normal+n+1); + cont = pickup_float(cont, normal+n+2); + + if (cont == NULL) + { + cout << "Analyzing obj data failed normal\n"; + } + } +} + +void Polygon::pickup_model(char *cont) +{ + cont = pickup_float(cont,c_xyz); + cont = pickup_float(cont,c_xyz+1); + cont = pickup_float(cont,c_xyz+2); + + if (cont == NULL) + { + cout << "Analyzing obj data failed model\n"; + } +} + +void Polygon::pickup_texture(char *cont) +{ + for (int n = 0; n < size*3; n += 3) + { + cont = pickup_float(cont, coord_tex+n); + cont = pickup_float(cont, coord_tex+n+1); + coord_tex[n+2] = 1.0; + + if (cont == NULL) + { + cout << "Analyzing obj data failed texture\n"; + } + } +} + +char *get_pixel(int tx, int ty, SDL_Surface *texture_image) +{ + return (char*)texture_image->pixels+(texture_image->format->BytesPerPixel*((texture_image->w)*ty+tx)); +} + +unsigned my_ntohl(unsigned u) { + // rr gg bb 00 + // rr + // bb gg rr + //unsigned u1 = ((u&0xff)<<24) + + // ((u&0xff00)<<8) + + // ((u&0xff0000)>>8) + + // ((u&0xff000000)>>24); + unsigned u1; + unsigned b = (u&0xff000000)>>24; + unsigned g = (u&0xff0000)>>16; + unsigned r = (u&0xff00)>>8; + u1 = r + (g<<8) + (b<<16); + //printf("pixel %x->%x\n",u,u1); + return u1; +} + +Uint32 Polygon::get_rgb(int tx, int ty) +{ + SDL_PixelFormat *fmt; + //Uint32 temp, pixel; + Uint8 red, green, blue; + + fmt = texture_image->format; + + if (tx<0) tx = 0; + if (texture_image->w-1< tx) tx = texture_image->w-1 ; + if (ty<0) ty = 0; + if (texture_image->h-1< ty) ty = texture_image->h-1 ; + + + + //SDL_LockSurface(texture_image); + char *p = get_pixel(tx,ty,texture_image); +#if 0 + pixel = my_ntohl(*(Uint32*)p); + //printf("pixel = %d\n", pixel); + //printf("pixel %x bpp = %d ",p, fmt->BytesPerPixel); + //SDL_UnlockSurface(texture_image); + + temp = pixel&fmt->Rmask; + temp = temp>>fmt->Rshift; + temp = temp<<fmt->Rloss; + red = (Uint8)temp; + + temp = pixel&fmt->Gmask; + temp = temp>>fmt->Gshift; + temp = temp<<fmt->Gloss; + green = (Uint8)temp; + + temp = pixel&fmt->Bmask; + temp = temp>>fmt->Bshift; + temp = temp<<fmt->Bloss; + blue = (Uint8)temp; +#endif + blue = (Uint8) p[0]; + green = (Uint8) p[1]; + red = (Uint8) p[2]; + + //printf("tx = %d ty = %d ", tx,ty); + //printf("pixel color => R: %d, G: %d, B: %d\n", red, green, blue); + + SDL_PixelFormat *pf = NULL; + //pf = viewer->screen->format; + + //cout << SDL_MapRGB(pf, red, green, blue) << endl; + return SDL_MapRGB(pf, red, green, blue); +}