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()
50 glGenTextures(1, &texture_id);
52 #elif GL_EXT_texture_object
53 glGenTexturesEXT(1, &texture_id);
58 glBindTexture(GL_TEXTURE_2D, texture_id);
60 #elif GL_EXT_texture_object
61 glBindTextureEXT(GL_TEXTURE_2D, texture_id);
65 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
66 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
67 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
68 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
73 * A function to resize the OpenGL window which will be used by
74 * the dynamic texture generating routines.
76 * @param width The width of the new window
77 * @param height The height of the new window
80 SGTexture::resize(unsigned int width, unsigned int height)
84 // Make sure that we don't get a divide by zero exception
88 // Set the viewport for the OpenGL window
89 glViewport(0, 0, width, height);
91 // Calculate the aspect ratio of the window
92 aspect = width/height;
94 // Go to the projection matrix, this gets modified by the perspective
96 glMatrixMode(GL_PROJECTION);
99 // Do the perspective calculations
100 gluPerspective(45.0, aspect, 1.0, 400.0);
102 // Return to the modelview matrix
103 glMatrixMode(GL_MODELVIEW);
107 * A function to prepare the OpenGL state machine for dynamic
108 * texture generation.
110 * @param width The width of the texture
111 * @param height The height of the texture
114 SGTexture::prepare(unsigned int width, unsigned int height) {
116 texture_width = width;
117 texture_height = height;
119 // Resize the OpenGL window to the size of our dynamic texture
120 resize(texture_width, texture_height);
122 // Clear the contents of the screen buffer to blue
123 glClearColor(0.0, 0.0, 1.0, 1.0);
124 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
126 // Turn off texturing (don't want the torus to be texture);
127 glDisable(GL_TEXTURE_2D);
131 * A function to generate the dynamic texture.
133 * The actual texture can be accessed by calling get_texture()
135 * @param width The width of the previous OpenGL window
136 * @param height The height of the previous OpenGL window
139 SGTexture::finish(unsigned int width, unsigned int height) {
140 // If a texture hasn't been created then it gets created, and the contents
141 // of the frame buffer gets copied into it. If the texture has already been
142 // created then its contents just get updated.
146 // Copies the contents of the frame buffer into the texture
147 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
148 texture_width, texture_height, 0);
151 // Copies the contents of the frame buffer into the texture
152 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,
153 texture_width, texture_height);
156 // Set the OpenGL window back to its previous size
157 resize(width, height);
159 // Clear the window back to black
160 glClearColor(0.0, 0.0, 0.0, 1.0);
161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
166 SGTexture::read_alpha_texture(const char *name)
169 SGTexture::ImageRec *image;
175 image = ImageOpen(name);
180 texture_width = image->xsize;
181 texture_height = image->ysize;
183 // printf("image->zsize = %d\n", image->zsize);
185 if (image->zsize != 1) {
190 texture_data = new GLubyte[ image->xsize * image->ysize ];
195 for(y=0; y<image->ysize; y++) {
196 ImageGetRow(image,lptr,y,0);
197 lptr += image->xsize;
203 SGTexture::read_rgb_texture(const char *name)
206 GLubyte *rbuf, *gbuf, *bbuf, *abuf;
207 SGTexture::ImageRec *image;
213 image = ImageOpen(name);
217 texture_width = image->xsize;
218 texture_height = image->ysize;
219 if (image->zsize != 3 && image->zsize != 4) {
224 texture_data = new GLubyte[ image->xsize * image->ysize * 3];
225 rbuf = new GLubyte[ image->xsize ];
226 gbuf = new GLubyte[ image->xsize ];
227 bbuf = new GLubyte[ image->xsize ];
228 abuf = new GLubyte[ image->xsize ];
229 if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) {
239 for(y=0; y<image->ysize; y++) {
240 if(image->zsize == 4) {
241 ImageGetRow(image,rbuf,y,0);
242 ImageGetRow(image,gbuf,y,1);
243 ImageGetRow(image,bbuf,y,2);
244 ImageGetRow(image,abuf,y,3); /* Discard. */
245 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
246 ptr += (image->xsize * 3);
248 ImageGetRow(image,rbuf,y,0);
249 ImageGetRow(image,gbuf,y,1);
250 ImageGetRow(image,bbuf,y,2);
251 rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize);
252 ptr += (image->xsize * 3);
264 SGTexture::read_raw_texture(const char *name)
267 SGTexture::ImageRec *image;
273 image = RawImageOpen(name);
279 texture_height = 256;
281 texture_data = new GLubyte[ 256 * 256 * 3 ];
286 for(y=0; y<256; y++) {
287 gzread(image->file, ptr, 256*3);
294 SGTexture::read_r8_texture(const char *name)
298 SGTexture::ImageRec *image;
304 //it wouldn't make sense to write a new function ...
305 image = RawImageOpen(name);
311 texture_height = 256;
313 texture_data = new GLubyte [ 256 * 256 * 3 ];
318 for(xy=0; xy<(256*256); xy++) {
319 gzread(image->file, c, 1);
321 //look in the table for the right colours
322 ptr[0]=msfs_colour[c[0]][0];
323 ptr[1]=msfs_colour[c[0]][1];
324 ptr[2]=msfs_colour[c[0]][2];
334 SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
336 unsigned int pos = (x + y*texture_width)*3;
337 texture_data[pos] = c[0];
338 texture_data[pos+1] = c[1];
339 texture_data[pos+2] = c[2];
344 SGTexture::get_pixel(GLuint x, GLuint y)
347 unsigned int pos = (x + y*texture_width)*3;
349 sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
356 SGTexture::ImageRec *
357 SGTexture::ImageOpen(const char *fileName)
364 SGTexture::ImageRec *image;
368 endianTest.testWord = 1;
369 if (endianTest.testByte[0] == 1) {
375 image = new SGTexture::ImageRec;
377 // fprintf(stderr, "Out of memory!\n");
380 if ((image->file = gzopen(fileName, "rb")) == 0) {
384 gzread(image->file, image, 12);
387 ConvertShort(&image->imagic, 6);
390 image->tmp = new GLubyte[ image->xsize * 256 ];
391 if (image->tmp == 0) {
392 // fprintf(stderr, "\nOut of memory!\n");
396 if ((image->type & 0xFF00) == 0x0100) {
397 x = image->ysize * image->zsize * (int) sizeof(unsigned);
398 image->rowStart = new unsigned[x];
399 image->rowSize = new int[x];
400 if (image->rowStart == 0 || image->rowSize == 0) {
401 // fprintf(stderr, "\nOut of memory!\n");
404 image->rleEnd = 512 + (2 * x);
405 gzseek(image->file, 512, SEEK_SET);
406 gzread(image->file, image->rowStart, x);
407 gzread(image->file, image->rowSize, x);
409 ConvertUint(image->rowStart, x/(int) sizeof(unsigned));
410 ConvertUint((unsigned *)image->rowSize, x/(int) sizeof(int));
418 SGTexture::ImageClose(SGTexture::ImageRec *image) {
419 gzclose(image->file);
425 SGTexture::ImageRec *
426 SGTexture::RawImageOpen(const char *fileName)
433 SGTexture::ImageRec *image;
436 endianTest.testWord = 1;
437 if (endianTest.testByte[0] == 1) {
443 image = new SGTexture::ImageRec;
445 // fprintf(stderr, "Out of memory!\n");
448 if ((image->file = gzopen(fileName, "rb")) == 0) {
452 gzread(image->file, image, 12);
455 ConvertShort(&image->imagic, 6);
459 //just allocate a pseudo value as I'm too lazy to change ImageClose()...
460 image->tmp = new GLubyte;
462 if (image->tmp == 0) {
463 // fprintf(stderr, "\nOut of memory!\n");
471 SGTexture::ImageGetRow(SGTexture::ImageRec *image, GLubyte *buf, int y, int z) {
472 GLubyte *iPtr, *oPtr, pixel;
475 if ((image->type & 0xFF00) == 0x0100) {
476 gzseek(image->file, (long) image->rowStart[y+z*image->ysize], SEEK_SET);
477 gzread(image->file, image->tmp,
478 (unsigned int)image->rowSize[y+z*image->ysize]);
484 count = (int)(pixel & 0x7F);
500 gzseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
502 gzread(image->file, buf, image->xsize);
507 SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) {
512 l += 3; r++; g++; b++;
517 SGTexture::ConvertShort(unsigned short *array, unsigned int length) {
518 unsigned short b1, b2;
521 ptr = (unsigned char *)array;
525 *array++ = (b1 << 8) | (b2);
531 SGTexture::ConvertUint(unsigned *array, unsigned int length) {
532 unsigned int b1, b2, b3, b4;
535 ptr = (unsigned char *)array;
541 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);