00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
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
00062
00063
00064 namespace ACG {
00065 namespace SceneGraph {
00066
00067
00068
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 )
00122 {
00123
00124 glPushAttrib( GL_LIGHTING_BIT );
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
00134 GLMatrixd modelview = _state.modelview();
00135
00136 Vec3d viewDirection(0.0, 1.0, 0.0);
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
00147
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
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
00174 _state.translate(-0.5*gridSize_, 0, -0.5*gridSize_);
00175
00176 glBegin(GL_LINES);
00177
00178
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
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
00189 for ( int i = 0 ; i < vertical ; ++i ) {
00190
00191
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
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
00213 for ( int i = 0 ; i < horizontal; ++i ) {
00214
00215
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
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( );
00242 glPopAttrib( );
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 }
00264 }
00265