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

package javax.media.opengl;

import javax.media.opengl.*;
import java.nio.*;
import com.sun.opengl.impl.*;

public class GLArrayDataServer extends GLArrayDataClient implements GLArrayData {

  //
  // lifetime matters
  //

  /**
   * Create a VBOBuffer object, using a predefined fixed function array index
   * and starting with a given Buffer object incl it's stride
   *
   * On profiles GL2 and ES1 the fixed function pipeline behavior is as expected.
   * On profile ES2 the fixed function emulation will transform these calls to 
   * EnableVertexAttribArray and VertexAttribPointer calls,
   * and a predefined vertex attribute variable name will be choosen.
   *
   * @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 GLArrayDataServer createFixed(int index, String name, int comps, int dataType, boolean normalized,
                                              int stride, Buffer buffer, int glBufferUsage)
    throws GLException
  {
    GLProfile.isValidateArrayDataType(index, comps, dataType, false, true);

    GLArrayDataServer ads = new GLArrayDataServer();
    ads.init(name, index, comps, dataType, normalized, stride, buffer, 0, buffer.limit(), glBufferUsage, false);
    return ads;
  }

  /**
   * Create a VBOBuffer object, using a predefined fixed function array index
   * and starting with a new created Buffer object with initialSize size
   *
   * On profiles GL2 and ES1 the fixed function pipeline behavior is as expected.
   * On profile ES2 the fixed function emulation will transform these calls to 
   * EnableVertexAttribArray and VertexAttribPointer calls,
   * and a predefined vertex attribute variable name will be choosen.
   *
   * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int)
   */
  public static GLArrayDataServer createFixed(int index, String name, int comps, int dataType, boolean normalized, 
                                              int initialSize, int glBufferUsage)
    throws GLException
  {
    GLProfile.isValidateArrayDataType(index, comps, dataType, false, true);

    GLArrayDataServer ads = new GLArrayDataServer();
    ads.init(name, index, comps, dataType, normalized, 0, null, 0, initialSize, glBufferUsage, false);
    return ads;
  }

  /**
   * Create a VBOBuffer object, using a predefined fixed function array index
   * and starting with a given Buffer offset incl it's stride
   *
   * This object can neither be enabled, nor rendered, since no knowledge of the buffer
   * itself is available. This object is only a VBO variant of GLArrayData
   * and cannot being drawn.
   *
   * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int)
   */
  public static GLArrayDataServer createFixed(int index, String name, int comps, int dataType, boolean normalized,
                                              int stride, long bufferOffset)
    throws GLException
  {
    GLProfile.isValidateArrayDataType(index, comps, dataType, false, true);

    GLArrayDataServer ads = new GLArrayDataServer();
    ads.init(name, index, comps, dataType, normalized, stride, null, bufferOffset, 0, -1, false);
    return ads;
  }

  /**
   * Create a VBOBuffer object, using a custom GLSL array attribute name
   * and starting with a new created Buffer object with initialSize size
   *
   * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int)
   */
  public static GLArrayDataServer createGLSL(String name, int comps, int dataType, boolean normalized,
                                             int initialSize, int glBufferUsage) 
    throws GLException
  {
    GLProfile.isValidateArrayDataType(-1, comps, dataType, true, true);
    Class[] types = new Class[]{ String.class, int.class, int.class, boolean.class, int.class, int.class };
    Object[] args = new Object[]{ name, new Integer(comps), new Integer(dataType), 
                                  new Boolean(normalized), new Integer(initialSize), new Integer(glBufferUsage) } ;

    if(GLProfile.isGL2ES2()) {
        return (GLArrayDataServer) GLReflection.createInstance("com.sun.opengl.impl.glsl.GLSLArrayDataServer", types, args);
    }
    throw new GLException("GLArrayDataServer not supported for profile: "+GLProfile.getProfile());
  }

  /**
   * Create a VBOBuffer object, using a custom GLSL array attribute name
   * and starting with a given Buffer object incl it's stride
   *
   * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int)
   */
  public static GLArrayDataServer createGLSL(String name, int comps, int dataType, boolean normalized,
                                             int stride, Buffer buffer, int glBufferUsage) 
    throws GLException
  {
    GLProfile.isValidateArrayDataType(-1, comps, dataType, true, true);
    Class[] types = new Class[]{ String.class, int.class, int.class, boolean.class, int.class, Buffer.class, int.class };
    Object[] args = new Object[]{ name, new Integer(comps), new Integer(dataType), 
                                  new Boolean(normalized), new Integer(stride), buffer, new Integer(glBufferUsage) } ;

    if(GLProfile.isGL2ES2()) {
        return (GLArrayDataServer) GLReflection.createInstance("com.sun.opengl.impl.glsl.GLSLArrayDataServer", types, args);
    }
    throw new GLException("GLArrayDataServer not supported for profile: "+GLProfile.getProfile());
  }

  //
  // Data matters GLArrayData
  //

  public long getOffset() { return vboUsage?bufferOffset:-1; }

  public boolean isVBO() { return vboUsage; }

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

  public void destroy(GL gl) {
    super.destroy(gl);
    if(vboName!=0) {
        int[] tmp = new int[1];
        tmp[0] = vboName;
        gl.glDeleteBuffers(1, tmp, 0);
        vboName = 0;
    }
  }

  //
  // data matters 
  //

  /**
   * Convenient way do disable the VBO behavior and 
   * switch to client side data one
   * Only possible if buffer is defined.
   */
  public void    setVBOUsage(boolean vboUsage) { 
    checkSeal(false);
    this.vboUsage=(null!=buffer)?vboUsage:true; 
  }

  public int getBufferUsage() {
    return glBufferUsage;
  }

  public String toString() {
    return "GLArrayDataServer["+name+
                       ", index "+index+
                       ", location "+location+
                       ", isVertexAttribute "+isVertexAttribute+
                       ", dataType "+dataType+ 
                       ", bufferClazz "+clazz+ 
                       ", vertices "+getVerticeNumber()+
                       ", components "+components+ 
                       ", stride "+stride+"u "+strideB+"b "+strideL+"c"+
                       ", initialSize "+initialSize+ 
                       ", glBufferUsage "+glBufferUsage+ 
                       ", vboUsage "+vboUsage+ 
                       ", vboName "+vboName+ 
                       ", sealed "+sealed+ 
                       ", bufferEnabled "+bufferEnabled+ 
                       ", bufferWritten "+bufferWritten+ 
                       ", buffer "+buffer+ 
                       ", offset "+bufferOffset+ 
                       "]";
  }

  //
  // non public matters ..
  //

  protected void init(String name, int index, int comps, int dataType, boolean normalized, 
                      int stride, Buffer data, long offset, int initialSize, int glBufferUsage, boolean isVertexAttribute)
    throws GLException
  {
    super.init(name, index, comps, dataType, normalized, stride, data, initialSize, isVertexAttribute);

    vboUsage=true;

    if( ! (GLProfile.isGL2ES2() && glBufferUsage==GL2ES2.GL_STREAM_DRAW) ) {
        switch(glBufferUsage) {
            case -1: // nop
            case GL2ES1.GL_STATIC_DRAW:
            case GL2ES1.GL_DYNAMIC_DRAW:
                break;
            default:
                throw new GLException("invalid glBufferUsage: "+glBufferUsage+":\n\t"+this); 
        }
    }
    this.bufferOffset=offset;
    this.glBufferUsage = glBufferUsage;
    this.vboName = 0;
  }

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

        if(vboUsage) {
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName);
            if(!bufferWritten) {
                gl.glBufferData(GL.GL_ARRAY_BUFFER, buffer.limit() * getBufferCompSize(), buffer, glBufferUsage);
            }
        }
        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); 
        }
        bufferWritten=true;
    } else {
        gl.glDisableClientState(index);
        bufferEnabled = false;
    }
  }
  protected void init_vbo(GL gl) {
    if(vboUsage && vboName==0) {
        int[] tmp = new int[1];
        gl.glGenBuffers(1, tmp, 0);
        vboName = tmp[0];
    }
  }

  protected long bufferOffset;
  protected int glBufferUsage;
  protected int vboName;
  protected boolean vboUsage;

}