1package com.jogamp.opengl.test.junit.jogl.demos.gl4;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.PrintStream;
7import java.nio.FloatBuffer;
8import java.util.Random;
10import com.jogamp.opengl.DebugGL4;
11import com.jogamp.opengl.GL;
12import com.jogamp.opengl.GL2ES2;
13import com.jogamp.opengl.GL4;
14import com.jogamp.opengl.GLAutoDrawable;
15import com.jogamp.opengl.GLEventListener;
16import com.jogamp.opengl.TraceGL4;
17import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
18import com.jogamp.common.nio.Buffers;
19import com.jogamp.math.FloatUtil;
20import com.jogamp.math.Matrix4f;
21import com.jogamp.math.Vec3f;
22import com.jogamp.opengl.util.PMVMatrix;
27 private int shaderProgram;
28 private int vertShader;
29 private int fragShader;
30 private int projectionMatrixLocation;
31 private int transformMatrixLocation;
32 private static final int locPos = 1;
33 private static final int locCol = 2;
36 private static final int NO_OF_INSTANCE = 30;
37 private final FloatBuffer triangleTransform = FloatBuffer.allocate(16 * NO_OF_INSTANCE);
39 private final float[] rotationSpeed =
new float[NO_OF_INSTANCE];
43 private PrintStream stream;
46 private static final boolean useTraceGL =
false;
54 stream =
new PrintStream(
new FileOutputStream(
new File(
"instanced.txt")));
55 }
catch (
final IOException e1) {
74 System.err.println(
"INIT GL IS: " + gl.getClass().getName());
81 }
catch (
final IOException e) {
98 float winScale = 0.1f;
102 projectionMatrix.
glScalef(winScale, winScale, winScale);
105 generateTriangleTransform();
106 gl.
glUniformMatrix4fv(transformMatrixLocation, NO_OF_INSTANCE,
false, triangleTransform);
116 System.out.println(
"Window resized to width=" + width +
" height=" + height);
119 aspect = (float) width / (
float) height;
140 private void initTransform() {
141 final Random rnd =
new Random();
143 for(
int i = 0; i < NO_OF_INSTANCE; i++) {
144 rotationSpeed[i] = 0.3f * rnd.nextFloat();
147 final float scale = 1f + 4 * rnd.nextFloat();
148 mat[i].
scale(scale, tmp);
150 mat[i].
translate(20f * rnd.nextFloat() - 10f,
151 10f * rnd.nextFloat() - 5f,
156 private void initVBO(
final GL4 gl) {
157 final FloatBuffer interleavedBuffer = Buffers.newDirectFloatBuffer(vertices.length + colors.length);
158 for(
int i = 0; i < vertices.length/3; i++) {
159 for(
int j = 0; j < 3; j++) {
160 interleavedBuffer.put(vertices[i*3 + j]);
162 for(
int j = 0; j < 4; j++) {
163 interleavedBuffer.put(colors[i*4 + j]);
166 interleavedBuffer.flip();
179 final int stride = Buffers.SIZEOF_FLOAT * (3+4);
184 private void initShaders(
final GL4 gl)
throws IOException {
188 final String[] vlines =
new String[] { vertexShaderString };
189 final int[] vlengths =
new int[] { vlines[0].length() };
190 gl.
glShaderSource(vertShader, vlines.length, vlines, vlengths, 0);
193 final int[] compiled =
new int[1];
194 gl.
glGetShaderiv(vertShader, GL2ES2.GL_COMPILE_STATUS, compiled, 0);
195 if(compiled[0] != 0) {
196 System.out.println(
"Vertex shader compiled");
198 final int[] logLength =
new int[1];
199 gl.
glGetShaderiv(vertShader, GL2ES2.GL_INFO_LOG_LENGTH, logLength, 0);
201 final byte[] log =
new byte[logLength[0]];
204 System.err.println(
"Error compiling the vertex shader: " +
new String(log));
208 final String[] flines =
new String[] { fragmentShaderString };
209 final int[] flengths =
new int[] { flines[0].length() };
210 gl.
glShaderSource(fragShader, flines.length, flines, flengths, 0);
213 gl.
glGetShaderiv(fragShader, GL2ES2.GL_COMPILE_STATUS, compiled, 0);
214 if(compiled[0] != 0){
215 System.out.println(
"Fragment shader compiled.");
217 final int[] logLength =
new int[1];
218 gl.
glGetShaderiv(fragShader, GL2ES2.GL_INFO_LOG_LENGTH, logLength, 0);
220 final byte[] log =
new byte[logLength[0]];
223 System.err.println(
"Error compiling the fragment shader: " +
new String(log));
237 System.out.println(
"projectionMatrixLocation:" + projectionMatrixLocation);
239 System.out.println(
"transformMatrixLocation:" + transformMatrixLocation);
242 private void generateTriangleTransform() {
243 triangleTransform.clear();
244 final Matrix4f tmp =
new Matrix4f();
245 for(
int i = 0; i < NO_OF_INSTANCE; i++) {
247 mat[i].
rotate(rotationSpeed[i], 0, 0, 1, tmp);
249 mat[i].
get(triangleTransform);
251 triangleTransform.flip();
254 private final String vertexShaderString =
257 "uniform mat4 mgl_PMatrix; \n" +
258 "uniform mat4 mgl_MVMatrix[" + NO_OF_INSTANCE +
"]; \n" +
259 "in vec3 mgl_Vertex; \n" +
260 "in vec4 mgl_Color; \n" +
261 "out vec4 frontColor; \n" +
262 "void main(void) \n" +
264 " frontColor = mgl_Color; \n" +
265 " gl_Position = mgl_PMatrix * mgl_MVMatrix[gl_InstanceID] * vec4(mgl_Vertex, 1);" +
268 private final String fragmentShaderString =
271 "in vec4 frontColor; \n" +
272 "out vec4 mgl_FragColor; \n" +
273 "void main (void) \n" +
275 " mgl_FragColor = frontColor; \n" +
278 private final float[] vertices = {
284 private final float[] colors = {
285 1.0f, 0.0f, 0.0f, 1.0f,
286 0.0f, 1.0f, 0.0f, 1.0f,
Basic Float math utility functions.
static final float QUARTER_PI
The value PI/4, i.e.
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
final Matrix4f loadIdentity()
Set this matrix to identity.
final Matrix4f rotate(final float ang_rad, final float x, final float y, final float z, final Matrix4f tmp)
Rotate this matrix about give axis and angle in radians, i.e.
final Matrix4f scale(final float x, final float y, final float z, final Matrix4f tmp)
Scale this matrix, i.e.
final Matrix4f translate(final float x, final float y, final float z, final Matrix4f tmp)
Translate this matrix, i.e.
float get(final int i)
Gets the ith component, 0 <= i < 16.
3D Vector based upon three float components.
final SyncMatrix4f getSyncP()
Returns the SyncMatrix of projection matrix (P).
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
TrianglesInstancedRendererHardcoded(final IInstancedRenderingView view)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
final void glScalef(final float x, final float y, final float z)
Scale the current matrix.
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
final void gluPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Multiply the current matrix with the perspective/frustum matrix.
final void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
final void gluLookAt(final Vec3f eye, final Vec3f center, final Vec3f up)
Multiply the current matrix with the eye, object and orientation, i.e.
final void glPopMatrix()
Pop the current matrix from it's stack.
final void glLoadIdentity()
Load the current matrix with the identity matrix.
FloatBuffer getSyncFloats()
Return the FloatBuffer after synchronizing it w/ the underlying getMatrix().
void glVertexAttribPointer(int index, int size, int type, boolean normalized, int stride, long pointer_buffer_offset)
Entry point to C language function: void {@native glVertexAttribPointer}(GLuint index,...
void glClearDepth(double depth)
Aliased entrypoint of void {@native glClearDepth}(GLclampd depth); and void {@native glClearDepthf...
void glBindAttribLocation(int program, int index, String name)
Entry point to C language function: void {@native glBindAttribLocation}(GLuint program,...
void glEnableVertexAttribArray(int index)
Entry point to C language function: void {@native glEnableVertexAttribArray}(GLuint index) Part of...
void glCompileShader(int shader)
Entry point to C language function: void {@native glCompileShader}(GLuint shader) Part of GL_ES_VE...
void glDeleteShader(int shader)
Entry point to C language function: void {@native glDeleteShader}(GLuint shader) Part of GL_ES_VER...
void glDetachShader(int program, int shader)
Entry point to C language function: void {@native glDetachShader}(GLuint program,...
int glCreateProgram()
Entry point to C language function: GLuint {@native glCreateProgram}() Part of GL_ES_VERSION_2_0,...
void glUseProgram(int program)
Entry point to C language function: void {@native glUseProgram}(GLuint program) Part of GL_ES_VERS...
void glGetShaderiv(int shader, int pname, IntBuffer params)
Entry point to C language function: void {@native glGetShaderiv}(GLuint shader, GLenum pname,...
int glGetUniformLocation(int program, String name)
Entry point to C language function: GLint {@native glGetUniformLocation}(GLuint program,...
void glShaderSource(int shader, int count, String[] string, IntBuffer length)
Entry point to C language function: void {@native glShaderSource}(GLuint shader, GLsizei count,...
void glAttachShader(int program, int shader)
Entry point to C language function: void {@native glAttachShader}(GLuint program,...
void glUniformMatrix4fv(int location, int count, boolean transpose, FloatBuffer value)
Entry point to C language function: void {@native glUniformMatrix4fv}(GLint location,...
void glDeleteProgram(int program)
Entry point to C language function: void {@native glDeleteProgram}(GLuint program) Part of GL_ES_V...
int glCreateShader(int type)
Entry point to C language function: GLuint {@native glCreateShader}(GLenum type) Part of GL_ES_VER...
void glGetShaderInfoLog(int shader, int bufSize, IntBuffer length, ByteBuffer infoLog)
Entry point to C language function: void {@native glGetShaderInfoLog}(GLuint shader,...
void glLinkProgram(int program)
Entry point to C language function: void {@native glLinkProgram}(GLuint program) Part of GL_ES_VER...
void glGenVertexArrays(int n, IntBuffer arrays)
Entry point to C language function: void {@native glGenVertexArrays}(GLsizei n, GLuint * arrays) P...
void glBindVertexArray(int array)
Entry point to C language function: void {@native glBindVertexArray}(GLuint array) Part of GL_ARB_...
void glDrawArraysInstanced(int mode, int first, int count, int instancecount)
Entry point to C language function: void {@native glDrawArraysInstanced}(GLenum mode,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL setGL(GL gl)
Sets the GL pipeline object this GLAutoDrawable uses.
GL4 getGL4()
Casts this object to the GL4 interface.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void glGenBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glGenBuffers}(GLsizei n, GLuint * buffers) Part ...
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
static final int GL_TRIANGLES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLES" with expre...
static final int GL_VERSION
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VERSION" with express...
static final int GL_FLOAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expressio...
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
String glGetString(int name)
Entry point to C language function: const GLubyte * {@native glGetString}(GLenum name) Part of GL_...
static final int GL_RENDERER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RENDERER" with expres...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_VENDOR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VENDOR" with expressi...
void glViewport(int x, int y, int width, int height)
Entry point to C language function: void {@native glViewport}(GLint x, GLint y, GLsizei width,...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
void glDeleteBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glDeleteBuffers}(GLsizei n, const GLuint * buffers...
void glBindBuffer(int target, int buffer)
Entry point to C language function: void {@native glBindBuffer}(GLenum target, GLuint buffer) Part...
void glBufferData(int target, long size, Buffer data, int usage)
Entry point to C language function: void {@native glBufferData}(GLenum target, GLsizeiptr size,...
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.