#include "chunk.hpp" #include #include #include "chnktype.hpp" #define UseLocalAssert No #include "ourasert.h" #define assert(x) GLOBALASSERT(x) // misc data structures functions BOOL operator==(const obinfile &o1, const obinfile &o2) { return (&o1 == &o2); } BOOL operator!=(const obinfile &o1, const obinfile &o2) { return (&o1 != &o2); } BOOL operator==(const shpinfile &s1, const shpinfile &s2) { return (&s1 == &s2); } BOOL operator!=(const shpinfile &s1, const shpinfile &s2) { return (&s1 != &s2); } BOOL operator== (const ChunkUV_List &c1, const ChunkUV_List &c2) { return(&c1 == &c2); } BOOL operator!= (const ChunkUV_List &c1, const ChunkUV_List &c2) { return(&c1 != &c2); } BOOL operator== (const ObjectID &o1, const ObjectID &o2) { return((o1.id1==o2.id1) && (o1.id2==o2.id2)); } BOOL operator!= (const ObjectID &o1, const ObjectID &o2) { return((o1.id1!=o2.id1) || (o1.id2!=o2.id2)); } ObjectID Minimum(const ObjectID &o1,const ObjectID &o2) { if(o1.id1o2.id1) return o2; if(o1.id2> 16); return uv_index; } else { //uvindex is just in the top 16 bits return (colour >> 16); } } unsigned int ChunkPoly::GetTextureIndex() { return (colour & CHUNK_TEXTUREINDEX_MASK); } void ChunkPoly::SetUVIndex(unsigned int uv_index) { assert(uv_index<=CHUNK_MAX_UVINDEX); //clear the old uvindex colour &=~CHUNK_UVINDEX_MASK; if(uv_index<65536) { //fit uv index into the top 16 bits if it will fit , to maintain compatibility with //old chunk loaders colour |= (uv_index<<16); } else { //put the bottom 16 bits of the uv_index in the top 16 bits of the colour colour |= (uv_index & 0xffff) << 16; //put the next 4 bits of the uv_index in the lower middle 4 bits of the colour uv_index>>=16; colour |= (uv_index <<12); } } void ChunkPoly::SetTextureIndex(unsigned int texture_index) { assert(texture_index<=CHUNK_MAX_TEXTUREINDEX); colour &=~ CHUNK_TEXTUREINDEX_MASK; colour |= texture_index; } ////////////////////////////////////////////// ChunkVector operator+(const ChunkVector& a, const ChunkVector& b) { ChunkVector v; v.x=a.x+b.x; v.y=a.y+b.y; v.z=a.z+b.z; return v; } ChunkVector operator-(const ChunkVector& a, const ChunkVector& b) { ChunkVector v; v.x=a.x-b.x; v.y=a.y-b.y; v.z=a.z-b.z; return v; } ChunkVector& ChunkVector::operator+=(const ChunkVector& a) { x += a.x; y += a.y; z += a.z; return *this; } ChunkVector& ChunkVector::operator-=(const ChunkVector& a) { x -= a.x; y -= a.y; z -= a.z; return *this; } ChunkVector::operator VECTORCH () const { VECTORCH v; v.vx = (int)x; v.vy = (int)y; v.vz = (int)z; return(v); } ChunkVector::operator ChunkVectorInt () const { ChunkVectorInt v; v.x = (int)x; v.y = (int)y; v.z = (int)z; return(v); } ChunkVector::operator ChunkVectorFloat () const { ChunkVectorFloat v; v.x = (float)x; v.y = (float)y; v.z = (float)z; return(v); } ChunkVector operator*(const ChunkVector & a, const double s) { ChunkVector v; v.x = a.x * s; v.y = a.y * s; v.z = a.z * s; return(v); } ChunkVector operator/(const ChunkVector & a, const double s) { ChunkVector v; v.x = a.x / s; v.y = a.y / s; v.z = a.z / s; return(v); } ChunkVector operator*(const ChunkVector& a, const ChunkVector& b) { ChunkVector v; v.x=a.y*b.z - a.z*b.y; v.y=a.z*b.x - a.x*b.z; v.z=a.x*b.y - a.y*b.x; return v; } double dot(const ChunkVector& a, const ChunkVector& b) { return(a.x*b.x + a.y*b.y + a.z*b.z); } double mod(const ChunkVector& a) { return(sqrt(dot(a,a))); } int ChunkVector::norm() { double modulos = mod(*this); if(modulos == 0)return(0); x /=modulos; y /= modulos; z /= modulos; return(1); } ////////////////////////////////////////////// ChunkVectorInt operator+(const ChunkVectorInt& a, const ChunkVectorInt& b) { ChunkVectorInt v; v.x=a.x+b.x; v.y=a.y+b.y; v.z=a.z+b.z; return v; } ChunkVectorInt operator-(const ChunkVectorInt& a, const ChunkVectorInt& b) { ChunkVectorInt v; v.x=a.x-b.x; v.y=a.y-b.y; v.z=a.z-b.z; return v; } ChunkVectorInt& ChunkVectorInt::operator+=(const ChunkVectorInt& a) { x += a.x; y += a.y; z += a.z; return *this; } ChunkVectorInt& ChunkVectorInt::operator-=(const ChunkVectorInt& a) { x -= a.x; y -= a.y; z -= a.z; return *this; } ChunkVectorInt::operator VECTORCH () const { VECTORCH v; v.vx = x; v.vy = y; v.vz = z; return(v); } ChunkVectorInt operator*(const ChunkVectorInt & a, const double s) { ChunkVectorInt v; v.x =(int) (a.x * s); v.y =(int) (a.y * s); v.z =(int) (a.z * s); return(v); } ChunkVectorInt operator/(const ChunkVectorInt & a, const double s) { ChunkVectorInt v; v.x =(int) (a.x / s); v.y =(int) (a.y / s); v.z =(int) (a.z / s); return(v); } double mod(const ChunkVectorInt& a) { return(sqrt((double)a.x*(double)a.x+(double)a.y*(double)a.y+(double)a.z*(double)a.z)); } int ChunkVectorInt::norm() { double modulos = mod(*this) /65536.0; if(modulos == 0)return(0); x =(int) (x/modulos); y =(int) (y/modulos); z =(int) (z/modulos); return(1); } //////////////////////////////////////////////////////// ChunkVectorFloat operator+(const ChunkVectorFloat& a, const ChunkVectorFloat& b) { ChunkVectorFloat v; v.x=a.x+b.x; v.y=a.y+b.y; v.z=a.z+b.z; return v; } ChunkVectorFloat operator-(const ChunkVectorFloat& a, const ChunkVectorFloat& b) { ChunkVectorFloat v; v.x=a.x-b.x; v.y=a.y-b.y; v.z=a.z-b.z; return v; } ChunkVectorFloat& ChunkVectorFloat::operator+=(const ChunkVectorFloat& a) { x += a.x; y += a.y; z += a.z; return *this; } ChunkVectorFloat& ChunkVectorFloat::operator-=(const ChunkVectorFloat& a) { x -= a.x; y -= a.y; z -= a.z; return *this; } ChunkVectorFloat operator*(const ChunkVectorFloat & a, const double s) { ChunkVectorFloat v; v.x =(float) (a.x * s); v.y =(float) (a.y * s); v.z =(float) (a.z * s); return(v); } ChunkVectorFloat operator/(const ChunkVectorFloat & a, const double s) { ChunkVectorFloat v; v.x =(float) (a.x / s); v.y =(float) (a.y / s); v.z =(float) (a.z / s); return(v); } ChunkVectorFloat::operator VECTORCH () const { VECTORCH v; v.vx =(int) (x*65536); v.vy =(int) (y*65536); v.vz =(int) (z*65536); return(v); } int ChunkVectorFloat::norm() { float modulos =(float) mod(*this); if(modulos == 0)return(0); x /= modulos; y /= modulos; z /= modulos; return(1); } double mod(const ChunkVectorFloat& a) { return(sqrt((double)a.x*(double)a.x+(double)a.y*(double)a.y+(double)a.z*(double)a.z)); } //////////////////////////////////////////////////////// ChunkShape::~ChunkShape() { if (v_list) delete [] v_list; if (v_normal_list) delete [] v_normal_list; if (p_normal_list) delete [] p_normal_list; if (poly_list) delete [] poly_list; if (uv_list) delete [] uv_list; if (texture_fns) for (int i = 0; inum_polys; num_verts=startframe->num_verts; flags=startframe->flags|animframeflag_interpolated_frame; num_interp_frames=0; pad3=0; pad4=0; v_list=new ChunkVector[num_verts]; p_normal_list=new ChunkVector[num_polys]; double start_mult=startwt/(double)(startwt+endwt); double end_mult=endwt/(double)(startwt+endwt); for(int i=0;iv_list[i].x*start_mult+endframe->v_list[i].x*end_mult; v_list[i].y=startframe->v_list[i].y*start_mult+endframe->v_list[i].y*end_mult; v_list[i].z=startframe->v_list[i].z*start_mult+endframe->v_list[i].z*end_mult; } for(i=0;iv_list[cs->poly_list[i].vert_ind[1]]-cs->v_list[cs->poly_list[i].vert_ind[0]]; ChunkVector v2=cs->v_list[cs->poly_list[i].vert_ind[2]]-cs->v_list[cs->poly_list[i].vert_ind[0]]; ChunkVector norm; norm.x=v1.y*v2.z-v1.z*v2.y; norm.y=v1.z*v2.x-v1.x*v2.z; norm.z=v1.x*v2.y-v1.y*v2.x; double length=sqrt(norm.x*norm.x+norm.y*norm.y+norm.z*norm.z); cs->p_normal_list[i]=norm*(1/length); } } */ ChunkAnimFrame& ChunkAnimFrame::operator=(const ChunkAnimFrame &frm) { if(name) delete [] name; if(v_list) delete [] v_list; if(p_normal_list) delete [] p_normal_list; if(frm.name) { name=new char[strlen(frm.name)+1]; strcpy(name,frm.name); } else name=0; num_polys=frm.num_polys; num_verts=frm.num_verts; if(num_polys) { p_normal_list=new ChunkVectorFloat[num_polys]; for(int i=0;i* poly_not_in_bb) { int i; if(!cs) return; num_verts=cs->num_verts; if(!v_normal_list)v_normal_list=new ChunkVectorFloat[cs->num_verts]; for(i=0;inum_polys;i++) { const ChunkPoly* cp=&cs->poly_list[i]; for(int j=0;jnum_verts;j++) { int vi=cp->vert_ind[j]; for(int k=0;kp_normal_list[i]; } } } for(i=0;inum_verts]; for(i=0;inum_verts;i++) { vert_in_bb[i]=0; } for(i=0;inum_polys;i++) { if(poly_not_in_bb->contains(i))continue; const ChunkPoly* cp=&cs->poly_list[i]; for(int j=0;jnum_verts;j++) { vert_in_bb[cp->vert_ind[j]]=1; } } } for(i=0;inum_verts; j++) { if(vert_in_bb && !vert_in_bb[j]) continue; max.x = std::max(max.x, caf->v_list[j].x); max.y = std::max(max.y, caf->v_list[j].y); max.z = std::max(max.z, caf->v_list[j].z); min.x = std::min(min.x, caf->v_list[j].x); min.y = std::min(min.y, caf->v_list[j].y); min.z = std::min(min.z, caf->v_list[j].z); double temp_rad = mod(caf->v_list[j]); radius = std::max(radius, (float)temp_rad); } } if(vert_in_bb) delete [] vert_in_bb; } void ChunkAnimSequence::DeleteInterpolatedFrames() { int i; int NewNumFrames=NumFrames; for(i=0;iflags & animframeflag_interpolated_frame)NewNumFrames--; } if(NewNumFrames==NumFrames)return; int framepos=0; for(i=0;iflags & animframeflag_interpolated_frame) continue; Frames[framepos++]=Frames[i]; } NumFrames=NewNumFrames; Frames=(ChunkAnimFrame**)realloc(Frames,sizeof(ChunkAnimFrame*)*NumFrames); } void ChunkAnimSequence::GenerateInterpolatedFrames(ChunkShape const *cs) { DeleteInterpolatedFrames(); /* int NewNumFrames=NumFrames; for(int i=0;inum_interp_frames; } if(NewNumFrames==NumFrames) return; ChunkAnimFrame** NewFrames=new ChunkAnimFrame*[NewNumFrames]; int framepos=0; for( i=0;inum_interp_frames==0)continue; ChunkAnimFrame* startframe=Frames[i]; ChunkAnimFrame* endframe=Frames[(i+1)%NumFrames]; for(int j=0;jnum_interp_frames;j++) { NewFrames[framepos++]=new ChunkAnimFrame(startframe,endframe,startframe->num_interp_frames-j,j+1,cs); } } delete [] Frames; Frames=NewFrames; NumFrames=NewNumFrames; */ }