Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
PixelLODEvaluator.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
33 #include <vlGraphics/Camera.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39 {
40  if (mPixelRangeSet.empty())
41  return 0;
42 
43  AABB aabb = actor->transform() ? actor->lod(0)->boundingBox().transformed( actor->transform()->worldMatrix() ) : actor->lod(0)->boundingBox();
44 
45  vec3 corner[] =
46  {
47  vec3(aabb.minCorner().x(), aabb.minCorner().y(), aabb.minCorner().z()),
48  vec3(aabb.minCorner().x(), aabb.maxCorner().y(), aabb.minCorner().z()),
49  vec3(aabb.maxCorner().x(), aabb.maxCorner().y(), aabb.minCorner().z()),
50  vec3(aabb.maxCorner().x(), aabb.minCorner().y(), aabb.minCorner().z()),
51  vec3(aabb.minCorner().x(), aabb.minCorner().y(), aabb.maxCorner().z()),
52  vec3(aabb.minCorner().x(), aabb.maxCorner().y(), aabb.maxCorner().z()),
53  vec3(aabb.maxCorner().x(), aabb.maxCorner().y(), aabb.maxCorner().z()),
54  vec3(aabb.maxCorner().x(), aabb.minCorner().y(), aabb.maxCorner().z())
55  };
56 
57  mat4 proj_matrix = camera->projectionMatrix() * camera->viewMatrix();
58 
59  aabb.setNull();
60 
61  // project the 8 corners in the viewport
62  for(int i=0; i<8; ++i)
63  {
64  vec4 out = proj_matrix * vec4(corner[i],1);
65 
66  if (out.w() == 0.0f)
67  continue;
68 
69  out.x() /= out.w();
70  out.y() /= out.w();
71  out.z() /= out.w();
72 
73  // map to range 0-1
74  out.x() = out.x() * 0.5f + 0.5f;
75  out.y() = out.y() * 0.5f + 0.5f;
76  out.z() = out.z() * 0.5f + 0.5f;
77 
78  // map to viewport
79  out.x() = out.x() * camera->viewport()->width() + camera->viewport()->x();
80  out.y() = out.y() * camera->viewport()->height() + camera->viewport()->y();
81 
82  aabb.addPoint(out.xyz());
83  }
84 
85  double pixels = aabb.width() * aabb.height();
86 
87  // we assume the distances are sorted in increasing order
88  int i=0;
89  for(; i<(int)mPixelRangeSet.size(); ++i)
90  {
91  if (pixels>mPixelRangeSet[mPixelRangeSet.size() - 1 - i])
92  return i;
93  }
94 
95  return i; // == mPixelRangeSet.size()
96 }
97 //-----------------------------------------------------------------------------
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
const T_Scalar & z() const
Definition: Vector4.hpp:103
const Renderable * lod(int lod_index) const
Returns the Renderable object representing the LOD level specifed by lod_index.
Definition: Actor.hpp:173
int y() const
Definition: Viewport.hpp:67
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:161
const T_Scalar & x() const
Definition: Vector4.hpp:101
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
const T_Scalar & z() const
Definition: Vector3.hpp:91
Vector3< T_Scalar > xyz() const
Definition: Vector4.hpp:131
void setNull()
Sets ths AABB as null, that is, empty.
Definition: AABB.hpp:57
std::vector< float > mPixelRangeSet
virtual int evaluate(Actor *actor, Camera *camera)
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:141
Visualization Library main namespace.
int height() const
Definition: Viewport.hpp:71
int width() const
Definition: Viewport.hpp:69
void transformed(AABB &out, const mat4 &mat) const
Transforms an AABB by the given matrix and returns it into the out parameter.
Definition: AABB.hpp:165
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
real width() const
Returns the width of the AABB computed as max.x - min.x.
Definition: AABB.cpp:151
const vec3 & maxCorner() const
Returns the corner of the AABB with the maximum x y z coordinates.
Definition: AABB.hpp:193
void addPoint(const vec3 &p, real radius)
Updates the AABB to contain the given point.
Definition: AABB.cpp:135
const T_Scalar & y() const
Definition: Vector3.hpp:90
const T_Scalar & y() const
Definition: Vector4.hpp:102
int x() const
Definition: Viewport.hpp:65
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
const vec3 & minCorner() const
Returns the corner of the AABB with the minimum x y z coordinates.
Definition: AABB.hpp:190
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
const mat4 & projectionMatrix() const
The Camera&#39;s projection matrix.
Definition: Camera.hpp:175
const T_Scalar & x() const
Definition: Vector3.hpp:89
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
const AABB & boundingBox() const
Returns the bounding box of a Renderable without recomputing the bounds if dirty. ...
Definition: Renderable.hpp:151
fvec4 vec4
Defined as: &#39;typedef fvec4 vec4&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector4.hpp:296
const T_Scalar & w() const
Definition: Vector4.hpp:104
real height() const
Returns the height of the AABB computed as max.y - min.y.
Definition: AABB.cpp:158