]> git.mxchange.org Git - simgear.git/blobdiff - simgear/screen/texture.cxx
Update some light parameters
[simgear.git] / simgear / screen / texture.cxx
index e3dd69badfe3f4af10706a2dad26d4b33277fa88..f573b98c6fcdd38c707a29454ba9ed15826f9266 100644 (file)
@@ -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; y<image->ysize; 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)
 {
@@ -340,26 +405,26 @@ SGTexture::set_pixel(GLuint x, GLuint y, sgVec3 &c)
         return;
 
     unsigned int pos = (x + y*texture_width)*3;
-    texture_data[pos]   = c[0];
-    texture_data[pos+1] = c[1];
-    texture_data[pos+2] = c[2];
+    texture_data[pos]   = (unsigned char)(c[0] * 256);
+    texture_data[pos+1] = (unsigned char)(c[1] * 256);
+    texture_data[pos+2] = (unsigned char)(c[2] * 256);
 }
 
 
-sgVec3 *
+float *
 SGTexture::get_pixel(GLuint x, GLuint y)
 {
     static sgVec3 c;
 
-    sgSetVec3(c, 0.0, 0.0, 0.0);
+    sgSetVec3(c, 0, 0, 0);
     if (!texture_data)
-        return;
+        return c;
 
     unsigned int pos = (x + y*texture_width)*3;
 
     sgSetVec3(c, texture_data[pos], texture_data[pos+1], texture_data[pos+2]);
 
-    return &c;
+    return c;
 }
 
 SGTexture::ImageRec *
@@ -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;