MetricGrid.cpp 7.34 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 24 25 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "GLViewer.h"
#include "MetricGrid.h"
#include <cmath>
#include <sstream>




void MetricGrid::displayGrid( GLViewer *viewer,
                              const double spacing,
                              const GLubyte alpha )
{
    const double spacing2 = 2.0 * spacing;
    const double step = 0.1 * spacing;

    // Display the points representing the current grid scale.
    glPointSize( 5.0f );
    glBegin( GL_POINTS );
        if( isAxisEnabled(X) )
        {
            glColor4ub( 255, 100, 100, alpha );
            glVertex3d(  spacing, 0, 0 );
            glVertex3d( -spacing, 0, 0 );
        }
        if( isAxisEnabled(Y) )
        {
            glColor4ub( 100, 255, 100, alpha );
            glVertex3d( 0,  spacing, 0 );
            glVertex3d( 0, -spacing, 0 );
        }
        if( isAxisEnabled(Z) )
        {
            glColor4ub( 100, 100, 255, alpha );
            glVertex3d( 0, 0,  spacing );
            glVertex3d( 0, 0, -spacing );
        }
    glEnd();

    // display the lines corresponding to the colored axis.
    glLineWidth( 2.0f );
    glBegin( GL_LINES );
        if( isAxisEnabled(X) )
        {
            glColor3ub( 255, 100, 100 );
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
            glColor4ub( 255, 100, 100, alpha );
            glVertex3d( -spacing2, 0, 0 );
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
            glVertex3d(  spacing2, 0, 0 );
        }
        else if( isGridEnabled(XY) || isGridEnabled(ZX) )
        {
            glColor3ub( 220, 220, 220 );
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
            glColor4ub( 220, 220, 220, alpha );
            glVertex3d( -spacing2, 0, 0 );
            glVertex3d( -0.1*spacing2, 0, 0 );
            glVertex3d(  0.1*spacing2, 0, 0 );
            glVertex3d(  spacing2, 0, 0 );
        }
        if( isAxisEnabled(Y) )
        {
            glColor3ub( 100, 255, 100 );
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
            glColor4ub( 100, 255, 100, alpha );
            glVertex3d( 0, -spacing2, 0 );
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
            glVertex3d( 0,  spacing2, 0 );
        }
        else if( isGridEnabled(XY) || isGridEnabled(YZ) )
        {
            glColor3ub( 220, 220, 220 );
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
            glColor4ub( 220, 220, 220, alpha );
            glVertex3d( 0, -spacing2, 0 );
            glVertex3d( 0, -0.1*spacing2, 0 );
            glVertex3d( 0,  0.1*spacing2, 0 );
            glVertex3d( 0,  spacing2, 0 );
        }
        if( isAxisEnabled(Z) )
        {
            glColor3ub( 100, 100, 255 );
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
            glColor4ub( 100, 100, 255, alpha );
            glVertex3d( 0, 0, -spacing2 );
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
            glVertex3d( 0, 0,  spacing2 );
        }
        else if( isGridEnabled(YZ) || isGridEnabled(ZX) )
        {
            glColor3ub( 220, 220, 220 );
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
            glColor4ub( 220, 220, 220, alpha );
            glVertex3d( 0, 0, -spacing2 );
            glVertex3d( 0, 0, -0.1*spacing2 );
            glVertex3d( 0, 0,  0.1*spacing2 );
            glVertex3d( 0, 0,  spacing2 );
        }
    glEnd();

    // Display lines corresponding to the grid spacing.
    glLineWidth( 1.0f );
    glColor4ub( 220, 220, 220, alpha );
    glBegin( GL_LINES );
    if( isGridEnabled(XY) )
        for( int i=1; i<=9; ++i )
        {
            double f = i * step;
            glVertex3d( -spacing, f, 0 );
            glVertex3d(  spacing, f, 0 );
            glVertex3d( f, -spacing, 0 );
            glVertex3d( f,  spacing, 0 );
            glVertex3d( -spacing, -f, 0 );
            glVertex3d(  spacing, -f, 0 );
            glVertex3d( -f, -spacing, 0 );
            glVertex3d( -f,  spacing, 0 );
        }
    if( isGridEnabled(YZ) )
        for( int i=1; i<=9; ++i )
        {
            double f = i * step;
            glVertex3d( 0, -spacing, f );
            glVertex3d( 0,  spacing, f );
            glVertex3d( 0, f, -spacing );
            glVertex3d( 0, f,  spacing );
            glVertex3d( 0, -spacing, -f );
            glVertex3d( 0,  spacing, -f );
            glVertex3d( 0, -f, -spacing );
            glVertex3d( 0, -f,  spacing );
        }
    if( isGridEnabled(ZX) )
        for( int i=1; i<=9; ++i )
        {
            double f = i * step;
            glVertex3d( -spacing, 0, f );
            glVertex3d(  spacing, 0, f );
            glVertex3d( f, 0, -spacing );
            glVertex3d( f, 0,  spacing );
            glVertex3d( -spacing, 0, -f );
            glVertex3d(  spacing, 0, -f );
            glVertex3d( -f, 0, -spacing );
            glVertex3d( -f, 0,  spacing );
        }
    glEnd();

    // Display labels representing the current grid scale.
    std::stringstream strStream;
    strStream << spacing;
    QString spacingStr = QString::fromStdString( strStream.str() );

    if( isAxisEnabled(X) )
    {
        glColor4ub( 255, 130, 130, alpha );
        viewer->renderText( spacing, spacing/40.0, spacing/40.0, "X = "+spacingStr );
    }
    if( isAxisEnabled(Y) )
    {
        glColor4ub( 200, 255, 200, alpha );
        viewer->renderText( spacing/40.0, spacing, spacing/40.0, "Y = "+spacingStr );
    }
    if( isAxisEnabled(Z) )
    {
        glColor4ub( 130, 130, 255, alpha );
        viewer->renderText( spacing/40.0, spacing/40.0, spacing, "Z = "+spacingStr );
    }
}


void MetricGrid::display( GLViewer *viewer )
{
    glPushAttrib( GL_LIGHTING         |
                  GL_CURRENT_BIT      |
                  GL_LINE_BIT         |
                  GL_POINT_BIT        |
                  GL_HINT_BIT         |
                  GL_COLOR_BUFFER_BIT );


    // Determine the current grid spacing wrt. the distance between the viewpoint and the focus point.
    double log10Dist = std::log10( 1.75*viewer->distToFocusPoint() );
    int log10DistI = (int) std::floor( log10Dist );
    double scaleFactor = std::pow( 10.0, (double)log10DistI );


    // Determine the alpha value for smooth transitions between successive grid scales.
    GLubyte alpha;
    if( log10Dist-log10DistI < 0.5 )
    {
        double t = (log10Dist-log10DistI) / 0.5;
        t = 0.5 + 0.5*std::cos( t*3.1415926535897932384626433832795 );
        alpha = (GLubyte)( 255.0*t*t );
    }
    else
        alpha = 0;


    // Display the grid using three different grid spacings.
    glDisable( GL_LIGHTING );
    glEnable( GL_LINE_SMOOTH );
    glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
    glEnable( GL_POINT_SMOOTH );
    glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );

    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    if( alpha != 255 )
        displayGrid( viewer, 10.0*scaleFactor, 255-alpha );
    displayGrid( viewer, scaleFactor, 255 );
    if( alpha )
        displayGrid( viewer, 0.1*scaleFactor, alpha );


    glPopAttrib();
}