Visualization Library 2.0.0-b3

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
EdgeUpdateCallback.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, 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 
32 #ifndef EdgeUpdateCallback_INCLUDE_ONCE
33 #define EdgeUpdateCallback_INCLUDE_ONCE
34 
35 #include <vlGraphics/Actor.hpp>
37 #include <vlGraphics/Geometry.hpp>
38 #include <vlGraphics/Camera.hpp>
39 
40 namespace vl
41 {
45  {
47 
48  public:
49  EdgeUpdateCallback(): mShowCreases(true)
50  {
51  VL_DEBUG_SET_OBJECT_NAME()
52  }
53 
54  EdgeUpdateCallback(const std::vector<EdgeExtractor::Edge>& edge): mEdges(edge), mShowCreases(false)
55  {
56  VL_DEBUG_SET_OBJECT_NAME()
57  }
58 
60  void setShowCreases(bool sonly) { mShowCreases = sonly; }
62  bool showCreases() const { return mShowCreases; }
63 
64  virtual void onActorDelete(Actor*) {}
65 
66  virtual void onActorRenderStarted(Actor* act, real /*frame_clock*/, const Camera* cam, Renderable* renderable, const Shader*, int pass)
67  {
68  if (pass != 0)
69  return;
70 
71  fmat4 vmat = (fmat4)cam->viewMatrix();
72  if (act->transform())
73  vmat = vmat * (fmat4)act->transform()->worldMatrix();
74  fmat4 nmat = vmat.as3x3();
75  nmat = nmat.getInverse().transpose();
76 
77  ref<Geometry> geom = cast<Geometry>(renderable);
78  ref<ArrayFloat3> vert_array = cast<ArrayFloat3>(geom->vertexArray());
79  // VL_CHECK(vert_array->size() == edges().size()*2);
80  for(unsigned i=0; i<edges().size(); ++i)
81  {
82  bool insert_edge = edges()[i].isCrease() && showCreases();
83  if (!insert_edge)
84  {
85  fvec3 v1 = vmat * edges()[i].vertex1();
86  fvec3 v2 = vmat * edges()[i].vertex2();
87  fvec3 v = ((v1+v2) * 0.5f).normalize();
88  fvec3 n1 = nmat * edges()[i].normal1();
89  fvec3 n2 = nmat * edges()[i].normal2();
90  insert_edge = dot(n1, v) * dot(n2, v) < 0;
91  }
92  if ( insert_edge )
93  {
94  vert_array->at(i*2+0) = edges()[i].vertex1();
95  vert_array->at(i*2+1) = edges()[i].vertex2();
96  }
97  else
98  {
99  // degenerate
100  vert_array->at(i*2+0) = vert_array->at(i*2+1);
101  }
102  }
103  }
104 
105  const std::vector<EdgeExtractor::Edge>& edges() const { return mEdges; }
106  std::vector<EdgeExtractor::Edge>& edges() { return mEdges; }
107 
108  private:
109  std::vector<EdgeExtractor::Edge> mEdges;
110  bool mShowCreases;
111  };
112 
113 }
114 
115 #endif
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:161
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:227
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
const std::vector< EdgeExtractor::Edge > & edges() const
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Visualization Library main namespace.
The EdgeUpdateCallback class updates at every frame the edges of an Actor for the purpose of edge-enh...
float dot(float a, float b)
Definition: glsl_math.hpp:1111
virtual void onActorRenderStarted(Actor *act, real, const Camera *cam, Renderable *renderable, const Shader *, int pass)
Event generated just before an Actor is rendered but after the render states are ready and setup...
T_Scalar getInverse(Matrix4 &dest) const
Definition: Matrix4.hpp:1009
virtual void onActorDelete(Actor *)
Event notifying that an Actor is being deleted.
An abstract class that represents all the objects that can be rendered.
Definition: Renderable.hpp:58
std::vector< EdgeExtractor::Edge > & edges()
The ActorEventCallback class defines a callback object to react to Actor-related events.
Definition: Actor.hpp:75
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1841
bool showCreases() const
If true only the edges forming the silhouette of an object will be rendered.
void setShowCreases(bool sonly)
If true only the edges forming the silhouette of an object will be rendered.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
Matrix4< float > fmat4
A 4x4 matrix using float precision.
Definition: Matrix4.hpp:1229
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
EdgeUpdateCallback(const std::vector< EdgeExtractor::Edge > &edge)
Matrix4 as3x3() const
Definition: Matrix4.hpp:310