Developer Documentation
CameraNode.cc
1/* ========================================================================= *
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42/*===========================================================================*\
43 *
44 * $Revision$
45 * $Date$
46 *
47\*===========================================================================*/
48
49
50
51
52
53//=============================================================================
54//
55// CLASS GlutPrimitiveNode - IMPLEMENTATION
56//
57//=============================================================================
58
59#define CAMERAVISNODE_C
60
61//== INCLUDES =================================================================
62#include "CameraNode.hh"
63
64#include <OpenMesh/Core/Utils/vector_cast.hh>
65
66#include <ACG/GL/IRenderer.hh>
67#include <ACG/GL/ShaderCache.hh>
68#include <ACG/ShaderUtils/GLSLShader.hh>
69
70//== NAMESPACES ===============================================================
71
72namespace ACG {
73namespace SceneGraph {
74
75//== IMPLEMENTATION ==========================================================
76
78CameraNode::CameraNode(BaseNode* _parent, std::string _name) :
79 BaseNode(_parent, _name),
80 bbmin_(FLT_MAX,FLT_MAX,FLT_MAX),
81 bbmax_(FLT_MIN,FLT_MIN,FLT_MIN),
82 cylinder_(0),
83 cone_(0),
84 axis_length_(0.1f),
85 update_vbo_(true),
86 offsetTris_(0),
87 offsetLines_(0),
88 offsetFront_(0),
89 showFrustum_(false) {
90
91 modelView_.identity();
92
93 projection_.identity();
94 // Setup a standard projection ( Full fovy 90, aspect 1.0, near 1.0, far 2.0 )
95 projection_.perspective(45 ,1.0,1.0,2.0);
96
97
98 cylinder_ = new GLCylinder(8, 4, 1.0f, false, false);
99 cone_ = new GLCone(8, 1, 1.0f, 0.0f, true, false);
100}
101
103 if (cylinder_)
104 delete cylinder_;
105
106 if (cone_)
107 delete cone_;
108}
109
110void CameraNode::boundingBox(Vec3d& _bbMin, Vec3d& _bbMax) {
111 _bbMin.minimize(bbmin_);
112 _bbMax.maximize(bbmax_);
113}
114
115//----------------------------------------------------------------------------
116
118 return (DrawModes::POINTS |
123}
124
125//----------------------------------------------------------------------------
126
127void CameraNode::draw(GLState& _state, const DrawModes::DrawMode& /*_drawMode*/) {
128
129 glPushAttrib(GL_LIGHTING_BIT);
130 glPushAttrib(GL_ENABLE_BIT);
131 ACG::GLState::shadeModel(GL_SMOOTH);
132 ACG::GLState::enable(GL_LIGHTING); // Turn lighting on
133
134 // Store modelview matrix
135 _state.push_modelview_matrix();
136
137 Vec4f lastBaseColor = _state.base_color();
138 Vec4f lastDiffuseColor = _state.diffuse_color();
139 Vec4f lastSpecularColor = _state.specular_color();
140
141 _state.set_base_color(ACG::Vec4f(1.0f, 1.0f, 1.0f, 1.0f));
142 _state.set_diffuse_color(ACG::Vec4f(1.0f, 1.0f, 1.0f, 1.0f));
143 _state.set_specular_color(ACG::Vec4f(1.0f, 1.0f, 0.0f, 1.0f));
144
145 // Draw camera box
146
147 updateVBO();
148
149 vbo_.bind();
150 ibo_.bind();
151
152 glPushAttrib(GL_LIGHTING_BIT);
153 ACG::GLState::disable(GL_LIGHTING); // Disable lighting
154
155 vdecl_.activateFixedFunction();
156
157
158 bool hasOrigin = projection_.isPerspective();
159 GLsizei lineOffset = offsetLines_;
160
161
162 if (showFrustum_)
163 {
164 // tris
165 glColor4f(0.66f, 0.66f, 0.66f, 1.0f);
166
167 glDrawElements(GL_TRIANGLES, lineOffset, GL_UNSIGNED_INT, (GLvoid*)(offsetTris_ * sizeof(int)));
168
169 // lines
170 GLsizei lineCount = hasOrigin ? 4 * 7 : 4 * 6;
171
172 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
173 glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(lineOffset * sizeof(int)));
174 }
175 else
176 {
177 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
178
179 // only front plane
180 GLsizei lineCount = hasOrigin ? 4 * 2 : 4 * 1;
181
182 glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(offsetFront_ * sizeof(int)));
183 }
184
185
187
188 glPopAttrib();
189
190
191 vbo_.unbind();
192 ibo_.unbind();
193
194
195 // Coordinate axes
196
197 _state.set_modelview(_state.modelview() * modelViewInv_);
198
199
200 // Draw right vector
201 _state.rotate(90, 0.0, 1.0, 0.0);
202
203 _state.set_base_color(ACG::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
204 _state.set_diffuse_color(ACG::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
205 _state.set_specular_color(ACG::Vec4f(1.0f, 0.4f, 0.4f, 1.0f));
206
207 cylinder_->setBottomRadius(axis_length_/20.0f);
208 cylinder_->setTopRadius(axis_length_/20.0f);
209 cylinder_->draw(_state, axis_length_);
210
211 // Draw top
212 _state.translate(0.0, 0.0, axis_length_ );
213 cone_->setBottomRadius(axis_length_/5.0f);
214 cone_->setTopRadius(0.0f);
215 cone_->draw(_state, axis_length_/2.0f);
216 _state.translate(0.0, 0.0, -axis_length_ );
217
218 // Draw up vector
219 _state.rotate(-90, 1.0, 0.0, 0.0);
220
221 _state.set_base_color(ACG::Vec4f(0.0f, 1.0f, 0.0f, 1.0f));
222 _state.set_diffuse_color(ACG::Vec4f(0.0f, 1.0f, 0.0f, 1.0f));
223 _state.set_specular_color(ACG::Vec4f(0.4f, 1.0f, 0.4f, 1.0f));
224
225 cylinder_->draw(_state, axis_length_);
226
227 // Draw top
228 _state.translate(0.0, 0.0, axis_length_ );
229 cone_->draw(_state, axis_length_/2.0f);
230 _state.translate(0.0, 0.0, -axis_length_ );
231
232 // Draw viewing direction vector
233 _state.rotate(-90, 0.0, 1.0, 0.0);
234
235 _state.set_base_color(ACG::Vec4f(0.0f, 0.0f, 1.0f, 1.0f));
236 _state.set_diffuse_color(ACG::Vec4f(0.0f, 0.0f, 1.0f, 1.0f));
237 _state.set_specular_color(ACG::Vec4f(0.4f, 0.4f, 1.0f, 1.0f));
238
239 cylinder_->draw(_state, axis_length_);
240
241 // Draw top
242 _state.translate(0.0, 0.0, axis_length_ );
243 cone_->draw(_state, axis_length_/2.0f);
244 _state.translate(0.0, 0.0, -axis_length_ );
245
246
247 // Reset to previous modelview
248 _state.pop_modelview_matrix();
249
250 _state.set_base_color(lastBaseColor);
251 _state.set_diffuse_color(lastDiffuseColor);
252 _state.set_specular_color(lastSpecularColor);
253
254 glPopAttrib(); // GL_ENABLE_BIT
255 glPopAttrib(); // LIGHTING
256}
257
258//----------------------------------------------------------------------------
259
260void CameraNode::getRenderObjects(IRenderer* _renderer, GLState& _state, const DrawModes::DrawMode& _drawMode, const Material* _mat)
261{
262 updateVBO();
263
264 RenderObject obj;
265 obj.initFromState(&_state);
266 obj.depthTest = true;
267 obj.shaderDesc.shadeMode = SG_SHADE_UNLIT;
268
269
270 obj.vertexBuffer = vbo_.id();
271 obj.indexBuffer = ibo_.id();
272 obj.vertexDecl = &vdecl_;
273
274 bool hasOrigin = projection_.isPerspective();
275
276 GLsizei lineOffset = offsetLines_;
277
278 if (showFrustum_)
279 {
280 // tris
281 obj.debugName = "CameraNode.frustum_tris";
282 obj.emissive = Vec3f(0.66f, 0.66f, 0.66f);
283 obj.glDrawElements(GL_TRIANGLES, lineOffset, GL_UNSIGNED_INT, (GLvoid*)(offsetTris_ * sizeof(int)));
284
285 obj.depthRange = Vec2f(0.01f, 1.0f);
286 _renderer->addRenderObject(&obj);
287
288
289 // lines
290 GLsizei lineCount = hasOrigin ? 4 * 7 : 4 * 6;
291
292 obj.debugName = "CameraNode.frustum_lines";
293 obj.emissive = Vec3f(0.0f, 0.0f, 0.0f);
294
295 obj.depthRange = Vec2f(0.0f, 1.0f);
296
297 obj.setupLineRendering(_state.line_width(), Vec2f(_state.viewport_width(), _state.viewport_height()));
298
299 obj.glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(lineOffset * sizeof(int)));
300 _renderer->addRenderObject(&obj);
301
302 obj.resetLineRendering();
303 }
304 else
305 {
306 obj.debugName = "CameraNode.frustum_lines";
307 obj.emissive = Vec3f(0.0f, 0.0f, 0.0f);
308
309 // only front plane
310 GLsizei lineCount = hasOrigin ? 4 * 2 : 4 * 1;
311
312 obj.setupLineRendering(_state.line_width(), Vec2f(_state.viewport_width(), _state.viewport_height()));
313
314 obj.glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(offsetFront_ * sizeof(int)));
315 _renderer->addRenderObject(&obj);
316 obj.resetLineRendering();
317 }
318
319
320 obj.vertexBuffer = 0;
321 obj.indexBuffer = 0;
322 obj.vertexDecl = 0;
323
324
325 // draw coordinate axis
326 cylinder_->setBottomRadius(axis_length_/20.0f);
327 cylinder_->setTopRadius(axis_length_/20.0f);
328
329 cone_->setBottomRadius(axis_length_/5.0f);
330 cone_->setTopRadius(0.0f);
331
332 GLMatrixd matView = obj.modelview * modelViewInv_;
333
334 // right vec
335 obj.debugName = "CameraNode.right_vec";
336 obj.emissive = Vec3f(1.0f, 0.0f, 0.0f);
337 obj.diffuse = Vec3f(1.0f, 0.0f, 0.0f);
338 obj.ambient = Vec3f(1.0f, 0.0f, 0.0f);
339 obj.specular = Vec3f(1.0f, 0.4f, 0.4f);
340
341 obj.modelview = matView;
342 obj.modelview.rotateY(90.0);
343 obj.modelview.scale(Vec3d(1.0, 1.0, axis_length_));
344 cylinder_->addToRenderer_primitive(_renderer, &obj);
345
346 // right top
347 obj.debugName = "CameraNode.right_top";
348 obj.modelview = matView;
349 obj.modelview.rotateY(90.0);
350 obj.modelview.translate(Vec3d(0.0, 0.0, axis_length_));
351 obj.modelview.scale(Vec3d(1.0, 1.0, axis_length_ * 0.5));
352 cone_->addToRenderer_primitive(_renderer, &obj);
353
354
355 // up vec
356 obj.debugName = "CameraNode.up_vec";
357 obj.emissive = Vec3f(0.0f, 1.0f, 0.0f);
358 obj.diffuse = Vec3f(0.0f, 1.0f, 0.0f);
359 obj.ambient = Vec3f(0.0f, 1.0f, 0.0f);
360 obj.specular = Vec3f(0.4f, 1.0f, 0.4f);
361
362 obj.modelview = matView;
363 obj.modelview.rotateX(-90.0);
364 obj.modelview.scale(Vec3d(1.0, 1.0, axis_length_));
365 cylinder_->addToRenderer_primitive(_renderer, &obj);
366
367 // up top
368 obj.debugName = "CameraNode.up_top";
369 obj.modelview = matView;
370 obj.modelview.rotateX(-90.0);
371 obj.modelview.translate(Vec3d(0.0, 0.0, axis_length_));
372 obj.modelview.scale(Vec3d(1.0, 1.0, axis_length_ * 0.5));
373 cone_->addToRenderer_primitive(_renderer, &obj);
374
375
376
377 // Draw viewing direction vector
378 obj.debugName = "CameraNode.view_vec";
379 obj.emissive = Vec3f(0.0f, 0.0f, 1.0f);
380 obj.diffuse = Vec3f(0.0f, 0.0f, 1.0f);
381 obj.ambient = Vec3f(0.0f, 0.0f, 1.0f);
382 obj.specular = Vec3f(0.4f, 0.4f, 1.0f);
383
384 obj.modelview = matView;
385 obj.modelview.scale(Vec3d(1.0, 1.0, axis_length_));
386 cylinder_->addToRenderer_primitive(_renderer, &obj);
387
388 // top
389 obj.debugName = "CameraNode.view_top";
390 obj.modelview = matView;
391 obj.modelview.translate(Vec3d(0.0, 0.0, axis_length_));
392 obj.modelview.scale(Vec3d(1.0, 1.0, axis_length_ * 0.5));
393 cone_->addToRenderer_primitive(_renderer, &obj);
394}
395
396//----------------------------------------------------------------------------
397
398void CameraNode::pick(GLState& _state, PickTarget /*_target*/)
399{
400 _state.pick_set_maximum(2);
401
402
403 updateVBO();
404
405 vbo_.bind();
406 ibo_.bind();
407
408
409 static ShaderGenDesc desc;
410 desc.vertexTemplateFile = "Picking/vertex.glsl";
411 desc.fragmentTemplateFile = "Picking/single_color_fs.glsl";
412 GLSL::Program* pickShader = ACG::ShaderCache::getInstance()->getProgram(&desc, nullptr);
413
414 if (pickShader && pickShader->isLinked())
415 {
416 pickShader->use();
417
418 // world view projection matrix
419 GLMatrixf matFrustum = _state.projection() * _state.modelview();
420 pickShader->setUniform("mWVP", matFrustum);
421
422 Vec4f pickColor = _state.pick_get_name_color_norm(0);
423 pickShader->setUniform("color", pickColor);
424
425
426
427 // pick frustum
428
429 vdecl_.activateShaderPipeline(pickShader);
430
431 GLsizei lineOffset = offsetLines_;
432
433 if (showFrustum_)
434 glDrawElements(GL_TRIANGLES, lineOffset, GL_UNSIGNED_INT, (GLvoid*)(offsetTris_ * sizeof(int)));
435 else
436 {
437 bool hasOrigin = projection_.isPerspective();
438 GLsizei lineCount = hasOrigin ? 4 * 2 : 4 * 1;
439
440 glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(offsetFront_ * sizeof(int)));
441 }
442
443
444 vdecl_.deactivateShaderPipeline(pickShader);
445
446
447 // pick coordinate axis
448 pickColor = _state.pick_get_name_color_norm(1);
449 pickShader->setUniform("color", pickColor);
450
451
452 cylinder_->setBottomRadius(axis_length_/20.0f);
453 cylinder_->setTopRadius(axis_length_/20.0f);
454
455 cone_->setBottomRadius(axis_length_/5.0f);
456 cone_->setTopRadius(0.0f);
457
458
459 GLMatrixf matVP = matFrustum * modelViewInv_;
460 GLMatrixf mat;
461
462 // right vec
463 mat = matVP;
464 mat.rotateY(90.0);
465 mat.scale(Vec3f(1.0, 1.0, axis_length_));
466 pickShader->setUniform("mWVP", mat);
467 cylinder_->draw_primitive(pickShader);
468
469 // right top
470 mat = matVP;
471 mat.rotateY(90.0);
472 mat.translate(Vec3f(0.0, 0.0, axis_length_));
473 mat.scale(Vec3f(1.0, 1.0, axis_length_ * 0.5));
474 pickShader->setUniform("mWVP", mat);
475 cone_->draw_primitive(pickShader);
476
477
478 // up vec
479 mat = matVP;
480 mat.rotateX(-90.0);
481 mat.scale(Vec3f(1.0, 1.0, axis_length_));
482 pickShader->setUniform("mWVP", mat);
483 cylinder_->draw_primitive(pickShader);
484
485 // up top
486 mat = matVP;
487 mat.rotateX(-90.0);
488 mat.translate(Vec3f(0.0, 0.0, axis_length_));
489 mat.scale(Vec3f(1.0, 1.0, axis_length_ * 0.5));
490 pickShader->setUniform("mWVP", mat);
491 cone_->draw_primitive(pickShader);
492
493
494
495 // Draw viewing direction vector
496 mat = matVP;
497 mat.scale(Vec3f(1.0, 1.0, axis_length_));
498 pickShader->setUniform("mWVP", mat);
499 cylinder_->draw_primitive(pickShader);
500
501 // top
502 mat = matVP;
503 mat.translate(Vec3f(0.0, 0.0, axis_length_));
504 mat.scale(Vec3f(1.0, 1.0, axis_length_ * 0.5));
505 pickShader->setUniform("mWVP", mat);
506 cone_->draw_primitive(pickShader);
507
508
509
510
511 pickShader->disable();
512 }
513 else
514 {
515 _state.pick_set_name(0);
516
517 // Store modelview matrix
518 _state.push_modelview_matrix();
519
520 // Draw camera box
521 bool hasOrigin = projection_.isPerspective();
522 GLsizei lineOffset = offsetLines_;
523
524
525 vbo_.bind();
526 ibo_.bind();
527
528 vdecl_.activateFixedFunction();
529
530 if (showFrustum_)
531 {
532 // tris
533 glDrawElements(GL_TRIANGLES, lineOffset, GL_UNSIGNED_INT, (GLvoid*)(offsetTris_ * sizeof(int)));
534
535 // lines
536 GLsizei lineCount = hasOrigin ? 4 * 7 : 4 * 6;
537 glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(lineOffset * sizeof(int)));
538 }
539 else
540 {
541 // only front plane
542 GLsizei lineCount = hasOrigin ? 4 * 2 : 4 * 1;
543
544 glDrawElements(GL_LINES, lineCount * 2, GL_UNSIGNED_INT, (GLvoid*)(offsetFront_ * sizeof(int)));
545 }
546
548
549 ibo_.unbind();
550 vbo_.unbind();
551
552
553 // Set modelview matrix such that it matches
554 // the remote settings (+ the local transformation).
555 // This is performed by multiplying the local
556 // modelview matrix by the inverse remote
557 // modelview matrix: M_l' = M_l * M^{-1}_r
558 _state.set_modelview(_state.modelview() * modelViewInv_);
559
560 _state.pick_set_name(1);
561
562 // Draw right vector
563 _state.rotate(90, 0.0, 1.0, 0.0);
564
565 cylinder_->setBottomRadius(axis_length_/20.0f);
566 cylinder_->setTopRadius(axis_length_/20.0f);
567 cylinder_->draw(_state, axis_length_);
568
569 // Draw top
570 _state.translate(0.0, 0.0, axis_length_ );
571 cone_->setBottomRadius(axis_length_/5.0f);
572 cone_->setTopRadius(0.0f);
573 cone_->draw(_state, axis_length_/2.0f);
574 _state.translate(0.0, 0.0, -axis_length_ );
575
576 // Draw up vector
577 _state.rotate(-90, 1.0, 0.0, 0.0);
578
579 cylinder_->draw(_state, axis_length_);
580
581 // Draw top
582 _state.translate(0.0, 0.0, axis_length_ );
583 cone_->draw(_state, axis_length_/2.0f);
584 _state.translate(0.0, 0.0, -axis_length_ );
585
586 // Draw viewing direction vector
587 _state.rotate(-90, 0.0, 1.0, 0.0);
588
589 cylinder_->draw(_state, axis_length_);
590
591 // Draw top
592 _state.translate(0.0, 0.0, axis_length_ );
593 cone_->draw(_state, axis_length_/2.0f);
594 _state.translate(0.0, 0.0, -axis_length_ );
595
596 // Reset to previous modelview
597 _state.pop_modelview_matrix();
598 }
599
600 vbo_.unbind();
601 ibo_.unbind();
602}
603
604//----------------------------------------------------------------------------
605
606void CameraNode::updateVBO()
607{
608 if (update_vbo_)
609 {
610 updateFrustumWS();
611
612 vbo_.bind();
613 vbo_.upload(sizeof(Vec4f) * vboData_.size(), &vboData_[0], GL_STATIC_DRAW);
614 vbo_.unbind();
615
616 vdecl_.clear();
617 vdecl_.addElement(GL_FLOAT, 4, VERTEX_USAGE_POSITION);
618
619 update_vbo_ = false;
620
621 updateBoundingBoxes();
622 }
623
624 if (!ibo_.is_valid())
625 {
626 int data[] =
627 {
628 // frustum triangles (planes facing the inside of the frustum)
629 3,2,6 , 6,5,3 , // right
630 1,0,4 , 4,7,1 , // left
631 4,5,6 , 6,7,4 , // top
632 0,1,2 , 2,3,0 , // bottom
633 0,3,5 , 5,4,0 , // back
634// 2,1,7 , 7,6,2 , // front
635
636 // frustum lines
637 3,2, 2,6, 6,5, 5,3, // right
638 1,0, 0,4, 4,7, 7,1, // left
639 4,5, 5,6, 6,7, 7,4, // top
640 0,1, 1,2, 2,3, 3,0, // bottom
641 0,3, 3,5, 5,4, 4,0, // back
642 2,1, 1,7, 7,6, 6,2, // front
643
644 // cam origin to near plane lines
645 8,1, 8,2, 8,6, 8,7
646 };
647
648 offsetTris_ = 0;
649 offsetLines_ = 6*5;
650 offsetFront_ = offsetLines_ + 8*5;
651
652 ibo_.bind();
653 ibo_.upload(sizeof(data), data, GL_STATIC_DRAW);
654 ibo_.unbind();
655 }
656}
657
658//----------------------------------------------------------------------------
659
660void CameraNode::updateBoundingBoxes()
661{
662 bbmin_ = Vec3d(DBL_MAX, DBL_MAX, DBL_MAX);
663 bbmax_ = Vec3d(-DBL_MAX, -DBL_MAX, -DBL_MAX);
664
665 if (update_vbo_ || vboData_.empty())
666 updateFrustumWS();
667
668 for (size_t i = 0; i < vboData_.size(); ++i)
669 {
670 Vec3d v = OpenMesh::vector_cast<Vec3d, Vec4f>(vboData_[i]);
671
672 bbmin_.minimize(v);
673 bbmax_.maximize(v);
674 }
675
676 bbmin_ -= Vec3d(axis_length_ * 2.0);
677 bbmax_ += Vec3d(axis_length_ * 2.0);
678}
679
680//----------------------------------------------------------------------------
681
682void CameraNode::updateFrustumWS()
683{
684 vboData_.resize(9);
685
686 // frustum vertices in clip space
687 Vec4f posCS[8] =
688 {
689 Vec4f(-1, -1, 1, 1), //0 frustum vertices..
690 Vec4f(-1, -1, -1, 1), //1
691 Vec4f(1, -1, -1, 1), //2
692 Vec4f(1, -1, 1, 1), //3
693 Vec4f(-1, 1, 1, 1), //4
694 Vec4f(1, 1, 1, 1), //5
695 Vec4f(1, 1, -1, 1), //6
696 Vec4f(-1, 1, -1, 1), //7
697 };
698
699 // transform to world space
700
701 GLMatrixf camWorldToClip = projection_ * modelView_;
702
703 GLMatrixf camClipToWorld(camWorldToClip);
704 camClipToWorld.invert();
705
706 for (int i= 0; i < 8; ++i)
707 {
708 Vec4f posWS = camClipToWorld * posCS[i];
709 posWS /= posWS[3];
710 vboData_[i] = posWS;
711 }
712
713 // camera position in world space
714 Vec4f camOriginWS(modelViewInv_(0, 3),
715 modelViewInv_(1, 3),
716 modelViewInv_(2, 3),
717 1.0f);
718
719 vboData_[8] = camOriginWS;
720
721
722 // make axis length relative to frustum size in world space
723 Vec3f rightVec = OpenMesh::vector_cast<Vec3f, Vec4f>(vboData_[3] - vboData_[0]);
724 Vec3f upVec = OpenMesh::vector_cast<Vec3f, Vec4f>(vboData_[7] - vboData_[1]);
725 axis_length_ = std::min(rightVec.norm(), upVec.norm()) * 0.015f;
726
727 // minimum axis length
728 axis_length_ = std::max(axis_length_, 0.05f);
729}
730
731
732
733//=============================================================================
734} // namespace SceneGraph
735} // namespace ACG
736//=============================================================================
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
void rotateX(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, x-axis)
Definition: GLMatrixT.hh:190
void rotateY(Scalar _angle, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with a rotation matrix (angle in degree, y-axis)
Definition: GLMatrixT.hh:196
void perspective(Scalar fovY, Scalar aspect, Scalar near_plane, Scalar far_plane)
multiply self with a perspective projection matrix
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
bool isPerspective() const
check if the matrix is a perspective projection matrix
void set_specular_color(const Vec4f &_col)
set specular color
Definition: GLState.cc:737
Vec4f pick_get_name_color_norm(unsigned int _idx)
same as pick_get_name_color, but the resulting color channels are normalized in [0....
Definition: GLState.cc:1077
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1026
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
Definition: GLState.cc:1507
const Vec4f & specular_color() const
get specular color
Definition: GLState.hh:966
const GLMatrixd & modelview() const
get modelview matrix
Definition: GLState.hh:816
int viewport_width() const
get viewport width
Definition: GLState.hh:847
void pick_set_name(size_t _idx)
sets the current name/color (like glLoadName(_idx))
Definition: GLState.cc:1061
const Vec4f & base_color() const
get base color (used when lighting is off)
Definition: GLState.hh:951
bool pick_set_maximum(size_t _idx)
Set the maximal number of primitives/components of your object.
Definition: GLState.cc:1051
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:533
const GLMatrixd & projection() const
get projection matrix
Definition: GLState.hh:811
void set_modelview(const GLMatrixd &_m)
set modelview
Definition: GLState.hh:753
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
Definition: GLState.cc:1527
void rotate(double _angle, double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
rotate around axis (_x, _y, _z) by _angle
Definition: GLState.cc:564
int viewport_height() const
get viewport height
Definition: GLState.hh:849
void set_base_color(const Vec4f &_col)
set base color (used when lighting is off)
Definition: GLState.cc:677
float line_width() const
get line width
Definition: GLState.hh:1000
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
Definition: GLState.cc:1729
const Vec4f & diffuse_color() const
get diffuse color
Definition: GLState.hh:961
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1010
void set_diffuse_color(const Vec4f &_col)
set diffuse color
Definition: GLState.cc:722
virtual void addRenderObject(RenderObject *_renderObject)
Callback for the scenegraph nodes, which send new render objects via this function.
Definition: IRenderer.cc:104
void identity()
setup an identity matrix
DrawModes::DrawMode availableDrawModes() const override
return available draw modes
Definition: CameraNode.cc:117
CameraNode(BaseNode *_parent=0, std::string _name="<CameraVis>")
Default constructor.
Definition: CameraNode.cc:78
void boundingBox(Vec3d &_bbMin, Vec3d &_bbMax) override
update bounding box
Definition: CameraNode.cc:110
virtual ~CameraNode()
Destructor.
Definition: CameraNode.cc:102
void pick(GLState &_state, PickTarget _target) override
picking
Definition: CameraNode.cc:398
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
drawing
Definition: CameraNode.cc:127
void getRenderObjects(IRenderer *_renderer, GLState &_state, const DrawModes::DrawMode &_drawMode, const Material *_mat) override
create render objects
Definition: CameraNode.cc:260
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
Definition: ShaderCache.cc:84
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
Definition: ShaderCache.cc:102
void addElement(const VertexElement *_pElement)
void deactivateFixedFunction() const
void deactivateShaderPipeline(GLSL::Program *_prog) const
void activateFixedFunction() const
void activateShaderPipeline(GLSL::Program *_prog) const
GLSL program class.
Definition: GLSLShader.hh:211
bool isLinked()
Returns if the program object has been succesfully linked.
Definition: GLSLShader.cc:370
void disable()
Resets to standard rendering pipeline.
Definition: GLSLShader.cc:355
void use()
Enables the program object for using.
Definition: GLSLShader.cc:345
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
Definition: GLSLShader.cc:385
vector_type & maximize(const vector_type &_rhs)
maximize values: same as *this = max(*this, _rhs), but faster
Definition: Vector11T.hh:588
vector_type & minimize(const vector_type &_rhs)
minimize values: same as *this = min(*this, _rhs), but faster
Definition: Vector11T.hh:560
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM > >().sqrnorm()))
compute euclidean norm
Definition: Vector11T.hh:434
DrawMode SOLID_SMOOTH_SHADED
draw smooth shaded (Gouraud shaded) faces (requires halfedge normals)
Definition: DrawModes.cc:82
DrawMode WIREFRAME
draw wireframe
Definition: DrawModes.cc:78
DrawMode HIDDENLINE
draw hidden line (2 rendering passes needed)
Definition: DrawModes.cc:80
DrawMode POINTS
draw unlighted points using the default base color
Definition: DrawModes.cc:73
DrawMode SOLID_FLAT_SHADED
draw flat shaded faces (requires face normals)
Definition: DrawModes.cc:81
PickTarget
What target to use for picking.
Definition: PickTarget.hh:74
Namespace providing different geometric functions concerning angles.
GLMatrixT< float > GLMatrixf
typedef
Definition: GLMatrixT.hh:322
VectorT< float, 4 > Vec4f
Definition: VectorT.hh:138
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:119
VectorT< float, 2 > Vec2f
Definition: VectorT.hh:102
@ VERTEX_USAGE_POSITION
"inPosition"
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121
Interface class between scenegraph and renderer.
Definition: RenderObject.hh:99
Vec3f diffuse
material definitions
ShaderGenDesc shaderDesc
Drawmode and other shader params.
void setupLineRendering(float _lineWidth, const Vec2f &_screenSize)
Setup rendering of thick lines.
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
void resetLineRendering()
Reset shader template names blocked by line rendering.
GLuint indexBuffer
Use vertex array object.
GLMatrixd modelview
Modelview transform.
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
Vec2f depthRange
glDepthRange: (znear, zmax)
void initFromState(GLState *_glState)
Initializes a RenderObject instance.
Definition: RenderObject.cc:61