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]
Billboard.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/Billboard.hpp>
33 #include <vlGraphics/Camera.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39 {
40  setAxis(vec3(0,1,0));
41  setNormal(vec3(0,0,1));
43  VL_DEBUG_SET_OBJECT_NAME()
44 }
45 //-----------------------------------------------------------------------------
47 {
48  return localMatrix().getT();
49 }
50 //-----------------------------------------------------------------------------
51 void Billboard::setPosition(const vec3& pos)
52 {
54 }
55 //-----------------------------------------------------------------------------
57 {
59  {
60  setWorldMatrix( mat4() );
61  }
62  else
63  if ( !camera )
64  {
66  }
67  else
68  {
69  mat4 world_mat;
70  vec3 pos = position();
72  pos = parent()->worldMatrix() * pos;
73  if ( type() == BT_SphericalBillboard )
74  {
75  // eye positional
76  world_mat.setZ( (camera->modelingMatrix().getT() - pos).normalize() );
77  world_mat.setY( camera->modelingMatrix().getY() );
78  world_mat.setX( cross(world_mat.getY(), world_mat.getZ()) );
79  world_mat = vl::mat4::getTranslation(pos) * world_mat;
80 
81  // eye directional
82  //world_mat.setZ( camera->modelingMatrix().getZ() );
83  //world_mat.setY( camera->modelingMatrix().getY() );
84  //world_mat.setX( camera->modelingMatrix().getX() );
85  //world_mat = vl::mat4::getTranslation(pos) * world_mat;
86  }
87  else
88  if ( type() == BT_AxisAlignedBillboard )
89  {
91  vec3 bill_to_eye = (camera->modelingMatrix().getT() - pos).normalize();
92  // flatten on the plane defined by the axis()
93  normal = normal - axis() * dot(normal, axis());
94  bill_to_eye = bill_to_eye - axis() * dot(bill_to_eye, axis());
95  normal.normalize();
96  bill_to_eye.normalize();
97  world_mat = vl::mat4::getTranslation(pos) * mat4::getRotation(normal,bill_to_eye);
98  }
99 
100  setWorldMatrix( world_mat );
101  }
102 }
103 //-----------------------------------------------------------------------------
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:227
Vector3< T_Scalar > getZ() const
Definition: Matrix4.hpp:126
Matrix4 & setX(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:136
void setLocalMatrix(const mat4 &m)
The matrix representing the transform&#39;s local space.
Definition: Transform.hpp:139
void setWorldMatrix(const mat4 &matrix)
Normally you should not use directly this function, call it only if you are sure you cannot do otherw...
Definition: Transform.hpp:161
Vector3< T_Scalar > getY() const
Definition: Matrix4.hpp:121
fvec3 cross(const fvec3 &v1, const fvec3 &v2)
Definition: Vector3.hpp:277
vec3 position()
The billboard position and rotation center.
Definition: Billboard.cpp:46
const mat4 & localMatrix() const
The matrix representing the transform&#39;s local space.
Definition: Transform.hpp:145
void setPosition(const vec3 &pos)
The billboard position and rotation center.
Definition: Billboard.cpp:51
void setAxis(const vec3 &axis)
The rotation axis in world coordinates. Used only for axis aligned billboards.
Definition: Billboard.hpp:59
Visualization Library main namespace.
virtual void computeWorldMatrix(Camera *camera=NULL)
Computes the world matrix by concatenating the parent&#39;s world matrix with its own local matrix...
Definition: Billboard.cpp:56
float dot(float a, float b)
Definition: glsl_math.hpp:1111
Matrix4 & setZ(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:152
void setNormal(const vec3 &normal)
Used only for axis aligned billboards.
Definition: Billboard.hpp:63
const vec3 & normal() const
Used only for axis aligned billboards.
Definition: Billboard.hpp:65
virtual void computeWorldMatrix(Camera *=NULL)
Computes the world matrix by concatenating the parent&#39;s world matrix with its own local matrix...
Definition: Transform.hpp:195
const mat4 & modelingMatrix() const
Returns the Camera&#39;s modelingMatrix() (inverse of the view matrix).
Definition: Camera.hpp:169
const Transform * parent() const
Returns the parent Transform.
Definition: Transform.hpp:104
#define NULL
Definition: OpenGLDefs.hpp:81
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
Matrix4 & setY(const Vector3< T_Scalar > &v)
Definition: Matrix4.hpp:144
bool assumeIdentityWorldMatrix()
If set to true the world matrix of this transform will always be considered and identity.
Definition: Transform.hpp:192
static Matrix4 & getRotation(Matrix4 &out, float degrees, float x, float y, float z)
Definition: Matrix4.hpp:904
Vector3< T_Scalar > getT() const
Definition: Matrix4.hpp:131
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
fmat4 mat4
Defined as: &#39;typedef fmat4 mat4&#39;. See also VL_PIPELINE_PRECISION.
Definition: Matrix4.hpp:1240
static Matrix4 & getTranslation(Matrix4 &out, const Vector3< float > &v)
Definition: Matrix4.hpp:548
EBillboardType mType
Definition: Billboard.hpp:75
EBillboardType type() const
The type of the billboard.
Definition: Billboard.hpp:68
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
const vec3 & axis() const
The rotation axis in world coordinates. Used only for axis aligned billboards.
Definition: Billboard.hpp:61
T normalize(T)
Definition: glsl_math.hpp:1128