aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/javax/media/opengl/GLArrayDataClient.java
blob: a8da6c0a1ff134b197db9ea9102b779d8c7d3e37 (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

package javax.media.opengl;

import javax.media.opengl.util.*;
import com.sun.opengl.impl.GLReflection;

import java.nio.*;

public class GLArrayDataClient implements GLArrayData {

  /**
   * @arg index The GL array index
   * @arg name  The optional custom name for the GL array index, maybe null.
   *            If null, the default name mapping will be used, see 'getPredefinedArrayIndexName(int)'.
   *            This name might be used as the shader attribute name.
   * @arg comps The array component number
   * @arg dataType The array index GL data type
   * @arg normalized Wheather the data shall be normalized
   *
   * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int)
   */
  public static GLArrayDataClient createFixed(int index, String name, int comps, int dataType, boolean normalized, 
                                              int initialSize)
    throws GLException
  {
      GLProfile.isValidateArrayDataType(index, comps, dataType, false, true);
      GLArrayDataClient adc = new GLArrayDataClient();
      adc.init(name, index, comps, dataType, normalized, 0, null, initialSize, false);
      return adc;
  }

  public static GLArrayDataClient createFixed(int index, String name, int comps, int dataType, boolean normalized, 
                                              int stride, Buffer buffer)
    throws GLException
  {
      GLProfile.isValidateArrayDataType(index, comps, dataType, false, true);
      GLArrayDataClient adc = new GLArrayDataClient();
      adc.init(name, index, comps, dataType, normalized, stride, buffer, comps*comps, false);
      return adc;
  }

  public static GLArrayDataClient createGLSL(int index, String name, int comps, int dataType, boolean normalized, 
                                             int initialSize)
    throws GLException
  {
      GLProfile.isValidateArrayDataType(index, comps, dataType, true, true);
      GLArrayDataClient adc = new GLArrayDataClient();
      adc.init(name, index, comps, dataType, normalized, 0, null, initialSize, true);
      return adc;
  }

  public static GLArrayDataClient createGLSL(int index, String name, int comps, int dataType, boolean normalized, 
                                             int stride, Buffer buffer)
    throws GLException
  {
      GLProfile.isValidateArrayDataType(index, comps, dataType, true, true);
      GLArrayDataClient adc = new GLArrayDataClient();
      adc.init(name, index, comps, dataType, normalized, stride, buffer, comps*comps, true);
      return adc;
  }

  // 
  // Data read access
  //

  public final boolean isVertexAttribute() { return isVertexAttribute; }

  public final int getIndex() { return index; }

  public final int getLocation() { return location; }

  public final void setLocation(int v) { location = v; }

  public final String getName() { return name; }

  public long getOffset() { return -1; }

  public final Buffer getBuffer() { return buffer; }

  public boolean isVBO() { return false; }

  public int getVBOName() { return -1; }

  public int getBufferUsage() { return -1; }

  public final int getComponentNumber() { return components; }

  public final int getComponentType() { return dataType; }

  public final int getComponentSize() {
    if(clazz==ByteBuffer.class) {
        return BufferUtil.SIZEOF_BYTE;
    }
    if(clazz==ShortBuffer.class) {
        return BufferUtil.SIZEOF_SHORT;
    }
    if(clazz==IntBuffer.class) {
        return BufferUtil.SIZEOF_INT;
    }
    if(clazz==FloatBuffer.class) {
        return BufferUtil.SIZEOF_FLOAT;
    }
    throw new GLException("Given Buffer Class not supported: "+clazz+":\n\t"+this);
  }

  public final int getElementNumber() {
    if(null==buffer) return 0;
    return ( sealed ) ? ( buffer.limit() / components ) : ( buffer.position() / components ) ;
  }

  public final boolean getNormalized() { return normalized; }

  public final int getStride() { return stride; }

  public final boolean sealed() { return sealed; }

  public final Class getBufferClass() { return clazz; }

  //
  // Data and GL state modification ..
  //

  public void setName(String newName) {
    location = -1;
    name = newName;
  }

  public void destroy(GL gl) {
    reset(gl);
  }

  public void reset(GL gl) {
    enableBuffer(gl, false);
    reset();
  }

  public void seal(GL gl, boolean seal)
  {
    seal(seal);
    if(sealedGL==seal) return;
    sealedGL = seal;
    if(seal) {
        init_vbo(gl);

        enableBuffer(gl, true);
    } else {
        enableBuffer(gl, false);
    }
  }

  public void enableBuffer(GL gl, boolean enable) {
    if(enableBufferAlways && enable) {
        bufferEnabled = false;
    }
    if( bufferEnabled != enable && components>0 ) {
        if(enable) {
            checkSeal(true);
            if(null!=buffer) {
                buffer.rewind();
            }
        }
        enableBufferGLImpl(gl, enable);
        bufferEnabled = enable;
    }
  }

  public void setEnableAlways(boolean always) {
    enableBufferAlways = always;
  }

  //
  // Data modification ..
  //

  public void reset() {
    if(buffer!=null) {
        buffer.clear();
    }
    this.sealed=false;
    this.bufferEnabled=false;
    this.bufferWritten=false;
  }

  public void seal(boolean seal)
  {
    if(sealed==seal) return;
    sealed = seal;
    if(seal) {
        bufferWritten=false;
        if (null!=buffer) {
            buffer.flip();
        }
    } else {
        if (null!=buffer) {
            buffer.position(buffer.limit());
            buffer.limit(buffer.capacity());
        }
    }
  }


  public void rewind() {
    if(buffer!=null) {
        buffer.rewind();
    }
  }

  public void padding(int done) {
    if ( buffer==null || sealed ) return;
    while(done<strideL) {
        if(clazz==ByteBuffer.class) {
            ((ByteBuffer)buffer).put((byte)0);
        } else if(clazz==ShortBuffer.class) {
            ((ShortBuffer)buffer).put((short)0);
        } else if(clazz==IntBuffer.class) {
            ((IntBuffer)buffer).put(0);
        } else if(clazz==FloatBuffer.class) {
            ((FloatBuffer)buffer).put(0f);
        } else {
            throw new GLException("Given Buffer Class not supported: "+clazz+" :\n\t"+this);
        }
        done++;
    }
  }

  /**
   * Generic buffer relative put method.
   *
   * This class buffer Class must match the arguments buffer class.
   * The arguments remaining elements must be a multiple of this arrays element stride.
   */
  public void put(Buffer v) {
    if ( buffer==null || sealed ) return;
    if(0!=(v.remaining() % strideL)) {
        throw new GLException("Buffer length ("+v.remaining()+") is not a multiple of component-stride:\n\t"+this);
    }
    Class vClazz = v.getClass();
    if(!GLReflection.instanceOf(vClazz, clazz.getName())) {
        throw new GLException("This array's buffer class "+clazz+" doesn't match the argument's Class: "+vClazz+" :\n\t"+this);
    }
    growBufferIfNecessary(v.remaining());
    if(clazz==ByteBuffer.class) {
        ((ByteBuffer)buffer).put((ByteBuffer)v);
    } else if(clazz==ShortBuffer.class) {
        ((ShortBuffer)buffer).put((ShortBuffer)v);
    } else if(clazz==IntBuffer.class) {
        ((IntBuffer)buffer).put((IntBuffer)v);
    } else if(clazz==FloatBuffer.class) {
        ((FloatBuffer)buffer).put((FloatBuffer)v);
    }
  }

  public void putb(byte v) {
    if ( buffer==null || sealed ) return;
    growBufferIfNecessary(1);
    if(clazz==ByteBuffer.class) {
        ((ByteBuffer)buffer).put(v);
    } else if(clazz==ShortBuffer.class) {
        ((ShortBuffer)buffer).put((short)v);
    } else if(clazz==IntBuffer.class) {
        ((IntBuffer)buffer).put((int)v);
    } else {
        throw new GLException("Byte doesn't match Buffer Class: "+clazz+" :\n\t"+this);
    }
  }

  public void puts(short v) {
    if ( buffer==null || sealed ) return;
    growBufferIfNecessary(1);
    if(clazz==ShortBuffer.class) {
        ((ShortBuffer)buffer).put(v);
    } else if(clazz==IntBuffer.class) {
        ((IntBuffer)buffer).put((int)v);
    } else {
        throw new GLException("Short doesn't match Buffer Class: "+clazz+" :\n\t"+this);
    }
  }

  public void puti(int v) {
    if ( buffer==null || sealed ) return;
    growBufferIfNecessary(1);
    if(clazz==IntBuffer.class) {
        ((IntBuffer)buffer).put(v);
    } else {
        throw new GLException("Integer doesn't match Buffer Class: "+clazz+" :\n\t"+this);
    }
  }

  public void putx(int v) {
    puti(v);
  }

  public void putf(float v) {
    if ( buffer==null || sealed ) return;
    growBufferIfNecessary(1);
    if(clazz==FloatBuffer.class) {
        ((FloatBuffer)buffer).put(v);
    } else if(clazz==IntBuffer.class) {
        ((IntBuffer)buffer).put(FixedPoint.toFixed(v));
    } else {
        throw new GLException("Float doesn't match Buffer Class: "+clazz+" :\n\t"+this);
    }
  }

  public void putd(double v) {
    if ( buffer==null || sealed ) return;
    growBufferIfNecessary(1);
    if(clazz==FloatBuffer.class) {
        ((FloatBuffer)buffer).put((float)v);
    } else {
        throw new GLException("Double doesn't match Buffer Class: "+clazz+" :\n\t"+this);
    }
  }

  public String toString() {
    return "GLArrayDataClient["+name+
                       ", index "+index+
                       ", location "+location+
                       ", isVertexAttribute "+isVertexAttribute+
                       ", dataType "+dataType+ 
                       ", bufferClazz "+clazz+ 
                       ", elements "+getElementNumber()+
                       ", components "+components+ 
                       ", stride "+stride+"u "+strideB+"b "+strideL+"c"+
                       ", initialSize "+initialSize+ 
                       ", sealed "+sealed+ 
                       ", bufferEnabled "+bufferEnabled+ 
                       ", bufferWritten "+bufferWritten+ 
                       ", buffer "+buffer+ 
                       "]";
  }

  public static final Class getBufferClass(int dataType) {
    switch(dataType) {
        case GL.GL_BYTE:
        case GL.GL_UNSIGNED_BYTE:
            return ByteBuffer.class;
        case GL.GL_SHORT:
        case GL.GL_UNSIGNED_SHORT:
            return ShortBuffer.class;
        case GL2ES1.GL_FIXED:
            return IntBuffer.class;
        case GL.GL_FLOAT:
            return FloatBuffer.class;
        default:    
            throw new GLException("Given OpenGL data type not supported: "+dataType);
    }
  }

  // non public matters

  protected final boolean growBufferIfNecessary(int spare) {
    if(buffer==null || buffer.remaining()<spare) { 
        growBuffer(initialSize);
        return true;
    }
    return false;
  }

  protected final void growBuffer(int additional) {
    if(sealed || 0==additional || 0==components) return;

    // add the stride delta
    additional += (additional/components)*(strideL-components);

    if(components>0) {
        int osize = (buffer!=null)?buffer.capacity():0;
        if(clazz==ByteBuffer.class) {
            ByteBuffer newBBuffer = BufferUtil.newByteBuffer( (osize+additional) * components );
            if(buffer!=null) {
                buffer.flip();
                newBBuffer.put((ByteBuffer)buffer);
            }
            buffer = newBBuffer;
        } else if(clazz==ShortBuffer.class) {
            ShortBuffer newSBuffer = BufferUtil.newShortBuffer( (osize+additional) * components );
            if(buffer!=null) {
                buffer.flip();
                newSBuffer.put((ShortBuffer)buffer);
            }
            buffer = newSBuffer;
        } else if(clazz==IntBuffer.class) {
            IntBuffer newIBuffer = BufferUtil.newIntBuffer( (osize+additional) * components );
            if(buffer!=null) {
                buffer.flip();
                newIBuffer.put((IntBuffer)buffer);
            }
            buffer = newIBuffer;
        } else if(clazz==FloatBuffer.class) {
            FloatBuffer newFBuffer = BufferUtil.newFloatBuffer( (osize+additional) * components );
            if(buffer!=null) {
                buffer.flip();
                newFBuffer.put((FloatBuffer)buffer);
            }
            buffer = newFBuffer;
        } else {
            throw new GLException("Given Buffer Class not supported: "+clazz+":\n\t"+this);
        }
    }
  }

  protected final void checkSeal(boolean test) throws GLException {
    if(sealed!=test) {
        if(test) {
            throw new GLException("Not Sealed yet, seal first:\n\t"+this); 
        } else {
            throw new GLException("Already Sealed, can't modify VBO:\n\t"+this); 
        }
    }
  }

  protected void init(String name, int index, int comps, int dataType, boolean normalized, int stride, Buffer data, 
                      int initialSize, boolean isVertexAttribute) 
    throws GLException
  {
    this.isVertexAttribute = isVertexAttribute;
    this.index = index;
    this.location = -1;
    this.name = (null==name)?GLContext.getPredefinedArrayIndexName(index):name;
    if(null==this.name) {
        throw new GLException("Not a valid GL array index: "+index);
    }
    this.dataType = dataType;
    this.clazz = getBufferClass(dataType);
    switch(dataType) {
        case GL.GL_BYTE:
        case GL.GL_UNSIGNED_BYTE:
        case GL.GL_SHORT:
        case GL.GL_UNSIGNED_SHORT:
        case GL2ES1.GL_FIXED:
            this.normalized = normalized;
            break;
        default:    
            this.normalized = false;
    }

    int bpc = getComponentSize();
    if(0<stride && stride<comps*bpc) {
        throw new GLException("stride ("+stride+") lower than component bytes, "+comps+" * "+bpc);
    }
    if(0<stride && stride%bpc!=0) {
        throw new GLException("stride ("+stride+") not a multiple of bpc "+bpc);
    }
    this.buffer = data;
    this.components = comps;
    this.stride=stride;
    this.strideB=(0==stride)?comps*bpc:stride;
    this.strideL=(0==stride)?comps:strideB/bpc;
    this.initialSize = initialSize;
    this.sealed=false;
    this.sealedGL=false;
    this.bufferEnabled=false;
    this.enableBufferAlways=false;
    this.bufferWritten=false;
    if(null==buffer) {
        growBuffer(initialSize);
    }
  }

  protected final void passArrayPointer(GL gl) {
    switch(index) {
        case GL.GL_VERTEX_ARRAY:
            gl.glVertexPointer(this);
            break;
        case GL.GL_NORMAL_ARRAY:
            gl.glNormalPointer(this);
            break;
        case GL.GL_COLOR_ARRAY:
            gl.glColorPointer(this);
            break;
        case GL.GL_TEXTURE_COORD_ARRAY:
            gl.glTexCoordPointer(this);
            break;
        default:
            throw new GLException("invalid glArrayIndex: "+index+":\n\t"+this); 
    }
  }

  protected void enableBufferGLImpl(GL gl, boolean enable) {
    if(enable) {
        gl.glEnableClientState(index);

        if(isVBO()) {
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER, getVBOName());
            if(!bufferWritten) {
                if(null!=buffer) {
                    gl.glBufferData(GL.GL_ARRAY_BUFFER, buffer.limit() * getComponentSize(), buffer, getBufferUsage());
                }
                bufferWritten=true;
            }
            passArrayPointer(gl);
        } else if(null!=buffer) {
            passArrayPointer(gl);
            bufferWritten=true;
        }
    } else {
        if(isVBO()) {
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
        }
        gl.glDisableClientState(index);
    }
  }

  protected void init_vbo(GL gl) {}

  protected GLArrayDataClient() { }

  protected int index;
  protected int location;
  protected String name;
  protected int components;
  protected int dataType;
  protected boolean normalized;
  protected int stride;  // user given stride
  protected int strideB; // stride in bytes
  protected int strideL; // stride in logical components
  protected Class clazz;
  protected Buffer buffer;
  protected int initialSize;
  protected boolean isVertexAttribute;
  protected boolean sealed, sealedGL;
  protected boolean bufferEnabled;
  protected boolean bufferWritten;
  protected boolean enableBufferAlways;
}