[f7b49d]: src / hugin1 / hugin / MeshManager.cpp Maximize Restore History

Download this file

MeshManager.cpp    446 lines (371 with data), 14.2 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// -*- c-basic-offset: 4 -*-
/** @file MeshManager.cpp
*
* @author James Legg
* @author Darko Makreshanski
*
* This 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 software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <wx/wx.h>
#include <wx/platform.h>
#if !defined Hugin_shared || !defined _WINDOWS
#define GLEW_STATIC
#endif
#include <GL/glew.h>
#ifdef __WXMAC__
#include <OpenGL/gl.h>
#else
#ifdef __WXMSW__
#include <vigra/windows.h>
#endif
#include <GL/gl.h>
#endif
#include "panoinc.h"
#include "ViewState.h"
#include "MeshManager.h"
#include "ChoosyRemapper.h"
#include "LayoutRemapper.h"
#include <iostream>
// If we want to draw the outline of each face instead of shading it normally,
// uncomment this. Wireframe mode is for testing mesh quality.
// #define WIREFRAME
const double MeshManager::PanosphereOverviewMeshInfo::scale_diff=1.5;
MeshManager::MeshManager(PT::Panorama *pano, VisualizationState *visualization_state)
: m_pano(pano),
visualization_state(visualization_state),
layout_mode_on(false)
{
}
MeshManager::~MeshManager()
{
for (std::vector<MeshInfo*>::iterator it = meshes.begin() ; it != meshes.end() ; it++) {
delete (*it);
}
meshes.clear();
}
void MeshManager::CheckUpdate()
{
unsigned int old_size = meshes.size();
// Resize to fit if images were removed
while (meshes.size() > m_pano->getNrOfImages())
{
delete (meshes[meshes.size()-1]);
meshes.pop_back();
}
// check each existing image individualy.
for (unsigned int i = 0; i < meshes.size(); i++)
{
if (visualization_state->RequireRecalculateMesh(i))
{
DEBUG_DEBUG("Update mesh for " << i);
meshes[i]->SetSrcImage(visualization_state->GetSrcImage(i));
meshes[i]->Update();
}
}
// add any new images.
for (unsigned int i = old_size; i < m_pano->getNrOfImages(); i++)
{
DEBUG_INFO("Making new mesh remapper for image " << i << ".");
//use the virtual method to get the right subclass for the MeshInfo
meshes.push_back(this->ObtainMeshInfo(visualization_state->GetSrcImage(i), layout_mode_on));
}
}
void MeshManager::RenderMesh(unsigned int image_number) const
{
meshes[image_number]->CallList();
}
unsigned int MeshManager::GetDisplayList(unsigned int image_number) const
{
return meshes[image_number]->display_list_number;
}
void MeshManager::SetLayoutMode(bool state)
{
if (layout_mode_on == state) return;
layout_mode_on = state;
/* All meshes must be recalculated, since the layout mode uses meshes that
* do not resemble properly remapped images.
*/
for (std::vector<MeshInfo*>::iterator it = meshes.begin() ; it != meshes.end() ; it++) {
delete (*it);
}
meshes.clear();
}
void MeshManager::SetLayoutScale(double scale)
{
for(unsigned int i=0;i<meshes.size();i++)
meshes[i]->SetScaleFactor(scale);
};
MeshManager::MeshInfo::MeshInfo(PT::Panorama * m_pano_in,
HuginBase::SrcPanoImage * image,
VisualizationState * visualization_state_in,
bool layout_mode_on_in)
: display_list_number(glGenLists(1)), // Find a free display list.
image(*image),
m_pano(m_pano_in),
m_visualization_state(visualization_state_in),
remap(layout_mode_on_in ? (MeshRemapper *) new LayoutRemapper(m_pano, &(this->image), m_visualization_state)
: (MeshRemapper *) new ChoosyRemapper(m_pano, &(this->image), m_visualization_state)),
layout_mode_on(layout_mode_on_in),
scale_factor(3.0)
{
}
MeshManager::MeshInfo::MeshInfo(const MeshInfo & source)
// copy remap object and display list, instead of references.
: display_list_number(glGenLists(1)),
image(source.image),
m_pano(source.m_pano),
m_visualization_state(source.m_visualization_state),
remap(source.layout_mode_on ? (MeshRemapper *) new LayoutRemapper(source.m_pano, (HuginBase::SrcPanoImage*) &(source.image), source.m_visualization_state)
: (MeshRemapper *) new ChoosyRemapper(source.m_pano, (HuginBase::SrcPanoImage*) &(source.image), source.m_visualization_state)),
layout_mode_on(source.layout_mode_on),
scale_factor(3.0)
{
}
MeshManager::MeshInfo::~MeshInfo()
{
glDeleteLists(display_list_number, 1);
delete remap;
}
void MeshManager::MeshInfo::Update()
{
if (layout_mode_on)
{
/** @todo Maybe we should find the scale once, instead of for each
* image, and find a more asthetic way to calculate it.
*/
double scale = m_visualization_state->GetVisibleArea().width() /
sqrt((double) m_pano->getNrOfImages()) / scale_factor;
MeshRemapper & remapper_ref = *remap;
LayoutRemapper &r = dynamic_cast<LayoutRemapper &>(remapper_ref);
r.setScale(scale);
}
this->CompileList();
}
MeshManager::MeshInfo::MeshCoords3D::MeshCoords3D(const MeshRemapper::Coords & coords)
{
for (int x = 0 ; x < 2 ; x++) {
for (int y = 0 ; y < 2 ; y++) {
tex_coords[x][y][0] = coords.tex_c[x][y][0];
tex_coords[x][y][1] = coords.tex_c[x][y][1];
vertex_coords[x][y][0] = coords.vertex_c[x][y][0];
vertex_coords[x][y][1] = coords.vertex_c[x][y][1];
vertex_coords[x][y][2] = 0;
}
}
}
void MeshManager::MeshInfo::SetScaleFactor(double scale)
{
scale_factor=scale;
Update();
};
void MeshManager::MeshInfo::CallList() const
{
glCallList(display_list_number);
}
void MeshManager::MeshInfo::CompileList()
{
// build the display list from the coordinates generated by the remapper
// DEBUG_INFO("Preparing to compile a display list for overview for " << image_number
// << ".");
DEBUG_ASSERT(remap);
bool multiTexture=m_visualization_state->getViewState()->GetSupportMultiTexture();
unsigned int number_of_faces = 0;
DEBUG_DEBUG("mesh update compile pano");
this->BeforeCompile();
glNewList(display_list_number, GL_COMPILE);
remap->UpdateAndResetIndex();
DEBUG_INFO("Specifying faces in display list.");
glPushMatrix();
this->Transform();
#ifndef WIREFRAME
glBegin(GL_QUADS);
#endif
// get each face's coordinates from the remapper
MeshRemapper::Coords coords;
bool write = true;
while (remap->GetNextFaceCoordinates(&coords))
{
MeshCoords3D coords3d = m_visualization_state->GetMeshManager()->GetMeshCoords3D(coords);
// DEBUG_DEBUG("mesh update " << coords3d.vertex_coords[0][0][0] << " " << coords3d.vertex_coords[0][0][1] << " " << coords3d.vertex_coords[0][0][2]);
number_of_faces++;
// go in an anticlockwise direction
#ifdef WIREFRAME
glBegin(GL_LINE_LOOP);
#endif
if(multiTexture)
{
glMultiTexCoord2dv(GL_TEXTURE0,coords3d.tex_coords[0][0]);
glMultiTexCoord2dv(GL_TEXTURE1,coords3d.tex_coords[0][0]);
}
else
glTexCoord2dv(coords3d.tex_coords[0][0]);
glVertex3dv(coords3d.vertex_coords[0][0]);
if(multiTexture)
{
glMultiTexCoord2dv(GL_TEXTURE0,coords3d.tex_coords[0][1]);
glMultiTexCoord2dv(GL_TEXTURE1,coords3d.tex_coords[0][1]);
}
else
glTexCoord2dv(coords3d.tex_coords[0][1]);
glVertex3dv(coords3d.vertex_coords[0][1]);
if(multiTexture)
{
glMultiTexCoord2dv(GL_TEXTURE0,coords3d.tex_coords[1][1]);
glMultiTexCoord2dv(GL_TEXTURE1,coords3d.tex_coords[1][1]);
}
else
glTexCoord2dv(coords3d.tex_coords[1][1]);
glVertex3dv(coords3d.vertex_coords[1][1]);
if(multiTexture)
{
glMultiTexCoord2dv(GL_TEXTURE0,coords3d.tex_coords[1][0]);
glMultiTexCoord2dv(GL_TEXTURE1,coords3d.tex_coords[1][0]);
}
else
glTexCoord2dv(coords3d.tex_coords[1][0]);
glVertex3dv(coords3d.vertex_coords[1][0]);
#ifdef WIREFRAME
glEnd();
#endif
}
#ifndef WIREFRAME
glEnd();
#endif
glPopMatrix();
glEndList();
this->AfterCompile();
// DEBUG_INFO("Prepared a display list for " << image_number << ", using "
// << number_of_faces << " face(s).");
DEBUG_DEBUG("after compile mesh");
}
void MeshManager::PanosphereOverviewMeshInfo::Convert(double &x, double &y, double &z, double th, double ph, double r)
{
th /= 180.0;
th *= M_PI;
ph /= 180.0;
ph *= M_PI;
x = r * sin(th) * cos(ph);
y = r * sin(ph);
z = r * cos(th) * cos(ph);
}
void MeshManager::PanosphereOverviewMeshInfo::BeforeCompile()
{
yaw = image.getYaw();
pitch = image.getPitch();
image.setYaw(0);
image.setPitch(0);
}
void MeshManager::PanosphereOverviewMeshInfo::Transform()
{
glRotated(yaw, 0,-1,0);
glRotated(pitch, -1,0,0);
}
void MeshManager::PanosphereOverviewMeshInfo::AfterCompile()
{
image.setYaw(yaw);
image.setPitch(pitch);
}
MeshManager::MeshInfo::Coord3D MeshManager::PanosphereOverviewMeshInfo::GetCoord3D(hugin_utils::FDiff2D & coord, VisualizationState * state)
{
double width, height, hfov, vfov;
HuginBase::PanoramaOptions * opts = state->GetOptions();
width = opts->getWidth();
height = opts->getHeight();
hfov = 360;
vfov = 180;
MeshManager::MeshInfo::Coord3D res;
double r = ((PanosphereOverviewVisualizationState*)state)->getSphereRadius();
double th, ph;
th = ((coord.x / width) * hfov - hfov / 2.0);
ph = ((coord.y / height) * vfov - vfov / 2.0);
Convert(
res.x,
res.y,
res.z,
-th,-ph,r);
return res;
}
MeshManager::MeshInfo::MeshCoords3D MeshManager::PanosphereOverviewMeshInfo::GetMeshCoords3D(MeshRemapper::Coords &coords, VisualizationState * state)
{
double width, height, hfov, vfov;
HuginBase::PanoramaOptions * opts = state->GetOptions();
width = opts->getWidth();
height = opts->getHeight();
hfov = 360;
vfov = 180;
double r = ((PanosphereOverviewVisualizationState*)state)->getSphereRadius();
MeshCoords3D res;
for (int x = 0 ; x < 2 ; x++) {
for (int y = 0 ; y < 2 ; y++) {
res.tex_coords[x][y][0] = coords.tex_c[x][y][0];
res.tex_coords[x][y][1] = coords.tex_c[x][y][1];
double th, ph;
th = ((coords.vertex_c[x][y][0] / width) * hfov - hfov / 2.0);
ph = ((coords.vertex_c[x][y][1] / height) * vfov - vfov / 2.0);
Convert(
res.vertex_coords[x][y][0],
res.vertex_coords[x][y][1],
res.vertex_coords[x][y][2],
-th,-ph,r);
// DEBUG_DEBUG("pano get " << res.vertex_coords[x][y][0] << " " << res.vertex_coords[x][y][1] << " " << res.vertex_coords[x][y][2]);
// DEBUG_DEBUG("pano get " << coords.vertex_c[x][y][0] << " " << coords.vertex_c[x][y][1]);
}
}
return res;
}
const double MeshManager::PlaneOverviewMeshInfo::scale = 100;
MeshManager::MeshInfo::Coord3D MeshManager::PlaneOverviewMeshInfo::GetCoord3D(hugin_utils::FDiff2D &coord, VisualizationState * state)
{
double width, height;
HuginBase::PanoramaOptions * opts = state->GetOptions();
width = opts->getWidth();
height = opts->getHeight();
Coord3D res;
res.x = (coord.x - width / 2.0) * scale / width;
res.y = (coord.y - height / 2.0) * (-scale) / width;
res.z = 0;
return res;
}
MeshManager::MeshInfo::MeshCoords3D MeshManager::PlaneOverviewMeshInfo::GetMeshCoords3D(MeshRemapper::Coords &coords, VisualizationState * state)
{
double width, height;
HuginBase::PanoramaOptions * opts = state->GetOptions();
width = opts->getWidth();
height = opts->getHeight();
MeshCoords3D res;
for (int x = 0 ; x < 2 ; x++) {
for (int y = 0 ; y < 2 ; y++) {
res.tex_coords[x][y][0] = coords.tex_c[x][y][0];
res.tex_coords[x][y][1] = coords.tex_c[x][y][1];
res.vertex_coords[x][y][0] = (coords.vertex_c[x][y][0] - width / 2.0) * scale / width;
res.vertex_coords[x][y][1] = (coords.vertex_c[x][y][1] - height / 2.0) * (-scale) / width;
res.vertex_coords[x][y][2] = 0;
}
}
return res;
}
MeshManager::MeshInfo * PanosphereOverviewMeshManager::ObtainMeshInfo(HuginBase::SrcPanoImage * src, bool layout_mode_on)
{
return new MeshManager::PanosphereOverviewMeshInfo(m_pano, src, visualization_state, layout_mode_on);
}
MeshManager::MeshInfo * PlaneOverviewMeshManager::ObtainMeshInfo(HuginBase::SrcPanoImage * src, bool layout_mode_on)
{
return new MeshManager::PlaneOverviewMeshInfo(m_pano, src, visualization_state, layout_mode_on);
}
MeshManager::MeshInfo * PreviewMeshManager::ObtainMeshInfo(HuginBase::SrcPanoImage * src, bool layout_mode_on)
{
return new MeshManager::PreviewMeshInfo(m_pano, src, visualization_state, layout_mode_on);
}