aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/ogl/NativeConfigTemplate3D.c
blob: ae2340f7c2bbb45e2221d70d4f158027478396fc (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
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
/*
 * $RCSfile$
 *
 * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

/*
 * Portions of this code were derived from work done by the Blackdown
 * group (www.blackdown.org), who did the initial Linux implementation
 * of the Java 3D API.
 */

#include <jni.h>
#include <math.h>
#include <stdlib.h>

#include "gldefs.h"

#if defined(SOLARIS) || defined(__linux__)
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif

#ifdef WIN32
#include <windows.h>
#endif

/* check if the extension is supported  */
extern int isExtensionSupported(const char *allExtensions, const char *extension); 

#if defined(SOLARIS) || defined(__linux__)

XVisualInfo *findVisualInfoSwitchDoubleBufferAndStereo(jlong display,
						       jint screen,
						       int* glxAttrs,
						       int sVal, int sIndex,
						       int dbVal, int dbIndex ) {
    int stereoLoop;
    int doubleBufferLoop;
    XVisualInfo *vis_info = NULL;

    int i, j;
    /*
     * set all "enums" to user's preferred state
     */
    if (dbVal == REQUIRED || dbVal == PREFERRED)
	glxAttrs[dbIndex] = GLX_DOUBLEBUFFER;
    else
        glxAttrs[dbIndex] = GLX_USE_GL;

    if (sVal == REQUIRED || sVal == PREFERRED)
	glxAttrs[sIndex] = GLX_STEREO;
    else
        glxAttrs[sIndex] = GLX_USE_GL;

    vis_info = glXChooseVisual((Display*)display, screen, glxAttrs);
    if (vis_info == NULL) {
        /*
         * coudn't match visual with default values - try
         * enabling UNNECESSARY attributes.
         */
	if(sVal == UNNECESSARY)
	    stereoLoop = 1;
	else
	    stereoLoop = 0;

	if(dbVal == UNNECESSARY)
	    doubleBufferLoop = 1;
	else
	    doubleBufferLoop = 0;
	
	i = 0; 
	while(i <= stereoLoop && vis_info == NULL ) {
	    if (sVal == UNNECESSARY)
		glxAttrs[sIndex] = i? GLX_STEREO : GLX_USE_GL;
	    j = 0; 
	    while(j <= doubleBufferLoop && vis_info == NULL) {
		if(dbVal == UNNECESSARY) {
		    glxAttrs[dbIndex] = j? GLX_USE_GL: GLX_DOUBLEBUFFER;
		}
		vis_info = glXChooseVisual((Display*)display, screen, glxAttrs);
		j++; 
	    } /* end of doubleBufferLoop */
	    i++; 
	} /* end of stereoLoop */    
    }

    if (vis_info == NULL) {
        /*
         * still coudn't match visual with default values - try 
         * disabling PREFERRED attributes.
         */
        /* restore default values */
        if (sVal == REQUIRED || sVal == PREFERRED)
            glxAttrs[sIndex] = GLX_STEREO;
        else
            glxAttrs[sIndex] = GLX_USE_GL;

        if (dbVal == REQUIRED || dbVal == PREFERRED)
            glxAttrs[dbIndex] = GLX_DOUBLEBUFFER;
        else
            glxAttrs[dbIndex] = GLX_USE_GL;

	if(sVal == PREFERRED)
	    stereoLoop = 1;
	else
	    stereoLoop = 0;
	
	if(dbVal == PREFERRED)
	    doubleBufferLoop = 1;
	else
	    doubleBufferLoop = 0;
	
	i = 0; 
	while(i <= stereoLoop && vis_info == NULL ) {
	    if (sVal == PREFERRED)
		glxAttrs[sIndex] = i?  GLX_USE_GL : GLX_STEREO ;
	    j = 0; 
	    while(j <= doubleBufferLoop && vis_info == NULL) {
		if(dbVal == PREFERRED) {
		    glxAttrs[dbIndex] = j? GLX_DOUBLEBUFFER : GLX_USE_GL;
		}
		vis_info = glXChooseVisual((Display*)display, screen, glxAttrs);
		j++; 
	    } /* end of doubleBufferLoop */
	    i++; 
	} /* end of stereoLoop */
    }

    if (vis_info == NULL) {

        /*
         * STILL coudn't match visual with default values - try 
         * disabling PREFERRED attributes and enabling UNNECESSARY.
         */
	
        /* restore default values */
        if (sVal == REQUIRED || sVal == PREFERRED)
            glxAttrs[sIndex] = GLX_STEREO;
        else
            glxAttrs[sIndex] = GLX_USE_GL;

        if (dbVal == REQUIRED || dbVal == PREFERRED)
            glxAttrs[dbIndex] = GLX_DOUBLEBUFFER;
        else
            glxAttrs[dbIndex] = GLX_USE_GL;

	if(sVal != REQUIRED)
	    stereoLoop = 1;
	else
	    stereoLoop = 0;
	
	if(dbVal != REQUIRED)
	    doubleBufferLoop = 1;
	else
	    doubleBufferLoop = 0;

	i = 0; 
	while(i <= stereoLoop && vis_info == NULL ) {
	    if (sVal == PREFERRED || sVal == UNNECESSARY)
		glxAttrs[sIndex] = i? GLX_USE_GL : GLX_STEREO ;
	    j = 0; 
	    while(j <= doubleBufferLoop && vis_info == NULL) {
		if(dbVal == PREFERRED || dbVal == UNNECESSARY) {
		    glxAttrs[dbIndex] = j? GLX_DOUBLEBUFFER : GLX_USE_GL;
		}
		vis_info = glXChooseVisual((Display*)display, screen, glxAttrs);
		j++; 
	    } /* end of doubleBufferLoop */
	    i++; 
	} /* end of stereoLoop */
    }
    return vis_info;
}

/*
 * Uses the past in array to choose the best OpenGL visual.
 * When the "best" visual cannot be used, the "enums" (three
 * state attributes) are looped through setting/resetting in all
 * combinations in hopes of finding an valid visual.
 */
JNIEXPORT
jint JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_chooseOglVisual(
    JNIEnv    *env,
    jobject    obj,
    jlong      display,
    jint       screen,
    jintArray  attrList,
    jlongArray vInfArray)
{
    VisualID vis_id = 0;
    jint *mx_ptr;
    int   glxAttrs[256];  /* value, attr pair plus a None */
    int   index;
    XVisualInfo *vis_info = NULL;

    /* use to cycle through when attr is not REQUIRED */
    int sVal;
    int sIndex;

    int dbVal;
    int dbIndex;
    
    int antialiasVal;
    int antialiasIndex;

    const char *glxExtensions = NULL;
    jlong *visInfo = (*env)->GetLongArrayElements(env, vInfArray, NULL);
    
    mx_ptr = (jint *)(*env)->GetPrimitiveArrayCritical(env, attrList, NULL);

    /*
     * convert Java 3D values to GLX
     */
    index = 0;
    glxAttrs[index++] = GLX_RGBA;  /* only interested in RGB visuals */
    glxAttrs[index++] = GLX_RED_SIZE;
    glxAttrs[index++] = mx_ptr[RED_SIZE];
    glxAttrs[index++] = GLX_GREEN_SIZE;
    glxAttrs[index++] = mx_ptr[GREEN_SIZE];
    glxAttrs[index++] = GLX_BLUE_SIZE;
    glxAttrs[index++] = mx_ptr[BLUE_SIZE];
    glxAttrs[index++] = GLX_DEPTH_SIZE;
    glxAttrs[index++] = mx_ptr[DEPTH_SIZE];

    
    dbIndex = index++;
    dbVal = mx_ptr[DOUBLEBUFFER];

    sIndex = index++;
    sVal = mx_ptr[STEREO];

    antialiasIndex = index++;
    antialiasVal = mx_ptr[ANTIALIASING]; 

    /* glxAttrs[index] = None; */

    (*env)->ReleasePrimitiveArrayCritical(env, attrList, mx_ptr, 0);

    
    if(antialiasVal == REQUIRED || antialiasVal== PREFERRED) {
	/* try GLX_ARB_multisample */
	glxExtensions = (const char *)glXGetClientString((Display*)display, GLX_EXTENSIONS);

	
	if(isExtensionSupported(glxExtensions, "GLX_ARB_multisample")){
	    /* path 1: */
	    /* Query the visual with mulitsamples */
	    
	    index = antialiasIndex;
	    glxAttrs[index++] = GLX_SAMPLE_BUFFERS_ARB;
	    glxAttrs[index++] = 1;
	    glxAttrs[index++] = GLX_SAMPLES_ARB;
	    glxAttrs[index++] = 1;
	    glxAttrs[index++] = None;
	    vis_info = findVisualInfoSwitchDoubleBufferAndStereo(display, screen, glxAttrs, sVal, sIndex,
								 dbVal, dbIndex);
	    
	    if(vis_info != NULL) {
		vis_id = XVisualIDFromVisual(vis_info->visual);
		visInfo[0] = (jlong)vis_info;
		(*env)->ReleaseLongArrayElements(env, vInfArray, visInfo, 0);
		return vis_id;
	    }
	}
    }

    /* normal path */
    if ( antialiasVal == REQUIRED || antialiasVal == PREFERRED) {
	/* step 1 : enable antialiasing */
	index = antialiasIndex;
        glxAttrs[index++] = GLX_ACCUM_RED_SIZE;
        glxAttrs[index++] = 8;
        glxAttrs[index++] = GLX_ACCUM_GREEN_SIZE;
        glxAttrs[index++] = 8;
        glxAttrs[index++] = GLX_ACCUM_BLUE_SIZE;
        glxAttrs[index++] = 8;
	glxAttrs[index++] = None;
	vis_info = findVisualInfoSwitchDoubleBufferAndStereo(display, screen, glxAttrs, sVal, sIndex,
							     dbVal, dbIndex);
	
	if( vis_info == NULL) {
	    /* try disable antialiasing if it is PREFERRED */
	    if(antialiasVal == PREFERRED) {
		glxAttrs[antialiasIndex] = None;
		vis_info = findVisualInfoSwitchDoubleBufferAndStereo(display, screen, glxAttrs, sVal, sIndex,
								     dbVal, dbIndex);
	    }
	}

	visInfo[0] = (jlong)vis_info;
	(*env)->ReleaseLongArrayElements(env, vInfArray, visInfo, 0);

	if( vis_info != NULL) {
	    vis_id = XVisualIDFromVisual(vis_info->visual);
	    return vis_id;
	} else {
	    return 0; 
	}
    }
    
    
    glxAttrs[antialiasIndex] = None;
    vis_info = findVisualInfoSwitchDoubleBufferAndStereo(display, screen, glxAttrs, sVal, sIndex,
							 dbVal, dbIndex);
     
    visInfo[0] = (jlong)vis_info;
    (*env)->ReleaseLongArrayElements(env, vInfArray, visInfo, 0);

    if( vis_info != NULL) {
	vis_id = XVisualIDFromVisual(vis_info->visual);
	return vis_id;
    } else {
	return 0; 
    }
}


JNIEXPORT 
void JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_freeVisual(
     JNIEnv *env,
     jclass  class,	/* this is a static native method */
     jlong   visInfo)
{
    XFree((XVisualInfo *)visInfo);
}


JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isStereoAvailable(
    JNIEnv *env,
    jobject obj,
    jlong display,
    jint screen,
    jint vid)
{
    Display *dpy = (Display*) display;
    XVisualInfo *vinfo, template;
    int nitems;
    int stereoFlag;
    static GLboolean first_time = GL_TRUE;
    static GLboolean force_no_stereo = GL_FALSE;
 
    if (first_time) {
	if (getenv("J3D_NO_STEREO") != NULL) {
	    fprintf(stderr, "Java 3D: stereo mode disabled\n");
	    force_no_stereo = GL_TRUE;
	}
	first_time = GL_FALSE;
    }

    if (force_no_stereo)
	return JNI_FALSE;

    template.visualid = vid;
    vinfo = XGetVisualInfo(dpy, VisualIDMask, &template, &nitems);
    if (nitems != 1) {
	fprintf(stderr, "Warning Canvas3D_isStereoAvailable got unexpected number of matching visuals %d\n", nitems);
    }

    glXGetConfig(dpy, vinfo, GLX_STEREO, &stereoFlag);

    return (stereoFlag ? JNI_TRUE : JNI_FALSE);
}

JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isDoubleBufferAvailable(
    JNIEnv *env,
    jobject obj,
    jlong display,
    jint screen,
    jint vid)
{
    Display *dpy = (Display*) display;
    XVisualInfo *vinfo, template;
    int nitems;
    int doubleBufferFlag;

    template.visualid = vid;
    vinfo = XGetVisualInfo(dpy, VisualIDMask, &template, &nitems);
    if (nitems != 1) {
	fprintf(stderr, "Warning Canvas3D_isDoubleBufferAvailable got unexpected number of matching visuals %d\n", nitems);
    }

    glXGetConfig(dpy, vinfo, GLX_DOUBLEBUFFER, &doubleBufferFlag);

    return (doubleBufferFlag ? JNI_TRUE : JNI_FALSE);
}

JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isSceneAntialiasingAccumAvailable(
    JNIEnv *env,
    jobject obj,
    jlong display,
    jint screen,
    jint vid)
{
    Display *dpy = (Display*) display;
    XVisualInfo *vinfo, template;
    int nitems;
    int numAccumRedBits;
	
    template.visualid = vid;
    vinfo = XGetVisualInfo(dpy, VisualIDMask, &template, &nitems);
    if (nitems != 1) {
	fprintf(stderr, "Warning Canvas3D_isSceneAntialiasingAvailable got unexpected number of matching visuals %d\n", nitems);
    }

    glXGetConfig(dpy, vinfo, GLX_ACCUM_RED_SIZE, &numAccumRedBits);

    return (numAccumRedBits > 0 ? JNI_TRUE : JNI_FALSE);
}

JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isSceneAntialiasingMultiSamplesAvailable(
    JNIEnv *env,
    jobject obj,
    jlong display,
    jint screen,
    jint vid)
{
    Display *dpy = (Display*) display;
    XVisualInfo *vinfo, template;
    int nitems;
    
    const char *glxExtensions;
    int numSampleBuffers;
    int numSamples;
	
    template.visualid = vid;
    vinfo = XGetVisualInfo(dpy, VisualIDMask, &template, &nitems);
    if (nitems != 1) {
	fprintf(stderr, "Warning Canvas3D_isSceneAntialiasingAvailable got unexpected number of matching visuals %d\n", nitems);
    }
    /* try GLX_ARB_multisample */
    glxExtensions = (const char *)glXGetClientString((Display*)display, GLX_EXTENSIONS);
    
    if(isExtensionSupported(glxExtensions, "GLX_ARB_multisample")){
	glXGetConfig(dpy, vinfo, GLX_SAMPLE_BUFFERS_ARB, &numSampleBuffers);
	glXGetConfig(dpy, vinfo, GLX_SAMPLES_ARB, &numSamples);
	if(numSampleBuffers > 0 && numSamples > 1){
	    return JNI_TRUE;
	}
    }

    return JNI_FALSE;
}


JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_J3dGraphicsConfig_isValidVisualID(
    JNIEnv *env,
    jclass cls,
    jlong display,
    jint vid)
{
   XVisualInfo template;
   int nitems;
   
   template.visualid = vid;
   XGetVisualInfo((Display *)display, VisualIDMask, &template, &nitems);
   return (nitems == 1);

}
#endif /* SOLARIS || __linux__ */


#ifdef WIN32

extern HWND createDummyWindow(const char* szAppName);

/*
void printPixelDescriptor(PIXELFORMATDESCRIPTOR *pfd)
{

    printf("color : r=%d, g=%d, b=%d, a=%d, shift r=%d, g=%d, b=%d, a=%d\n",
	   pfd->cRedBits, pfd->cGreenBits, pfd->cBlueBits, pfd->cAlphaBits,
	   pfd->cRedShift, pfd->cGreenShift, pfd->cBlueShift, pfd->cAlphaShift); 
    printf("Accum r=%d, g=%d, b=%d, a=%d, depth %d, stencil %d, AuxBuffers %d\n",
	   pfd->cAccumRedBits, pfd->cAccumGreenBits, pfd->cAccumBlueBits,
	   pfd->cAccumAlphaBits, pfd->cDepthBits, pfd->cStencilBits, pfd->cAuxBuffers);
    printf("iLayerType %x, bReserved %x, dwLayerMask %x, dwVisibleMask %x, dwDamageMask %x\n",
	   pfd->iLayerType, pfd->bReserved, pfd->dwLayerMask, pfd->dwVisibleMask, pfd->dwDamageMask);
    if (pfd->dwFlags & PFD_SUPPORT_OPENGL) {
	printf("SUPPORT_OPENGL ");
    }
    if (pfd->dwFlags & PFD_DRAW_TO_WINDOW) {
	printf("DRAW_TO_WINDOW ");
    }
    if (pfd->dwFlags & PFD_DRAW_TO_BITMAP) {
	printf("DRAW_TO_BITMAP ");
    }
    if (pfd->dwFlags & PFD_SUPPORT_GDI) {
	printf("SUPPORT_GDI ");
    }
    if (pfd->dwFlags & PFD_SUPPORT_GDI) {
	printf("NEED_PALETTE ");
    }
    if (pfd->dwFlags & PFD_SUPPORT_GDI) {
	printf("NEED_SYSTEM_PALETTE ");
    }
    if (pfd->dwFlags & PFD_STEREO) {
	printf("STEREO ");
    }
    if (pfd->dwFlags & PFD_SUPPORT_GDI) {
	printf("SWAP_LAYER_BUFFERS ");
    }
    if (pfd->dwFlags & PFD_GENERIC_FORMAT) {
	printf("PFD_GENERIC_FORMAT ");		    
    }
    if (pfd->dwFlags & PFD_GENERIC_ACCELERATED) {
	printf("PFD_GENERIC_FORMAT ");		    
    }
    if (pfd->dwFlags & PFD_DOUBLEBUFFER) {
	printf("PFD_DOUBLEBUFFER ");		    	
    }
    printf("\n");

}

*/

BOOL isSupportedWGL(const char * extensions, const char *extension_string) {    
    /* get the list of supported extensions */
    const char *p = extensions; 

    /* search for extension_string in the list */
    while(p = strstr(p, extension_string)){
	const char *q = p + strlen(extension_string);

	/* must be terminated by <space> or <nul> */
	if(*q == ' ' || *q == '\0') {
	    return TRUE;
	}

	/* try to find another match */
	p = q;
    }
    return FALSE;
}

HDC getMonitorDC(int screen)
{
    return CreateDC("DISPLAY", NULL, NULL, NULL);     
}

int findPixelFormatSwitchDoubleBufferAndStereo (PIXELFORMATDESCRIPTOR* pfd, HDC hdc, int *mx_ptr)
{

    int pf;
    
    pf = ChoosePixelFormat(hdc, pfd);

    /* Check if pixel format support min. requirement */
    DescribePixelFormat(hdc, pf, sizeof(*pfd), pfd);
    
    if ((pfd->cRedBits < (unsigned char) mx_ptr[RED_SIZE]) || 
	(pfd->cGreenBits < (unsigned char) mx_ptr[GREEN_SIZE]) ||
	(pfd->cBlueBits  < (unsigned char) mx_ptr[BLUE_SIZE]) ||
	(pfd->cDepthBits < (unsigned char) mx_ptr[DEPTH_SIZE]) ||
	((mx_ptr[DOUBLEBUFFER] == REQUIRED) && ((pfd->dwFlags & PFD_DOUBLEBUFFER) == 0)) ||
	((mx_ptr[STEREO] == REQUIRED) && ((pfd->dwFlags & PFD_STEREO) == 0)))
	{
	    return -1;
	}
    
    if ((mx_ptr[ANTIALIASING] == REQUIRED) &&
	((pfd->cAccumRedBits <= 0) ||
	 (pfd->cAccumGreenBits <= 0) ||
	 (pfd->cAccumBlueBits  <= 0)))
	{
	    return -1;
	}
    return pf; 
}


void printErrorMessage(char *message) 
{
    DWORD err;
    char * errString;

    err = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
		  FORMAT_MESSAGE_FROM_SYSTEM,
		  NULL, err, 0, (LPTSTR)&errString, 0, NULL);    
    fprintf(stderr, "%s - %s\n", message, errString);
    LocalFree(errString);
}

/* Prefer multiSample in following order
   4, 5, 6, 3, 7, 8, 2, 9, 10, 11, ...
*/
int getMultiSampleScore(int s)
{
    static int multiSampleScore[9] = {9999, 9999, 6, 3, 0, 1, 2, 4, 5};

    if (s < 9) {
	return multiSampleScore[s];
    }
    return s-2;
}


/* Max no of format wglChoosePixelFormatEXT can return  */
#define NFORMAT 100

int getExtPixelFormat(int *nativeConfigAttrs)
{
    static const BOOL debug = FALSE;
    static char szAppName[] = "Choose Pixel Format";

    static PIXELFORMATDESCRIPTOR pfd = {
	sizeof(PIXELFORMATDESCRIPTOR),
	1,                      /* Version number */
	PFD_DRAW_TO_WINDOW |
	PFD_SUPPORT_OPENGL,
	PFD_TYPE_RGBA,
	16,                     /* 16 bit color depth */
	0, 0, 0,                /* RGB bits and pixel sizes */
	0, 0, 0,                /* Do not care about them */
	0, 0,                   /* no alpha buffer info */
	0, 0, 0, 0, 0,          /* no accumulation buffer */
	8,                      /* 8 bit depth buffer */
	0,                      /* no stencil buffer */
	0,                      /* no auxiliary buffers */
	PFD_MAIN_PLANE,         /* layer type */
	0,                      /* reserved, must be 0 */
	0,                      /* no layer mask */
	0,                      /* no visible mask */
	0                       /* no damage mask */
    };

    HWND hwnd;
    HGLRC hrc;
    HDC   hdc;
    int attr[22];
    int piValues[12];
    int i, idx;
    int pNumFormats[NFORMAT], nAvailableFormat;
    const char* supportedExtensions;
    int score;
    int highestScore, highestScorePF;
    int highestScoreAlpha, lowestScoreMultiSample;

    /* declare function pointers for WGL functions */
    PFNWGLGETEXTENSIONSSTRINGEXTPROC  wglGetExtensionsStringEXT = NULL;
    PFNWGLCHOOSEPIXELFORMATEXTPROC wglChoosePixelFormatEXT = NULL;
    PFNWGLGETPIXELFORMATATTRIBIVEXTPROC wglGetPixelFormatAttribivEXT = NULL;    

    /*
     * Select any pixel format and bound current context to
     * it so that we can get the wglChoosePixelFormatARB entry point.
     * Otherwise wglxxx entry point will always return null.
     * That's why we need to create a dummy window also.
     */
    hwnd = createDummyWindow((const char *)szAppName);

    if (!hwnd) {
	return -1;
    }
    hdc = GetDC(hwnd);

    pNumFormats[0] = ChoosePixelFormat(hdc, &pfd);
    if (!pNumFormats[0]) {
	printErrorMessage("Failed in ChoosePixelFormat");
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return -1;
    }

    SetPixelFormat(hdc, pNumFormats[0], &pfd);
    
    hrc = wglCreateContext(hdc);
    if (!hrc) {
	printErrorMessage("Failed in wglCreateContext");
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return -1;
    }
    
    if (!wglMakeCurrent(hdc, hrc)) {
	printErrorMessage("Failed in wglMakeCurrent");
	ReleaseDC(hwnd, hdc);
	wglDeleteContext(hrc);
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return -1;
    }

    wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)
	wglGetProcAddress("wglGetExtensionsStringARB");

    if (wglGetExtensionsStringEXT == NULL) {
	wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC)
	    wglGetProcAddress("wglGetExtensionsStringEXT");
	if (wglGetExtensionsStringEXT == NULL) {
	    if (debug) {
		printf("wglGetExtensionsStringEXT/ARB not support !\n");
	    }
	    ReleaseDC(hwnd, hdc);
	    wglDeleteContext(hrc);
	    DestroyWindow(hwnd);
	    UnregisterClass(szAppName, (HINSTANCE)NULL);	    
	    return -1;
	}
	if (debug) {
	    printf("Support wglGetExtensionsStringEXT\n");
	}
    } else {
	if (debug) {	
	    printf("Support wglGetExtensionsStringARB\n");
	}
    }
    
    /* get the list of supported extensions */
    supportedExtensions = (const char *)wglGetExtensionsStringEXT(hdc);    

    if (debug) {
	fprintf(stderr, "WGL Supported extensions: %s.\n", supportedExtensions);    
    }
    
    if (!isSupportedWGL(supportedExtensions, "WGL_ARB_multisample") &&
	!isSupportedWGL(supportedExtensions, "WGL_EXT_multisample") &&
	!isSupportedWGL(supportedExtensions, "WGL_SGIS_multisample")) {	

	/* Under Wildcat III it doesn't use wglGetExtensionString */
	supportedExtensions = (char *) glGetString(GL_EXTENSIONS);

	if (debug) {
	    fprintf(stderr, "GL Supported extensions: %s.\n", supportedExtensions);    
	}
	
	if (!isSupportedWGL(supportedExtensions, "GL_ARB_multisample") &&
	    !isSupportedWGL(supportedExtensions, "GL_EXT_multisample") &&
	    !isSupportedWGL(supportedExtensions, "GL_SGIS_multisample")) {	    
	    ReleaseDC(hwnd, hdc);
	    wglDeleteContext(hrc);
	    DestroyWindow(hwnd);
	    UnregisterClass(szAppName, (HINSTANCE)NULL);	    
	    return -1;
	}
    }
    
    wglChoosePixelFormatEXT = (PFNWGLCHOOSEPIXELFORMATEXTPROC)
	wglGetProcAddress("wglChoosePixelFormatARB");
    
    if (wglChoosePixelFormatEXT == NULL) {
	wglChoosePixelFormatEXT = (PFNWGLCHOOSEPIXELFORMATEXTPROC)
	    wglGetProcAddress("wglChoosePixelFormatEXT");
	if (wglChoosePixelFormatEXT == NULL) {
	    if (debug) {
		printf("wglChoosePixelFormatARB/EXT not support !\n");
	    }
	    ReleaseDC(hwnd, hdc);
	    wglDeleteContext(hrc);
	    DestroyWindow(hwnd);
	    UnregisterClass(szAppName, (HINSTANCE)NULL);	    	    
	    return -1;
	} 
	if (debug) {
	    printf("Support wglChoosePixelFormatEXT\n");
	}
    } else {
	if (debug) {
	    printf("Support wglChoosePixelFormatARB\n");
	}	
    }
    
    idx = 0;
    attr[idx++] = WGL_SUPPORT_OPENGL_EXT;
    attr[idx++] = TRUE;
    attr[idx++] = WGL_DRAW_TO_WINDOW_EXT; 
    attr[idx++] = TRUE;
    attr[idx++] = WGL_RED_BITS_EXT;
    attr[idx++] = nativeConfigAttrs[RED_SIZE];
    attr[idx++] = WGL_GREEN_BITS_EXT;
    attr[idx++] = nativeConfigAttrs[GREEN_SIZE];
    attr[idx++] = WGL_BLUE_BITS_EXT;
    attr[idx++] = nativeConfigAttrs[BLUE_SIZE];
    attr[idx++] = WGL_DEPTH_BITS_EXT;
    attr[idx++] = nativeConfigAttrs[DEPTH_SIZE];

    if (nativeConfigAttrs[DOUBLEBUFFER] == REQUIRED) {
	attr[idx++] = WGL_DOUBLE_BUFFER_EXT;
	attr[idx++] = TRUE;
    }
    if (nativeConfigAttrs[STEREO] == REQUIRED) {    
	attr[idx++] = WGL_STEREO_EXT;
	attr[idx++] = TRUE;
    }

    if (nativeConfigAttrs[ANTIALIASING] == REQUIRED) {        
	attr[idx++] = WGL_SAMPLE_BUFFERS_ARB;
	attr[idx++] = TRUE;
	attr[idx++] = WGL_SAMPLES_ARB;
	attr[idx++] = 2;
    }

    /*
     * Terminate by 2 zeros to avoid driver bugs
     * that assume attributes always come in pairs.
     */
    attr[idx++] = 0;
    attr[idx++] = 0;
	
    if (!wglChoosePixelFormatEXT(hdc, (const int *)attr, NULL, NFORMAT,
				 pNumFormats, &nAvailableFormat)) {
	printErrorMessage("Failed in wglChoosePixelFormatEXT");
	ReleaseDC(hwnd, hdc);
	wglDeleteContext(hrc);
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);	    
	return -1;
    }
    
    if (debug) {
	printf("No. of available pixel format is: %d\n", nAvailableFormat);
    }

    if (nAvailableFormat <= 0) {
	ReleaseDC(hwnd, hdc);
	wglDeleteContext(hrc);
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);	    
	return -1;
    }
    
    wglGetPixelFormatAttribivEXT = (PFNWGLGETPIXELFORMATATTRIBIVEXTPROC)
	wglGetProcAddress("wglGetPixelFormatAttribivARB");

    if (wglGetPixelFormatAttribivEXT == NULL) {
	wglGetPixelFormatAttribivEXT = (PFNWGLGETPIXELFORMATATTRIBIVEXTPROC)
	    wglGetProcAddress("wglGetPixelFormatAttribivEXT");

	if (wglGetPixelFormatAttribivEXT == NULL) {
	    if (debug) {
		printf("wglGetPixelFormatAttribivEXT/ARB not support !\n");
	    }
	    ReleaseDC(hwnd, hdc);
	    wglDeleteContext(hrc);
	    DestroyWindow(hwnd);
	    UnregisterClass(szAppName, (HINSTANCE)NULL);	    	    
	    return -1;
	} 
	if (debug) {
	    printf("Support wglGetPixelFormatAttribivEXT\n");
	}
    } else {
	if (debug) {
	    printf("Support wglGetPixelFormatAttribivARB\n");
	}	
    }

    idx = 0;
    attr[idx++] = WGL_ACCELERATION_EXT;
    attr[idx++] = WGL_RED_BITS_EXT;
    attr[idx++] = WGL_GREEN_BITS_EXT;
    attr[idx++] = WGL_BLUE_BITS_EXT;
    attr[idx++] = WGL_ALPHA_BITS_EXT;    
    attr[idx++] = WGL_DEPTH_BITS_EXT;
    attr[idx++] = WGL_STENCIL_BITS_EXT;
    attr[idx++] = WGL_SAMPLE_BUFFERS_ARB;
    attr[idx++] = WGL_SAMPLES_ARB;
    attr[idx++] = WGL_DOUBLE_BUFFER_EXT;
    attr[idx++] = WGL_STEREO_EXT;
    attr[idx] = 0;

    /* Select the best pixel format based on score */
    highestScore = 0;
    highestScorePF = -1;
    highestScoreAlpha = 0;
    lowestScoreMultiSample = 9999;
    
    for (i=0; i < nAvailableFormat; i++) {
	if (!wglGetPixelFormatAttribivEXT(hdc, pNumFormats[i], 0, idx, attr, piValues)) {
	    printErrorMessage("Failed in wglGetPixelFormatAttribivEXT");
	    ReleaseDC(hwnd, hdc);
	    wglDeleteContext(hrc);
	    DestroyWindow(hwnd);
	    UnregisterClass(szAppName, (HINSTANCE)NULL);
	    return -1;
	}
	if (debug) {
	    printf("Format %d\n", pNumFormats[i]);

	    if (piValues[0] == WGL_FULL_ACCELERATION_EXT) {
		printf("WGL_FULL_ACCELERATION_EXT");
	    } else if (piValues[0] == WGL_GENERIC_ACCELERATION_EXT) {
		printf("WGL_GENERIC_ACCELERATION_EXT");		
	    } else {
		printf("WGL_NO_ACCELERATION_EXT");		
	    }

	    printf(" R %d, G %d, B %d, A %d, Depth %d, Stencil %d",
		   piValues[1], piValues[2], piValues[3], piValues[4],
		   piValues[5], piValues[6]);

	    if (piValues[7] == TRUE) {
		printf(" MultiSample %d", piValues[8]);
	    }

	    if (piValues[9] == TRUE) {
		printf(" DoubleBuffer");
	    }
	    
	    if (piValues[10] == TRUE) {
		printf(" Stereo");
	    }
	    printf("\n");
	}

	/* Red, Green, Blue are fixed under windows so they are not checked */
	score = 0;

	if (piValues[0] == WGL_FULL_ACCELERATION_EXT) {
	    score += 20000;
	} else if (piValues[0] == WGL_GENERIC_ACCELERATION_EXT) {
	    score += 10000;
	}
	if ((nativeConfigAttrs[DOUBLEBUFFER] == PREFERRED) &&
	    (piValues[9] == TRUE)) {
	    score += 5000;
	}
	if (piValues[4] > 0) { /* Alpha */
	    score += 2500;
	}
	if ((nativeConfigAttrs[STEREO] == PREFERRED) &&
	    (piValues[10] == TRUE)) {
	    score += 1250;
	}
	if ((nativeConfigAttrs[ANTIALIASING] == PREFERRED) &&
	    (piValues[7] == TRUE)) {
	    score += 624;
	}
	
	/* Stencil bit * 10 + Depth bit */	
	score += piValues[6]*10 + piValues[5];

	if (score > highestScore) {
	    highestScore = score;
	    highestScorePF = i;	    
	    highestScoreAlpha = piValues[4];
	    lowestScoreMultiSample = getMultiSampleScore(piValues[8]);
	} else if (score == highestScore) {
	    if (piValues[4] > highestScoreAlpha) {
		highestScore = score;
		highestScorePF = i;	    
		highestScoreAlpha = piValues[4];
		lowestScoreMultiSample = getMultiSampleScore(piValues[8]);
	    } else if (piValues[4] == highestScoreAlpha) {
		if (getMultiSampleScore(piValues[8]) < lowestScoreMultiSample) { 
		    highestScore = score;
		    highestScorePF = i;	    
		    highestScoreAlpha = piValues[4];
		    lowestScoreMultiSample = getMultiSampleScore(piValues[8]);
		} 
	    }
	}

    }

    if (debug) {
	printf("Select Pixel Format %d\n", pNumFormats[highestScorePF]);
    }

    ReleaseDC(hwnd, hdc);
    wglDeleteContext(hrc);
    DestroyWindow(hwnd);
    UnregisterClass(szAppName, (HINSTANCE)NULL);
    return pNumFormats[highestScorePF];
}


JNIEXPORT
jint JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_choosePixelFormat(
    JNIEnv   *env,
    jobject   obj,
    jlong     ctxInfo,
    jint      screen,
    jintArray attrList)
{
    int *mx_ptr;
    int   dbVal;  /* value for double buffering */
    int   sVal;   /* value for stereo */
    HDC   hdc;
    int pf;  /* PixelFormat */
    PIXELFORMATDESCRIPTOR pfd;
     
    mx_ptr = (int *)(*env)->GetIntArrayElements(env, attrList, NULL);

    if (mx_ptr[ANTIALIASING] != UNNECESSARY) {
	pf = getExtPixelFormat(mx_ptr);
	if (pf > 0) {
	    return pf;
	}

	/* fallback to use standard ChoosePixelFormat and accumulation buffer */
    }

    hdc = getMonitorDC(screen);
    
    ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;  /*TODO: when would this change? */
    pfd.iPixelType = PFD_TYPE_RGBA;

    /*
     * Convert Java 3D values to PixelFormat
     */

    pfd.cRedBits   = (unsigned char) mx_ptr[RED_SIZE];
    pfd.cGreenBits = (unsigned char) mx_ptr[GREEN_SIZE];
    pfd.cBlueBits  = (unsigned char) mx_ptr[BLUE_SIZE];
    pfd.cDepthBits = (unsigned char) mx_ptr[DEPTH_SIZE];

    if (mx_ptr[DOUBLEBUFFER] == REQUIRED || mx_ptr[DOUBLEBUFFER] == PREFERRED)
	dbVal = PFD_DOUBLEBUFFER;
    else 
	dbVal = PFD_DOUBLEBUFFER_DONTCARE;

    sVal = 0;
    if (mx_ptr[STEREO] == REQUIRED || mx_ptr[STEREO] == PREFERRED) {
	sVal = PFD_STEREO;
    } else {
	sVal = 0;
    }
    
    pfd.dwFlags = dbVal | sVal | PFD_SUPPORT_OPENGL;
    pfd.cStencilBits = 2;
    
    if (mx_ptr[ANTIALIASING] == REQUIRED || mx_ptr[ANTIALIASING] == PREFERRED) {
	pfd.cAccumRedBits   = 8;
	pfd.cAccumGreenBits = 8;
	pfd.cAccumBlueBits  = 8;
    }

    pf = findPixelFormatSwitchDoubleBufferAndStereo(&pfd, hdc, mx_ptr); 

    if (pf == -1) {
	/* try disable stencil buffer */
	pfd.cStencilBits = 0;
	pf =  findPixelFormatSwitchDoubleBufferAndStereo(&pfd, hdc, mx_ptr);

	if (pf == -1) {
	    /* try disable accumulate buffer */
	    if (mx_ptr[ANTIALIASING] == PREFERRED) {
		pfd.cStencilBits = 2;
		pfd.cAccumRedBits   = 0;
		pfd.cAccumGreenBits = 0;
		pfd.cAccumBlueBits  = 0;
		pf =  findPixelFormatSwitchDoubleBufferAndStereo(&pfd, hdc, mx_ptr);

		if (pf == -1) {
		    /* try disable stencil buffer */
		    pfd.cStencilBits = 0;
		    pf =  findPixelFormatSwitchDoubleBufferAndStereo(&pfd, hdc, mx_ptr);
		}
	    }
	}
    }
    
    DeleteDC(hdc);

    (*env)->ReleaseIntArrayElements(env, attrList, mx_ptr, JNI_ABORT);
    return pf;
}


JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isStereoAvailable(
    JNIEnv *env,
    jobject obj,
    jlong ctxInfo,
    jlong display,
    jint screen,
    jint pixelFormat)
{
    HDC hdc;          /* HW Device Context */

    PIXELFORMATDESCRIPTOR pfd;

    static GLboolean first_time = GL_TRUE;
    static GLboolean force_no_stereo = GL_FALSE;

    if (first_time) {
        if (getenv("J3D_NO_STEREO") != NULL) {
            fprintf(stderr, "Java 3D: stereo mode disabled\n");
            force_no_stereo = GL_TRUE;
        }
        first_time = GL_FALSE;
    }

    if (force_no_stereo)
        return JNI_FALSE;

    hdc = getMonitorDC(screen);

    /* Check the chosen PixelFormat to see if it is stereo capable */
    DescribePixelFormat(hdc, pixelFormat, sizeof(pfd), &pfd);

    DeleteDC(hdc);
    if (pfd.dwFlags & PFD_STEREO) 
        return JNI_TRUE;
    else
        return JNI_FALSE;

    return JNI_TRUE;
}


JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isDoubleBufferAvailable(
    JNIEnv *env,
    jobject obj,
    jlong ctxInfo,
    jlong display,    
    jint screen,
    jint pixelFormat)
{
    HDC hdc;          /* HW Device Context */

    PIXELFORMATDESCRIPTOR pfd;

    hdc = getMonitorDC(screen);
    
    /* Check the chosen PixelFormat to see if it is doubleBuffer capable */
    DescribePixelFormat(hdc, pixelFormat, sizeof(pfd), &pfd);

    DeleteDC(hdc);
    if (pfd.dwFlags & PFD_DOUBLEBUFFER)
        return JNI_TRUE;
    else
        return JNI_FALSE;
}



JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isSceneAntialiasingAccumAvailable(
    JNIEnv *env,
    jobject obj,
    jlong ctxInfo,
    jlong display,
    jint screen,
    jint pixelFormat)
{
    HDC hdc;          /* HW Device Context */
    PIXELFORMATDESCRIPTOR pfd;

    hdc = getMonitorDC(screen);
    /* Check the chosen PixelFormat to see if it is sceneAntialiasing capable */
    DescribePixelFormat(hdc, pixelFormat, sizeof(pfd), &pfd);

    DeleteDC(hdc);
    if (pfd.cAccumRedBits > 0)
        return JNI_TRUE;
    else
        return JNI_FALSE;
}

JNIEXPORT
jboolean JNICALL Java_javax_media_j3d_NativeConfigTemplate3D_isSceneAntialiasingMultiSamplesAvailable(
    JNIEnv *env,
    jobject obj,
    jlong ctxInfo,
    jlong display,
    jint screen,
    jint pixelFormat)
{
    /*
     * Issue 16: this routine is commented out (and JNI_FALSE is
     * returned) until a solution can be found that doesn't affect the
     * current WGL context for the calling thread, or we need to
     * define and implement a mechanism to revalidate the current
     * context.
     */
    return JNI_FALSE;

#if 0
    static char szAppName[] = "Choose Pixel Format";
    HWND hwnd;
    HGLRC hrc;
    HDC   hdc;
    int attr[3];
    int piValues[2];
    int pf;
    BOOL support;
    
    static PIXELFORMATDESCRIPTOR pfd = {
	sizeof(PIXELFORMATDESCRIPTOR),
	1,                      /* Version number */
	PFD_DRAW_TO_WINDOW |
	PFD_SUPPORT_OPENGL,
	PFD_TYPE_RGBA,
	16,                     /* 16 bit color depth */
	0, 0, 0,                /* RGB bits and pixel sizes */
	0, 0, 0,                /* Do not care about them */
	0, 0,                   /* no alpha buffer info */
	0, 0, 0, 0, 0,          /* no accumulation buffer */
	8,                      /* 8 bit depth buffer */
	0,                      /* no stencil buffer */
	0,                      /* no auxiliary buffers */
	PFD_MAIN_PLANE,         /* layer type */
	0,                      /* reserved, must be 0 */
	0,                      /* no layer mask */
	0,                      /* no visible mask */
	0                       /* no damage mask */
    };

    PFNWGLGETPIXELFORMATATTRIBIVEXTPROC wglGetPixelFormatAttribivEXT = NULL;

    hwnd = createDummyWindow((const char *)szAppName);
 
    if (!hwnd) {
	return -1;
    }
    hdc = GetDC(hwnd);

    pf = ChoosePixelFormat(hdc, &pfd);
    if (!pf) {
	printErrorMessage("Failed in ChoosePixelFormat");
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return -1;
    }

    SetPixelFormat(hdc, pf, &pfd);
    
    hrc = wglCreateContext(hdc);
    if (!hrc) {
	printErrorMessage("Failed in wglCreateContext");
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return -1;
    }
    
    if (!wglMakeCurrent(hdc, hrc)) {
	printErrorMessage("Failed in wglMakeCurrent");
	ReleaseDC(hwnd, hdc);
	wglDeleteContext(hrc);
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return -1;
    }

    wglGetPixelFormatAttribivEXT = (PFNWGLGETPIXELFORMATATTRIBIVEXTPROC)
	wglGetProcAddress("wglGetPixelFormatAttribivARB");

    if (wglGetPixelFormatAttribivEXT == NULL) {
	wglGetPixelFormatAttribivEXT = (PFNWGLGETPIXELFORMATATTRIBIVEXTPROC)
	    wglGetProcAddress("wglGetPixelFormatAttribivEXT");

	if (wglGetPixelFormatAttribivEXT == NULL) {
	    ReleaseDC(hwnd, hdc);
	    wglDeleteContext(hrc);
	    DestroyWindow(hwnd);
	    UnregisterClass(szAppName, (HINSTANCE)NULL);	    	    
	    return FALSE;
	} 
    }

    attr[0] = WGL_SAMPLE_BUFFERS_ARB;
    attr[1] = WGL_SAMPLES_ARB;
    attr[2] = 0;
    support = FALSE;
    
    if (wglGetPixelFormatAttribivEXT(hdc, pixelFormat, 0, 2, attr, piValues)) {
	if ((piValues[0] == TRUE) && (piValues[1] > 1)) {
	    support = TRUE;
	}
    } 

    ReleaseDC(hwnd, hdc);
    wglDeleteContext(hrc);
    DestroyWindow(hwnd);
    UnregisterClass(szAppName, (HINSTANCE)NULL);
    return support;
#endif /* 0 */
}
#endif /* WIN32 */