-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToglCache.cc
262 lines (206 loc) · 4.68 KB
/
ToglCache.cc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// ToglCache.cc cache togl frame buffer for fast redraws
// created 10/27/98 magi
#include <assert.h>
#include <iostream>
#include "togl.h"
#include "ToglCache.h"
#include "plvGlobals.h"
#include "plvDraw.h"
#include "Trackball.h"
DisplayCache::DisplayCache (struct Togl* togl)
{
mTogl = togl;
mData = NULL;
mZData = NULL;
mValid = false;
mX = mY = 0;
mEnabled = true;
// save callbacks for chaining
mOldDestroyProc = Togl_GetDestroyFunc (togl);
mOldReshapeProc = Togl_GetReshapeFunc (togl);
mOldDisplayProc = Togl_GetDisplayFunc (togl);
// then replace them
Togl_SetDestroyFunc (togl, DC_ToglDestroy);
Togl_SetReshapeFunc (togl, DC_ToglReshape);
Togl_SetDisplayFunc (togl, DC_ToglDisplay);
AddToglDC (togl, this);
Allocate();
}
DisplayCache::~DisplayCache (void)
{
Free();
}
bool
DisplayCache::Render (void)
{
if (!mEnabled)
return false;
// write from our cache to frame buffer
if (!mValid) {
#if TOGLCACHE_DEBUG
cout << "can't render from cache" << endl;
#endif
return false;
}
glDrawBuffer (GL_FRONT);
while (glGetError() != GL_NO_ERROR)
; // clear error state since we don't care if this fails
glMatrixMode (GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode (GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable (GL_DEPTH_TEST);
glRasterPos2f (-1, -1);
glDepthMask(GL_FALSE);
glDrawPixels (mX, mY, GL_RGBA, GL_UNSIGNED_BYTE, mData);
glRasterPos2f (-1, -1);
glDepthMask(GL_TRUE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawPixels (mX, mY, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, mZData);
if (glGetError() != GL_NO_ERROR) {
cout << "ERROR in glDrawPixels!" << endl;
}
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glPopMatrix();
glMatrixMode (GL_PROJECTION);
glPopMatrix();
glMatrixMode (GL_MODELVIEW);
glEnable (GL_DEPTH_TEST);
glDrawBuffer (GL_BACK);
#if TOGLCACHE_DEBUG
cout << "mX, mY = " << mX << "," << mY << endl;
cout << "render from cache" << endl;
#endif
return true;
}
bool
DisplayCache::Update (void)
{
mValid = false;
if (mEnabled) {
// read cache from frame buffer
glReadBuffer (GL_BACK);
while (glGetError() != GL_NO_ERROR)
; // clear error state since we don't care if this fails
glReadPixels (0, 0, mX, mY, GL_RGBA, GL_UNSIGNED_BYTE, mData);
glReadPixels (0, 0, mX, mY, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, mZData);
if (!glGetError()) {
#if TOGLCACHE_DEBUG
cout << "creating cache" << endl;
#endif
mValid = true;
} // buffer read succeeded
} // enabled
return mValid;
}
bool
DisplayCache::Invalidate (void)
{
mValid = false;
return true;
}
void
DisplayCache::Enable (bool bEnable)
{
mEnabled = bEnable;
}
bool
DisplayCache::Allocate (void)
{
Free();
#if TOGLCACHE_DEBUG
cout << "Reallocating cache" << endl;
#endif
mValid = false;
mX = Togl_Width (mTogl);
mY = Togl_Height (mTogl);
mData = new char [4 * mX * mY];
mZData = new char [sizeof(unsigned int) * mX * mY];
return true;
}
bool
DisplayCache::Free (void)
{
if (mData) {
delete[] mData;
mData = NULL;
delete[] mZData;
mZData = NULL;
mX = mY = 0;
mValid = false;
}
return true;
}
void
DisplayCache::InvalidateToglCache (struct Togl* togl)
{
DisplayCache* dc = FindToglDC (togl);
if (dc != NULL)
dc->Invalidate();
}
void
DisplayCache::UpdateToglCache (struct Togl* togl)
{
DisplayCache* dc = FindToglDC (togl);
if (dc != NULL)
dc->Update();
}
void
DisplayCache::EnableToglCache (struct Togl* togl, bool bEnable)
{
DisplayCache* dc = FindToglDC (togl);
assert (dc);
dc->Enable (bEnable);
}
void
DisplayCache::DC_ToglDestroy (struct Togl* togl)
{
DisplayCache* dc = FindToglDC (togl);
assert (dc);
if (dc->mOldDestroyProc)
dc->mOldDestroyProc (togl);
delete dc;
}
void
DisplayCache::DC_ToglReshape (struct Togl* togl)
{
DisplayCache* dc = FindToglDC (togl);
assert (dc);
dc->Allocate();
if (dc->mOldReshapeProc)
dc->mOldReshapeProc (togl);
}
void
DisplayCache::DC_ToglDisplay (struct Togl* togl)
{
DisplayCache* dc = FindToglDC (togl);
assert (dc);
if (!dc->Render()) {
dc->mOldDisplayProc (togl);
}
}
#include <unordered_map>
struct eqtoglptr
{
bool operator()(unsigned long t1, unsigned long t2) const
{
return (t1 == t2);
}
};
typedef unordered_map<unsigned long, DisplayCache*, hash<unsigned long>,
eqtoglptr> DCToglHash;
static DCToglHash dcHash;
void
DisplayCache::AddToglDC (struct Togl* togl, DisplayCache* dc)
{
unsigned long hash = (unsigned long)togl;
dcHash[hash] = dc;
}
DisplayCache*
DisplayCache::FindToglDC (struct Togl* togl)
{
unsigned long hash = (unsigned long)togl;
return dcHash[hash];
}