]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture2d.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / texture2d.cpp
index c6137d73b60f41c0adeb62f5d6547406a64b9cf2..b6b5954837c2e8b2ca4d35a801bd28f199e75757 100644 (file)
@@ -1,11 +1,4 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <png.h>
+#include "bindable.h"
 #include "except.h"
 #include "texture2d.h"
 
@@ -14,113 +7,131 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Texture2D::Texture2D()
+Texture2D::Texture2D():
+       Texture(GL_TEXTURE_2D),
+       width(0),
+       height(0),
+       allocated(0)
+{ }
+
+void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
 {
-       target=GL_TEXTURE_2D;
+       if(width>0)
+               throw InvalidState("Texture storage may only be specified once");
+       if(wd==0 || ht==0)
+               throw InvalidParameterValue("Invalid texture dimensions");
+
+       ifmt = fmt;
+       width = wd;
+       height = ht;
 }
 
-/**
-Uploads an image into the texture.  Direct wrapper for glTexImage2D.
-*/
-void Texture2D::image(int level, int ifmt, sizei wd, sizei ht, int border, GLenum fmt, GLenum type, void *data)
+void Texture2D::allocate(unsigned level)
 {
-       maybe_bind();
+       if(allocated&(1<<level))
+               return;
 
-       glTexImage2D(target, level, ifmt, wd, ht, border, fmt, type, data);
-       width_=wd;
-       height_=ht;
+       image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
 }
 
-/**
-Uploads an image into the texture, with a simpler interface.
-*/
-void Texture2D::image(int level, sizei wd, sizei ht, TextureFormat tfmt, void *data)
+void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
 {
-       int ifmt;
-       int fmt;
-       int type;
+       require_storage();
+
+       unsigned w = width;
+       unsigned h = height;
+       get_level_size(level, w, h);
 
-       switch(tfmt)
+       Bind _bind(this, true);
+       glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
+
+       allocated |= 1<<level;
+       if(gen_mipmap && level==0)
        {
-       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_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_REV; break;
-       default: throw InvalidParameterValue("Invalid texture format");
+               for(; (w || h); w>>=1, h>>=1, ++level) ;
+               allocated |= (1<<level)-1;
        }
-
-       image(level, ifmt, wd, ht, 0, fmt, type, data);
 }
 
-/**
-Loads an image from a file and uploads it into the texture.  Currently assumes
-the file to be a PNG image.
-*/
-void Texture2D::image(const string &fn)
+void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
 {
-       FILE *file=fopen(fn.c_str(), "r");
-       if(!file) throw Exception("Couldn't open "+fn);
+       require_storage();
+       allocate(level);
 
-       char sig[8];
-       fread(sig, 8, 1, file);
-       if(png_sig_cmp((png_byte *)sig, 0, 8))
-                throw Exception("Not a PNG image");
+       Bind _bind(this, true);
+       glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
+}
 
-       png_struct *pngs=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
-       png_info   *pngi=png_create_info_struct(pngs);
+void Texture2D::load_image(const string &fn)
+{
+       Graphics::Image img;
+       img.load_file(fn);
 
-       if(setjmp(png_jmpbuf(pngs)))
-       {
-               png_destroy_read_struct(&pngs, &pngi, 0);
-               fclose(file);
-               throw Exception("PNG error");
-       }
-       png_init_io(pngs, file);
-       png_set_sig_bytes(pngs, 8);
+       image(img);
+}
 
-       png_read_info(pngs, pngi);
+void Texture2D::image(const Graphics::Image &img)
+{
+       unsigned w = img.get_width();
+       unsigned h = img.get_height();
+       PixelFormat fmt = pixelformat_from_graphics(img.get_format());
+       if(width==0)
+               storage(fmt, w, h);
+       else if(w!=width || h!=height)
+               throw IncompatibleData("Image does not match texture storage");
+
+       image(0, fmt, UNSIGNED_BYTE, img.get_data());
+}
 
-       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);
+void Texture2D::require_storage()
+{
+       if(width==0 || height==0)
+               throw InvalidState("Texture storage has not been specified");
+}
 
-       if(ctype==PNG_COLOR_TYPE_PALETTE || depth<8)
-       {
-               png_destroy_read_struct(&pngs, &pngi, 0);
-               fclose(file);
-               throw Exception("Invalid color type or bit depth");
-       }
+void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
+{
+       w >>= level;
+       h >>= level;
 
-       if(depth==16)
-               png_set_strip_16(pngs);
+       if(!w && h)
+               w = 1;
+       else if(!h && w)
+               h = 1;
+}
 
-       TextureFormat fmt;
-       unsigned planes;
 
-       switch(ctype)
-       {
-       case PNG_COLOR_TYPE_GRAY: fmt=LUMINANCE8; planes=1; break;
-       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");
-       }
+Texture2D::Loader::Loader(Texture2D &t):
+       Texture::Loader(t)
+{
+       add("image_data", &Loader::image_data);
+       add("raw_data", &Loader::raw_data);
+       add("storage", &Loader::storage);
+       add("storage", &Loader::storage_b);
+}
 
-       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;
+void Texture2D::Loader::image_data(const string &data)
+{
+       Graphics::Image img;
+       img.load_memory(data.data(), data.size());
 
-       png_read_image(pngs, row_ptrs);
+       static_cast<Texture2D &>(obj).image(img);
+}
 
-       image(0, wd, ht, fmt, data);
-       delete[] data;
+void Texture2D::Loader::raw_data(const string &data)
+{
+       Texture2D &t2d = static_cast<Texture2D &>(obj);
+       t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
+}
 
-       png_destroy_read_struct(&pngs, &pngi, 0);
-       fclose(file);
+void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
+{
+       static_cast<Texture2D &>(obj).storage(fmt, w, h);
+}
+
+void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
+{
+       storage(fmt, w, h);
 }
 
 } // namespace GL