Repository navigation
Expand file tree
/
Copy pathrendertarget.cpp
More file actions
executable file
·111 lines (92 loc) · 3.34 KB
/
Copy pathrendertarget.cpp
File metadata and controls
executable file
·111 lines (92 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rendertarget.h"
#include "platformgl.h"
#include "engine.h"
RenderTarget::RenderTarget(int width, int height, bool depthBuffer)
{
CHECK_GL_ERROR;
screenport = 0;
savedScreenport = 0;
frameBufferID = 0;
depthBufferID = 0;
glGenFramebuffers(1, &frameBufferID);
if (depthBuffer) {
glGenRenderbuffers(1, &depthBufferID); // the depth buffer
}
glGenTextures(1, &renderTextureID);
glBindTexture(GL_TEXTURE_2D, renderTextureID);
// Texture params. Be sure to clamp and turn filtering to not use mips.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// generate the textures
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
// create 16 bit depth buffer
if (depthBufferID) {
glBindRenderbuffer(GL_RENDERBUFFER, depthBufferID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
CHECK_GL_ERROR;
texture.w = width;
texture.h = height;
texture.format = TEX_RGB16;
texture.flags = Texture::PARAM_LINEAR;
texture.creator = 0;
texture.item = 0;
texture.glID = renderTextureID;
}
RenderTarget::~RenderTarget()
{
CHECK_GL_ERROR;
if (depthBufferID) {
glDeleteRenderbuffers(1, &depthBufferID);
}
glDeleteTextures( 1, &renderTextureID );
glDeleteFramebuffers( 1, &frameBufferID );
CHECK_GL_ERROR;
delete screenport;
}
void RenderTarget::Clear()
{
glClearColor(.0f, .0f, .0f, 1.0f);
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void RenderTarget::SetActive( bool active, Engine* engine )
{
if ( active ) {
if ( !screenport ) {
screenport = new Screenport( texture.w, texture.h);
}
savedScreenport = engine->GetScreenportMutable();
engine->SetScreenport( screenport );
screenport->Resize(texture.w, texture.h, GPUDevice::Instance());
glBindFramebuffer( GL_FRAMEBUFFER, frameBufferID );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTextureID, 0);
if (depthBufferID) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferID);
}
if ( gDebugging ) {
int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
GLASSERT( status == GL_FRAMEBUFFER_COMPLETE);
(void)status;
}
}
else {
engine->SetScreenport( savedScreenport );
savedScreenport->Resize(0, 0, GPUDevice::Instance());
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}
}