SelectionTool.cpp 5.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "SelectionTool.h"
#include "GLViewer.h"




std::string SelectionTool::getSelectionShaderFunctions()
{
    return  "uniform sampler2DShadow u_DepthBuffer;"

            "uniform int u_SelectBackFacingGeometry;"

            "uniform mat4 u_CameraFullTransform;"
            "uniform vec3 u_CameraLocation;"

24
25
            "vec2 _toBrushCenter = vec2(0.0);"
            "float _squareDistanceToBrush = 0.0;"
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            "vec3 getClippingCoordinates( vec3 objCoordVertex )"
            "{"
            "    vec4 projVert = u_CameraFullTransform * vec4(objCoordVertex,1.0);"
            "    return projVert.xyz / projVert.w;"
            "}"

            "bool isInsideViewport( vec3 clipCoordVertex )"
            "{"
            "    return  clipCoordVertex.x >= -1.0  &&  clipCoordVertex.x <= 1.0  &&"
            "            clipCoordVertex.y >= -1.0  &&  clipCoordVertex.y <= 1.0  &&"
            "            clipCoordVertex.z >= -1.0  &&  clipCoordVertex.z <= 1.0;"
            "}"

            "bool passCullingTest( vec3 objCoordVertex, vec3 objCoordNormal )"
            "{"
            "    return u_SelectBackFacingGeometry != 0  ||  dot(u_CameraLocation-objCoordVertex,objCoordNormal) >= 0.0;"
            "}"

            "bool passOcclusionTest( vec3 objCoordVertex )"
            "{"
            "    vec4 projVert = u_CameraFullTransform * vec4(objCoordVertex,1.0);"
            "    projVert.xyz = 0.5*projVert.xyz + vec3(0.5*projVert.w);"
            "    projVert.z -= 0.0001*projVert.w;"

            "    return textureProj( u_DepthBuffer, projVert ) > 0.5;"
52
            "}"
53

54
            "float squareDistanceToBrush()"
55
            "{"
56
57
            "   return _squareDistanceToBrush;"
            "}"
58

59
60
61
            "vec2 toBrushCenter()"
            "{"
            "   return _toBrushCenter;"
62
63
64
65
66
67
68
69
            "}";
}


void SelectionTool::displaySelectionState() const
{
    // Backup the current OpenGL state.

70
    GLboolean depthTestEnabled = glIsEnabled( GL_DEPTH_TEST );
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    GLboolean multisampleEnabled = glIsEnabled( GL_MULTISAMPLE );
    GLfloat currentColor[4];
    glGetFloatv( GL_CURRENT_COLOR, currentColor );
    GLfloat lineWidth;
    glGetFloatv( GL_LINE_WIDTH, &lineWidth );


    // Initialize OpenGL matrices.

    GLint matrixMode;
    glGetIntegerv( GL_MATRIX_MODE, &matrixMode );

    glMatrixMode( GL_PROJECTION );
    glPushMatrix();
    glLoadIdentity();
    glOrtho( 0.0, m_Viewer.width(), 0.0, m_Viewer.height(), -1.0, 1.0 );

    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();
    glLoadIdentity();

92
93
94
    glDisable( GL_DEPTH_TEST );
    //glDisable( GL_MULTISAMPLE );

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    // Display the "+" or "-" sign indicating what is the current selection update mode.

    QPoint cursor = m_Viewer.mapFromGlobal( m_Viewer.cursor().pos() );
    cursor.setY( m_Viewer.height() - 1 - cursor.y() );

    glColor3ub( 0, 0, 0 );

    if( m_Context.selectionMode != SelectionTool::SELECTION_MODE_SET )
    {
        glLineWidth( 1.0f );
        glBegin( GL_LINES );
            glVertex2f( cursor.x()+ 8, cursor.y()+11 );
            glVertex2f( cursor.x()+14, cursor.y()+11 );
            if( m_Context.selectionMode == SelectionTool::SELECTION_MODE_ADD )
            {
                glVertex2f( cursor.x()+11, cursor.y()+ 8 );
                glVertex2f( cursor.x()+11, cursor.y()+14 );
            }
        glEnd();
    }


    // Display the label indicating which entity is currently selected.

120
    QString entityName;
121
122
    switch( m_Context.entityToSelect )
    {
123
124
125
        case SELECTABLE_ENTITY_VERTEX:  entityName = "Vertex"; break;
        case SELECTABLE_ENTITY_FACE  :  entityName = "Face"  ; break;
        case SELECTABLE_ENTITY_TEXEL :  entityName = "Texel" ; break;
126
127
    }

128
129
130
131
132
133
134
135
136
137
138
139
    int entityNameX = cursor.x() + 10;
    int entityNameY = m_Viewer.height() - 1 - cursor.y() + 15;

    glColor4ub( 255, 255, 255, 192 );
    m_Viewer.renderText( entityNameX, entityNameY-1, entityName );
    m_Viewer.renderText( entityNameX, entityNameY+1, entityName );
    m_Viewer.renderText( entityNameX-1, entityNameY, entityName );
    m_Viewer.renderText( entityNameX+1, entityNameY, entityName );

    glColor3ub( 0, 0, 0 );
    m_Viewer.renderText( entityNameX, entityNameY, entityName );

140
141
142
143
144
145
146
147
148
149
150

    // Restore OpenGL matrices.

    glPopMatrix();
    glMatrixMode( GL_PROJECTION );
    glPopMatrix();
    glMatrixMode( matrixMode );


    // Restore the backuped OpenGL state.

151
    depthTestEnabled?  glEnable( GL_DEPTH_TEST )  :  glDisable( GL_DEPTH_TEST );
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    multisampleEnabled?  glEnable( GL_MULTISAMPLE )  :  glDisable( GL_MULTISAMPLE );
    glColor4fv( currentColor );
    glLineWidth( lineWidth );
}


void SelectionTool::initSelectionShader( GPU::Shader &shader )
{
    QMatrix4x4 mv, pj;
    glGetFloatv( GL_MODELVIEW_MATRIX , mv.data() );
    glGetFloatv( GL_PROJECTION_MATRIX, pj.data() );

    QVector3D camLoc( -QVector3D::dotProduct( mv.column(0).toVector3D(), mv.column(3).toVector3D() ),
                      -QVector3D::dotProduct( mv.column(1).toVector3D(), mv.column(3).toVector3D() ),
                      -QVector3D::dotProduct( mv.column(2).toVector3D(), mv.column(3).toVector3D() ) );

    shader.SetUniform( "u_CameraFullTransform"     , (pj*mv).data() );
    shader.SetUniform( "u_CameraLocation"          , &camLoc[0] );
    shader.SetUniform( "u_SelectBackFacingGeometry", &m_Context.selectBackFacingGeometry );

    shader.RemoveAllSamplerBindings();
    shader.SetSamplerBinding( "u_DepthBuffer", &m_Context.depthBuffer );
}