SGTexture::SGTexture()
: texture_id(0),
texture_data(0),
- num_colors(3)
+ num_colors(3),
+ file(0)
{
}
ptr = texture_data;
for(y=0; y<256; y++) {
- gzread(image->gzfile, ptr, 256*3);
+ gzread(image->file, ptr, 256*3);
ptr+=256*3;
}
ImageClose(image);
ptr = texture_data;
for(xy=0; xy<(256*256); xy++) {
- gzread(image->gzfile, c, 1);
+ gzread(image->file, c, 1);
//look in the table for the right colours
ptr[0]=msfs_colour[c[0]][0];
image->tmp[x]=*ptr;
ptr = ptr + num_colors;
}
- fwrite(image->tmp, 1, texture_width, image->file);
+ fwrite(image->tmp, 1, texture_width, file);
}
}
errstr = OUT_OF_MEMORY;
return 0;
}
- if ((image->gzfile = gzopen(fileName, "rb")) == 0) {
+ if ((image->file = gzopen(fileName, "rb")) == 0) {
errstr = FILE_OPEN_ERROR;
return 0;
}
- gzread(image->gzfile, image, 12);
+ gzread(image->file, image, 12);
if (swapFlag) {
ConvertShort(&image->imagic, 6);
return 0;
}
image->rleEnd = 512 + (2 * x);
- gzseek(image->gzfile, 512, SEEK_SET);
- gzread(image->gzfile, image->rowStart, x);
- gzread(image->gzfile, image->rowSize, x);
+ gzseek(image->file, 512, SEEK_SET);
+ gzread(image->file, image->rowStart, x);
+ gzread(image->file, image->rowSize, x);
if (swapFlag) {
ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
void
SGTexture::ImageClose(SGTexture::ImageRec *image) {
- if (image->gzfile) gzclose(image->gzfile);
- if (image->file) fclose(image->file);
+ if (image->file) gzclose(image->file);
+ if (file) fclose(file);
delete image->tmp;
delete image;
}
errstr = OUT_OF_MEMORY;
return 0;
}
- if ((image->gzfile = gzopen(fileName, "rb")) == 0) {
+ if ((image->file = gzopen(fileName, "rb")) == 0) {
errstr = FILE_OPEN_ERROR;
return 0;
}
- gzread(image->gzfile, image, 12);
+ gzread(image->file, image, 12);
if (swapFlag) {
ConvertShort(&image->imagic, 6);
errstr = OUT_OF_MEMORY;
return 0;
}
- if ((image->file = fopen(fileName, "wb")) == 0) {
+ if ((file = fopen(fileName, "wb")) == 0) {
errstr = FILE_OPEN_ERROR;
return 0;
}
image->ysize = texture_height;
image->zsize = num_colors;
- fwrite(image, 1, 12, image->file);
+ fwrite(image, 1, 12, file);
- fseek(image->file, 512, SEEK_SET);
+ fseek(file, 512, SEEK_SET);
if (swapFlag) {
ConvertShort(&image->imagic, 6);
return 0;
}
image->rleEnd = 512 + (2 * x);
- fseek(image->file, 512, SEEK_SET);
- fwrite(image->rowStart, 1, x, image->file);
- fwrite(image->rowSize, 1, x, image->file);
+ fseek(file, 512, SEEK_SET);
+ fwrite(image->rowStart, 1, x, file);
+ fwrite(image->rowSize, 1, x, file);
if (swapFlag) {
ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
int count;
if ((image->type & 0xFF00) == 0x0100) {
- gzseek(image->gzfile, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
- gzread(image->gzfile, image->tmp,
+ gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
+ gzread(image->file, image->tmp,
(unsigned int)image->rowSize[y+z*image->ysize]);
iPtr = image->tmp;
}
}
} else {
- gzseek(image->gzfile, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
+ gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
SEEK_SET);
- gzread(image->gzfile, buf, image->xsize);
+ gzread(image->file, buf, image->xsize);
}
}
int count;
if ((image->type & 0xFF00) == 0x0100) {
- fseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
+ fseek(file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
fread( image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
- image->file);
+ file);
iPtr = image->tmp;
oPtr = buf;
}
}
} else {
- fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
+ fseek(file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
SEEK_SET);
- fread(buf, 1, image->xsize, image->file);
+ fread(buf, 1, image->xsize, file);
}
}
set_pixel(x,y,ap);
}
}
+
+void
+SGTexture::make_normalmap(float brightness) {
+ GLubyte *map = (GLubyte *)malloc (texture_width * texture_height * 3);
+
+ for (int y=0; y<texture_height; y++)
+ for (int x=0; x<texture_width; x++)
+ {
+ int mpos = (x + y*texture_width)*3;
+ int dpos = (x + y*texture_width)*num_colors;
+
+ int xp1 = (x < (texture_width-1)) ? x+1 : 0;
+ int yp1 = (y < (texture_height-1)) ? y+1 : 0;
+ int posxp1 = (xp1 + y*texture_width)*num_colors;
+ int posyp1 = (x + yp1*texture_width)*num_colors;
+
+ map[mpos+0] = (128+(texture_data[posxp1]-texture_data[dpos])/2);
+ map[mpos+1] = (128+(texture_data[posyp1]-texture_data[dpos])/2);
+ map[mpos+2] = 128 + GLubyte(128*brightness);
+ }
+
+ free (texture_data);
+ texture_data = map;
+ num_colors = 3;
+}