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
|
/**
* 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.graph.curve.opengl;
import java.nio.FloatBuffer;
import javax.media.opengl.GL2ES2;
// FIXME: Subsume GL2GL3.GL_DRAW_FRAMEBUFFER -> GL2ES2.GL_DRAW_FRAMEBUFFER !
import javax.media.opengl.GL;
import javax.media.opengl.GLException;
import javax.media.opengl.GLUniformData;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import jogamp.graph.curve.opengl.shader.AttributeNames;
import jogamp.graph.curve.opengl.shader.UniformNames;
import com.jogamp.common.nio.Buffers;
import com.jogamp.graph.geom.Triangle;
import com.jogamp.graph.geom.Vertex;
import com.jogamp.graph.curve.opengl.GLRegion;
import com.jogamp.graph.curve.opengl.RenderState;
import com.jogamp.opengl.util.FBObject;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.glsl.ShaderState;
public class VBORegion2PES2 extends GLRegion {
private GLArrayDataServer verticeTxtAttr;
private GLArrayDataServer texCoordTxtAttr;
private GLArrayDataServer indicesTxt;
private GLArrayDataServer verticeFboAttr;
private GLArrayDataServer texCoordFboAttr;
private GLArrayDataServer indicesFbo;
private FBObject fbo;
private PMVMatrix fboPMVMatrix;
GLUniformData mgl_fboPMVMatrix;
private int tex_width_c = 0;
private int tex_height_c = 0;
GLUniformData mgl_ActiveTexture;
GLUniformData mgl_TextureSize; // if GLSL < 1.30
public VBORegion2PES2(int renderModes, int textureEngine) {
super(renderModes);
fboPMVMatrix = new PMVMatrix();
mgl_fboPMVMatrix = new GLUniformData(UniformNames.gcu_PMVMatrix, 4, 4, fboPMVMatrix.glGetPMvMatrixf());
mgl_ActiveTexture = new GLUniformData(UniformNames.gcu_TextureUnit, textureEngine);
}
public void update(GL2ES2 gl, RenderState rs) {
if(!isDirty()) {
return;
}
if(null == indicesFbo) {
final int initialSize = 256;
final ShaderState st = rs.getShaderState();
indicesFbo = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER);
indicesFbo.puts((short) 0); indicesFbo.puts((short) 1); indicesFbo.puts((short) 3);
indicesFbo.puts((short) 1); indicesFbo.puts((short) 2); indicesFbo.puts((short) 3);
indicesFbo.seal(true);
texCoordFboAttr = GLArrayDataServer.createGLSL(AttributeNames.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT,
false, initialSize, GL.GL_STATIC_DRAW);
st.ownAttribute(texCoordFboAttr, true);
texCoordFboAttr.putf(5); texCoordFboAttr.putf(5);
texCoordFboAttr.putf(5); texCoordFboAttr.putf(6);
texCoordFboAttr.putf(6); texCoordFboAttr.putf(6);
texCoordFboAttr.putf(6); texCoordFboAttr.putf(5);
texCoordFboAttr.seal(true);
verticeFboAttr = GLArrayDataServer.createGLSL(AttributeNames.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT,
false, initialSize, GL.GL_STATIC_DRAW);
st.ownAttribute(verticeFboAttr, true);
indicesTxt = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER);
verticeTxtAttr = GLArrayDataServer.createGLSL(AttributeNames.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT,
false, initialSize, GL.GL_STATIC_DRAW);
st.ownAttribute(verticeTxtAttr, true);
texCoordTxtAttr = GLArrayDataServer.createGLSL(AttributeNames.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT,
false, initialSize, GL.GL_STATIC_DRAW);
st.ownAttribute(texCoordTxtAttr, true);
if(DEBUG_INSTANCE) {
System.err.println("VBORegion2PES2 Create: " + this);
}
}
// process triangles
indicesTxt.seal(gl, false);
indicesTxt.rewind();
for(int i=0; i<triangles.size(); i++) {
final Triangle t = triangles.get(i);
final Vertex[] t_vertices = t.getVertices();
if(t_vertices[0].getId() == Integer.MAX_VALUE){
t_vertices[0].setId(numVertices++);
t_vertices[1].setId(numVertices++);
t_vertices[2].setId(numVertices++);
vertices.add(t_vertices[0]);
vertices.add(t_vertices[1]);
vertices.add(t_vertices[2]);
indicesTxt.puts((short) t_vertices[0].getId());
indicesTxt.puts((short) t_vertices[1].getId());
indicesTxt.puts((short) t_vertices[2].getId());
} else {
indicesTxt.puts((short) t_vertices[0].getId());
indicesTxt.puts((short) t_vertices[1].getId());
indicesTxt.puts((short) t_vertices[2].getId());
}
}
indicesTxt.seal(gl, true);
indicesTxt.enableBuffer(gl, false);
// process vertices and update bbox
box.reset();
verticeTxtAttr.seal(gl, false);
verticeTxtAttr.rewind();
texCoordTxtAttr.seal(gl, false);
texCoordTxtAttr.rewind();
for(int i=0; i<vertices.size(); i++) {
final Vertex v = vertices.get(i);
verticeTxtAttr.putf(v.getX());
verticeTxtAttr.putf(v.getY());
verticeTxtAttr.putf(v.getZ());
box.resize(v.getX(), v.getY(), v.getZ());
final float[] tex = v.getTexCoord();
texCoordTxtAttr.putf(tex[0]);
texCoordTxtAttr.putf(tex[1]);
}
texCoordTxtAttr.seal(gl, true);
texCoordTxtAttr.enableBuffer(gl, false);
verticeTxtAttr.seal(gl, true);
verticeTxtAttr.enableBuffer(gl, false);
// update all bbox related data
verticeFboAttr.seal(gl, false);
verticeFboAttr.rewind();
verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]);
verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]);
verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]);
verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]);
verticeFboAttr.seal(gl, true);
verticeFboAttr.enableBuffer(gl, false);
fboPMVMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
fboPMVMatrix.glLoadIdentity();
fboPMVMatrix.glOrthof(box.getLow()[0], box.getHigh()[0], box.getLow()[1], box.getHigh()[1], -1, 1);
// push data 2 GPU ..
indicesFbo.seal(gl, true);
indicesFbo.enableBuffer(gl, false);
setDirty(false);
// the buffers were disabled, since due to real/fbo switching and other vbo usage
}
int[] maxTexSize = new int[] { -1 } ;
protected void drawImpl(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int width) {
if(vp_width <=0 || vp_height <= 0 || width <= 0){
renderRegion(gl);
} else {
if(0 > maxTexSize[0]) {
gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, maxTexSize, 0);
}
if(width > maxTexSize[0]) {
width = maxTexSize[0];
}
if(width != tex_width_c) {
renderRegion2FBO(gl, rs, width);
}
// System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3));
renderFBO(gl, rs, vp_width, vp_height);
}
}
private void renderFBO(GL2ES2 gl, RenderState rs, int width, int hight) {
final ShaderState st = rs.getShaderState();
gl.glViewport(0, 0, width, hight);
st.uniform(gl, mgl_ActiveTexture);
fbo.use(gl, 0);
verticeFboAttr.enableBuffer(gl, true);
texCoordFboAttr.enableBuffer(gl, true);
indicesFbo.enableBuffer(gl, true);
gl.glDrawElements(GL2ES2.GL_TRIANGLES, indicesFbo.getElementCount() * indicesFbo.getComponentCount(), GL2ES2.GL_UNSIGNED_SHORT, 0);
verticeFboAttr.enableBuffer(gl, false);
texCoordFboAttr.enableBuffer(gl, false);
indicesFbo.enableBuffer(gl, false);
fbo.unuse(gl);
// setback: gl.glActiveTexture(currentActiveTextureEngine[0]);
}
private void renderRegion2FBO(GL2ES2 gl, RenderState rs, int tex_width) {
final ShaderState st = rs.getShaderState();
tex_width_c = tex_width;
tex_height_c = (int) ( ( ( tex_width_c * box.getHeight() ) / box.getWidth() ) + 0.5f );
// System.out.println("FBO Size: "+tex_width+" -> "+tex_height_c+"x"+tex_width_c);
// System.out.println("FBO Scale: " + m.glGetMatrixf().get(0) +" " + m.glGetMatrixf().get(5));
if(null != fbo && fbo.getWidth() != tex_width_c && fbo.getHeight() != tex_height_c ) {
fbo.destroy(gl);
fbo = null;
}
if(null == fbo) {
fbo = new FBObject(tex_width_c, tex_height_c);
fbo.init(gl);
// FIXME: shall not use bilinear, due to own AA ? However, w/o bilinear result is not smooth
fbo.attachTexture2D(gl, mgl_ActiveTexture.intValue(), GL2ES2.GL_LINEAR, GL2ES2.GL_LINEAR, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE);
// fbo.attachTexture2D(gl, mgl_ActiveTexture.intValue(), GL2ES2.GL_NEAREST, GL2ES2.GL_NEAREST, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE);
fbo.attachDepthBuffer(gl, GL.GL_DEPTH_COMPONENT16); // FIXME: or shall we use 24 or 32 bit depth ?
if(!fbo.isStatusValid()) {
throw new GLException("FBO invalid: "+fbo);
}
} else {
fbo.bind(gl);
}
//render texture
gl.glViewport(0, 0, tex_width_c, tex_height_c);
st.uniform(gl, mgl_fboPMVMatrix); // use orthogonal matrix
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_BUFFER_BIT);
renderRegion(gl);
fbo.unbind(gl);
st.uniform(gl, rs.getPMVMatrix()); // switch back to real PMV matrix
// if( !gl.isGL3() ) {
// GLSL < 1.30
if(null == mgl_TextureSize) {
mgl_TextureSize = new GLUniformData(UniformNames.gcu_TextureSize, 2, Buffers.newDirectFloatBuffer(2));
}
final FloatBuffer texSize = (FloatBuffer) mgl_TextureSize.getBuffer();
texSize.put(0, (float)fbo.getWidth());
texSize.put(1, (float)fbo.getHeight());
st.uniform(gl, mgl_TextureSize);
//}
}
private void renderRegion(GL2ES2 gl) {
verticeTxtAttr.enableBuffer(gl, true);
texCoordTxtAttr.enableBuffer(gl, true);
indicesTxt.enableBuffer(gl, true);
gl.glDrawElements(GL2ES2.GL_TRIANGLES, indicesTxt.getElementCount() * indicesTxt.getComponentCount(), GL2ES2.GL_UNSIGNED_SHORT, 0);
verticeTxtAttr.enableBuffer(gl, false);
texCoordTxtAttr.enableBuffer(gl, false);
indicesTxt.enableBuffer(gl, false);
}
public void destroy(GL2ES2 gl, RenderState rs) {
if(DEBUG_INSTANCE) {
System.err.println("VBORegion2PES2 Destroy: " + this);
}
final ShaderState st = rs.getShaderState();
if(null != fbo) {
fbo.destroy(gl);
fbo = null;
}
if(null != verticeTxtAttr) {
st.ownAttribute(verticeTxtAttr, false);
verticeTxtAttr.destroy(gl);
verticeTxtAttr = null;
}
if(null != texCoordTxtAttr) {
st.ownAttribute(texCoordTxtAttr, false);
texCoordTxtAttr.destroy(gl);
texCoordTxtAttr = null;
}
if(null != indicesTxt) {
indicesTxt.destroy(gl);
indicesTxt = null;
}
if(null != verticeFboAttr) {
st.ownAttribute(verticeFboAttr, false);
verticeFboAttr.destroy(gl);
verticeFboAttr = null;
}
if(null != texCoordFboAttr) {
st.ownAttribute(texCoordFboAttr, false);
texCoordFboAttr.destroy(gl);
texCoordFboAttr = null;
}
if(null != indicesFbo) {
indicesFbo.destroy(gl);
indicesFbo = null;
}
triangles.clear();
vertices.clear();
}
}
|