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
|
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions 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.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package jogamp.opengl.util.glsl.fixedfunc;
import java.nio.Buffer;
import java.nio.IntBuffer;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2ES2;
import com.jogamp.opengl.GLArrayData;
import com.jogamp.opengl.GLException;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.fixedfunc.GLLightingFunc;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
import com.jogamp.opengl.fixedfunc.GLPointerFunc;
import com.jogamp.common.nio.Buffers;
import com.jogamp.common.util.ValueConv;
import com.jogamp.opengl.util.GLArrayDataWrapper;
import com.jogamp.opengl.util.GLBuffers;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.glsl.fixedfunc.ShaderSelectionMode;
public class FixedFuncHook implements GLLightingFunc, GLMatrixFunc, GLPointerFunc {
public static final int MAX_TEXTURE_UNITS = 8;
protected final GLProfile gl2es1GLProfile;
protected FixedFuncPipeline fixedFunction;
protected PMVMatrix pmvMatrix;
protected boolean ownsPMVMatrix;
protected GL2ES2 gl;
/**
* @param gl
* @param mode TODO
* @param pmvMatrix optional pass through PMVMatrix for the {@link FixedFuncHook} and {@link FixedFuncPipeline}
*/
public FixedFuncHook (final GL2ES2 gl, final ShaderSelectionMode mode, final PMVMatrix pmvMatrix) {
this.gl2es1GLProfile = GLProfile.createCustomGLProfile(GLProfile.GL2ES1, gl.getGLProfile().getImpl());
this.gl = gl;
if(null != pmvMatrix) {
this.ownsPMVMatrix = false;
this.pmvMatrix = pmvMatrix;
} else {
this.ownsPMVMatrix = true;
this.pmvMatrix = new PMVMatrix();
}
fixedFunction = new FixedFuncPipeline(this.gl, mode, this.pmvMatrix);
}
/**
* @param gl
* @param mode TODO
* @param pmvMatrix optional pass through PMVMatrix for the {@link FixedFuncHook} and {@link FixedFuncPipeline}
*/
public FixedFuncHook(final GL2ES2 gl, final ShaderSelectionMode mode, final PMVMatrix pmvMatrix,
final Class<?> shaderRootClass, final String shaderSrcRoot, final String shaderBinRoot,
final String vertexColorFile, final String vertexColorLightFile,
final String fragmentColorFile, final String fragmentColorTextureFile) {
this.gl2es1GLProfile = GLProfile.createCustomGLProfile(GLProfile.GL2ES1, gl.getGLProfile().getImpl());
this.gl = gl;
if(null != pmvMatrix) {
this.ownsPMVMatrix = false;
this.pmvMatrix = pmvMatrix;
} else {
this.ownsPMVMatrix = true;
this.pmvMatrix = new PMVMatrix();
}
fixedFunction = new FixedFuncPipeline(this.gl, mode, this.pmvMatrix, shaderRootClass, shaderSrcRoot,
shaderBinRoot, vertexColorFile, vertexColorLightFile, fragmentColorFile, fragmentColorTextureFile);
}
public boolean verbose() { return fixedFunction.verbose(); }
public void setVerbose(final boolean v) { fixedFunction.setVerbose(v); }
public void destroy() {
fixedFunction.destroy(gl);
fixedFunction = null;
pmvMatrix=null;
gl=null;
}
public PMVMatrix getMatrix() { return pmvMatrix; }
//
// FixedFuncHookIf - hooks
//
public final boolean isGL4core() {
return false;
}
public final boolean isGL3core() {
return false;
}
public final boolean isGLcore() {
return false;
}
public final boolean isGLES2Compatible() {
return false;
}
public final boolean isGLES3Compatible() {
return false;
}
public final GLProfile getGLProfile() {
return gl2es1GLProfile;
}
public void glDrawArrays(final int mode, final int first, final int count) {
fixedFunction.glDrawArrays(gl, mode, first, count);
}
public void glDrawElements(final int mode, final int count, final int type, final java.nio.Buffer indices) {
fixedFunction.glDrawElements(gl, mode, count, type, indices);
}
public void glDrawElements(final int mode, final int count, final int type, final long indices_buffer_offset) {
fixedFunction.glDrawElements(gl, mode, count, type, indices_buffer_offset);
}
public void glActiveTexture(final int texture) {
fixedFunction.glActiveTexture(texture);
gl.glActiveTexture(texture);
}
public void glEnable(final int cap) {
if(fixedFunction.glEnable(cap, true)) {
gl.glEnable(cap);
}
}
public void glDisable(final int cap) {
if(fixedFunction.glEnable(cap, false)) {
gl.glDisable(cap);
}
}
@Override
public void glGetFloatv(final int pname, final java.nio.FloatBuffer params) {
if(PMVMatrix.isMatrixGetName(pname)) {
pmvMatrix.glGetFloatv(pname, params);
return;
}
gl.glGetFloatv(pname, params);
}
@Override
public void glGetFloatv(final int pname, final float[] params, final int params_offset) {
if(PMVMatrix.isMatrixGetName(pname)) {
pmvMatrix.glGetFloatv(pname, params, params_offset);
return;
}
gl.glGetFloatv(pname, params, params_offset);
}
@Override
public void glGetIntegerv(final int pname, final IntBuffer params) {
if(PMVMatrix.isMatrixGetName(pname)) {
pmvMatrix.glGetIntegerv(pname, params);
return;
}
gl.glGetIntegerv(pname, params);
}
@Override
public void glGetIntegerv(final int pname, final int[] params, final int params_offset) {
if(PMVMatrix.isMatrixGetName(pname)) {
pmvMatrix.glGetIntegerv(pname, params, params_offset);
return;
}
gl.glGetIntegerv(pname, params, params_offset);
}
public void glTexEnvi(final int target, final int pname, final int value) {
fixedFunction.glTexEnvi(target, pname, value);
}
public void glGetTexEnviv(final int target, final int pname, final IntBuffer params) {
fixedFunction.glGetTexEnviv(target, pname, params);
}
public void glGetTexEnviv(final int target, final int pname, final int[] params, final int params_offset) {
fixedFunction.glGetTexEnviv(target, pname, params, params_offset);
}
public void glBindTexture(final int target, final int texture) {
fixedFunction.glBindTexture(target, texture);
gl.glBindTexture(target, texture);
}
public void glTexImage2D(final int target, final int level, int internalformat, final int width, final int height, final int border,
final int format, final int type, final Buffer pixels) {
// align internalformat w/ format, an ES2 requirement
switch(internalformat) {
case 3: internalformat= ( GL.GL_RGBA == format ) ? GL.GL_RGBA : GL.GL_RGB; break;
case 4: internalformat= ( GL.GL_RGB == format ) ? GL.GL_RGB : GL.GL_RGBA; break;
}
fixedFunction.glTexImage2D(target, /* level, */ internalformat, /*width, height, border, */ format /*, type, pixels*/);
gl.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
}
public void glTexImage2D(final int target, final int level, int internalformat, final int width, final int height, final int border,
final int format, final int type, final long pixels_buffer_offset) {
// align internalformat w/ format, an ES2 requirement
switch(internalformat) {
case 3: internalformat= ( GL.GL_RGBA == format ) ? GL.GL_RGBA : GL.GL_RGB; break;
case 4: internalformat= ( GL.GL_RGB == format ) ? GL.GL_RGB : GL.GL_RGBA; break;
}
fixedFunction.glTexImage2D(target, /* level, */ internalformat, /*width, height, border, */ format /*, type, pixels*/);
gl.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels_buffer_offset);
}
public void glPointSize(final float size) {
fixedFunction.glPointSize(size);
}
public void glPointParameterf(final int pname, final float param) {
fixedFunction.glPointParameterf(pname, param);
}
public void glPointParameterfv(final int pname, final float[] params, final int params_offset) {
fixedFunction.glPointParameterfv(pname, params, params_offset);
}
public void glPointParameterfv(final int pname, final java.nio.FloatBuffer params) {
fixedFunction.glPointParameterfv(pname, params);
}
//
// MatrixIf
//
public int glGetMatrixMode() {
return pmvMatrix.glGetMatrixMode();
}
@Override
public void glMatrixMode(final int mode) {
pmvMatrix.glMatrixMode(mode);
}
@Override
public void glLoadMatrixf(final java.nio.FloatBuffer m) {
pmvMatrix.glLoadMatrixf(m);
}
@Override
public void glLoadMatrixf(final float[] m, final int m_offset) {
glLoadMatrixf(Buffers.newDirectFloatBuffer(m, m_offset));
}
@Override
public void glPopMatrix() {
pmvMatrix.glPopMatrix();
}
@Override
public void glPushMatrix() {
pmvMatrix.glPushMatrix();
}
@Override
public void glLoadIdentity() {
pmvMatrix.glLoadIdentity();
}
@Override
public void glMultMatrixf(final java.nio.FloatBuffer m) {
pmvMatrix.glMultMatrixf(m);
}
@Override
public void glMultMatrixf(final float[] m, final int m_offset) {
glMultMatrixf(Buffers.newDirectFloatBuffer(m, m_offset));
}
@Override
public void glTranslatef(final float x, final float y, final float z) {
pmvMatrix.glTranslatef(x, y, z);
}
@Override
public void glRotatef(final float angdeg, final float x, final float y, final float z) {
pmvMatrix.glRotatef(angdeg, x, y, z);
}
@Override
public void glScalef(final float x, final float y, final float z) {
pmvMatrix.glScalef(x, y, z);
}
public void glOrtho(final double left, final double right, final double bottom, final double top, final double near_val, final double far_val) {
glOrthof((float) left, (float) right, (float) bottom, (float) top, (float) near_val, (float) far_val);
}
@Override
public void glOrthof(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar) {
pmvMatrix.glOrthof(left, right, bottom, top, zNear, zFar);
}
public void glFrustum(final double left, final double right, final double bottom, final double top, final double zNear, final double zFar) {
glFrustumf((float) left, (float) right, (float) bottom, (float) top, (float) zNear, (float) zFar);
}
@Override
public void glFrustumf(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar) {
pmvMatrix.glFrustumf(left, right, bottom, top, zNear, zFar);
}
//
// LightingIf
//
@Override
public void glColor4f(final float red, final float green, final float blue, final float alpha) {
fixedFunction.glColor4f(gl, red, green, blue, alpha);
}
public void glColor4ub(final byte red, final byte green, final byte blue, final byte alpha) {
glColor4f(ValueConv.byte_to_float(red, false),
ValueConv.byte_to_float(green, false),
ValueConv.byte_to_float(blue, false),
ValueConv.byte_to_float(alpha, false) );
}
@Override
public void glLightfv(final int light, final int pname, final java.nio.FloatBuffer params) {
fixedFunction.glLightfv(gl, light, pname, params);
}
@Override
public void glLightfv(final int light, final int pname, final float[] params, final int params_offset) {
glLightfv(light, pname, Buffers.newDirectFloatBuffer(params, params_offset));
}
@Override
public void glMaterialfv(final int face, final int pname, final java.nio.FloatBuffer params) {
fixedFunction.glMaterialfv(gl, face, pname, params);
}
@Override
public void glMaterialfv(final int face, final int pname, final float[] params, final int params_offset) {
glMaterialfv(face, pname, Buffers.newDirectFloatBuffer(params, params_offset));
}
@Override
public void glMaterialf(final int face, final int pname, final float param) {
glMaterialfv(face, pname, Buffers.newDirectFloatBuffer(new float[] { param }));
}
//
// Misc Simple States
//
@Override
public void glShadeModel(final int mode) {
fixedFunction.glShadeModel(gl, mode);
}
public void glAlphaFunc(final int func, final float ref) {
fixedFunction.glAlphaFunc(func, ref);
}
/** ES2 supports CullFace implicit
public void glCullFace(int faceName) {
fixedFunction.glCullFace(faceName);
gl.glCullFace(faceName);
} */
//
// PointerIf
//
public void glClientActiveTexture(final int textureUnit) {
fixedFunction.glClientActiveTexture(textureUnit);
}
@Override
public void glEnableClientState(final int glArrayIndex) {
fixedFunction.glEnableClientState(gl, glArrayIndex);
}
@Override
public void glDisableClientState(final int glArrayIndex) {
fixedFunction.glDisableClientState(gl, glArrayIndex);
}
@Override
public void glVertexPointer(final GLArrayData array) {
if(array.isVBO()) {
if(!gl.isVBOArrayBound()) {
throw new GLException("VBO array is not enabled: "+array);
}
} else {
if(gl.isVBOArrayBound()) {
throw new GLException("VBO array is not disabled: "+array);
}
Buffers.rangeCheck(array.getBuffer(), 1);
if (!Buffers.isDirect(array.getBuffer())) {
throw new GLException("Argument \"pointer\" was not a direct buffer"); }
}
fixedFunction.glVertexPointer(gl, array);
}
@Override
public void glVertexPointer(final int size, final int type, final int stride, final java.nio.Buffer pointer) {
glVertexPointer(GLArrayDataWrapper.createFixed(GL_VERTEX_ARRAY, size, type, GLBuffers.isGLTypeFixedPoint(type), stride,
pointer, 0, 0, 0, GL.GL_ARRAY_BUFFER));
}
@Override
public void glVertexPointer(final int size, final int type, final int stride, final long pointer_buffer_offset) {
final int vboName = gl.getBoundBuffer(GL.GL_ARRAY_BUFFER);
if(vboName==0) {
throw new GLException("no GL_ARRAY_BUFFER VBO bound");
}
glVertexPointer(GLArrayDataWrapper.createFixed(GL_VERTEX_ARRAY, size, type, GLBuffers.isGLTypeFixedPoint(type), stride,
null, vboName, pointer_buffer_offset, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER));
}
@Override
public void glColorPointer(final GLArrayData array) {
if(array.isVBO()) {
if(!gl.isVBOArrayBound()) {
throw new GLException("VBO array is not enabled: "+array);
}
} else {
if(gl.isVBOArrayBound()) {
throw new GLException("VBO array is not disabled: "+array);
}
Buffers.rangeCheck(array.getBuffer(), 1);
if (!Buffers.isDirect(array.getBuffer())) {
throw new GLException("Argument \"pointer\" was not a direct buffer"); }
}
fixedFunction.glColorPointer(gl, array);
}
@Override
public void glColorPointer(final int size, final int type, final int stride, final java.nio.Buffer pointer) {
glColorPointer(GLArrayDataWrapper.createFixed(GL_COLOR_ARRAY, size, type, GLBuffers.isGLTypeFixedPoint(type), stride,
pointer, 0, 0, 0, GL.GL_ARRAY_BUFFER));
}
@Override
public void glColorPointer(final int size, final int type, final int stride, final long pointer_buffer_offset) {
final int vboName = gl.getBoundBuffer(GL.GL_ARRAY_BUFFER);
if(vboName==0) {
throw new GLException("no GL_ARRAY_BUFFER VBO bound");
}
glColorPointer(GLArrayDataWrapper.createFixed(GL_COLOR_ARRAY, size, type, GLBuffers.isGLTypeFixedPoint(type), stride,
null, vboName, pointer_buffer_offset, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER));
}
@Override
public void glNormalPointer(final GLArrayData array) {
if(array.getCompsPerElem()!=3) {
throw new GLException("Only 3 components per normal allowed");
}
if(array.isVBO()) {
if(!gl.isVBOArrayBound()) {
throw new GLException("VBO array is not enabled: "+array);
}
} else {
if(gl.isVBOArrayBound()) {
throw new GLException("VBO array is not disabled: "+array);
}
Buffers.rangeCheck(array.getBuffer(), 1);
if (!Buffers.isDirect(array.getBuffer())) {
throw new GLException("Argument \"pointer\" was not a direct buffer"); }
}
fixedFunction.glNormalPointer(gl, array);
}
@Override
public void glNormalPointer(final int type, final int stride, final java.nio.Buffer pointer) {
glNormalPointer(GLArrayDataWrapper.createFixed(GL_NORMAL_ARRAY, 3, type, GLBuffers.isGLTypeFixedPoint(type), stride,
pointer, 0, 0, 0, GL.GL_ARRAY_BUFFER));
}
@Override
public void glNormalPointer(final int type, final int stride, final long pointer_buffer_offset) {
final int vboName = gl.getBoundBuffer(GL.GL_ARRAY_BUFFER);
if(vboName==0) {
throw new GLException("no GL_ARRAY_BUFFER VBO bound");
}
glNormalPointer(GLArrayDataWrapper.createFixed(GL_NORMAL_ARRAY, 3, type, GLBuffers.isGLTypeFixedPoint(type), stride,
null, vboName, pointer_buffer_offset, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER));
}
@Override
public void glTexCoordPointer(final GLArrayData array) {
if(array.isVBO()) {
if(!gl.isVBOArrayBound()) {
throw new GLException("VBO array is not enabled: "+array);
}
} else {
if(gl.isVBOArrayBound()) {
throw new GLException("VBO array is not disabled: "+array);
}
Buffers.rangeCheck(array.getBuffer(), 1);
if (!Buffers.isDirect(array.getBuffer())) {
throw new GLException("Argument \"pointer\" was not a direct buffer"); }
}
fixedFunction.glTexCoordPointer(gl, array);
}
@Override
public void glTexCoordPointer(final int size, final int type, final int stride, final java.nio.Buffer pointer) {
glTexCoordPointer(
GLArrayDataWrapper.createFixed(GL_TEXTURE_COORD_ARRAY, size, type, GLBuffers.isGLTypeFixedPoint(type), stride,
pointer, 0, 0, 0, GL.GL_ARRAY_BUFFER));
}
@Override
public void glTexCoordPointer(final int size, final int type, final int stride, final long pointer_buffer_offset) {
final int vboName = gl.getBoundBuffer(GL.GL_ARRAY_BUFFER);
if(vboName==0) {
throw new GLException("no GL_ARRAY_BUFFER VBO bound");
}
glTexCoordPointer(
GLArrayDataWrapper.createFixed(GL_TEXTURE_COORD_ARRAY, size, type, GLBuffers.isGLTypeFixedPoint(type), stride,
null, vboName, pointer_buffer_offset, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER) );
}
@Override
public final String toString() {
final StringBuilder buf = new StringBuilder();
buf.append(getClass().getName()+" (");
if(null!=pmvMatrix) {
buf.append(", matrixDirty: "+ (0 != pmvMatrix.getModifiedBits(false)));
}
buf.append("\n\t, FixedFunction: "+fixedFunction);
buf.append(gl);
buf.append(" )");
return buf.toString();
}
}
|