-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathvtkOpenGLContextBufferId.cxx
231 lines (199 loc) · 6.59 KB
/
vtkOpenGLContextBufferId.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLContextBufferId.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLContextBufferId.h"
#include "vtkIntArray.h"
#include <cassert>
#include "vtkObjectFactory.h"
#include "vtkOpenGLTexture.h"
#include "vtkTextureObject.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLError.h"
vtkStandardNewMacro(vtkOpenGLContextBufferId);
// ----------------------------------------------------------------------------
vtkOpenGLContextBufferId::vtkOpenGLContextBufferId()
{
this->Texture=0;
this->Context=0;
}
// ----------------------------------------------------------------------------
vtkOpenGLContextBufferId::~vtkOpenGLContextBufferId()
{
if(this->Texture!=0)
{
vtkErrorMacro("texture should have been released.");
}
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextBufferId::ReleaseGraphicsResources()
{
if(this->Texture!=0)
{
this->Texture->Delete();
this->Texture=0;
}
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextBufferId::SetContext(vtkRenderWindow *context)
{
vtkOpenGLRenderWindow *c = vtkOpenGLRenderWindow::SafeDownCast(context);
if (this->Context != c)
{
this->ReleaseGraphicsResources();
this->Context = c;
this->Modified();
}
}
// ----------------------------------------------------------------------------
vtkRenderWindow *vtkOpenGLContextBufferId::GetContext()
{
return this->Context;
}
// ----------------------------------------------------------------------------
bool vtkOpenGLContextBufferId::IsSupported()
{
assert("pre: context_is_set" && this->GetContext()!=0);
return vtkTextureObject::IsSupported(this->Context);
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextBufferId::Allocate()
{
assert("pre: positive_width" && this->GetWidth()>0);
assert("pre: positive_height" && this->GetHeight()>0);
assert("pre: context_is_set" && this->GetContext()!=0);
if(this->Texture==0)
{
this->Texture=vtkTextureObject::New();
this->Texture->SetContext(this->Context);
}
this->Context->MakeCurrent();
// 3: RGB
this->Texture->Allocate2D(static_cast<unsigned int>(this->GetWidth()),
static_cast<unsigned int>(this->GetHeight()),3,
VTK_UNSIGNED_CHAR);
}
// ----------------------------------------------------------------------------
bool vtkOpenGLContextBufferId::IsAllocated() const
{
return this->Texture!=0 &&
this->Texture->GetWidth()==static_cast<unsigned int>(this->Width) &&
this->Texture->GetHeight()==static_cast<unsigned int>(this->Height);
}
// ----------------------------------------------------------------------------
void vtkOpenGLContextBufferId::SetValues(int srcXmin,
int srcYmin)
{
assert("pre: is_allocated" && this->IsAllocated());
// copy the current read buffer to the texture.
this->Texture->CopyFromFrameBuffer(srcXmin,srcYmin,0,0,this->Width,
this->Height);
}
// ----------------------------------------------------------------------------
vtkIdType vtkOpenGLContextBufferId::GetPickedItem(int x, int y)
{
assert("pre: is_allocated" && this->IsAllocated());
vtkOpenGLClearErrorMacro();
vtkIdType result=-1;
if(x<0 || x>=this->Width)
{
vtkDebugMacro(<<"x mouse position out of range: x=" << x << " (width="
<< this->Width <<")");
}
else
{
if(y<0 || y>=this->Height)
{
vtkDebugMacro(<<"y mouse position out of range: y="<< y << " (height="
<< this->Height << ")");
}
else
{
this->Context->MakeCurrent();
// Render texture to current write buffer. Texel x,y is rendered at
// pixel x,y (instead of pixel 0,0 to work around pixel ownership test).
GLint savedDrawBuffer;
glGetIntegerv(GL_DRAW_BUFFER,&savedDrawBuffer);
bool savedDepthTest=glIsEnabled(GL_DEPTH_TEST)==GL_TRUE;
bool savedAlphaTest=glIsEnabled(GL_ALPHA_TEST)==GL_TRUE;
bool savedStencilTest=glIsEnabled(GL_STENCIL_TEST)==GL_TRUE;
bool savedBlend=glIsEnabled(GL_BLEND)==GL_TRUE;
if(savedDrawBuffer!=GL_BACK_LEFT)
{
glDrawBuffer(GL_BACK_LEFT);
}
if(savedDepthTest)
{
glDisable(GL_DEPTH_TEST);
}
if(savedAlphaTest)
{
glDisable(GL_ALPHA_TEST);
}
if(savedStencilTest)
{
glDisable(GL_STENCIL_TEST);
}
if(savedBlend)
{
glDisable(GL_BLEND);
}
this->Texture->CopyToFrameBuffer(x,y,x,y,x,y,
this->Context->GetSize()[0],
this->Context->GetSize()[1],NULL,NULL);
GLint savedReadBuffer;
glGetIntegerv(GL_READ_BUFFER,&savedReadBuffer);
glReadBuffer(GL_BACK_LEFT);
// To workaround pixel ownership test,
// get value from current read buffer at pixel (x,y) instead of just
// (0,0).
glPixelStorei(GL_PACK_ALIGNMENT,1);
unsigned char rgb[3];
rgb[0]=5;
rgb[1]=1;
rgb[2]=8;
glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_BYTE,rgb);
if(savedReadBuffer!=GL_BACK_LEFT)
{
glReadBuffer(static_cast<GLenum>(savedReadBuffer));
}
if(savedDrawBuffer!=GL_BACK_LEFT)
{
glDrawBuffer(static_cast<GLenum>(savedDrawBuffer));
}
if(savedDepthTest)
{
glEnable(GL_DEPTH_TEST);
}
if(savedAlphaTest)
{
glEnable(GL_ALPHA_TEST);
}
if(savedStencilTest)
{
glEnable(GL_STENCIL_TEST);
}
if(savedBlend)
{
glEnable(GL_BLEND);
}
int value=(static_cast<int>(rgb[0])<<16)|(static_cast<int>(rgb[1])<<8)
|static_cast<int>(rgb[2]);
result=static_cast<vtkIdType>(value-1);
}
}
assert("post: valid_result" && result>=-1 );
vtkOpenGLCheckErrorMacro("failed after GetPickedItem");
return result;
}
//-----------------------------------------------------------------------------
void vtkOpenGLContextBufferId::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}