0025258: Uninitialized class field in IntPatch_CSFunction
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.cxx
CommitLineData
b311480e 1// Created by: Kirill GAVRILOV
973c2be1 2// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
7fd59977 15#include <OpenGl_FrameBuffer.hxx>
01ca42b2 16#include <OpenGl_ArbFBO.hxx>
7fd59977 17
fd4a6963 18#include <Standard_Assert.hxx>
19
20IMPLEMENT_STANDARD_HANDLE (OpenGl_FrameBuffer, OpenGl_Resource)
21IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameBuffer, OpenGl_Resource)
7fd59977 22
7fd59977 23static inline bool isOddNumber (const GLsizei theNumber)
24{
25 return theNumber & 0x01;
26}
27
28static inline GLsizei getEvenNumber (const GLsizei theNumber)
29{
30 return isOddNumber (theNumber) ? (theNumber + 1) : theNumber;
31}
32
33//! Notice - 0 is not power of two here
34static inline bool isPowerOfTwo (const GLsizei theNumber)
35{
18f4e8e2 36 return !(theNumber & (theNumber - 1));
7fd59977 37}
38
fd4a6963 39// =======================================================================
40// function : OpenGl_FrameBuffer
41// purpose :
42// =======================================================================
7fd59977 43OpenGl_FrameBuffer::OpenGl_FrameBuffer (GLint theTextureFormat)
18f4e8e2 44: myVPSizeX (0),
7fd59977 45 myVPSizeY (0),
46 myTextFormat (theTextureFormat),
7fd59977 47 myGlFBufferId (NO_FRAMEBUFFER),
18f4e8e2 48 myColorTexture (new OpenGl_Texture()),
49 myDepthStencilTexture (new OpenGl_Texture())
7fd59977 50{
51 //
52}
53
fd4a6963 54// =======================================================================
55// function : ~OpenGl_FrameBuffer
56// purpose :
57// =======================================================================
58OpenGl_FrameBuffer::~OpenGl_FrameBuffer()
59{
60 Release (NULL);
61}
62
63// =======================================================================
64// function : Init
65// purpose :
66// =======================================================================
2166f0fa 67Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlContext,
fd4a6963 68 const GLsizei theViewportSizeX,
18f4e8e2 69 const GLsizei theViewportSizeY)
7fd59977 70{
01ca42b2 71 if (theGlContext->arbFBO == NULL)
7fd59977 72 {
7fd59977 73 return Standard_False;
74 }
75
76 // clean up previous state
fd4a6963 77 Release (theGlContext.operator->());
7fd59977 78
7fd59977 79 // setup viewport sizes as is
80 myVPSizeX = theViewportSizeX;
81 myVPSizeY = theViewportSizeY;
82
18f4e8e2 83 // Create the textures (will be used as color buffer and depth-stencil buffer)
84 if (!initTrashTextures (theGlContext))
7fd59977 85 {
fd4a6963 86 Release (theGlContext.operator->());
7fd59977 87 return Standard_False;
88 }
89
7fd59977 90 // Build FBO and setup it as texture
01ca42b2 91 theGlContext->arbFBO->glGenFramebuffers (1, &myGlFBufferId);
92 theGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, myGlFBufferId);
18f4e8e2 93 theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
94 GL_TEXTURE_2D, myColorTexture->TextureId(), 0);
95 theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
96 GL_TEXTURE_2D, myDepthStencilTexture->TextureId(), 0);
01ca42b2 97 if (theGlContext->arbFBO->glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
7fd59977 98 {
fd4a6963 99 Release (theGlContext.operator->());
7fd59977 100 return Standard_False;
101 }
102
18f4e8e2 103 UnbindBuffer (theGlContext);
7fd59977 104 return Standard_True;
105}
106
fd4a6963 107// =======================================================================
108// function : Release
109// purpose :
110// =======================================================================
10b9c7df 111void OpenGl_FrameBuffer::Release (OpenGl_Context* theGlCtx)
7fd59977 112{
18f4e8e2 113 if (isValidFrameBuffer())
7fd59977 114 {
fd4a6963 115 // application can not handle this case by exception - this is bug in code
116 Standard_ASSERT_RETURN (theGlCtx != NULL,
117 "OpenGl_FrameBuffer destroyed without GL context! Possible GPU memory leakage...",);
fd4a6963 118 if (theGlCtx->IsValid())
2166f0fa 119 {
01ca42b2 120 theGlCtx->arbFBO->glDeleteFramebuffers (1, &myGlFBufferId);
2166f0fa 121 }
fd4a6963 122 myGlFBufferId = NO_FRAMEBUFFER;
7fd59977 123 }
7fd59977 124
18f4e8e2 125 myColorTexture->Release (theGlCtx);
126 myDepthStencilTexture->Release (theGlCtx);
7fd59977 127}
128
fd4a6963 129// =======================================================================
130// function : initTrashTexture
131// purpose :
132// =======================================================================
18f4e8e2 133Standard_Boolean OpenGl_FrameBuffer::initTrashTextures (const Handle(OpenGl_Context)& theGlContext)
7fd59977 134{
18f4e8e2 135 return myColorTexture->Init (theGlContext, myTextFormat,
136 GL_RGBA, GL_UNSIGNED_BYTE,
137 myVPSizeX, myVPSizeY, Graphic3d_TOT_2D)
138 && myDepthStencilTexture->Init (theGlContext, GL_DEPTH24_STENCIL8,
139 GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
140 myVPSizeX, myVPSizeY, Graphic3d_TOT_2D);
7fd59977 141}
fd4a6963 142
143// =======================================================================
144// function : SetupViewport
145// purpose :
146// =======================================================================
147void OpenGl_FrameBuffer::SetupViewport (const Handle(OpenGl_Context)& /*theGlCtx*/)
148{
149 glViewport (0, 0, myVPSizeX, myVPSizeY);
150}
151
152// =======================================================================
153// function : ChangeViewport
154// purpose :
155// =======================================================================
156void OpenGl_FrameBuffer::ChangeViewport (const GLsizei theVPSizeX,
157 const GLsizei theVPSizeY)
158{
159 myVPSizeX = theVPSizeX;
160 myVPSizeY = theVPSizeY;
161}
162
163// =======================================================================
164// function : BindBuffer
165// purpose :
166// =======================================================================
167void OpenGl_FrameBuffer::BindBuffer (const Handle(OpenGl_Context)& theGlCtx)
168{
01ca42b2 169 theGlCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, myGlFBufferId);
fd4a6963 170}
171
172// =======================================================================
173// function : UnbindBuffer
174// purpose :
175// =======================================================================
176void OpenGl_FrameBuffer::UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx)
177{
01ca42b2 178 theGlCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, NO_FRAMEBUFFER);
fd4a6963 179}