JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
MultisampleDemoES2.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40
41package com.jogamp.opengl.test.junit.jogl.demos.es2;
42
43import com.jogamp.opengl.GL;
44import com.jogamp.opengl.GL2ES2;
45import com.jogamp.opengl.GLAutoDrawable;
46import com.jogamp.opengl.GLEventListener;
47import com.jogamp.opengl.GLUniformData;
48import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
49
50import com.jogamp.opengl.util.ImmModeSink;
51import com.jogamp.opengl.util.PMVMatrix;
52import com.jogamp.opengl.util.glsl.ShaderCode;
53import com.jogamp.opengl.util.glsl.ShaderProgram;
54import com.jogamp.opengl.util.glsl.ShaderState;
55
56public class MultisampleDemoES2 implements GLEventListener {
57
58 private boolean multisample, clearBuffers;
59 private final ShaderState st;
60 private final PMVMatrix pmvMatrix;
61 private ShaderProgram sp0;
62 private GLUniformData pmvMatrixUniform;
63 private ImmModeSink immModeSink;
64
65 public MultisampleDemoES2(final boolean multisample) {
66 this.multisample = multisample;
67 this.clearBuffers = true;
68 st = new ShaderState();
69 st.setVerbose(true);
70 pmvMatrix = new PMVMatrix();
71 }
72
73 public void setClearBuffers(final boolean v) { clearBuffers = v; }
74
75 @Override
76 public void init(final GLAutoDrawable glad) {
77 final GL2ES2 gl = glad.getGL().getGL2ES2();
78
79 System.err.println();
80 System.err.println("req. msaa: "+multisample);
81 System.err.println("Requested: " + glad.getNativeSurface().getGraphicsConfiguration().getRequestedCapabilities());
82 multisample = multisample && glad.getChosenGLCapabilities().getNumSamples() > 0 ;
83 System.err.println("Chosen : " + glad.getChosenGLCapabilities());
84 System.err.println("has msaa: "+multisample);
85 System.err.println();
86
88 "shader/bin", "mgl_default_xxx", true);
90 "shader/bin", "mgl_default_xxx", true);
91 vp0.defaultShaderCustomization(gl, true, true);
92 fp0.defaultShaderCustomization(gl, true, true);
93
94 sp0 = new ShaderProgram();
95 sp0.add(gl, vp0, System.err);
96 sp0.add(gl, fp0, System.err);
97 st.attachShaderProgram(gl, sp0, true);
98
99 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv());
100 st.ownUniform(pmvMatrixUniform);
101 st.uniform(gl, pmvMatrixUniform);
102
103 // Using predef array names, see
104 // GLPointerFuncUtil.getPredefinedArrayIndexName(glArrayIndex);
105 immModeSink = ImmModeSink.createGLSL(40,
106 3, GL.GL_FLOAT, // vertex
107 4, GL.GL_FLOAT, // color
108 0, GL.GL_FLOAT, // normal
109 0, GL.GL_FLOAT, // texCoords
110 GL.GL_STATIC_DRAW, st);
111 final int numSteps = 20;
112 final double increment = Math.PI / numSteps;
113 final double radius = 1;
114 immModeSink.glBegin(GL.GL_LINES);
115 for (int i = numSteps - 1; i >= 0; i--) {
116 immModeSink.glVertex3f((float) (radius * Math.cos(i * increment)),
117 (float) (radius * Math.sin(i * increment)),
118 0f);
119 immModeSink.glColor4f( 1f, 1f, 1f, 1f );
120 immModeSink.glVertex3f((float) (-1.0 * radius * Math.cos(i * increment)),
121 (float) (-1.0 * radius * Math.sin(i * increment)),
122 0f);
123 immModeSink.glColor4f( 1f, 1f, 1f, 1f );
124 }
125 immModeSink.glEnd(gl, false);
126
127 st.useProgram(gl, false);
128 }
129
130 @Override
131 public void dispose(final GLAutoDrawable glad) {
132 final GL2ES2 gl = glad.getGL().getGL2ES2();
133 immModeSink.destroy(gl);
134 immModeSink = null;
135 st.destroy(gl);
136 }
137
138 @Override
139 public void display(final GLAutoDrawable glad) {
140 final GL2ES2 gl = glad.getGL().getGL2ES2();
141 if (multisample) {
143 }
144 if( clearBuffers ) {
145 gl.glClearColor(0, 0, 0, 0);
146 // gl.glEnable(GL.GL_DEPTH_TEST);
147 // gl.glDepthFunc(GL.GL_LESS);
149 }
150
151 st.useProgram(gl, true);
152
153 immModeSink.draw(gl, true);
154
155 st.useProgram(gl, false);
156 }
157
158 // Unused routines
159 @Override
160 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
161 System.err.println("reshape ..");
162 final GL2ES2 gl = glad.getGL().getGL2ES2();
164 pmvMatrix.glLoadIdentity();
165 // pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
166 pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
168 pmvMatrix.glLoadIdentity();
169
170 st.useProgram(gl, true);
171 st.uniform(gl, pmvMatrixUniform);
172 st.useProgram(gl, false);
173 }
174
175 public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {
176 }
177}
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
void display(final GLAutoDrawable glad)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void init(final GLAutoDrawable glad)
Called by the drawable immediately after the OpenGL context is initialized.
void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged)
void reshape(final GLAutoDrawable glad, 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.
final void glVertex3f(final float x, final float y, final float z)
static ImmModeSink createGLSL(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final int glBufferUsage, final ShaderState st)
Uses a GL2ES2 GLSL shader immediate mode sink, utilizing the given ShaderState.
void draw(final GL gl, final boolean disableBufferAfterDraw)
final void glColor4f(final float x, final float y, final float z, final float a)
final void glEnd(final GL gl)
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void glOrthof(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the current matrix with the orthogonal matrix.
Definition: PMVMatrix.java:469
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
Convenient shader code class to use and instantiate vertex or fragment programs.
Definition: ShaderCode.java:75
final int defaultShaderCustomization(final GL2ES2 gl, final boolean preludeVersion, final boolean addDefaultPrecision)
Default customization of this shader source code.
static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class<?> context, final String[] sourceFiles, final boolean mutableStringBuilder)
Creates a complete ShaderCode object while reading all shader source of sourceFiles,...
synchronized void add(final ShaderCode shaderCode)
Adds a new shader to this program.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
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.
CapabilitiesImmutable getRequestedCapabilities()
Return the capabilities used to choose this graphics configuration.
AbstractGraphicsConfiguration getGraphicsConfiguration()
Returns the graphics configuration corresponding to this window.
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...
Definition: GL2ES2.java:39
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...
Definition: GL2ES2.java:541
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
int getNumSamples()
Returns the number of sample buffers to be allocated if sample buffers are enabled,...
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_MULTISAMPLE
Common in ES1, GL2 and GL3.
Definition: GL.java:1249
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...
Definition: GL.java:673
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...
Definition: GL.java:786
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...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
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...
Definition: GL.java:738
static final int GL_LINES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LINES" with expressio...
Definition: GL.java:430
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.