summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Bosveld <joel@JOEL-UBUNTU.(none)>2007-12-10 21:48:51 +0900
committerJoel Bosveld <joel@JOEL-UBUNTU.(none)>2007-12-10 21:48:51 +0900
commit49d9308c827b3078ad0ae49f6fe8f2e0746e0f6f (patch)
treedf614f6a14e765a3bac785a360a2cd48394cc629
parent540679ccb8e3abc018a6cd4f2d465e56254b93d8 (diff)
downloadcubedbus-49d9308c827b3078ad0ae49f6fe8f2e0746e0f6f.tar.gz
cubedbus-49d9308c827b3078ad0ae49f6fe8f2e0746e0f6f.tar.bz2
Added basic texture support (does not yet work...)
-rw-r--r--cubedbusModel.c.in108
1 files changed, 96 insertions, 12 deletions
diff --git a/cubedbusModel.c.in b/cubedbusModel.c.in
index c45b801..f683a0a 100644
--- a/cubedbusModel.c.in
+++ b/cubedbusModel.c.in
@@ -28,6 +28,10 @@ GLuint dList;
bool order;
float rotate[4], translate[3], scale[3];
float rotateSpeed;
+
+CompTexture *tex;
+
+unsigned height, width;
};
@@ -53,7 +57,7 @@ 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;
@@ -129,9 +133,9 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
/* Second pass - fill arrays */
- vect3d vertex[nVertex];
- vect3d normal[nNormal];
- vect2d texture[nTexture];
+ vect3d *vertex=malloc(sizeof(vect3d)*nVertex);
+ vect3d *normal=malloc(sizeof(vect3d)*nNormal);
+ vect2d *texture=malloc(sizeof(vect2d)*nTexture);
nVertex=0;
nNormal=0;
@@ -174,7 +178,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if(tmpType[1] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- free(strline);
+ //free(strline);
return FALSE;
}
@@ -182,7 +186,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if(tmpType[2] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- free(strline);
+ //free(strline);
return FALSE;
}
@@ -190,7 +194,7 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if(tmpType[3] == NULL)
{
compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "Error reading file...");
- free(strline);
+ //free(strline);
return FALSE;
}
@@ -201,6 +205,25 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
nVertex++;
}
+ if(!strcmp(tmpType[0], "vt"))
+ {
+ texture[nTexture].x=0;
+ texture[nTexture].y=0;
+
+ tmpType[1]=strsep(&strline," ");
+ if(tmpType[1] != NULL)
+ {
+ texture[nTexture].x=atof(tmpType[1]);
+ }
+
+ tmpType[2]=strsep(&strline," ");
+ if(tmpType[2] != NULL)
+ {
+ texture[nTexture].y=atof(tmpType[2]);
+ }
+
+ nTexture++;
+ }
/* prepare for next line */
// free(strline);
n=0;
@@ -215,6 +238,29 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
int vertNum=0;
int polyCount;
char *tempPtr;
+ char **tempInfo;
+
+ if(nTexture)
+ {
+ 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*/
+ "/home/joel/hand/hand.png", /*XXX*/ /*file*/
+ &(modelData->width), /*pointer for width*/
+ &(modelData->height)) ) /*pointer for height*/
+ {
+ compLogMessage (obj->s->display, "cubedbus", CompLogLevelWarn, "Failed to load image");
+
+ finiTexture(obj->s, modelData->tex);
+ modelData->tex=NULL;
+ }
+
+ }
/* Final (third) pass - use data */
@@ -245,7 +291,14 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
if(c=='\n') /*the line is ended, copy stuff to array, and prepare for next line */
{
- tmpType = malloc ( sizeof(char *) * 5); /* XXX need dynamically resizing array - makes assumption that quad is highest polygon */
+ tmpType = malloc ( sizeof(char *));
+ if(tmpType==NULL)
+ {
+ compLogMessage(obj->s->display, "cubedbus", CompLogLevelWarn, "out of mem");
+ free(tmpType);
+ return FALSE;
+ }
+
tmpType[0]=strsep(&strline," ");
if(tmpType[0] == NULL)
{
@@ -261,14 +314,37 @@ cubedbusAddModelObject (CubedbusObject *obj, CompOption *option, int nOption)
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," ");
+ if(tmpType[polyCount] == NULL)
+ {
+ //free(tmpType);
+ break;
+ }
tempPtr=strsep(&tmpType[polyCount],"/");
- if(tempPtr == NULL) break;
+ if(tempPtr == NULL)
+ {
+ //free(tmpType);
+ break;
+ }
vertNum=atoi(tempPtr);
/* XXX also need to do normal and tex coord */
+ if(modelData->tex != NULL)
+ {
+ glTexCoord2f (COMP_TEX_COORD_X (&(modelData->tex->matrix), (modelData->width)*(texture[vertNum-1].x)),
+ COMP_TEX_COORD_Y (&(modelData->tex->matrix), (modelData->height)*(1-texture[vertNum-1].y) ));
+ }
glVertex3f (vertex[vertNum-1].x,vertex[vertNum-1].y,vertex[vertNum-1].z);
polyCount++ ;
@@ -322,10 +398,18 @@ cubedbusDrawModelObject (CubedbusObject *obj)
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);
+
+ }
+
glCallList(data->dList);
-
+
+ if(data->tex != NULL) disableTexture (obj->s, data->tex);
data->rotate[0]+=data->rotateSpeed;
if(data->rotate[0]>360) data->rotate[0]=0;