- GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
- }
- qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
-}
-
-static void R_FindImageForTexture(gltexture_t *glt)
-{
- int i, j, best, best2, x, y, z, w, h, d;
- textypeinfo_t *texinfo;
- gltexturepool_t *pool;
- gltextureimage_t *image, **imagechainpointer;
- texinfo = glt->textype;
- pool = glt->pool;
-
- // remains -1 until uploaded
- glt->texnum = -1;
-
- x = 0;
- y = 0;
- z = 0;
- w = glt->width;
- h = glt->height;
- d = glt->depth;
- if (glt->flags & TEXF_FRAGMENT)
- {
- for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
- {
- image = *imagechainpointer;
- if (image->type != GLIMAGETYPE_FRAGMENTS)
- continue;
- if (image->texturetype != glt->texturetype)
- continue;
- if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP))
- continue;
- if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
- continue;
- if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
- continue;
-
- // got a fragments texture, find a place in it if we can
- for (best = image->width, i = 0;i < image->width - w;i++)
- {
- for (best2 = 0, j = 0;j < w;j++)
- {
- if (image->blockallocation[i+j] >= best)
- break;
- if (best2 < image->blockallocation[i+j])
- best2 = image->blockallocation[i+j];
- }
- if (j == w)
- {
- // this is a valid spot
- x = i;
- y = best = best2;
- }
- }
-
- if (best + h > image->height)
- continue;
-
- for (i = 0;i < w;i++)
- image->blockallocation[x + i] = best + h;
-
- glt->x = x;
- glt->y = y;
- glt->z = 0;
- glt->image = image;
- image->texturecount++;
- return;
- }
-
- image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
- if (image == NULL)
- Sys_Error("R_FindImageForTexture: ran out of memory\n");
- image->type = GLIMAGETYPE_FRAGMENTS;
- // make sure the created image is big enough for the fragment
- for (image->width = block_size;image->width < glt->width;image->width <<= 1);
- image->height = 1;
- if (gltexturetypedimensions[glt->texturetype] >= 2)
- for (image->height = block_size;image->height < glt->height;image->height <<= 1);
- image->depth = 1;
- if (gltexturetypedimensions[glt->texturetype] >= 3)
- for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
- image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
- memset(image->blockallocation, 0, image->width * sizeof(short));
-
- x = 0;
- y = 0;
- z = 0;
- for (i = 0;i < w;i++)
- image->blockallocation[x + i] = y + h;
- }
- else
- {
- for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
-
- image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
- if (image == NULL)
- Sys_Error("R_FindImageForTexture: ran out of memory\n");
- image->type = GLIMAGETYPE_TILE;
- image->blockallocation = NULL;
-
- // calculate final size
- if (r_max_size.integer > realmaxsize)
- Cvar_SetValue("r_max_size", realmaxsize);
- for (image->width = 1;image->width < glt->width;image->width <<= 1);
- for (image->height = 1;image->height < glt->height;image->height <<= 1);
- for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
- for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
- for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
- for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
- if (image->width < 1) image->width = 1;
- if (image->height < 1) image->height = 1;
- if (image->depth < 1) image->depth = 1;