fixed reflections in VR
[laserbrain_demo] / src / rtarg.h
1 #ifndef RTARG_H_
2 #define RTARG_H_
3
4 #include <gmath/gmath.h>
5 #include "opengl.h"
6 #include "texture.h"
7
8 // flags for RenderTarget::create
9 enum {
10         RT_COLOR = 1,
11         RT_DEPTH = 2,
12         RT_STENCIL = 4
13 };
14
15 // flags for push_render_target/pop_render_target and RenderTarget::bind
16 enum {
17         RT_NO_VPORT     = 1,
18         RT_NO_BIND      = 2,
19
20         RT_FAKE         = RT_NO_VPORT | RT_NO_BIND
21 };
22
23 class RenderTarget {
24 private:
25         int width, height;
26         int tex_width, tex_height;
27
28         unsigned int fbo;
29         Texture *tex[4];
30         int rtcount;
31         // either the depth texture or a dummy depth render target is used
32         Texture *depth;
33         unsigned int rbdepth, rbdepth_fmt;
34         Mat4 texmat; // texture matrix to map tex coords from [0,1] to the useful area
35
36         bool own_fbo;
37         bool own_texture[4];
38         bool auto_vport;
39
40 public:
41         RenderTarget();
42         ~RenderTarget();
43
44         bool create(int xsz, int ysz, unsigned int fmt = GL_SRGB_ALPHA,
45                         unsigned int flags = RT_COLOR | RT_DEPTH);
46         bool create_mrt(int xsz, int ysz, int num, unsigned int fmt = GL_RGB16F,
47                         unsigned int flags = RT_COLOR | RT_DEPTH);
48         /* create a RenderTarget wrapping an existing FBO.
49          * At this point this is just a hack with limited functionality.
50          * Calls which return textures might return 0. get_width/get_height
51          * will return whatever is passed here as xsz/ysz. resize will not do
52          * anything.
53          */
54         bool create_wrap_fbo(unsigned int fbo, int xsz, int ysz);
55         void destroy();
56
57         bool resize(int xsz, int ysz);
58
59         int get_width() const;
60         int get_height() const;
61
62         void set_texture(Texture *tex, int idx = 0);
63         void set_depth_texture(Texture *tex);
64
65         Texture *texture(int idx = 0) const;
66         Texture *depth_texture() const;
67
68         void set_auto_viewport(bool enable);
69         bool auto_viewport() const;
70
71         void bind(unsigned int flags = 0) const;
72         const Mat4 &texture_matrix() const;
73 };
74
75 void set_render_target(RenderTarget *rt);
76 RenderTarget *current_render_target();
77
78 bool push_render_target(RenderTarget *rt, unsigned int flags = 0);
79 bool pop_render_target(unsigned int flags = 0);
80
81 #endif  // RTARG_H_