summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Mikos <metastability@opencompositing.org>2008-01-29 00:29:28 +1030
committerJoel Bosveld <joel@joel-desktop.(none)>2008-01-31 22:31:14 +0900
commit76dd910e9cacd62899348ebc927b734ef7e061a9 (patch)
tree33190c72d4863ba648abb1388682bbd091935094
parent841b94f322da17cadb94a54017fbb392692b887f (diff)
downloadcubedbus-76dd910e9cacd62899348ebc927b734ef7e061a9.tar.gz
cubedbus-76dd910e9cacd62899348ebc927b734ef7e061a9.tar.bz2
Patch for animated obj models.
-rw-r--r--cubedbus.c63
-rw-r--r--cubedbus.h2
-rw-r--r--cubedbusModel.c.in1139
3 files changed, 701 insertions, 503 deletions
diff --git a/cubedbus.c b/cubedbus.c
index ba527e8..fc63b2c 100644
--- a/cubedbus.c
+++ b/cubedbus.c
@@ -215,50 +215,33 @@ cubedbusModifyObject (CompDisplay *d,
CompOption *option,
int nOption)
{
- //get gs pointer
CompScreen *s;
Window xid;
xid = getIntOptionNamed (option, nOption, "root", 0);
s = findScreenAtDisplay (d, xid);
- if(!s) return FALSE;
+ if(!s) return FALSE;
CUBEDBUS_SCREEN(s);
- CubedbusObject *head=gs->head;
- CubedbusObject *objptr=gs->head;
-
- CubedbusName *cubedbusName = malloc (sizeof(CubedbusName));
- int i;
-
- if(!cubedbusGetName( getStringOptionNamed(option,nOption,"name",""), cubedbusName )) return FALSE;
-
- /* sets the head pointer correctly */
+ CubedbusObject *objptr;
+ objptr=gs->head;
+ char name[16];
- for(i=0;i<cubedbusName->n -1 ;i++) /* stop 1 before the object to be created */
+ char *tempName=getStringOptionNamed (option,nOption,"name", "");
+
+ if(strlen(tempName) >= 16) /* should be array length */
{
- while(1)
- {
- if(objptr == NULL) return FALSE;
-
- if(!strcmp(objptr->name, cubedbusName->string[i]))
- {
- head=objptr;
- CubedbusObj *data=objptr->data;
- objptr=data->firstChild;
- break;
- }
- else objptr=objptr->next;
- }
+ compLogMessage (NULL,"cubedbus",CompLogLevelError, "name is too long");
+ return FALSE;
}
-
- while(1)
+ strcpy(name,tempName);
+
+ while(1)
{
- if(objptr==NULL) return false;
- if(!strcmp(objptr->name,cubedbusName->string[i]))
- {
- return (objptr->modfunc) (objptr, option, nOption);
- }
+ if(objptr==NULL) return FALSE;
+ if(!strcmp(objptr->name,name)) return (objptr->modfunc) (objptr, option, nOption);
objptr=objptr->next;
}
+
}
@@ -364,23 +347,13 @@ cubedbusPaintInside (CompScreen *s,
CUBEDBUS_SCREEN (s);
CUBE_SCREEN (s);
-
Bool enabled;
ScreenPaintAttrib sA = *sAttrib;
sA.yRotate += cs->invert * (360.0f / size) *
(cs->xRotations - (s->x * cs->nOutput)); /*?*/
-
- FILE *fp=fopen("/tmp/compiz.matrix","w");
- fprintf(fp,"%f\n",sA.xRotate);
- fprintf(fp,"%f\n",sA.yRotate);
- fprintf(fp,"%f\n",sA.vRotate);
- fprintf(fp,"%f\n",sA.xTranslate);
- fprintf(fp,"%f\n",sA.yTranslate);
- fprintf(fp,"%f\n",sA.zTranslate);
- fprintf(fp,"%f\n",sA.zCamera);
-fclose(fp);
+
CompTransform mT = *transform;
(*s->applyScreenTransform) (s, &sA, output, &mT);
@@ -455,6 +428,7 @@ cubedbusPreparePaintScreen (CompScreen *s,
{
CUBEDBUS_SCREEN (s);
+ timeSec += ms/1000.0f;
/* check for messages */
UNWRAP (gs, s, preparePaintScreen);
@@ -545,7 +519,8 @@ cubedbusInitScreen (CompPlugin *p,
s->base.privates[gd->screenPrivateIndex].ptr = gs; /* For the CUBEDBUS_SCREEN macro to work */
-
+ timeSec = 0;
+
/**/
gs->head= malloc (sizeof(CubedbusObject));
if(gs->head == NULL) return FALSE;
diff --git a/cubedbus.h b/cubedbus.h
index 227a214..369062c 100644
--- a/cubedbus.h
+++ b/cubedbus.h
@@ -39,4 +39,6 @@ static bool
cubedbusGetName(char *name, CubedbusName *storage);
+//global variable (not part of Compiz style)
+float timeSec;
diff --git a/cubedbusModel.c.in b/cubedbusModel.c.in
index 4f6f6c5..05d9f43 100644
--- a/cubedbusModel.c.in
+++ b/cubedbusModel.c.in
@@ -10,22 +10,38 @@ static bool cubedbusDeleteModelObject (CubedbusObject *obj);
static bool cubedbusModifyModelObject (CubedbusObject *obj, CompOption *option, int nOption);
static bool cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption);
+
+static int
+addNumToString (char *, unsigned int, int, unsigned int, unsigned int);
+
+
static char *getLine (FILE *fp);
static char *findPath (char *object, char *file);
typedef struct _vect3d
{
-float x;
-float y;
-float z;
+ float r[3];
} vect3d;
typedef struct _vect2d
{
-float x;
-float y;
+ float r[2];
} vect2d;
+typedef struct _groupIndices
+{
+ int polyCount;
+
+ int startV;
+ int numV;
+ int startT;
+ int numT;
+ int startN;
+ int numN;
+
+ Bool texture;
+ Bool normal;
+} groupIndices;
struct _mtlStruct
{
@@ -52,6 +68,25 @@ bool order;
float rotate[4], translate[3], scale[3];
float rotateSpeed;
+int fileCounter;
+Bool animation;
+int fps;
+
+vect3d **vertex;
+vect3d **normal;
+vect2d **texture;
+vect3d **reorderedNormal;
+char ***mtllib;
+unsigned int * indices;
+groupIndices *group;
+
+vect3d *vertexBuffer;
+vect2d *textureBuffer;
+vect3d *reorderedNormalBuffer;
+
+int nVertex;
+int nGroups;
+
mtlStruct *head;
};
@@ -61,463 +96,601 @@ static bool
cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
{
- obj->data= malloc( sizeof(CubedbusModelObject) );
- if(obj->data == NULL) return FALSE;
-
- CubedbusModelObject *modelData=obj->data;
+ obj->data= malloc(sizeof(CubedbusModelObject));
+ if (obj->data == NULL)
+ return FALSE;
+
+ CubedbusModelObject *modelData=obj->data;
+
+ modelData->rotate[0]=getFloatOptionNamed(option, nOption, "rotateAngle",
+ 0.0);
+ modelData->rotate[1]=getFloatOptionNamed(option, nOption, "rotateX", 0.0);
+ modelData->rotate[2]=getFloatOptionNamed(option, nOption, "rotateY", 0.0);
+ modelData->rotate[3]=getFloatOptionNamed(option, nOption, "rotateZ", 0.0);
+ modelData->translate[0]=getFloatOptionNamed(option, nOption, "translateX",
+ 0.0);
+ modelData->translate[1]=getFloatOptionNamed(option, nOption, "translateY",
+ 0.0);
+ modelData->translate[2]=getFloatOptionNamed(option, nOption, "translateZ",
+ 0.0);
+ modelData->scale[0]=getFloatOptionNamed(option, nOption, "scaleX", 1.0);
+ modelData->scale[1]=getFloatOptionNamed(option, nOption, "scaleY", 1.0);
+ modelData->scale[2]=getFloatOptionNamed(option, nOption, "scaleZ", 1.0);
+ modelData->order=getBoolOptionNamed(option, nOption, "order", TRUE);
+ modelData->rotateSpeed=getFloatOptionNamed(option, nOption, "rotateSpeed",
+ 0);
+
+ int i;
+
+ char *strline=NULL;
+ char **tmpType;
+
+ int nVertex=0;
+ int nNormal=0;
+ int nTexture=0;
+ int nMtllib=0;
+ int nIndices=0;
+
+ char delim[]= { ' ', '\t', '\0' };
+
+ Bool animation = TRUE;
+ modelData->animation = animation;
+ modelData->fps = 3;
+
+ float scaleFactor = 0.2f;
+
+
+ int maxNumZeros = 6; //number of zeros in FILENAME00000.obj
+
+ int startFileNum = 1;
+
+ char * baseFilename = getStringOptionNamed(option, nOption, "file", "");
+
+ int lenBaseFilename = strlen(baseFilename);
+
+ /* for animation, strip the file extension from the filename */
+ if (animation &&
+ baseFilename[lenBaseFilename-4]=='.' && baseFilename[lenBaseFilename-3]=='o' &&
+ baseFilename[lenBaseFilename-2]=='b' && baseFilename[lenBaseFilename-1]=='j')
+ {
+ lenBaseFilename-=4;
+ }
+
+ int size = lenBaseFilename+1;
+
+
+ if (animation)
+ size+=maxNumZeros+1+4; //numbers + '_' + ".obj"
+
+
+ char * filename = malloc (size*sizeof(char));
+
+ memcpy (filename, baseFilename, (lenBaseFilename+1)*sizeof(char));
+
+ filename[lenBaseFilename]='\0';
+
+
+ int fileCounter = 0;
+
+
+ /* verify existence of files and/or check for animation frames */
+
+ FILE *fp;
+
+ do {
+ if (animation)
+ size = addNumToString (filename, size, lenBaseFilename, startFileNum+fileCounter, maxNumZeros);
- modelData->rotate[0]=getFloatOptionNamed (option,nOption, "rotateAngle", 0.0);
- modelData->rotate[1]=getFloatOptionNamed (option,nOption, "rotateX", 0.0);
- modelData->rotate[2]=getFloatOptionNamed (option,nOption, "rotateY", 0.0);
- modelData->rotate[3]=getFloatOptionNamed (option,nOption, "rotateZ", 0.0);
- modelData->translate[0]=getFloatOptionNamed (option,nOption, "translateX", 0.0);
- modelData->translate[1]=getFloatOptionNamed (option,nOption, "translateY", 0.0);
- modelData->translate[2]=getFloatOptionNamed (option,nOption, "translateZ", 0.0);
- modelData->scale[0]=getFloatOptionNamed (option,nOption, "scaleX", 1.0);
- modelData->scale[1]=getFloatOptionNamed (option,nOption, "scaleY", 1.0);
- modelData->scale[2]=getFloatOptionNamed (option,nOption, "scaleZ", 1.0);
- modelData->order=getBoolOptionNamed (option, nOption, "order", TRUE);
- modelData->rotateSpeed=getFloatOptionNamed (option, nOption, "rotateSpeed", 0);
+ fp = fopen(filename, "r");
+ if (fp!=NULL) {
+ printf ("opened %s\n",filename);
+
+ fclose(fp);
+ fileCounter++;
+ }
+ } while (animation && fp!=NULL);
+
+ modelData->fileCounter = fileCounter;
+
+ if (fileCounter == 0)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Failed to open model file :");
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ filename);
+ if (filename)
+ free (filename);
+ return FALSE;
+ }
+
+
+ modelData->vertex = malloc(sizeof(vect3d*)*fileCounter);
+ modelData->normal = malloc(sizeof(vect3d*)*fileCounter);
+ modelData->texture = malloc(sizeof(vect2d*)*fileCounter);
+
+ modelData->reorderedNormal = malloc(sizeof(vect3d*)*fileCounter);
+
+ modelData->mtllib = malloc(sizeof(char **)*fileCounter);
+
+ modelData->indices = NULL;
+ modelData->group = NULL;
+
+ int nGroups = 0;
+
+ int oldPolyCount = 0;
+ Bool oldUsingNormal = FALSE;
+
+ int fc;
+
+ for (fc = 0; fc<fileCounter; fc++) {
+
+
+ /* First pass - count how much data we need to store */
+
+ size = addNumToString (filename, size, lenBaseFilename, startFileNum+fc, maxNumZeros);
+
+ fp = fopen(filename, "r");
+ if (fp == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Failed to open model file");
+ return FALSE;
+ }
+
+ tmpType = malloc(sizeof(char *) * 2);
+ while ((strline=getLine(fp))!=NULL)
+ {
+ if (tmpType==NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "out of mem");
+ return FALSE;
+ }
- char *strline=NULL;
- char **tmpType;
-
- int nVertex=0;
- int nNormal=0;
- int nTexture=0;
- int nMtllib=0;
+ tmpType[0]=strsep(&strline, delim);
+ if (strline==NULL) continue;
- char delim[]={' ','\t','\0'};
-
- /* First pass - count how much data we need to store */
-
- FILE *fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
- if(fp == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Failed to open model file");
- return FALSE;
+ if (tmpType[0] == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Error reading file...");
+ //needs extra freeing here
+ if (filename)
+ free (filename);
+ return FALSE;
+ }
+ while (strline!=NULL) {
+ if (!strcmp(tmpType[0], "")) {
+ tmpType[0]=strsep(&strline, delim);
}
-
- while((strline=getLine(fp))!=NULL)
+ else break;
+
+ }
+ if (strline==NULL || tmpType[0]==NULL) continue;
+
+ if (!strcmp(tmpType[0], "v"))
+ nVertex++;
+ if (!strcmp(tmpType[0], "vn"))
+ nNormal++;
+ if (!strcmp(tmpType[0], "vt"))
+ nTexture++;
+
+ if (!strcmp(tmpType[0], "mtllib"))
+ {
+ while (1)
+ {
+ tmpType[1]=strsep(&strline, delim);
+ if (tmpType[1]==NULL)
+ break;
+ if (!strcmp(tmpType[1], ""))
+ continue;
+ nMtllib++;
+ }
+ }
+ if (!strcmp(tmpType[0], "f"))
{
- tmpType = malloc ( sizeof (char *) * 2);
- tmpType[0]=strsep(&strline,delim);
+ while ((tmpType[0]=strsep(&strline, delim))!=NULL) {
+ nIndices++;
+ }
+ }
+ }
+
+ //no need to free (strline) because getLine does that at the end with a realloc size 0
+
+ fclose(fp);
+
+
+ /* Second pass - fill arrays */
+
+ fp = fopen(filename, "r");
+ if (fp == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Failed to open model file");
+ return FALSE;
+ }
+
+ modelData->vertex[fc] = malloc(sizeof(vect3d)*nVertex);
+ modelData->normal[fc] = malloc(sizeof(vect3d)*nNormal);
+ modelData->texture[fc] = malloc(sizeof(vect2d)*nTexture);
+
+ modelData->reorderedNormal[fc] = malloc(sizeof(vect3d)*nVertex);
+
+ modelData->mtllib[fc] = malloc(sizeof(char *) * (nMtllib+1)); /* it will place a NULL pointer in last position */
+
+ if (fc==0)
+ modelData->indices =malloc(sizeof(unsigned int *)*nIndices);
+
+ modelData->vertexBuffer = malloc(sizeof(vect3d)*nVertex);
+ modelData->textureBuffer = malloc(sizeof(vect2d)*nTexture);
+ modelData->reorderedNormalBuffer = malloc(sizeof(vect3d)*nVertex);
+
+ modelData->nVertex = nVertex;
+
+ nVertex = 0;
+ nNormal = 0;
+ nTexture = 0;
+ nMtllib = 0;
+ nIndices = 0;
+
+
+ tmpType = (char **) realloc(tmpType, sizeof(char *));
+ while ((strline=getLine(fp))!=NULL)
+ {
+ if (tmpType==NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "out of mem");
+ return FALSE;
+ }
+
+ tmpType[0]=strsep(&strline, delim);
+ if (strline==NULL) continue;
+
+ if (tmpType[0] == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Error reading file...");
+ //needs extra freeing here
+ if (filename)
+ free (filename);
+ return FALSE;
+ }
+ while (strline!=NULL) {
+ if (!strcmp(tmpType[0], "")) {
+ tmpType[0]=strsep(&strline, delim);
+ }
+ else break;
+
+ }
+ if (strline==NULL || tmpType[0]==NULL) continue;
+
+ if (!strcmp(tmpType[0], "v"))
+ {
+ for (i=0; i<3; i++)
+ modelData->vertex[fc][nVertex].r[i]=atof(strsep(&strline, delim))*scaleFactor;
- if(tmpType[0] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- if(!strcmp(tmpType[0], "v" )) nVertex++;
- if(!strcmp(tmpType[0], "vn")) nNormal++;
- if(!strcmp(tmpType[0], "vt")) nTexture++;
-
- if(!strcmp(tmpType[0], "mtllib"))
- {
- while(1)
- {
- tmpType[1]=strsep(&strline,delim);
- if(tmpType[1]==NULL) break;
- if(!strcmp(tmpType[1],"")) continue;
- nMtllib++;
- }
- }
-
- //free(strline);
- strline=NULL;
- }
-
-
-
- fclose (fp);
-
- /* Second pass - fill arrays */
-
- vect3d *vertex=malloc(sizeof(vect3d)*nVertex);
- vect3d *normal=malloc(sizeof(vect3d)*nNormal);
- vect2d *texture=malloc(sizeof(vect2d)*nTexture);
- char **mtllib=malloc(sizeof(char *) * (nMtllib+1)); /* it will place a NULL pointer in last position */
-
- nVertex=0;
- nNormal=0;
- nTexture=0;
- nMtllib=0;
+ nVertex++;
+ }
+
+ if (!strcmp(tmpType[0], "vn"))
+ {
+ for (i=0; i<3; i++)
+ modelData->normal[fc][nNormal].r[i]=atof(strsep(&strline, delim));
+
+ nNormal++;
+ }
+
+ if (!strcmp(tmpType[0], "vt"))
+ {
+ for (i=0; i<2; i++)
+ modelData->texture[fc][nTexture].r[i]=atof(strsep(&strline, delim));
+
+ nTexture++;
+ }
- fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
- if(fp == NULL)
+ if (!strcmp(tmpType[0], "mtllib"))
{
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Failed to open model file");
- return FALSE;
+ while (1)
+ {
+ modelData->mtllib[fc][nMtllib]=strsep(&strline, delim);
+ if (modelData->mtllib[fc][nMtllib] == NULL)
+ break;
+ if (!strcmp(modelData->mtllib[fc][nMtllib], ""))
+ continue; /* if blank override next time */
+ nMtllib++;
+ }
}
- while((strline=getLine(fp))!=NULL)
+
+ }
+
+ fclose(fp);
+
+ }
+
+
+
+
+ //vert3d *orderedVertex = malloc(sizeof(vect3d)*nVertex);
+
+
+
+ fp = fopen(filename, "r");
+ if (fp == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Failed to open model file");
+ return FALSE;
+ }
+
+
+ tmpType = (char **) realloc(tmpType, sizeof(char *));
+
+ while ((strline=getLine(fp))!=NULL)
+ {
+ if (tmpType==NULL)
{
- tmpType = malloc ( sizeof (char *));
- tmpType[0]=strsep(&strline,delim);
- if(tmpType[0] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- if(!strcmp(tmpType[0], "v"))
- {
- vertex[nVertex].x=atof(strsep(&strline,delim));
- vertex[nVertex].y=atof(strsep(&strline,delim));
- vertex[nVertex].z=atof(strsep(&strline,delim));
-
- nVertex++;
- }
-
- if(!strcmp(tmpType[0], "vn"))
- {
- normal[nNormal].x=atof(strsep(&strline,delim));
- normal[nNormal].y=atof(strsep(&strline,delim));
- normal[nNormal].z=atof(strsep(&strline,delim));
-
- nNormal++;
- }
-
- if(!strcmp(tmpType[0], "vt"))
- {
- texture[nTexture].x=atof(strsep(&strline,delim));
- texture[nTexture].y=atof(strsep(&strline,delim));
-
- nTexture++;
- }
-
- if(!strcmp(tmpType[0], "mtllib"))
- {
- while(1)
- {
- mtllib[nMtllib]=strsep(&strline,delim);
- if(mtllib[nMtllib] == NULL) break;
- if(!strcmp(mtllib[nMtllib],"")) continue; /* if blank override next time */
- nMtllib++;
- }
- }
- //free(strline);
- strline=NULL;
- }
-
- fclose (fp);
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "out of mem");
+ return FALSE;
+ }
+
+ tmpType[0]=strsep(&strline, delim);
+ if (strline==NULL) continue;
+ if (tmpType[0] == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Error reading file...");
+ //needs extra freeing here
+ if (filename)
+ free (filename);
+ return FALSE;
+ }
+ while (strline!=NULL) {
+ if (!strcmp(tmpType[0], "")) {
+ tmpType[0]=strsep(&strline, delim);
+ }
+ else break;
+ }
+ if (strline==NULL || tmpType[0]==NULL) continue;
- mtlStruct *mtlPtr;
+ //printf (tmpType[0]);
+ //continue;
- modelData->head = malloc(sizeof (mtlStruct) );
- if(modelData->head == NULL)
- {
- //free
- return FALSE;
- }
-
- mtlPtr=modelData->head;
+ if (!strcmp(tmpType[0], "f"))
+ {
+
+ char **tmpPtr; /* used to chec value of vertex/texture/normal numbers */
+
+ int polyCount = 0;
+ Bool usingNormal = FALSE;
+
+
+ tmpPtr=malloc(sizeof(char *) * 1);
+
+ while (TRUE) {
- mtlPtr->illum = 0;
- mtlPtr->next=NULL;
- mtlPtr->name=malloc(sizeof(char) * 5);
- strcpy (mtlPtr->name, "fake");
- mtlPtr->tex=NULL;
-
- /* Parse MTL file and load textures */
-
- int mtlCount;
- char *tmpName;
-
- if(nMtllib != 0)
- for(mtlCount=0;mtllib[mtlCount] != NULL;mtlCount++)
- {
- fp=fopen((tmpName=findPath( getStringOptionNamed(option, nOption, "file", ""), mtllib[mtlCount])) , "r");
- if(fp == NULL)
- {
+ tmpType[0]=strsep(&strline, delim);
+ if (tmpType[0]==NULL) break;
+
+ while (strline!=NULL) {
+ if (!strcmp(tmpType[0], "")) {
+ tmpType[0]=strsep(&strline, delim);
+ }
+ else break;
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "mtl: can't open %s: %s",
- tmpName, strerror(errno));
- break;
- }
- free(tmpName);
- tmpName = NULL;
-
- while((strline=getLine(fp))!=NULL)
- {
- /* code to move stuff to array */
- tmpType = malloc ( sizeof (char *) * 4);
- tmpType[0]=strsep(&strline,delim);
-
- if(tmpType[0] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
- if(!strcmp(tmpType[0], "newmtl"))
- {
- tmpType[1]=strsep(&strline,delim);
- if(tmpType[1] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
- mtlPtr->next = malloc (sizeof (mtlStruct) );
- if (mtlPtr->next == NULL) return FALSE;
- mtlPtr=mtlPtr->next;
-
- mtlPtr->name=tmpType[1];
- mtlPtr->next=NULL;
-
- mtlPtr->Ka[0]=mtlPtr->Ka[1]=mtlPtr->Ka[2]=0.2; /* Set defaults */
- mtlPtr->Kd[0]=mtlPtr->Kd[1]=mtlPtr->Kd[2]=0.8;
- mtlPtr->Ks[0]=mtlPtr->Ks[1]=mtlPtr->Ks[2]=1.0;
- mtlPtr->Ka[4]=mtlPtr->Kd[4]=mtlPtr->Ks[4]=1.0; /* Set defaults */
- mtlPtr->Ns[0]=0.0;
-
- mtlPtr->tex = NULL;
- }
- if(!strcmp(tmpType[0], "Ka"))
- {
- mtlPtr->Ka[0]=atof(strsep(&strline,delim));
- mtlPtr->Ka[1]=atof(strsep(&strline,delim));
- mtlPtr->Ka[2]=atof(strsep(&strline,delim));
- }
- if(!strcmp(tmpType[0], "Kd"))
- {
- mtlPtr->Kd[0]=atof(strsep(&strline,delim));
- mtlPtr->Kd[1]=atof(strsep(&strline,delim));
- mtlPtr->Kd[2]=atof(strsep(&strline,delim));
- }
- if(!strcmp(tmpType[0], "Ks"))
- {
- mtlPtr->Ks[0]=atof(strsep(&strline,delim));
- mtlPtr->Ks[1]=atof(strsep(&strline,delim));
- mtlPtr->Ks[2]=atof(strsep(&strline,delim));
- }
- if(!strcmp(tmpType[0], "illum"))
- {
- mtlPtr->illum=atoi(strsep(&strline,delim));
- }
- if(!strcmp(tmpType[0], "Tr") || !strcmp(tmpType[0], "d"))
- {
- mtlPtr->Ka[4]=mtlPtr->Kd[4]=mtlPtr->Ks[4]=atof(strsep(&strline,delim));
- }
- if(!strcmp(tmpType[0], "Ns"))
- {
- mtlPtr->Ns[0]=atof(strsep(&strline,delim));
- }
- if(!strcmp(tmpType[0], "map_Ka") || !strcmp(tmpType[0], "map_Kd") || !strcmp(tmpType[0], "map_Ks")) /* XXX map_Kd and map_Ks */
- {
- mtlPtr->tex=malloc(sizeof(CompTexture));
-
- tmpType[1]=strsep(&strline,delim);
-
- if(mtlPtr->tex == NULL)
- {
- compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "CompTexture is not malloced properly");
- }
- else if(!readImageToTexture (obj->s, mtlPtr->tex, /*texture pointer*/
- (tmpName=findPath(getStringOptionNamed (option, nOption, "file", ""),tmpType[1])), /*file*/
- &(mtlPtr->width), /*pointer for width*/
- &(mtlPtr->height)) ) /*pointer for height*/
- {
- compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "Failed to load image");
-
- finiTexture(obj->s, mtlPtr->tex);
- mtlPtr->tex=NULL;
- }
- if(tmpName != NULL) free(tmpName);
- tmpName = NULL;
- }
- //free(strline);
- strline=NULL;
- }
+ }
+ if (tmpType[0]==NULL) break;
+ //printf (tmpType[0]);
+ //printf ("\n");
+
+ tmpPtr[0]=strsep(&tmpType[0], "/");
+ if (tmpPtr[0]!=NULL) {
+ //printf (tmpPtr[0]);
+ //printf("\n");
+
+ printf ("v: %d, ", atoi(tmpPtr[0]));
+ modelData->indices[nIndices] = atoi(tmpPtr[0])-1;
+ nIndices++;
+ polyCount++;
+ }
+ else break;
-
- fclose(fp);
- }
+ tmpPtr[0]=strsep(&tmpType[0], "/");
+ if (tmpPtr[0]!=NULL) {
+ if (strcmp(tmpPtr[0], "")) {
+ printf ("t: %d, ", atoi(tmpPtr[0]));
+ }
+ }
+ else continue;
-
- mtlPtr= modelData->head;
-
- int vertNum=0;
- int textNum=0;
- int normNum=0;
- int polyCount;
- char **tempInfo; /* used to check for realloc */
- char **tmpPtr; /* used to chec value of vertex/texture/normal numbers */
+ tmpPtr[0]=strsep(&tmpType[0], "/");
+ if (tmpPtr[0]!=NULL && strcmp(tmpPtr[0], "")) {
+ printf ("n: %d, ", atoi(tmpPtr[0]));
+ memcpy(modelData->reorderedNormal[0][modelData->indices[nIndices-1]].r,
+ modelData->normal[0][atoi(tmpPtr[0])-1].r,
+ 3*sizeof(float));
+ usingNormal = TRUE;
+ }
+
+ }
+ printf ("\n");
+ if (polyCount!=0 && (polyCount!=oldPolyCount || usingNormal!=oldUsingNormal)) {
+ oldPolyCount = polyCount;
+ oldUsingNormal = usingNormal;
+ nGroups++;
- /* Final (third) pass - use data */
-
- fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
- if(fp == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Failed to open model file");
- return FALSE;
- }
+ printf ("polyCount = %d\n", polyCount);
+ printf ("usingNormal = %d\n", usingNormal);
+ modelData->group = (groupIndices *) realloc (modelData->group, (nGroups)*sizeof(groupIndices));
+ modelData->group[nGroups-1].polyCount = polyCount;
+ modelData->group[nGroups-1].startV = nIndices-polyCount;
+
+ if (nGroups>1)
+ modelData->group[nGroups-2].numV = nIndices-polyCount-modelData->group[nGroups-2].startV;
+
+ modelData->group[nGroups-1].normal = usingNormal;
+ }
+
+
+ if (tmpPtr)
+ free (tmpPtr);
+ }
+ }
+
+ if (nGroups!=0)
+ modelData->group[nGroups-1].numV = nIndices-modelData->group[nGroups-1].startV;
+
+
+ fclose(fp);
+
+ modelData->dList=glGenLists(1);
+ glNewList(modelData->dList, GL_COMPILE);
+
+ glRotatef(180.0, 0.0, 1.0, 0.0);
+ glRotatef(90.0, 0.0, 1.0, 0.0);
+ glDisable(GL_CULL_FACE);
+ glEnable(GL_RESCALE_NORMAL);
+ glEnable(GL_DEPTH_TEST);
+
+ glEnable (GL_COLOR_MATERIAL);
+ glColor4f (1, 1, 1, 1);
+
+ modelData->nGroups = nGroups;
+
+ for (i=0; i<nGroups; i++)
+ {
+ if (modelData->group[i].polyCount<2) continue;
- modelData->dList=glGenLists (1);
- glNewList (modelData->dList, GL_COMPILE);
-
- while((strline=getLine(fp))!=NULL)
- {
- tmpType = malloc ( sizeof(char *));
- if(tmpType==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "out of mem");
- //free(tmpType);
- //free(strline);
- return FALSE;
- }
-
- tmpType[0]=strsep(&strline,delim);
- if(tmpType[0] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
- if(!strcmp(tmpType[0], "f"))
- {
- glBegin(GL_POLYGON);
-
- polyCount=1;
-
- while(1)
- {
- tempInfo=realloc (tmpType, sizeof(char *) * (polyCount +1));
- if(tempInfo==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "out of mem");
- //free(tmpType);
- //free(strline);
- return FALSE;
- }
- tmpType=tempInfo;
-
- tmpType[polyCount]=strsep(&strline,delim);
- if(tmpType[polyCount] == NULL)
- {
- //free(strline);
- break;
- }
-
- tmpPtr=malloc( sizeof(char *) * 3);
-
- tmpPtr[0]=strsep(&tmpType[polyCount],"/");
- if(tmpPtr[0]==NULL) break;
- vertNum=atoi(tmpPtr[0]);
-
- tmpPtr[1]=strsep(&tmpType[polyCount],"/");
- if(tmpPtr[1]==NULL) tmpPtr[1]="0";
- textNum=atoi(tmpPtr[1]);
-
- tmpPtr[2]=strsep(&tmpType[polyCount],"/");
- if(tmpPtr[2]==NULL) tmpPtr[2]="0";
- normNum=atoi(tmpPtr[2]);
-
-
-
- if(mtlPtr->tex != NULL)
- {
- glTexCoord2f (COMP_TEX_COORD_X (&(mtlPtr->tex->matrix), (mtlPtr->width-1) * (texture[textNum-1].x)),
- COMP_TEX_COORD_Y (&(mtlPtr->tex->matrix), (mtlPtr->height-1) * (1-texture[textNum-1].y) ));
- }
- if(nNormal) glNormal3f (normal[normNum-1].x,normal[normNum-1].y,normal[normNum-1].z);
- glVertex3f (vertex[vertNum-1].x,vertex[vertNum-1].y,vertex[vertNum-1].z);
-
- polyCount++ ;
- }
-
- glEnd();
- }
-
- if(!strcmp(tmpType[0], "usemtl"))
- {
- tempInfo=realloc (tmpType, sizeof(char *) * 2);
- if(tempInfo==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "out of mem");
- //free(tmpType);
- //free(strline);
- return FALSE;
- }
- tmpType=tempInfo;
- tmpType[1]=strsep(&strline,delim);
- if(tmpType[1]== NULL)
- {
- //free(strline);
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "no mtl specified");
- return FALSE;
- }
-
- if(mtlPtr->tex != NULL) disableTexture (obj->s, mtlPtr->tex);
-
- for(mtlPtr=modelData->head; ; mtlPtr=mtlPtr->next)
- {
- if(mtlPtr==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "couldn't find material: %s", tmpType[1]);
- mtlPtr=modelData->head;
- break;
- }
-
- if(!strcmp(tmpType[1], mtlPtr->name))
- {
- if(mtlPtr->tex != NULL)
- {
- glEnable(mtlPtr->tex->target);
- enableTexture(obj->s, mtlPtr->tex,
- COMP_TEXTURE_FILTER_GOOD);
- }
- if(mtlPtr->illum==0) glDisable(GL_LIGHTING);
- else
- {
- glEnable(GL_LIGHTING);
- glMaterialfv(GL_FRONT_AND_BACK,
- GL_AMBIENT,
- mtlPtr->Ka);
-
- glMaterialfv(GL_FRONT_AND_BACK,
- GL_DIFFUSE,
- mtlPtr->Kd);
-
- glMaterialfv(GL_FRONT_AND_BACK,
- GL_SPECULAR,
- mtlPtr->Ks);
-
- glMaterialfv(GL_FRONT_AND_BACK,
- GL_SHININESS,
- mtlPtr->Ns);
- }
- break;
- }
- }
- }
- //free(strline);
- strline=NULL;
- }
+ printf ("modelData->group->startV = %d\n",modelData->group[i].startV);
+ printf ("modelData->group->numV = %d\n",modelData->group[i].numV);
+
+ float * v = (float *) modelData->vertex[0];
+ float * n = (float *) modelData->reorderedNormal[0];
+ GLenum cap = GL_QUADS;
+ if (modelData->group[i].polyCount==3) cap = GL_TRIANGLES;
+ if (modelData->group[i].polyCount==2) cap = GL_LINE_LOOP;
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer (3, GL_FLOAT, 0, v);
+ if (modelData->group[i].normal)
+ {
+ glEnableClientState(GL_NORMAL_ARRAY);
+ glNormalPointer (GL_FLOAT, 0, n);
+ }
+
+ if (modelData->group[i].polyCount<5)
+ glDrawElements(cap, modelData->group[i].numV, GL_UNSIGNED_INT,
+ modelData->indices + modelData->group[i].startV);
+ else {
+ int j=0;
+ for (j=0; j<modelData->group[i].numV/modelData->group[i].polyCount; j++) {
+ glDrawElements(GL_POLYGON, modelData->group[i].polyCount, GL_UNSIGNED_INT,
+ modelData->indices+modelData->group[i].startV+j*modelData->group[i].polyCount);
+ }
+ }
+ glDisableClientState (GL_NORMAL_ARRAY);
+
+ }
+
+ glEndList();
+
+ if (tmpType)
+ free(tmpType);
+
+ if (filename)
+ free (filename);
+
+
+
+ return TRUE;
+}
+static int
+addNumToString (char * s, unsigned int size, int offset, unsigned int x, unsigned int maxNumZeros)
+{
+ int c = 0;
+ int numZeros = 0;
+
+ int i = x;
+ while (i!=0) {
+ c++;
+ i/=10;
+ }
+
+ if (maxNumZeros>c)
+ numZeros = maxNumZeros - c;
+
+ if (offset+c+numZeros+1>size) {
+ size = offset+1+c+numZeros+4+1;
+ s = (char *) realloc(s, size*sizeof(char));
+ }
+
+ s[offset] = '_';
+
+ while (i<numZeros) {
+ s[offset+1+i] = '0';
+ i++;
+ }
- fclose (fp);
+ sprintf(s+offset+numZeros+1, "%d", x);
+
+ s[offset+1+c+numZeros] = '.';
+ s[offset+1+c+numZeros+1] = 'o';
+ s[offset+1+c+numZeros+2] = 'b';
+ s[offset+1+c+numZeros+3] = 'j';
+ s[offset+1+c+numZeros+4] = '\0';
- if(mtlPtr->tex != NULL)
- {
- disableTexture (obj->s, mtlPtr->tex);
- }
-
- glEndList();
- return TRUE;
+ return size;
}
static bool
cubedbusDeleteModelObject (CubedbusObject *obj)
{
CubedbusModelObject *data=obj->data;
- glDeleteLists (data->dList, 1);
+
+ glDeleteLists (data->dList, 1);
+
+ int fc;
+ for (fc=0; fc<data->fileCounter; fc++) {
+ if (data->vertex[fc])
+ free (data->vertex[fc]);
+ if (data->normal[fc])
+ free (data->normal[fc]);
+ if (data->texture[fc])
+ free (data->texture[fc]);
+ if (data->reorderedNormal[fc])
+ free (data->reorderedNormal[fc]);
+ if (data->mtllib[fc])
+ free (data->mtllib[fc]);
+ }
+ if (data->vertex)
+ free (data->vertex);
+ if (data->normal)
+ free (data->normal);
+ if (data->texture)
+ free (data->texture);
+ if (data->reorderedNormal)
+ free (data->reorderedNormal);
+ if (data->mtllib)
+ free (data->mtllib);
+
+ if (data->indices)
+ free (data->indices);
+ if (data->group)
+ free (data->group);
+
+ if (data->vertexBuffer)
+ free (data->vertexBuffer);
+ if (data->textureBuffer)
+ free (data->textureBuffer);
+ if (data->reorderedNormalBuffer)
+ free(data->reorderedNormalBuffer);
+
+
free(obj->data);
free(obj);
return TRUE;
@@ -526,41 +699,89 @@ cubedbusDeleteModelObject (CubedbusObject *obj)
static bool
cubedbusModifyModelObject (CubedbusObject *obj, CompOption *option, int nOption)
{
- CubedbusModelObject *modelData=obj->data;
-
- modelData->rotate[0]=getFloatOptionNamed (option,nOption, "rotateAngle", modelData->rotate[0]);
- modelData->rotate[1]=getFloatOptionNamed (option,nOption, "rotateX", modelData->rotate[1]);
- modelData->rotate[2]=getFloatOptionNamed (option,nOption, "rotateY", modelData->rotate[2]);
- modelData->rotate[3]=getFloatOptionNamed (option,nOption, "rotateZ",modelData->rotate[3]);
- modelData->translate[0]=getFloatOptionNamed (option,nOption, "translateX", modelData->translate[0]);
- modelData->translate[1]=getFloatOptionNamed (option,nOption, "translateY", modelData->translate[1]);
- modelData->translate[2]=getFloatOptionNamed (option,nOption, "translateZ", modelData->translate[2]);
- modelData->scale[0]=getFloatOptionNamed (option,nOption, "scaleX", modelData->scale[0]);
- modelData->scale[1]=getFloatOptionNamed (option,nOption, "scaleY", modelData->scale[1]);
- modelData->scale[2]=getFloatOptionNamed (option,nOption, "scaleZ", modelData->scale[2]);
- modelData->order=getBoolOptionNamed (option, nOption, "order", modelData->order);
- modelData->rotateSpeed=getFloatOptionNamed (option, nOption, "rotateSpeed", modelData->rotateSpeed);
-
return TRUE;
}
static bool
cubedbusDrawModelObject (CubedbusObject *obj)
{
-
CubedbusModelObject *data=obj->data;
- if(data->order) glTranslatef (data->translate[0], data->translate[1], data->translate[2]);
- glRotatef ( data->rotate[0], data->rotate[1], data->rotate[2], data->rotate[3]);
- if(!(data->order)) glTranslatef (data->translate[0], data->translate[1], data->translate[2]);
-
- glScalef( data->scale[0],data->scale[1], data->scale[2]); /* Rotate, translate and scale */
-
- glCallList(data->dList);
-
- data->rotate[0]+=data->rotateSpeed;
- if(data->rotate[0]>360) data->rotate[0]-=360;
- if(data->rotate[0]<0) data->rotate[0]+=360;
+ groupIndices * group;
+ int i, j;
+
+ if(data->order) glTranslatef (data->translate[0], data->translate[1], data->translate[2]);
+ glRotatef ( data->rotate[0], data->rotate[1], data->rotate[2], data->rotate[3]);
+ if(!(data->order)) glTranslatef (data->translate[0], data->translate[1], data->translate[2]);
+
+ glScalef( data->scale[0],data->scale[1], data->scale[2]); /* Rotate, translate and scale */
+
+ glDisable(GL_CULL_FACE);
+ glEnable(GL_RESCALE_NORMAL);
+ glEnable(GL_DEPTH_TEST);
+
+ glEnable (GL_COLOR_MATERIAL);
+ glColor4f (1, 1, 1, 1);
+
+ if (data->animation)
+ {
+ float t = fmodf(data->fps*timeSec, (float) data->fileCounter);
+ int ti = (int) t;
+ int ti2= (ti+1)%(data->fileCounter);
+ float dt = t-ti;
+
+ for (i=0; i<data->nVertex; i++)
+ {
+ for (j=0; j<3; j++)
+ {
+ data->vertexBuffer[i].r[j] = (1-dt)*(data->vertex[ti][i].r[j]) +
+ dt*(data->vertex[ti2][i].r[j]);
+ data->reorderedNormalBuffer[i].r[j] = (1-dt)*data->reorderedNormal[ti][i].r[j] +
+ dt*(data->reorderedNormal[ti2][i].r[j]);
+ }
+ }
+
+
+
+ for (i=0; i<data->nGroups; i++)
+ {
+ group = &(data->group[i]);
+ if (group->polyCount<2) continue;
+
+ float * v = (float *) data->vertexBuffer;
+ float * n = (float *) data->reorderedNormal[0];
+ GLenum cap = GL_QUADS;
+ if (group->polyCount==3) cap = GL_TRIANGLES;
+ if (group->polyCount==2) cap = GL_LINE_LOOP;
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer (3, GL_FLOAT, 0, v);
+ if (group->normal)
+ {
+ glEnableClientState(GL_NORMAL_ARRAY);
+ glNormalPointer (GL_FLOAT, 0, n);
+ }
+
+ if (data->group[i].polyCount<5)
+ glDrawElements(cap, group->numV, GL_UNSIGNED_INT,
+ data->indices + group->startV);
+ else {
+ for (j=0; j< group->numV/group->polyCount; j++) {
+ glDrawElements(GL_POLYGON, group->polyCount, GL_UNSIGNED_INT,
+ data->indices+group->startV+j*group->polyCount);
+ }
+ }
+ glDisableClientState (GL_NORMAL_ARRAY);
+ }
+ }
+ else
+ {
+ glCallList(data->dList);
+ }
+
+ data->rotate[0]+=data->rotateSpeed;
+ if(data->rotate[0]>360) data->rotate[0]=0;
+ if(data->rotate[0]<0) data->rotate[0]=360;
glDisable (GL_LIGHTING);