X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ftexture2d.cpp;h=b6b5954837c2e8b2ca4d35a801bd28f199e75757;hp=c6137d73b60f41c0adeb62f5d6547406a64b9cf2;hb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;hpb=5318aa4fd553be4ce0bc428e73592b787842cdea diff --git a/source/texture2d.cpp b/source/texture2d.cpp index c6137d73..b6b59548 100644 --- a/source/texture2d.cpp +++ b/source/texture2d.cpp @@ -1,11 +1,4 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include +#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<>=1, h>>=1, ++level) ; + allocated |= (1<>= 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(obj).image(img); +} - image(0, wd, ht, fmt, data); - delete[] data; +void Texture2D::Loader::raw_data(const string &data) +{ + Texture2D &t2d = static_cast(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(obj).storage(fmt, w, h); +} + +void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned) +{ + storage(fmt, w, h); } } // namespace GL