aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/ogl/RasterRetained.c
blob: 09d4e6db5c6813c19fd88f28f6b27cb3d6b4a941 (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
/*
 * $RCSfile$
 *
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

#include <stdio.h>
#include <jni.h>

#include "gldefs.h"

#ifdef DEBUG
/* Uncomment the following for VERBOSE debug messages */
/* #define VERBOSE */
#endif /* DEBUG */


extern void throwAssert(JNIEnv *env, char *str);


JNIEXPORT
void JNICALL Java_javax_media_j3d_RasterRetained_execute(JNIEnv *env, 
		jobject obj, jlong ctxInfo, jobject geo, 
                jboolean updateAlpha, jfloat alpha,
                jint type, jint w_raster, jint h_raster,
                jint x_offset, jint y_offset, jfloat x, jfloat y, jfloat z, jbyteArray imageYdown)


{
    jclass geo_class;
    JNIEnv table;

    jfieldID w_field, h_field;
    int width, height;

    GraphicsContextPropertiesInfo *ctxProperties = (GraphicsContextPropertiesInfo *)ctxInfo;
    jlong ctx = ctxProperties->context;

    table = *env;

    geo_class =  (jclass) (*(table->GetObjectClass))(env, geo);

#ifdef VERBOSE
    fprintf(stderr, 
       "x %g ,y %g ,z %g ,w_raster %d,h_raster %d, x_offset %d, y_offset %d\n",
       x,y,z,w_raster,h_raster,x_offset,y_offset);
#endif

    if ((type == javax_media_j3d_Raster_RASTER_COLOR) ||
	(type == javax_media_j3d_Raster_RASTER_COLOR_DEPTH)) {
	jobject image;
	jclass image_class;
	jfieldID image_field, format_field; 
	jbyte *byteData;
	int format;
	int glformat;

	image_field = (jfieldID)(*(table->GetFieldID))(env, geo_class,
		    "image","Ljavax/media/j3d/ImageComponent2DRetained;");
	image = (jobject) (*(table->GetObjectField))(env, geo, image_field);

	if (image == NULL) {
	  return;
	}
	image_class = (jclass) (*(table->GetObjectClass))(env, image);

	format_field = (jfieldID) (*(table->GetFieldID))(env, image_class,
		    "storedYdownFormat", "I");
	format = (jint)(*(table->GetIntField))(env, image, format_field);
	w_field = (jfieldID) (*(table->GetFieldID))(env, image_class,
		    "width", "I");
	width = (jint)(*(table->GetIntField))(env, image, w_field);
	h_field = (jfieldID)(*(table->GetFieldID))(env, image_class,
		    "height", "I");
	height = (jint)(*(table->GetIntField))(env, image, h_field);


	/* 
	 * raster position is upper left corner, default for Java3D 
	 * ImageComponent currently has the data reverse in Y
	 */
	glPixelZoom(1.0, -1.0);
	glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
	if (x_offset >= 0) {
	    glPixelStorei(GL_UNPACK_SKIP_PIXELS, x_offset);
	    if (x_offset + w_raster > width) {
		w_raster = width - x_offset;
	    }
	} else {
	    w_raster += x_offset;
	    if (w_raster > width) {
		w_raster  = width;
	    }
	}
	if (y_offset >= 0) {
	    glPixelStorei(GL_UNPACK_SKIP_ROWS, y_offset);
	    if (y_offset + h_raster > height) {
		h_raster = height - y_offset;
	    }
	} else {
	    h_raster += y_offset;
	    if (h_raster > height) {
		h_raster = height;
	    }
	}



	glRasterPos3f(x, y, z);

        byteData = (jbyte *)(*(table->GetPrimitiveArrayCritical))(env,
                    imageYdown, NULL);
		
/*
	{
        int i, j, *intData;

	fprintf(stderr, "format = %d, w_raster = %d, h_raster = %d\n\n", format, w_raster, h_raster);
	intData = (int*)byteData;
	for (i = 0; i < w_raster; i++) {
	    for (j = 0; j < h_raster; j++, intData++) {
		fprintf(stderr, " 0x%x", *intData);
	    }
	    fprintf(stderr, "\n");
	}
	}
	*/
        switch (format) {
        case FORMAT_BYTE_RGBA:
            glformat = GL_RGBA;
            break;
        case FORMAT_BYTE_RGB:
            glformat = GL_RGB;
            break;

        case FORMAT_BYTE_ABGR:         
	    if (ctxProperties->abgr_ext) { /* If its zero, should never come here! */
		glformat = GL_ABGR_EXT;
	    }
	    break;
        case FORMAT_BYTE_BGR:         
	    if (ctxProperties->bgr_ext) { /* If its zero, should never come here! */
		glformat = ctxProperties->bgr_ext_enum;
	    }
	    break;
        case FORMAT_BYTE_LA:
            glformat = GL_LUMINANCE_ALPHA;
            break;

        case FORMAT_BYTE_GRAY:
        case FORMAT_USHORT_GRAY:
	default:
	    throwAssert(env, "illegal format");
            break;
        }
	glDrawPixels(w_raster, h_raster, glformat, GL_UNSIGNED_BYTE, 
		     byteData);	

	(*(table->ReleasePrimitiveArrayCritical))(env, 
			imageYdown, byteData, 0);
    }

    if ((type == javax_media_j3d_Raster_RASTER_DEPTH) ||
	(type == javax_media_j3d_Raster_RASTER_COLOR_DEPTH)) {
	GLint draw_buf;
	jobject depth;
	jclass depth_class;
	jfieldID depth_field, depth_type_field;
	int depth_type;

	depth_field = (jfieldID)(*(table->GetFieldID))(env, geo_class,
		"depthComponent","Ljavax/media/j3d/DepthComponentRetained;");

	depth = (jobject) (*(table->GetObjectField))(env, geo, depth_field);
	if (depth == NULL) {
	  return;
	}
	depth_class = (jclass) (*(table->GetObjectClass))(env, depth);

	depth_type_field = (jfieldID) (*(table->GetFieldID))(env,
	depth_class, "type", "I");
	depth_type = (jint)(*(table->GetIntField))(env, depth,
	depth_type_field);

	w_field = (jfieldID) (*(table->GetFieldID))(env, depth_class,
		"width", "I");
	width = (jint)(*(table->GetIntField))(env, depth, w_field);
	h_field = (jfieldID)(*(table->GetFieldID))(env, depth_class,
		"height", "I");
	height = (jint)(*(table->GetIntField))(env, depth, h_field);


	glGetIntegerv(GL_DRAW_BUFFER, &draw_buf);
	/* disable draw buffer */
	glDrawBuffer(GL_NONE);
	/* glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); */

	/* 
	 * raster position is upper left corner, default for Java3D 
	 * ImageComponent currently has the data reverse in Y
	 */
	glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
	if (x_offset >= 0) {
	    glPixelStorei(GL_UNPACK_SKIP_PIXELS, x_offset);
	    if (x_offset + w_raster > width) {
		w_raster = width - x_offset;
	    }	    
	} else {
	    w_raster += x_offset;
	    if (w_raster > width) {
		w_raster  = width;
	    }	    
	}
	if (y_offset >= 0) {
	    glPixelStorei(GL_UNPACK_SKIP_ROWS, y_offset);
	    if (y_offset + h_raster > height) {
		h_raster = height - y_offset;
	    }
	} else {
	    h_raster += y_offset;
	    if (h_raster > height) {
		h_raster = height;
	    }
	}


	if (depth_type ==  javax_media_j3d_DepthComponentRetained_DEPTH_COMPONENT_TYPE_INT) { 
	    jfieldID intData_field;
	    jintArray intData_array;
	    jint *intData;

	    intData_field = (jfieldID)(*(table->GetFieldID))(env,
	    depth_class, "depthData","[I");

	    intData_array = (jintArray)(*(table->GetObjectField))(env, depth,
			intData_field);
	    intData = (jint *)(*(table->GetPrimitiveArrayCritical))(env,
	    intData_array, NULL);
	    glDrawPixels(w_raster, h_raster, GL_DEPTH_COMPONENT,
			GL_UNSIGNED_INT, intData);
	    (*(table->ReleasePrimitiveArrayCritical))(env, intData_array,
			intData, 0);
	} else { /* javax_media_j3d_DepthComponentRetained_DEPTH_COMPONENT_TYPE_FLOAT */
	    jfieldID floatData_field;
	    jfloatArray floatData_array;
	    jfloat *floatData;

	    floatData_field = (jfieldID)(*(table->GetFieldID))(env,
		    depth_class, "depthData","[F");
	    floatData_array = (jfloatArray)(*(table->GetObjectField))(env, depth,
		    floatData_field);
	    floatData = (jfloat *)(*(table->GetPrimitiveArrayCritical))(env,
		    floatData_array, NULL);
	    glDrawPixels(w_raster, h_raster, GL_DEPTH_COMPONENT,
		    GL_FLOAT, floatData);
	    (*(table->ReleasePrimitiveArrayCritical))(env, floatData_array,
		    floatData, 0);
	}

	/* re-enable draw buffer */
	glDrawBuffer(draw_buf);

    }
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
    glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);

}


#if 0

JNIEXPORT
void JNICALL Java_javax_media_j3d_RasterRetained_executeTiled(JNIEnv *env, 
		jobject obj, jlong ctxInfo, jobject geo, 
                jint format, jint w_raster, jint h_raster,
                jint x_offset, jint y_offset, jint deltaw, jint deltah, jfloat x, jfloat y, jfloat z, jbyteArray tile)


{
    jclass geo_class;
    JNIEnv table;
    jint j;
    int alphaChanged = 0;  	/* used so we can get alpha data from */
				/* JNI before using it so we can use  */
				/* GetPrimitiveArrayCritical */
    jobject image;
    jclass image_class;
    jfieldID byteData_field, image_field, format_field; 
    jbyteArray byteData_array;
    jbyte *byteData;
    int glformat;
    float rasterPos[3];
    GraphicsContextPropertiesInfo *ctxProperties = (GraphicsContextPropertiesInfo *)ctxInfo;
    jlong ctx = ctxProperties->context;

    table = *env;


#ifdef VERBOSE
    fprintf(stderr, 
       "x %g ,y %g ,z %g ,w_raster %d,h_raster %d, x_offset %d, y_offset %d\n",
       x,y,z,w_raster,h_raster,x_offset,y_offset);
#endif


	/* 
	 * raster position is upper left corner, default for Java3D 
	 * ImageComponent currently has the data reverse in Y
	 */
	glPixelZoom(1.0, -1.0);
	/*	glPixelStorei(GL_UNPACK_ROW_LENGTH, width);*/
	if (x_offset >= 0) {
	    glPixelStorei(GL_UNPACK_SKIP_PIXELS, x_offset);
	    if (x_offset + w_raster > width) {
		w_raster = width - x_offset;
	    }	    
	} else {
	    w_raster += x_offset;
	    if (w_raster > width) {
		w_raster  = width;
	    }	    
	}
	if (y_offset >= 0) {
	    glPixelStorei(GL_UNPACK_SKIP_ROWS, y_offset);
	    if (y_offset + h_raster > height) {
		h_raster = height - y_offset;
	    }	    
	} else {
	    h_raster += y_offset;
	    if (h_raster > height) {
		h_raster = height;
	    }	    
	}
	
	if (deltaw == 0 && deltah == 0) {
	    glRasterPos3f(x, y, z);
	}
	else {
	    glGetFloatv(GL_CURRENT_RASTER_POSITION,rasterPos);
	    rasterPos[0] += (float)deltaw;
	    rasterPos[1] += (float)deltah;
	    glRasterPos3f(rasterPos[0], rasterPos[1], rasterPos[2]);
	}
		
	

        byteData = (jbyte *)(*(table->GetPrimitiveArrayCritical))(env,
                    tile, NULL);
        switch (format) {
        case FORMAT_BYTE_RGBA:
            glformat = GL_RGBA;
            break;
        case FORMAT_BYTE_RGB:
            glformat = GL_RGB;
            break;

        case FORMAT_BYTE_ABGR:         
	    if (ctxProperties->abgr_ext) { /* If its zero, should never come here! */
		glformat = GL_ABGR_EXT;
	    }
	    break;


        case FORMAT_BYTE_BGR:         
	    if (ctxProperties->bgr_ext) { /* If its zero, should never come here! */
		glformat = ctxProperties->bgr_ext_enum;
	    }
	    break;

        case FORMAT_BYTE_LA:
            glformat = GL_LUMINANCE_ALPHA;
            break;
        case FORMAT_BYTE_GRAY:
        case FORMAT_USHORT_GRAY:	    
	default:
	    throwAssert(env, "illegal format");
            break;
        }
	fprintf(stderr, "w_raster = %d, h_raster = %d, glformat = %d\n",w_raster, h_raster, glformat);
	glDrawPixels(w_raster, h_raster, glformat, GL_UNSIGNED_BYTE, 
		     byteData);	

	(*(table->ReleasePrimitiveArrayCritical))(env, 
			tile, byteData, 0);

	/* 	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);*/
    glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
    glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);

}
#endif