home *** CD-ROM | disk | FTP | other *** search
- /* Macdelete
- * A collection of routines for the removal of objects from memory
- */
-
- #include "texture.h"
- #include "geom.h"
- #include "surface.h"
- #include "light.h"
-
- #include "list.h"
- #include "grid.h"
- #include "sphere.h"
- #include "box.h"
- #include "blob.h"
- #include "disc.h"
- #include "cone.h"
- #include "plane.h"
- #include "cylinder.h"
- #include "sampling.h"
- #include "csg.h"
- #include "instance.h"
- #include "triangle.h"
- #include "hf.h"
- #include "poly.h"
-
- #include "image.h"
- #include "blotch.h"
- #include "bump.h"
- #include "checker.h"
- #include "cloud.h"
- #include "fbm.h"
- #include "fbmbump.h"
- #include "gloss.h"
- #include "imagetext.h"
- #include "marble.h"
- #include "mount.h"
- #include "sky.h"
- #include "stripe.h"
- #include "windy.h"
- #include "wood.h"
-
- #include "maceditor.h"
-
- char UnlinkObject(Geom *obj, Geom *parent)
- {
- Geom *p;
- Grid *grid;
- List *list;
- char found;
-
- if(!obj || !parent) return;
-
- found = FALSE;
- switch(ObjectType(parent)) {
- case LIST:
- list = parent->obj;
- if(list->list) {
- if(list->list==obj) {
- list->list = obj->next;
- parent->prims--;
- return TRUE;
- }
- p = list->list;
- while((p->next != obj) && p) p = p->next;
- if(p) {
- p->next = obj->next ;
- parent->prims--;
- return TRUE;
- }
- }
- if(list->unbounded) {
- if(list->unbounded==obj) {
- list->unbounded = obj->next;
- parent->prims--;
- return TRUE;
- }
- p = list->unbounded;
- while((p->next != obj) && p) p = p->next;
- if(p) {
- p->next = obj->next;
- parent->prims--;
- return TRUE;
- }
- }
- break;
- case GRID:
- grid = parent->obj;
- if(grid->objects) {
- if(grid->objects==obj) {
- grid->objects = obj->next;
- parent->prims--;
- found = TRUE;
- }
- p = grid->objects;
- while((p->next != obj) && p) p = p->next;
- if(p) {
- p->next = obj->next ;
- parent->prims--;
- found = TRUE;
- }
- }
- if(grid->unbounded) {
- if(grid->unbounded==obj) {
- grid->unbounded = obj->next;
- parent->prims--;
- found = TRUE;
- }
- p = grid->unbounded;
- while((p->next != obj) && p) p = p->next;
- if(p) {
- p->next = obj->next;
- parent->prims--;
- found = TRUE;
- }
- }
- /* If we found obj anywhere, we must remove it from the voxel array lists */
- if(found==TRUE) {
- short x,y,z;
- GeomList *cell, *cell2;
-
- for(x = 0 ; x < grid->xsize; x++) {
- for(y = 0 ; y < grid->ysize; y++) {
- for(z = 0 ; z < grid->zsize; z++) {
- cell = grid->cells[x][y][z];
- if(cell) {
- if(cell->obj == obj) {
- grid->cells[x][y][z] = cell->next;
- Free(cell);
- }
- else {
- cell2 = cell->next;
- while(cell2 && (cell2->obj != obj)) {
- cell = cell2;
- cell2 = cell->next;
- }
- if(cell2) {
- cell->next = cell2->next;
- Free(cell2);
- }
- }
- }
- }
- }
- }
- }
- return found;
- break;
- case CSG:
- RLerror(RL_WARN,"Sorry, Rayshade-M cannot delete single objects in a CSG",0,0,0);
- break;
- }
- return FALSE;
- }
-
- void DeleteSurface(Surface *surf)
- {
- if(surf == NULL)
- return ;
- if(surf->name)
- Free(surf->name) ;
- Free(surf) ;
- }
-
- void DeleteTrans(Trans *trans)
- {
- if(trans == NULL)
- return ;
- Free(trans->tr);
- Free(trans);
- }
-
- void DeleteImage(Image *image)
- {
- if(image == NULL)
- return ;
-
- if(image->data)
- Free(image->data) ;
- if(image->filename)
- Free(image->filename) ;
- }
-
- void DeleteColorMap(Color *map)
- {
- if(map == NULL)
- return ;
- Free(map) ;
- }
-
- void DeleteMapping(Mapping *map)
- {
- if(map == NULL)
- return ;
- Free(map) ;
- }
-
- void DeleteTexture(Texture *text)
- {
- if(text == NULL)
- return ;
-
- switch(TextureType(text)) {
- case T_BLOTCH:
- DeleteSurface(((Blotch *) text->data)->surf) ;
- break ;
- case T_BUMP:
- break ;
- case T_CHECKER:
- DeleteSurface(((Checker *) text->data)->surf) ;
- break ;
- case T_CLOUD:
- break ;
- case T_FBM:
- DeleteColorMap(((FBm *) text->data)->colormap) ;
- break ;
- case T_FBMBUMP:
- break ;
- case T_GLOSS:
- break ;
- case T_IMAGE:
- DeleteImage(((ImageText *) text->data)->image) ;
- DeleteMapping(((ImageText *) text->data)->mapping) ;
- break ;
- case T_MARBLE:
- break ;
- case T_SKY:
- break ;
- case T_STRIPE:
- DeleteSurface(((Stripe *) text->data)->surf) ;
- DeleteMapping(((Stripe *) text->data)->mapping) ;
- break ;
- case T_WOOD:
- break ;
- }
- if(text->data) Free(text->data) ;
- DeleteTrans(text->trans) ;
- }
-
- void DeleteLight(Light *light)
- {
- if(!light) return;
- Free(light->light);
- Free(light->cache);
- Free(light);
- }
-
- void DeleteObject(Geom *obj)
- {
- Trans *trans, *newtrans ;
- if(obj == NULL)
- return ;
-
- switch(ObjectType(obj)) {
- case PLANE:
- break ;
- case SPHERE:
- break ;
- case CONE:
- break ;
- case BOX:
- break ;
- case TORUS:
- break ;
- case BLOB:
- if(((Blob *)obj->obj)->list) Free(((Blob *)obj->obj)->list) ;
- if(((Blob *)obj->obj)->ilist) Free(((Blob *)obj->obj)->ilist) ;
- if(((Blob *)obj->obj)->iarr) Free(((Blob *)obj->obj)->iarr) ;
- break ;
- case DISC:
- break ;
- case TRIANGLE:
- if(((Triangle *)obj->obj)->vnorm) Free(((Triangle *)obj->obj)->vnorm) ;
- if(((Triangle *)obj->obj)->dpdu) Free(((Triangle *)obj->obj)->dpdu) ;
- if(((Triangle *)obj->obj)->dpdv) Free(((Triangle *)obj->obj)->dpdv) ;
- if(((Triangle *)obj->obj)->uv) Free(((Triangle *)obj->obj)->uv) ;
- break ;
- case POLYGON:
- if(((RPolygon *)obj->obj)->points) Free(((RPolygon *)obj->obj)->points) ;
- break ;
- case HF:
- {
- Hf *hf ;
- int i,j ;
-
- hf = obj->obj ;
-
- for(i = 0 ; i < hf->size ; i++)
- Free(hf->data[i]) ;
- Free(hf->data) ;
- Free(hf->q) ;
- for(i = 0 ; i <hf->lsize[0] ; i++) {
- Free(hf->boundsmax[0][i]) ;
- Free(hf->boundsmin[0][i]) ;
- }
-
- for(i = 1 ; i < hf->levels ; i++) {
- for(j = 0 ; j < hf->lsize[i] ; j++) {
- Free(hf->boundsmax[i][j]) ;
- Free(hf->boundsmin[i][j]) ;
- }
- }
- Free(hf->boundsmax[0]) ;
- Free(hf->boundsmin[0]) ;
- Free(hf->boundsmax) ;
- Free(hf->boundsmin) ;
- Free(hf->spacing) ;
- Free(hf->lsize) ;
- }
- break ;
- case CYLINDER:
- break ;
- case CSG:
- DeleteObject(((Csg *)obj->obj)->obj1) ;
- DeleteObject(((Csg *)obj->obj)->obj2) ;
- break ;
- case LIST:
- {
- Geom *obj2, *next ;
-
- obj2 = ((List *)obj->obj)->list ;
- while(obj2) {
- next = obj2->next ;
- DeleteObject(obj2) ;
- obj2 = next ;
- }
- }
- break ;
- case GRID:
- {
- Geom *obj2, *next;
- Grid *grid;
-
- grid = (Grid *)obj->obj;
-
- obj2 = grid->objects;
- while(obj2) {
- next = obj2->next;
- DeleteObject(obj2);
- obj2 = next;
- }
- obj2 = grid->unbounded;
- while(obj2) {
- next = obj2->next;
- DeleteObject(obj2);
- obj2 = next;
- }
- /* Use a procedure already provided in grid.c */
- if(grid->cells)
- GridFreeVoxels(grid);
- }
- break ;
- case INSTANCE:
- break ;
- }
- if(obj->name) Free(obj->name) ;
- if(obj->obj) Free(obj->obj) ;
- DeleteSurface(obj->surf) ;
- trans = obj->trans ;
- while(trans) {
- newtrans = trans->next ;
- DeleteTrans(trans) ;
- trans = newtrans ;
- }
- DeleteTexture(obj->texture) ;
- }