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>
21 #include "texture.hxx"
24 SGTexture::SGTexture()
31 SGTexture::SGTexture(unsigned int width, unsigned int height)
34 texture_data = new GLubyte[ width * height * 3 ];
37 SGTexture::~SGTexture()
43 if ( texture_id != 0 ) {
54 glGenTextures(1, &texture_id);
56 #elif GL_EXT_texture_object
57 glGenTexturesEXT(1, &texture_id);
63 glBindTexture(GL_TEXTURE_2D, texture_id);
65 #elif GL_EXT_texture_object
66 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
70 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
71 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
72 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
73 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
78 * A function to resize the OpenGL window which will be used by
79 * the dynamic texture generating routines.
81 * @param width The width of the new window
82 * @param height The height of the new window
85 SGTexture::resize(unsigned int width, unsigned int height)
89 // Make sure that we don't get a divide by zero exception
93 // Set the viewport for the OpenGL window
94 glViewport(0, 0, width, height);
96 // Calculate the aspect ratio of the window
97 aspect = width/height;
99 // Go to the projection matrix, this gets modified by the perspective
101 glMatrixMode(GL_PROJECTION);
104 // Do the perspective calculations
105 gluPerspective(45.0, aspect, 1.0, 400.0);
107 // Return to the modelview matrix
108 glMatrixMode(GL_MODELVIEW);
112 * A function to prepare the OpenGL state machine for dynamic
113 * texture generation.
115 * @param width The width of the texture
116 * @param height The height of the texture
119 SGTexture::prepare(unsigned int width, unsigned int height) {
121 texture_width = width;
122 texture_height = height;
124 // Resize the OpenGL window to the size of our dynamic texture
125 resize(texture_width, texture_height);
127 // Clear the contents of the screen buffer to blue
128 glClearColor(0.0, 0.0, 1.0, 1.0);
129 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
131 // Turn off texturing (don't want the torus to be texture);
132 glDisable(GL_TEXTURE_2D);
136 * A function to generate the dynamic texture.
138 * The actual texture can be accessed by calling get_texture()
140 * @param width The width of the previous OpenGL window
141 * @param height The height of the previous OpenGL window
144 SGTexture::finish(unsigned int width, unsigned int height) {
145 // If a texture hasn't been created then it gets created, and the contents
146 // of the frame buffer gets copied into it. If the texture has already been
147 // created then its contents just get updated.
151 // Copies the contents of the frame buffer into the texture
152 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
153 texture_width, texture_height, 0);
156 // Copies the contents of the frame buffer into the texture
157 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
158 texture_width, texture_height);
161 // Set the OpenGL window back to its previous size
162 resize(width, height);
164 // Clear the window back to black
165 glClearColor(0.0, 0.0, 0.0, 1.0);
166 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
171 SGTexture::read_alpha_texture(const char *name)
174 SGTexture::ImageRec *image;
180 image = ImageOpen(name);
185 texture_width = image->xsize;
186 texture_height = image->ysize;
188 // printf("image->zsize = %d\n", image->zsize);
190 if (image->zsize != 1) {
195 texture_data = new GLubyte[ image->xsize * image->ysize ];
200 for(y=0; y<image->ysize; y++) {
201 ImageGetRow(image,lptr,y,0);
202 lptr += image->xsize;
208 SGTexture::read_rgb_texture(const char *name)
211 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
212 SGTexture::ImageRec *image;
218 image = ImageOpen(name);
222 texture_width = image->xsize;
223 texture_height = image->ysize;
224 if (image->zsize != 3 && image->zsize != 4) {
229 texture_data = new GLubyte[ image->xsize * image->ysize * 3 ];
230 rbuf = new GLubyte[ image->xsize ];
231 gbuf = new GLubyte[ image->xsize ];
232 bbuf = new GLubyte[ image->xsize ];
233 abuf = new GLubyte[ image->xsize ];
234 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
244 for(y=0; y<image->ysize; y++) {
245 if(image->zsize == 4) {
246 ImageGetRow(image,rbuf,y,0);
247 ImageGetRow(image,gbuf,y,1);
248 ImageGetRow(image,bbuf,y,2);
249 ImageGetRow(image,abuf,y,3); // discard
250 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
251 ptr += (image->xsize * 3);
253 ImageGetRow(image,rbuf,y,0);
254 ImageGetRow(image,gbuf,y,1);
255 ImageGetRow(image,bbuf,y,2);
256 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
257 ptr += (image->xsize * 3);
271 SGTexture::read_rgba_texture(const char *name)
274 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
275 SGTexture::ImageRec *image;
281 image = ImageOpen(name);
285 texture_width = image->xsize;
286 texture_height = image->ysize;
287 if (image->zsize != 3 && image->zsize != 4) {
292 texture_data = new GLubyte[ image->xsize * image->ysize * 4 ];
293 rbuf = new GLubyte[ image->xsize ];
294 gbuf = new GLubyte[ image->xsize ];
295 bbuf = new GLubyte[ image->xsize ];
296 abuf = new GLubyte[ image->xsize ];
297 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
307 memset(abuf, 255, image->xsize);
308 for(y=0; y<image->ysize; y++) {
309 if(image->zsize == 4) {
310 ImageGetRow(image,rbuf,y,0);
311 ImageGetRow(image,gbuf,y,1);
312 ImageGetRow(image,bbuf,y,2);
313 ImageGetRow(image,abuf,y,3);
314 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
315 ptr += (image->xsize * 4);
317 ImageGetRow(image,rbuf,y,0);
318 ImageGetRow(image,gbuf,y,1);
319 ImageGetRow(image,bbuf,y,2);
320 rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize);
321 ptr += (image->xsize * 3);
333 SGTexture::read_raw_texture(const char *name)
336 SGTexture::ImageRec *image;
342 image = RawImageOpen(name);
348 texture_height = 256;
350 texture_data = new GLubyte[ 256 * 256 * 3 ];
355 for(y=0; y<256; y++) {
356 gzread(image->file, ptr, 256*3);
363 SGTexture::read_r8_texture(const char *name)
367 SGTexture::ImageRec *image;
373 //it wouldn't make sense to write a new function ...
374 image = RawImageOpen(name);
380 texture_height = 256;
382 texture_data = new GLubyte [ 256 * 256 * 3 ];
387 for(xy=0; xy<(256*256); xy++) {
388 gzread(image->file, c, 1);
390 //look in the table for the right colours
391 ptr[0]=msfs_colour[c[0]][0];
392 ptr[1]=msfs_colour[c[0]][1];
393 ptr[2]=msfs_colour[c[0]][2];
402 SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
407 unsigned int pos = (x + y*texture_width)*3;
408 texture_data[pos] = (unsigned char)(c[0] * 256);
409 texture_data[pos+1] = (unsigned char)(c[1] * 256);
410 texture_data[pos+2] = (unsigned char)(c[2] * 256);
415 SGTexture::get_pixel(GLuint x, GLuint y)
419 sgSetVec3(c, 0, 0, 0);
423 unsigned int pos = (x + y*texture_width)*3;
425 sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
430 SGTexture::ImageRec *
431 SGTexture::ImageOpen(const char *fileName)
438 SGTexture::ImageRec *image;
442 endianTest.testWord = 1;
443 if (endianTest.testByte[0] == 1) {
449 image = new SGTexture::ImageRec;
451 // fprintf(stderr, "Out of memory!\n");
454 if ((image->file = gzopen(fileName, "rb")) == 0) {
458 gzread(image->file, image, 12);
461 ConvertShort(&image->imagic, 6);
464 image->tmp = new GLubyte[ image->xsize * 256 ];
465 if (image->tmp == 0) {
466 // fprintf(stderr, "\nOut of memory!\n");
470 if ((image->type & 0xFF00) == 0x0100) {
471 x = image->ysize * image->zsize * (int) sizeof(unsigned);
472 image->rowStart = new unsigned[x];
473 image->rowSize = new int[x];
474 if (image->rowStart == 0 || image->rowSize == 0) {
475 // fprintf(stderr, "\nOut of memory!\n");
478 image->rleEnd = 512 + (2 * x);
479 gzseek(image->file, 512, SEEK_SET);
480 gzread(image->file, image->rowStart, x);
481 gzread(image->file, image->rowSize, x);
483 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
484 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
492 SGTexture::ImageClose(SGTexture::ImageRec *image) {
493 gzclose(image->file);
499 SGTexture::ImageRec *
500 SGTexture::RawImageOpen(const char *fileName)
507 SGTexture::ImageRec *image;
510 endianTest.testWord = 1;
511 if (endianTest.testByte[0] == 1) {
517 image = new SGTexture::ImageRec;
519 // fprintf(stderr, "Out of memory!\n");
522 if ((image->file = gzopen(fileName, "rb")) == 0) {
526 gzread(image->file, image, 12);
529 ConvertShort(&image->imagic, 6);
533 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
534 image->tmp = new GLubyte;
536 if (image->tmp == 0) {
537 // fprintf(stderr, "\nOut of memory!\n");
545 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
546 GLubyte *iPtr, *oPtr, pixel;
549 if ((image->type & 0xFF00) == 0x0100) {
550 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
551 gzread(image->file, image->tmp,
552 (unsigned int)image->rowSize[y+z*image->ysize]);
558 count = (int)(pixel & 0x7F);
574 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
576 gzread(image->file, buf, image->xsize);
581 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
586 l += 3; r++; g++; b++;
591 SGTexture::rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
598 l += 4; r++; g++; b++; a++;
604 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
605 unsigned short b1, b2;
608 ptr = (unsigned char *)array;
612 *array++ = (b1 << 8) | (b2);
618 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
619 unsigned int b1, b2, b3, b4;
622 ptr = (unsigned char *)array;
628 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);