-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscm-frame.cpp
More file actions
122 lines (93 loc) · 3.75 KB
/
scm-frame.cpp
File metadata and controls
122 lines (93 loc) · 3.75 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
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2011-2012 Robert Kooima
//
// LIBSCM 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 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITH-
// OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
#include "scm-frame.hpp"
#include "scm-log.hpp"
//------------------------------------------------------------------------------
/// Create a new OpenGL framebuffer object with color and depth textures
///
/// @param w width in pixels
/// @param h height in pixels
scm_frame::scm_frame(GLsizei w, GLsizei h) : width(w), height(h)
{
glGenFramebuffers(1, &frame);
glGenTextures (1, &color);
glGenTextures (1, &depth);
init_color();
init_depth();
init_frame();
}
/// Delete the framebuffer object and its textures
scm_frame::~scm_frame()
{
if (color) glDeleteTextures (1, &color);
if (depth) glDeleteTextures (1, &depth);
if (frame) glDeleteFramebuffers(1, &frame);
}
/// Bind the framebuffer as render target and set the Viewport and Scissor
void scm_frame::bind_frame() const
{
glBindFramebuffer(GL_FRAMEBUFFER, frame);
glViewport(0, 0, width, height);
glScissor (0, 0, width, height);
}
/// Bind the color texture
void scm_frame::bind_color() const
{
glBindTexture(GL_TEXTURE_RECTANGLE, color);
}
/// Bind the depth texture
void scm_frame::bind_depth() const
{
glBindTexture(GL_TEXTURE_RECTANGLE, depth);
}
//------------------------------------------------------------------------------
/// Initialize the storage and parameters of a color buffer.
void scm_frame::init_color()
{
glBindTexture(GL_TEXTURE_RECTANGLE, color);
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
/// Initialize the storage and parameters of a depth buffer.
void scm_frame::init_depth()
{
glBindTexture(GL_TEXTURE_RECTANGLE, depth);
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_DEPTH_COMPONENT24, width, height,
0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
/// Generate and initialize a framebuffer object with color and depth buffers
/// and confirm its status.
void scm_frame::init_frame()
{
GLint previous;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previous);
glBindTexture(GL_TEXTURE_RECTANGLE, 0);
glBindFramebuffer(GL_FRAMEBUFFER, frame);
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_RECTANGLE, color, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_TEXTURE_RECTANGLE, depth, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
scm_log("* init_framebuffer incomplete");
}
glBindFramebuffer(GL_FRAMEBUFFER, previous);
}
//------------------------------------------------------------------------------