This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSofaGL.cpp
322 lines (260 loc) · 11.4 KB
/
SofaGL.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
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
#include <GL/glew.h>
#include "SofaGL.h"
#include "VisualPickVisitor.h"
#include <sofa/core/objectmodel/Tag.h>
#include <sofa/core/visual/VisualLoop.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/helper/narrow_cast.h>
#include <sofa/simulation/mechanicalvisitor/MechanicalPickParticlesVisitor.h>
using sofa::simulation::mechanicalvisitor::MechanicalPickParticlesVisitor;
using std::cout;
using std::cerr;
using std::endl;
namespace sofa {
using core::objectmodel::Tag;
namespace simplegui {
template <typename T> inline T sqr(const T& t){ return t*t; }
SofaGL::SofaGL(SofaScene *s) :
_sofaScene(s)
{
if(!_sofaScene)
{
std::cerr << "Error: you are trying to create a SofaGL object with a null SofaScene" << std::endl;
return;
}
glewInit();
_vparams = sofa::core::visual::VisualParams::defaultInstance();
_vparams->drawTool() = &_drawToolGL;
_vparams->setSupported(sofa::core::visual::API_OpenGL);
_isPicking = false;
_sofaScene->initVisual();
}
void SofaGL::draw()
{
glGetIntegerv (GL_VIEWPORT, _viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, _mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, _projmatrix);
if(_vparams)
{
_vparams->viewport() = sofa::type::fixed_array<int, 4>(_viewport[0], _viewport[1], _viewport[2], _viewport[3]);
SReal xmin,xmax,ymin,ymax,zmin,zmax;
_sofaScene->getBoundingBox(&xmin,&xmax,&ymin,&ymax,&zmin,&zmax);
_vparams->sceneBBox() = sofa::type::BoundingBox(xmin,xmax,ymin,ymax,zmin,zmax);
_vparams->setProjectionMatrix(_projmatrix);
_vparams->setModelViewMatrix(_mvmatrix);
}
//_sofaScene->getSimulation()->updateVisual(_sofaScene->getSimulation()->GetRoot().get()); // needed to update normals and VBOs ! (i think it should be better if updateVisual() was called from draw(), why it is not already the case ?)
_sofaScene->updateVisual(); // needed to update normals and VBOs ! (i think it should be better if updateVisual() was called from draw(), why it is not already the case ?)
if( _isPicking ){
// start picking
glSelectBuffer(BUFSIZE,selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(pickX,_viewport[3]-pickY,5,5,_viewport);
glMultMatrixd(_projmatrix);
glMatrixMode(GL_MODELVIEW);
// draw
_vparams->pass() = sofa::core::visual::VisualParams::Std;
VisualPickVisitor pick ( _vparams );
pick.setTags(_sofaScene->groot()->getTags());
cerr<<"SofaGL::draw root used " << endl;
_sofaScene->groot()->execute ( &pick );
// stop picking
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();
hits = glRenderMode(GL_RENDER);
if (hits != 0)
{
GLuint* buffer = selectBuf;
// process the hits
GLint i, j, numberOfNames=0;
GLuint names, *ptr, minZ,*ptrNames=nullptr;
ptr = (GLuint *) buffer;
minZ = 0xffffffff;
for (i = 0; i < hits; i++) {
names = *ptr;
ptr++;
if (*ptr < minZ) {
numberOfNames = names;
minZ = *ptr;
ptrNames = ptr+2;
}
ptr += names+2;
}
if (numberOfNames > 0) {
cerr << "You picked object ";
ptr = ptrNames;
for (j = 0; j < numberOfNames; j++,ptr++) {
cerr<< pick.names[*ptr] << " ";
}
}
else
cerr<<"You didn't click a snowman!";
cerr<<endl;
}
else cerr<<"no hits !" << endl;
_isPicking = false;
}
// _sofaScene->getSimulation()->draw(_vparams, _sofaScene->getSimulation()->GetRoot().get());
draw(_vparams);
}
void SofaGL::draw(sofa::core::visual::VisualParams* vparams)
{
core::visual::VisualLoop* vloop = _sofaScene->groot()->getVisualLoop();
assert(vloop);
if (!vparams) vparams = sofa::core::visual::VisualParams::defaultInstance();
vparams->update();
vloop->drawStep(vparams);
}
void SofaGL::getPickDirection( GLdouble* dx, GLdouble* dy, GLdouble* dz, int x, int y )
{
// Intersection of the ray with the near and far planes
GLint realy = _viewport[3] - (GLint) y - 1; // convert coordinates from image space (y downward) to window space (y upward)
GLdouble wx, wy, wz; /* returned world x, y, z coords */
gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0, _mvmatrix, _projmatrix, _viewport, &wx, &wy, &wz); // z=0: near plane
//cout<<"World coords at z=0.0 are ("<<wx<<","<<wy<<","<<wz<<")"<<endl;
GLdouble wx1, wy1, wz1;
gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0, _mvmatrix, _projmatrix, _viewport, &wx1, &wy1, &wz1); // z=1: far plane
GLdouble nrm = sqrt( sqr(wx1-wx) + sqr(wy1-wy) + sqr(wz1-wz) );
*dx = (wx1-wx)/nrm;
*dy = (wy1-wy)/nrm;
*dz = (wz1-wz)/nrm;
}
void SofaGL::glPick(int x, int y )
{
pickX = x; pickY = y;
_isPicking = true;
}
PickedPoint SofaGL::pick(GLdouble ox, GLdouble oy, GLdouble oz, int x, int y )
{
type::Vec3d origin(ox,oy,oz), direction;
getPickDirection(&direction[0],&direction[1],&direction[2],x,y);
double distance = 10.5, distanceGrowth = 0.1; // cone around the ray ????
// cout<< "SofaGL::rayPick from origin " << origin << ", in direction " << direction << endl;
MechanicalPickParticlesVisitor picker(sofa::core::ExecParams::defaultInstance(), origin, direction, distance, distanceGrowth );
picker.execute( _sofaScene->groot()->getContext() );
PickedPoint pickedPoint;
if (!picker.particles.empty())
{
sofa::core::behavior::BaseMechanicalState *mstate = picker.particles.begin()->second.first;
unsigned index = picker.particles.begin()->second.second;
pickedPoint.state = mstate;
pickedPoint.index = index;
pickedPoint.point = Vec3(mstate->getPX(index), mstate->getPY(index), mstate->getPZ(index));
}
return pickedPoint;
}
Interactor* SofaGL::getInteractor( const PickedPoint& glpicked )
{
cout << "SofaGL::getInteractor, looking for " << glpicked << endl;
for( Picked_to_Interactor::iterator i=_picked_to_interactor.begin(); i!=_picked_to_interactor.end(); i++ )
{
cout << "SofaGL::getInteractor, map contains " << (*i).first << endl;
}
if( _picked_to_interactor.find(glpicked)!=_picked_to_interactor.end() ) // there is already an interactor on this particle
{
return _picked_to_interactor[glpicked];
}
else { // new interactor
return NULL;
}
}
Interactor* SofaGL::pickInteractor( GLdouble ox, GLdouble oy, GLdouble oz, int x, int y )
{
type::Vec3d origin(ox,oy,oz), direction;
getPickDirection(&direction[0],&direction[1],&direction[2],x,y);
double distance = 10.5, distanceGrowth = 0.1; // cone around the ray ????
// cout<< "SofaScene::rayPick from origin " << origin << ", in direction " << direction << endl;
MechanicalPickParticlesVisitor picker(sofa::core::ExecParams::defaultInstance(), origin, direction, distance, distanceGrowth, sofa::core::objectmodel::Tag("!NoPicking") );
picker.execute(_sofaScene->groot()->getContext());
if (!picker.particles.empty())
{
PickedPoint pickedPoint(picker.particles.begin()->second.first, picker.particles.begin()->second.second);
if( _picked_to_interactor.find(pickedPoint)!=_picked_to_interactor.end() )
return _picked_to_interactor[pickedPoint];
}
return NULL;
}
void SofaGL::attach( Interactor* interactor )
{
interactor->attach( _sofaScene );
_picked_to_interactor[interactor->getPickedPoint()] = interactor;
// cout<<"SofaGL::attach "<< endl; _sofaScene->printGraph();
}
void SofaGL::move( Interactor* interactor, int x, int y)
{
if( !interactor )
return;
// get the distance to the current point
Vec3 current = interactor->getPoint();
GLdouble wcur[3]; // window coordinates of the current point
gluProject(current[0],current[1],current[2],_mvmatrix,_projmatrix,_viewport,wcur,wcur+1,wcur+2);
// cout << "current point = " << current << endl;
// cout<<"move anchor, distance = " << wcur[2] << endl;
// compute and set the position of the new point
GLdouble p[3];
gluUnProject ( x, _viewport[3]-y-1, wcur[2], _mvmatrix, _projmatrix, _viewport, &p[0], &p[1], &p[2]); // new position of the picked point
// cout<<"x="<< x <<", y="<< y <<", X="<<p[0]<<", Y="<<p[1]<<", Z="<<p[2]<<endl;
interactor->setPoint(Vec3(p[0], p[1], p[2]));
}
void SofaGL::detach( Interactor* drag)
{
if( !drag )
return;
// remove it from the map
Picked_to_Interactor::iterator i=_picked_to_interactor.begin();
while( i!=_picked_to_interactor.end() && (*i).second != drag )
i++;
if( i!=_picked_to_interactor.end() ){
// cout << "Deleted interactor at " << (*i).first << endl;
_picked_to_interactor.erase(i);
// cout << "new count of interactors: " << picked_to_interactor.size() << endl;
}
else assert( false && "Active interactor not found in the map" );
drag->detach();
// cout<<"SofaGL::detach "<< endl; _sofaScene->printGraph();
}
void SofaGL::getSceneBBox( float* xmin, float* ymin, float* zmin, float* xmax, float* ymax, float* zmax )
{
SReal xm, xM, ym, yM, zm, zM;
_sofaScene->getBoundingBox(&xm,&xM,&ym,&yM,&zm,&zM);
*xmin = helper::narrow_cast<float>(xm);
*xmax = helper::narrow_cast<float>(xM);
*ymin = helper::narrow_cast<float>(ym);
*ymax = helper::narrow_cast<float>(yM);
*zmin = helper::narrow_cast<float>(zm);
*zmax = helper::narrow_cast<float>(zM);
}
void SofaGL::viewAll( SReal* xcam, SReal* ycam, SReal* zcam, SReal* xcen, SReal* ycen, SReal* zcen, SReal a, SReal* nearPlane, SReal* farPlane)
{
// scene center and radius
SReal xmin, xmax, ymin, ymax, zmin, zmax;
_sofaScene->getBoundingBox(&xmin,&xmax,&ymin,&ymax,&zmin,&zmax);
cout<<"SofaGL::viewAll, bounding box = ("<< xmin <<" "<<ymin<<" "<<zmin<<"),("<<xmax<<" "<<ymax<<" "<<zmax<<")"<<endl;
*xcen = (xmin+xmax)*0.5;
*ycen = (ymin+ymax)*0.5;
*zcen = (zmin+zmax)*0.5;
SReal radius = sqrt( sqr(xmin-xmax) + sqr(ymin-ymax) + sqr(zmin-zmax) );
// Desired distance: distance * tan(a) = radius
SReal distance = 2 * radius / tan(a);
// SReal ratio = ((SReal) _viewport[3] - _viewport[1])/(_viewport[2] - _viewport[0]);
// distance *= ratio;
cout<<"SofaGL::viewAll, angle = " << a << ", tan = " << tan(a) << ", distance = " << distance << endl;
cout<<"SofaGL::viewAll, xmin xmax ymin ymax zmin zmax = " << xmin << " " << xmax <<" "<<ymin<<" "<<ymax<<" "<<zmin<<" "<<zmax<< endl;
// move the camera along the current camera-center line, at the right distance
// cam = cen + distance * (cam-cen)/|cam-cen|
SReal curdist = sqrt( sqr(*xcam-*xcen)+sqr(*ycam-*ycen)+sqr(*zcam-*zcen) );
*xcam = *xcen + distance * (*xcam-*xcen) / curdist;
*ycam = *ycen + distance * (*ycam-*ycen) / curdist;
*zcam = *zcen + distance * (*zcam-*zcen) / curdist;
// update the depth bounds
*nearPlane = distance - radius*1.5;
*farPlane = distance + radius*1.5;
}
}//newgui
}//sofa