PlaneNode.cc 6.16 KB
Newer Older
Dirk Wilden's avatar
Dirk Wilden committed
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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  OpenFlipper is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision: 5600 $
//   $Author: wilden $
//   $Date: 2009-04-09 09:16:26 +0200 (Do, 09. Apr 2009) $
//
//=============================================================================

#include "PlaneNode.hh"
#include "../GL/gl.hh"
#include <iostream>
#include <math.h>

#include "TranslationManipulatorNode.hh"

namespace ACG {
namespace SceneGraph {


//== IMPLEMENTATION ==========================================================


void PlaneNode::boundingBox(Vec3f& /*_bbMin*/, Vec3f& /*_bbMax*/)
{
	//_bbMin.minimize( Vect3f  )
}

//----------------------------------------------------------------------------

unsigned int PlaneNode::availableDrawModes() const
{
  return ( DrawModes::POINTS |
	        DrawModes::POINTS_SHADED |
	        DrawModes::POINTS_COLORED );
}

//----------------------------------------------------------------------------

David Bommes's avatar
David Bommes committed
62
void PlaneNode::drawPlane( GLState&  /*_state*/) {
Dirk Wilden's avatar
Dirk Wilden committed
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

  Vec3f u1 = width_  * 0.5f * u_;
  Vec3f v1 = height_ * 0.5f * v_;
  Vec3f a = u1;
  Vec3f b = v1;
  Vec3f c = -u1;
  Vec3f d = -v1;

  //first draw the lines

  glColor3f( 1.0, 1.0, 1.0 );
  glLineWidth(2.0);

  //draw the plane
  glBegin(GL_QUADS);
    glVertex3fv( &a[0] );
    glVertex3fv( &b[0] );
    glVertex3fv( &c[0] );
    glVertex3fv( &d[0] );
  glEnd();


  glLineWidth(1.0);

  //then the red front side

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

  glPolygonMode(GL_FRONT, GL_FILL);

  glColor4f( 0.6, 0.15, 0.2, 0.5 );

  //draw the plane
  glBegin(GL_QUADS);
    glVertex3fv( &a[0] );
    glVertex3fv( &b[0] );
    glVertex3fv( &c[0] );
    glVertex3fv( &d[0] );
  glEnd();

  //finally the green back side

  glPolygonMode(GL_BACK, GL_FILL);

  glColor4f( 0.1, 0.8, 0.2, 0.5 );

  //draw the plane
  glBegin(GL_QUADS);
    glVertex3fv( &a[0] );
    glVertex3fv( &b[0] );
    glVertex3fv( &c[0] );
    glVertex3fv( &d[0] );
  glEnd();

}

//----------------------------------------------------------------

void PlaneNode::drawPlanePick( GLState&  _state) {

  _state.pick_set_maximum(1);
  _state.pick_set_name(0);

  Vec3f u1 = width_  * 0.5f * u_;
  Vec3f v1 = height_ * 0.5f * v_;
  Vec3f a = u1;
  Vec3f b = v1;
  Vec3f c = -u1;
  Vec3f d = -v1;

  //draw the plane
  glBegin(GL_QUADS);
    glVertex3fv( &a[0] );
    glVertex3fv( &b[0] );
    glVertex3fv( &c[0] );
    glVertex3fv( &d[0] );
  glEnd();
}

//----------------------------------------------------------------

void PlaneNode::draw(GLState&  _state  , unsigned int /*_drawMode*/)
{

  _state.push_modelview_matrix();
  glPushAttrib(GL_COLOR_BUFFER_BIT);
  glPushAttrib(GL_LIGHTING_BIT);

  glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
  glEnable(GL_COLOR_MATERIAL);

  _state.translate(position_[0], position_[1], position_[2]);

  drawPlane(_state);

  glPopAttrib();
  glPopAttrib();
  _state.pop_modelview_matrix();
}

//----------------------------------------------------------------

void PlaneNode::setPosition(const Vec3f& _position, const Vec3f& _normal)
{
  //find a non zero component
  int comp = -1;
  for (int i=0; i < 2; i++)
    if ( _normal[i] != 0.0 ){
      comp = i;
      break;
    }

  if (comp == -1){
    std::cerr << "PlaneNode: normal is invalid!" << std::endl;
    return;
  }

  //compute orthogonal vectors in the plane
  u_[comp] = (-_normal[ (comp + 1) % 3 ] - _normal[(comp + 2) % 3]) / _normal[comp];
  u_[ (comp + 1) % 3 ] = 1;
  u_[ (comp + 2) % 3 ] = 1;
  u_ = u_.normalize();

  v_ = u_ % _normal;
  v_ = v_.normalize();

  position_ = _position;
  normal_   = _normal;
}

//----------------------------------------------------------------

void PlaneNode::setSize(double _width, double _height)
{
  width_ = _width;
  height_ = _height;
}

//----------------------------------------------------------------

void
PlaneNode::pick(GLState& _state, PickTarget _target)
{
  if (_target == PICK_ANYTHING) {

	  _state.push_modelview_matrix();
	  
	  _state.translate(position_[0], position_[1], position_[2]);
	  
	  drawPlanePick(_state);
	  
	  _state.pop_modelview_matrix();
  }
}

//----------------------------------------------------------------

Vec3f PlaneNode::position()
{
  TranslationManipulatorNode* manipNode = dynamic_cast< TranslationManipulatorNode* > (parent());

  if (manipNode)
    return manipNode->matrix().transform_point( position_ );
  else
    return position_;
}

//----------------------------------------------------------------

Vec3f PlaneNode::normal()
{
  TranslationManipulatorNode* manipNode = dynamic_cast< TranslationManipulatorNode* > (parent());

  if (manipNode)
    return manipNode->matrix().transform_vector( normal_ );
  else
    return normal_;
}

//----------------------------------------------------------------

int PlaneNode::objectID()
{
  return objectID_;
}

//----------------------------------------------------------------

void PlaneNode::objectID(int _id)
{
  objectID_ = _id;
}

//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================