Commit | Line | Data |
---|---|---|
2166f0fa SK |
1 | // File: OpenGl_FrameBuffer.hxx |
2 | // Author: Kirill GAVRILOV | |
3 | // Copyright: OPEN CASCADE 2011 | |
4 | ||
7fd59977 | 5 | #ifndef OPENGL_FRAME_BUFFER_H |
6 | #define OPENGL_FRAME_BUFFER_H | |
7 | ||
2166f0fa | 8 | #include <OpenGl_Context.hxx> |
7fd59977 | 9 | |
10 | #include <Standard_Boolean.hxx> | |
11 | #include <InterfaceGraphic.hxx> | |
12 | ||
7fd59977 | 13 | class OpenGl_FrameBuffer |
14 | { | |
15 | ||
16 | public: | |
17 | ||
18 | //! Helpful constants | |
19 | static const GLuint NO_TEXTURE = 0; | |
20 | static const GLuint NO_FRAMEBUFFER = 0; | |
21 | static const GLuint NO_RENDERBUFFER = 0; | |
22 | ||
7fd59977 | 23 | public: |
24 | ||
25 | OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8); | |
26 | ||
27 | virtual ~OpenGl_FrameBuffer() | |
28 | { | |
2166f0fa | 29 | Release (Handle(OpenGl_Context)()); |
7fd59977 | 30 | } |
31 | ||
32 | //! Texture width. | |
33 | GLsizei GetSizeX() const | |
34 | { | |
35 | return mySizeX; | |
36 | } | |
37 | ||
38 | //! Texture height. | |
39 | GLsizei GetSizeY() const | |
40 | { | |
41 | return mySizeY; | |
42 | } | |
43 | ||
44 | //! Viewport width. | |
45 | GLsizei GetVPSizeX() const | |
46 | { | |
47 | return myVPSizeX; | |
48 | } | |
49 | ||
50 | //! Viewport height. | |
51 | GLsizei GetVPSizeY() const | |
52 | { | |
53 | return myVPSizeY; | |
54 | } | |
55 | ||
56 | //! Returns true if current object was initialized | |
57 | Standard_Boolean IsValid() const | |
58 | { | |
59 | return IsValidFrameBuffer() && IsValidTexture() && IsValidDepthBuffer(); | |
60 | } | |
61 | ||
62 | //! Notice! Obsolete hardware (GeForce FX etc) | |
63 | //! doesn't support rectangular textures! | |
64 | //! There are 3 possible results if you are trying | |
65 | //! to create non power-of-two FBO on these cards: | |
66 | //! 1) FBO creation will fail, | |
67 | //! current implementation will try to generate compatible FBO; | |
68 | //! 2) FBO rendering will be done in software mode (ForceWare 'hack'); | |
69 | //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers). | |
2166f0fa SK |
70 | Standard_Boolean Init (const Handle(OpenGl_Context)& theGlContext, |
71 | GLsizei theViewportSizeX, | |
7fd59977 | 72 | GLsizei theViewportSizeY, |
73 | GLboolean toForcePowerOfTwo = GL_FALSE); | |
74 | ||
75 | //! Release GL objects | |
2166f0fa | 76 | void Release (const Handle(OpenGl_Context)& theGlContext); |
7fd59977 | 77 | |
78 | //! Setup viewport to render into FBO | |
79 | void SetupViewport() | |
80 | { | |
81 | glViewport (0, 0, myVPSizeX, myVPSizeY); | |
82 | } | |
83 | ||
84 | //! Override viewport settings | |
85 | void ChangeViewport (const GLsizei theVPSizeX, | |
86 | const GLsizei theVPSizeY) | |
87 | { | |
88 | myVPSizeX = theVPSizeX; | |
89 | myVPSizeY = theVPSizeY; | |
90 | } | |
91 | ||
92 | //! Bind frame buffer (to render into the texture). | |
2166f0fa | 93 | void BindBuffer (const Handle(OpenGl_Context)& theGlContext) |
7fd59977 | 94 | { |
2166f0fa | 95 | theGlContext->extFBO->glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, myGlFBufferId); |
7fd59977 | 96 | } |
97 | ||
98 | //! Unbind frame buffer. | |
2166f0fa | 99 | void UnbindBuffer (const Handle(OpenGl_Context)& theGlContext) |
7fd59977 | 100 | { |
2166f0fa | 101 | theGlContext->extFBO->glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, NO_FRAMEBUFFER); |
7fd59977 | 102 | } |
103 | ||
104 | //! Bind the texture. | |
105 | void BindTexture () | |
106 | { | |
107 | glEnable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering | |
108 | glBindTexture (GL_TEXTURE_2D, myGlTextureId); | |
109 | } | |
110 | ||
111 | //! Unbind the texture. | |
112 | void UnbindTexture() | |
113 | { | |
114 | glBindTexture (GL_TEXTURE_2D, NO_TEXTURE); | |
115 | glDisable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering | |
116 | } | |
117 | ||
118 | private: | |
119 | ||
120 | //! Check texture could be created | |
121 | Standard_Boolean IsProxySuccess() const; | |
122 | ||
123 | //! Generate texture with undefined data | |
2166f0fa | 124 | Standard_Boolean InitTrashTexture (const Handle(OpenGl_Context)& theGlContext); |
7fd59977 | 125 | |
126 | Standard_Boolean IsValidTexture() const | |
127 | { | |
128 | return myGlTextureId != NO_TEXTURE; | |
129 | } | |
130 | ||
131 | Standard_Boolean IsValidFrameBuffer() const | |
132 | { | |
133 | return myGlFBufferId != NO_FRAMEBUFFER; | |
134 | } | |
135 | ||
136 | Standard_Boolean IsValidDepthBuffer() const | |
137 | { | |
138 | return myGlTextureId != NO_RENDERBUFFER; | |
139 | } | |
140 | ||
7fd59977 | 141 | private: |
142 | ||
143 | GLsizei mySizeX; // texture width | |
144 | GLsizei mySizeY; // texture height | |
145 | GLsizei myVPSizeX; // viewport width (should be <= texture width) | |
146 | GLsizei myVPSizeY; // viewport height (should be <= texture height) | |
147 | GLint myTextFormat; // GL_RGB, GL_RGBA,... | |
148 | GLuint myGlTextureId; // GL texture ID | |
149 | GLuint myGlFBufferId; // FBO object ID | |
150 | GLuint myGlDepthRBId; // RenderBuffer object for depth ID | |
151 | ||
7fd59977 | 152 | }; |
153 | ||
154 | #endif //OPENGL_FRAME_BUFFER_H |