new render target class while working on the exhibit UI
[laserbrain_demo] / src / rtarg.h
diff --git a/src/rtarg.h b/src/rtarg.h
new file mode 100644 (file)
index 0000000..f77682d
--- /dev/null
@@ -0,0 +1,63 @@
+#ifndef RTARG_H_
+#define RTARG_H_
+
+#include <gmath/gmath.h>
+#include "opengl.h"
+#include "texture.h"
+
+// flags for RenderTarget::create
+enum {
+       RT_COLOR = 1,
+       RT_DEPTH = 2,
+       RT_STENCIL = 4
+};
+
+class RenderTarget {
+private:
+       int width, height;
+       int tex_width, tex_height;
+
+       unsigned int fbo;
+       Texture *tex[4];
+       int rtcount;
+       // either the depth texture or a dummy depth render target is used
+       Texture *depth;
+       unsigned int rbdepth;
+       Mat4 texmat; // texture matrix to map tex coords from [0,1] to the useful area
+
+       bool own_texture[4];
+       bool auto_vport;
+
+public:
+       RenderTarget();
+       ~RenderTarget();
+
+       bool create(int xsz, int ysz, unsigned int fmt = GL_SRGB_ALPHA,
+                       unsigned int flags = RT_COLOR | RT_DEPTH);
+       bool create_mrt(int xsz, int ysz, int num, unsigned int fmt = GL_RGB16F,
+                       unsigned int flags = RT_COLOR | RT_DEPTH);
+       void destroy();
+
+       int get_width() const;
+       int get_height() const;
+
+       void set_texture(Texture *tex, int idx = 0);
+       void set_depth_texture(Texture *tex);
+
+       Texture *texture(int idx = 0) const;
+       Texture *depth_texture() const;
+
+       void set_auto_viewport(bool enable);
+       bool auto_viewport() const;
+
+       void bind() const;
+       const Mat4 &texture_matrix() const;
+};
+
+void set_render_target(RenderTarget *rt);
+RenderTarget *current_render_target();
+
+bool push_render_target(RenderTarget *rt);
+bool pop_render_target();
+
+#endif // RTARG_H_