GridNode.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6856 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-12 08:29:55 +0200 (Mi, 12. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS GridNode - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 //== INCLUDES =================================================================
00053 
00054 
00055 #include "GridNode.hh"
00056 #include "SceneGraph.hh"
00057 #include "../GL/gl.hh"
00058 #include <stdio.h>
00059 #include <math.h>
00060 
00061 //== NAMESPACES ===============================================================
00062 
00063 
00064 namespace ACG {
00065 namespace SceneGraph {
00066 
00067 
00068 //== IMPLEMENTATION ==========================================================
00069 
00070 
00071 GridNode::
00072 GridNode(BaseNode* _parent, const std::string& _name)
00073   : MaterialNode(_parent, _name),
00074     horizontalLines_(7),
00075     verticalLines_(7),
00076     maxRefinement_(49),
00077     gridSize_(1500.0)
00078 {
00079   baseLineColor_ = Vec3f(0.5, 0.5, 0.5);
00080   midLineColor_  = Vec3f(0.3, 0.3, 0.3);
00081   bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
00082   bb_max_ = Vec3f( 0.5*gridSize_, 0.0,  0.5*gridSize_);
00083 }
00084 
00085 
00086 //-----------------------------------------------------------------------------
00087 
00088 
00089 unsigned int
00090 GridNode::availableDrawModes() const
00091 {
00092   return ( DrawModes::WIREFRAME           |
00093                 DrawModes::SOLID_FLAT_SHADED   );
00094 }
00095 
00096 
00097 //-----------------------------------------------------------------------------
00098 
00099 
00100 void
00101 GridNode::boundingBox(Vec3f& _bbMin, Vec3f& _bbMax)
00102 {
00103   _bbMin.minimize(bb_min_);
00104   _bbMax.maximize(bb_max_);
00105 }
00106 
00107 
00108 //----------------------------------------------------------------
00109 
00110 void
00111 GridNode::pick(GLState& _state, PickTarget _target)
00112 {
00113 
00114 
00115 }
00116 
00117 //-----------------------------------------------------------------------------
00118 
00119 
00120 void
00121 GridNode::draw(GLState&  _state  , unsigned int /* _drawMode */ )
00122 {
00123 
00124   glPushAttrib( GL_LIGHTING_BIT ); // STACK_ATTRIBUTES <- LIGHTING_ATTRIBUTE
00125   glDisable(GL_LIGHTING);
00126 
00127   glPushAttrib( GL_DEPTH_TEST );
00128   glEnable( GL_DEPTH_TEST );
00129 
00130   glLineWidth(0.1);
00131   _state.push_modelview_matrix();
00132 
00133   //first we calculate the hitpoint of the camera direction with the grid
00134   GLMatrixd modelview = _state.modelview();
00135 
00136   Vec3d viewDirection(0.0, 1.0, 0.0); // = _state.viewing_direction();
00137   Vec3f eye = (Vec3f) _state.eye();
00138 
00139   double lambda = - (eye[1] / viewDirection[1]);
00140 
00141   Vec3f hitPoint;
00142   hitPoint[0] = eye[0] + lambda * viewDirection[0];
00143   hitPoint[1] = 0;
00144   hitPoint[2] = eye[2] + lambda * viewDirection[2];
00145 
00146   //the distance between camera and hitpoint defines a factor that controls
00147   //how many lines are drawn
00148   double distance = _state.modelview().transform_point( hitPoint ).norm();
00149 
00150   int factor =  floor(2000.0 / distance) - 1;
00151 
00152   int vertical = verticalLines_;
00153   int horizontal = horizontalLines_;
00154 
00155   if (factor > 20){
00156     vertical = maxRefinement_;
00157     horizontal = maxRefinement_;
00158 
00159   } else if (factor > 0){
00160     // 'factor' times split the grid (split every cell into 4 new cells)
00161     int rest = 0;
00162 
00163     for (int i=0; i < factor; i++)
00164       rest -= floor( pow(double(2.0), i));
00165 
00166     vertical   = vertical   * floor( pow(double(2.0),factor)) + rest;
00167     horizontal = horizontal * floor( pow(double(2.0),factor)) + rest;
00168 
00169     vertical   = std::min(vertical, maxRefinement_ );
00170     horizontal = std::min(vertical, maxRefinement_ );
00171   }
00172 
00173   //now start drawing
00174   _state.translate(-0.5*gridSize_, 0, -0.5*gridSize_);
00175 
00176   glBegin(GL_LINES);
00177 
00178     //red line (x-axis)
00179     glColor3f( 0.7, 0.0, 0.0 );
00180     glVertex3f(   0.0, 0.0, gridSize_*0.5);
00181     glVertex3f(gridSize_, 0.0, gridSize_*0.5);
00182 
00183     //blue line (z-axis)
00184     glColor3f( 0.0, 0.0, 0.7 );
00185     glVertex3f(gridSize_*0.5, 0.0,     0.0);
00186     glVertex3f(gridSize_*0.5, 0.0, gridSize_);
00187 
00188     //remaining vertical lines
00189     for ( int i = 0 ; i <  vertical ; ++i ) {
00190 
00191       //first draw a baseLine
00192       glColor3fv( &baseLineColor_[0] );
00193 
00194       double big  = gridSize_ / (vertical-1) * i;
00195       double next = gridSize_ / (vertical-1) * (i+1);
00196 
00197       glVertex3f( big, 0.0, 0.0);
00198       glVertex3f( big, 0.0, gridSize_);
00199 
00200       if ( i+1 < vertical)
00201         for (int j=1; j < 10; j++){
00202 
00203           //then draw 9 darker lines in between
00204           glColor3fv( &midLineColor_[0] );
00205   
00206           double smallPos = big + (next - big) / 10.0 * j;
00207           glVertex3f( smallPos, 0.0, 0.0);
00208           glVertex3f( smallPos, 0.0, gridSize_);
00209         }
00210     }
00211 
00212     //remaining horizontal lines
00213     for ( int i = 0 ; i <  horizontal; ++i ) {
00214 
00215       //first draw a baseline
00216       glColor3fv( &baseLineColor_[0] );
00217 
00218       double big  = gridSize_ / (vertical-1) * i;
00219       double next = gridSize_ / (vertical-1) * (i+1);
00220 
00221       glVertex3f(   0.0, 0.0, gridSize_ / (horizontal-1) * i);
00222       glVertex3f(gridSize_, 0.0, gridSize_ / (horizontal-1) * i);
00223 
00224       if ( i+1 < vertical)
00225         for (int j=1; j < 10; j++){
00226 
00227           //then draw 9 darker lines in between
00228           glColor3fv( &midLineColor_[0] );
00229   
00230           double smallPos = big + (next - big) / 10.0 * j;
00231           glVertex3f(   0.0, 0.0, smallPos);
00232           glVertex3f(gridSize_, 0.0, smallPos);
00233         }   
00234     }
00235   glEnd();
00236 
00237   _state.pop_modelview_matrix();
00238 
00239   glLineWidth(1.0);
00240 
00241   glPopAttrib( ); // ->Depth test
00242   glPopAttrib( ); // ->Lighting
00243 }
00244 
00245 //-----------------------------------------------------------------------------
00246 
00247 void
00248 GridNode::gridSize(float _size){
00249   gridSize_ = _size;
00250 
00251   bb_min_ = Vec3f(-0.5*gridSize_, 0.0, -0.5*gridSize_);
00252   bb_max_ = Vec3f( 0.5*gridSize_, 0.0,  0.5*gridSize_);
00253 }
00254 
00255 //-----------------------------------------------------------------------------
00256 
00257 float
00258 GridNode::gridSize(){
00259   return gridSize_;
00260 }
00261 
00262 //=============================================================================
00263 } // namespace SceneGraph
00264 } // namespace ACG
00265 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .