28package com.jogamp.opengl.test.junit.jogl.acore;
30import java.io.IOException;
31import java.nio.FloatBuffer;
32import java.nio.ShortBuffer;
33import java.util.logging.Level;
34import java.util.logging.Logger;
36import com.jogamp.opengl.GL;
37import com.jogamp.opengl.GL2ES2;
38import com.jogamp.opengl.GL3;
39import com.jogamp.opengl.GL3bc;
40import com.jogamp.opengl.GLAutoDrawable;
41import com.jogamp.opengl.GLCapabilities;
42import com.jogamp.opengl.GLEventListener;
43import com.jogamp.opengl.GLException;
44import com.jogamp.opengl.GLProfile;
46import org.junit.Assert;
48import org.junit.FixMethodOrder;
49import org.junit.runners.MethodSorters;
51import com.jogamp.common.nio.Buffers;
52import com.jogamp.newt.opengl.GLWindow;
53import com.jogamp.opengl.test.junit.util.MiscUtils;
54import com.jogamp.opengl.test.junit.util.QuitAdapter;
55import com.jogamp.opengl.test.junit.util.UITestCase;
56import com.jogamp.opengl.util.Animator;
57import com.jogamp.opengl.util.GLBuffers;
79@FixMethodOrder(MethodSorters.NAME_ASCENDING)
81 static long duration = 500;
88 void display(
final GL3VAODemo t,
final GL3bc gl) {
89 t.displayCPUSourcing(gl);
96 void display(
final GL3VAODemo t,
final GL3bc gl) {
104 void display(
final GL3VAODemo t,
final GL3bc gl) {
112 private final Mode[] allModes;
113 private Mode currentMode;
114 private int currentModeIdx;
116 public GL3VAODemo(
final Mode[] modes) {
118 currentMode = allModes[0];
122 private final static float[] vertexColorData =
new float[]{
123 0.0f, 0.75f, 0.0f, 1,0,0,
124 -0.5f, -0.75f, 0.0f, 0,1,0,
125 0.9f, -0.75f, 0.0f, 0,0,1
127 private final FloatBuffer vertexColorDataBuffer =
GLBuffers.newDirectFloatBuffer(vertexColorData);
129 private final short[] indices =
new short[]{0, 1, 2};
130 private final ShortBuffer indicesBuffer =
GLBuffers.newDirectShortBuffer(indices);
133 private int ibo = -1;
134 private int vbo = -1;
135 private int vertID = -1;
136 private int fragID = -1;
137 private int progID = -1;
139 private int vao = -1;
141 private static int createShader(
final GL3 gl,
final int type,
142 final String[] srcLines){
145 final int[] lengths =
new int[srcLines.length];
146 for (
int i = 0; i < srcLines.length; i++) {
147 lengths[i] = srcLines[i].length();
149 gl.
glShaderSource(shaderID, srcLines.length, srcLines, lengths, 0);
154 private void initBuffers(
final GL3 gl) {
156 final int[] buffArray =
new int[2];
175 private void initShaders(
final GL3 gl) {
176 final String[] vertSrc =
new String[]{
178 "in vec4 vPosition;\n",
180 "out vec4 pColor;\n",
182 " pColor = vColor;\n",
183 " gl_Position = vPosition;\n",
186 vertID = createShader(gl, GL2ES2.GL_VERTEX_SHADER, vertSrc);
188 final String[] fragSrc =
new String[]{
192 " gl_FragColor = pColor;\n",
195 fragID = createShader(gl, GL2ES2.GL_FRAGMENT_SHADER, fragSrc);
208 private int initVAO(
final GL3 gl) {
209 final int[] buff =
new int[1];
210 gl.glGenVertexArrays(1, buff, 0);
211 final int vao = buff[0];
212 Assert.assertTrue(
"Invalid VAO: "+vao, vao > 0);
215 gl.glUseProgram(progID);
216 final int posLoc = gl.glGetAttribLocation(progID,
"vPosition");
217 final int colorLoc = gl.glGetAttribLocation(progID,
"vColor");
220 gl.glBindVertexArray(vao);
221 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
222 gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, ibo);
224 gl.glEnableVertexAttribArray(posLoc);
225 gl.glEnableVertexAttribArray(colorLoc);
227 final int stride = 6 * Buffers.SIZEOF_FLOAT;
228 final int cOff = 3 * Buffers.SIZEOF_FLOAT;
229 gl.glVertexAttribPointer(posLoc, 3, GL.GL_FLOAT,
false, stride, 0L);
230 gl.glVertexAttribPointer(colorLoc,3, GL.GL_FLOAT,
false, stride, cOff);
232 gl.glBindVertexArray(0);
234 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
235 gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
240 public void init(
final GLAutoDrawable drawable) {
241 final GL3 gl = drawable.getGL().getGL3();
242 gl.glEnable(GL.GL_DEPTH_TEST);
243 gl.glDisable(GL.GL_CULL_FACE);
249 gl.setSwapInterval(1);
253 public void dispose(
final GLAutoDrawable drawable) {
254 final GL3 gl = drawable.getGL().getGL3();
255 gl.glDeleteBuffers(2,
new int[]{vbo, ibo}, 0);
256 gl.glDetachShader(progID, fragID);
257 gl.glDetachShader(progID, vertID);
258 gl.glDeleteProgram(progID);
259 gl.glDeleteShader(fragID);
260 gl.glDeleteShader(vertID);
263 private void displayCPUSourcing(
final GL3bc gl) {
264 final int posLoc = gl.glGetAttribLocation(progID,
"vPosition");
265 final int colorLoc = gl.glGetAttribLocation(progID,
"vColor");
266 gl.glEnableVertexAttribArray(posLoc);
267 gl.glEnableVertexAttribArray(colorLoc);
269 final int stride = 6 * Buffers.SIZEOF_FLOAT;
271 gl.glVertexAttribPointer(posLoc, 3, GL.GL_FLOAT,
false, stride, vertexColorDataBuffer);
272 vertexColorDataBuffer.position(3);
273 gl.glVertexAttribPointer(colorLoc,3, GL.GL_FLOAT,
false, stride, vertexColorDataBuffer);
274 vertexColorDataBuffer.position(0);
276 gl.glDrawElements(GL.GL_TRIANGLES, 3, GL.GL_UNSIGNED_SHORT, indicesBuffer);
278 gl.glDisableVertexAttribArray(posLoc);
279 gl.glDisableVertexAttribArray(colorLoc);
282 private void displayVBOOnly(
final GL3 gl) {
283 final int posLoc = gl.glGetAttribLocation(progID,
"vPosition");
284 final int colorLoc = gl.glGetAttribLocation(progID,
"vColor");
285 gl.glEnableVertexAttribArray(posLoc);
286 gl.glEnableVertexAttribArray(colorLoc);
288 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
289 final int stride = 6 * Buffers.SIZEOF_FLOAT;
290 final int cOff = 3 * Buffers.SIZEOF_FLOAT;
291 gl.glVertexAttribPointer(posLoc, 3, GL.GL_FLOAT,
false, stride, 0L);
292 gl.glVertexAttribPointer(colorLoc,3, GL.GL_FLOAT,
false, stride, cOff);
293 gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, ibo);
294 gl.glDrawElements(GL.GL_TRIANGLES, 3, GL.GL_UNSIGNED_SHORT, 0L);
296 gl.glDisableVertexAttribArray(posLoc);
297 gl.glDisableVertexAttribArray(colorLoc);
298 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
299 gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
302 private void displayVBOVAO(
final GL3 gl) {
304 gl.glBindVertexArray(vao);
305 gl.glDrawElements(GL.GL_TRIANGLES, 3, GL.GL_UNSIGNED_SHORT, 0L);
306 gl.glBindVertexArray(0);
307 }
catch (
final GLException ex) {
308 Logger.getLogger(TestBug692GL3VAONEWT.class.getName()).log(Level.SEVERE,
null,ex);
313 public void display(
final GLAutoDrawable drawable) {
314 final GL3bc gl = drawable.getGL().getGL3bc();
315 final float color = ((float) currentMode.ordinal() + 1) / (Mode.values().length + 2);
316 gl.glClearColor(color, color, color, 0);
317 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
318 gl.glUseProgram(progID);
321 currentModeIdx = ( currentModeIdx + 1 ) % allModes.length;
322 newMode = allModes[ currentModeIdx ];
324 if (newMode != currentMode) {
325 currentMode = newMode;
326 System.out.println(
"Display mode: " + currentMode);
333 public void reshape(
final GLAutoDrawable drawable,
final int x,
final int y,
final int w,
final int h) {
337 private void testImpl(
final GLProfile profile,
final GL3VAODemo.Mode[] modes)
throws InterruptedException {
338 final GLCapabilities capabilities =
new GLCapabilities(profile);
339 final GLWindow glWindow = GLWindow.create(capabilities);
340 glWindow.setSize(512, 512);
342 final Animator anim =
new Animator(glWindow);
344 final QuitAdapter quitAdapter =
new QuitAdapter();
345 glWindow.addKeyListener(quitAdapter);
346 glWindow.addWindowListener(quitAdapter);
348 final GL3VAODemo vaoTest =
new GL3VAODemo(modes);
349 glWindow.addGLEventListener(vaoTest);
350 glWindow.setVisible(
true);
353 final long t0 = System.currentTimeMillis();
355 while(!quitAdapter.shouldQuit() && t1-t0<duration) {
357 t1 = System.currentTimeMillis();
367 System.err.println(
"GL3bc n/a");
370 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.CPU_SRC };
377 System.err.println(
"GL3bc n/a");
380 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.VBO_ONLY };
387 System.err.println(
"GL3bc n/a");
390 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.VBO_VAO };
397 System.err.println(
"GL3bc n/a");
400 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.CPU_SRC, GL3VAODemo.Mode.VBO_ONLY };
407 System.err.println(
"GL3bc n/a");
410 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.CPU_SRC, GL3VAODemo.Mode.VBO_VAO };
417 System.err.println(
"GL3bc n/a");
420 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.VBO_ONLY, GL3VAODemo.Mode.VBO_VAO };
427 System.err.println(
"GL3bc n/a");
430 final GL3VAODemo.Mode[] modes =
new GL3VAODemo.Mode[] { GL3VAODemo.Mode.CPU_SRC, GL3VAODemo.Mode.VBO_ONLY, GL3VAODemo.Mode.VBO_VAO };
434 public static void main(
final String args[])
throws IOException {
435 for(
int i=0; i<args.length; i++) {
436 if(args[i].equals(
"-time")) {
441 org.junit.runner.JUnitCore.
main(tstname);
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Specifies the the OpenGL profile.
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL3bc
The desktop OpenGL compatibility profile 3.x, with x >= 1, ie GL2 plus GL3.
Test Vertex Array Object (VAO) Usage and BufferStateTracker.
void test13CPUSourceAndVBOVAO()
static void main(final String args[])
void test12CPUSourceAndVBOOnly()
void test23VBOOnlyAndVBOVAO()
static int atoi(final String str, final int def)
Utility routines for dealing with direct buffers.
Different modes of displaying the geometry.
abstract void display(GL3VAODemo t, GL3bc gl)
void glCompileShader(int shader)
Entry point to C language function: void {@native glCompileShader}(GLuint shader) Part of GL_ES_VE...
void glValidateProgram(int program)
Entry point to C language function: void {@native glValidateProgram}(GLuint program) Part of GL_ES...
int glCreateProgram()
Entry point to C language function: GLuint {@native glCreateProgram}() Part of GL_ES_VERSION_2_0,...
void glReleaseShaderCompiler()
Start: GL_ARB_ES2_compatibility functions, which are part of ES2 core as well.
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,...
int glCreateShader(int type)
Entry point to C language function: GLuint {@native glCreateShader}(GLenum type) Part of GL_ES_VER...
void glLinkProgram(int program)
Entry point to C language function: void {@native glLinkProgram}(GLuint program) Part of GL_ES_VER...
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_ELEMENT_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ELEME...
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...