X3DBuffer.c

Go to the documentation of this file.
00001 /* @(#)root/g3d:$Id: X3DBuffer.c 20882 2007-11-19 11:31:26Z rdm $ */
00002 /* Author: Nenad Buncic   13/12/95*/
00003 
00004 #include "X3DBuffer.h"
00005 #include "X3DDefs.h"
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 
00009 
00010 #if defined (WIN32) || defined (__MWERKS__)
00011    void FillX3DBuffer (X3DBuffer *buff) { }
00012    int  AllocateX3DBuffer () { return 0;}
00013 #else
00014 
00015 int currPoint = 0;
00016 int currSeg   = 0;
00017 int currPoly  = 0;
00018 
00019 Color   *colors;
00020 point   *points;
00021 segment *segs;
00022 polygon *polys;
00023 
00024 
00025 int AllocateX3DBuffer ()
00026 {
00027 /******************************************************************************
00028    Allocate memory for points, colors, segments and polygons.
00029    Returns 1 if OK, otherwise 0.
00030 ******************************************************************************/
00031 
00032     int ret = 1;
00033 
00034     points = NULL;
00035     colors = NULL;
00036     segs   = NULL;
00037     polys  = NULL;
00038 
00039     /*
00040      *  Allocate memory for points
00041      */
00042 
00043     if (gSize3D.numPoints) {
00044         points = (point *) calloc(gSize3D.numPoints, sizeof (point));
00045         if (!points) {
00046             puts ("Unable to allocate memory for points !");
00047             ret = 0;
00048         }
00049     }
00050     else return (0);    /* if there are no points, return back */
00051 
00052 
00053 
00054     /*
00055      *  Allocate memory for colors
00056      */
00057 
00058     colors = (Color *) calloc(28+4, sizeof (Color));
00059     if(!colors) {
00060         puts ("Unable to allocate memory for colors !");
00061         ret = 0;
00062     }
00063     else {
00064         colors[ 0].red = 92;   colors[ 0].green = 92;   colors[0].blue = 92;
00065         colors[ 1].red = 122;  colors[ 1].green = 122;  colors[1].blue = 122;
00066         colors[ 2].red = 184;  colors[ 2].green = 184;  colors[2].blue = 184;
00067         colors[ 3].red = 215;  colors[ 3].green = 215;  colors[3].blue = 215;
00068         colors[ 4].red = 138;  colors[ 4].green = 15;   colors[4].blue = 15;
00069         colors[ 5].red = 184;  colors[ 5].green = 20;   colors[5].blue = 20;
00070         colors[ 6].red = 235;  colors[ 6].green = 71;   colors[6].blue = 71;
00071         colors[ 7].red = 240;  colors[ 7].green = 117;  colors[7].blue = 117;
00072         colors[ 8].red = 15;   colors[ 8].green = 138;  colors[8].blue = 15;
00073         colors[ 9].red = 20;   colors[ 9].green = 184;  colors[9].blue = 20;
00074         colors[10].red = 71;   colors[10].green = 235;  colors[10].blue = 71;
00075         colors[11].red = 117;  colors[11].green = 240;  colors[11].blue = 117;
00076         colors[12].red = 15;   colors[12].green = 15;   colors[12].blue = 138;
00077         colors[13].red = 20;   colors[13].green = 20;   colors[13].blue = 184;
00078         colors[14].red = 71;   colors[14].green = 71;   colors[14].blue = 235;
00079         colors[15].red = 117;  colors[15].green = 117;  colors[15].blue = 240;
00080         colors[16].red = 138;  colors[16].green = 138;  colors[16].blue = 15;
00081         colors[17].red = 184;  colors[17].green = 184;  colors[17].blue = 20;
00082         colors[18].red = 235;  colors[18].green = 235;  colors[18].blue = 71;
00083         colors[19].red = 240;  colors[19].green = 240;  colors[19].blue = 117;
00084         colors[20].red = 138;  colors[20].green = 15;   colors[20].blue = 138;
00085         colors[21].red = 184;  colors[21].green = 20;   colors[21].blue = 184;
00086         colors[22].red = 235;  colors[22].green = 71;   colors[22].blue = 235;
00087         colors[23].red = 240;  colors[23].green = 117;  colors[23].blue = 240;
00088         colors[24].red = 15;   colors[24].green = 138;  colors[24].blue = 138;
00089         colors[25].red = 20;   colors[25].green = 184;  colors[25].blue = 184;
00090         colors[26].red = 71;   colors[26].green = 235;  colors[26].blue = 235;
00091         colors[27].red = 117;  colors[27].green = 240;  colors[27].blue = 240;
00092     }
00093 
00094 
00095     /*
00096      *  Allocate memory for segments
00097      */
00098 
00099     if (gSize3D.numSegs) {
00100         segs = (segment *) calloc (gSize3D.numSegs, sizeof (segment));
00101         if (!segs) {
00102             puts ("Unable to allocate memory for segments !");
00103             ret = 0;
00104         }
00105     }
00106 
00107 
00108     /*
00109      * Allocate memory for polygons
00110      */
00111 
00112     if (gSize3D.numPolys) {
00113         polys = (polygon *) calloc(gSize3D.numPolys, sizeof (polygon));
00114         if (!polys) {
00115             puts ("Unable to allocate memory for polygons !");
00116             ret = 0;
00117         }
00118     }
00119 
00120     /*
00121      * In case of error, free allocated memory
00122      */
00123 
00124 
00125     if (!ret) {
00126         if (points) free (points);
00127         if (colors) free (colors);
00128         if (segs)   free (segs);
00129         if (polys)  free (polys);
00130 
00131         points = NULL;
00132         colors = NULL;
00133         segs   = NULL;
00134         polys  = NULL;
00135     }
00136 
00137     return (ret);
00138 }
00139 
00140 void FillX3DBuffer (X3DBuffer *buff)
00141 {
00142 /******************************************************************************
00143    Read points, Read segments & Read polygons
00144 ******************************************************************************/
00145 
00146 
00147     int n, i, j, p, q, c;
00148     int oldNumOfPoints, oldNumOfSegments;
00149 
00150     if (buff) {
00151 
00152         oldNumOfPoints   = currPoint;
00153         oldNumOfSegments = currSeg;
00154 
00155         /*
00156          * Read points
00157          */
00158 
00159         for (i = 0; i < buff->numPoints; i++, currPoint++) {
00160             points[currPoint].x = buff->points[3*i  ];
00161             points[currPoint].y = buff->points[3*i+1];
00162             points[currPoint].z = buff->points[3*i+2];
00163         }
00164 
00165 
00166         /*
00167          * Read segments
00168          */
00169 
00170         for (i = 0; i < buff->numSegs; i++, currSeg++) {
00171             c = buff->segs[3*i];
00172             p = oldNumOfPoints + buff->segs[3*i+1];
00173             q = oldNumOfPoints + buff->segs[3*i+2];
00174 
00175             segs[currSeg].color = &(colors[c]);
00176             segs[currSeg].P     = &(points[p]);
00177             segs[currSeg].Q     = &(points[q]);
00178 
00179             /*
00180              * Update points' segment lists
00181              */
00182 
00183             if(points[p].numSegs == 0){
00184                 if((points[p].segs = (segment **)calloc(1, sizeof(segment *))) == NULL){
00185                     puts("Unable to allocate memory for point segments !");
00186                     return;
00187                 }
00188             }else{
00189                 if((points[p].segs = (segment **)realloc(points[p].segs,
00190                     (points[p].numSegs + 1) * sizeof(segment *))) == NULL){
00191                     puts("Unable to allocate memory for point segments !");
00192                     return;
00193                 }
00194             }
00195 
00196             if(points[q].numSegs == 0){
00197                 if((points[q].segs = (segment **)calloc(1, sizeof(segment *))) == NULL){
00198                     puts("Unable to allocate memory for point segments !");
00199                     return;
00200                 }
00201             }else{
00202                 if((points[q].segs = (segment **)realloc(points[q].segs,
00203                     (points[q].numSegs + 1) * sizeof(segment *))) == NULL){
00204                     puts("Unable to allocate memory for point segments !");
00205                     return;
00206                 }
00207             }
00208             points[p].segs[points[p].numSegs] = &(segs[currSeg]);
00209             points[q].segs[points[q].numSegs] = &(segs[currSeg]);
00210             points[p].numSegs++;
00211             points[q].numSegs++;
00212 
00213         }
00214 
00215         /*
00216          * Read polygons
00217          */
00218 
00219         n = 0;
00220 
00221         for (i = 0; i < buff->numPolys; i++, currPoly++) {
00222             c = buff->polys[n++];
00223             polys[currPoly].color   = &(colors)[c];
00224             polys[currPoly].numSegs = buff->polys[n++];
00225 
00226             polys[currPoly].segs    = (segment **) calloc(polys[currPoly].numSegs, sizeof(segment *));
00227             if (!polys[currPoly].segs) {
00228                 puts("Unable to allocate memory for polygon segments !");
00229                 return;
00230             }
00231             for (j = 0; j < polys[currPoly].numSegs; j++) {
00232                 int seg = oldNumOfSegments + buff->polys[n++];
00233                 polys[currPoly].segs[j] = &(segs[seg]);
00234 
00235                 /*
00236                  * Update segments' polygon lists
00237                  */
00238 
00239                 if(segs[seg].numPolys == 0) {
00240                     if((segs[seg].polys = (polygon **) calloc(1, sizeof(polygon *)))== NULL){
00241                         puts("Unable to allocate memory for segment polygons !");
00242                         return;
00243                     }
00244                 }
00245                 else{
00246                     if((segs[seg].polys = (polygon **) realloc(segs[seg].polys,
00247                         (segs[seg].numPolys + 1) * sizeof(polygon *))) == NULL){
00248                         puts("Unable to allocate memory for segment polygons !");
00249                         return;
00250                     }
00251                 }
00252                 segs[seg].polys[segs[seg].numPolys] = &(polys[currPoly]);
00253                 segs[seg].numPolys++;
00254             }
00255         }
00256     }
00257 }
00258 
00259 #endif
00260 

Generated on Tue Jul 5 14:16:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1