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()
29 SGTexture::SGTexture(unsigned int width, unsigned int height)
31 texture_data = new GLubyte[ width * height * 3 ];
34 SGTexture::~SGTexture()
44 glGenTextures(1, &texture_id);
46 #elif GL_EXT_texture_object
47 glGenTexturesEXT(1, &texture_id);
52 glBindTexture(GL_TEXTURE_2D, texture_id);
54 #elif GL_EXT_texture_object
55 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
59 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
60 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
61 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
62 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
67 * A function to resize the OpenGL window which will be used by
68 * the dynamic texture generating routines.
70 * @param width The width of the new window
71 * @param height The height of the new window
74 SGTexture::resize(unsigned int width, unsigned int height)
78 // Make sure that we don't get a divide by zero exception
82 // Set the viewport for the OpenGL window
83 glViewport(0, 0, width, height);
85 // Calculate the aspect ratio of the window
86 aspect = width/height;
88 // Go to the projection matrix, this gets modified by the perspective
90 glMatrixMode(GL_PROJECTION);
93 // Do the perspective calculations
94 gluPerspective(45.0, aspect, 1.0, 400.0);
96 // Return to the modelview matrix
97 glMatrixMode(GL_MODELVIEW);
101 * A function to prepare the OpenGL state machine for dynamic
102 * texture generation.
104 * @param width The width of the texture
105 * @param height The height of the texture
108 SGTexture::prepare(unsigned int width, unsigned int height) {
110 texture_width = width;
111 texture_height = height;
113 // Resize the OpenGL window to the size of our dynamic texture
114 resize(texture_width, texture_height);
116 // Clear the contents of the screen buffer to blue
117 glClearColor(0.0, 0.0, 1.0, 1.0);
118 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120 // Turn off texturing (don't want the torus to be texture);
121 glDisable(GL_TEXTURE_2D);
125 * A function to generate the dynamic texture.
127 * The actual texture can be accessed by calling get_texture()
129 * @param width The width of the previous OpenGL window
130 * @param height The height of the previous OpenGL window
133 SGTexture::finish(unsigned int width, unsigned int height) {
134 // If a texture hasn't been created then it gets created, and the contents
135 // of the frame buffer gets copied into it. If the texture has already been
136 // created then its contents just get updated.
140 // Copies the contents of the frame buffer into the texture
141 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
142 texture_width, texture_height, 0);
145 // Copies the contents of the frame buffer into the texture
146 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
147 texture_width, texture_height);
150 // Set the OpenGL window back to its previous size
151 resize(width, height);
153 // Clear the window back to black
154 glClearColor(0.0, 0.0, 0.0, 1.0);
155 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160 SGTexture::read_alpha_texture(const char *name)
163 SGTexture::ImageRec *image;
169 image = ImageOpen(name);
174 texture_width = image->xsize;
175 texture_height = image->ysize;
177 // printf("image->zsize = %d\n", image->zsize);
179 if (image->zsize != 1) {
184 texture_data = new GLubyte[ image->xsize * image->ysize ];
189 for(y=0; y<image->ysize; y++) {
190 ImageGetRow(image,lptr,y,0);
191 lptr += image->xsize;
197 SGTexture::read_rgb_texture(const char *name)
200 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
201 SGTexture::ImageRec *image;
207 image = ImageOpen(name);
211 texture_width = image->xsize;
212 texture_height = image->ysize;
213 if (image->zsize != 3 && image->zsize != 4) {
218 texture_data = new GLubyte[ image->xsize * image->ysize * 3];
219 rbuf = new GLubyte[ image->xsize ];
220 gbuf = new GLubyte[ image->xsize ];
221 bbuf = new GLubyte[ image->xsize ];
222 abuf = new GLubyte[ image->xsize ];
223 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
233 for(y=0; y<image->ysize; y++) {
234 if(image->zsize == 4) {
235 ImageGetRow(image,rbuf,y,0);
236 ImageGetRow(image,gbuf,y,1);
237 ImageGetRow(image,bbuf,y,2);
238 ImageGetRow(image,abuf,y,3); /* Discard. */
239 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
240 ptr += (image->xsize * 3);
242 ImageGetRow(image,rbuf,y,0);
243 ImageGetRow(image,gbuf,y,1);
244 ImageGetRow(image,bbuf,y,2);
245 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
246 ptr += (image->xsize * 3);
258 SGTexture::read_raw_texture(const char *name)
261 SGTexture::ImageRec *image;
267 image = RawImageOpen(name);
273 texture_height = 256;
275 texture_data = new GLubyte[ 256 * 256 * 3 ];
280 for(y=0; y<256; y++) {
281 gzread(image->file, ptr, 256*3);
288 SGTexture::read_r8_texture(const char *name)
292 SGTexture::ImageRec *image;
298 //it wouldn't make sense to write a new function ...
299 image = RawImageOpen(name);
305 texture_height = 256;
307 texture_data = new GLubyte [ 256 * 256 * 3 ];
312 for(xy=0; xy<(256*256); xy++) {
313 gzread(image->file, c, 1);
315 //look in the table for the right colours
316 ptr[0]=msfs_colour[c[0]][0];
317 ptr[1]=msfs_colour[c[0]][1];
318 ptr[2]=msfs_colour[c[0]][2];
327 SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
329 unsigned int pos = (x + y*texture_width)*3;
330 texture_data[pos] = c[0];
331 texture_data[pos+1] = c[1];
332 texture_data[pos+2] = c[2];
337 SGTexture::get_pixel(GLuint x, GLuint y)
340 unsigned int pos = (x + y*texture_width)*3;
342 sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
348 SGTexture::ImageRec *
349 SGTexture::ImageOpen(const char *fileName)
356 SGTexture::ImageRec *image;
360 endianTest.testWord = 1;
361 if (endianTest.testByte[0] == 1) {
367 image = new SGTexture::ImageRec;
369 // fprintf(stderr, "Out of memory!\n");
372 if ((image->file = gzopen(fileName, "rb")) == 0) {
376 gzread(image->file, image, 12);
379 ConvertShort(&image->imagic, 6);
382 image->tmp = new GLubyte[ image->xsize * 256 ];
383 if (image->tmp == 0) {
384 // fprintf(stderr, "\nOut of memory!\n");
388 if ((image->type & 0xFF00) == 0x0100) {
389 x = image->ysize * image->zsize * (int) sizeof(unsigned);
390 image->rowStart = new unsigned[x];
391 image->rowSize = new int[x];
392 if (image->rowStart == 0 || image->rowSize == 0) {
393 // fprintf(stderr, "\nOut of memory!\n");
396 image->rleEnd = 512 + (2 * x);
397 gzseek(image->file, 512, SEEK_SET);
398 gzread(image->file, image->rowStart, x);
399 gzread(image->file, image->rowSize, x);
401 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
402 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
410 SGTexture::ImageClose(SGTexture::ImageRec *image) {
411 gzclose(image->file);
417 SGTexture::ImageRec *
418 SGTexture::RawImageOpen(const char *fileName)
425 SGTexture::ImageRec *image;
428 endianTest.testWord = 1;
429 if (endianTest.testByte[0] == 1) {
435 image = new SGTexture::ImageRec;
437 // fprintf(stderr, "Out of memory!\n");
440 if ((image->file = gzopen(fileName, "rb")) == 0) {
444 gzread(image->file, image, 12);
447 ConvertShort(&image->imagic, 6);
451 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
452 image->tmp = new GLubyte;
454 if (image->tmp == 0) {
455 // fprintf(stderr, "\nOut of memory!\n");
463 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
464 GLubyte *iPtr, *oPtr, pixel;
467 if ((image->type & 0xFF00) == 0x0100) {
468 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
469 gzread(image->file, image->tmp,
470 (unsigned int)image->rowSize[y+z*image->ysize]);
476 count = (int)(pixel & 0x7F);
492 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
494 gzread(image->file, buf, image->xsize);
499 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
504 l += 3; r++; g++; b++;
509 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
510 unsigned short b1, b2;
513 ptr = (unsigned char *)array;
517 *array++ = (b1 << 8) | (b2);
523 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
524 unsigned int b1, b2, b3, b4;
527 ptr = (unsigned char *)array;
533 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);