#include "texture.hxx"
#include "colours.h"
+
+const char *FILE_OPEN_ERROR = "Unable to open file.";
+const char *WRONG_COUNT = "Unsupported number of color channels.";
+const char *NO_TEXTURE = "No texture data resident.";
+const char *OUT_OF_MEMORY = "Out of memory.";
+
+
SGTexture::SGTexture()
: texture_id(0),
texture_data(0),
- num_colors(3)
+ num_colors(3),
+ file(0)
{
}
SGTexture::SGTexture(unsigned int width, unsigned int height)
- : texture_id(0)
+ : texture_id(0),
+ errstr("")
{
texture_data = new GLubyte[ width * height * 3 ];
}
// Resize the OpenGL window to the size of our dynamic texture
resize(texture_width, texture_height);
- // Clear the contents of the screen buffer to blue
- glClearColor(0.0, 0.0, 1.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- // Turn off texturing (don't want the torus to be texture);
- glDisable(GL_TEXTURE_2D);
+ glClearColor(0.0, 0.0, 0.0, 1.0);
}
/**
image = ImageOpen(name);
if(!image) {
+ errstr = FILE_OPEN_ERROR;
return;
}
if (image->zsize != 1) {
ImageClose(image);
+ errstr = WRONG_COUNT;
return;
}
texture_data = new GLubyte[ image->xsize * image->ysize ];
num_colors = 1;
- if (!texture_data)
+ if (!texture_data) {
+ errstr = NO_TEXTURE;
return;
+ }
lptr = texture_data;
for(y=0; y<image->ysize; y++) {
delete texture_data;
image = ImageOpen(name);
- if(!image)
+ if(!image) {
+ errstr = FILE_OPEN_ERROR;
return;
+ }
texture_width = image->xsize;
texture_height = image->ysize;
if (image->zsize != 3 && image->zsize != 4) {
ImageClose(image);
+ errstr = WRONG_COUNT;
return;
}
delete gbuf;
delete bbuf;
delete abuf;
+ errstr = OUT_OF_MEMORY;
return;
}
delete texture_data;
image = ImageOpen(name);
- if(!image)
+ if(!image) {
+ errstr = FILE_OPEN_ERROR;
return;
+ }
texture_width = image->xsize;
texture_height = image->ysize;
if (image->zsize != 3 && image->zsize != 4) {
ImageClose(image);
+ errstr = WRONG_COUNT;
return;
}
delete gbuf;
delete bbuf;
delete abuf;
+ errstr = OUT_OF_MEMORY;
return;
}
image = RawImageOpen(name);
- if(!image)
+ if(!image) {
+ errstr = FILE_OPEN_ERROR;
return;
+ }
texture_width = 256;
texture_height = 256;
texture_data = new GLubyte[ 256 * 256 * 3 ];
- if(!texture_data)
+ if(!texture_data) {
+ errstr = OUT_OF_MEMORY;
return;
+ }
ptr = texture_data;
for(y=0; y<256; y++) {
//it wouldn't make sense to write a new function ...
image = RawImageOpen(name);
- if(!image)
+ if(!image) {
+ errstr = FILE_OPEN_ERROR;
return;
+ }
texture_width = 256;
texture_height = 256;
texture_data = new GLubyte [ 256 * 256 * 3 ];
- if(!texture_data)
+ if(!texture_data) {
+ errstr = OUT_OF_MEMORY;
return;
+ }
ptr = texture_data;
for(xy=0; xy<(256*256); xy++) {
void
-SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
+SGTexture::write_texture(const char *name) {
+ SGTexture::ImageRec *image = ImageWriteOpen(name);
+
+ for (int c=0; c<num_colors; c++) {
+ GLubyte *ptr = texture_data + c;
+ for (int y=0; y<texture_height; y++) {
+ for (int x=0; x<texture_width; x++) {
+ image->tmp[x]=*ptr;
+ ptr = ptr + num_colors;
+ }
+ fwrite(image->tmp, 1, texture_width, file);
+ }
+ }
+
+ ImageClose(image);
+}
+
+
+void
+SGTexture::set_pixel(GLuint x, GLuint y, GLubyte *c)
{
- if (!texture_data)
+ if (!texture_data) {
+ errstr = NO_TEXTURE;
return;
+ }
- unsigned int pos = (x + y*texture_width)*3;
- texture_data[pos] = (unsigned char)(c[0] * 256);
- texture_data[pos+1] = (unsigned char)(c[1] * 256);
- texture_data[pos+2] = (unsigned char)(c[2] * 256);
+ unsigned int pos = (x + y*texture_width) * num_colors;
+ memcpy(texture_data+pos, c, num_colors);
}
-float *
+GLubyte *
SGTexture::get_pixel(GLuint x, GLuint y)
{
- static sgVec3 c;
+ static GLubyte c[4] = {0, 0, 0, 0};
- sgSetVec3(c, 0, 0, 0);
- if (!texture_data)
+ if (!texture_data) {
+ errstr = NO_TEXTURE;
return c;
+ }
- unsigned int pos = (x + y*texture_width)*3;
-
- sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
+ unsigned int pos = (x + y*texture_width)*num_colors;
+ memcpy(c, texture_data + pos, num_colors);
return c;
}
}
image = new SGTexture::ImageRec;
+ memset(image, 0, sizeof(SGTexture::ImageRec));
if (image == 0) {
- // fprintf(stderr, "Out of memory!\n");
+ errstr = OUT_OF_MEMORY;
return 0;
}
if ((image->file = gzopen(fileName, "rb")) == 0) {
+ errstr = FILE_OPEN_ERROR;
return 0;
}
image->tmp = new GLubyte[ image->xsize * 256 ];
if (image->tmp == 0) {
- // fprintf(stderr, "\nOut of memory!\n");
+ errstr = OUT_OF_MEMORY;
return 0;
}
image->rowStart = new unsigned[x];
image->rowSize = new int[x];
if (image->rowStart == 0 || image->rowSize == 0) {
- // fprintf(stderr, "\nOut of memory!\n");
+ errstr = OUT_OF_MEMORY;
return 0;
}
image->rleEnd = 512 + (2 * x);
void
SGTexture::ImageClose(SGTexture::ImageRec *image) {
- gzclose(image->file);
+ if (image->file) gzclose(image->file);
+ if (file) fclose(file);
delete image->tmp;
delete image;
}
-
SGTexture::ImageRec *
SGTexture::RawImageOpen(const char *fileName)
{
}
image = new SGTexture::ImageRec;
+ memset(image, 0, sizeof(SGTexture::ImageRec));
if (image == 0) {
- // fprintf(stderr, "Out of memory!\n");
+ errstr = OUT_OF_MEMORY;
return 0;
}
if ((image->file = gzopen(fileName, "rb")) == 0) {
+ errstr = FILE_OPEN_ERROR;
return 0;
}
image->tmp = new GLubyte;
if (image->tmp == 0) {
- // fprintf(stderr, "\nOut of memory!\n");
+ errstr = OUT_OF_MEMORY;
+ return 0;
+ }
+
+ return image;
+}
+
+SGTexture::ImageRec *
+SGTexture::ImageWriteOpen(const char *fileName)
+{
+ union {
+ int testWord;
+ char testByte[4];
+ } endianTest;
+ ImageRec* image;
+ int swapFlag;
+ int x;
+
+ endianTest.testWord = 1;
+ if (endianTest.testByte[0] == 1) {
+ swapFlag = 1;
+ } else {
+ swapFlag = 0;
+ }
+
+ image = new SGTexture::ImageRec;
+ memset(image, 0, sizeof(SGTexture::ImageRec));
+ if (image == 0) {
+ errstr = OUT_OF_MEMORY;
+ return 0;
+ }
+ if ((file = fopen(fileName, "wb")) == 0) {
+ errstr = FILE_OPEN_ERROR;
+ return 0;
+ }
+
+ image->imagic = 474;
+ image->type = 0x0001;
+ image->dim = (num_colors > 1) ? 3 : 2;
+ image->xsize = texture_width;
+ image->ysize = texture_height;
+ image->zsize = num_colors;
+
+ fwrite(image, 1, 12, file);
+
+ fseek(file, 512, SEEK_SET);
+
+ if (swapFlag) {
+ ConvertShort(&image->imagic, 6);
+ }
+
+ image->tmp = new GLubyte[ image->xsize * 256 ];
+ if (image->tmp == 0) {
+ errstr = OUT_OF_MEMORY;
return 0;
}
+ if ((image->type & 0xFF00) == 0x0100) {
+ x = image->ysize * image->zsize * (int) sizeof(unsigned);
+ image->rowStart = new unsigned[x];
+ image->rowSize = new int[x];
+ if (image->rowStart == 0 || image->rowSize == 0) {
+ errstr = OUT_OF_MEMORY;
+ return 0;
+ }
+ image->rleEnd = 512 + (2 * x);
+ 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));
+ }
+ }
+
return image;
+
}
void
pixel = *iPtr++;
count = (int)(pixel & 0x7F);
if (!count) {
+ errstr = WRONG_COUNT;
return;
}
if (pixel & 0x80) {
}
}
+void
+SGTexture::ImagePutRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
+ GLubyte *iPtr, *oPtr, pixel;
+ int count;
+
+ if ((image->type & 0xFF00) == 0x0100) {
+ fseek(file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
+ fread( image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
+ file);
+
+ iPtr = image->tmp;
+ oPtr = buf;
+ for (;;) {
+ pixel = *iPtr++;
+ count = (int)(pixel & 0x7F);
+ if (!count) {
+ errstr = WRONG_COUNT;
+ return;
+ }
+ if (pixel & 0x80) {
+ while (count--) {
+ *oPtr++ = *iPtr++;
+ }
+ } else {
+ pixel = *iPtr++;
+ while (count--) {
+ *oPtr++ = pixel;
+ }
+ }
+ }
+ } else {
+ fseek(file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
+ SEEK_SET);
+ fread(buf, 1, image->xsize, file);
+ }
+}
+
+
void
SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
while(n--) {
}
}
+
+void
+SGTexture::make_monochrome(GLubyte r, GLubyte g, GLubyte b) {
+
+ if (num_colors >= 3)
+ return;
+
+ GLubyte ap[3];
+ for (int y=0; y<texture_height; y++)
+ for (int x=0; x<texture_width; x++)
+ {
+ GLubyte *rgb = get_pixel(x,y);
+ GLubyte avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
+
+ ap[0] = avg*r/255;
+ ap[1] = avg*g/255;
+ ap[2] = avg*b/255;
+
+ 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;
+}