summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Mikos <david@FAMILYCOMPUTER.(none)>2008-02-16 23:48:03 +1030
committerDavid Mikos <david@FAMILYCOMPUTER.(none)>2008-02-16 23:48:03 +1030
commit57816c4ddc1c993db786d2cadb492c327d4e9b5c (patch)
treefe6be227454566102781a9787e7360caa49ef9b4
parent76dd910e9cacd62899348ebc927b734ef7e061a9 (diff)
downloadcubedbus-57816c4ddc1c993db786d2cadb492c327d4e9b5c.tar.gz
cubedbus-57816c4ddc1c993db786d2cadb492c327d4e9b5c.tar.bz2
More settings for obj loader. Animation off by default.
-rw-r--r--cubedbus.c23
-rw-r--r--cubedbusModel.c.in1215
-rw-r--r--scripts/animatedModel.sh3
3 files changed, 886 insertions, 355 deletions
diff --git a/cubedbus.c b/cubedbus.c
index fc63b2c..849ed8e 100644
--- a/cubedbus.c
+++ b/cubedbus.c
@@ -347,7 +347,11 @@ cubedbusPaintInside (CompScreen *s,
CUBEDBUS_SCREEN (s);
CUBE_SCREEN (s);
- Bool enabled;
+ static const float lmodel_ambient[] = { 0.4, 0.4, 0.4, 0.4 };
+ static const float lmodel_localviewer[] = { 0.0 };
+
+
+ Bool enabled;
ScreenPaintAttrib sA = *sAttrib;
@@ -366,6 +370,8 @@ cubedbusPaintInside (CompScreen *s,
glPushAttrib (GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT);
+ glEnable (GL_BLEND);
+
enabled = glIsEnabled (GL_CULL_FACE);
glDisable(GL_CULL_FACE);
@@ -373,10 +379,21 @@ cubedbusPaintInside (CompScreen *s,
glPushMatrix();
- glDisable (GL_LIGHTING);
- glDisable (GL_COLOR_MATERIAL);
+
+ glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
+ glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
+
+ glEnable (GL_NORMALIZE);
+ glEnable (GL_COLOR_MATERIAL);
glEnable (GL_DEPTH_TEST);
+ glEnable (GL_LIGHTING);
+ glEnable (GL_LIGHT1);
+ glDisable (GL_LIGHT0);
+
glColor4usv (defaultColor);
+
+ glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+
/*********************************************/
CubedbusObject *obj=gs->head;
diff --git a/cubedbusModel.c.in b/cubedbusModel.c.in
index 05d9f43..3e5ab06 100644
--- a/cubedbusModel.c.in
+++ b/cubedbusModel.c.in
@@ -1,3 +1,7 @@
+/*
+ * animation patch by David Mikos
+ */
+
#define CUBEDBUS_MODEL_OBJECT 5
#include <errno.h>
@@ -9,13 +13,16 @@ static bool cubedbusDrawModelObject (CubedbusObject *obj);
static bool cubedbusDeleteModelObject (CubedbusObject *obj);
static bool cubedbusModifyModelObject (CubedbusObject *obj, CompOption *option, int nOption);
static bool cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption);
-
+static bool cubedbusDrawVBOModel (CubedbusObject *obj, float *vertex, float *normal);
static int
addNumToString (char *, unsigned int, int, unsigned int, unsigned int);
+static int
+addVertex (unsigned int **indices, int nUniqueIndices, int iTexture, int iNormal);
+
-static char *getLine (FILE *fp);
+static char *getLine (FILE *fp, char **oldStrline);
static char *findPath (char *object, char *file);
typedef struct _vect3d
@@ -31,6 +38,7 @@ typedef struct _vect2d
typedef struct _groupIndices
{
int polyCount;
+ int complexity;
int startV;
int numV;
@@ -39,20 +47,20 @@ typedef struct _groupIndices
int startN;
int numN;
+ int materialIndex; //negative for no new material
+
Bool texture;
Bool normal;
} groupIndices;
struct _mtlStruct
{
-mtlStruct *next;
-
-char *name;
GLfloat Ka[4];
GLfloat Kd[4];
GLfloat Ks[4];
GLfloat Ns[1]; /* shininess */
+GLfloat Ni[1]; /* optical_density - currently not used*/
int illum;
@@ -66,28 +74,32 @@ struct _CubedbusModelObject
GLuint dList;
bool order;
float rotate[4], translate[3], scale[3];
-float rotateSpeed;
+float rotateSpeed, scaleGlobal;
+
+float color[4];
int fileCounter;
Bool animation;
int fps;
-vect3d **vertex;
-vect3d **normal;
-vect2d **texture;
-vect3d **reorderedNormal;
-char ***mtllib;
+vect3d **reorderedVertex; //not used yet
+vect3d **reorderedNormal; //to be replaced
+vect3d **reorderedTexture; //not used yet
+
unsigned int * indices;
groupIndices *group;
-vect3d *vertexBuffer;
-vect2d *textureBuffer;
+vect3d *reorderedVertexBuffer;
+vect2d *reorderedTextureBuffer;
vect3d *reorderedNormalBuffer;
int nVertex;
+int nTexture;
+int nNormal;
int nGroups;
+int nIndices;
-mtlStruct *head;
+mtlStruct **material;
};
@@ -107,60 +119,87 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
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->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->scaleGlobal=getFloatOptionNamed(option, nOption, "scale", 1.0);
+
modelData->order=getBoolOptionNamed(option, nOption, "order", TRUE);
- modelData->rotateSpeed=getFloatOptionNamed(option, nOption, "rotateSpeed",
- 0);
+ modelData->rotateSpeed=getFloatOptionNamed(option, nOption, "rotateSpeed", 0);
+ modelData->animation = getBoolOptionNamed(option, nOption, "animation", FALSE);
+ modelData->fps = getIntOptionNamed(option, nOption, "fps", 5);
+
+ modelData->color[0] = getFloatOptionNamed(option, nOption, "r", 1);
+ modelData->color[1] = getFloatOptionNamed(option, nOption, "g", 1);
+ modelData->color[2] = getFloatOptionNamed(option, nOption, "b", 1);
+ modelData->color[3] = getFloatOptionNamed(option, nOption, "a", 1);
+
+
+ if ( compFindOption(option, nOption, "r", NULL) == NULL ||
+ compFindOption(option, nOption, "g", NULL) == NULL ||
+ compFindOption(option, nOption, "b", NULL) == NULL )
+ {
+ modelData->color[0] = 1;
+ modelData->color[1] = 1;
+ modelData->color[2] = 1;
+ }
- int i;
- char *strline=NULL;
- char **tmpType;
+ int i, j;
+
+ char *strline = NULL;
+ char *oldStrline = NULL;
+ char *tmpType;
int nVertex=0;
int nNormal=0;
int nTexture=0;
- int nMtllib=0;
int nIndices=0;
+ int nUniqueIndices = 0;
+ int nMaterial=0;
+ int *nMtllib;
+
+ char ***mtllib;
+ unsigned int **tmpIndices = NULL; /* reorder indices per vertex (store alternating corresponding textures/normals) */
+ vect3d *vertex = NULL;
+ vect3d *normal = NULL;
+ vect2d *texture = NULL;
+
char delim[]= { ' ', '\t', '\0' };
- Bool animation = TRUE;
- modelData->animation = animation;
- modelData->fps = 3;
+ Bool animation = modelData->animation;
- float scaleFactor = 0.2f;
-
- int maxNumZeros = 6; //number of zeros in FILENAME00000.obj
+ int maxNumZeros = getIntOptionNamed(option, nOption, "numZeros", 0); /* number of zeros in FILENAME_00000.obj */
- int startFileNum = 1;
+ int startFileNum = getIntOptionNamed(option, nOption, "start", 0); /* first file in animation */
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')
+
+ if (lenBaseFilename>3 && animation)
{
- lenBaseFilename-=4;
+ if (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"
+ size+=maxNumZeros+1+4; /* numbers + '_' + ".obj" */
char * filename = malloc (size*sizeof(char));
@@ -169,13 +208,33 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
filename[lenBaseFilename]='\0';
+ /* if no animation and no .obj specified, add file extension */
+
+ if (lenBaseFilename>3 && !animation)
+ {
+ if (filename[lenBaseFilename-4]!='.' ||
+ filename[lenBaseFilename-3]!='o' ||
+ filename[lenBaseFilename-2]!='b' ||
+ filename[lenBaseFilename-1]!='j')
+ {
+ lenBaseFilename+=4;
+ filename = realloc (filename, (lenBaseFilename+1)*sizeof(char));
+ filename[lenBaseFilename-4]='.';
+ filename[lenBaseFilename-3]='o';
+ filename[lenBaseFilename-2]='b';
+ filename[lenBaseFilename-1]='j';
+ filename[lenBaseFilename] ='\0';
+ }
+ }
+
+
int fileCounter = 0;
/* verify existence of files and/or check for animation frames */
- FILE *fp;
+ FILE *fp, *mtlfp;
do {
if (animation)
@@ -204,13 +263,13 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
}
- 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->reorderedVertex = malloc(sizeof(vect3d*)*fileCounter);
+ modelData->reorderedTexture = malloc(sizeof(vect2d*)*fileCounter);
+ modelData->reorderedNormal = malloc(sizeof(vect3d*)*fileCounter);
- modelData->mtllib = malloc(sizeof(char **)*fileCounter);
+ modelData->material = malloc(sizeof(mtlStruct*)*fileCounter);
+ mtllib = malloc(sizeof(char **)*fileCounter);
+ nMtllib = malloc (sizeof(int)*fileCounter);
modelData->indices = NULL;
modelData->group = NULL;
@@ -219,84 +278,87 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
int oldPolyCount = 0;
Bool oldUsingNormal = FALSE;
+ Bool oldUsingTexture = FALSE;
int fc;
for (fc = 0; fc<fileCounter; fc++) {
-
+ nMtllib[fc] = 0;
+
+
/* First pass - count how much data we need to store */
- size = addNumToString (filename, size, lenBaseFilename, startFileNum+fc, maxNumZeros);
+ if (animation)
+ 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");
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ filename);
+
return FALSE;
}
- tmpType = malloc(sizeof(char *) * 2);
- while ((strline=getLine(fp))!=NULL)
+ while ((strline=getLine(fp, &oldStrline))!=NULL)
{
- if (tmpType==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
- "out of mem");
- return FALSE;
- }
-
- tmpType[0]=strsep(&strline, delim);
+ tmpType=strsep(&strline, delim);
if (strline==NULL) continue;
- if (tmpType[0] == NULL)
+ if (tmpType == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
"Error reading file...");
//needs extra freeing here
if (filename)
free (filename);
+ if (oldStrline)
+ free (oldStrline);
return FALSE;
}
while (strline!=NULL) {
- if (!strcmp(tmpType[0], "")) {
- tmpType[0]=strsep(&strline, delim);
- }
- else break;
-
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
+ }
+ else break;
}
- if (strline==NULL || tmpType[0]==NULL) continue;
+ if (strline==NULL || tmpType==NULL) continue;
- if (!strcmp(tmpType[0], "v"))
+ if (!strcmp(tmpType, "v"))
nVertex++;
- if (!strcmp(tmpType[0], "vn"))
+ else if (!strcmp(tmpType, "vn"))
nNormal++;
- if (!strcmp(tmpType[0], "vt"))
+ else if (!strcmp(tmpType, "vt"))
nTexture++;
-
- if (!strcmp(tmpType[0], "mtllib"))
+ else if (!strcmp(tmpType, "f") || !strcmp(tmpType, "p") || !strcmp(tmpType, "l"))
+ {
+ while ((tmpType=strsep(&strline, delim))!=NULL) {
+ nIndices++;
+ }
+ }
+ else if (!strcmp(tmpType, "usemtl"))
+ {
+ nMaterial++;
+ }
+ else if (!strcmp(tmpType, "mtllib"))
{
while (1)
{
- tmpType[1]=strsep(&strline, delim);
- if (tmpType[1]==NULL)
+ tmpType=strsep(&strline, delim);
+ if (tmpType==NULL)
break;
- if (!strcmp(tmpType[1], ""))
+
+ if (!strcmp(tmpType, ""))
continue;
- nMtllib++;
- }
- }
- if (!strcmp(tmpType[0], "f"))
- {
- while ((tmpType[0]=strsep(&strline, delim))!=NULL) {
- nIndices++;
+
+ nMtllib[fc]++;
}
}
}
- //no need to free (strline) because getLine does that at the end with a realloc size 0
-
fclose(fp);
@@ -310,95 +372,108 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
- modelData->vertex[fc] = malloc(sizeof(vect3d)*nVertex);
- modelData->normal[fc] = malloc(sizeof(vect3d)*nNormal);
- modelData->texture[fc] = malloc(sizeof(vect2d)*nTexture);
+ vertex = realloc(vertex, sizeof(vect3d)*nVertex);
+ normal = realloc(normal, sizeof(vect3d)*nNormal);
+ texture = realloc(texture, sizeof(vect2d)*nTexture);
- modelData->reorderedNormal[fc] = malloc(sizeof(vect3d)*nVertex);
+ modelData->reorderedVertex[fc] = malloc(sizeof(vect3d)*nIndices);
+ modelData->reorderedTexture[fc] = malloc(sizeof(vect2d)*nIndices);
+ modelData->reorderedNormal[fc] = malloc(sizeof(vect3d)*nIndices);
- modelData->mtllib[fc] = malloc(sizeof(char *) * (nMtllib+1)); /* it will place a NULL pointer in last position */
+ modelData->material[fc] = malloc(sizeof(mtlStruct)*nMaterial);
+ mtllib[fc] = malloc(sizeof(char **) * nMtllib[fc]);
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->indices = malloc(sizeof(unsigned int *)*nIndices);
+ modelData->reorderedVertexBuffer = malloc(sizeof(vect3d)*nIndices);
+ modelData->reorderedTextureBuffer = malloc(sizeof(vect2d)*nIndices);
+ modelData->reorderedNormalBuffer = malloc(sizeof(vect3d)*nIndices);
+ }
+
+ tmpIndices = realloc(tmpIndices, sizeof(unsigned int **)*nVertex);
+ for (i=0; i<nVertex; i++)
+ {
+ tmpIndices[i] = NULL;
+ }
- modelData->nVertex = nVertex;
+ modelData->nVertex = nVertex;
+ modelData->nNormal = nNormal;
+ modelData->nTexture = nTexture;
nVertex = 0;
nNormal = 0;
nTexture = 0;
- nMtllib = 0;
nIndices = 0;
+ nUniqueIndices = 0;
+ nMtllib[fc] = 0;
- tmpType = (char **) realloc(tmpType, sizeof(char *));
- while ((strline=getLine(fp))!=NULL)
+ while ((strline=getLine(fp, &oldStrline))!=NULL)
{
- if (tmpType==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
- "out of mem");
- return FALSE;
- }
-
- tmpType[0]=strsep(&strline, delim);
+ tmpType=strsep(&strline, delim);
if (strline==NULL) continue;
- if (tmpType[0] == NULL)
+ if (tmpType == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
"Error reading file...");
//needs extra freeing here
if (filename)
free (filename);
+ if (oldStrline)
+ free (oldStrline);
return FALSE;
}
while (strline!=NULL) {
- if (!strcmp(tmpType[0], "")) {
- tmpType[0]=strsep(&strline, delim);
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
}
else break;
}
- if (strline==NULL || tmpType[0]==NULL) continue;
+ if (strline==NULL || tmpType==NULL) continue;
- if (!strcmp(tmpType[0], "v"))
+ if (!strcmp(tmpType, "v"))
{
for (i=0; i<3; i++)
- modelData->vertex[fc][nVertex].r[i]=atof(strsep(&strline, delim))*scaleFactor;
+ vertex[nVertex].r[i]=atof(strsep(&strline, delim))*modelData->scaleGlobal;
nVertex++;
}
-
- if (!strcmp(tmpType[0], "vn"))
+ else if (!strcmp(tmpType, "vn"))
{
for (i=0; i<3; i++)
- modelData->normal[fc][nNormal].r[i]=atof(strsep(&strline, delim));
+ normal[nNormal].r[i]=atof(strsep(&strline, delim));
nNormal++;
}
-
- if (!strcmp(tmpType[0], "vt"))
+ else if (!strcmp(tmpType, "vt"))
{
for (i=0; i<2; i++)
- modelData->texture[fc][nTexture].r[i]=atof(strsep(&strline, delim));
+ texture[nTexture].r[i]=atof(strsep(&strline, delim));
nTexture++;
}
-
- if (!strcmp(tmpType[0], "mtllib"))
+ else if (!strcmp(tmpType, "mtllib"))
{
while (1)
{
- modelData->mtllib[fc][nMtllib]=strsep(&strline, delim);
- if (modelData->mtllib[fc][nMtllib] == NULL)
+ tmpType=strsep(&strline, delim);
+ if (tmpType==NULL)
break;
- if (!strcmp(modelData->mtllib[fc][nMtllib], ""))
- continue; /* if blank override next time */
- nMtllib++;
+
+ if (!strcmp(tmpType, ""))
+ continue;
+
+
+ int lenMtllib = strlen(tmpType)+1;
+ /* it will place a NULL pointer in last position */
+
+ mtllib[fc][nMtllib[fc]] = malloc (sizeof (char) * lenMtllib);
+ memcpy (mtllib[fc][nMtllib[fc]], tmpType, sizeof (char) * lenMtllib);
+
+ nMtllib[fc]++;
}
}
@@ -406,15 +481,10 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
fclose(fp);
- }
-
-
-
-
- //vert3d *orderedVertex = malloc(sizeof(vect3d)*nVertex);
-
+
+
fp = fopen(filename, "r");
if (fp == NULL)
{
@@ -423,187 +493,532 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
return FALSE;
}
+ nVertex = 0;
+ nNormal = 0;
+ nTexture = 0;
+ nMaterial = 0;
+ Bool loadingMaterial = FALSE;
+ int lastLoadedMaterial = -1;
+
- tmpType = (char **) realloc(tmpType, sizeof(char *));
-
- while ((strline=getLine(fp))!=NULL)
+ while ((strline=getLine(fp, &oldStrline))!=NULL)
{
- if (tmpType==NULL)
- {
- compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
- "out of mem");
- return FALSE;
- }
-
- tmpType[0]=strsep(&strline, delim);
+ tmpType=strsep(&strline, delim);
if (strline==NULL) continue;
- if (tmpType[0] == NULL)
+ if (tmpType == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
"Error reading file...");
//needs extra freeing here
if (filename)
free (filename);
+ if (oldStrline)
+ free (oldStrline);
return FALSE;
}
while (strline!=NULL) {
- if (!strcmp(tmpType[0], "")) {
- tmpType[0]=strsep(&strline, delim);
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
}
else break;
}
- if (strline==NULL || tmpType[0]==NULL) continue;
+ if (strline==NULL || tmpType==NULL) continue;
- //printf (tmpType[0]);
- //continue;
-
- if (!strcmp(tmpType[0], "f"))
+ Bool updateGroup = FALSE;
+ int complexity = 0;
+ int polyCount = 0;
+ Bool usingNormal = FALSE;
+ Bool usingTexture = FALSE;
+
+ if (!strcmp(tmpType, "v"))
{
+ nVertex++;
+ }
+ else if (!strcmp(tmpType, "vt"))
+ {
+ nTexture++;
+ }
+ else if (!strcmp(tmpType, "n"))
+ {
+ nNormal++;
+ }
+ else if (!strcmp(tmpType, "usemtl") && fc==0)
+ { /* parse mtl file(s) and load specified material */
+ loadingMaterial = FALSE;
+ char * materialName = "";
+
+ tmpType=strsep(&strline, delim);
+ if (tmpType==NULL) continue;
+
+ while (strline!=NULL) {
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
+ }
+ else break;
+ }
+ if (tmpType==NULL) continue;
+
+ materialName = malloc (sizeof(char)*(strlen(tmpType)+1));
+ if (materialName ==NULL) continue;
+
+ strcpy (materialName, tmpType);
- char **tmpPtr; /* used to chec value of vertex/texture/normal numbers */
+ //printf ("material name - %s\n", materialName);
- int polyCount = 0;
- Bool usingNormal = FALSE;
+
+
+ for (i=0; i<nMtllib[fc] && !loadingMaterial; i++)
+ {
+ char* mtlFilename = findPath(filename, mtllib[fc][i]);
+ if (mtlFilename==NULL)
+ continue;
+
+ mtlfp = fopen(mtlFilename, "r");
+ if (mtlfp == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn,
+ "Failed to open material file : %s", mtllib[fc][i]);
+ continue;
+ }
+
+
+ while ((strline=getLine(mtlfp, &oldStrline))!=NULL)
+ {
+ tmpType=strsep(&strline, delim);
+ if (strline==NULL || tmpType==NULL) continue;
+
+ while (strline!=NULL) {
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
+ }
+ else break;
+ }
+ if (strline==NULL || tmpType==NULL) continue;
+
+ if (!strcmp(tmpType, "newmtl"))
+ {
+ tmpType=strsep(&strline, delim);
+ if (tmpType==NULL) break;
+
+ while (strline!=NULL) {
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
+ }
+ else break;
+ }
+ if (tmpType==NULL) break;
+
+ if (!strcmp(tmpType, materialName))
+ {
+ loadingMaterial = TRUE;
+
+ /* set defaults */
+ modelData->material[fc][nMaterial].Ns[0] = 100;
+ modelData->material[fc][nMaterial].Ni[0] = 1;
+ modelData->material[fc][nMaterial].illum = 2;
+
+ for (j=0; j<3; j++)
+ {
+ modelData->material[fc][nMaterial].Ka[j] = 0.2;
+ modelData->material[fc][nMaterial].Kd[j] = 0.8;
+ modelData->material[fc][nMaterial].Ks[j] = 1;
+ }
+ modelData->material[fc][nMaterial].Ka[3] = 1;
+ modelData->material[fc][nMaterial].Kd[3] = 1;
+ modelData->material[fc][nMaterial].Ks[3] = 1;
+
+ modelData->material[fc][nMaterial].tex = NULL;
+ }
+ else {
+ if (loadingMaterial) /* finished loading material */
+ break;
+ }
- tmpPtr=malloc(sizeof(char *) * 1);
+ }
+ else if (loadingMaterial) {
+
+ char *tmpPtr[3] = { NULL, NULL, NULL }; /* used to check numerical parameters*/
+
+ float tmpNum[3] = {0, 0, 0 };
+ float tmpIllum = 100;
+
+ for (j=0; j<3; j++)
+ {
+ tmpPtr[j]=strsep(&strline, delim);
+ if (tmpPtr[j]==NULL) break;
+
+ while (strline!=NULL) {
+ if (!strcmp(tmpPtr[j], "")) {
+ tmpPtr[j]=strsep(&strline, delim);
+ }
+ else break;
+ }
+ if (tmpPtr[j]==NULL) break;
+
+ tmpNum[j] = atof(tmpPtr[j]);
+
+ if (j==0)
+ tmpIllum = atoi(tmpPtr[j]);
+ }
+
+ if (!strcmp(tmpType, "Ns"))
+ {
+ modelData->material[fc][nMaterial].Ns[0] = tmpNum[0];
+ }
+ else if (!strcmp(tmpType, "Ka"))
+ {
+ for (j=0; j<3; j++)
+ modelData->material[fc][nMaterial].Ka[j] = tmpNum[j];
+ }
+ else if (!strcmp(tmpType, "Kd"))
+ {
+ for (j=0; j<3; j++)
+ modelData->material[fc][nMaterial].Kd[j] = tmpNum[j];
+ }
+ else if (!strcmp(tmpType, "Ks"))
+ {
+ for (j=0; j<3; j++)
+ modelData->material[fc][nMaterial].Ks[j] = tmpNum[j];
+ }
+ else if (!strcmp(tmpType, "Ni"))
+ {
+ modelData->material[fc][nMaterial].Ni[0] = tmpNum[0];
+ }
+ else if (!strcmp(tmpType, "d") || !strcmp(tmpType,"Tr"))
+ {
+ modelData->material[fc][nMaterial].Ka[3] = tmpNum[0];
+ modelData->material[fc][nMaterial].Kd[3] = tmpNum[0];
+ modelData->material[fc][nMaterial].Ks[3] = tmpNum[0];
+ }
+ else if (!strcmp(tmpType, "illum"))
+ {
+ modelData->material[fc][nMaterial].illum = tmpIllum;
+ }
+ else if (!strcmp(tmpType, "map_Ka") || !strcmp(tmpType, "map_Kd") || !strcmp(tmpType, "map_Ks")) /* XXX map_Kd and map_Ks */
+ {
+ lastLoadedMaterial = nMaterial;
+
+ char *tmpName = NULL;
+ modelData->material[fc][nMaterial].tex=malloc(sizeof(CompTexture));
+
+ if (modelData->material[fc][nMaterial].tex == NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus",
+ CompLogLevelWarn,
+ "CompTexture is not malloced properly");
+ }
+ else if (!readImageToTexture(obj->s,
+ modelData->material[fc][nMaterial].tex, /*texture pointer*/
+ (tmpName=findPath(filename, tmpPtr[0])), /*file*/
+ &(modelData->material[fc][nMaterial].width), /*pointer for width*/
+ &(modelData->material[fc][nMaterial].height)) ) /*pointer for height*/
+ {
+ compLogMessage(obj->s->display, "cubedbus",
+ CompLogLevelWarn,
+ "Failed to load image");
+
+ finiTexture(obj->s, modelData->material[fc][nMaterial].tex);
+ modelData->material[fc][nMaterial].tex=NULL;
+ lastLoadedMaterial = -1;
+ }
+ else
+ finiTexture(obj->s, modelData->material[fc][nMaterial].tex);
+
+ if (tmpName != NULL)
+ free(tmpName);
+ tmpName = NULL;
+ }
+
+ }
+
+ }
+
+ fclose(mtlfp);
+ }
+
+ free(materialName);
+
+ if (loadingMaterial)
+ updateGroup = TRUE;
+ }
+ else if (!strcmp(tmpType, "f") || !strcmp(tmpType, "p") || !strcmp(tmpType, "l") )
+ {
+ if (!strcmp(tmpType, "l") )
+ complexity = 1;
+ else if (!strcmp(tmpType, "f"))
+ complexity = 2;
+ char *tmpPtr; /* used to check value of vertex/texture/normal numbers */
+
+
while (TRUE) {
- tmpType[0]=strsep(&strline, delim);
- if (tmpType[0]==NULL) break;
+ tmpType=strsep(&strline, delim);
+ if (tmpType==NULL) break;
while (strline!=NULL) {
- if (!strcmp(tmpType[0], "")) {
- tmpType[0]=strsep(&strline, delim);
+ if (!strcmp(tmpType, "")) {
+ tmpType=strsep(&strline, delim);
}
else break;
}
- if (tmpType[0]==NULL) break;
- //printf (tmpType[0]);
- //printf ("\n");
-
- tmpPtr[0]=strsep(&tmpType[0], "/");
- if (tmpPtr[0]!=NULL) {
- //printf (tmpPtr[0]);
- //printf("\n");
+ if (tmpType==NULL) break;
+
+ int vertexIndex = -1;
+ int textureIndex = -1;
+ int normalIndex = -1;
+
+ tmpPtr=strsep(&tmpType, "/");
+ if (tmpPtr!=NULL) {
- printf ("v: %d, ", atoi(tmpPtr[0]));
- modelData->indices[nIndices] = atoi(tmpPtr[0])-1;
- nIndices++;
- polyCount++;
+ vertexIndex = atoi(tmpPtr);
+ //printf ("v: %d, ", vertexIndex);
+ if (vertexIndex>0)
+ {
+ if (vertexIndex > modelData->nVertex) /* skip vertex index past last in obj file */
+ break;
+ vertexIndex--;
+ }
+ else if (vertexIndex<0)
+ {
+ vertexIndex+=nVertex;
+ if (vertexIndex<0) /* skip vertex index < 0 in obj file */
+ break;
+ }
+ else /* skip vertex index of 0 in obj file */
+ break;
}
else break;
- tmpPtr[0]=strsep(&tmpType[0], "/");
- if (tmpPtr[0]!=NULL) {
- if (strcmp(tmpPtr[0], "")) {
- printf ("t: %d, ", atoi(tmpPtr[0]));
+ tmpPtr=strsep(&tmpType, "/");
+ if (tmpPtr!=NULL && complexity!=0) { /* texture */
+ if (strcmp(tmpPtr, "")) {
+ //printf ("t: %d, ", atoi(tmpPtr));
+ textureIndex = atoi(tmpPtr);
+
+ if (textureIndex>0)
+ {
+ if (textureIndex > modelData->nTexture) /* skip normal index past last in obj file */
+ break;
+ textureIndex--;
+ }
+ else if (textureIndex<0)
+ {
+ textureIndex+=nTexture;
+ if (textureIndex<0) /* skip texture index < 0 in obj file */
+ break;
+ }
+ else /* skip texture index of 0 in obj file */
+ break;
+
+ usingTexture = TRUE;
+ }
+
+
+ tmpPtr=strsep(&tmpType, "/");
+ if (tmpPtr!=NULL && strcmp(tmpPtr, "") && complexity==2)
+ { /* normal */
+
+ normalIndex = atoi(tmpPtr);
+
+ if (normalIndex>0)
+ {
+ if (normalIndex > modelData->nNormal) /* skip normal index past last in obj file */
+ break;
+ normalIndex--;
+ }
+ else if (normalIndex<0)
+ {
+ normalIndex+=nNormal;
+ if (normalIndex<0) /* skip normal index < 0 in obj file */
+ break;
+ }
+ else /* skip normal index of 0 in obj file */
+ break;
+
+ usingNormal = TRUE;
}
+
}
- else continue;
- 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;
+ /* reorder vertices/textures/normals */
+
+ int tmpInd = addVertex(&tmpIndices[vertexIndex],
+ nUniqueIndices, textureIndex,
+ normalIndex);
+ if (tmpInd<0)
+ {
+ memcpy(modelData->reorderedVertex[fc][nUniqueIndices].r,
+ vertex[vertexIndex].r,
+ 3*sizeof(float));
+
+ if (textureIndex>=0)
+ {
+ memcpy(modelData->reorderedTexture[fc][nUniqueIndices].r,
+ texture[textureIndex].r,
+ 2*sizeof(float));
+
+ /* scale as per 1st file in animation texture files */
+ if (lastLoadedMaterial>=0)
+ if (modelData->material[fc][lastLoadedMaterial].tex!=NULL)
+ {
+ modelData->reorderedTexture[fc][nUniqueIndices].r[0]=
+ COMP_TEX_COORD_X (&(modelData->material[fc][lastLoadedMaterial].tex->matrix),
+ (modelData->material[fc][lastLoadedMaterial].width-1) * (texture[textureIndex].r[0]));
+ modelData->reorderedTexture[fc][nUniqueIndices].r[1]=
+ COMP_TEX_COORD_Y (&(modelData->material[fc][lastLoadedMaterial].tex->matrix),
+ (modelData->material[fc][lastLoadedMaterial].height-1) * (1-texture[textureIndex].r[1]));
+ }
+
+ }
+ else
+ {
+ modelData->reorderedTexture[fc][nUniqueIndices].r[0] = 0;
+ modelData->reorderedTexture[fc][nUniqueIndices].r[1] = 0;
+ }
+
+ if (normalIndex>=0)
+ memcpy(modelData->reorderedNormal[fc][nUniqueIndices].r,
+ normal[normalIndex].r,
+ 3*sizeof(float));
+ else
+ {
+ modelData->reorderedNormal[fc][nUniqueIndices].r[0] = 0;
+ modelData->reorderedNormal[fc][nUniqueIndices].r[1] = 0;
+ modelData->reorderedNormal[fc][nUniqueIndices].r[2] = 1;
+ }
+
+
+ modelData->indices[nIndices] = nUniqueIndices;
+ nUniqueIndices++;
}
+ else
+ modelData->indices[nIndices] = tmpInd;
+
+ //printf("Actually got here. %i, %i, %i\n", nIndices, nUniqueIndices, vertexIndex);
+ nIndices++;
+ polyCount++;
}
- printf ("\n");
- if (polyCount!=0 && (polyCount!=oldPolyCount || usingNormal!=oldUsingNormal)) {
+
+ updateGroup = TRUE;
+ }
+
+ if (updateGroup && fc==0)
+ {
+ //printf ("\n");
+ if (polyCount!=0 && ( polyCount!=oldPolyCount
+ || usingNormal!=oldUsingNormal || usingTexture!=oldUsingTexture
+ || loadingMaterial ) )
+ {
oldPolyCount = polyCount;
+ oldUsingTexture = usingTexture;
oldUsingNormal = usingNormal;
- nGroups++;
- printf ("polyCount = %d\n", polyCount);
- printf ("usingNormal = %d\n", usingNormal);
+ nGroups++;
+
+ //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].complexity = complexity;
modelData->group[nGroups-1].startV = nIndices-polyCount;
+ modelData->group[nGroups-1].materialIndex = -1;
+ if (loadingMaterial)
+ {
+ modelData->group[nGroups-1].materialIndex = nMaterial;
+ nMaterial++;
+ }
+ loadingMaterial = FALSE;
+
if (nGroups>1)
modelData->group[nGroups-2].numV = nIndices-polyCount-modelData->group[nGroups-2].startV;
- modelData->group[nGroups-1].normal = usingNormal;
+ modelData->group[nGroups-1].texture = usingTexture;
+ modelData->group[nGroups-1].normal = usingNormal;
}
-
-
- if (tmpPtr)
- free (tmpPtr);
-
}
+
}
- if (nGroups!=0)
+ if (nGroups!=0 && fc==0)
modelData->group[nGroups-1].numV = nIndices-modelData->group[nGroups-1].startV;
+ if (fc==0)
+ modelData->nIndices = nUniqueIndices;
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);
+ if (tmpIndices)
+ for (i=0; i<modelData->nVertex; i++)
+ if (tmpIndices[i])
+ free (tmpIndices[i]);
+
+ }
modelData->nGroups = nGroups;
-
- for (i=0; i<nGroups; i++)
- {
- if (modelData->group[i].polyCount<2) continue;
-
- 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)
+
+ if (!animation)
{
- glEnableClientState(GL_NORMAL_ARRAY);
- glNormalPointer (GL_FLOAT, 0, n);
+ 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);
+
+ glDisable(GL_COLOR_MATERIAL);
+
+ cubedbusDrawVBOModel(obj,
+ (float *) modelData->reorderedVertex[0],
+ (float *) modelData->reorderedNormal[0]);
+
+ glEndList();
}
- 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);
+ if (mtllib)
+ {
+ for (fc=0; fc<fileCounter; fc++)
+ {
+ if (mtllib[fc])
+ {
+ for (i=0; i<nMtllib[fc]; i++)
+ {
+ if (mtllib[fc][i])
+ free(mtllib[fc][i]);
+ }
+ free(mtllib[fc]);
+ }
}
+
+ free(mtllib);
}
- glDisableClientState (GL_NORMAL_ARRAY);
-
- }
+
+ if (nMtllib)
+ free (nMtllib);
- glEndList();
- if (tmpType)
- free(tmpType);
+ if (vertex)
+ free (vertex);
+ if (normal)
+ free (normal);
+ if (texture)
+ free (texture);
+
+ if (tmpIndices)
+ free (tmpIndices);
if (filename)
free (filename);
-
+ if (oldStrline)
+ free (oldStrline);
return TRUE;
}
@@ -647,6 +1062,40 @@ addNumToString (char * s, unsigned int size, int offset, unsigned int x, unsigne
return size;
}
+static int
+addVertex (unsigned int **indices, int nUniqueIndices, int iTexture, int iNormal)
+{
+ if (*indices==NULL)
+ {
+ *indices = malloc (4*sizeof(int));
+ (*indices)[0] = 1; //store size of array as 1st element
+ (*indices)[1] = iTexture;
+ (*indices)[2] = iNormal;
+ (*indices)[3] = nUniqueIndices; /* ptr to the new unique vertex index */
+
+ //printf ("indices == NULL\n");
+
+ return -1; /* new vertex/texture/normal */
+ }
+ int i;
+ int len = (*indices)[0];
+ for (i=0; i<len; i++)
+ {
+ if ((*indices)[1+3*i]==iTexture && (*indices)[2+3*i]==iNormal)
+ {
+ //printf ("\n-- %i, %i --\n", (*indices)[1+3*i], (*indices)[2+3*i]);
+ return (*indices)[3+3*i];
+ }
+ }
+ *indices = realloc ( *indices, ( 1+3*((*indices)[0]+1) )*sizeof(int) );
+ (*indices)[1+3*(*indices)[0]]=iTexture;
+ (*indices)[2+3*(*indices)[0]]=iNormal;
+ (*indices)[3+3*(*indices)[0]]=nUniqueIndices;
+ (*indices)[0]++;
+
+ return -1;
+}
+
static bool
cubedbusDeleteModelObject (CubedbusObject *obj)
{
@@ -656,37 +1105,33 @@ cubedbusDeleteModelObject (CubedbusObject *obj)
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->reorderedVertex[fc])
+ free (data->reorderedVertex[fc]);
+ if (data->reorderedTexture[fc])
+ free (data->reorderedTexture[fc]);
if (data->reorderedNormal[fc])
free (data->reorderedNormal[fc]);
- if (data->mtllib[fc])
- free (data->mtllib[fc]);
+ if (data->material[fc])
+ free (data->material[fc]);
}
- if (data->vertex)
- free (data->vertex);
- if (data->normal)
- free (data->normal);
- if (data->texture)
- free (data->texture);
+ if (data->reorderedVertex)
+ free (data->reorderedVertex);
+ if (data->reorderedTexture)
+ free (data->reorderedTexture);
if (data->reorderedNormal)
free (data->reorderedNormal);
- if (data->mtllib)
- free (data->mtllib);
+ if (data->material)
+ free (data->material);
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->reorderedVertexBuffer)
+ free (data->reorderedVertexBuffer);
+ if (data->reorderedTextureBuffer)
+ free (data->reorderedTextureBuffer);
if (data->reorderedNormalBuffer)
free(data->reorderedNormalBuffer);
@@ -706,10 +1151,9 @@ static bool
cubedbusDrawModelObject (CubedbusObject *obj)
{
CubedbusModelObject *data=obj->data;
-
- groupIndices * group;
- int i, j;
+ 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]);
@@ -720,59 +1164,37 @@ cubedbusDrawModelObject (CubedbusObject *obj)
glEnable(GL_RESCALE_NORMAL);
glEnable(GL_DEPTH_TEST);
- glEnable (GL_COLOR_MATERIAL);
- glColor4f (1, 1, 1, 1);
+ /* FIX - this check is here because models which are not found are not deleted */
+ if (data->fileCounter==0)
+ return TRUE;
+ /*FIX - this is for when the mtl file is not found or overridden */
+ glEnable (GL_COLOR_MATERIAL);
+ glColor4fv (data->color);
+
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;
+ float dt2 = 1-dt;
- for (i=0; i<data->nVertex; i++)
+ for (i=0; i<data->nIndices; 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] +
+ data->reorderedVertexBuffer[i].r[j] = dt2*(data->reorderedVertex[ti][i].r[j]) +
+ dt*(data->reorderedVertex[ti2][i].r[j]);
+ data->reorderedNormalBuffer[i].r[j] = dt2*(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);
- }
+ cubedbusDrawVBOModel (obj,
+ (float *) data->reorderedVertexBuffer,
+ (float *) data->reorderedNormalBuffer);
}
else
{
@@ -783,23 +1205,107 @@ cubedbusDrawModelObject (CubedbusObject *obj)
if(data->rotate[0]>360) data->rotate[0]=0;
if(data->rotate[0]<0) data->rotate[0]=360;
- glDisable (GL_LIGHTING);
return TRUE;
}
-/* Here after lies my functions */
+static bool
+cubedbusDrawVBOModel (CubedbusObject *obj, float * vertex, float * normal)
+{
+ CubedbusModelObject *data=obj->data;
+
+ groupIndices * group;
+ int i, j;
+
+
+ for (i=0; i<data->nGroups; i++)
+ {
+ group = &(data->group[i]);
+ if (group->polyCount<1) continue;
+
+ float * v = vertex;
+ float * n = normal;
+ float * t = (float *) data->reorderedTexture[0];
+
+ GLenum cap = GL_QUADS;
+ if (group->polyCount==3) cap = GL_TRIANGLES;
+ if (group->polyCount==2 || group->complexity==1) cap = GL_LINE_LOOP;
+ if (group->polyCount==1 || group->complexity==0) cap = GL_POINTS;
+
+
+ if (group->materialIndex>=0)
+ {
+ glDisable (GL_COLOR_MATERIAL);
+
+ switch (data->material[0][group->materialIndex].illum)
+ {
+ case 0:
+ glDisable (GL_LIGHTING);
+ break;
+ default:
+ glEnable (GL_LIGHTING);
+ }
+
+ glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+ glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, data->material[0][group->materialIndex].Ns);
+ glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, data->material[0][group->materialIndex].Ka);
+ glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, data->material[0][group->materialIndex].Kd);
+ glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, data->material[0][group->materialIndex].Ks);
+
+ if (data->material[0][group->materialIndex].tex!=NULL)
+ {
+ 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);
+ glVertexPointer (3, GL_FLOAT, 0, v);
+ if (group->texture)
+ {
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ glTexCoordPointer(2, GL_FLOAT, 0, t);
+ }
+ 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);
+ }
+ }
+
+ if(group->materialIndex>=0 && data->material[0][group->materialIndex].tex != NULL)
+ {
+ disableTexture (obj->s, data->material[0][group->materialIndex].tex);
+ }
+
+ //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+ glDisableClientState (GL_NORMAL_ARRAY);
+ }
+
+ return TRUE;
+
+}
/*************************************
* returns string with line from file *
-* NOTE: you must free line after use *
+* NOTE: you must free oldStrline after use *
+* NOTE: can use strsep on strline (modifies the address of strline)
* NOTE: returns NULL if error occurs *
*************************************/
static char *
-getLine (FILE *fp)
+getLine (FILE *fp, char **oldStrline)
{
- char *tmp=NULL;
char *strline=NULL;
int c;
@@ -807,22 +1313,27 @@ getLine (FILE *fp)
while( (c=getc(fp) ) != EOF )
{
- tmp=realloc(strline, 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=realloc((*oldStrline), sizeof(char)*(n+1));
+
+ if(strline == NULL)
+ {
+ compLogMessage(NULL, "cubedbus", CompLogLevelWarn, "Error reading file - out of mem");
+ if (*oldStrline)
+ free (*oldStrline);
+ return NULL;
+ }
- strline[n]=c;
- if(c=='\n' || c=='\0' || c=='\r') /* the line is ended - return */
- {
- strline[n]='\0';
- return strline;
- }
- n++;
+ (*oldStrline) = strline;
+
+ /* 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' || c=='\r') /* the line is ended - return */
+ {
+ strline[n]='\0';
+ return strline;
+ }
+ n++;
}
return NULL;
@@ -841,44 +1352,44 @@ findPath (char *object, char *file)
char *filePath; /* string containing /full/path/to/file */
char *tmpPtr;
- char *myObject=malloc(sizeof(char)*(strlen(object)+1));
- char *myFile=malloc(sizeof(char)*(strlen(file)+1));
-
- if(myObject == NULL || myFile == NULL) return NULL;
-
- strcpy(myObject, object);
- strcpy(myFile, file);
-
int i;
- for(i=strlen(myObject);i>=0;i--)
- {
- if(myObject[i]=='/')
- {
- myObject[i]='\0'; /* end string at last / (gives path to object) */
- tmpPtr=realloc(myObject, sizeof(char)*(i+1));
- if(tmpPtr==NULL) return NULL;
- myObject=tmpPtr;
- break;
- }
- }
+
+ if(file==NULL || object==NULL)
+ return NULL;
+
+
+ if(file[0]=='/')
+ {
+ filePath = malloc(sizeof(char)*(strlen(file)+1));
+ if (filePath == NULL)
+ return NULL;
- for(i=strlen(myFile);i>=0;i--)
- {
- if(myFile[i]=='/')
- {
- i++;
- break;
- }
- }
+ strcpy(filePath, file);
+ return filePath;
+ }
- if(i==-1) i=0;
+ filePath = malloc(sizeof(char)*(strlen(object)+1));
+ if (filePath == NULL)
+ return NULL;
- filePath=malloc(sizeof(char) * (strlen(myObject)+strlen(myFile) +1 ));
- sprintf(filePath, "%s/%s", myObject, &myFile[i]);
- free(myObject);
- free(myFile);
- tmpPtr=realloc(filePath, sizeof(char) * (strlen(filePath)+1));
- if(tmpPtr != NULL) filePath=tmpPtr;
+ strcpy(filePath, object);
+
+ for(i=strlen(filePath)-1; i>=0; i--)
+ {
+ if(filePath[i]=='/')
+ {
+ filePath[i+1]='\0'; /* end string at last / (gives path to object) */
+ break;
+ }
+ }
+ tmpPtr = realloc(filePath, sizeof(char)*(strlen(filePath)+strlen(file)+1));
+ if (tmpPtr==NULL)
+ {
+ free (filePath);
+ return NULL;
+ }
+ filePath = tmpPtr;
+ strcat (filePath, file);
return filePath;
}
diff --git a/scripts/animatedModel.sh b/scripts/animatedModel.sh
new file mode 100644
index 0000000..d22b6c2
--- /dev/null
+++ b/scripts/animatedModel.sh
@@ -0,0 +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 string:'numZeros' int32:2 string:'fps' int32:15 string:'start' int32:0
+
+#Run this in terminal - accepts as argument 1 .obj file