summaryrefslogtreecommitdiffstats
path: root/src/net/java/joglutils/lighting/Light.java
blob: 9597cde42eb919d6ea3bbc1cfad5549872565329 (plain)
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
/*
 * 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 ERIK
 * TOLLERUD 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 {
    
    static HashMap<GL,boolean[]> assignedLights = new HashMap<GL,boolean[]>();
    static HashMap<GL,Boolean> shaderBuiltin = new HashMap<GL,Boolean>();
    static HashMap<GL,Integer> shaderProgNums = new HashMap<GL,Integer>();
    
    private GL attachedGL;
    private int lightNumber;
    
    //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;
    
    private boolean phongShaded;
    private boolean phongDiffColorMat;
    private boolean phongAmbColorMat;
    
    
    /**
     * 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;
        phongShaded = false;
        phongDiffColorMat = false;
        phongAmbColorMat = false;
        
    }
    /**
     * 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 net.java.joglutils.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)  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 net.java.joglutils.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 net.java.joglutils.lighting.LightingException 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 net.java.joglutils.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 net.java.joglutils.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;
        if (this.phongShaded) {
            if(!this.shaderBuiltin.containsKey(gl))
                this.initializePhongShader(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.
     * Note that this will not apply Phong Shader settings.
     */
    public void apply() {
        apply(this.attachedGL,this.lightNumber);
        
    }
    /**
     * Reconfigure the settings for this Light from the state of the attached
     * {@link GL} Context. Note that this will not retrieve Phong Shader settings.
     */
    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. Note that this will not apply Phong Shader settings.
     * @param gl the OpenGL context to use.
     * @throws net.java.joglutils.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. Note that this will not retrieve Phong Shader settings.
     * @param gl the OpenGL context to use.
     * @throws net.java.joglutils.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 net.java.joglutils.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 net.java.joglutils.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. Note that this will not apply Phong Shader settings.
     * @param gl the OpenGL context to use.
     * @param lightNumber the number of the light to use (should be on [0,7]).
     * @throws net.java.joglutils.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. Note that this will not retrieve Phong Shader settings.
     * @param gl the OpenGL context to use.
     * @param lightNumber the number of the light to use (should be on [0,7]).
     * @throws net.java.joglutils.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 net.java.joglutils.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;
        
        if(this.phongShaded) {
            //TODO:link in shader as just another one?
            if(!shaderBuiltin.containsKey(gl))
                initializePhongShader(gl);
            
            if(shaderBuiltin.get(gl)) {
                int progID = shaderProgNums.get(gl);
                gl.glUseProgram(progID+lightNumber);
                 
                int diffMatCol = gl.glGetUniformLocation(progID,"diffMatCol");
                int ambMatCol = gl.glGetUniformLocation(progID,"ambMatCol");
                
                gl.glUniform1i(diffMatCol,phongDiffColorMat ? 1 : 0);
                gl.glUniform1i(ambMatCol,phongAmbColorMat ? 1 : 0);
                      
                
                /*for light one program
                 int progID = shaderProgNums.get(gl);
                 
                int lightNumUniform = gl.glGetUniformLocation(progID,"lightNumber");
                gl.glUniform1i(lightNumUniform,lightNumber);
                 
                gl.glUseProgram(progID);
                 */
            } else {
                int progID = shaderProgNums.get(gl);
                 
                int diffMatCol = gl.glGetUniformLocationARB(progID,"diffMatCol");
                int ambMatCol = gl.glGetUniformLocationARB(progID,"ambMatCol");
                
                gl.glUniform1iARB(diffMatCol,phongDiffColorMat ? 1 : 0);
                gl.glUniform1iARB(ambMatCol,phongAmbColorMat ? 1 : 0);
                
                gl.glUseProgramObjectARB(progID+lightNumber);
                /*for light one program
                int progID = shaderProgNums.get(gl);
                 
                int lightNumUniform = gl.glGetUniformLocationARB(progID,"lightNumber");
                gl.glUniform1iARB(lightNumUniform,lightNumber);
                 
                gl.glUseProgramObjectARB(progID);
                 */
            }
        } else {
            Integer result = shaderProgNums.get(gl);
            if (result != null) {
                int lightID = result+lightNumber;
                java.nio.IntBuffer buffer = java.nio.IntBuffer.allocate(1);
                gl.glGetIntegerv(GL.GL_CURRENT_PROGRAM,buffer);
                int currProgram = buffer.get();
                
                if (currProgram == lightID)
                    gl.glUseProgramObjectARB(0);
            }
        }
        
    }
    /**
     * 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 net.java.joglutils.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;
        
        if (this.phongShaded) {
            if(gl.isFunctionAvailable("glUseProgram"))
                gl.glUseProgram(0);
            else if(gl.isFunctionAvailable("glUseProgramObjectARB"))
                gl.glUseProgramObjectARB(0);
        }
    }
    
    //-----------------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
     * @return the appropriate light number (on [0,7])
     * @param lightID the OpenGL ID (should be one of GL_LIGHTn where n is on [0,7])
     * @throws net.java.joglutils.lighting.LightingException if the supplied lightID is not an OpenGL light ID
     */
    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
     * @return the OpenGL ID for that light number (from the GL_LIGHTn family)
     * @param lightNum a number on [0,7] specifying the a light
     * @throws net.java.joglutils.lighting.LightingException if the supplied lightID is not an OpenGL light ID
     */
    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;
    }
    
    /**
     * Sets if phong per-fragment shading (as opposed to fixed-functionality per-vertex shading) is to be used with this Light.  Must come before the light is enabled.
     * This uses a vertex and fragment shader, so the fixed-functionality pipeline will not function normally.
     * @param usePhongShading if Phong should be used. If true and this Light is attached to a GL Context, initializes on this Light's context
     * @throws net.java.joglutils.lighting.LightingException if the current OpenGL context does not support GLSL shaders or the shader did not sucessfully compile and link
     */
    public void setPhongShaded(boolean usePhongShading) throws LightingException {
        if (this.attachedGL != null)
            initializePhongShader(this.attachedGL);
        
        this.phongShaded = usePhongShading;
    }
    
    /**
     * Tells if this Light is using Phong shading
     * @return true if this Light is in Phong shading mode
     */
    public boolean isPhongShaded() {
        return this.phongShaded;
    }
    
    /**
     * Sets whether or not the opengl color should be used for the phong color shader
     * @param ambient the ambient color material mode
     * @param diffuse the diffuse color material mode
     */
    public void setPhongColorMaterial(boolean ambient, boolean diffuse) {
        this.phongAmbColorMat = ambient;
        this.phongDiffColorMat = diffuse;
    }
    
    /**
     * Determines if color material is used for ambient in the Phong shader
     * @return true if color material is used for the phong ambient shader
     */
    public boolean isAmbientPhongColorMaterial() {
        return this.phongAmbColorMat;
    }
    
    /**
     * Determines if color material is used for diffuse in the Phong shader
     * @return true if color material is used for the phong diffuse shader
     */
    public boolean isDiffusePhongColorMaterial() {
        return this.phongDiffColorMat;
    }
    
    /**
     * Initializes Phong shading mode for this Light (instead of the fixed functionality Gouraud shading) on the current OpenGL context. Must be called before Phong shading can be activated.
     * @throws net.java.joglutils.lighting.LightingException if the OpenGL context does not support GLSL shaders or the shader did not sucessfully compile and link
     */
    public static void initializePhongShader() throws LightingException {
        initializePhongShader(javax.media.opengl.glu.GLU.getCurrentGL());
    }
    
    /**
     * Initializes Phong shading mode for this Light (instead of the fixed functionality Gouraud shading). Must be called on the context before Phong shading can be activated.
     * @param gl the openGL context on which to initialize the shader
     * @throws net.java.joglutils.lighting.LightingException if the OpenGL context does not support GLSL shaders or the shader did not sucessfully compile and link
     */
    public static void initializePhongShader(GL gl) throws LightingException {
        /* Old way that only works for light zero
        //program source
        final String[] fragSource = {
            "varying vec3 normalEye;\n",
            "varying vec3 positionEye;\n",
            "uniform int lightNum;\n",
            "\n",
            "void main()\n",
            "{\n",
            "\n",
            "vec3 N = normalize (normalEye);\n",
            "vec3 L = normalize (gl_LightSource[0].position.xyz);\n",
            "vec3 E = normalize (-positionEye);\n",
            "vec3 H = normalize (L + E);\n",
            "\n",
            "vec4 ambientColor = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;\n",
            "vec4 diffuseColor = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * max (dot (N, L), 0.0);\n",
            "vec4 specularColor = gl_FrontMaterial.specular * gl_LightSource[0]. specular * pow (max (dot (N, H), 0.0), gl_FrontMaterial.shininess);\n",
            "gl_FragColor = ambientColor + diffuseColor + specularColor;\n",
            "}\n"
        };
        final String[] vertSource={
            "varying vec3 normalEye;\n",
            "varying vec3 positionEye;\n",
            "\n",
            "void main()\n",
            "{\n",
            "normalEye = gl_NormalMatrix * gl_Normal;\n",
            "positionEye = (gl_ModelViewMatrix * gl_Vertex).xyz;\n",
            "gl_Position = ftransform ();\n",
            "}\n"
        };
         
        int[] vertLengths = new int[vertSource.length];
        for (int i=0; i < vertLengths.length;++i)
            vertLengths[i] = vertSource[i].length();
        int[] fragLengths = new int[fragSource.length];
        for (int i=0; i < fragLengths.length;++i)
            fragLengths[i] = fragSource[i].length();
         
         
        boolean builtin;
        int progID;
         
        if(gl.isFunctionAvailable("glCreateProgram"))
            builtin = true;
        else if (gl.isFunctionAvailable("glCreateProgramObjectARB"))
            builtin = false;
        else
            throw new LightingException("Could not generate Shader for Light - GLSL Shaders not supported by this GL Context");
         
        if(builtin) {
            progID = gl.glCreateProgram();
            int vertID = gl.glCreateShader(GL.GL_VERTEX_SHADER);
            int fragID = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
         
            gl.glShaderSource(vertID,vertSource.length,vertSource,vertLengths,0);
            gl.glShaderSource(fragID,fragSource.length,fragSource,fragLengths,0);
         
            gl.glCompileShader(vertID);
            gl.glCompileShader(fragID);
         
            gl.glAttachShader(progID,vertID);
            gl.glAttachShader(progID,fragID);
         
            gl.glLinkProgram(progID);
         
            int[] getProgArray = new int[1];
            gl.glGetProgramiv(progID,GL.GL_LINK_STATUS,getProgArray,0);
            if (getProgArray[0] == GL.GL_FALSE) {
                gl.glGetProgramiv(progID,GL.GL_INFO_LOG_LENGTH,getProgArray,0);
                int logLength = getProgArray[0];
                byte[] logArray = new byte[logLength];
                gl.glGetProgramInfoLog(progID,logLength,getProgArray,0,logArray,0);
                String log = new String(logArray);
                throw new LightingException("Shader Program could not link. Log:\n"+log);
            }
         
        } else {
            progID = gl.glCreateProgramObjectARB();
            int vertID = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
            int fragID = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
         
            gl.glShaderSourceARB(vertID,vertSource.length,vertSource,vertLengths,0);
            gl.glShaderSourceARB(fragID,fragSource.length,fragSource,fragLengths,0);
         
            gl.glCompileShaderARB(vertID);
            gl.glCompileShaderARB(fragID);
         
            gl.glAttachObjectARB(progID,vertID);
            gl.glAttachObjectARB(progID,fragID);
         
            gl.glLinkProgramARB(progID);
         
            int[] getProgArray = new int[1];
            gl.glGetObjectParameterivARB(progID,GL.GL_OBJECT_LINK_STATUS_ARB,getProgArray,0);
            if (getProgArray[0] == GL.GL_FALSE) {
                gl.glGetObjectParameterivARB(progID,GL.GL_OBJECT_INFO_LOG_LENGTH_ARB,getProgArray,0);
                int logLength = getProgArray[0];
                byte[] logArray = new byte[logLength];
                gl.glGetInfoLogARB(progID,logLength,getProgArray,0,logArray,0);
                String log = new String(logArray);
                throw new LightingException("Shader Program could not link. Log:\n"+log);
            }
        }
         */
        
        boolean builtin;
        int progID;
        
        if(gl.isFunctionAvailable("glCreateProgram"))
            builtin = true;
        else if (gl.isFunctionAvailable("glCreateProgramObjectARB"))
            builtin = false;
        else
            throw new LightingException("Could not generate Shader for Light - GLSL Shaders not supported by this GL Context");
        
        if(builtin) {
            progID = gl.glCreateProgram();
            //generate the other 7 shaders so that the numbers are in order
            for (int currID = 1+progID; currID < (progID+8); ++currID) {
                int result = gl.glCreateProgram();
                if (result != currID)
                    throw new LightingException("Couldn't generate shader programs in numerical order - can't use Phong Shading");
            }
            
            int vertID = gl.glCreateShader(GL.GL_VERTEX_SHADER);
            String[] vertSource = generatePhongVertexShaderSource();
            gl.glShaderSource(vertID,vertSource.length,vertSource,generateShaderLengths(vertSource),0);
            
            
            //generate program for each light and compile and link
            for (int currID = progID;currID < (8+progID);++currID) {
                
                int fragID = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
                String[] fragSource = generatePhongFragmentShaderSource(currID-progID);
                gl.glShaderSource(fragID,fragSource.length,fragSource,generateShaderLengths(fragSource),0);
                
                gl.glCompileShader(vertID);
                gl.glCompileShader(fragID);
                
                gl.glAttachShader(currID,vertID);
                gl.glAttachShader(currID,fragID);
                
                gl.glLinkProgram(currID);
                
                int[] getProgArray = new int[1];
                gl.glGetProgramiv(currID,GL.GL_LINK_STATUS,getProgArray,0);
                if (getProgArray[0] == GL.GL_FALSE) {
                    gl.glGetProgramiv(currID,GL.GL_INFO_LOG_LENGTH,getProgArray,0);
                    int logLength = getProgArray[0];
                    byte[] logArray = new byte[logLength];
                    gl.glGetProgramInfoLog(currID,logLength,getProgArray,0,logArray,0);
                    String log = new String(logArray);
                    throw new LightingException("Shader Program could not link. Log:\n"+log);
                }
            }
        } else {
            progID = gl.glCreateProgramObjectARB();
            //generate the other 7 shaders so that the numbers are in order
            for (int currID = 1+progID; currID < (progID+8); ++currID) {
                int result = gl.glCreateProgramObjectARB();
                if (result != currID)
                    throw new LightingException("Couldn't generate shader programs in numerical order - can't use Phong Shading");
            }
            
            int vertID = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
            String[] vertSource = generatePhongVertexShaderSource();
            gl.glShaderSourceARB(vertID,vertSource.length,vertSource,generateShaderLengths(vertSource),0);
            
            //TODO: do this in a way that doesn't depend on the numbers being in order
            //generate program for each light and compile and link
            for (int currID = progID;currID < (8+progID);++currID) {
                
                int fragID = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
                String[] fragSource = generatePhongFragmentShaderSource(currID-progID);
                gl.glShaderSourceARB(fragID,fragSource.length,fragSource,generateShaderLengths(fragSource),0);
                
                gl.glCompileShaderARB(vertID);
                gl.glCompileShaderARB(fragID);
                
                gl.glAttachObjectARB(currID,vertID);
                gl.glAttachObjectARB(currID,fragID);
                
                gl.glLinkProgramARB(currID);
                
                int[] getProgArray = new int[1];
                gl.glGetObjectParameterivARB(currID,GL.GL_OBJECT_LINK_STATUS_ARB,getProgArray,0);
                if (getProgArray[0] == GL.GL_FALSE) {
                    gl.glGetObjectParameterivARB(currID,GL.GL_OBJECT_INFO_LOG_LENGTH_ARB,getProgArray,0);
                    int logLength = getProgArray[0];
                    byte[] logArray = new byte[logLength];
                    gl.glGetInfoLogARB(currID,logLength,getProgArray,0,logArray,0);
                    String log = new String(logArray);
                    throw new LightingException("Shader Program could not link. Log:\n"+log);
                }
            }
        }
        
        shaderProgNums.put(gl,progID);
        shaderBuiltin.put(gl, builtin);
    }
    
    /**
     * Removes the Phong Shader program from the current GL Context
     *
     * @throws net.java.joglutils.lighting.LightingException if there is no Phong Shader
     **/
    
    public static void removePhongShader() throws LightingException  {
        removePhongShader(javax.media.opengl.glu.GLU.getCurrentGL());
    }
    
    /**
     * Removes the Phong Shader program from the specified GL Context
     *
     * @param gl the OpenGL context to remove the Phong shader program from
     * @throws net.java.joglutils.lighting.LightingException if there is no Phong Shader initialized on this context
     */
    public static void removePhongShader(GL gl) throws LightingException {
        if (shaderBuiltin.containsKey(gl)) {
            int progID = shaderProgNums.remove(gl);
            if(shaderBuiltin.remove(gl)) {
                java.nio.IntBuffer intBuff = java.nio.IntBuffer.allocate(3);
                for (int currID = progID; currID < (8+progID); ++currID) {
                    gl.glGetAttachedShaders(currID,2,intBuff,intBuff);
                    if (intBuff.get() != 2)
                        throw new LightingException("More than 2 shaders attached to the Light phong shader program.  Program ID Number:"+progID);
                    
                    gl.glDeleteShader(intBuff.get());
                    gl.glDeleteShader(intBuff.get());
                    
                    gl.glDeleteProgram(currID);
                    intBuff.clear();
                }
            } else {
                java.nio.IntBuffer intBuff = java.nio.IntBuffer.allocate(3);
                for (int currID = progID; currID < (8+progID); ++currID) {
                    gl.glGetAttachedObjectsARB(currID,2,intBuff,intBuff);
                    if (intBuff.get() != 2)
                        throw new LightingException("More than 2 shaders attached to the Light phong shader program.  Program ID Number:"+progID);
                    
                    gl.glDeleteObjectARB(intBuff.get());
                    gl.glDeleteObjectARB(intBuff.get());
                    
                    gl.glDeleteObjectARB(currID);
                    intBuff.clear();
                }
            }
        } else
            throw new LightingException("Tried to remove Light phong shader on a GL Context with no phong shader");
    }
    
    //----------- 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);
    }
    
    private static String[] generatePhongFragmentShaderSource(int lightNumber) {
        String[] fragSource = {
            "varying vec3 normalEye;\n",
            "varying vec3 positionEye;\n",
            "uniform bool ambMatCol;\n",
            "uniform bool diffMatCol;\n",
            "\n",
            "void main()\n",
            "{\n",
            "\n",
            "vec3 N = normalize (normalEye);\n",
            "vec3 L = normalize (gl_LightSource["+lightNumber+"].position.xyz);\n",
            "vec3 E = normalize (-positionEye);\n",
            "vec3 H = normalize (L + E);\n",
            "\n",
            "vec4 ambientColor = (ambMatCol ? gl_Color : gl_FrontMaterial.ambient ) * (gl_LightSource["+lightNumber+"].ambient+gl_LightModel.ambient);\n",
            "vec4 diffuseColor = (diffMatCol ? gl_Color : gl_FrontMaterial.diffuse ) * gl_LightSource["+lightNumber+"].diffuse * max (dot (N, L), 0.0);\n",
            //"vec4 ambientColor = gl_FrontMaterial.ambient * (gl_LightSource["+lightNumber+"].ambient+gl_LightModel.ambient);\n",
            //"vec4 diffuseColor = gl_FrontMaterial.diffuse * gl_LightSource["+lightNumber+"].diffuse * max (dot (N, L), 0.0);\n",
            //"vec4 ambientColor = gl_Color * (gl_LightSource["+lightNumber+"].ambient+gl_LightModel.ambient);\n",
            //"vec4 diffuseColor = gl_Color * gl_LightSource["+lightNumber+"].diffuse * max (dot (N, L), 0.0);\n",
            "vec4 specularColor = gl_FrontMaterial.specular * gl_LightSource["+lightNumber+"]. specular * pow (max (dot (N, H), 0.0), gl_FrontMaterial.shininess);\n",
            "gl_FragColor = ambientColor + diffuseColor + specularColor;\n",
            "}\n"
        };
        return fragSource;
    }
    
    private static String[] generatePhongVertexShaderSource() {
        final String[] vertSource={
            "varying vec3 normalEye;\n",
            "varying vec3 positionEye;\n",
            "\n",
            "void main()\n",
            "{\n",
            "normalEye = gl_NormalMatrix * gl_Normal;\n",
            "positionEye = (gl_ModelViewMatrix * gl_Vertex).xyz;\n",
            "gl_Position = ftransform ();\n",
            "gl_FrontColor = gl_Color;\n",
            "}\n"
        };
        return vertSource;
    }
    
    private static int[] generateShaderLengths(String[] shaderSource) {
        int[] vertLengths = new int[shaderSource.length];
        for (int i=0; i < vertLengths.length;++i)
            vertLengths[i] = shaderSource[i].length();
        
        return vertLengths;
    }
    
}