00001 #ifndef __IMAGE3D__
00002 #define __IMAGE3D__
00003
00004 #include <OpenGL.h>
00005 #include <QImage>
00006 #include "TextureData.h"
00007 #include "ColorTypes.h"
00008 #include "Texture.h"
00009
00010 namespace apig {
00011
00013 class AbstractImage3D : public TextureData {
00014 public:
00015 AbstractImage3D(int w=0, int h=0, int d=0) : w(w), h(h), d(d) {}
00016 virtual ~AbstractImage3D() {}
00017
00018 virtual bool loaded() const = 0;
00019 virtual void destroy() = 0;
00020
00021 int width() const { return w; }
00022 int height() const { return h; }
00023 int depth() const { return d; }
00024 bool contains(int i, int j, int k) const { return (i >= 0) && (i < w) && (j >= 0) && (j < h) && (k >= 0) && (k < d); }
00025 bool contains(float x, float y, float z) const { return (x >= 0) && (x < w) && (y >= 0) && (y < h) && (z >= 0) && (z < d); }
00026
00027 virtual GLint defaultTexFormat() const = 0;
00028 virtual GLenum textureMode() const { return GL_TEXTURE_3D; }
00029 virtual void loadToGPU(GLint texFormat) const { loadTexture3D(texFormat, GL_TEXTURE_3D); }
00030 virtual void loadTexture3D(GLint texFormat, GLenum target = GL_TEXTURE_3D) const = 0;
00031
00032 enum WrapMode { CLAMP_TO_EDGE, CLAMP_TO_BORDER, REPEAT, MIRRORED_REPEAT };
00033
00034 protected:
00035 int w, h, d;
00036 };
00037
00039
00044 template<class Color>
00045 class Image3D : public AbstractImage3D {
00046 public:
00047 Image3D(int w=0, int h=0, int d=0, Color* data=NULL);
00048 Image3D(QString fileName);
00049 virtual ~Image3D() {}
00050
00051 virtual bool loaded() const { return data != NULL; }
00052 virtual void destroy();
00053 Image3D<Color> clone() const;
00054 void save(QString dirName, QString subDirName) const;
00055
00056 virtual GLint defaultTexFormat() const { return Color::TEX_FORMAT; }
00057 virtual void loadTexture3D(GLint texFormat, GLenum target = GL_TEXTURE_3D) const;
00058
00059
00060 static Image3D<Color> readTexture(Texture *tex);
00061
00062
00063 void setBorderColor(Color border);
00064 void setWrapMode(WrapMode wrapMode);
00065 void setupBorder(WrapMode wrapMode, Color border);
00066
00067
00068
00069 inline Color& texel(int i, int j, int k) { return data[i + (j + k*h)*w]; }
00070 inline const Color& texel(int i, int j, int k) const { return data[i + (j + k*h)*w]; }
00071 inline Color& operator()(int i, int j, int k) { return texel(i,j,k); }
00072 inline const Color& operator()(int i, int j, int k) const { return texel(i,j,k); }
00073 Color sample(int i, int j, int k) const;
00074 Color interp(float x, float y, float z) const;
00075 Color operator()(float x, float y, float z) const { return interp(x,y,z); }
00076
00077 private:
00078 Color *data;
00079 Color borderColor;
00080 WrapMode wrapMode;
00081 };
00082
00083 typedef Image3D<UByte4> Image3DUByte4;
00084 typedef Image3D<Float1> Image3DFloat;
00085 typedef Image3D<Float3> Image3DFloat3;
00086 typedef Image3D<Float4> Image3DFloat4;
00087
00088 }
00089
00090 #include "Image3D_impl.h"
00091
00092 #endif
00093