aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/javax/media/opengl/util/ImmModeSink.java
blob: 7201cec6546c734ed9c9bb9ba161e7b9354027df (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

package javax.media.opengl.util;

import javax.media.opengl.*;
import java.nio.*;
import java.util.Iterator;
import java.util.ArrayList;

public class ImmModeSink {

  public static final boolean DEBUG_BEGIN_END = false;
  public static final boolean DEBUG_DRAW = false;
  public static final boolean FLOAT2FIXED = false;

  public static final int GL_QUADS      = 0x0007;
  public static final int GL_QUAD_STRIP = 0x0008;
  public static final int GL_POLYGON    = 0x0009;

  public ImmModeSink(int glDataType, int glDrawUsage,
                     int vComps, int nComps, int cComps, int tComps, int initialSize) {

    vboSet = new  VBOSet(glDataType, glDrawUsage, vComps, nComps, cComps, tComps, initialSize);
    this.vboSetList   = new ArrayList();
  }

  private void destroyList(GL gl) {
    for(Iterator i=vboSetList.iterator(); i.hasNext() ; ) {
        ((VBOSet)i.next()).destroy(gl);
    }
    vboSetList.clear();
  }

  public void destroy(GL gl) {
    destroyList(gl);

    vboSet.destroy(gl);
  }

  public void reset() {
    reset(null);
  }

  public void reset(GL gl) {
    destroyList(gl);
    vboSet.reset(gl);
  }

  public String toString() {
    return "ImmModeSink[listsz: "+vboSetList.size()+
                       ",\n"+vboSet+
                       "]";
  }

  public void draw(GL gl, boolean disableBufferAfterDraw) {
    if(DEBUG_DRAW) {
        Exception e = new Exception("ImmModeSink.draw(disableBufferAfterDraw: "+disableBufferAfterDraw+"):\n\t"+this);
        e.printStackTrace();
    }
    int n=0;
    for(Iterator i=vboSetList.iterator(); i.hasNext() ; n++) {
        ((VBOSet)i.next()).draw(gl, disableBufferAfterDraw, n);
    }
  }

  public void glBegin(int mode) {
    if(DEBUG_BEGIN_END) {
        Exception e = new Exception("ImmModeSink.glBegin("+vboSet.mode+"):\n\t"+this);
        e.printStackTrace();
    }
    vboSet.modeOrig = mode;
    switch(mode) {
        case GL_QUADS:
            mode=GL.GL_TRIANGLE_STRIP;
            break;
        case GL_QUAD_STRIP:
            mode=GL.GL_TRIANGLE_STRIP;
            break;
        case GL_POLYGON:
            mode=GL.GL_LINES;
            break;
    }
    vboSet.mode = mode;
    vboSet.checkSeal(false);
  }

  public final void glEnd(GL gl) {
      glEnd(gl, true);
  }

  public void glEnd(GL gl, boolean immediateDraw) {
    if(DEBUG_BEGIN_END) {
        Exception e = new Exception("ImmModeSink START glEnd(immediate: "+immediateDraw+"):\n\t"+this);
        e.printStackTrace();
    }
    if(immediateDraw) {
        vboSet.seal(gl, false);
        vboSet.draw(gl, true, -1);
        reset(gl);
    } else {
        vboSet.seal(gl, true);
        vboSetList.add(vboSet);
        vboSet = vboSet.regenerate();
    }
  }

  public final void glVertex2f(float x, float y) {
    vboSet.glVertex2f(x,y);
  }

  public final void glVertex3f(float x, float y, float z) {
    vboSet.glVertex3f(x,y,z);
  }

  public final void glNormal3f(float x, float y, float z) {
    vboSet.glNormal3f(x,y,z);
  }

  public final void glColor3f(float x, float y, float z) {
    vboSet.glColor3f(x,y,z);
  }

  public final void glTexCoord2f(float x, float y) {
    vboSet.glTexCoord2f(x,y);
  }

  public final void glTexCoord3f(float x, float y, float z) {
    vboSet.glTexCoord3f(x,y,z);
  }

  private VBOSet vboSet;
  private ArrayList vboSetList;

  protected static class VBOSet {
    protected VBOSet(int glDataType, int glDrawUsage,
                   int vComps, int nComps, int cComps, int tComps, int initialSize) {
        nComps = 0;
        tComps = 0;
        if(FLOAT2FIXED && glDataType==GL.GL_FLOAT) {
            glDataType=GL.GL_FIXED;
        }
        this.glDataType=glDataType;
        this.glDrawUsage=glDrawUsage;
        this.vComps=vComps;
        this.nComps=nComps;
        this.cComps=cComps;
        this.tComps=tComps;
        this.initialSize=initialSize;

        this.vertexVBO   = VBOBufferDraw.create(GL2ES1.GL_VERTEX_ARRAY,        glDataType, glDrawUsage, vComps, initialSize);
        this.normalVBO   = VBOBufferDraw.create(GL2ES1.GL_NORMAL_ARRAY,        glDataType, glDrawUsage, nComps, initialSize);
        this.colorVBO    = VBOBufferDraw.create(GL2ES1.GL_COLOR_ARRAY,         glDataType, glDrawUsage, cComps, initialSize);
        this.texcoordVBO = VBOBufferDraw.create(GL2ES1.GL_TEXTURE_COORD_ARRAY, glDataType, glDrawUsage, tComps, initialSize);

        this.sealed=false;
        this.mode = -1;
        this.modeOrig = -1;
    }

    protected final VBOSet regenerate() {
        return new VBOSet(glDataType, glDrawUsage, vComps, nComps, cComps, tComps, initialSize);
    }

    protected void destroy(GL gl) {
        vertexVBO.destroy(gl);
        normalVBO.destroy(gl);
        colorVBO.destroy(gl);
        texcoordVBO.destroy(gl);

        this.mode = -1;
        this.modeOrig = -1;
        this.sealed=false;
    }

    protected void reset(GL gl) {
        vertexVBO.reset(gl);
        normalVBO.reset(gl);
        colorVBO.reset(gl);
        texcoordVBO.reset(gl);

        this.mode = -1;
        this.modeOrig = -1;
        this.sealed=false;
    }

    public String toString() {
        return "VBOSet[mode "+mode+ 
                       ", modeOrig "+modeOrig+ 
                       ", sealed "+sealed+ 
                       ",\n\t vertexVBO "+vertexVBO+
                       ",\n\t normalVBO "+normalVBO+
                       ",\n\t colorVBO "+colorVBO+
                       ",\n\t texcoordVBO "+texcoordVBO+
                       "]";
    }

    protected void checkSeal(boolean test) throws GLException {
        if(mode<0) {
                throw new GLException("No mode set yet, call glBegin(mode) first:\n\t"+this); 
        }
        if(sealed!=test) {
            if(test) {
                throw new GLException("Not Sealed yet, call glEnd() first:\n\t"+this); 
            } else {
                throw new GLException("Already Sealed, can't modify VBO after glEnd():\n\t"+this); 
            }
        }
    }

    protected void rewind() {
        checkSeal(true);

        vertexVBO.rewind();
        normalVBO.rewind();
        colorVBO.rewind();
        texcoordVBO.rewind();
    }

    protected void seal(GL gl, boolean disableBufferAfterSeal)
    {
        checkSeal(false);
        sealed = true;

        vertexVBO.seal(gl, disableBufferAfterSeal);
        normalVBO.seal(gl, disableBufferAfterSeal);
        colorVBO.seal(gl, disableBufferAfterSeal);
        texcoordVBO.seal(gl, disableBufferAfterSeal);
    }

    protected void draw(GL gl, boolean disableBufferAfterDraw, int i)
    {
        if(DEBUG_DRAW) {
            Exception e = new Exception("ImmModeSink.draw["+i+"](disableBufferAfterDraw: "+disableBufferAfterDraw+"):\n\t"+this);
            e.printStackTrace();
        }
        vertexVBO.enableBuffer(gl);
        normalVBO.enableBuffer(gl);
        colorVBO.enableBuffer(gl);
        texcoordVBO.enableBuffer(gl);

        if (vertexVBO.getBuffer()!=null) {
            gl.glDrawArrays(mode, 0, vertexVBO.getVerticeNumber());
        }

        if(disableBufferAfterDraw) {
            vertexVBO.disableBuffer(gl);
            normalVBO.disableBuffer(gl);
            colorVBO.disableBuffer(gl);
            texcoordVBO.disableBuffer(gl);
        }
    }

    protected void glVertex2f(float x, float y) {
        checkSeal(false);
        vertexVBO.putf(x);
        if(vertexVBO.getComponents()>1) 
            vertexVBO.putf(y);
        vertexVBO.padding(2);
    }

    protected void glVertex3f(float x, float y, float z) {
        checkSeal(false);
        vertexVBO.putf(x);
        if(vertexVBO.getComponents()>1) 
            vertexVBO.putf(y);
        if(vertexVBO.getComponents()>2) 
            vertexVBO.putf(z);
        vertexVBO.padding(3);
    }

    protected void glNormal3f(float x, float y, float z) {
        checkSeal(false);
        normalVBO.putf(x);
        if(normalVBO.getComponents()>1) 
            normalVBO.putf(y);
        if(normalVBO.getComponents()>2) 
            normalVBO.putf(z);
        normalVBO.padding(3);
    }

    protected void glColor3f(float x, float y, float z) {
        checkSeal(false);
        colorVBO.putf(x);
        if(colorVBO.getComponents()>1) 
            colorVBO.putf(y);
        if(colorVBO.getComponents()>2) 
            colorVBO.putf(z);
        colorVBO.padding(3);
    }

    protected void glTexCoord2f(float x, float y) {
        checkSeal(false);
        texcoordVBO.putf(x);
        if(texcoordVBO.getComponents()>1) 
            texcoordVBO.putf(y);
        texcoordVBO.padding(2);
    }

    protected void glTexCoord3f(float x, float y, float z) {
        checkSeal(false);
        texcoordVBO.putf(x);
        if(texcoordVBO.getComponents()>1) 
            texcoordVBO.putf(y);
        if(texcoordVBO.getComponents()>2) 
            texcoordVBO.putf(z);
        texcoordVBO.padding(3);
    }

    VBOBufferDraw vertexVBO;
    VBOBufferDraw normalVBO;
    VBOBufferDraw colorVBO;
    VBOBufferDraw texcoordVBO;
    int mode, modeOrig;
    int glDataType, glDrawUsage, vComps, nComps, cComps, tComps, initialSize;
    boolean sealed;
  }

}