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.math.FloatUtil;
11import com.jogamp.math.Matrix4f;
12import com.jogamp.math.Vec3f;
13import com.jogamp.opengl.DebugGL4;
14import com.jogamp.opengl.GL;
15import com.jogamp.opengl.GL2ES2;
16import com.jogamp.opengl.GL4;
17import com.jogamp.opengl.GLAutoDrawable;
18import com.jogamp.opengl.GLEventListener;
19import com.jogamp.opengl.GLException;
20import com.jogamp.opengl.GLUniformData;
21import com.jogamp.opengl.TraceGL4;
22import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
23import com.jogamp.opengl.util.GLArrayDataClient;
24import com.jogamp.opengl.util.GLArrayDataServer;
25import com.jogamp.opengl.util.PMVMatrix;
26import com.jogamp.opengl.util.glsl.ShaderCode;
27import com.jogamp.opengl.util.glsl.ShaderProgram;
28import com.jogamp.opengl.util.glsl.ShaderState;
33 private static final String shaderBasename =
"triangles";
35 private static final boolean useInterleaved =
true;
46 private static final int NO_OF_INSTANCE = 30;
47 private final FloatBuffer triangleTransform = FloatBuffer.allocate(16 * NO_OF_INSTANCE);
49 private final float[] rotationSpeed =
new float[NO_OF_INSTANCE];
51 private static final boolean useTraceGL =
false;
52 private PrintStream stream;
55 private boolean isInitialized =
false;
62 stream =
new PrintStream(
new FileOutputStream(
new File(
"instanced-with-st.txt")));
63 }
catch (
final IOException e1) {
71 private void initTransform() {
72 final Random rnd =
new Random();
74 for(
int i = 0; i < NO_OF_INSTANCE; i++) {
75 rotationSpeed[i] = 0.3f * rnd.nextFloat();
78 final float scale = 1f + 4 * rnd.nextFloat();
79 mat[i].
scale(scale, tmp);
81 mat[i].
translate(20f * rnd.nextFloat() - 10f,
82 10f * rnd.nextFloat() - 5f,
100 System.err.println(
"INIT GL IS: " + gl.getClass().getName());
109 if(!st.
uniform(gl, projectionMatrixUniform)) {
110 throw new GLException(
"Error setting mgl_PMatrix in shader: " + st);
113 transformMatrixUniform =
new GLUniformData(
"mgl_MVMatrix", 4, 4, triangleTransform);
116 if(!st.
uniform(gl, transformMatrixUniform)) {
117 throw new GLException(
"Error setting mgl_MVMatrix in shader: " + st);
121 initVBO_interleaved(gl);
123 initVBO_nonInterleaved(gl);
126 isInitialized =
true;
131 if(!isInitialized )
return;
140 float winScale = 0.1f;
141 if(view !=
null) winScale = view.
getScale();
142 projectionMatrix.
glScalef(winScale, winScale, winScale);
143 st.
uniform(gl, projectionMatrixUniform);
146 generateTriangleTransform();
147 st.
uniform(gl, transformMatrixUniform);
169 aspect = (float) width / (
float) height;
183 private void generateTriangleTransform() {
184 triangleTransform.clear();
186 for(
int i = 0; i < NO_OF_INSTANCE; i++) {
187 mat[i].
rotate(rotationSpeed[i], 0, 0, 1, tmp);
188 mat[i].
get(triangleTransform);
190 triangleTransform.rewind();
193 private void initVBO_nonInterleaved(
final GL4 gl) {
194 final int VERTEX_COUNT = 3;
197 final FloatBuffer verticeBuf = (FloatBuffer)verticesVBO.
getBuffer();
198 verticeBuf.put(vertices);
199 verticesVBO.
seal(gl,
true);
202 final FloatBuffer colorBuf = (FloatBuffer)colorsVBO.
getBuffer();
203 colorBuf.put(colors);
204 colorsVBO.
seal(gl,
true);
214 private void initVBO_interleaved(
final GL4 gl) {
215 final int VERTEX_COUNT = 3;
220 final FloatBuffer ib = (FloatBuffer)interleavedVBO.
getBuffer();
222 for(
int i = 0; i < VERTEX_COUNT; i++) {
223 ib.put(vertices, i*3, 3);
224 ib.put(colors, i*4, 4);
226 interleavedVBO.
seal(gl,
true);
232 private void initShader(
final GL4 gl) {
235 "shader",
"shader/bin", shaderBasename,
true);
237 "shader",
"shader/bin", shaderBasename,
true);
238 vp0.replaceInShaderSource(
"NO_OF_INSTANCE", String.valueOf(NO_OF_INSTANCE));
240 vp0.defaultShaderCustomization(gl,
true,
true);
241 fp0.defaultShaderCustomization(gl,
true,
true);
246 final ShaderProgram sp =
new ShaderProgram();
249 if(!sp.link(gl, System.err)) {
250 throw new GLException(
"Couldn't link program: "+sp);
254 st =
new ShaderState();
259 private static final float[] vertices = {
265 private final float[] colors = {
266 1.0f, 0.0f, 0.0f, 1.0f,
267 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).
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
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 dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
TriangleInstancedRendererWithShaderState(final IInstancedRenderingView view)
static GLArrayDataClient createGLSL(final String name, final int comps, final int dataType, final boolean normalized, final int initialElementCount)
Create a client side buffer object, using a custom GLSL array attribute name and starting with a new ...
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data starting with a new created Buffer object with initialEl...
GLArrayDataWrapper addGLSLSubArray(final String name, final int comps, final int vboTarget)
Configure a segment of this GLSL interleaved array (see createGLSLInterleaved(int,...
Buffer getBuffer()
The Buffer holding the data, may be null if a GPU buffer without client bound data.
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.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
void ownAttribute(final GLArrayData attribute, final boolean own)
Binds or unbinds the GLArrayData lifecycle to this ShaderState.
synchronized void useProgram(final GL2ES2 gl, final boolean on)
Turns the shader program on or off.
synchronized boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)
Attach or switch a shader program.
synchronized void destroy(final GL2ES2 gl)
Calls release(gl, true, true, true).
boolean uniform(final GL2ES2 gl, final GLUniformData data)
Set the uniform data, if it's location is valid, i.e.
void ownUniform(final GLUniformData uniform)
Bind the GLUniform lifecycle to this ShaderState.
void glClearDepth(double depth)
Aliased entrypoint of void {@native glClearDepth}(GLclampd depth); and void {@native glClearDepthf...
static final int GL_VERTEX_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_EXT_vertex_shader, GL_ARB_vertex_shader Alias for: GL_VERTEX_SH...
static final int GL_FRAGMENT_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_ATI_fragment_shader, GL_ARB_fragment_shader Alias for: GL_FRAGM...
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.
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...
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.