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()
30 SGTexture::SGTexture(unsigned int width, unsigned int height)
33 texture_data = new GLubyte[ width * height * 3 ];
36 SGTexture::~SGTexture()
42 if ( texture_id != 0 ) {
53 glGenTextures(1, &texture_id);
55 #elif GL_EXT_texture_object
56 glGenTexturesEXT(1, &texture_id);
62 glBindTexture(GL_TEXTURE_2D, texture_id);
64 #elif GL_EXT_texture_object
65 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
69 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
70 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
71 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
77 * A function to resize the OpenGL window which will be used by
78 * the dynamic texture generating routines.
80 * @param width The width of the new window
81 * @param height The height of the new window
84 SGTexture::resize(unsigned int width, unsigned int height)
88 // Make sure that we don't get a divide by zero exception
92 // Set the viewport for the OpenGL window
93 glViewport(0, 0, width, height);
95 // Calculate the aspect ratio of the window
96 aspect = width/height;
98 // Go to the projection matrix, this gets modified by the perspective
100 glMatrixMode(GL_PROJECTION);
103 // Do the perspective calculations
104 gluPerspective(45.0, aspect, 1.0, 400.0);
106 // Return to the modelview matrix
107 glMatrixMode(GL_MODELVIEW);
111 * A function to prepare the OpenGL state machine for dynamic
112 * texture generation.
114 * @param width The width of the texture
115 * @param height The height of the texture
118 SGTexture::prepare(unsigned int width, unsigned int height) {
120 texture_width = width;
121 texture_height = height;
123 // Resize the OpenGL window to the size of our dynamic texture
124 resize(texture_width, texture_height);
126 // Clear the contents of the screen buffer to blue
127 glClearColor(0.0, 0.0, 1.0, 1.0);
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
130 // Turn off texturing (don't want the torus to be texture);
131 glDisable(GL_TEXTURE_2D);
135 * A function to generate the dynamic texture.
137 * The actual texture can be accessed by calling get_texture()
139 * @param width The width of the previous OpenGL window
140 * @param height The height of the previous OpenGL window
143 SGTexture::finish(unsigned int width, unsigned int height) {
144 // If a texture hasn't been created then it gets created, and the contents
145 // of the frame buffer gets copied into it. If the texture has already been
146 // created then its contents just get updated.
150 // Copies the contents of the frame buffer into the texture
151 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
152 texture_width, texture_height, 0);
155 // Copies the contents of the frame buffer into the texture
156 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
157 texture_width, texture_height);
160 // Set the OpenGL window back to its previous size
161 resize(width, height);
163 // Clear the window back to black
164 glClearColor(0.0, 0.0, 0.0, 1.0);
165 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
170 SGTexture::read_alpha_texture(const char *name)
173 SGTexture::ImageRec *image;
179 image = ImageOpen(name);
184 texture_width = image->xsize;
185 texture_height = image->ysize;
187 // printf("image->zsize = %d\n", image->zsize);
189 if (image->zsize != 1) {
194 texture_data = new GLubyte[ image->xsize * image->ysize ];
199 for(y=0; y<image->ysize; y++) {
200 ImageGetRow(image,lptr,y,0);
201 lptr += image->xsize;
207 SGTexture::read_rgb_texture(const char *name)
210 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
211 SGTexture::ImageRec *image;
217 image = ImageOpen(name);
221 texture_width = image->xsize;
222 texture_height = image->ysize;
223 if (image->zsize != 3 && image->zsize != 4) {
228 texture_data = new GLubyte[ image->xsize * image->ysize * 3];
229 rbuf = new GLubyte[ image->xsize ];
230 gbuf = new GLubyte[ image->xsize ];
231 bbuf = new GLubyte[ image->xsize ];
232 abuf = new GLubyte[ image->xsize ];
233 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
243 for(y=0; y<image->ysize; y++) {
244 if(image->zsize == 4) {
245 ImageGetRow(image,rbuf,y,0);
246 ImageGetRow(image,gbuf,y,1);
247 ImageGetRow(image,bbuf,y,2);
248 ImageGetRow(image,abuf,y,3); /* Discard. */
249 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
250 ptr += (image->xsize * 3);
252 ImageGetRow(image,rbuf,y,0);
253 ImageGetRow(image,gbuf,y,1);
254 ImageGetRow(image,bbuf,y,2);
255 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
256 ptr += (image->xsize * 3);
268 SGTexture::read_raw_texture(const char *name)
271 SGTexture::ImageRec *image;
277 image = RawImageOpen(name);
283 texture_height = 256;
285 texture_data = new GLubyte[ 256 * 256 * 3 ];
290 for(y=0; y<256; y++) {
291 gzread(image->file, ptr, 256*3);
298 SGTexture::read_r8_texture(const char *name)
302 SGTexture::ImageRec *image;
308 //it wouldn't make sense to write a new function ...
309 image = RawImageOpen(name);
315 texture_height = 256;
317 texture_data = new GLubyte [ 256 * 256 * 3 ];
322 for(xy=0; xy<(256*256); xy++) {
323 gzread(image->file, c, 1);
325 //look in the table for the right colours
326 ptr[0]=msfs_colour[c[0]][0];
327 ptr[1]=msfs_colour[c[0]][1];
328 ptr[2]=msfs_colour[c[0]][2];
337 SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
342 unsigned int pos = (x + y*texture_width)*3;
343 texture_data[pos] = (unsigned char)(c[0] * 256);
344 texture_data[pos+1] = (unsigned char)(c[1] * 256);
345 texture_data[pos+2] = (unsigned char)(c[2] * 256);
350 SGTexture::get_pixel(GLuint x, GLuint y)
354 sgSetVec3(c, 0, 0, 0);
358 unsigned int pos = (x + y*texture_width)*3;
360 sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
365 SGTexture::ImageRec *
366 SGTexture::ImageOpen(const char *fileName)
373 SGTexture::ImageRec *image;
377 endianTest.testWord = 1;
378 if (endianTest.testByte[0] == 1) {
384 image = new SGTexture::ImageRec;
386 // fprintf(stderr, "Out of memory!\n");
389 if ((image->file = gzopen(fileName, "rb")) == 0) {
393 gzread(image->file, image, 12);
396 ConvertShort(&image->imagic, 6);
399 image->tmp = new GLubyte[ image->xsize * 256 ];
400 if (image->tmp == 0) {
401 // fprintf(stderr, "\nOut of memory!\n");
405 if ((image->type & 0xFF00) == 0x0100) {
406 x = image->ysize * image->zsize * (int) sizeof(unsigned);
407 image->rowStart = new unsigned[x];
408 image->rowSize = new int[x];
409 if (image->rowStart == 0 || image->rowSize == 0) {
410 // fprintf(stderr, "\nOut of memory!\n");
413 image->rleEnd = 512 + (2 * x);
414 gzseek(image->file, 512, SEEK_SET);
415 gzread(image->file, image->rowStart, x);
416 gzread(image->file, image->rowSize, x);
418 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
419 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
427 SGTexture::ImageClose(SGTexture::ImageRec *image) {
428 gzclose(image->file);
434 SGTexture::ImageRec *
435 SGTexture::RawImageOpen(const char *fileName)
442 SGTexture::ImageRec *image;
445 endianTest.testWord = 1;
446 if (endianTest.testByte[0] == 1) {
452 image = new SGTexture::ImageRec;
454 // fprintf(stderr, "Out of memory!\n");
457 if ((image->file = gzopen(fileName, "rb")) == 0) {
461 gzread(image->file, image, 12);
464 ConvertShort(&image->imagic, 6);
468 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
469 image->tmp = new GLubyte;
471 if (image->tmp == 0) {
472 // fprintf(stderr, "\nOut of memory!\n");
480 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
481 GLubyte *iPtr, *oPtr, pixel;
484 if ((image->type & 0xFF00) == 0x0100) {
485 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
486 gzread(image->file, image->tmp,
487 (unsigned int)image->rowSize[y+z*image->ysize]);
493 count = (int)(pixel & 0x7F);
509 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
511 gzread(image->file, buf, image->xsize);
516 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
521 l += 3; r++; g++; b++;
526 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
527 unsigned short b1, b2;
530 ptr = (unsigned char *)array;
534 *array++ = (b1 << 8) | (b2);
540 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
541 unsigned int b1, b2, b3, b4;
544 ptr = (unsigned char *)array;
550 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);