aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/OpenGL_Win32.c
blob: a4c22b3ee8c112abdb7e74c8b3b642ff01bd8e19 (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
/*
 * Original Author: Leo Chan -- 1995
 *
 * Adam King 1997
 *		Ported to Win32 from X
 *
 * This file takes care of the C implementation of finding the correct
 * Win32 window, assigning an OpenGL graphics context to it, storing that
 * graphics context in the java class data structure.
 *
 * also contains the use() and swap() functions for double buffering
 *
 * September 12, 1997	- Adam King
 *		- Added support for rendering directly into a Canvas ( BIG rewrite )
 */

/**
 * Pointer Semantics of GLContext:
 * ==============================

   "displayHandle"	:= not used
   	
   "pData"              := (HDC)
   	This is original Java-Native Window Handle !
	It is transfered -> "windowHandle" in the beginning,
	so if no own created window will be used, "pData" == "windowHandle" !

   "windowHandle" 	:= (HDC)
        note: createOwnWindow is not recognized while creation process ..

   "pixmapHandle"	:= (HBITMAP)
   	if("offScreenRenderer" == TRUE)
		"pixmapHandle" contains the new BITMAP (by CreateDIBSection)!
		"windowHandle" contains the new created OffScreenWindow 
			       (by CreateCompatibleDC)!
        else
		"pixmapHandle" is unused !

   "sharedGLContextNative" := (HGLRC)
   	This is the optional shared GLContext !

   "glContext" := (HGLRC)
   	This is THE used GLContext !
 */

#include "OpenGL_Win32_common.h"

static jboolean verbose = JNI_FALSE;

JNIEXPORT jboolean JNICALL Java_gl4java_GLContext_loadJAWT0
(JNIEnv *env, jclass unused, jstring str)
{
  (void)env;
  (void)unused;
  (void)str;
  return JNI_FALSE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_useJAWT( JNIEnv *env, jclass unused )
{
    (void)env;
    (void)unused;
    return JNI_FALSE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_hasJAWTSurfaceChanged( JNIEnv *env, jobject obj,
					      jlong thisWin )
{
    (void)env;
    (void)obj;
    (void)thisWin;
    return JNI_FALSE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_lockJAWT( JNIEnv *env, jobject obj,
				 jobject canvas,
				 jlong thisWin, jboolean verbose
			       )
{
    (void) env;
    (void) obj;
    (void) canvas;
    (void) thisWin;
    (void) verbose;
    return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_unlockJAWT( JNIEnv *env, jobject obj,
				   jlong thisWin, jboolean verbose
				 )
{
    (void) env;
    (void) obj;
    (void) thisWin;
    (void) verbose;
    return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_openOpenGLNative( JNIEnv *env, jobject obj,
					jobject canvas)
{
    jboolean ret = JNI_TRUE;

    jclass cls = 0;
    jfieldID fpixmapHandle=0;
    jfieldID foffScreenRenderer=0;
    jfieldID faccumSize=0;
    jfieldID fwindowHandle=0, fpData=0;
    jfieldID fverbose=0;
    jfieldID fglContext=0;
    jfieldID fshareWith=0;
    jfieldID fcreatewinw = 0, fcreatewinh = 0;
    jfieldID fglCaps=0;

    /* these variables will be mapped in the java-object ! */
    jboolean joffScreenRenderer=JNI_FALSE;
    HDC thisWin=0;
    HGLRC gc=0;
    PIXELFORMATDESCRIPTOR pfd;
    HDC pData=0;
	HGLRC shareWith=NULL;
    jint jcreatewinw = 0, jcreatewinh = 0;
    HBITMAP pix=0;
    GLCapabilities glCaps;
    jobject  jglCaps=0;
    jthrowable exc;
    jclass _GLCapabilities= 0;

    (void) canvas;

    cls = (*env)->GetObjectClass(env, obj);
    if(cls==0)
    {
        fprintf(stderr,"oo0.0 cls==0\n");
		  fflush(stderr);
		  return JNI_FALSE;
    }

    if(ret==JNI_TRUE)
    {
	    fverbose =(*env)->GetStaticFieldID(env, cls, "gljNativeDebug", "Z");
	    if (fverbose == 0)
	    {
                fprintf(stderr,"oo0.2 fverbose==0\n");
                fflush(stderr);
		return JNI_FALSE;
	    } else {
		verbose = (*env)->GetStaticBooleanField(env, cls, fverbose);
	    }
    }

    if(JNI_TRUE==verbose)
    {
		fprintf(stderr, "sizes:\n jlong=%d\n HGLRC=%d\n HDC=%d\n",
	  		sizeof(jlong), sizeof(HGLRC), sizeof(HDC) );
		fflush(stderr);
    }

    /* FIRST OF ALL CHECK IF A NATIVE POINTER OR WIN-TYPE FITS IN �jlong� */
    ret = testWin32Java();

    ret = testJavaGLTypes(verbose);

    if(ret==JNI_TRUE) {
	    fpData = (*env)->GetFieldID(env, cls, "pData", "J");
		if (fpData == 0) {
			fprintf(stderr, "pData not accessible !\n");
			fflush(stderr);
			ret= JNI_FALSE;
		} else pData = (HDC)
	      ( (PointerHolder)(*env)->GetLongField(env, obj, fpData));
    }
    if(JNI_TRUE==verbose)
    {
    	fprintf(stderr, "received pData : %d\n", (int)pData);
    }

    if(ret==JNI_TRUE) {
	    fwindowHandle = (*env)->GetFieldID(env, cls, "windowHandle", "J");
	    if (fwindowHandle == 0) {
			fprintf(stderr, "windowHandle not accessible !\n");
			fflush(stderr);
			ret= JNI_FALSE;
		}
    }

    if(ret==JNI_TRUE) {
	    fglContext=(*env)->GetFieldID(env, cls, "glContext", "J");
	    if (fglContext == 0)
	    {
			fprintf(stderr, "fglContext not accessible !\n");
			fflush(stderr);
			ret= JNI_FALSE;
		}
    }

    if(ret==JNI_TRUE) {
	    foffScreenRenderer = 
	    		(*env)->GetFieldID(env, cls, "offScreenRenderer", "Z");
	    if (foffScreenRenderer == 0)
	    {
			fprintf(stderr, "foffScreenRenderer not accessible !\n");
			fflush(stderr);
			ret= JNI_FALSE;
		}
	    else joffScreenRenderer =(*env)->GetBooleanField(env, obj, foffScreenRenderer);
    }

    if(ret==JNI_TRUE) {
            fglCaps = (*env)->GetFieldID(env, cls, "glCaps", "Lgl4java/GLCapabilities;");
            if (fglCaps == 0)
			{
				fprintf(stderr, "fglCaps not accessible !\n");
				fflush(stderr);
				ret= JNI_FALSE;
			}
            else jglCaps =(*env)->GetObjectField(env, obj, fglCaps);
    }

    if( jglCaps==0 )
    {
      fprintf(stderr,"\nGL4Java openOpenGL ERROR: ZERO capsObj was fetched by GetObjectField !\n");
      fflush(stderr);
      return JNI_FALSE;
    }

    _GLCapabilities= (*env)->FindClass(env, "Lgl4java/GLCapabilities;");
    exc = (*env)->ExceptionOccurred(env);
    if(exc) {
          if(JNI_TRUE==verbose)
	  {
             fprintf(stderr, "GL4Java: openOpen FindClass gl4java/GLCapabilities failed, cannot check glCaps object\n");
	     (*env)->ExceptionDescribe(env);
             fflush(stderr);
	  }
	  (*env)->ExceptionClear(env);
	  _GLCapabilities=0;
    }
    exc=0;

    if( _GLCapabilities!=0 &&
        (*env)->IsInstanceOf(env, jglCaps, _GLCapabilities)==JNI_FALSE )
    {
      fprintf(stderr,"\nGL4Java openOpenGL ERROR: glCaps is not instanceof gl4java/GLCapabilities !\n");
      fflush(stderr);
      return JNI_FALSE;
    }

    if( JNI_TRUE != javaGLCapabilities2NativeGLCapabilities ( env, jglCaps, &glCaps ) )
    {
      fprintf(stderr,"\nGL4Java ERROR: glCaps Object is not valid !\n");
      fflush(stderr);
      return JNI_FALSE;
    }

    if(ret==JNI_TRUE) {
	    fcreatewinw = (*env)->GetFieldID(env, cls, "createwinw", "I");
	    if (fcreatewinw == 0) ret= JNI_FALSE;
	    else jcreatewinw =(*env)->GetIntField(env, obj, fcreatewinw);
    }

    if(ret==JNI_TRUE) {
	    fcreatewinh = (*env)->GetFieldID(env, cls, "createwinh", "I");
	    if (fcreatewinh == 0) ret= JNI_FALSE;
	    else jcreatewinh =(*env)->GetIntField(env, obj, fcreatewinh);
    }

    if(ret==JNI_TRUE) {
	    fshareWith = (*env)->GetFieldID(env, cls, "sharedGLContextNative", "J");
	    if (fshareWith == 0) ret= JNI_FALSE;
	    else shareWith = (HGLRC) 
	      ( (PointerHolder)(*env)->GetLongField(env, obj, fshareWith));
    }

    if(ret==JNI_TRUE) {
	    fpixmapHandle = (*env)->GetFieldID(env, cls, "pixmapHandle", "J");
	    if (fpixmapHandle == 0) ret= JNI_FALSE;
    }

    if(joffScreenRenderer==JNI_TRUE)
    {
	glCaps.buffer=BUFFER_SINGLE;
    }

    if(JNI_TRUE==verbose && joffScreenRenderer==JNI_TRUE)
    {
		fprintf(stderr,"\nGL4Java:  (USING OFFSCREEN GLPIXMAP BUFFER,\n\t forced: !doubleBuffer)\n");
		fflush(stderr);
    }

    disp__wglMakeCurrent(NULL, NULL);

    /**
     * with version jdk1.1.6 and later, we are able to use 
     * the java-function straight to achieve the native window
     * handle ....
     *
     * this handle is saved to pData for the windows version !
     */
    if( pData == 0)
    {
	    printf( "get_GC: Error, pData is zero\n");
	    return JNI_FALSE;
    }
    if( pData != 0 )
	    thisWin = (HDC)pData;

    /* get the graphics context for this widget */
    if( (gc = get_GC( &thisWin, &glCaps, shareWith, 
	              joffScreenRenderer, jcreatewinw, jcreatewinh, 
		      &pix, verbose)) == 0 
      )
    {
	    printf( "getGC error" );
	    return JNI_FALSE;
    }

	/* fetch the states of doubleBuffer and stereo */
    (void) PixelFormatDescriptorFromDc( thisWin, &pfd);

    if(JNI_TRUE==verbose) 
    { 
	fprintf(stdout,"\nGL4Java: writing capabilities to GLContext's java object\n");
        fflush(stdout);
    }

    if(jglCaps!=0)
       (void) nativeGLCapabilities2JavaGLCapabilities (env, jglCaps, &glCaps);

    if(ret==JNI_TRUE && fglCaps && jglCaps) {
                    (*env)->SetObjectField(env, obj, fglCaps, jglCaps);
    }
  
    if(ret==JNI_TRUE && fwindowHandle!=0) {
	    (*env)->SetLongField(env, obj, fwindowHandle, 
	                        (jlong)((PointerHolder)thisWin));
    	 if(JNI_TRUE==verbose)
    		fprintf(stderr, "go and set windowHandle thisWin : %d; 0x%X\n",
         	(int)thisWin, (int)thisWin);
    }

    if(ret==JNI_TRUE && fpixmapHandle!=0) {
	    (*env)->SetLongField(env, obj, fpixmapHandle, (jlong)((PointerHolder)pix));
    	 if(JNI_TRUE==verbose)
    		fprintf(stderr, "go and set pixmapHandle pix: %d; 0x%X\n",
         	(int)pix, (int)pix);
    }

    if(ret==JNI_TRUE && fglContext !=0 ) {
	    (*env)->SetLongField(env, obj, fglContext, (jlong)((PointerHolder)gc));
    	 if(JNI_TRUE==verbose)
    		fprintf(stderr, "go and set gc  : %d, 0x%X\n",
         	(int)gc, (int)gc);
    }

	/*
	gl4java_bind_ext(JNI_TRUE==verbose);
	*/

    return ret;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljMakeCurrentNative( JNIEnv *env, jobject obj,
					     jobject canvas,
					     jlong disp,
					     jlong thisWin,
					     jlong glContext)
{
	jboolean ret = JNI_TRUE;

        (void)canvas;

	if( !thisWin )
	{
	    return JNI_FALSE;
	}

	if( !glContext )
	{
	    return JNI_FALSE;
	}

        if ( ret==JNI_TRUE && !disp__wglMakeCurrent((HDC)((PointerHolder)thisWin), 
	                                      (HGLRC)((PointerHolder)glContext)) )
        {
    		return JNI_FALSE;
        } 
        return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljFreeNative( JNIEnv *env, jobject obj,
				      jobject canvas,
				      jlong disp,
				      jlong thisWin,
				      jlong glContext
				    )
{
    (void)thisWin;
    (void)glContext;
    (void)disp;
    (void)canvas;

    if ( ! disp__wglMakeCurrent( NULL, NULL ) )
    {
			fprintf(stderr, "gljFree failed\n");
			fflush(stderr);
			return JNI_FALSE;
    }
    return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljDestroyNative( JNIEnv *env, jobject obj,
					 jobject canvas)
{
	jboolean ret = JNI_TRUE;

	jclass cls = 0;
	jfieldID fwindowHandle=0, fpixmapHandle=0;
	jfieldID fglContext=0;
    jfieldID foffScreenRenderer=0;
    jfieldID fownwind=0;

    jboolean jownwind = JNI_FALSE ;
    jboolean joffScreenRenderer=JNI_FALSE;
    HBITMAP pix=0;
	HDC thisWin=0;
	HGLRC gc=0;


	(void)canvas;

	cls = (*env)->GetObjectClass(env, obj);
	if(cls==0)
	{
	    ret=JNI_FALSE;
	}

	if(ret==JNI_TRUE) {
	    fwindowHandle = (*env)->GetFieldID(env, cls, "windowHandle", "J");
	    if (fwindowHandle == 0) ret= JNI_FALSE;
	    else thisWin =(HDC) 
	      ((PointerHolder)(*env)->GetLongField(env, obj, fwindowHandle));
	}

	if(ret==JNI_TRUE) {
	    fglContext=(*env)->GetFieldID(env, cls, "glContext", "J");
	    if (fglContext == 0) ret= JNI_FALSE;
	    else gc =(HGLRC)
	      ((PointerHolder)(*env)->GetLongField(env, obj, fglContext));
	}


    if(ret==JNI_TRUE) {
	    fpixmapHandle = (*env)->GetFieldID(env, cls, "pixmapHandle", "J");
	    if (fpixmapHandle == 0) ret= JNI_FALSE;
	    else pix =(HBITMAP)
	      ((PointerHolder)(*env)->GetLongField(env, obj, fpixmapHandle));
    }

    if(ret==JNI_TRUE) {
	    foffScreenRenderer = 
	    		(*env)->GetFieldID(env, cls, "offScreenRenderer", "Z");
	    if (foffScreenRenderer == 0) ret= JNI_FALSE;
	    else joffScreenRenderer =(*env)->GetBooleanField(env, obj, foffScreenRenderer);
    }

    if(ret==JNI_TRUE) {
	    fownwind = (*env)->GetFieldID(env, cls, "createOwnWindow", "Z");
	    if (fownwind == 0) ret= JNI_FALSE;
	    else jownwind =(*env)->GetBooleanField(env, obj, fownwind);
    }

    if(ret==JNI_TRUE)
    {
	    if ( gc == 0 )
	    {
	        if(JNI_TRUE==verbose)
			{
				fprintf(stderr, "gljDestroy error, GL context is 0\n");
				fflush(stderr);
			}
		}

		if ( ! disp__wglMakeCurrent( NULL, NULL ) )
		{
			fprintf(stderr, "gljDestroy failed (free)\n");
			fflush(stderr);
		} 

		if(ret==JNI_TRUE) 
		{
			if(gc!=0)
				disp__wglDeleteContext(gc);

			if(pix!=0)
			{
			    if(thisWin!=0)
				    DeleteDC(thisWin);
			    thisWin=0;
			    DeleteObject(pix);
			    pix=0;
			}
			if(jownwind && thisWin!=0)
			{
			    DeleteDC(thisWin);
			    thisWin=0;
			    jownwind=JNI_FALSE;
			}
		}
	}

	gc = 0;
	thisWin = 0;

	ret=JNI_TRUE;

    if(ret==JNI_TRUE && fwindowHandle!=0) {
	    (*env)->SetLongField(env, obj, fwindowHandle, (jlong)((PointerHolder)thisWin));
    }

    if(ret==JNI_TRUE && fglContext) {
	    (*env)->SetLongField(env, obj, fglContext, (jlong)((PointerHolder)gc));
    }


    if(ret==JNI_TRUE && fpixmapHandle!=0) {
	    (*env)->SetLongField(env, obj, fpixmapHandle, (jlong)((PointerHolder)pix));
    }

    if(ret==JNI_TRUE && fownwind) {
	    (*env)->SetBooleanField(env, obj, fownwind, jownwind);
    }

    return ret;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljSwapNative( JNIEnv *env, jobject obj,
				      jlong disp,
				      jlong thisWin,
				      jlong glContext,
				      jboolean doubleBuffer
			      )
{
	jboolean ret = JNI_TRUE;

        (void)glContext;
        (void)disp;

	if( !thisWin )
	{
            fprintf(stderr,"Swap does not got the window ...\n");
	    return JNI_FALSE;
	}

	if( doubleBuffer==JNI_TRUE )
	{
		if( SwapBuffers( (HDC)((PointerHolder)thisWin) ) == FALSE )
		{
			/* Error in Win2000 implementation :o)
			 * printf( "Error in swap buffer!\n" );
			 */
	                return JNI_FALSE;
		}
	} else {
		disp__glFlush();
	}
	return ret;
}

JNIEXPORT void JNICALL
Java_gl4java_GLContext_gljResizeNative( JNIEnv *env, jobject obj,
	                                jboolean isOwnWindow,
				        jlong disp, jlong thisWin,
				        jint width, jint height)
{
	(void)isOwnWindow;
	(void)disp;
	(void)thisWin;
	(void)width;
	(void)height;

	return;
}