Logo ROOT  
Reference Guide
TGLLightSet.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Matevz Tadel, Feb 2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TGLLightSet.h"
13
14#include "TGLBoundingBox.h"
15#include "TGLOrthoCamera.h"
16
17#include "TGLIncludes.h"
18
19/** \class TGLLightSet
20\ingroup opengl
21Encapsulates a set of lights for OpenGL.
22*/
23
25
27 TObject(),
28
29 fLightState(kLightMask), // All on
30 fUseSpecular(kTRUE),
31
32 fFrontPower(0.4), fSidePower(0.7), fSpecularPower(0.8)
33{
34 // Constructor.
35}
36
37////////////////////////////////////////////////////////////////////////////////
38/// Toggle light on/off.
39
41{
42 if (light == kLightSpecular) {
44 } else if (light >= kLightMask) {
45 Error("TGLLightSet::ToggleLight", "invalid light type");
46 return;
47 } else {
48 fLightState ^= light;
49 }
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Set a light on/off.
54
56{
57 if (light == kLightSpecular) {
58 fUseSpecular = on;
59 } else if (light >= kLightMask) {
60 Error("TGLViewer::ToggleLight", "invalid light type");
61 return;
62 }
63
64 if (on) {
65 fLightState |= light;
66 } else {
67 fLightState &= ~light;
68 }
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Setup lights for current given bounding box and camera.
73/// This is called by standard GL viewer.
74/// Expects matrix-mode to be model-view.
75
77 const TGLCamera & camera, Bool_t debug)
78{
79 glPushMatrix();
80
81 if (!bbox.IsEmpty())
82 {
83 // Calculate a sphere radius to arrange lights round
84 Double_t lightRadius = bbox.Extents().Mag() * 2.9;
85 Double_t sideLightsZ, frontLightZ;
86
87 const TGLOrthoCamera* orthoCamera = dynamic_cast<const TGLOrthoCamera*>(&camera);
88 if (orthoCamera) {
89 // Find distance from near clip plane to furstum center - i.e. vector of half
90 // clip depth. Ortho lights placed this distance from eye point
91 sideLightsZ =
93 frontLightZ = sideLightsZ;
94 } else {
95 // Perspective camera
96 // Extract vector from camera eye point to center.
97 // Camera must have been applied already.
98 TGLVector3 eyeVector = camera.EyePoint() - camera.FrustumCenter();
99
100 // Pull forward slightly (0.85) to avoid to sharp a cutoff
101 sideLightsZ = eyeVector.Mag() * -0.85;
102 frontLightZ = 0.2 * lightRadius;
103 }
104
105 // Reset the modelview so static lights are placed in fixed eye space
106 // This will destroy camera application.
107 glLoadIdentity();
108
109 // 0: Front, 1: Top, 2: Bottom, 3: Left, 4: Right
110 TGLVertex3 c = bbox.Center();
111 TGLVector3 center(c.X(), c.Y(), c.Z());
112 camera.RefModelViewMatrix().MultiplyIP(center);
113 // Float_t pos0[] = { center.X(), center.Y(), frontLightZ, 1.0 };
114 Float_t pos0[] = { 0.0, 0.0, Float_t(frontLightZ), 1.0 };
115 Float_t pos1[] = { Float_t(center.X()), Float_t(center.Y() + lightRadius), Float_t(sideLightsZ), 1.0 };
116 Float_t pos2[] = { Float_t(center.X()), Float_t(center.Y() - lightRadius), Float_t(sideLightsZ), 1.0 };
117 Float_t pos3[] = { Float_t(center.X() - lightRadius), Float_t(center.Y()), Float_t(sideLightsZ), 1.0 };
118 Float_t pos4[] = { Float_t(center.X() + lightRadius), Float_t(center.Y()), Float_t(sideLightsZ), 1.0 };
119
120 Float_t specular = fUseSpecular ? fSpecularPower : 0.0f;
121 const Float_t frontLightColor[] = { fFrontPower, fFrontPower, fFrontPower, 1.0f };
122 const Float_t sideLightColor[] = { fSidePower, fSidePower, fSidePower, 1.0f };
123 const Float_t specLightColor[] = { specular, specular, specular, 1.0f };
124
125 glLightfv(GL_LIGHT0, GL_POSITION, pos0);
126 glLightfv(GL_LIGHT0, GL_DIFFUSE, frontLightColor);
127 glLightfv(GL_LIGHT0, GL_SPECULAR, specLightColor);
128
129 glLightfv(GL_LIGHT1, GL_POSITION, pos1);
130 glLightfv(GL_LIGHT1, GL_DIFFUSE, sideLightColor);
131 glLightfv(GL_LIGHT2, GL_POSITION, pos2);
132 glLightfv(GL_LIGHT2, GL_DIFFUSE, sideLightColor);
133 glLightfv(GL_LIGHT3, GL_POSITION, pos3);
134 glLightfv(GL_LIGHT3, GL_DIFFUSE, sideLightColor);
135 glLightfv(GL_LIGHT4, GL_POSITION, pos4);
136 glLightfv(GL_LIGHT4, GL_DIFFUSE, sideLightColor);
137 }
138
139 // Set light states every time - must be deferred until now when we know we
140 // are in the correct thread for GL context
141 // TODO: Could detect state change and only adjust if a change
142 for (UInt_t light = 0; (1<<light) < kLightMask; light++)
143 {
144 if ((1<<light) & fLightState)
145 {
146 glEnable(GLenum(GL_LIGHT0 + light));
147
148 // Debug mode - show active lights in yellow
149 if (debug)
150 {
151 // Lighting itself needs to be disable so a single one can show...!
152 glDisable(GL_LIGHTING);
153 Float_t position[4]; // Only float parameters for lights (no double)....
154 glGetLightfv(GLenum(GL_LIGHT0 + light), GL_POSITION, position);
155 Double_t size = bbox.Extents().Mag() / 10.0;
156 TGLVertex3 dPosition(position[0], position[1], position[2]);
157 TGLUtil::DrawSphere(dPosition, size, TGLUtil::fgYellow);
158 glEnable(GL_LIGHTING);
159 }
160 }
161 else
162 {
163 glDisable(GLenum(GL_LIGHT0 + light));
164 }
165 }
166
167 // Restore camera which was applied before we were called, and is disturbed
168 // by static light positioning above.
169 glPopMatrix();
170}
unsigned int GLenum
Definition: GL_glu.h:266
#define c(i)
Definition: RSha256.hxx:101
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
Concrete class describing an orientated (free) or axis aligned box of 8 vertices.
TGLVector3 Extents() const
TGLVertex3 Center() const
Bool_t IsEmpty() const
Abstract base camera class - concrete classes for orthographic and perspective cameras derive from it...
Definition: TGLCamera.h:44
TGLVertex3 FrustumCenter() const
Find the center of the camera frustum from intersection of planes This method will work even with par...
Definition: TGLCamera.cxx:251
const TGLMatrix & RefModelViewMatrix() const
Definition: TGLCamera.h:121
const TGLPlane & FrustumPlane(EFrustumPlane plane) const
Definition: TGLCamera.h:219
TGLVertex3 EyePoint() const
Return the camera eye point (vertex) in world space Camera must have valid frustum cache - call Apply...
Definition: TGLCamera.cxx:219
Encapsulates a set of lights for OpenGL.
Definition: TGLLightSet.h:22
Bool_t fUseSpecular
light states (on/off) mask
Definition: TGLLightSet.h:37
Float_t fFrontPower
Definition: TGLLightSet.h:39
void StdSetupLights(const TGLBoundingBox &bbox, const TGLCamera &camera, Bool_t debug=kFALSE)
Setup lights for current given bounding box and camera.
Definition: TGLLightSet.cxx:76
void ToggleLight(ELight light)
Toggle light on/off.
Definition: TGLLightSet.cxx:40
Float_t fSidePower
power of the front lamp
Definition: TGLLightSet.h:40
TGLLightSet()
power of specular lamp
Definition: TGLLightSet.cxx:26
Float_t fSpecularPower
power of the side lamps
Definition: TGLLightSet.h:41
void SetLight(ELight light, Bool_t on)
Set a light on/off.
Definition: TGLLightSet.cxx:55
UInt_t fLightState
Definition: TGLLightSet.h:36
void MultiplyIP(TGLVector3 &v, Double_t w=1) const
Multiply vector in-place.
Definition: TGLUtil.cxx:1076
Orthographic projection camera.
Double_t DistanceTo(const TGLVertex3 &vertex) const
Distance from plane to vertex.
Definition: TGLUtil.cxx:493
static void DrawSphere(const TGLVertex3 &position, Double_t radius, const UChar_t rgba[4])
Draw sphere, centered on vertex 'position', with radius 'radius', color 'rgba'.
Definition: TGLUtil.cxx:2352
static const UChar_t fgYellow[4]
Definition: TGLUtil.h:1051
3 component (x/y/z) vector class.
Definition: TGLUtil.h:247
Double_t Mag() const
Definition: TGLUtil.h:297
3 component (x/y/z) vertex class.
Definition: TGLUtil.h:83
Double_t X() const
Definition: TGLUtil.h:118
Double_t Y() const
Definition: TGLUtil.h:120
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880