7fd59977 |
1 | #ifndef OPENGL_FRAME_BUFFER_H |
2 | #define OPENGL_FRAME_BUFFER_H |
3 | |
4 | #include <OpenGl_Extension.hxx> |
5 | |
6 | #include <Standard_Boolean.hxx> |
7 | #include <InterfaceGraphic.hxx> |
8 | |
9 | #ifndef GL_FRAMEBUFFER_EXT |
10 | #define GL_FRAMEBUFFER_EXT 0x8D40 |
11 | #endif |
12 | |
13 | #ifndef GL_COLOR_ATTACHMENT0_EXT |
14 | #define GL_COLOR_ATTACHMENT0_EXT 0x8CE0 |
15 | #endif |
16 | |
17 | #ifndef GL_FRAMEBUFFER_COMPLETE_EXT |
18 | #define GL_FRAMEBUFFER_COMPLETE_EXT 0x8CD5 |
19 | #endif |
20 | |
21 | #ifndef GL_RENDERBUFFER_EXT |
22 | #define GL_RENDERBUFFER_EXT 0x8D41 |
23 | #endif |
24 | |
25 | #ifndef GL_DEPTH_ATTACHMENT_EXT |
26 | #define GL_DEPTH_ATTACHMENT_EXT 0x8D00 |
27 | #endif |
28 | |
29 | #ifdef WNT |
30 | #define GL_API_ENTRY APIENTRY |
31 | #else |
32 | #define GL_API_ENTRY |
33 | #endif |
34 | |
35 | class OpenGl_FrameBuffer |
36 | { |
37 | |
38 | public: |
39 | |
40 | //! Helpful constants |
41 | static const GLuint NO_TEXTURE = 0; |
42 | static const GLuint NO_FRAMEBUFFER = 0; |
43 | static const GLuint NO_RENDERBUFFER = 0; |
44 | |
45 | public: |
46 | |
47 | typedef void (GL_API_ENTRY *glGenFramebuffersEXT_t) (GLsizei n, GLuint* ids); |
48 | typedef void (GL_API_ENTRY *glDeleteFramebuffersEXT_t) (GLsizei n, GLuint* ids); |
49 | typedef void (GL_API_ENTRY *glBindFramebufferEXT_t) (GLenum target, GLuint id); |
50 | typedef void (GL_API_ENTRY *glFramebufferTexture2DEXT_t) (GLenum target, GLenum attachmentPoint, |
51 | GLenum textureTarget, GLuint textureId, |
52 | GLint level); |
53 | typedef GLenum (GL_API_ENTRY *glCheckFramebufferStatusEXT_t) (GLenum target); |
54 | typedef void (GL_API_ENTRY *glGenRenderbuffersEXT_t) (GLsizei n, GLuint* ids); |
55 | typedef void (GL_API_ENTRY *glDeleteRenderbuffersEXT_t) (GLsizei n, GLuint* ids); |
56 | typedef void (GL_API_ENTRY *glBindRenderbufferEXT_t) (GLenum target, GLuint id); |
57 | typedef void (GL_API_ENTRY *glRenderbufferStorageEXT_t) (GLenum target, GLenum internalFormat, |
58 | GLsizei width, GLsizei height); |
59 | |
60 | typedef void (GL_API_ENTRY *glFramebufferRenderbufferEXT_t) (GLenum target, |
61 | GLenum attachmentPoint, |
62 | GLenum renderbufferTarget, |
63 | GLuint renderbufferId); |
64 | |
65 | public: |
66 | |
67 | OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8); |
68 | |
69 | virtual ~OpenGl_FrameBuffer() |
70 | { |
71 | Release(); |
72 | } |
73 | |
74 | //! Texture width. |
75 | GLsizei GetSizeX() const |
76 | { |
77 | return mySizeX; |
78 | } |
79 | |
80 | //! Texture height. |
81 | GLsizei GetSizeY() const |
82 | { |
83 | return mySizeY; |
84 | } |
85 | |
86 | //! Viewport width. |
87 | GLsizei GetVPSizeX() const |
88 | { |
89 | return myVPSizeX; |
90 | } |
91 | |
92 | //! Viewport height. |
93 | GLsizei GetVPSizeY() const |
94 | { |
95 | return myVPSizeY; |
96 | } |
97 | |
98 | //! Returns true if current object was initialized |
99 | Standard_Boolean IsValid() const |
100 | { |
101 | return IsValidFrameBuffer() && IsValidTexture() && IsValidDepthBuffer(); |
102 | } |
103 | |
104 | //! Notice! Obsolete hardware (GeForce FX etc) |
105 | //! doesn't support rectangular textures! |
106 | //! There are 3 possible results if you are trying |
107 | //! to create non power-of-two FBO on these cards: |
108 | //! 1) FBO creation will fail, |
109 | //! current implementation will try to generate compatible FBO; |
110 | //! 2) FBO rendering will be done in software mode (ForceWare 'hack'); |
111 | //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers). |
112 | Standard_Boolean Init (GLsizei theViewportSizeX, |
113 | GLsizei theViewportSizeY, |
114 | GLboolean toForcePowerOfTwo = GL_FALSE); |
115 | |
116 | //! Release GL objects |
117 | void Release(); |
118 | |
119 | //! Setup viewport to render into FBO |
120 | void SetupViewport() |
121 | { |
122 | glViewport (0, 0, myVPSizeX, myVPSizeY); |
123 | } |
124 | |
125 | //! Override viewport settings |
126 | void ChangeViewport (const GLsizei theVPSizeX, |
127 | const GLsizei theVPSizeY) |
128 | { |
129 | myVPSizeX = theVPSizeX; |
130 | myVPSizeY = theVPSizeY; |
131 | } |
132 | |
133 | //! Bind frame buffer (to render into the texture). |
134 | void BindBuffer() |
135 | { |
136 | glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, myGlFBufferId); |
137 | } |
138 | |
139 | //! Unbind frame buffer. |
140 | void UnbindBuffer() |
141 | { |
142 | glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, NO_FRAMEBUFFER); |
143 | } |
144 | |
145 | //! Bind the texture. |
146 | void BindTexture () |
147 | { |
148 | glEnable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering |
149 | glBindTexture (GL_TEXTURE_2D, myGlTextureId); |
150 | } |
151 | |
152 | //! Unbind the texture. |
153 | void UnbindTexture() |
154 | { |
155 | glBindTexture (GL_TEXTURE_2D, NO_TEXTURE); |
156 | glDisable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering |
157 | } |
158 | |
159 | private: |
160 | |
161 | //! Check texture could be created |
162 | Standard_Boolean IsProxySuccess() const; |
163 | |
164 | //! Generate texture with undefined data |
165 | Standard_Boolean InitTrashTexture(); |
166 | |
167 | Standard_Boolean IsValidTexture() const |
168 | { |
169 | return myGlTextureId != NO_TEXTURE; |
170 | } |
171 | |
172 | Standard_Boolean IsValidFrameBuffer() const |
173 | { |
174 | return myGlFBufferId != NO_FRAMEBUFFER; |
175 | } |
176 | |
177 | Standard_Boolean IsValidDepthBuffer() const |
178 | { |
179 | return myGlTextureId != NO_RENDERBUFFER; |
180 | } |
181 | |
182 | Standard_Boolean AreFBOFunctionsValid(); |
183 | Standard_Boolean InitFBOFunctions(); |
184 | |
185 | private: |
186 | |
187 | GLsizei mySizeX; // texture width |
188 | GLsizei mySizeY; // texture height |
189 | GLsizei myVPSizeX; // viewport width (should be <= texture width) |
190 | GLsizei myVPSizeY; // viewport height (should be <= texture height) |
191 | GLint myTextFormat; // GL_RGB, GL_RGBA,... |
192 | GLuint myGlTextureId; // GL texture ID |
193 | GLuint myGlFBufferId; // FBO object ID |
194 | GLuint myGlDepthRBId; // RenderBuffer object for depth ID |
195 | |
196 | // functions |
197 | glGenFramebuffersEXT_t glGenFramebuffersEXT; |
198 | glDeleteFramebuffersEXT_t glDeleteFramebuffersEXT; |
199 | glBindFramebufferEXT_t glBindFramebufferEXT; |
200 | glFramebufferTexture2DEXT_t glFramebufferTexture2DEXT; |
201 | glCheckFramebufferStatusEXT_t glCheckFramebufferStatusEXT; |
202 | glGenRenderbuffersEXT_t glGenRenderbuffersEXT; |
203 | glDeleteRenderbuffersEXT_t glDeleteRenderbuffersEXT; |
204 | glBindRenderbufferEXT_t glBindRenderbufferEXT; |
205 | glRenderbufferStorageEXT_t glRenderbufferStorageEXT; |
206 | glFramebufferRenderbufferEXT_t glFramebufferRenderbufferEXT; |
207 | |
208 | }; |
209 | |
210 | #endif //OPENGL_FRAME_BUFFER_H |