forked from bwrrp/glblaat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLUtility.cpp
163 lines (146 loc) · 4.57 KB
/
GLUtility.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "GLUtility.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <cassert>
#include "GL.h"
#include "GLTextureRectangle.h"
using namespace std;
GLUtility::GLUtility() { }
GLUtility::~GLUtility() { }
// ----------------------------------------------------------------------------
GLTextureRectangle *GLUtility::GrabColorBuffer(int vpx, int vpy,
int vpwidth, int vpheight,
GLTextureRectangle *oldtex)
{
// Can we use an existing texture?
GLTextureRectangle *tex = oldtex;
assert(tex->GetHeight() == vpheight && tex->GetWidth() == vpwidth);
if (!tex)
{
// Create the texture
tex = GLTextureRectangle::New(vpwidth, vpheight,
GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 0);
if (!tex)
{
cerr << "GLUtility: GrabColorBuffer failed to create texture!" << endl;
return tex;
}
// Set texture parameters
tex->BindToCurrent();
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
else
{
tex->BindToCurrent();
}
// Capture RGBA buffer
glCopyTexSubImage2D(tex->GetTextureTarget(), 0, 0, 0,
vpx, vpy, vpwidth, vpheight);
// Done
tex->UnbindCurrent();
GLUtility::CheckOpenGLError("GLUtility: GrabColorBuffer");
return tex;
}
// ----------------------------------------------------------------------------
GLTextureRectangle *GLUtility::GrabDepthBuffer(int vpx, int vpy,
int vpwidth, int vpheight,
GLTextureRectangle *oldtex)
{
// get z bits
GLint depthBits;
GLint depthformat;
glGetIntegerv(GL_DEPTH_BITS,&depthBits);
if(depthBits==16)
{
depthformat = GL_DEPTH_COMPONENT16;
}
else
{
depthformat = GL_DEPTH_COMPONENT24;
}
// Can we use an existing texture?
GLTextureRectangle *tex = oldtex;
assert(tex->GetHeight() == vpheight && tex->GetWidth() == vpwidth);
if (!tex)
{
tex = GLTextureRectangle::New(vpwidth, vpheight,
depthformat, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
if (!tex)
{
cerr << "GLUtility: GrabDepthBuffer failed to create texture!" << endl;
return tex;
}
// Set texture parameters
tex->BindToCurrent();
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(tex->GetTextureTarget(),
GL_TEXTURE_COMPARE_FUNC, GL_LESS);
}
else
{
tex->BindToCurrent();
}
// Capture depth buffer
glCopyTexSubImage2D(tex->GetTextureTarget(), 0, 0, 0,
vpx, vpy, vpwidth, vpheight);
// Done
tex->UnbindCurrent();
GLUtility::CheckOpenGLError("GLUtility: GrabDepthBuffer");
return tex;
}
// ----------------------------------------------------------------------------
bool GLUtility::ErrorFlag = false;
// ----------------------------------------------------------------------------
void GLUtility::CheckOpenGLError(const std::string &task)
{
GLenum err = glGetError();
// Make sure we get all error flags before we continue, but don't loop more than 100 times
int i = 0;
while (err != GL_NO_ERROR && i < 10) {
ErrorFlag = true;
const char *errstring = reinterpret_cast<const char*>(gluErrorString(err));
cerr << task << " FAILED (err[" << (i+1) << "]=" << err <<
", \"" << (errstring ? errstring : "???") << "\")" << endl;
//assert(false);
err = glGetError();
++i;
if (i == 10) {
// This should not happen, but apparently it does
// glGetError should return 0 if it generates an error itself
cerr << "GLUtility: 10 consecutive errors reached, are you misssing a glEnd()?";
}
}
}
// ----------------------------------------------------------------------------
void GLUtility::ClearOpenGLError()
{
// Loop until there are no more errors
int i = 0;
while (glGetError() != GL_NO_ERROR && i < 10) {
++i;
if (i == 10) {
// This should not happen, but apparently it does
// glGetError should return 0 if it generates an error itself
cerr << "GLUtility: 10 consecutive errors reached, are you missing a glEnd()?";
}
}
ErrorFlag = false;
}
// ----------------------------------------------------------------------------
bool GLUtility::GetErrorFlag()
{
return ErrorFlag;
}