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
|
package com.jogamp.opengl.test.junit.jogl.demos.gl4;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.FloatBuffer;
import java.util.Random;
import javax.media.opengl.DebugGL4;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GL4;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;
import javax.media.opengl.GLUniformData;
import javax.media.opengl.TraceGL4;
import com.jogamp.opengl.math.Matrix4;
import com.jogamp.opengl.util.GLArrayDataClient;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderProgram;
import com.jogamp.opengl.util.glsl.ShaderState;
public class TriangleInstancedRendererWithShaderState implements GLEventListener {
private float aspect;
private static final String shaderBasename = "triangles";
// private static final boolean useInterleaved = false;
private static final boolean useInterleaved = true;
private ShaderState st;
private PMVMatrix projectionMatrix;
private GLUniformData projectionMatrixUniform;
private GLUniformData transformMatrixUniform;
private GLArrayDataServer interleavedVBO;
private GLArrayDataClient verticesVBO;
private GLArrayDataClient colorsVBO;
private static final int NO_OF_INSTANCE = 30;
private final FloatBuffer triangleTransform = FloatBuffer.allocate(16 * NO_OF_INSTANCE);
private final Matrix4[] mat = new Matrix4[NO_OF_INSTANCE];
private final float[] rotationSpeed = new float[NO_OF_INSTANCE];
private static final boolean useTraceGL = false;
private PrintStream stream;
private final IInstancedRenderingView view;
private boolean isInitialized = false;
public TriangleInstancedRendererWithShaderState(IInstancedRenderingView view) {
this.view = view;
if(useTraceGL) {
try {
stream = new PrintStream(new FileOutputStream(new File("instanced-with-st.txt")));
} catch (IOException e1) {
e1.printStackTrace();
}
}
initTransform();
}
private void initTransform() {
Random rnd = new Random();
for(int i = 0; i < NO_OF_INSTANCE; i++) {
rotationSpeed[i] = 0.3f * rnd.nextFloat();
mat[i] = new Matrix4();
mat[i].loadIdentity();
float scale = 1f + 4 * rnd.nextFloat();
mat[i].scale(scale, scale, scale);
//setup initial position of each triangle
mat[i].translate(20f * rnd.nextFloat() - 10f,
10f * rnd.nextFloat() - 5f,
0f);
}
}
@Override
public void init(GLAutoDrawable drawable) {
GL4 gl = drawable.getGL().getGL4();
drawable.setGL(new DebugGL4(gl));
if(useTraceGL) {
drawable.setGL(new TraceGL4(gl, stream));
}
gl.glClearColor(1, 1, 1, 1); //white background
// gl.glClearColor(0, 0, 0, 1); //black background
gl.glClearDepth(1.0f);
System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
System.err.println("INIT GL IS: " + gl.getClass().getName());
System.err.println("GL_VENDOR: " + gl.glGetString(GL4.GL_VENDOR));
System.err.println("GL_RENDERER: " + gl.glGetString(GL4.GL_RENDERER));
System.err.println("GL_VERSION: " + gl.glGetString(GL4.GL_VERSION));
initShader(gl);
projectionMatrix = new PMVMatrix();
projectionMatrixUniform = new GLUniformData("mgl_PMatrix", 4, 4, projectionMatrix.glGetPMatrixf());
st.ownUniform(projectionMatrixUniform);
if(!st.uniform(gl, projectionMatrixUniform)) {
throw new GLException("Error setting mgl_PMatrix in shader: " + st);
}
transformMatrixUniform = new GLUniformData("mgl_MVMatrix", 4, 4, triangleTransform);
st.ownUniform(transformMatrixUniform);
if(!st.uniform(gl, transformMatrixUniform)) {
throw new GLException("Error setting mgl_MVMatrix in shader: " + st);
}
if(useInterleaved) {
initVBO_interleaved(gl);
} else {
initVBO_nonInterleaved(gl);
}
isInitialized = true;
}
@Override
public void display(GLAutoDrawable drawable) {
if(!isInitialized ) return;
GL4 gl = drawable.getGL().getGL4();
gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT);
st.useProgram(gl, true);
projectionMatrix.glMatrixMode(GL2.GL_PROJECTION);
projectionMatrix.glPushMatrix();
float winScale = 0.1f;
if(view != null) winScale = view.getScale();
projectionMatrix.glScalef(winScale, winScale, winScale);
projectionMatrix.update();
st.uniform(gl, projectionMatrixUniform);
projectionMatrix.glPopMatrix();
generateTriangleTransform();
st.uniform(gl, transformMatrixUniform);
if(useInterleaved) {
interleavedVBO.enableBuffer(gl, true);
} else {
verticesVBO.enableBuffer(gl, true);
colorsVBO.enableBuffer(gl, true);
}
//gl.glVertexAttribDivisor() is not required since each instance has the same attribute (color).
gl.glDrawArraysInstanced(GL4.GL_TRIANGLES, 0, 3, NO_OF_INSTANCE);
if(useInterleaved) {
interleavedVBO.enableBuffer(gl, false);
} else {
verticesVBO.enableBuffer(gl, false);
colorsVBO.enableBuffer(gl, false);
}
st.useProgram(gl, false);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL4 gl3 = drawable.getGL().getGL4();
gl3.glViewport(0, 0, width, height);
aspect = (float) width / (float) height;
projectionMatrix.glMatrixMode(GL2.GL_PROJECTION);
projectionMatrix.glLoadIdentity();
projectionMatrix.gluPerspective(45, aspect, 0.001f, 20f);
projectionMatrix.gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0);
}
@Override
public void dispose(GLAutoDrawable drawable){
GL4 gl = drawable.getGL().getGL4();
st.destroy(gl);
}
private void generateTriangleTransform() {
triangleTransform.clear();
for(int i = 0; i < NO_OF_INSTANCE; i++) {
mat[i].rotate(rotationSpeed[i], 0, 0, 1);
triangleTransform.put(mat[i].getMatrix());
}
triangleTransform.rewind();
}
private void initVBO_nonInterleaved(GL4 gl) {
int VERTEX_COUNT = 3;
verticesVBO = GLArrayDataClient.createGLSL("mgl_Vertex", 3, GL4.GL_FLOAT, false, VERTEX_COUNT);
FloatBuffer verticeBuf = (FloatBuffer)verticesVBO.getBuffer();
verticeBuf.put(vertices);
verticesVBO.seal(gl, true);
colorsVBO = GLArrayDataClient.createGLSL("mgl_Color", 4, GL4.GL_FLOAT, false, VERTEX_COUNT);
FloatBuffer colorBuf = (FloatBuffer)colorsVBO.getBuffer();
colorBuf.put(colors);
colorsVBO.seal(gl, true);
verticesVBO.enableBuffer(gl, false);
colorsVBO.enableBuffer(gl, false);
st.ownAttribute(verticesVBO, true);
st.ownAttribute(colorsVBO, true);
st.useProgram(gl, false);
}
private void initVBO_interleaved(GL4 gl) {
int VERTEX_COUNT = 3;
interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3 + 4, GL.GL_FLOAT, false, VERTEX_COUNT, GL.GL_STATIC_DRAW);
interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER);
interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer();
for(int i = 0; i < VERTEX_COUNT; i++) {
ib.put(vertices, i*3, 3);
ib.put(colors, i*4, 4);
}
interleavedVBO.seal(gl, true);
interleavedVBO.enableBuffer(gl, false);
st.ownAttribute(interleavedVBO, true);
st.useProgram(gl, false);
}
private void initShader(GL4 gl) {
// Create & Compile the shader objects
ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(),
"shader", "shader/bin", shaderBasename, true);
ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(),
"shader", "shader/bin", shaderBasename, true);
vp0.replaceInShaderSource("NO_OF_INSTANCE", String.valueOf(NO_OF_INSTANCE));
vp0.defaultShaderCustomization(gl, true, true);
fp0.defaultShaderCustomization(gl, true, true);
//vp0.dumpShaderSource(System.out);
// Create & Link the shader program
ShaderProgram sp = new ShaderProgram();
sp.add(vp0);
sp.add(fp0);
if(!sp.link(gl, System.err)) {
throw new GLException("Couldn't link program: "+sp);
}
// Let's manage all our states using ShaderState.
st = new ShaderState();
st.attachShaderProgram(gl, sp, true);
}
private static final float[] vertices = {
1.0f, 0.0f, 0,
-0.5f, 0.866f, 0,
-0.5f, -0.866f, 0
};
private final float[] colors = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0f, 0f, 1.0f, 1f
};
}
|