1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * A versatile mesh processing toolbox                             o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005                                                \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 
24 #ifndef FILTER_IMG_PATCH_PARAM_PLUGIN__VISIBILITYCHECK_H
25 #define FILTER_IMG_PATCH_PARAM_PLUGIN__VISIBILITYCHECK_H
26 
27 
28 
29 
30 #include <common/meshmodel.h>
31 #include <wrap/glw/glw.h>
32 
33 #define USE_VBO
34 
35 
36 
37 
38 class VisibilityCheck
39 {
40 protected:
41     enum VMarker
42     {
43         V_UNDEFINED ,
44         V_BACKFACE  ,
45         V_VISIBLE   ,
46     };
47 
48     glw::Context                &m_Context;
49     CMeshO                      *m_Mesh;
50     int                         m_meshid;
51     RasterModel                 *m_Raster;
52     std::vector<unsigned char>  m_VertFlag;
53 
54     static VisibilityCheck      *s_Instance;
55 
VisibilityCheck(glw::Context & ctx)56     inline                  VisibilityCheck( glw::Context &ctx ) : m_Context(ctx), m_Mesh(NULL), m_Raster(NULL),m_plugcontext(NULL) {}
~VisibilityCheck()57     virtual                 ~VisibilityCheck()                                                                  {}
58 
59 public:
60     static VisibilityCheck* GetInstance( glw::Context &ctx );
61     static void             ReleaseInstance();
62 
63     virtual void            setMesh(int meshid,CMeshO *mesh )                                 = 0;
64     virtual void            setRaster( RasterModel *mesh )                          = 0;
65     virtual void            checkVisibility()                                       = 0;
66 
isVertVisible(const unsigned int n)67     inline bool             isVertVisible( const unsigned int n ) const             { return m_VertFlag[n]==V_VISIBLE; }
isVertVisible(const CVertexO * v)68     inline bool             isVertVisible( const CVertexO *v ) const                { return isVertVisible( v - &m_Mesh->vert[0] ); }
isVertVisible(const CMeshO::VertexIterator & v)69     inline bool             isVertVisible( const CMeshO::VertexIterator &v ) const  { return isVertVisible( &*v ); }
70 
isFaceVisible(const unsigned int n)71     inline bool             isFaceVisible( const unsigned int n ) const             { return isFaceVisible( &m_Mesh->face[n] ); }
isFaceVisible(const CFaceO * f)72     inline bool             isFaceVisible( const CFaceO *f ) const                  { return isVertVisible(f->cV(0)) || isVertVisible(f->cV(1)) || isVertVisible(f->cV(2)); }
isFaceVisible(const CMeshO::FaceIterator & f)73     inline bool             isFaceVisible( const CMeshO::FaceIterator &f ) const    { return isFaceVisible( &*f ); }
74 
75     MLPluginGLContext* m_plugcontext;
76 };
77 
78 
79 class VisibilityCheck_VMV2002 : public VisibilityCheck
80 {
81     friend class VisibilityCheck;
82 
83 private:
84     glw::RenderbufferHandle m_ColorRB;
85     glw::RenderbufferHandle m_DepthRB;
86     glw::FramebufferHandle  m_FrameBuffer;
87 
88     vcg::Point2i            m_ViewportMin;
89     vcg::Point2i            m_ViewportMax;
90 
91     void        init( std::vector<unsigned char> &visBuffer );
92     bool        iteration( std::vector<unsigned char> &visBuffer );
93     void        release();
94 
VisibilityCheck_VMV2002(glw::Context & ctx)95     inline      VisibilityCheck_VMV2002( glw::Context &ctx ) : VisibilityCheck(ctx) {}
~VisibilityCheck_VMV2002()96     inline      ~VisibilityCheck_VMV2002()                                          {}
97 
98 public:
99     static bool isSupported();
100 
setMesh(int meshid,CMeshO * mesh)101     void        setMesh(int meshid,CMeshO *mesh )                                             { m_Mesh = mesh; m_meshid = meshid; }
setRaster(RasterModel * rm)102     void        setRaster( RasterModel *rm )                                        { m_Raster = rm; }
103     void        checkVisibility();
104 };
105 
106 
107 class VisibilityCheck_ShadowMap : public VisibilityCheck
108 {
109     friend class VisibilityCheck;
110 
111 private:
112     vcg::Matrix44f          m_Pose;
113     vcg::Matrix44f          m_Proj;
114     vcg::Matrix44f          m_ShadowProj;
115     vcg::Point3f            m_Viewpoint;
116     vcg::Point3f            m_ZAxis;
117 
118     glw::Texture2DHandle    m_ShadowMap;
119 
120     glw::Texture2DHandle    m_VertexMap;
121     glw::Texture2DHandle    m_NormalMap;
122     glw::RenderbufferHandle m_ColorBuffer;
123     glw::FramebufferHandle  m_FBuffer;
124 
125     glw::ProgramHandle      m_VisDetectionShader;
126 
127     static bool             s_AreVBOSupported;
128     glw::BufferHandle       m_ShadowVBOVertices;
129     glw::BufferHandle       m_ShadowVBOIndices;
130 
131     void        shadowProjMatrices();
132     void        setupShadowTexture();
133     void        updateShadowTexture();
134     void        initMeshTextures();
135     bool        initShaders();
136 
137                 VisibilityCheck_ShadowMap( glw::Context &ctx );
~VisibilityCheck_ShadowMap()138     inline      ~VisibilityCheck_ShadowMap()    {}
139 
140 public:
141     static bool isSupported();
142 
143     void        setMesh(int meshid,CMeshO *mesh );
144     void        setRaster( RasterModel *rm );
145     void        checkVisibility();
146 };
147 
148 
149 
150 
151 #endif // FILTER_IMG_PATCH_PARAM_PLUGIN__VISIBILITYCHECK_H
152