From 54b3c711c368f2fae65428c2d3c19f06eaafd8d2 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 7 Aug 2003 12:31:16 +0000 Subject: [PATCH] Add support for rgba textures --- simgear/screen/texture.cxx | 84 ++++++++++++++++++++++++++++++++++++-- simgear/screen/texture.hxx | 4 ++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/simgear/screen/texture.cxx b/simgear/screen/texture.cxx index b10707f9..f573b98c 100644 --- a/simgear/screen/texture.cxx +++ b/simgear/screen/texture.cxx @@ -23,7 +23,8 @@ SGTexture::SGTexture() : texture_id(0), - texture_data(0) + texture_data(0), + num_colors(3) { } @@ -225,7 +226,7 @@ SGTexture::read_rgb_texture(const char *name) return; } - texture_data = new GLubyte[ image->xsize * image->ysize * 3]; + texture_data = new GLubyte[ image->xsize * image->ysize * 3 ]; rbuf = new GLubyte[ image->xsize ]; gbuf = new GLubyte[ image->xsize ]; bbuf = new GLubyte[ image->xsize ]; @@ -245,7 +246,7 @@ SGTexture::read_rgb_texture(const char *name) ImageGetRow(image,rbuf,y,0); ImageGetRow(image,gbuf,y,1); ImageGetRow(image,bbuf,y,2); - ImageGetRow(image,abuf,y,3); /* Discard. */ + ImageGetRow(image,abuf,y,3); // discard rgbtorgb(rbuf,gbuf,bbuf,ptr,image->xsize); ptr += (image->xsize * 3); } else { @@ -264,6 +265,70 @@ SGTexture::read_rgb_texture(const char *name) delete abuf; } + + +void +SGTexture::read_rgba_texture(const char *name) +{ + GLubyte *ptr; + GLubyte *rbuf, *gbuf, *bbuf, *abuf; + SGTexture::ImageRec *image; + int y; + + if (texture_data) + delete texture_data; + + image = ImageOpen(name); + if(!image) + return; + + texture_width = image->xsize; + texture_height = image->ysize; + if (image->zsize != 3 && image->zsize != 4) { + ImageClose(image); + return; + } + + texture_data = new GLubyte[ image->xsize * image->ysize * 4 ]; + rbuf = new GLubyte[ image->xsize ]; + gbuf = new GLubyte[ image->xsize ]; + bbuf = new GLubyte[ image->xsize ]; + abuf = new GLubyte[ image->xsize ]; + if(!texture_data || !rbuf || !gbuf || !bbuf || !abuf) { + delete texture_data; + delete rbuf; + delete gbuf; + delete bbuf; + delete abuf; + return; + } + + ptr = texture_data; + memset(abuf, 255, image->xsize); + for(y=0; yysize; y++) { + if(image->zsize == 4) { + ImageGetRow(image,rbuf,y,0); + ImageGetRow(image,gbuf,y,1); + ImageGetRow(image,bbuf,y,2); + ImageGetRow(image,abuf,y,3); + rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize); + ptr += (image->xsize * 4); + } else { + ImageGetRow(image,rbuf,y,0); + ImageGetRow(image,gbuf,y,1); + ImageGetRow(image,bbuf,y,2); + rgbatorgba(rbuf,gbuf,bbuf,abuf,ptr,image->xsize); + ptr += (image->xsize * 3); + } + } + + ImageClose(image); + delete rbuf; + delete gbuf; + delete bbuf; + delete abuf; +} + void SGTexture::read_raw_texture(const char *name) { @@ -522,6 +587,19 @@ SGTexture::rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n) { } } +void +SGTexture::rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a, + GLubyte *l, int n) { + while(n--) { + l[0] = r[0]; + l[1] = g[0]; + l[2] = b[0]; + l[3] = a[0]; + l += 4; r++; g++; b++; a++; + } +} + + void SGTexture::ConvertShort(unsigned short *array, unsigned int length) { unsigned short b1, b2; diff --git a/simgear/screen/texture.hxx b/simgear/screen/texture.hxx index bb99b5c2..05fa393d 100644 --- a/simgear/screen/texture.hxx +++ b/simgear/screen/texture.hxx @@ -31,6 +31,7 @@ private: GLsizei texture_width; GLsizei texture_height; + GLsizei num_colors; void resize(unsigned int width = 256, unsigned int height = 256); @@ -55,6 +56,8 @@ protected: void ConvertUint(unsigned *array, unsigned int length); void ConvertShort(unsigned short *array, unsigned int length); void rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n); + void rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a, + GLubyte *l, int n); ImageRec *ImageOpen(const char *fileName); ImageRec *RawImageOpen(const char *fileName); @@ -80,6 +83,7 @@ public: /* Copyright (c) Mark J. Kilgard, 1997. */ void read_alpha_texture(const char *name); void read_rgb_texture(const char *name); + void read_rgba_texture(const char *name); void read_raw_texture(const char *name); void read_r8_texture(const char *name); -- 2.39.5