LightNode.cc 23.7 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  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 with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  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 LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
\*===========================================================================*/




//=============================================================================
//
//  CLASS LightNode - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================


#include "LightNode.hh"
Jan Möbius's avatar
Jan Möbius committed
57
#include <ACG/GL/IRenderer.hh>
Jan Möbius's avatar
Jan Möbius committed
58

Jan Möbius's avatar
Jan Möbius committed
59
#ifndef M_PI
Mike Kremer's avatar
Mike Kremer committed
60
#define M_PI 3.14159265358979323846
Jan Möbius's avatar
Jan Möbius committed
61
#endif
Jan Möbius's avatar
Jan Möbius committed
62
63
64
65
66
67
68
69

//== NAMESPACES ===============================================================

namespace ACG {
namespace SceneGraph {

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

70
71
static LightSourceHandle* lightSourceHandle = 0;

Jan Möbius's avatar
Jan Möbius committed
72
73
74
75
76
77
78
//default Constructor
LightSource::LightSource()
{
  // set OpenGL defaults
  enabled_              = false;
  fixedPosition_        = false;

Matthias Möller's avatar
Matthias Möller committed
79
80
81
  ambientColor_         = Vec4f(0.1f,0.1f,0.1f,1.f);
  diffuseColor_         = Vec4f(1.f,1.f,1.f,1.f);
  specularColor_        = Vec4f(1.f,1.f,1.f,1.f);
Jan Möbius's avatar
Jan Möbius committed
82
  
Matthias Möller's avatar
Matthias Möller committed
83
84
  position_             = Vec4f(0.f,0.f,1.f,0.f);
  realPosition_         = Vec4f(0.f,0.f,1.f,0.f);
Jan Möbius's avatar
Jan Möbius committed
85
  
86
  spotDirection_        = Vec3d(0.0,0.0,-1.0);
Mike Kremer's avatar
Mike Kremer committed
87
88
  realSpotDirection_    = Vec3d(0.0,0.0,-1.0);
  
89
90
  // Holds initial light source position
  // converted to camera coordinates
Matthias Möller's avatar
Matthias Möller committed
91
  initialPosition_      = Vec4f(0.f, 0.f, 0.f, 0.f);
92
93
94
  initialSpotDirection_ = Vec3d(0.0, 0.0, -1.0);
  initialPositionInit_  = false;
  
Jan Möbius's avatar
Jan Möbius committed
95
  spotExponent_         = 0;
96
  spotCutoff_           = 180;
Jan Möbius's avatar
Jan Möbius committed
97
98
99
100

  constantAttenuation_  = 1;
  linearAttenuation_    = 0;
  quadraticAttenuation_ = 0;
101
  
102
103
  brightness_           = 1.0f;
  
104
  radius_               = 0.1f;
Jan Möbius's avatar
Jan Möbius committed
105
106
}

107
void LightSource::position( Vec3d _pos){ 
Jan Möbius's avatar
Jan Möbius committed
108
  // Set homogeneous coordinte to 1.0 to get a positional light source
109
  position_ = Vec4d( _pos[0],_pos[1],_pos[2],1.0); 
Jan Möbius's avatar
Jan Möbius committed
110
111
}

112
113
Vec3d LightSource::position() {
  return Vec3d( position_[0], position_[1], position_[2]);
Jan Möbius's avatar
Jan Möbius committed
114
115
}

116
void LightSource::direction( Vec3d _pos) { 
Jan Möbius's avatar
Jan Möbius committed
117
118
  // Set homogeneous coordinate of position to 0.0 to tell OpenGL
  // that this is a directional light source
119
  position_ = Vec4d( _pos[0],_pos[1],_pos[2],0.0); 
Jan Möbius's avatar
Jan Möbius committed
120
121
}

122
123
Vec3d LightSource::direction() {
    return Vec3d(position_[0], position_[1], position_[2]);
124
125
}

Jan Möbius's avatar
Jan Möbius committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
bool LightSource::directional() {
  return ( position_[3] == 0.0  );
}

void LightSource::enable()
{ enabled_ = true; }

void LightSource::disable()
{ enabled_ = false; }

bool LightSource::enabled() {
  return enabled_;
}

140
void LightSource::spotDirection( Vec3d _pos)
Jan Möbius's avatar
Jan Möbius committed
141
142
{ spotDirection_ = _pos; }

143
144
Vec3d LightSource::spotDirection( ) { 
  return Vec3d(spotDirection_[0],spotDirection_[1],spotDirection_[2]); 
Jan Möbius's avatar
Jan Möbius committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
}  

void LightSource::ambientColor(  Vec4f _color)
{ ambientColor_ = _color; }

Vec4f LightSource::ambientColor()
{ return ambientColor_; }

void LightSource::diffuseColor(  Vec4f _color)
{ diffuseColor_ = _color; }

Vec4f LightSource::diffuseColor()
{ return diffuseColor_; }

void LightSource::specularColor(  Vec4f _color)
{ specularColor_ = _color; }

Vec4f LightSource::specularColor()
{ return specularColor_; }

165
166
167
168
169
170
void LightSource::setColor(const Vec4f& _ambient, const Vec4f& _diffuse, const Vec4f& _specular) {
    ambientColor_ = _ambient;
    diffuseColor_ = _diffuse;
    specularColor_ = _specular;
}

Jan Möbius's avatar
Jan Möbius committed
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
void LightSource::fixedPosition( bool _state)
{ fixedPosition_ = _state; }

bool LightSource::fixedPosition() {
  return fixedPosition_;   
}

void LightSource::spotExponent(float _exponent) {
    spotExponent_ = _exponent;
}

float LightSource::spotExponent() {
    return spotExponent_;
}

void LightSource::spotCutoff(float _cutoff) {
    spotCutoff_ = _cutoff;
}

float LightSource::spotCutoff() {
    return spotCutoff_;
}

void LightSource::constantAttenuation(float _constantAttenuation) {
    constantAttenuation_ = _constantAttenuation;
}

float LightSource::constantAttenuation() {
    return constantAttenuation_;
}

void LightSource::linearAttenuation(float _linearAttenuation) {
    linearAttenuation_ = _linearAttenuation;
}

float LightSource::linearAttenuation() {
    return linearAttenuation_;
}

void LightSource::quadraticAttenuation(float _quadraticAttenuation) {
    quadraticAttenuation_ = _quadraticAttenuation;
}

float LightSource::quadraticAttenuation() {
    return quadraticAttenuation_;
}

218
219
220
void LightSource::brightness(float _brightness) {
    brightness_ = _brightness;
}
Jan Möbius's avatar
Jan Möbius committed
221

222
223
224
float LightSource::brightness() const {
    return brightness_;
}
Jan Möbius's avatar
Jan Möbius committed
225

226
227
LightNode::LightNode( BaseNode* _parent, 
    const std::string&   _name) 
Jan Möbius's avatar
Jan Möbius committed
228
  : BaseNode(_parent, _name),
229
230
    visualize_(false),
    lightId_(GL_INVALID_ENUM) {
231
  
232
233
234
    if(lightSourceHandle == 0) {
        lightSourceHandle = new LightSourceHandle();
    }
235
236
237

    sphere_ = new ACG::GLSphere(10, 10);
    cone_ = new ACG::GLCone(10, 10, 1.0f, 1.0f, false, true);
Jan Möbius's avatar
Jan Möbius committed
238
239
}

240
241
242
//----------------------------------------------------------------------------

LightNode::~LightNode() {
243
244
245
246
247
  if (sphere_)
    delete sphere_;

  if (cone_)
    delete cone_;
248
}
Jan Möbius's avatar
Jan Möbius committed
249
250
    
//----------------------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
251
252
253
254
255
256
257

void LightNode::getLightSource(LightSource* _light) const
{
  memcpy(_light, &light_, sizeof(LightSource));
}

//----------------------------------------------------------------------------
Jan Möbius's avatar
Jan Möbius committed
258

259
260
261
262
263
264
265
void LightNode::getLightSourceViewSpace( LightSource* _light ) const
{
  memcpy(_light, &transformedLight_, sizeof(LightSource));
}

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

266
267
268
void LightNode::boundingBox(ACG::Vec3d& _bbMin, ACG::Vec3d& _bbMax) {
    
    if( visualize_ && !light_.directional() ) {
269
270
271
272
273
274
275
        ACG::Vec3d r;
        if(light_.fixedPosition())
            r = ACG::Vec3d((double)light_.realPosition_[0],
                           (double)light_.realPosition_[1],
                           (double)light_.realPosition_[2]);
        else
            r = light_.position();
276
277
        _bbMin.minimize( r - Vec3d(light_.radius()*3) );
        _bbMax.maximize( r + Vec3d(light_.radius()*3) );
278
279
280
281
282
    }
}

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

283
void LightNode::draw(GLState& _state, const DrawModes::DrawMode& /*_drawMode*/) {
284
285
286
287
    
    // Visualize light node
    if(visualize_ && !light_.directional()) {
        
288
289
290
291
292
293
294
295
        // Get initial camera coords of light if in fixed
        // mode and if they haven't been computed yet
        if(light_.fixedPosition_ && !light_.initialPositionInit_) {
            light_.initialPosition_ = _state.modelview() * light_.position_;
            light_.initialSpotDirection_ = _state.modelview().transform_vector(light_.spotDirection());
            light_.initialPositionInit_ = true;
        }
        
Mike Kremer's avatar
Mike Kremer committed
296
        if(light_.fixedPosition_) {
297
298
             light_.realPosition_ = _state.inverse_modelview() * light_.initialPosition_;
             light_.realSpotDirection_ = _state.inverse_modelview().transform_vector(light_.initialSpotDirection_);
Mike Kremer's avatar
Mike Kremer committed
299
300
301
302
303
304
305
306
         } else {
             light_.realPosition_ = light_.position_;
             light_.realSpotDirection_ = light_.spotDirection_;
         }
         
         ACG::Vec3f p = ACG::Vec3f(light_.realPosition_[0],
                                   light_.realPosition_[1],
                                   light_.realPosition_[2]);
307
         ACG::Vec3d spotDir = light_.realSpotDirection_;          
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
         
         ACG::Vec4f ac = light_.ambientColor();
         ACG::Vec4f dc = light_.diffuseColor();
         ACG::Vec4f sc = light_.specularColor();
         
         // Make light sources appear as bright as possible
         float max = 0;
         for(int i = 0; i < 3; ++i) {
             if(ac[i] > max) max = ac[i];
         }
         ac += ACG::Vec4f(1.0f - max);
         max = 0;
         for(int i = 0; i < 3; ++i) {
             if(dc[i] > max) max = dc[i];
         }
         dc += ACG::Vec4f(1.0f - max);
         max = 0;
         for(int i = 0; i < 3; ++i) {
             if(sc[i] > max) max = sc[i];
         }
         sc += ACG::Vec4f(1.0f - max);
329
      
330
         // Backup variables
Mike Kremer's avatar
Mike Kremer committed
331
332
333
         GLboolean lighting_backup;
 
         // Origin
334
         _state.push_modelview_matrix();
Mike Kremer's avatar
Mike Kremer committed
335
336
337
338
339
         // Transform to light origin and direction
         _state.translate(p[0], p[1], p[2]);
             
         // Set lighting
         glGetBooleanv(GL_LIGHTING, &lighting_backup);
340
         ACG::GLState::enable(GL_LIGHTING);
Mike Kremer's avatar
Mike Kremer committed
341
 
342
343
344
345
346
347
348
349
350
351
352
353
         // Make light directional just for the drawing
         // of itself
         bool backup_directional = light_.directional();
         ACG::Vec3d backup_position = light_.position();
         
         // Get light id
         lightId_ = lightSourceHandle->getLight(this);
    
         // Return if we don't have a valid light source
         if(lightId_ == GL_INVALID_ENUM) {
             
             // Reset all stored attributes before returning
354
             if(!lighting_backup) ACG::GLState::disable(GL_LIGHTING);
Mike Kremer's avatar
Mike Kremer committed
355
 
356
357
358
359
360
361
             _state.pop_modelview_matrix();
             
             return;
         }
         
         glLightf(lightId_, GL_SPOT_EXPONENT, 0.0f);
362
363
364
365
366
367
         float pos[4];
         pos [0] = backup_position[0];
         pos [1] = backup_position[1];
         pos [2] = backup_position[2];
         pos [3] = 0.0f;

368
369
370
371
372
373
374
375
376
377
         glLightfv(lightId_, GL_POSITION, pos);
         
         // Set colors
         float gl_ac[] = {ac[0], ac[1], ac[2], ac[3]};
         glLightfv(lightId_, GL_AMBIENT, gl_ac);
         float gl_dc[] = {dc[0], dc[1], dc[2], dc[3]};
         glLightfv(lightId_, GL_DIFFUSE, gl_dc);
         float gl_sc[] = {sc[0], sc[1], sc[2], sc[3]};
         glLightfv(lightId_, GL_SPECULAR, gl_sc);
         
378
         ACG::GLState::enable(lightId_);
379
         
380
         sphere_->draw(_state, light_.radius());
Mike Kremer's avatar
Mike Kremer committed
381
382
383
384
385
386
387
388
 
         // Visualize spot cone (or direction)
         if(light_.spotCutoff() < 180.0f) {
             // Note: if the cutoff angle is 180, the light source
             // is a point light emitting light into all directions equally
             
             // Rotate into light direction
             ACG::Vec3d z = ACG::Vec3d(0.0f, 0.0f, 1.0f);
389
             ACG::Vec3d spot = spotDir;
Mike Kremer's avatar
Mike Kremer committed
390
391
392
393
394
             float angle = acos((z | spot)/(z.norm()*spot.norm()));
             angle = angle*360/(2*M_PI);
             ACG::Vec3d rA = z % spot;
             _state.rotate(angle, rA[0], rA[1], rA[2]);
 
395
             // Inverse normal orientation
396
397
398
399
             cone_->setNormalOrientation(ACG::GLPrimitive::INSIDE);
             cone_->setBottomRadius(light_.radius()/6.0f);
             cone_->setTopRadius(light_.radius()/6.0f);
             cone_->draw(_state, light_.radius()*2.0f);
Mike Kremer's avatar
Mike Kremer committed
400
401
             _state.translate(0.0, 0.0, light_.radius()*2);
             // Draw arrow tip
402
403
404
             cone_->setBottomRadius(light_.radius()/2.0f);
             cone_->setTopRadius(0.0f);
             cone_->draw(_state, light_.radius());
Mike Kremer's avatar
Mike Kremer committed
405
         }
406
407
408
         
         // Free light id
         lightSourceHandle->removeLight(this);
Mike Kremer's avatar
Mike Kremer committed
409
410
 
         // Undo state changes
411
412
413
414
         
         if(!backup_directional) {
             light_.position(backup_position);
         }
Mike Kremer's avatar
Mike Kremer committed
415
416
 
         // Lighting
417
         if(!lighting_backup) ACG::GLState::disable(GL_LIGHTING);
Mike Kremer's avatar
Mike Kremer committed
418
419
420
 
         _state.pop_modelview_matrix();
     }
421

422
423
}

424
425
426
427
428
429
430
431
432
433
void LightNode::pick(GLState& _state, PickTarget _target) {

    GLenum prev_depth = _state.depthFunc();
    
    if (_target == PICK_FACE ||
       _target == PICK_ANYTHING) {
        
        // Visualize light node
        if(visualize_ && !light_.directional()) {
            
434
435
436
437
438
439
440
441
            // Get initial camera coords of light if in fixed
            // mode and if they haven't been computed yet
            if(light_.fixedPosition_ && !light_.initialPositionInit_) {
                light_.initialPosition_ = _state.modelview() * light_.position_;
                light_.initialSpotDirection_ = _state.modelview().transform_vector(light_.spotDirection());
                light_.initialPositionInit_ = true;
            }
            
442
            if(light_.fixedPosition_) {
443
444
                 light_.realPosition_ = _state.inverse_modelview() * light_.initialPosition_;
                 light_.realSpotDirection_ = _state.inverse_modelview().transform_vector(light_.initialSpotDirection_);
445
446
447
448
449
450
451
             } else {
                 light_.realPosition_ = light_.position_;
                 light_.realSpotDirection_ = light_.spotDirection_;
             }
             
             // Enable depth test but store original status
             glPushAttrib(GL_DEPTH_BUFFER_BIT);
452
453
             ACG::GLState::enable(GL_DEPTH_TEST);
             ACG::GLState::depthFunc(GL_LEQUAL);
454
455
456
457
458
459
460
461
462
463
464
465
466
467
             
             _state.pick_set_maximum(1);
             _state.pick_set_name(0);
             
             ACG::Vec3f p = ACG::Vec3f(light_.realPosition_[0],
                                       light_.realPosition_[1],
                                       light_.realPosition_[2]);
             ACG::Vec3d spotDir = light_.realSpotDirection_;          
          
             // Origin
             _state.push_modelview_matrix();
             // Transform to light origin and direction
             _state.translate(p[0], p[1], p[2]);
                 
468
             sphere_->draw(_state, light_.radius());
469
470
471
472
473
474
475
476
477
478
479
480
481
482

             // Visualize spot cone (or direction)
             if(light_.spotCutoff() < 180.0f) {
                 // Note: if the cutoff angle is 180, the light source
                 // is a point light emitting light into all directions equally
                 
                 // Rotate into light direction
                 ACG::Vec3d z = ACG::Vec3d(0.0f, 0.0f, 1.0f);
                 ACG::Vec3d spot = spotDir;
                 float angle = acos((z | spot)/(z.norm()*spot.norm()));
                 angle = angle*360/(2*M_PI);
                 ACG::Vec3d rA = z % spot;
                 _state.rotate(angle, rA[0], rA[1], rA[2]);

483
484
485
486
                 cone_->setNormalOrientation(ACG::GLPrimitive::OUTSIDE);
                 cone_->setBottomRadius(light_.radius()/6.0f);
                 cone_->setTopRadius(light_.radius()/6.0f);
                 cone_->draw(_state, light_.radius()*2.0f);
487
488
                 _state.translate(0.0, 0.0, light_.radius()*2);
                 // Draw arrow tip
489
490
491
                 cone_->setBottomRadius(light_.radius()/2.0f);
                 cone_->setTopRadius(0.0f);
                 cone_->draw(_state, light_.radius());
492
493
494
495
             }

             _state.pop_modelview_matrix();
             
496
             ACG::GLState::depthFunc(prev_depth);
497
498
499
500
         }  
    }
}

501
void LightNode::enter(GLState& _state, const DrawModes::DrawMode& /* _drawmode */ ) 
Jan Möbius's avatar
Jan Möbius committed
502
{
503
504
    if(visualize_) return;
    
505
506
507
508
509
510
511
512
    // Get initial camera coords of light if in fixed
    // mode and if they haven't been computed yet
    if(light_.fixedPosition_ && !light_.initialPositionInit_) {
        light_.initialPosition_ = _state.modelview() * light_.position_;
        light_.initialSpotDirection_ = _state.modelview().transform_vector(light_.spotDirection());
        light_.initialPositionInit_ = true;
    }
    
513
514
515
516
517
518
519
520
    // Get light id
    lightId_ = lightSourceHandle->getLight(this);
    
    // Return if we don't have a valid light source
    if(lightId_ == GL_INVALID_ENUM) return;
    
    /// transfer GL-preferences to lightSave_
    getParameters(lightId_, lightSave_);
Jan Möbius's avatar
Jan Möbius committed
521
  
522
523
524
525
526
527
    // save old light
    lightSave_.enabled_ = glIsEnabled(lightId_);

    if(light_.enabled_ ) {
        // correct Position for fixed Lights
        if(light_.fixedPosition_) {
528
529
            light_.realPosition_ = _state.inverse_modelview() * light_.initialPosition_;
            light_.realSpotDirection_ = _state.inverse_modelview().transform_vector(light_.initialSpotDirection_);
530
531
532
            //std::cerr << "New Light pos :" << _state.inverse_modelview().transform_vector(light_.position) << std::endl;
        } else {
            light_.realPosition_ = light_.position_;
Mike Kremer's avatar
Mike Kremer committed
533
            light_.realSpotDirection_ = light_.spotDirection_;
534
535
536
            //std::cerr << "New Light pos :" << light_.position << std::endl;
        }

537
538
539
540
541
        // transform to view space for shader pipeline
        transformedLight_ = light_;
        transformedLight_.position_ = _state.modelview() * light_.realPosition_;
        transformedLight_.spotDirection_ = _state.modelview().transform_vector(light_.realSpotDirection_);

542
        ACG::GLState::enable(lightId_);
543
        setParameters(lightId_, light_);
Jan Möbius's avatar
Jan Möbius committed
544
    } else {
545
        ACG::GLState::disable(lightId_);
Jan Möbius's avatar
Jan Möbius committed
546
547
548
549
550
551
552
    }
}


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


553
void LightNode::leave(GLState& /* _state */ , const DrawModes::DrawMode& /* _drawmode*/ )
Jan Möbius's avatar
Jan Möbius committed
554
{
555
556
557
558
    if(visualize_) return;
    
    // Return if we don't have a valid light source
    if(lightId_ == GL_INVALID_ENUM) return;
559
      
560
561
    // restore old enabled light
    if(lightSave_.enabled_) {
562
        ACG::GLState::enable(lightId_);
563
564
565
        setParameters(lightId_, lightSave_);
    }
    else {
566
        ACG::GLState::disable(lightId_);
567
568
569
570
    }
    
    // Free light id
    lightSourceHandle->removeLight(this);
Jan Möbius's avatar
Jan Möbius committed
571
572
573
574
575
576
577
}

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

void LightNode::setParameters(GLenum _index, LightSource& _light)
{

578
579
580
581
582
583
584
585
586
587
588
589
  // Multiply colors by brightness
  Vec4f& a = _light.ambientColor_;
  GLfloat ambient[4] = {a[0]*_light.brightness_, 
                        a[1]*_light.brightness_,
                        a[2]*_light.brightness_,
                        a[3]*_light.brightness_};

  Vec4f& d = _light.diffuseColor_;
  GLfloat diffuse[4] = {d[0]*_light.brightness_,
                        d[1]*_light.brightness_,
                        d[2]*_light.brightness_,
                        d[3]*_light.brightness_};
590
                        
591
592
593
594
595
596
  Vec4f& s = _light.specularColor_;
  GLfloat specular[4] = {s[0]*_light.brightness_,
                         s[1]*_light.brightness_,
                         s[2]*_light.brightness_,
                         s[3]*_light.brightness_};
  
Jan Möbius's avatar
Jan Möbius committed
597
  // set preferences of _light for GL_LIGHT#_index
598
599
600
  glLightfv(_index, GL_AMBIENT,  ambient);
  glLightfv(_index, GL_DIFFUSE,  diffuse);
  glLightfv(_index, GL_SPECULAR, specular);
Jan Möbius's avatar
Jan Möbius committed
601

602
603
  Vec3d& sd = _light.realSpotDirection_;
  GLfloat dir[3] = {(float)sd[0], (float)sd[1], (float)sd[2]};
Mike Kremer's avatar
Mike Kremer committed
604
  
605
606
607
608
609
610
611
612
613
614
615
  bool directional = _light.directional();
  
  Vec4f& p = _light.realPosition_;
  GLfloat realPos[4] = {(float)p[0], (float)p[1], (float)p[2], (directional ? 0.0f : 1.0f)};
  
  glLightfv(_index, GL_POSITION,  realPos);
  
  if(!directional) glLightfv(_index, GL_SPOT_DIRECTION,  dir);
  if(!directional) glLightf(_index, GL_SPOT_EXPONENT,  _light.spotExponent_);
  if(!directional) glLightf(_index, GL_SPOT_CUTOFF,  _light.spotCutoff_);
  
Jan Möbius's avatar
Jan Möbius committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  glLightf(_index, GL_CONSTANT_ATTENUATION,  _light.constantAttenuation_);
  glLightf(_index, GL_LINEAR_ATTENUATION,  _light.linearAttenuation_);
  glLightf(_index, GL_QUADRATIC_ATTENUATION,  _light.quadraticAttenuation_);
}

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

void LightNode::getParameters(GLenum _index, LightSource& _light)
{
  // get preferences of GL_LIGHT#_index and store them in _light
  glGetLightfv(_index, GL_AMBIENT,  (GLfloat *)_light.ambientColor_.data());
  glGetLightfv(_index, GL_DIFFUSE,  (GLfloat *)_light.diffuseColor_.data());
  glGetLightfv(_index, GL_SPECULAR,  (GLfloat *)_light.specularColor_.data());
  glGetLightfv(_index, GL_POSITION,  (GLfloat *)_light.position_.data());
  glGetLightfv(_index, GL_SPOT_DIRECTION,  (GLfloat *)_light.spotDirection_.data());

  glGetLightfv(_index, GL_SPOT_EXPONENT,  &_light.spotExponent_);
  glGetLightfv(_index, GL_SPOT_CUTOFF,  &_light.spotCutoff_);
  glGetLightfv(_index, GL_CONSTANT_ATTENUATION,  &_light.constantAttenuation_);
  glGetLightfv(_index, GL_LINEAR_ATTENUATION,  &_light.linearAttenuation_);
  glGetLightfv(_index, GL_QUADRATIC_ATTENUATION,  &_light.quadraticAttenuation_);
}
Jan Möbius's avatar
Jan Möbius committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672


void LightNode::getRenderObjects( IRenderer* _renderer, GLState& _state , const DrawModes::DrawMode& _drawMode , const Material* _mat )
{

  // fill IRenderer light struct with light data in view-space
  IRenderer::LightData light;

  if (transformedLight_.directional())
    light.ltype = ACG::SG_LIGHT_DIRECTIONAL;
  else if (transformedLight_.spotCutoff() > 179.5f)
    light.ltype = ACG::SG_LIGHT_POINT;
  else
    light.ltype = ACG::SG_LIGHT_SPOT;

#define V4toV3(v) (ACG::Vec3f(v[0], v[1], v[2]))

  light.diffuse = V4toV3(transformedLight_.diffuseColor());
  light.ambient = V4toV3(transformedLight_.ambientColor());
  light.specular = V4toV3(transformedLight_.specularColor());
  
  light.pos = V4toV3(transformedLight_.position());
  light.dir = V4toV3(transformedLight_.direction());

  light.atten[0] = transformedLight_.constantAttenuation();
  light.atten[1] = transformedLight_.linearAttenuation();
  light.atten[2] = transformedLight_.quadraticAttenuation();

  light.spotCutoffExponent[0] = transformedLight_.spotCutoff();
  light.spotCutoffExponent[1] = transformedLight_.spotExponent();

  _renderer->addLight(light);

}

Jan Möbius's avatar
Jan Möbius committed
673
674
675
676
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================