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
62
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
|
/*
* Copyright (c) 2006 Erik Tollerud (erik.tollerud@gmail.com) All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of Erik Tollerud, Sun Microsystems, Inc. or the names of
* contributors may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ERIK TOLLERUD,
* SUN MICROSYSTEMS, INC. ("SUN"), AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ERIK
* TOLLERUD, SUN, OR SUN'S LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF BEN
* CHAPPELL OR SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use
* in the design, construction, operation or maintenance of any nuclear
* facility.
*/
package net.java.joglutils.lighting;
import javax.media.opengl.*;
import java.awt.Color;
import java.util.*;
import java.nio.*;
/**
*
* This class encapsulates the settings for an OpenGL light. It has two modes:
* <br> 1. if an object implementing {@link GL} is attached, the OpenGL state is
* updated as changes to this Light are made, or on enable() or apply() calls.
* <br> 2.if no object is atttached, enable(GL) and apply(GL) applies settings, but the OpenGL state
* is not altered when this Light's settings are changed.
* <br> Note: GL_LIGHTING must be enabled in the OpenGL context by the user - this object will not do so.
* @author Erik J. Tollerud
*/
public class Light {
GL attachedGL;
int lightNumber;
static HashMap<GL,boolean[]> assignedLights = new HashMap<GL,boolean[]>();
//Light defaults are determined by constructor
private float[] ambient;
private float[] diffuse;
private float[] specular;
private float[] lightPosition;
private float lightW;
private float[] spotDirection;
private float spotCutoff;
private float spotExponent;
private float constantAttenuation;
private float linearAttenuation;
private float quadraticAttenuation;
/**
* Creates a new instance of Light with the default settings.
*/
public Light() {
attachedGL = null;
lightNumber = -1;
float[] localAmb = {0.0f,0.0f,0.0f,1.0f};
ambient = localAmb;
float[] localDiff = {1.0f,1.0f,1.0f,1.0f};
diffuse = localDiff;
float[] localSpec = {1.0f,1.0f,1.0f,1.0f};
specular = localSpec;
float[] localPos = {0,0,1};
lightPosition = localPos;
lightW = 0;
float[] localSpotDir = {0,0,-1};
spotDirection = localSpotDir;
spotCutoff= 180;
spotExponent = 0;
constantAttenuation = 1;
linearAttenuation = 0;
quadraticAttenuation = 0;
}
/**
* Creates a new instance of Light with default settings attached to the specified {@link GL} Context
*
* @param gl the OpenGL context to attach the light to
* @throws sddm.lighting.LightingException if all lights in the specified OpenGL context are already attached. Light is detached from any OpenGL Context.
*/
public Light(GL gl) throws LightingException {
this();
this.attachedGL = gl;
this.lightNumber = this.findAndAssignFreeLightNumber(gl);
if (this.lightNumber == -1) {
this.attachedGL = null;
throw new LightingException("No more Lights available in specified OpenGL context");
}
retrieve();
}
/**
* Creates a new instance of Light with default settings attached to the specified {@link GL} Context with an explicit light number
* @param gl the OpenGL context to attach the light to
* @param lightNumber the light number to be used (must be on [0,7])
* @throws sddm.lighting.LightingException if the light number is invalid or is already in use by another Light. Light is detached from any OpenGL Context.
*/
public Light(GL gl, int lightNumber) throws LightingException {
this();
if (lightNumber < 0 || lightNumber > maxNumberOfLightsInGL(gl))
throw new LightingException("Requested light not availible in specified OpenGL context");
this.attachedGL = gl;
this.lightNumber = this.findAndAssignFreeLightNumber(gl);
if (this.lightNumber == -1) {
this.attachedGL = null;
throw new LightingException("No more Lights available in specified OpenGL context");
}
retrieve();
}
protected void finalize() {
//Must free light number if this Light is destroyed
if (this.attachedGL != null)
this.unassignLightNumber(this.attachedGL,this.lightNumber);
}
/**
* Determines the OpenGL light identifier associated with this Light. If no GL context is attached to this
* object, the result is undefined (although generally reflects the last light used)
* @return the OpenGL identifier for this light
*/
public int getGLLightIdentifier() {
return this.numToID(this.lightNumber);
}
/**
* Determines the number of the light associated with this Light. If no GL context is attached to this
* object, the result is undefined (although generally reflects the last light used)
* @return a number on [0,7] representing which light is used
*/
public int getLightNumber() {
return this.lightNumber;
}
/**
* Sets the number of the OpenGL light to use in subsequent method calls
* @param lightNumber the light number to apply to this Light (must be on [0,7])
* @throws sddm.lighting.LightingException if the light number is invalid or is already in use by another Light object on the attached OpenGL Context. Light is detached from any OpenGL Context.
*/
public void setLightNumber(int lightNumber) throws LightingException {
if(lightNumber < 0 || lightNumber > 7)
throw new LightingException("Attempted to assign a light number that was not on [0,7]");
if (this.attachedGL != null) {
if(!isLightNumberFree(this.attachedGL,lightNumber))
throw new LightingException("Attempted to assign a light number that was not free on the attached OpenGL Context");
this.unassignLightNumber(this.attachedGL,this.lightNumber);
this.assignLightNumber(this.attachedGL,lightNumber);
}
this.lightNumber = lightNumber;
}
/**
* Sets the OpenGL light to use in subsequent method calls from a GL identifier
* @param lightID the OpenGL light ID (must be of the GL_LIGHTn family)
* @throws sddm.lighting.LightingException if the input is not a valid light or the light is in use by another Light on the attached OpenGL Context. Light is detached from any OpenGL Context.
*/
public void setLightID(int lightID) throws LightingException {
setLightNumber(this.idToNum(lightID));
}
/**
* Attached the specified OpenGL context to this object
*
* @param gl the OpenGL context to attach this to
* @throws sddm.lighting.LightingException if the specified context has no free lights. Light is detached from any OpenGL Context.
*/
public void setAttachedGL(GL gl) throws LightingException {
this.unassignLightNumber(this.attachedGL,this.lightNumber);
if(!this.hasFreeLights(gl)) {
this.attachedGL = null;
this.lightNumber = -1;
throw new LightingException("attempted to attach Light object to OpenGL Context with no free lights");
}
this.lightNumber = findAndAssignFreeLightNumber(gl);
this.attachedGL = gl;
}
/**
* Returns the OpenGL context attached to this Lighting object
* @return the attached OpenGL context, or null if there is no attached context
*/
public GL getAttachedGL() {
return this.attachedGL;
}
/**
* Detaches the currently attached OpenGL context from this object
*/
public void detachGL() {
this.unassignLightNumber(this.attachedGL,this.lightNumber);
this.attachedGL = null;
}
/**
* Determines if an OpenGL context is attached.
* @return true if an OpenGL context is attached to this object.
*/
public boolean isAttached() {
if (this.attachedGL == null)
return false;
return true;
}
/**
* Applies the settings on this Light to the attached {@link GL} Context.
*/
public void apply() {
apply(this.attachedGL,this.lightNumber);
}
/**
* Reconfigure the settings for this Light from the state of the
* attached {@link GL} Context.
*/
public void retrieve() {
retrieve(this.attachedGL,this.lightNumber);
}
/**
* Enables this Light on the attached {@link GL} Context.
*/
public void enable() {
enable(this.attachedGL,this.lightNumber);
}
/**
* Disables this Light on the attached {@link GL} Context.
*/
public void disable() {
disable(this.attachedGL,this.lightNumber);
}
/**
* Applies the settings on this Light to the specified OpenGL context,
* using the light number stored in the Light.
* @param gl the OpenGL context to use.
* @throws sddm.lighting.LightingException if the light number stored in this Light is invalid on the specified context.
*/
public void apply(GL gl) throws LightingException {
apply(gl,this.lightNumber);
}
/**
* Reconfigures the settings on this Light from the state of the specified
* OpenGL context, using the light number stored in the Light.
* @param gl the OpenGL context to use.
* @throws sddm.lighting.LightingException if the light number stored in this Light is invalid on the specified context.
*/
public void retrieve(GL gl) throws LightingException {
retrieve(gl,this.lightNumber);
}
/**
* Enables the light number stored in this Light on the specified OpenGL Context.
* @param gl the OpenGL context to use.
* @throws sddm.lighting.LightingException if the light number stored in this Light is invalid on the specified context.
*/
public void enable(GL gl) throws LightingException {
enable(gl,this.lightNumber);
}
/**
* Disables the light number stored in this Light on the specified OpenGL Context.
* @param gl the OpenGL context to use.
* @throws sddm.lighting.LightingException if the light number stored in this Light is invalid on the specified context.
*/
public void disable(GL gl) throws LightingException {
disable(gl,this.lightNumber);
}
/**
* Applies the settings on this Light to the specified OpenGL context,
* using the requested light number.
* @param gl the OpenGL context to use.
* @param lightNumber the number of the light to use (should be on [0,7]).
* @throws sddm.lighting.LightingException if the requested light is not valid on the specified context.
*/
public void apply(GL gl, int lightNumber) throws LightingException{
if(!this.isLightNumberValid(gl, lightNumber))
throw new LightingException("attempted to apply Light settings to invalid lightNumber for the requested OpenGL context");
int lightID = numToID(lightNumber);
//set color components
gl.glLightfv(lightID,GL.GL_AMBIENT,ambient,0);
gl.glLightfv(lightID,GL.GL_DIFFUSE,diffuse,0);
gl.glLightfv(lightID,GL.GL_SPECULAR,specular,0);
//OpenGL position information is contanied in Light lightPosition and light distance
float[] position = new float[4];
position[0] = lightPosition[0];
position[1] = lightPosition[1];
position[2] = lightPosition[2];
position[3] = lightW;
gl.glLightfv(lightID,GL.GL_POSITION,position,0);
//set other parameters
gl.glLightfv(lightID,GL.GL_SPOT_DIRECTION,spotDirection,0);
gl.glLightf(lightID,GL.GL_SPOT_CUTOFF,spotCutoff);
gl.glLightf(lightID,GL.GL_SPOT_EXPONENT,spotExponent);
gl.glLightf(lightID,GL.GL_CONSTANT_ATTENUATION,constantAttenuation);
gl.glLightf(lightID,GL.GL_LINEAR_ATTENUATION,linearAttenuation);
gl.glLightf(lightID,GL.GL_QUADRATIC_ATTENUATION,quadraticAttenuation);
this.lightNumber = lightNumber;
}
/**
* Reconfigures the settings on this Light from the state of the specified
* OpenGL context, using the requested light number.
* @param gl the OpenGL context to use.
* @param lightNumber the number of the light to use (should be on [0,7]).
* @throws sddm.lighting.LightingException if the requested light is not valid on the specified context.
*/
public void retrieve(GL gl, int lightNumber) throws LightingException{
if(!this.isLightNumberValid(gl, lightNumber))
throw new LightingException("attempted to retrieve Light settings to invalid lightNumber for the requested OpenGL context");
int lightID = numToID(lightNumber);
FloatBuffer buff = FloatBuffer.allocate(24);
//get color components - 4 each
gl.glGetLightfv(lightID,GL.GL_AMBIENT,buff);
gl.glGetLightfv(lightID,GL.GL_DIFFUSE,buff);
gl.glGetLightfv(lightID,GL.GL_SPECULAR,buff);
//get light position - 4 (including distance)
gl.glGetLightfv(lightID,GL.GL_POSITION,buff);
//get spot direction- 3
gl.glGetLightfv(lightID,GL.GL_SPOT_DIRECTION,buff);
//get individual floats - 1 each
gl.glGetLightfv(lightID,GL.GL_SPOT_CUTOFF,buff);
gl.glGetLightfv(lightID,GL.GL_SPOT_EXPONENT,buff);
gl.glGetLightfv(lightID,GL.GL_CONSTANT_ATTENUATION,buff);
gl.glGetLightfv(lightID,GL.GL_LINEAR_ATTENUATION,buff);
gl.glGetLightfv(lightID,GL.GL_QUADRATIC_ATTENUATION,buff);
//set state from the loaded buffer - start with the three colors
buff.get(this.ambient);
buff.get(this.diffuse);
buff.get(this.specular);
//grab first 3 for position, and 4th element from GL_POSITION for distance
buff.get(this.lightPosition);
this.lightW = buff.get();
// grab spot direction
buff.get(this.spotDirection);
//get individual floats
this.spotCutoff = buff.get();
this.spotExponent = buff.get();
this.constantAttenuation = buff.get();
this.linearAttenuation = buff.get();
this.quadraticAttenuation = buff.get();
this.lightNumber = lightNumber;
}
/**
* Enables the requested light on the specified OpenGL Context.
* @param gl the OpenGL context to use.
* @param lightNumber the number of the light to use (should be on [0,7]).
* @throws sddm.lighting.LightingException if the requested light is not valid on the specified context.
*/
public void enable(GL gl, int lightNumber) throws LightingException {
if(!this.isLightNumberValid(gl, lightNumber))
throw new LightingException("attempted to enable Light on with an invalid lightNumber for the requested OpenGL context");
gl.glEnable(numToID(lightNumber));
this.lightNumber = lightNumber;
}
/**
* Disables the requested light on the specified OpenGL Context.
* @param gl the OpenGL context to use.
* @param lightNumber the number of the light to use (should be on [0,7]).
* @throws sddm.lighting.LightingException if the requested light is not valid on the specified context.
*/
public void disable(GL gl, int lightNumber) throws LightingException {
if(!this.isLightNumberValid(gl, lightNumber))
throw new LightingException("attempted to disable Light on with an invalid lightNumber for the requested OpenGL context");
gl.glDisable(numToID(lightNumber));
this.lightNumber = lightNumber;
}
//-----------------Setters and Getters-----------------
//TODO: implement range checking on all of these
/**
* Sets the ambient (light "filling the room") color for this Light. Default is {0,0,0,1}.
* @param ambient the Color to be copied into this Light. Later changes to the Color object will not be reflected in the Light.
*/
public void setAmbient(Color ambient) {
this.ambient = ambient.getRGBComponents(null);
if (this.attachedGL != null)
this.attachedGL.glLightfv(numToID(lightNumber),GL.GL_AMBIENT,this.ambient,0);
}
/**
* Retrieves the ambient color for this Light.
* @return a copy of the color used in this Light.
*/
public Color getAmbient() {
return new Color(ambient[0],ambient[1],ambient[2],ambient[3]);
}
/**
* Sets the diffuse ("dull reflection") color for this Light. Default is {1,1,1,1}.
* @param diffuse the Color to be copied into this Light. Later changes to the Color object will not be reflected in the Light.
*/
public void setDiffuse(Color diffuse) {
this.diffuse = diffuse.getRGBComponents(null);
if (this.attachedGL != null)
this.attachedGL.glLightfv(numToID(lightNumber),GL.GL_DIFFUSE,this.diffuse,0);
}
/**
* Retrieves the diffuse color for this Light.
* @return a copy of the color used in this Light.
*/
public Color getDiffuse() {
return new Color(diffuse[0],diffuse[1],diffuse[2],diffuse[3]);
}
/**
* Sets the specular ("shiny reflection") color for this Light. Default is {1,1,1,1}.
* @param specular the Color to be copied into this Light. Later changes to the Color object will not be reflected in the Light.
*/
public void setSpecular(Color specular) {
this.specular = specular.getRGBComponents(null);
if (this.attachedGL != null)
this.attachedGL.glLightfv(numToID(lightNumber),GL.GL_SPECULAR,this.specular,0);
}
/**
* Retrieves the specular color for this Light.
* @return a copy of the color used in this Light.
*/
public Color getSpecular() {
return new Color(specular[0],specular[1],specular[2],specular[3]);
}
/**
* Specifies the direction vector for this Light.
* (i.e. this the first 3 components specified when setting GL_POSITION with glLight)
* Default is {0,0,1}.
* @param lightPosition an array of three components in {x,y,z} format to be copied into the Light.
*/
public void setLightPosition(float[] lightPosition) {
if (this.attachedGL != null) {
float[] position = new float[4];
position[0] = lightPosition[0];
position[1] = lightPosition[1];
position[2] = lightPosition[2];
position[3] = this.lightW;
this.attachedGL.glLightfv(numToID(lightNumber),GL.GL_POSITION,position,0);
}
this.lightPosition = lightPosition.clone();
}
/**
* Specifies the direction vector for this Light.
* (i.e. this the first 3 components specified when setting GL_POSITION with glLight)
* Default is {0,0,1}.
* @param lightx the x component of the light position
* @param lighty the y component of the light position
* @param lightz the z component of the light position
*/
public void setLightPosition(float lightx, float lighty, float lightz) {
float[] pos = {lightx,lighty,lightz};
setLightPosition(pos);
}
/**
* Retrieves the direction vector for this light.
* @return a copy of the position of this Light
*/
public float[] getLightPosition() {
return this.lightPosition.clone();
}
/**
* Sets the spotlight direction for this Light. Default is {0,0,-1}.
* @param spotDirection A 3-element array in {x,y,z} format specifying the spotlight direction vector.
*/
public void setSpotDirection(float[] spotDirection) {
if (this.attachedGL != null)
this.attachedGL.glLightfv(numToID(lightNumber),GL.GL_SPOT_DIRECTION,spotDirection,0);
this.spotDirection = spotDirection.clone();
}
/**
* Sets the spotlight direction for this Light. Default is {0,0,-1}.
* @param spotx the x coordinate of the spotlight direction vector.
* @param spoty the y coordinate of the spotlight direction vector.
* @param spotz the z coordinate of the spotlight direction vector.
*/
public void setSpotDirection(float spotx, float spoty, float spotz) {
float[] spotVec = {spotx, spoty, spotz};
setSpotDirection(spotVec);
}
/**
* Retrieves a vector indicating the spotlight direction used in this Light.
* @return a copy of the spotlight direction of this Light
*/
public float[] getSpotDirection() {
return this.spotDirection.clone();
}
/**
* Specifies the w-component to be used for this Light. Default is 0.
* (i.e. this the last component specified when setting GL_POSITION with glLight)
* If 0, this light is treated as a direction source.
* @param lightW the distance to be applied.
*/
public void setLightW(float lightW) {
if (this.attachedGL != null) {
float[] position = new float[4];
position[0] = this.lightPosition[0];
position[1] = this.lightPosition[1];
position[2] = this.lightPosition[2];
position[3] = lightW;
this.attachedGL.glLightfv(numToID(lightNumber),GL.GL_POSITION,position,0);
}
this.lightW = lightW;
}
/**
* Retrieves the w-component of this Light.
* @return the component. If 0, this Light is a directional source.
*/
public float getLightW() {
return lightW;
}
/**
* Sets this Light so that the light seems to be infinitely far away in terms of some of the calculations.
*/
public void makeDirectional() {
this.setLightW(0.0f);
}
/**
* Sets the spotlight cutoff angle for this Light. Default is 180.
* @param spotCutoff the angle to use.
*/
public void setSpotCutoff(float spotCutoff) {
if (this.attachedGL != null)
this.attachedGL.glLightf(numToID(lightNumber),GL.GL_SPOT_CUTOFF,spotCutoff);
this.spotCutoff = spotCutoff;
}
/**
* Retrieves the spotlight cutoff angle for this Light.
* @return the angle used in this Light.
*/
public float getSpotCutoff() {
return spotCutoff;
}
/**
* Sets the spotlight exponent in this Light. Default is 0.
* @param spotExponent the exponent to set on this Light.
*/
public void setSpotExponent(float spotExponent) {
if (this.attachedGL != null)
this.attachedGL.glLightf(numToID(lightNumber),GL.GL_SPOT_EXPONENT,spotExponent);
this.spotExponent = spotExponent;
}
/**
* Retrieves the spotlight exponent in this Light.
* @return the exponent used in this Light.
*/
public float getSpotExponent() {
return spotExponent;
}
/**
* Sets the coefficient of the constant term in the attenuation equation for this Light.
* Default is 1.
* @param constantAttenuation the value to use for the constant coefficient.
*/
public void setConstantAttenuation(float constantAttenuation) {
if (this.attachedGL != null)
this.attachedGL.glLightf(numToID(lightNumber),GL.GL_CONSTANT_ATTENUATION,constantAttenuation);
this.constantAttenuation = constantAttenuation;
}
/**
* Retrieves the coefficient of the constant term in the attenuation equation for this Light.
* @return the constant coefficient.
*/
public float getConstantAttenuation() {
return constantAttenuation;
}
/**
* Sets the coefficient of the linear term in the attenuation equation for this Light.
* Default is 0.
* @param linearAttenuation the value to use for the linear coefficient.
*/
public void setLinearAttenuation(float linearAttenuation) {
if (this.attachedGL != null)
this.attachedGL.glLightf(numToID(lightNumber),GL.GL_LINEAR_ATTENUATION,linearAttenuation);
this.linearAttenuation = linearAttenuation;
}
/**
* Retrieves the coefficient of the linear term in the attenuation equation for this Light.
* @return the linear coefficient.
*/
public float getLinearAttenuation() {
return linearAttenuation;
}
/**
* Sets the coefficient of the quadratic term in the attenuation equation for this Light.
* Default is 0.
* @param quadraticAttenuation the value to use for the quadratic coefficient.
*/
public void setQuadraticAttenuation(float quadraticAttenuation) {
if (this.attachedGL != null)
this.attachedGL.glLightf(numToID(lightNumber),GL.GL_QUADRATIC_ATTENUATION,quadraticAttenuation);
this.quadraticAttenuation = quadraticAttenuation;
}
/**
* Retrieves the coefficient of the quadratic term in the attenuation equation for this Light.
* @return the quadratic coefficient.
*/
public float getQuadraticAttenuation() {
return quadraticAttenuation;
}
//------------------public statics---------------
/**
* Determines the maximum number of lights in the specified {@link GL} context
* @param gl the OpenGL context to test
* @return the maximum number of lights (highest possible in OpenGL is 8)
*/
public static int maxNumberOfLightsInGL(GL gl) {
java.nio.IntBuffer buff = java.nio.IntBuffer.allocate(1);
gl.glGetIntegerv(gl.GL_MAX_LIGHTS,buff);
return buff.get();
}
/**
* Converts the specified OpenGL light ID number to a number
* @param lightID the OpenGL ID (should be one of GL_LIGHTn where n is on [0,7])
* @throws sddm.lighting.LightingException if the supplied lightID is not an OpenGL light ID
* @return the appropriate light number (on [0,7])
*/
public static int idToNum(int lightID) throws LightingException {
int retNum = -1;
switch (lightID) {
case GL.GL_LIGHT0:
retNum = 0;
break;
case GL.GL_LIGHT1:
retNum = 1;
break;
case GL.GL_LIGHT2:
retNum = 2;
break;
case GL.GL_LIGHT3:
retNum = 3;
break;
case GL.GL_LIGHT4:
retNum = 4;
break;
case GL.GL_LIGHT5:
retNum = 5;
break;
case GL.GL_LIGHT6:
retNum = 6;
break;
case GL.GL_LIGHT7:
retNum = 7;
break;
default:
throw new LightingException("tried to determine light number of a non-ID int");
}
return retNum;
}
/**
* Determines the OpenGL ID for the specified light number
* @param lightNum a number on [0,7] specifying the a light
* @throws sddm.lighting.LightingException if the input is invalid
* @return the OpenGL ID for that light number (from the GL_LIGHTn family)
*/
public static int numToID(int lightNum) throws LightingException {
int retID = -1;
switch (lightNum) {
case 0:
retID = GL.GL_LIGHT0;
break;
case 1:
retID = GL.GL_LIGHT1;
break;
case 2:
retID = GL.GL_LIGHT2;
break;
case 3:
retID = GL.GL_LIGHT3;
break;
case 4:
retID = GL.GL_LIGHT4;
break;
case 5:
retID = GL.GL_LIGHT5;
break;
case 6:
retID = GL.GL_LIGHT6;
break;
case 7:
retID = GL.GL_LIGHT7;
break;
default:
throw new LightingException("tried to determine ID of a number not on [0,7]");
}
return retID;
}
/**
* Determines if there is space in a specified OpenGL context for another Light to be attached
* @param gl the openGL context to test
* @return true if another Light object can be attached to this GL context
*/
public static boolean hasFreeLights(GL gl) {
boolean[] lights = assignedLights.get(gl);
if (lights == null)
return true;
for (boolean b : lights) {
if (!b)
return true;
}
return false;
}
//----------- Private internal functions/methods below this point------------
private static boolean isLightNumberValid(GL gl ,int lightNumber) {
return (lightNumber > -1 && lightNumber < maxNumberOfLightsInGL(gl));
}
private static boolean isLightNumberFree(GL gl, int lightNumber) {
boolean[] lights = assignedLights.get(gl);
if (lights == null)
return true;
if (lightNumber >= lights.length || lightNumber < 0)
return false;
return (!lights[lightNumber]);
}
private static int findAndAssignFreeLightNumber(GL gl) {
boolean[] lights = assignedLights.get(gl);
if (lights == null) {
lights = new boolean[maxNumberOfLightsInGL(gl)];
lights[0] = true;
assignedLights.put(gl,lights);
return 0;
}
int i = 0;
while(i < lights.length) {
if(!lights[i])
break;
++i;
}
if (i < lights.length) {
lights[i] = true;
assignedLights.put(gl,lights);
return i;
}
return -1;
}
private static void assignLightNumber(GL gl, int lightNumber) {
//No range checking
boolean[] lights = assignedLights.get(gl);
if (lights == null) {
lights = new boolean[maxNumberOfLightsInGL(gl)];
lights[lightNumber] = true;
} else
lights[lightNumber] = true;
assignedLights.put(gl,lights);
}
private static void unassignLightNumber(GL gl, int lightNumber) {
//No range checking
boolean[] lights = assignedLights.get(gl);
lights[lightNumber] = false;
assignedLights.put(gl,lights);
}
}
|