-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointCloudView.cpp
186 lines (170 loc) · 6 KB
/
PointCloudView.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "signac.h"
#include "PointCloudView.h"
///////////////////////////////////////////////////////////////////////////////
PointCloudView::PointCloudView():
myWidth(800),
myHeight(600),
myColormapperEnabled(false),
myUpdateChannelBounds(false)
{
Signac::instance->addPointCloudView(this);
myOutput = new PixelData(PixelData::FormatRgba, myWidth, myHeight);
}
///////////////////////////////////////////////////////////////////////////////
PointCloudView::~PointCloudView()
{
Signac::instance->removePointCloudView(this);
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::addPointCloud(PointCloud* pc)
{
myPointClouds.push_back(pc);
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::removePointCloud(PointCloud*pc)
{
myPointClouds.remove(pc);
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::setColormap(PixelData* colormap)
{
myColormap = colormap;
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::setColormapper(Program* p)
{
myMappingProgram = p;
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::draw(const DrawContext& c)
{
// Initialize the texture and render target (if needed)
if(myChannelRT(c) == NULL)
{
myChannelTexture(c) = c.gpuContext->createTexture();
myChannelTexture(c)->initialize(
myWidth, myHeight,
Texture::Type2D,
Texture::ChannelRGBA,
Texture::FormatFloat);
myChannelRT(c) = c.gpuContext->createRenderTarget(RenderTarget::RenderToTexture);
myChannelRT(c)->setTextureTarget(myChannelTexture(c));
myOutputRT(c) = c.gpuContext->createRenderTarget(RenderTarget::RenderOffscreen);
myOutputRT(c)->setReadbackTarget(myOutput);
float vertices[] = {
// Pos // Tex
-1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f
};
myQuad(c) = c.gpuContext->createVertexArray();
myQuad(c)->addBuffer(0, GpuBuffer::VertexData, 16 * sizeof(float), vertices);
myQuad(c)->addAttribute(0, 0, "vertex", GpuBuffer::Float, false, 4, 0, 0);
myChannelTextureDraw(c) = new GpuDrawCall();
myChannelTextureDraw(c)->setVertexArray(myQuad(c));
myChannelTextureDraw(c)->addTexture("channels", myChannelTexture(c));
myChannelTextureDraw(c)->addTexture("colormap");
myChannelTextureDraw(c)->primType = GpuDrawCall::PrimTriangleStrip;
myChannelTextureDraw(c)->items = 4;
}
if(myWidth != myChannelTexture(c)->getWidth() ||
myHeight != myChannelTexture(c)->getHeight())
{
myChannelTexture(c)->resize(myWidth, myHeight);
}
if(myColormapperEnabled)
{
if(myColormap != NULL)
{
Texture* cm = myColormap->getTexture(c);
if(myColormapTexture(c) != cm)
{
myChannelTextureDraw(c)->setTexture("colormap", cm);
myColormapTexture(c) = cm;
}
}
// Render the point cloud to the output texture.
myChannelRT(c)->bind();
myChannelRT(c)->clear();
}
else
{
// Render the point cloud to the output texture.
myOutputRT(c)->bind();
myOutputRT(c)->clear();
}
// FOr now draw all batches no frustum culling
foreach(PointCloud* pc, myPointClouds)
{
pc->draw(c);
}
if(myColormapperEnabled)
{
myChannelRT(c)->unbind();
}
else
{
myOutputRT(c)->unbind();
myOutputRT(c)->readback();
}
if(myColormapperEnabled && !myMappingProgram.isNull())
{
myChannelTextureDraw(c)->setProgram(myMappingProgram->getGpuProgram(c));
/// If an update of the channel bounds is requested, read back the
// channel texture and compute min/max
if(myUpdateChannelBounds)
{
myChannelMax = -std::numeric_limits<float>::max();
myChannelMin = std::numeric_limits<float>::max();
myUpdateChannelBounds = false;
size_t chdatasize = myWidth * myHeight * 4;
float* chdata = new float[chdatasize];
myChannelTexture(c)->readRawPixels((byte*)chdata, chdatasize);
for(size_t i = 0; i < chdatasize; i += 4)
{
myChannelMax = std::max(myChannelMax, chdata[i]);
myChannelMin = std::min(myChannelMin, chdata[i]);
}
delete chdata;
ofmsg("channel texture bounds %1% %2%", %myChannelMax %myChannelMin);
}
myMappingProgram->getDataBounds(c)->set(myChannelMin, myChannelMax);
myOutputRT(c)->bind();
myOutputRT(c)->clear();
myChannelTextureDraw(c)->run();
myOutputRT(c)->unbind();
myOutputRT(c)->readback();
}
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::resize(int width, int height)
{
myWidth = width;
myHeight = height;
myOutput->resize(width, height);
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::updateChannelBounds(bool useChannelTexture)
{
// Get channel bounds
float chmin = myPointClouds.front()->getData()->floatRangeMin;
float chmax = myPointClouds.front()->getData()->floatRangeMax;
foreach(PointCloud* pc, myPointClouds)
{
Dimension* d = pc->getData();
if(d != NULL)
{
chmin = min(chmin, (float)d->floatRangeMin);
chmax = max(chmax, (float)d->floatRangeMax);
}
}
setChannelBounds(chmin, chmax);
myUpdateChannelBounds = useChannelTexture;
}
///////////////////////////////////////////////////////////////////////////////
void PointCloudView::setChannelBounds(float cmin, float cmax)
{
myChannelMin = cmin;
myChannelMax = cmax;
}