2 * Texture manipulation routines
4 * Copyright (c) Mark J. Kilgard, 1997.
5 * Code added in april 2003 by Erik Hofman
7 * This program is freely distributable without licensing fees
8 * and is provided without guarantee or warrantee expressed or
9 * implied. This program is -not- in the public domain.
14 #include <simgear/compiler.h>
24 #include "texture.hxx"
28 const char *FILE_OPEN_ERROR = "Unable to open file.";
29 const char *WRONG_COUNT = "Unsupported number of color channels.";
30 const char *NO_TEXTURE = "No texture data resident.";
31 const char *OUT_OF_MEMORY = "Out of memory.";
34 SGTexture::SGTexture()
41 SGTexture::SGTexture(unsigned int width, unsigned int height)
45 texture_data = new GLubyte[ width * height * 3 ];
48 SGTexture::~SGTexture()
54 if ( texture_id != 0 ) {
65 glGenTextures(1, &texture_id);
67 #elif GL_EXT_texture_object
68 glGenTexturesEXT(1, &texture_id);
74 glBindTexture(GL_TEXTURE_2D, texture_id);
76 #elif GL_EXT_texture_object
77 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
81 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
82 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
83 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
84 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
89 * A function to resize the OpenGL window which will be used by
90 * the dynamic texture generating routines.
92 * @param width The width of the new window
93 * @param height The height of the new window
96 SGTexture::resize(unsigned int width, unsigned int height)
100 // Make sure that we don't get a divide by zero exception
104 // Set the viewport for the OpenGL window
105 glViewport(0, 0, width, height);
107 // Calculate the aspect ratio of the window
108 aspect = width/height;
110 // Go to the projection matrix, this gets modified by the perspective
112 glMatrixMode(GL_PROJECTION);
115 // Do the perspective calculations
116 gluPerspective(45.0, aspect, 1.0, 400.0);
118 // Return to the modelview matrix
119 glMatrixMode(GL_MODELVIEW);
123 * A function to prepare the OpenGL state machine for dynamic
124 * texture generation.
126 * @param width The width of the texture
127 * @param height The height of the texture
130 SGTexture::prepare(unsigned int width, unsigned int height) {
132 texture_width = width;
133 texture_height = height;
135 // Resize the OpenGL window to the size of our dynamic texture
136 resize(texture_width, texture_height);
138 glClearColor(0.0, 0.0, 0.0, 1.0);
142 * A function to generate the dynamic texture.
144 * The actual texture can be accessed by calling get_texture()
146 * @param width The width of the previous OpenGL window
147 * @param height The height of the previous OpenGL window
150 SGTexture::finish(unsigned int width, unsigned int height) {
151 // If a texture hasn't been created then it gets created, and the contents
152 // of the frame buffer gets copied into it. If the texture has already been
153 // created then its contents just get updated.
157 // Copies the contents of the frame buffer into the texture
158 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
159 texture_width, texture_height, 0);
162 // Copies the contents of the frame buffer into the texture
163 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
164 texture_width, texture_height);
167 // Set the OpenGL window back to its previous size
168 resize(width, height);
170 // Clear the window back to black
171 glClearColor(0.0, 0.0, 0.0, 1.0);
172 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
177 SGTexture::read_alpha_texture(const char *name)
180 SGTexture::ImageRec *image;
186 image = ImageOpen(name);
188 errstr = FILE_OPEN_ERROR;
192 texture_width = image->xsize;
193 texture_height = image->ysize;
195 // printf("image->zsize = %d\n", image->zsize);
197 if (image->zsize != 1) {
199 errstr = WRONG_COUNT;
203 texture_data = new GLubyte[ image->xsize * image->ysize ];
211 for(y=0; y<image->ysize; y++) {
212 ImageGetRow(image,lptr,y,0);
213 lptr += image->xsize;
219 SGTexture::read_rgb_texture(const char *name)
222 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
223 SGTexture::ImageRec *image;
229 image = ImageOpen(name);
231 errstr = FILE_OPEN_ERROR;
235 texture_width = image->xsize;
236 texture_height = image->ysize;
237 if (image->zsize != 3 && image->zsize != 4) {
239 errstr = WRONG_COUNT;
243 texture_data = new GLubyte[ image->xsize * image->ysize * 3 ];
245 rbuf = new GLubyte[ image->xsize ];
246 gbuf = new GLubyte[ image->xsize ];
247 bbuf = new GLubyte[ image->xsize ];
248 abuf = new GLubyte[ image->xsize ];
249 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
255 errstr = OUT_OF_MEMORY;
260 for(y=0; y<image->ysize; y++) {
261 if(image->zsize == 4) {
262 ImageGetRow(image,rbuf,y,0);
263 ImageGetRow(image,gbuf,y,1);
264 ImageGetRow(image,bbuf,y,2);
265 ImageGetRow(image,abuf,y,3); // discard
266 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
267 ptr += (image->xsize * 3);
269 ImageGetRow(image,rbuf,y,0);
270 ImageGetRow(image,gbuf,y,1);
271 ImageGetRow(image,bbuf,y,2);
272 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
273 ptr += (image->xsize * 3);
287 SGTexture::read_rgba_texture(const char *name)
290 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
291 SGTexture::ImageRec *image;
297 image = ImageOpen(name);
299 errstr = FILE_OPEN_ERROR;
303 texture_width = image->xsize;
304 texture_height = image->ysize;
305 if (image->zsize != 3 && image->zsize != 4) {
307 errstr = WRONG_COUNT;
311 texture_data = new GLubyte[ image->xsize * image->ysize * 4 ];
313 rbuf = new GLubyte[ image->xsize ];
314 gbuf = new GLubyte[ image->xsize ];
315 bbuf = new GLubyte[ image->xsize ];
316 abuf = new GLubyte[ image->xsize ];
317 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
323 errstr = OUT_OF_MEMORY;
328 memset(abuf, 255, image->xsize);
329 for(y=0; y<image->ysize; y++) {
330 if(image->zsize == 4) {
331 ImageGetRow(image,rbuf,y,0);
332 ImageGetRow(image,gbuf,y,1);
333 ImageGetRow(image,bbuf,y,2);
334 ImageGetRow(image,abuf,y,3);
335 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
336 ptr += (image->xsize * 4);
338 ImageGetRow(image,rbuf,y,0);
339 ImageGetRow(image,gbuf,y,1);
340 ImageGetRow(image,bbuf,y,2);
341 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
342 ptr += (image->xsize * 3);
354 SGTexture::read_raw_texture(const char *name)
357 SGTexture::ImageRec *image;
363 image = RawImageOpen(name);
366 errstr = FILE_OPEN_ERROR;
371 texture_height = 256;
373 texture_data = new GLubyte[ 256 * 256 * 3 ];
375 errstr = OUT_OF_MEMORY;
380 for(y=0; y<256; y++) {
381 gzread(image->gzfile, ptr, 256*3);
388 SGTexture::read_r8_texture(const char *name)
392 SGTexture::ImageRec *image;
398 //it wouldn't make sense to write a new function ...
399 image = RawImageOpen(name);
402 errstr = FILE_OPEN_ERROR;
407 texture_height = 256;
409 texture_data = new GLubyte [ 256 * 256 * 3 ];
411 errstr = OUT_OF_MEMORY;
416 for(xy=0; xy<(256*256); xy++) {
417 gzread(image->gzfile, c, 1);
419 //look in the table for the right colours
420 ptr[0]=msfs_colour[c[0]][0];
421 ptr[1]=msfs_colour[c[0]][1];
422 ptr[2]=msfs_colour[c[0]][2];
431 SGTexture::write_texture(const char *name) {
432 SGTexture::ImageRec *image = ImageWriteOpen(name);
434 for (int c=0; c<num_colors; c++) {
435 GLubyte *ptr = texture_data + c;
436 for (int y=0; y<texture_height; y++) {
437 for (int x=0; x<texture_width; x++) {
439 ptr = ptr + num_colors;
441 fwrite(image->tmp, 1, texture_width, image->file);
450 SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
457 unsigned int pos = (x + y*texture_width)*3;
458 texture_data[pos] = (unsigned char)(c[0] * 256);
459 texture_data[pos+1] = (unsigned char)(c[1] * 256);
460 texture_data[pos+2] = (unsigned char)(c[2] * 256);
465 SGTexture::get_pixel(GLuint x, GLuint y)
469 sgSetVec3(c, 0, 0, 0);
475 unsigned int pos = (x + y*texture_width)*3;
477 sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
482 SGTexture::ImageRec *
483 SGTexture::ImageOpen(const char *fileName)
490 SGTexture::ImageRec *image;
494 endianTest.testWord = 1;
495 if (endianTest.testByte[0] == 1) {
501 image = new SGTexture::ImageRec;
502 memset(image, 0, sizeof(SGTexture::ImageRec));
504 errstr = OUT_OF_MEMORY;
507 if ((image->gzfile = gzopen(fileName, "rb")) == 0) {
508 errstr = FILE_OPEN_ERROR;
512 gzread(image->gzfile, image, 12);
515 ConvertShort(&image->imagic, 6);
518 image->tmp = new GLubyte[ image->xsize * 256 ];
519 if (image->tmp == 0) {
520 errstr = OUT_OF_MEMORY;
524 if ((image->type & 0xFF00) == 0x0100) {
525 x = image->ysize * image->zsize * (int) sizeof(unsigned);
526 image->rowStart = new unsigned[x];
527 image->rowSize = new int[x];
528 if (image->rowStart == 0 || image->rowSize == 0) {
529 errstr = OUT_OF_MEMORY;
532 image->rleEnd = 512 + (2 * x);
533 gzseek(image->gzfile, 512, SEEK_SET);
534 gzread(image->gzfile, image->rowStart, x);
535 gzread(image->gzfile, image->rowSize, x);
537 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
538 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
546 SGTexture::ImageClose(SGTexture::ImageRec *image) {
547 if (image->gzfile) gzclose(image->gzfile);
548 if (image->file) fclose(image->file);
553 SGTexture::ImageRec *
554 SGTexture::RawImageOpen(const char *fileName)
561 SGTexture::ImageRec *image;
564 endianTest.testWord = 1;
565 if (endianTest.testByte[0] == 1) {
571 image = new SGTexture::ImageRec;
572 memset(image, 0, sizeof(SGTexture::ImageRec));
574 errstr = OUT_OF_MEMORY;
577 if ((image->gzfile = gzopen(fileName, "rb")) == 0) {
578 errstr = FILE_OPEN_ERROR;
582 gzread(image->gzfile, image, 12);
585 ConvertShort(&image->imagic, 6);
589 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
590 image->tmp = new GLubyte;
592 if (image->tmp == 0) {
593 errstr = OUT_OF_MEMORY;
600 SGTexture::ImageRec *
601 SGTexture::ImageWriteOpen(const char *fileName)
611 endianTest.testWord = 1;
612 if (endianTest.testByte[0] == 1) {
618 image = new SGTexture::ImageRec;
619 memset(image, 0, sizeof(SGTexture::ImageRec));
621 errstr = OUT_OF_MEMORY;
624 if ((image->file = fopen(fileName, "wb")) == 0) {
625 errstr = FILE_OPEN_ERROR;
630 image->type = 0x0001;
631 image->dim = (num_colors > 1) ? 3 : 2;
632 image->xsize = texture_width;
633 image->ysize = texture_height;
634 image->zsize = num_colors;
636 fwrite(image, 1, 12, image->file);
638 fseek(image->file, 512, SEEK_SET);
641 ConvertShort(&image->imagic, 6);
644 image->tmp = new GLubyte[ image->xsize * 256 ];
645 if (image->tmp == 0) {
646 errstr = OUT_OF_MEMORY;
650 if ((image->type & 0xFF00) == 0x0100) {
651 x = image->ysize * image->zsize * (int) sizeof(unsigned);
652 image->rowStart = new unsigned[x];
653 image->rowSize = new int[x];
654 if (image->rowStart == 0 || image->rowSize == 0) {
655 errstr = OUT_OF_MEMORY;
658 image->rleEnd = 512 + (2 * x);
659 fseek(image->file, 512, SEEK_SET);
660 fwrite(image->rowStart, 1, x, image->file);
661 fwrite(image->rowSize, 1, x, image->file);
663 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
664 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
673 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
674 GLubyte *iPtr, *oPtr, pixel;
677 if ((image->type & 0xFF00) == 0x0100) {
678 gzseek(image->gzfile, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
679 gzread(image->gzfile, image->tmp,
680 (unsigned int)image->rowSize[y+z*image->ysize]);
686 count = (int)(pixel & 0x7F);
688 errstr = WRONG_COUNT;
703 gzseek(image->gzfile, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
705 gzread(image->gzfile, buf, image->xsize);
710 SGTexture::ImagePutRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
711 GLubyte *iPtr, *oPtr, pixel;
714 if ((image->type & 0xFF00) == 0x0100) {
715 fseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
716 fread( image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
723 count = (int)(pixel & 0x7F);
725 errstr = WRONG_COUNT;
740 fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
742 fread(buf, 1, image->xsize, image->file);
748 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
753 l += 3; r++; g++; b++;
758 SGTexture::rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
765 l += 4; r++; g++; b++; a++;
771 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
772 unsigned short b1, b2;
775 ptr = (unsigned char *)array;
779 *array++ = (b1 << 8) | (b2);
785 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
786 unsigned int b1, b2, b3, b4;
789 ptr = (unsigned char *)array;
795 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);