+ pixel = olm->bspDirBytes + ( ( ( oy * olm->customWidth ) + ox ) * 3 );
+ VectorScale( deluxel, 1000.0f, direction );
+ VectorNormalize( direction, direction );
+ VectorScale( direction, 127.5f, direction );
+ for ( i = 0; i < 3; i++ )
+ pixel[ i ] = (byte)( 127.5f + direction[ i ] );
+ }
+ }
+ }
+ }
+}
+
+
+
+/*
+ CompareRawLightmap()
+ compare function for qsort()
+ */
+
+static int CompareRawLightmap( const void *a, const void *b ){
+ rawLightmap_t *alm, *blm;
+ surfaceInfo_t *aInfo, *bInfo;
+ int i, min, diff;
+
+
+ /* get lightmaps */
+ alm = &rawLightmaps[ *( (const int*) a ) ];
+ blm = &rawLightmaps[ *( (const int*) b ) ];
+
+ /* get min number of surfaces */
+ min = ( alm->numLightSurfaces < blm->numLightSurfaces ? alm->numLightSurfaces : blm->numLightSurfaces );
+
+ /* iterate */
+ for ( i = 0; i < min; i++ )
+ {
+ /* get surface info */
+ aInfo = &surfaceInfos[ lightSurfaces[ alm->firstLightSurface + i ] ];
+ bInfo = &surfaceInfos[ lightSurfaces[ blm->firstLightSurface + i ] ];
+
+ /* compare shader names */
+ diff = strcmp( aInfo->si->shader, bInfo->si->shader );
+ if ( diff != 0 ) {
+ return diff;
+ }
+ }
+
+ /* test style count */
+ diff = 0;
+ for ( i = 0; i < MAX_LIGHTMAPS; i++ )
+ diff += blm->styles[ i ] - alm->styles[ i ];
+ if ( diff ) {
+ return diff;
+ }
+
+ /* compare size */
+ diff = ( blm->w * blm->h ) - ( alm->w * alm->h );
+ if ( diff != 0 ) {
+ return diff;
+ }
+
+ /* must be equivalent */
+ return 0;
+}
+
+
+
+void FillOutLightmap( outLightmap_t *olm ){
+ int x, y;
+ int ofs;
+ vec3_t dir_sum, light_sum;
+ int cnt, filled;
+ byte *lightBitsNew = NULL;
+ byte *lightBytesNew = NULL;
+ byte *dirBytesNew = NULL;
+
+ lightBitsNew = safe_malloc( ( olm->customWidth * olm->customHeight + 8 ) / 8 );
+ lightBytesNew = safe_malloc( olm->customWidth * olm->customHeight * 3 );
+ if ( deluxemap ) {
+ dirBytesNew = safe_malloc( olm->customWidth * olm->customHeight * 3 );
+ }
+
+ /*
+ memset(olm->lightBits, 0, (olm->customWidth * olm->customHeight + 8) / 8);
+ olm->lightBits[0] |= 1;
+ olm->lightBits[(10 * olm->customWidth + 30) >> 3] |= 1 << ((10 * olm->customWidth + 30) & 7);
+ memset(olm->bspLightBytes, 0, olm->customWidth * olm->customHeight * 3);
+ olm->bspLightBytes[0] = 255;
+ olm->bspLightBytes[(10 * olm->customWidth + 30) * 3 + 2] = 255;
+ */
+
+ memcpy( lightBitsNew, olm->lightBits, ( olm->customWidth * olm->customHeight + 8 ) / 8 );
+ memcpy( lightBytesNew, olm->bspLightBytes, olm->customWidth * olm->customHeight * 3 );
+ if ( deluxemap ) {
+ memcpy( dirBytesNew, olm->bspDirBytes, olm->customWidth * olm->customHeight * 3 );
+ }
+
+ for (;; )
+ {
+ filled = 0;
+ for ( y = 0; y < olm->customHeight; ++y )
+ {
+ for ( x = 0; x < olm->customWidth; ++x )
+ {
+ ofs = y * olm->customWidth + x;
+ if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
+ continue;
+ }
+ cnt = 0;
+ VectorClear( dir_sum );
+ VectorClear( light_sum );
+
+ /* try all four neighbors */
+ ofs = ( ( y + olm->customHeight - 1 ) % olm->customHeight ) * olm->customWidth + x;
+ if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
+ ++cnt;
+ VectorAdd( light_sum, olm->bspLightBytes + ofs * 3, light_sum );
+ if ( deluxemap ) {
+ VectorAdd( dir_sum, olm->bspDirBytes + ofs * 3, dir_sum );
+ }
+ }
+
+ ofs = ( ( y + 1 ) % olm->customHeight ) * olm->customWidth + x;
+ if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
+ ++cnt;
+ VectorAdd( light_sum, olm->bspLightBytes + ofs * 3, light_sum );
+ if ( deluxemap ) {
+ VectorAdd( dir_sum, olm->bspDirBytes + ofs * 3, dir_sum );
+ }
+ }
+
+ ofs = y * olm->customWidth + ( x + olm->customWidth - 1 ) % olm->customWidth;
+ if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
+ ++cnt;
+ VectorAdd( light_sum, olm->bspLightBytes + ofs * 3, light_sum );
+ if ( deluxemap ) {
+ VectorAdd( dir_sum, olm->bspDirBytes + ofs * 3, dir_sum );
+ }
+ }
+
+ ofs = y * olm->customWidth + ( x + 1 ) % olm->customWidth;
+ if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
+ ++cnt;
+ VectorAdd( light_sum, olm->bspLightBytes + ofs * 3, light_sum );
+ if ( deluxemap ) {
+ VectorAdd( dir_sum, olm->bspDirBytes + ofs * 3, dir_sum );
+ }
+ }
+
+ if ( cnt ) {
+ ++filled;
+ ofs = y * olm->customWidth + x;
+ lightBitsNew[ofs >> 3] |= ( 1 << ( ofs & 7 ) );
+ VectorScale( light_sum, 1.0 / cnt, lightBytesNew + ofs * 3 );
+ if ( deluxemap ) {
+ VectorScale( dir_sum, 1.0 / cnt, dirBytesNew + ofs * 3 );