]> git.tdb.fi Git - libs/gl.git/commitdiff
Add basic support for 1D textures
authorMikko Rasa <tdb@tdb.fi>
Sat, 28 Dec 2013 15:10:36 +0000 (17:10 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 28 Dec 2013 15:10:36 +0000 (17:10 +0200)
source/texture1d.cpp [new file with mode: 0644]
source/texture1d.h [new file with mode: 0644]

diff --git a/source/texture1d.cpp b/source/texture1d.cpp
new file mode 100644 (file)
index 0000000..bcebca4
--- /dev/null
@@ -0,0 +1,60 @@
+#include "bindable.h"
+#include "error.h"
+#include "texture1d.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+Texture1D::Texture1D():
+       Texture(GL_TEXTURE_1D),
+       width(0),
+       allocated(0)
+{ }
+
+void Texture1D::storage(PixelFormat fmt, unsigned wd)
+{
+       if(width>0)
+               throw invalid_operation("Texture1D::storage");
+       if(wd==0)
+               throw invalid_argument("Texture1D::storage");
+       require_pixelformat(fmt);
+
+       ifmt = fmt;
+       width = wd;
+}
+
+void Texture1D::allocate(unsigned level)
+{
+       if(allocated&(1<<level))
+               return;
+
+       image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
+}
+
+void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
+{
+       if(width==0)
+               throw invalid_operation("Texture1D::image");
+
+       unsigned w = get_level_size(level);
+
+       BindRestore _bind(this);
+       glTexImage1D(target, level, ifmt, w, 0, fmt, type, data);
+
+       allocated |= 1<<level;
+       if(gen_mipmap && level==0)
+       {
+               for(; w; w>>=1, ++level) ;
+               allocated |= (1<<level)-1;
+       }
+}
+
+unsigned Texture1D::get_level_size(unsigned level)
+{
+       return width>>level;
+}
+
+} // namespace GL
+} // namespace Msp
diff --git a/source/texture1d.h b/source/texture1d.h
new file mode 100644 (file)
index 0000000..9957eeb
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef MSP_GL_TEXTURE1D_H_
+#define MSP_GL_TEXTURE1D_H_
+
+#include "datatype.h"
+#include "pixelformat.h"
+#include "texture.h"
+
+namespace Msp {
+namespace GL {
+
+class Texture1D: public Texture
+{
+private:
+       PixelFormat ifmt;
+       unsigned width;
+       unsigned allocated;
+
+public:
+       Texture1D();
+
+       void storage(PixelFormat, unsigned);
+       void allocate(unsigned);
+       void image(unsigned, PixelFormat, DataType, const void *);
+       unsigned get_width() const { return width; }
+
+private:
+       unsigned get_level_size(unsigned);
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif