summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Bosveld <joel@JOEL-UBUNTU.(none)>2007-12-15 08:21:58 +0900
committerJoel Bosveld <joel@JOEL-UBUNTU.(none)>2007-12-15 08:21:58 +0900
commit006ead3e85c22e01a3b565d9fec15970f1f9904f (patch)
tree0f0304193fc9c62a71a2cfae5f94781583e3f40f
parent949192754aac8b500a232812746c972acd02577b (diff)
downloadcubedbus-006ead3e85c22e01a3b565d9fec15970f1f9904f.tar.gz
cubedbus-006ead3e85c22e01a3b565d9fec15970f1f9904f.tar.bz2
Little bit of cleaning (doesn't yet work - locksup)
-rw-r--r--cubedbus.c1
-rw-r--r--cubedbusModel.c.in797
2 files changed, 348 insertions, 450 deletions
diff --git a/cubedbus.c b/cubedbus.c
index 74c77b1..1daadd1 100644
--- a/cubedbus.c
+++ b/cubedbus.c
@@ -516,6 +516,7 @@ cubedbusInitScreen (CompPlugin *p,
s->base.privates[gd->screenPrivateIndex].ptr = gs; /* For the CUBEDBUS_SCREEN macro to work */
+
/**/
gs->head= malloc (sizeof(CubedbusObject));
if(gs->head == NULL) return FALSE;
diff --git a/cubedbusModel.c.in b/cubedbusModel.c.in
index c90be1f..4898466 100644
--- a/cubedbusModel.c.in
+++ b/cubedbusModel.c.in
@@ -10,6 +10,8 @@ static bool cubedbusDeleteModelObject (CubedbusObject *obj);
static bool cubedbusModifyModelObject (CubedbusObject *obj, CompOption *option, int nOption);
static bool cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption);
+char *getLine (FILE *fp);
+
typedef struct _vect3d
{
float x;
@@ -76,10 +78,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
modelData->order=getBoolOptionNamed (option, nOption, "order", TRUE);
modelData->rotateSpeed=getFloatOptionNamed (option, nOption, "rotateSpeed", 0);
- int c;
char *strline=NULL;
- int n=0;
- char *tmp=NULL;
char **tmpType;
int nVertex=0;
@@ -87,10 +86,6 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
int nTexture=0;
int nMtllib=0;
- char delim[3];
- delim[0]=' ';
- delim[1]='\n';
- delim[2]='\0';
/* First pass - count how much data we need to store */
@@ -101,59 +96,35 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
-
-
- while( (c=getc(fp) ) != EOF )
- {
- tmp=realloc(tmp, sizeof(char)*(n+1));
- if(tmp == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file - out of mem");
+ while((strline=getLine(fp))!=NULL)
+ {
+ tmpType = malloc ( sizeof (char *) * 2);
+ tmpType[0]=strsep(&strline," ");
+ if(tmpType[0] == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
free(strline);
return FALSE;
- }
- strline=tmp; /* The strline array should now be big enough to hold all characters read so far, plus the one being read */
-
- strline[n]=c;
- if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
+ }
+
+ 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)
{
- strline[n]='\0';
- tmpType=malloc(sizeof (char *) );
- if(tmpType == NULL) 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], "v"))
- {
- nVertex++;
- }
- if(!strcmp(tmpType[0], "vn"))
- {
- nNormal++;
- }
- if(!strcmp(tmpType[0], "vt"))
- {
- nTexture++;
- }
- if(!strcmp(tmpType[0], "mtllib"))
- {
- while(strsep(&strline,delim) != NULL) nMtllib++;
- }
-
- /* prepare for next line */
- // free(strline);
- n=0;
- tmp=NULL;
- continue;
+ tmpType[1]=strsep(&strline," ");
+ if(tmpType[1]==NULL) break;
+ if(!strcmp(tmpType[1],"")) continue;
+ nMtllib++;
}
- n++;
- }
+ }
+ free(strline);
+ strline=NULL;
+ }
+
fclose (fp);
@@ -170,147 +141,75 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
nMtllib=0;
fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
-
if(fp == NULL)
- {
+ {
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Failed to open model file");
return FALSE;
- }
-
- while( (c=getc(fp) ) != EOF )
- {
- tmp=realloc(tmp, sizeof(char)*(n+1));
- if(tmp == NULL)
+ }
+ while((strline=getLine(fp))!=NULL)
+ {
+ tmpType = malloc ( sizeof (char *));
+ tmpType[0]=strsep(&strline," ");
+ if(tmpType[0] == NULL)
{
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file - out of mem");
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
free(strline);
return FALSE;
}
- strline=tmp; /* The strline array should now be big enough to hold all characters read so far, plus the one being read */
-
- strline[n]=c;
- if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
+
+ if(!strcmp(tmpType[0], "v"))
{
- strline[n]='\0';
- /* 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], "v"))
- {
- tmpType[1]=strsep(&strline,delim);
- if(tmpType[1] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- tmpType[2]=strsep(&strline,delim);
- if(tmpType[2] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- tmpType[3]=strsep(&strline,delim);
- if(tmpType[3] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- vertex[nVertex].x=atof(tmpType[1]);
- vertex[nVertex].y=atof(tmpType[2]);
- vertex[nVertex].z=atof(tmpType[3]);
-
- nVertex++;
- }
-
- if(!strcmp(tmpType[0], "vn"))
- {
- tmpType[1]=strsep(&strline,delim);
- if(tmpType[1] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- tmpType[2]=strsep(&strline,delim);
- if(tmpType[2] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- tmpType[3]=strsep(&strline,delim);
- if(tmpType[3] == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- //free(strline);
- return FALSE;
- }
-
- normal[nNormal].x=atof(tmpType[1]);
- normal[nNormal].y=atof(tmpType[2]);
- normal[nNormal].z=atof(tmpType[3]);
-
- nNormal++;
- }
-
- if(!strcmp(tmpType[0], "vt"))
- {
- texture[nTexture].x=0;
- texture[nTexture].y=0;
-
- tmpType[1]=strsep(&strline,delim);
- if(tmpType[1] != NULL)
- {
- texture[nTexture].x=atof(tmpType[1]);
- }
-
- tmpType[2]=strsep(&strline,delim);
- if(tmpType[2] != NULL)
- {
- texture[nTexture].y=atof(tmpType[2]);
- }
-
- nTexture++;
- }
- if(!strcmp(tmpType[0], "mtllib"))
- {
- while( (mtllib[nMtllib]=strsep(&strline,delim)) != NULL) nMtllib++;
- }
- /* prepare for next line */
- // free(strline);
- n=0;
- tmp=NULL;
- continue;
+ vertex[nVertex].x=atof(strsep(&strline," "));
+ vertex[nVertex].y=atof(strsep(&strline," "));
+ vertex[nVertex].z=atof(strsep(&strline," "));
+
+ nVertex++;
}
- n++;
- }
+
+ if(!strcmp(tmpType[0], "vn"))
+ {
+ normal[nNormal].x=atof(strsep(&strline," "));
+ normal[nNormal].y=atof(strsep(&strline," "));
+ normal[nNormal].z=atof(strsep(&strline," "));
+
+ nNormal++;
+ }
+
+ if(!strcmp(tmpType[0], "vt"))
+ {
+ texture[nTexture].x=atof(strsep(&strline," "));
+ texture[nTexture].y=atof(strsep(&strline," "));
+
+ nTexture++;
+ }
+
+ if(!strcmp(tmpType[0], "mtllib"))
+ {
+ while(1)
+ {
+ mtllib[nMtllib]=strsep(&strline," ");
+ if(mtllib[nMtllib] == NULL) break;
+ if(!strcmp(mtllib[nMtllib],"")) continue; /* if blank override next time */
+ nMtllib++;
+ }
+ }
+ free(strline);
+ strline=NULL;
+ }
+
fclose (fp);
- int vertNum=0;
- int textNum=0;
- int normNum=0;
- int polyCount;
- char *tempPtr;
- char **tempInfo;
+
mtlStruct *mtlPtr;
modelData->head = malloc(sizeof (mtlStruct) );
-
+ if(modelData->head == NULL)
+ {
+ //free
+ return FALSE;
+ }
+
mtlPtr=modelData->head;
mtlPtr->illum = 0;
@@ -322,141 +221,128 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
/* XXX Parse MTL file and load textures XXX */
- FILE *mtl = NULL;
-
int mtlCount;
for(mtlCount=0;mtllib[mtlCount] != NULL;mtlCount++)
{
-
- mtl=fopen(mtllib[mtlCount], "r"); /* XXX should get path as well XXX */
- if(mtl == NULL)
+ fp=fopen(mtllib[mtlCount], "r"); /* XXX should get path as well XXX */
+ if(fp == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "mtl: can't open %s: %s",
mtllib[mtlCount], strerror(errno));
return FALSE;
- }
- tmp=NULL;
- n=0;
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Parsing file: %s",mtllib[mtlCount] );
- while( (c=getc(mtl) ) != EOF )
- {
- tmp=realloc(tmp, sizeof(char)*(n+1));
- if(tmp == NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file - out of mem");
- free(strline);
- return FALSE;
- }
- strline=tmp; /* The strline array should now be big enough to hold all characters read so far, plus the one being read */
-
- strline[n]=c;
- if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
- {
- strline[n]='\0';
- /* 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")) /* XXX map_Kd and map_Ks */
- {
- mtlPtr->tex=malloc(sizeof(CompTexture));
+ }
+
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Parsing file: %s",mtllib[mtlCount] );
- tmpType[1]=strsep(&strline,delim);
-
- compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "%s", tmpType[1]);
-
- if(mtlPtr->tex == NULL)
+ while((strline=getLine(fp))!=NULL)
+ {
+ /* code to move stuff to array */
+ tmpType = malloc ( sizeof (char *) * 4);
+ tmpType[0]=strsep(&strline," ");
+
+ 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," ");
+ if(tmpType[1] == NULL)
{
- compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "CompTexture is not malloced properly");
- }
- else if(!readImageToTexture (obj->s, mtlPtr->tex, /*texture pointer*/
- tmpType[1], /*XXX*/ /*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;
- }
-
- }
- /* prepare for next line */
- // free(strline);
- n=0;
- tmp=NULL;
- continue;
+ 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," "));
+ mtlPtr->Ka[1]=atof(strsep(&strline," "));
+ mtlPtr->Ka[2]=atof(strsep(&strline," "));
+ }
+ if(!strcmp(tmpType[0], "Kd"))
+ {
+ mtlPtr->Kd[0]=atof(strsep(&strline," "));
+ mtlPtr->Kd[1]=atof(strsep(&strline," "));
+ mtlPtr->Kd[2]=atof(strsep(&strline," "));
+ }
+ if(!strcmp(tmpType[0], "Ks"))
+ {
+ mtlPtr->Ks[0]=atof(strsep(&strline," "));
+ mtlPtr->Ks[1]=atof(strsep(&strline," "));
+ mtlPtr->Ks[2]=atof(strsep(&strline," "));
+ }
+ if(!strcmp(tmpType[0], "illum"))
+ {
+ mtlPtr->illum=atoi(strsep(&strline," "));
+ }
+ if(!strcmp(tmpType[0], "Tr") || !strcmp(tmpType[0], "d"))
+ {
+ mtlPtr->Ka[4]=mtlPtr->Kd[4]=mtlPtr->Ks[4]=atof(strsep(&strline," "));
+ }
+ if(!strcmp(tmpType[0], "Ns"))
+ {
+ mtlPtr->Ns[0]=atof(strsep(&strline," "));
+ }
+ if(!strcmp(tmpType[0], "map_Ka")) /* XXX map_Kd and map_Ks */
+ {
+ mtlPtr->tex=malloc(sizeof(CompTexture));
- }
- n++;
- }
- fclose(mtl);
- }
+ tmpType[1]=strsep(&strline," ");
+
+ compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "%s", tmpType[1]);
+
+ if(mtlPtr->tex == NULL)
+ {
+ compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "CompTexture is not malloced properly");
+ }
+ else if(!readImageToTexture (obj->s, mtlPtr->tex, /*texture pointer*/
+ tmpType[1], /*XXX*/ /*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;
+ }
+ }
+ free(strline);
+ strline=NULL;
+ }
+
+
+ fclose(fp);
+ }
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 */
+
/* Final (third) pass - use data */
fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
@@ -470,174 +356,145 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
modelData->dList=glGenLists (1);
glNewList (modelData->dList, GL_COMPILE);
- while( (c=getc(fp) ) != EOF )
+ while((strline=getLine(fp))!=NULL)
{
- tmp=realloc(tmp, sizeof(char)*(n+1));
- if(tmp == 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," ");
+ if(tmpType[0] == NULL)
{
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file - out of mem");
- free(strline);
- return FALSE;
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
+ free(strline);
+ return FALSE;
}
- strline=tmp; /* The strline array should now be big enough to hold all characters read so far, plus the one being read */
-
- strline[n]=c;
- if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
+ if(!strcmp(tmpType[0], "f"))
{
- strline[n]='\0';
- tmpType = malloc ( sizeof(char *));
- if(tmpType==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "out of mem");
- free(tmpType);
- 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);
- return FALSE;
- }
- tmpType=tempInfo;
-
- tmpType[polyCount]=strsep(&strline,delim);
- if(tmpType[polyCount] == NULL)
- {
- //free(tmpType);
- break;
- }
-
- tempPtr=strsep(&tmpType[polyCount],"/");
- if(tempPtr == NULL)
- {
- //free(tmpType);
- break;
- }
-
- vertNum=atoi(tempPtr);
-
-
- tempPtr=strsep(&tmpType[polyCount],"/");
- if(tempPtr == NULL)
- {
- //free(tmpType);
- break;
- }
-
- textNum=atoi(tempPtr);
-
-
- tempPtr=strsep(&tmpType[polyCount],"/");
- if(tempPtr == NULL)
- {
- //free(tmpType);
- break;
- }
-
- normNum=atoi(tempPtr);
-
-
- 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"))
+ glBegin(GL_POLYGON);
+
+ polyCount=1;
+
+ while(1)
{
- tempInfo=realloc (tmpType, sizeof(char *) * 2);
+ 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[1]=strsep(&strline,delim);
- if(tmpType[1]== NULL)
- {
- //free(tmpType);
- 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)
+ tmpType[polyCount]=strsep(&strline," ");
+ if(tmpType[polyCount] == NULL)
{
- //compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "couldn't find material"); XXX
- mtlPtr=modelData->head;
+ free(strline);
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
- {
- 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;
}
- }
- }
+ 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," ");
+ 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);
- /* prepare for next line */
- // free(strline) (cannot free lines that have been strsep'd?);
- n=0;
- tmp=NULL;
- continue;
+ for(mtlPtr=modelData->head;; mtlPtr=mtlPtr->next)
+ {
+ if(mtlPtr==NULL)
+ {
+ //compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "couldn't find material"); XXX
+ 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;
+ }
+ }
}
- n++;
+ free(strline);
+ strline=NULL;
}
+
fclose (fp);
@@ -685,3 +542,43 @@ cubedbusDrawModelObject (CubedbusObject *obj)
return TRUE;
}
+/* Here after lies my functions */
+
+/*************************************
+* returns string with line from file *
+* NOTE: you must free line after use *
+* NOTE: returns NULL if error occurs *
+*************************************/
+
+char *
+getLine (FILE *fp)
+{
+ char *tmp=NULL;
+ char *strline=NULL;
+
+ int c;
+ int n=0;
+
+ while( (c=getc(fp) ) != EOF )
+ {
+ tmp=realloc(tmp, sizeof(char)*(n+1));
+ if(tmp == NULL)
+ {
+ compLogMessage(NULL, "cubedbus", CompLogLevelWarn, "Error reading file - out of mem");
+ free(strline);
+ return NULL;
+ }
+ strline=tmp; /* The strline array should now be big enough to hold all characters read so far, plus the one being read */
+
+ strline[n]=c;
+ if(c=='\n' || c=='\0') /* the line is ended - return */
+ {
+ strline[n]='\0';
+ return strline;
+ }
+ n++;
+ }
+
+ return NULL;
+}
+