Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
24
26 TObject(),
27
28 fLightState(kLightMask), // All on
30
32{
33 // Constructor.
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Toggle light on/off.
38
40{
41 if (light == kLightSpecular) {
43 } else if (light >= kLightMask) {
44 Error("TGLLightSet::ToggleLight", "invalid light type");
45 return;
46 } else {
47 fLightState ^= light;
48 }
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Set a light on/off.
53
55{
56 if (light == kLightSpecular) {
57 fUseSpecular = on;
58 } else if (light >= kLightMask) {
59 Error("TGLViewer::ToggleLight", "invalid light type");
60 return;
61 }
62
63 if (on) {
64 fLightState |= light;
65 } else {
66 fLightState &= ~light;
67 }
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// Setup lights for current given bounding box and camera.
72/// This is called by standard GL viewer.
73/// Expects matrix-mode to be model-view.
74
76 const TGLCamera & camera, Bool_t debug)
77{
78 glPushMatrix();
79
80 if (!bbox.IsEmpty())
81 {
82 // Calculate a sphere radius to arrange lights round
83 Double_t lightRadius = bbox.Extents().Mag() * 2.9;
84 Double_t sideLightsZ, frontLightZ;
85
86 const TGLOrthoCamera* orthoCamera = dynamic_cast<const TGLOrthoCamera*>(&camera);
87 if (orthoCamera) {
88 // Find distance from near clip plane to furstum center - i.e. vector of half
89 // clip depth. Ortho lights placed this distance from eye point
90 sideLightsZ =
92 frontLightZ = sideLightsZ;
93 } else {
94 // Perspective camera
95 // Extract vector from camera eye point to center.
96 // Camera must have been applied already.
97 TGLVector3 eyeVector = camera.EyePoint() - camera.FrustumCenter();
98
99 // Pull forward slightly (0.85) to avoid to sharp a cutoff
100 sideLightsZ = eyeVector.Mag() * -0.85;
101 frontLightZ = 0.2 * lightRadius;
102 }
103
104 // Reset the modelview so static lights are placed in fixed eye space
105 // This will destroy camera application.
106 glLoadIdentity();
107
108 // 0: Front, 1: Top, 2: Bottom, 3: Left, 4: Right
109 TGLVertex3 c = bbox.Center();
110 TGLVector3 center(c.X(), c.Y(), c.Z());
111 camera.RefModelViewMatrix().MultiplyIP(center);
112 // Float_t pos0[] = { center.X(), center.Y(), frontLightZ, 1.0 };
113 Float_t pos0[] = { 0.0, 0.0, Float_t(frontLightZ), 1.0 };
114 Float_t pos1[] = { Float_t(center.X()), Float_t(center.Y() + lightRadius), Float_t(sideLightsZ), 1.0 };
115 Float_t pos2[] = { Float_t(center.X()), Float_t(center.Y() - lightRadius), Float_t(sideLightsZ), 1.0 };
116 Float_t pos3[] = { Float_t(center.X() - lightRadius), Float_t(center.Y()), Float_t(sideLightsZ), 1.0 };
117 Float_t pos4[] = { Float_t(center.X() + lightRadius), Float_t(center.Y()), Float_t(sideLightsZ), 1.0 };
118
119 Float_t specular = fUseSpecular ? fSpecularPower : 0.0f;
120 const Float_t frontLightColor[] = { fFrontPower, fFrontPower, fFrontPower, 1.0f };
121 const Float_t sideLightColor[] = { fSidePower, fSidePower, fSidePower, 1.0f };
122 const Float_t specLightColor[] = { specular, specular, specular, 1.0f };
123
124 glLightfv(GL_LIGHT0, GL_POSITION, pos0);
125 glLightfv(GL_LIGHT0, GL_DIFFUSE, frontLightColor);
126 glLightfv(GL_LIGHT0, GL_SPECULAR, specLightColor);
127
128 glLightfv(GL_LIGHT1, GL_POSITION, pos1);
129 glLightfv(GL_LIGHT1, GL_DIFFUSE, sideLightColor);
130 glLightfv(GL_LIGHT2, GL_POSITION, pos2);
131 glLightfv(GL_LIGHT2, GL_DIFFUSE, sideLightColor);
132 glLightfv(GL_LIGHT3, GL_POSITION, pos3);
133 glLightfv(GL_LIGHT3, GL_DIFFUSE, sideLightColor);
134 glLightfv(GL_LIGHT4, GL_POSITION, pos4);
135 glLightfv(GL_LIGHT4, GL_DIFFUSE, sideLightColor);
136 }
137
138 // Set light states every time - must be deferred until now when we know we
139 // are in the correct thread for GL context
140 // TODO: Could detect state change and only adjust if a change
141 for (UInt_t light = 0; (1<<light) < kLightMask; light++)
142 {
143 if ((1<<light) & fLightState)
144 {
145 glEnable(GLenum(GL_LIGHT0 + light));
146
147 // Debug mode - show active lights in yellow
148 if (debug)
149 {
150 // Lighting itself needs to be disable so a single one can show...!
151 glDisable(GL_LIGHTING);
152 Float_t position[4]; // Only float parameters for lights (no double)....
153 glGetLightfv(GLenum(GL_LIGHT0 + light), GL_POSITION, position);
154 Double_t size = bbox.Extents().Mag() / 10.0;
155 TGLVertex3 dPosition(position[0], position[1], position[2]);
157 glEnable(GL_LIGHTING);
158 }
159 }
160 else
161 {
162 glDisable(GLenum(GL_LIGHT0 + light));
163 }
164 }
165
166 // Restore camera which was applied before we were called, and is disturbed
167 // by static light positioning above.
168 glPopMatrix();
169}
#define c(i)
Definition RSha256.hxx:101
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
Concrete class describing an orientated (free) or axis aligned box of 8 vertices.
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...
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...
Bool_t fUseSpecular
!
Definition TGLLightSet.h:37
Float_t fFrontPower
! power of the front lamp
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.
void ToggleLight(ELight light)
Toggle light on/off.
Float_t fSidePower
! power of the side lamps
Definition TGLLightSet.h:40
Float_t fSpecularPower
! power of specular lamp
Definition TGLLightSet.h:41
void SetLight(ELight light, Bool_t on)
Set a light on/off.
UInt_t fLightState
! light states (on/off) mask
Definition TGLLightSet.h:36
void MultiplyIP(TGLVector3 &v, Double_t w=1) const
Multiply vector in-place.
Definition TGLUtil.cxx:1069
Orthographic projection camera.
Double_t DistanceTo(const TGLVertex3 &vertex) const
Distance from plane to vertex.
Definition TGLUtil.cxx:487
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:2348
static const UChar_t fgYellow[4]
Definition TGLUtil.h:1424
3 component (x/y/z) vector class.
Definition TGLUtil.h:248
Double_t Mag() const
Definition TGLUtil.h:298
3 component (x/y/z) vertex class.
Definition TGLUtil.h:84
Double_t X() const
Definition TGLUtil.h:119
Double_t Y() const
Definition TGLUtil.h:121
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
TObject()
TObject constructor.
Definition TObject.h:259