]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture2d.cpp
Add vertex arrays and buffers
[libs/gl.git] / source / texture2d.cpp
index e8e975926e62d683c9bc9c2f978654e796fc0ac7..e8c18bb8ea5ecf890a44eb513a8c0f2b08551ef4 100644 (file)
@@ -1,5 +1,5 @@
 #include <png.h>
-#include <msp/error.h>
+#include <msp/core/error.h>
 #include "texture2d.h"
 
 using namespace std;
@@ -15,16 +15,19 @@ Texture2D::Texture2D()
 /**
 Uploads an image into the texture.  Direct wrapper for glTexImage2D.
 */
-void Texture2D::image(int level, int ifmt, sizei width, sizei height, int border, GLenum fmt, GLenum type, void *data)
+void Texture2D::image(int level, int ifmt, sizei wd, sizei ht, int border, GLenum fmt, GLenum type, void *data)
 {
-       if(bound!=this) bind();
-       glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
+       maybe_bind();
+
+       glTexImage2D(target, level, ifmt, wd, ht, border, fmt, type, data);
+       width_=wd;
+       height_=ht;
 }
 
 /**
-Uploads an image into the texture.
+Uploads an image into the texture, with a simpler interface.
 */
-void Texture2D::image(int level, sizei width, sizei height, TextureFormat tfmt, void *data)
+void Texture2D::image(int level, sizei wd, sizei ht, TextureFormat tfmt, void *data)
 {
        int ifmt;
        int fmt;
@@ -35,13 +38,13 @@ void Texture2D::image(int level, sizei width, sizei height, TextureFormat tfmt,
        case LUMINANCE8:        ifmt=GL_LUMINANCE;       fmt=GL_LUMINANCE;       type=GL_UNSIGNED_BYTE; break;
        case LUMINANCE8_ALPHA8: ifmt=GL_LUMINANCE_ALPHA; fmt=GL_LUMINANCE_ALPHA; type=GL_UNSIGNED_BYTE; break;
        case RGB8:  ifmt=GL_RGB;  fmt=GL_RGB;  type=GL_UNSIGNED_BYTE;        break;
-       case RGBA8: ifmt=GL_RGBA; fmt=GL_RGBA; type=GL_UNSIGNED_INT_8_8_8_8; break;
+       case RGBA8: ifmt=GL_RGBA; fmt=GL_RGBA; type=GL_UNSIGNED_INT_8_8_8_8_REV; break;
        case BGR8:  ifmt=GL_RGB;  fmt=GL_BGR;  type=GL_UNSIGNED_BYTE;        break;
-       case BGRA8: ifmt=GL_RGBA; fmt=GL_BGRA; type=GL_UNSIGNED_INT_8_8_8_8; break;
-       default: return;
+       case BGRA8: ifmt=GL_RGBA; fmt=GL_BGRA; type=GL_UNSIGNED_INT_8_8_8_8_REV; break;
+       default: throw InvalidParameterValue("Invalid texture format");
        }
 
-       image(level, ifmt, width, height, 0, fmt, type, data);
+       image(level, ifmt, wd, ht, 0, fmt, type, data);
 }
 
 /**
@@ -72,8 +75,8 @@ void Texture2D::image(const string &fn)
 
        png_read_info(pngs, pngi);
 
-       unsigned width=png_get_image_width(pngs, pngi);
-       unsigned height=png_get_image_height(pngs, pngi);
+       unsigned wd=png_get_image_width(pngs, pngi);
+       unsigned ht=png_get_image_height(pngs, pngi);
        unsigned depth=png_get_bit_depth(pngs, pngi);
        unsigned ctype=png_get_color_type(pngs, pngi);
 
@@ -96,17 +99,18 @@ void Texture2D::image(const string &fn)
        case PNG_COLOR_TYPE_GRAY_ALPHA: fmt=LUMINANCE8_ALPHA8; planes=2; break;
        case PNG_COLOR_TYPE_RGB: fmt=RGB8; planes=3; break;
        case PNG_COLOR_TYPE_RGB_ALPHA: fmt=RGBA8; planes=4; break;
+       default: throw Exception("Invalid color type");
        }
 
-       png_byte *data=(png_byte *)malloc(width*height*planes);
-       png_byte *row_ptrs[height];
-       for(unsigned i=0; i<height; ++i)
-               row_ptrs[i]=data+(height-1-i)*width*planes;
+       png_byte *data=new png_byte[wd*ht*planes];
+       png_byte *row_ptrs[ht];
+       for(unsigned i=0; i<ht; ++i)
+               row_ptrs[i]=data+(ht-1-i)*wd*planes;
 
        png_read_image(pngs, row_ptrs);
 
-       image(0, width, height, fmt, data);
-       free(data);
+       image(0, wd, ht, fmt, data);
+       delete[] data;
 
        png_destroy_read_struct(&pngs, &pngi, 0);
        fclose(file);