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]
ClipPlane.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 
32 #include <vlGraphics/ClipPlane.hpp>
33 
34 using namespace vl;
35 
36 //-----------------------------------------------------------------------------
38 {
39  VL_DEBUG_SET_OBJECT_NAME()
40  mPlane.setNormal(n);
41  mPlane.setOrigin(o);
42  mEnabled = true;
43 }
44 //-----------------------------------------------------------------------------
45 ClipPlane::ClipPlane(const vec3& o, const vec3& n)
46 {
47  VL_DEBUG_SET_OBJECT_NAME()
48  mPlane.setNormal(n);
49  mPlane.setOrigin(dot(o, n));
50  mEnabled = true;
51 }
52 //-----------------------------------------------------------------------------
53 void ClipPlane::apply(int index, const Camera* camera, OpenGLContext*) const
54 {
55  VL_CHECK(index>=0 && index<6);
56 
57  // we do our own transformations
58 
59  if (enabled())
60  {
61  glEnable(GL_CLIP_PLANE0 + index);
62 
63  glMatrixMode(GL_MODELVIEW);
64  glPushMatrix();
65 
66  glLoadIdentity();
67 
68  mat4 mat;
69  if ( boundTransform() )
70  mat = camera->viewMatrix() * boundTransform()->worldMatrix();
71 
72  vec3 pt1 = mPlane.normal() * mPlane.origin();
73  vec3 pt2 = mPlane.normal() * mPlane.origin() + mPlane.normal();
74 
75  pt1 = mat * pt1;
76  pt2 = mat * pt2;
77 
78  vec3 n = pt2 - pt1;
79  real orig = dot(n, pt1);
80 
81 #if defined(VL_OPENGL_ES1)
82  float equation[] = { n.x(), n.y(), n.z(), -orig };
83  glClipPlanef(GL_CLIP_PLANE0 + index, equation);
84 #else
85  double equation[] = { n.x(), n.y(), n.z(), -orig };
86  glClipPlane(GL_CLIP_PLANE0 + index, equation);
87 #endif
88 
89  glPopMatrix();
90  }
91  else
92  {
93  glDisable(GL_CLIP_PLANE0 + index);
94  }
95 }
96 //-----------------------------------------------------------------------------
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:161
const T_Scalar & z() const
Definition: Vector3.hpp:91
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
real origin() const
Definition: Plane.hpp:76
Visualization Library main namespace.
float dot(float a, float b)
Definition: glsl_math.hpp:1111
Transform * boundTransform()
Returns the vl::Transform to which the Light is attached.
Definition: ClipPlane.hpp:71
ClipPlane(real o=0.0f, vec3 n=vec3(0, 0, 0))
Constructor.
Definition: ClipPlane.cpp:37
const T_Scalar & y() const
Definition: Vector3.hpp:90
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
bool enabled() const
Definition: ClipPlane.hpp:94
const vec3 & normal() const
Definition: Plane.hpp:74
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
#define VL_CHECK(expr)
Definition: checks.hpp:73
void setNormal(const vec3 &normal)
Definition: Plane.hpp:78
void setOrigin(real origin)
Definition: Plane.hpp:80
virtual void apply(int index, const Camera *camera, OpenGLContext *) const
Applies the light render states.
Definition: ClipPlane.cpp:53