summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Mikos <david@FAMILYCOMPUTER.(none)>2008-02-19 19:20:24 +1030
committerDavid Mikos <david@FAMILYCOMPUTER.(none)>2008-02-19 19:20:24 +1030
commitf9a4809a119e5f45075082dcab6f938e610724a6 (patch)
treedc7359b872c6950d5d6f67d53ba4029cd98eef4f
parent4f26d204f464f9977bd5ff636ff764ee964b646b (diff)
downloadcubedbus-f9a4809a119e5f45075082dcab6f938e610724a6.tar.gz
cubedbus-f9a4809a119e5f45075082dcab6f938e610724a6.tar.bz2
In model loader, save some memory, ignore bad vertices, allow relative paths.
-rw-r--r--cubedbusModel.c.in134
-rwxr-xr-xscripts/animatedModel.sh2
-rwxr-xr-xscripts/model.sh2
3 files changed, 77 insertions, 61 deletions
diff --git a/cubedbusModel.c.in b/cubedbusModel.c.in
index 472e4f3..d44c4d4 100644
--- a/cubedbusModel.c.in
+++ b/cubedbusModel.c.in
@@ -55,51 +55,51 @@ typedef struct _groupIndices
struct _mtlStruct
{
-GLfloat Ka[4];
-GLfloat Kd[4];
-GLfloat Ks[4];
+ GLfloat Ka[4];
+ GLfloat Kd[4];
+ GLfloat Ks[4];
-GLfloat Ns[1]; /* shininess */
-GLfloat Ni[1]; /* optical_density - currently not used*/
+ GLfloat Ns[1]; /* shininess */
+ GLfloat Ni[1]; /* optical_density - currently not used*/
-int illum;
+ int illum;
-unsigned height, width;
+ unsigned height, width;
-CompTexture *tex;
+ CompTexture *tex;
};
struct _CubedbusModelObject
{
-GLuint dList;
-bool order;
-float rotate[4], translate[3], scale[3];
-float rotateSpeed, scaleGlobal;
+ GLuint dList;
+ bool order;
+ float rotate[4], translate[3], scale[3];
+ float rotateSpeed, scaleGlobal;
-float color[4];
+ float color[4];
-int fileCounter;
-Bool animation;
-int fps;
+ int fileCounter;
+ Bool animation;
+ int fps;
-vect3d **reorderedVertex;
-vect3d **reorderedNormal;
-vect3d **reorderedTexture;
+ vect3d **reorderedVertex;
+ vect2d **reorderedTexture;
+ vect3d **reorderedNormal;
-unsigned int * indices;
-groupIndices *group;
+ unsigned int * indices;
+ groupIndices *group;
-vect3d *reorderedVertexBuffer;
-vect2d *reorderedTextureBuffer;
-vect3d *reorderedNormalBuffer;
+ vect3d *reorderedVertexBuffer;
+ vect2d *reorderedTextureBuffer;
+ vect3d *reorderedNormalBuffer;
-int nVertex;
-int nTexture;
-int nNormal;
-int nGroups;
-int nIndices;
+ int nVertex;
+ int nTexture;
+ int nNormal;
+ int nGroups;
+ int nIndices;
-mtlStruct **material;
+ mtlStruct **material;
};
@@ -202,7 +202,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if (animation)
- size+=maxNumZeros+1+4; /* numbers + '_' + ".obj" */
+ size+=strlen(pre)+maxNumZeros+strlen(post)+4; /* '$pre' + numbers + '$post' + ".obj" */
char * filename = malloc (size*sizeof(char));
@@ -257,15 +257,12 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if (fileCounter == 0)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
- "Failed to open model file :");
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
- filename);
+ "Failed to open model file : %s", filename);
if (filename)
free (filename);
return FALSE;
}
-
modelData->reorderedVertex = malloc(sizeof(vect3d*)*fileCounter);
modelData->reorderedTexture = malloc(sizeof(vect2d*)*fileCounter);
modelData->reorderedNormal = malloc(sizeof(vect3d*)*fileCounter);
@@ -282,7 +279,8 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
int oldPolyCount = 0;
Bool oldUsingNormal = FALSE;
Bool oldUsingTexture = FALSE;
-
+
+
int fc;
for (fc = 0; fc<fileCounter; fc++) {
@@ -361,7 +359,6 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
fclose(fp);
-
/* Second pass - fill arrays */
fp = fopen(filename, "r");
@@ -373,8 +370,8 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
vertex = realloc(vertex, sizeof(vect3d)*nVertex);
- normal = realloc(normal, sizeof(vect3d)*nNormal);
texture = realloc(texture, sizeof(vect2d)*nTexture);
+ normal = realloc(normal, sizeof(vect3d)*nNormal);
modelData->reorderedVertex[fc] = malloc(sizeof(vect3d)*nIndices);
modelData->reorderedTexture[fc] = malloc(sizeof(vect2d)*nIndices);
@@ -382,6 +379,9 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
modelData->material[fc] = malloc(sizeof(mtlStruct)*nMaterial);
mtllib[fc] = malloc(sizeof(char **) * nMtllib[fc]);
+
+ for (i=0; i<nMtllib[fc]; i++)
+ mtllib[fc][i] = NULL;
if (fc==0)
{
@@ -408,7 +408,6 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
nUniqueIndices = 0;
nMtllib[fc] = 0;
-
while ((strline=getLine(fp, &oldStrline))!=NULL)
{
tmpType=strsep(&strline, delim);
@@ -437,22 +436,43 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if (!strcmp(tmpType, "v"))
{
for (i=0; i<3; i++)
+ {
+ if (strline==NULL)
+ {
+ vertex[nVertex].r[0]=0;
+ vertex[nVertex].r[1]=0;
+ vertex[nVertex].r[2]=0;
+ break;
+ }
vertex[nVertex].r[i]=atof(strsep(&strline, delim))*modelData->scaleGlobal;
-
+ }
nVertex++;
}
else if (!strcmp(tmpType, "vn"))
{
for (i=0; i<3; i++)
+ {
+ if (strline==NULL)
+ {
+ normal[nNormal].r[0] = 0;
+ normal[nNormal].r[1] = 0;
+ normal[nNormal].r[2] = 1;
+ break;
+ }
normal[nNormal].r[i]=atof(strsep(&strline, delim));
-
+ }
nNormal++;
}
else if (!strcmp(tmpType, "vt"))
{
- for (i=0; i<2; i++)
- texture[nTexture].r[i]=atof(strsep(&strline, delim));
-
+ texture[nTexture].r[0]=atof(strsep(&strline, delim));
+ if (strline!=NULL)
+ texture[nTexture].r[1]=atof(strsep(&strline, delim));
+ else
+ {
+ texture[nTexture].r[0]=0;
+ texture[nTexture].r[1]=0;
+ }
nTexture++;
}
else if (!strcmp(tmpType, "mtllib"))
@@ -480,8 +500,6 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
fclose(fp);
-
-
@@ -564,8 +582,6 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
strcpy (materialName, tmpType);
- //printf ("material name - %s\n", materialName);
-
for (i=0; i<nMtllib[fc] && !loadingMaterial; i++)
@@ -726,7 +742,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
else
finiTexture(obj->s, modelData->material[fc][nMaterial].tex);
- if (tmpName != NULL)
+ if (tmpName)
free(tmpName);
tmpName = NULL;
}
@@ -953,6 +969,8 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
+
+
modelData->nGroups = nGroups;
if (!animation)
@@ -1196,10 +1214,9 @@ cubedbusDrawModelObject (CubedbusObject *obj)
}
data->rotate[0]+=data->rotateSpeed;
- if(data->rotate[0]>360) data->rotate[0]=0;
- if(data->rotate[0]<0) data->rotate[0]=360;
+ if(data->rotate[0]>360) data->rotate[0]-=360;
+ if(data->rotate[0]<0) data->rotate[0]+=360;
-
return TRUE;
}
@@ -1211,7 +1228,6 @@ cubedbusDrawVBOModel (CubedbusObject *obj, float * vertex, float * normal)
groupIndices * group;
int i, j;
-
for (i=0; i<data->nGroups; i++)
{
group = &(data->group[i]);
@@ -1251,7 +1267,6 @@ cubedbusDrawVBOModel (CubedbusObject *obj, float * vertex, float * normal)
glEnable(data->material[0][group->materialIndex].tex->target);
enableTexture(obj->s, data->material[0][group->materialIndex].tex,
COMP_TEXTURE_FILTER_GOOD);
-
}
}
glEnableClientState(GL_VERTEX_ARRAY);
@@ -1290,12 +1305,13 @@ cubedbusDrawVBOModel (CubedbusObject *obj, float * vertex, float * normal)
}
-/*************************************
-* returns string with line from file *
+/*******************************************
+* returns string with line from file *
* NOTE: you must free oldStrline after use *
-* NOTE: can use strsep on strline (modifies the address of strline)
-* NOTE: returns NULL if error occurs *
-*************************************/
+* NOTE: can use strsep on strline *
+* (modifies the address of strline) *
+* NOTE: returns NULL if error occurs *
+********************************************/
static char *
getLine (FILE *fp, char **oldStrline)
diff --git a/scripts/animatedModel.sh b/scripts/animatedModel.sh
index 54fc9db..589c25b 100755
--- a/scripts/animatedModel.sh
+++ b/scripts/animatedModel.sh
@@ -1,3 +1,3 @@
-dbus-send --type=method_call --dest=org.freedesktop.compiz /org/freedesktop/compiz/cubedbus/allscreens/add org.freedesktop.compiz.activate string:'root' int32:`xwininfo -root | grep id: | awk '{ print $4 }'` string:'type' int32:5 string:'name' string:'root.model' string:'file' string:$1 string:'rotateY' double:1.0 string:'rotateSpeed' double:1.0 string:'animation' boolean:true string:'r' double:1.0 string:'g' double:1.0 string:'b' double:1.0 string:'a' double:1.0 string:'scale' double:1.0 string:'numZeros' int32:2 string:'fps' int32:15 string:'start' int32:0 string:'pre' string:'_' string:'post' string:''
+dbus-send --type=method_call --dest=org.freedesktop.compiz /org/freedesktop/compiz/cubedbus/allscreens/add org.freedesktop.compiz.activate string:'root' int32:`xwininfo -root | grep id: | awk '{ print $4 }'` string:'type' int32:5 string:'name' string:'root.model' string:'file' string:`echo $(cd \`dirname $1\` && pwd)`/`basename $1` string:'rotateY' double:1.0 string:'rotateSpeed' double:1.0 string:'animation' boolean:true string:'r' double:1.0 string:'g' double:1.0 string:'b' double:1.0 string:'a' double:1.0 string:'scale' double:1.0 string:'numZeros' int32:2 string:'fps' int32:15 string:'start' int32:0 string:'pre' string:'_' string:'post' string:''
#Run this in terminal - accepts as argument 1 .obj file
diff --git a/scripts/model.sh b/scripts/model.sh
index d13c40d..0af2919 100755
--- a/scripts/model.sh
+++ b/scripts/model.sh
@@ -1,3 +1,3 @@
-dbus-send --type=method_call --dest=org.freedesktop.compiz /org/freedesktop/compiz/cubedbus/allscreens/add org.freedesktop.compiz.activate string:'root' int32:`xwininfo -root | grep id: | awk '{ print $4 }'` string:'type' int32:5 string:'name' string:'root.model' string:'file' string:$1 string:'rotateY' double:1.0 string:'rotateSpeed' double:1.0
+dbus-send --type=method_call --dest=org.freedesktop.compiz /org/freedesktop/compiz/cubedbus/allscreens/add org.freedesktop.compiz.activate string:'root' int32:`xwininfo -root | grep id: | awk '{ print $4 }'` string:'type' int32:5 string:'name' string:'root.model' string:'file' string:`echo $(cd \`dirname $1\` && pwd)`/`basename $1` string:'rotateY' double:1.0 string:'rotateSpeed' double:1.0
#Run this in terminal - accepts as argument 1 .obj file