summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Bosveld <joel@JOEL-UBUNTU.(none)>2007-12-14 18:06:16 +0900
committerJoel Bosveld <joel@JOEL-UBUNTU.(none)>2007-12-14 18:06:16 +0900
commit949192754aac8b500a232812746c972acd02577b (patch)
tree548426c8035bc792691219233da6a3170807ab72
parent6f388b608e0026f703026880eb2f3c3e159f061e (diff)
downloadcubedbus-949192754aac8b500a232812746c972acd02577b.tar.gz
cubedbus-949192754aac8b500a232812746c972acd02577b.tar.bz2
Added support for MTL files (does not yet work properly)
-rw-r--r--cubedbusModel.c.in350
1 files changed, 277 insertions, 73 deletions
diff --git a/cubedbusModel.c.in b/cubedbusModel.c.in
index 1b9f936..c90be1f 100644
--- a/cubedbusModel.c.in
+++ b/cubedbusModel.c.in
@@ -1,6 +1,8 @@
#define CUBEDBUS_MODEL_OBJECT 5
+#include <errno.h>
typedef struct _CubedbusModelObject CubedbusModelObject;
+typedef struct _mtlStruct mtlStruct;
static bool cubedbusDrawModelObject (CubedbusObject *obj);
@@ -22,6 +24,24 @@ float y;
} vect2d;
+struct _mtlStruct
+{
+mtlStruct *next;
+
+char *name;
+GLfloat Ka[4];
+GLfloat Kd[4];
+GLfloat Ks[4];
+
+GLfloat Ns[1]; /* shininess */
+
+int illum;
+
+unsigned height, width;
+
+CompTexture *tex;
+};
+
struct _CubedbusModelObject
{
GLuint dList;
@@ -29,9 +49,7 @@ bool order;
float rotate[4], translate[3], scale[3];
float rotateSpeed;
-CompTexture *tex;
-
-unsigned height, width;
+mtlStruct *head;
};
@@ -57,7 +75,6 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
modelData->scale[2]=getFloatOptionNamed (option,nOption, "scaleZ", 1.0);
modelData->order=getBoolOptionNamed (option, nOption, "order", TRUE);
modelData->rotateSpeed=getFloatOptionNamed (option, nOption, "rotateSpeed", 0);
- modelData->tex=NULL;
int c;
char *strline=NULL;
@@ -68,8 +85,12 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
int nVertex=0;
int nNormal=0;
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 */
@@ -96,10 +117,11 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
strline[n]=c;
if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
{
+ strline[n]='\0';
tmpType=malloc(sizeof (char *) );
if(tmpType == NULL) return FALSE;
- tmpType[0]=strsep(&strline," ");
+ tmpType[0]=strsep(&strline,delim);
if(tmpType[0] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -118,7 +140,11 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if(!strcmp(tmpType[0], "vt"))
{
nTexture++;
- }
+ }
+ if(!strcmp(tmpType[0], "mtllib"))
+ {
+ while(strsep(&strline,delim) != NULL) nMtllib++;
+ }
/* prepare for next line */
// free(strline);
@@ -136,10 +162,12 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
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;
fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
@@ -163,9 +191,10 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
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," ");
+ tmpType[0]=strsep(&strline,delim);
if(tmpType[0] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -174,7 +203,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
if(!strcmp(tmpType[0], "v"))
{
- tmpType[1]=strsep(&strline," ");
+ tmpType[1]=strsep(&strline,delim);
if(tmpType[1] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -182,7 +211,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
- tmpType[2]=strsep(&strline," ");
+ tmpType[2]=strsep(&strline,delim);
if(tmpType[2] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -190,7 +219,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
- tmpType[3]=strsep(&strline," ");
+ tmpType[3]=strsep(&strline,delim);
if(tmpType[3] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -207,7 +236,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if(!strcmp(tmpType[0], "vn"))
{
- tmpType[1]=strsep(&strline," ");
+ tmpType[1]=strsep(&strline,delim);
if(tmpType[1] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -215,7 +244,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
- tmpType[2]=strsep(&strline," ");
+ tmpType[2]=strsep(&strline,delim);
if(tmpType[2] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -223,7 +252,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
- tmpType[3]=strsep(&strline," ");
+ tmpType[3]=strsep(&strline,delim);
if(tmpType[3] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -243,20 +272,24 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
texture[nTexture].x=0;
texture[nTexture].y=0;
- tmpType[1]=strsep(&strline," ");
+ tmpType[1]=strsep(&strline,delim);
if(tmpType[1] != NULL)
{
texture[nTexture].x=atof(tmpType[1]);
}
- tmpType[2]=strsep(&strline," ");
+ 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;
@@ -274,30 +307,156 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
int polyCount;
char *tempPtr;
char **tempInfo;
+ mtlStruct *mtlPtr;
+
+ modelData->head = malloc(sizeof (mtlStruct) );
- if(nTexture)
+ mtlPtr=modelData->head;
+
+ mtlPtr->illum = 0;
+ mtlPtr->next=NULL;
+ mtlPtr->name=malloc(sizeof(char) * 5);
+ strcpy (mtlPtr->name, "fake");
+
+
+
+ /* 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)
{
- modelData->tex=malloc(sizeof(CompTexture));
-
- if(modelData->tex == NULL)
- {
- compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "CompTexture is not malloced properly");
- }
- else if(!readImageToTexture (obj->s, modelData->tex, /*texture pointer*/
- getStringOptionNamed (option, nOption, "texture", ""), /*XXX*/ /*file*/
- &(modelData->width), /*pointer for width*/
- &(modelData->height)) ) /*pointer for height*/
+ 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, "Failed to load image");
-
- finiTexture(obj->s, modelData->tex);
- modelData->tex=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));
+
+ tmpType[1]=strsep(&strline,delim);
+
+ 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;
+ }
+
+ }
+ /* prepare for next line */
+ // free(strline);
+ n=0;
+ tmp=NULL;
+ continue;
+
+ }
+ n++;
+ }
+ fclose(mtl);
+ }
+
+ mtlPtr= modelData->head;
/* Final (third) pass - use data */
fp = fopen(getStringOptionNamed (option, nOption, "file", ""), "r");
@@ -325,7 +484,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
strline[n]=c;
if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
{
-
+ strline[n]='\0';
tmpType = malloc ( sizeof(char *));
if(tmpType==NULL)
{
@@ -334,7 +493,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
- tmpType[0]=strsep(&strline," ");
+ tmpType[0]=strsep(&strline,delim);
if(tmpType[0] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
@@ -358,24 +517,23 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
tmpType=tempInfo;
- tmpType[polyCount]=strsep(&strline," ");
+ tmpType[polyCount]=strsep(&strline,delim);
if(tmpType[polyCount] == NULL)
{
//free(tmpType);
break;
}
- tempPtr=strsep(&tmpType[polyCount],"/");
- if(tempPtr == NULL)
- {
- //free(tmpType);
- break;
- }
+ tempPtr=strsep(&tmpType[polyCount],"/");
+ if(tempPtr == NULL)
+ {
+ //free(tmpType);
+ break;
+ }
- vertNum=atoi(tempPtr);
+ vertNum=atoi(tempPtr);
- if(nTexture)
- {
+
tempPtr=strsep(&tmpType[polyCount],"/");
if(tempPtr == NULL)
{
@@ -384,10 +542,8 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
textNum=atoi(tempPtr);
- }
+
- if(nNormal)
- {
tempPtr=strsep(&tmpType[polyCount],"/");
if(tempPtr == NULL)
{
@@ -396,23 +552,83 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
normNum=atoi(tempPtr);
- }
+
- /* XXX also need to do normal and tex coord */
- if(modelData->tex != NULL)
+ if(mtlPtr->tex != NULL)
{
- glTexCoord2f (COMP_TEX_COORD_X (&(modelData->tex->matrix), (modelData->width-1) * (texture[textNum-1].x)),
- COMP_TEX_COORD_Y (&(modelData->tex->matrix), (modelData->height-1) * (1-texture[textNum-1].y) ));
+ 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);
+ 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)
+ {
+ //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
+ {
+ 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;
+ }
+ }
+ }
+
/* prepare for next line */
// free(strline) (cannot free lines that have been strsep'd?);
@@ -457,27 +673,15 @@ cubedbusDrawModelObject (CubedbusObject *obj)
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 */
-
- if(data->tex != NULL)
- {
- glEnable (data->tex->target);
-
- enableTexture (obj->s, data->tex,
- COMP_TEXTURE_FILTER_GOOD);
-
- }
-
- glDisable (GL_LIGHTING);
glCallList(data->dList);
-
- glDisable (GL_LIGHTING);
-
- if(data->tex != NULL) disableTexture (obj->s, data->tex);
-
+
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]<0) data->rotate[0]=360;
+
+ glDisable (GL_LIGHTING);
+
return TRUE;
}