added support to image loader for loading mask as well (used for fog effects), remove...
authorlordhavoc <lordhavoc@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 21 Nov 2000 09:02:28 +0000 (09:02 +0000)
committerlordhavoc <lordhavoc@d7cf8633-e32d-0410-b094-e92efae38249>
Tue, 21 Nov 2000 09:02:28 +0000 (09:02 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@91 d7cf8633-e32d-0410-b094-e92efae38249

image.c
image.h
model_sprite.c

diff --git a/image.c b/image.c
index 6740dac..a7f3b8c 100644 (file)
--- a/image.c
+++ b/image.c
@@ -437,6 +437,37 @@ byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int ma
        return NULL;
 }
 
+int image_makemask (byte *in, byte *out, int size)
+{
+       int             i, count;
+       count = 0;
+       for (i = 0;i < size;i++)
+       {
+               out[0] = out[1] = out[2] = 255;
+               out[3] = in[3];
+               if (in[3] != 255)
+                       count++;
+               in += 4;
+               out += 4;
+       }
+       return count;
+}
+
+byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight)
+{
+       byte    *in, *data;
+       in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
+       if (!data)
+               return NULL;
+       if (image_makemask(data, data, image_width * image_height))
+               return data; // some transparency
+       else
+       {
+               free(data);
+               return NULL; // all opaque
+       }
+}
+
 int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
 {
        int texnum;
@@ -446,19 +477,37 @@ int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean
        texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
        free(data);
        return texnum;
-       /*
-       if (texnum >= 0) // specific texnum, not cached
-       {
-               glBindTexture(GL_TEXTURE_2D, texnum);
-               GL_Upload32 (data, image_width, image_height, mipmap, true);
-               free(data);
-               return texnum;
-       }
-       else // any texnum, cached
+}
+
+int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
+{
+       int texnum;
+       byte *data;
+       if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
+               return 0;
+       texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
+       free(data);
+       return texnum;
+}
+
+int image_masktexnum;
+int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
+{
+       int texnum, count;
+       byte *data;
+       char *filename2;
+       image_masktexnum = 0;
+       if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
+               return 0;
+       texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
+       count = image_makemask(data, data, image_width * image_height);
+       if (count)
        {
-               texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
-               free(data);
-               return texnum;
+               filename2 = malloc(strlen(filename) + 6);
+               sprintf(filename2, "%s_mask", filename);
+               image_masktexnum = GL_LoadTexture (filename2, image_width, image_height, data, mipmap, true, 4);
+               free(filename2);
        }
-       */
+       free(data);
+       return texnum;
 }
diff --git a/image.h b/image.h
index 375f511..d0dc563 100644 (file)
--- a/image.h
+++ b/image.h
@@ -1,3 +1,8 @@
 
 extern void Image_Copy8bitRGBA(byte *in, byte *out, int pixels, int *pal);
 extern void Image_CopyRGBAGamma(byte *in, byte *out, int pixels);
+extern int image_makemask (byte *in, byte *out, int size);
+extern byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight);
+extern int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap);
+extern int image_masktexnum;
+extern int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap);
index 430b18f..b228149 100644 (file)
@@ -69,38 +69,43 @@ void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum,
        pspriteframe->right = width + origin[0];
 
        sprintf (name, "%s_%i", loadmodel->name, framenum);
-       pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel);
-       // make fog version (just alpha)
-       pixbuf = pixel = malloc(width*height*4);
-       inpixel = (byte *)(pinframe + 1);
-       if (bytesperpixel == 1)
+       pspriteframe->gl_texturenum = loadtextureimagewithmask(name, 0, 0, false, true);
+       pspriteframe->gl_fogtexturenum = image_masktexnum;
+       if (pspriteframe->gl_texturenum == 0)
        {
-               for (i = 0;i < width*height;i++)
+               pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel);
+               // make fog version (just alpha)
+               pixbuf = pixel = malloc(width*height*4);
+               inpixel = (byte *)(pinframe + 1);
+               if (bytesperpixel == 1)
                {
-                       *pixel++ = 255;
-                       *pixel++ = 255;
-                       *pixel++ = 255;
-                       if (*inpixel++ != 255)
+                       for (i = 0;i < width*height;i++)
+                       {
                                *pixel++ = 255;
-                       else
-                               *pixel++ = 0;
+                               *pixel++ = 255;
+                               *pixel++ = 255;
+                               if (*inpixel++ != 255)
+                                       *pixel++ = 255;
+                               else
+                                       *pixel++ = 0;
+                       }
                }
-       }
-       else
-       {
-               inpixel+=3;
-               for (i = 0;i < width*height;i++)
+               else
                {
-                       *pixel++ = 255;
-                       *pixel++ = 255;
-                       *pixel++ = 255;
-                       *pixel++ = *inpixel;
-                       inpixel+=4;
+                       inpixel+=3;
+                       for (i = 0;i < width*height;i++)
+                       {
+                               *pixel++ = 255;
+                               *pixel++ = 255;
+                               *pixel++ = 255;
+                               *pixel++ = *inpixel;
+                               inpixel+=4;
+                       }
                }
+               sprintf (name, "%s_%ifog", loadmodel->name, framenum);
+               pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4);
+               free(pixbuf);
        }
-       sprintf (name, "%s_%ifog", loadmodel->name, framenum);
-       pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4);
-       free(pixbuf);
 
        return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size);
 }