28package com.jogamp.opengl.test.junit.jogl.tile;
30import com.jogamp.newt.event.TraceKeyAdapter;
31import com.jogamp.newt.event.TraceWindowAdapter;
32import com.jogamp.newt.event.awt.AWTKeyAdapter;
33import com.jogamp.newt.event.awt.AWTWindowAdapter;
34import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
35import com.jogamp.opengl.test.junit.util.QuitAdapter;
36import com.jogamp.opengl.test.junit.util.UITestCase;
37import com.jogamp.opengl.util.Animator;
38import com.jogamp.opengl.util.GLPixelBuffer;
39import com.jogamp.opengl.util.RandomTileRenderer;
40import com.jogamp.opengl.util.TileRendererBase;
41import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
42import com.jogamp.opengl.util.texture.TextureData;
43import com.jogamp.opengl.util.texture.TextureIO;
45import java.awt.Dimension;
48import java.io.IOException;
49import java.lang.reflect.InvocationTargetException;
51import com.jogamp.nativewindow.util.PixelFormat;
52import com.jogamp.opengl.GL;
53import com.jogamp.opengl.GLAutoDrawable;
54import com.jogamp.opengl.GLCapabilities;
55import com.jogamp.opengl.GLEventListener;
56import com.jogamp.opengl.awt.GLCanvas;
58import org.junit.Assert;
59import org.junit.FixMethodOrder;
61import org.junit.runners.MethodSorters;
81@FixMethodOrder(MethodSorters.NAME_ASCENDING)
83 static long duration = 3500;
84 static int width = 512;
85 static int height = 512;
88 public void test01_aa0() throws IOException, InterruptedException, InvocationTargetException {
92 public void test02_aa8() throws IOException, InterruptedException, InvocationTargetException {
96 void doTest(
final int msaaCount)
throws IOException, InterruptedException, InvocationTargetException {
103 final Frame frame =
new Frame(
"Gears AWT Test");
104 Assert.assertNotNull(frame);
106 final GLCanvas glad =
new GLCanvas(caps);
107 Assert.assertNotNull(glad);
108 final Dimension glc_sz =
new Dimension(width, height);
109 glad.setMinimumSize(glc_sz);
110 glad.setPreferredSize(glc_sz);
111 glad.setSize(glc_sz);
114 final Gears gears =
new Gears();
115 glad.addGLEventListener( gears );
117 final QuitAdapter quitAdapter =
new QuitAdapter();
118 new AWTKeyAdapter(
new TraceKeyAdapter(quitAdapter), glad).addTo(glad);
119 new AWTWindowAdapter(
new TraceWindowAdapter(quitAdapter), glad).addTo(frame);
122 final int maxTileSize = 64;
123 final int imageWidth = 256 * 6;
124 final int imageHeight = 256 * 4;
127 final RandomTileRenderer renderer =
new RandomTileRenderer();
128 renderer.setImageSize(imageWidth, imageHeight);
129 final GLPixelBuffer.GLPixelBufferProvider pixelBufferProvider = GLPixelBuffer.defaultProviderWithRowStride;
130 final boolean[] flipVertically = {
false };
131 final boolean[] rendererActive = {
true };
134 final int w = maxTileSize, h = maxTileSize;
139 final GL gl = drawable.
getGL();
140 final PixelFormat.Composition hostPixelComp = pixelBufferProvider.getHostPixelComp(gl.
getGLProfile(), 3);
141 final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, 3,
true);
142 final GLPixelBuffer pixelBuffer = pixelBufferProvider.allocate(gl, hostPixelComp, pixelAttribs,
true, imageWidth, imageHeight, 1, 0);
143 renderer.setImageBuffer(pixelBuffer);
145 flipVertically[0] =
false;
147 flipVertically[0] =
true;
149 System.err.println(
"XXX pre-init: "+renderer);
152 public void dispose(
final GLAutoDrawable drawable) {}
154 public void display(
final GLAutoDrawable drawable) {
155 if( dx+w <= imageWidth && dy+h <= imageHeight ) {
156 renderer.setTileRect(dx, dy, w, h);
158 if( dx + w > imageWidth ) {
162 }
else if( rendererActive[0] ) {
163 rendererActive[0] =
false;
165 System.err.println(
"XXX pre-display: "+renderer);
168 public void reshape(
final GLAutoDrawable drawable,
final int x,
final int y,
final int width,
final int height) {}
170 final GLEventListener postTileGLEL =
new GLEventListener() {
172 public void init(
final GLAutoDrawable drawable) {}
174 public void dispose(
final GLAutoDrawable drawable) {}
176 public void display(
final GLAutoDrawable drawable) {
177 if( !rendererActive[0] ) {
178 final GLPixelBuffer imageBuffer = renderer.getImageBuffer();
180 System.err.println(
"XXX !active -> save");
181 System.err.println(
"XXX post-display: "+renderer);
182 final TextureData textureData =
new TextureData(
185 imageWidth, imageHeight,
187 imageBuffer.pixelAttributes,
193 final String filename = getSnapshotFilename(0,
"-tile", glad.getChosenGLCapabilities(), imageWidth, imageHeight,
false, TextureIO.PNG,
null);
194 final File file =
new File(filename);
195 TextureIO.write(textureData, file);
196 }
catch (
final IOException e) {
199 renderer.detachAutoDrawable();
200 System.err.println(
"XXX post-display detached: "+renderer);
201 drawable.getGL().glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
202 glad.getGLEventListener(0).reshape(drawable, 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
206 public void reshape(
final GLAutoDrawable drawable,
final int x,
final int y,
final int width,
final int height) {}
208 renderer.setGLEventListener(preTileGLEL, postTileGLEL);
210 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
213 frame.setVisible(
true);
216 final Animator animator =
new Animator(glad);
217 animator.setUpdateFPSFrames(60, System.err);
220 boolean signalTileRenderer =
true;
222 while(!quitAdapter.shouldQuit() && animator.isAnimating() &&
223 ( rendererActive[0] || animator.getTotalFPSDuration()<duration ) )
225 if( signalTileRenderer && animator.getTotalFPSDuration() > 90 ) {
226 signalTileRenderer =
false;
228 System.err.println(
"XXX START TILE RENDERING");
229 renderer.attachAutoDrawable(glad);
234 Assert.assertNotNull(frame);
235 Assert.assertNotNull(glad);
236 Assert.assertNotNull(animator);
239 Assert.assertEquals(
false, animator.isAnimating());
240 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
242 frame.setVisible(
false);
244 Assert.assertEquals(
false, frame.isVisible());
245 javax.swing.SwingUtilities.invokeAndWait(
new Runnable() {
252 public static void main(
final String args[]) {
253 for(
int i=0; i<args.length; i++) {
254 if(args[i].equals(
"-time")) {
257 duration = Integer.parseInt(args[i]);
258 }
catch (
final Exception ex) { ex.printStackTrace(); }
Specifies a set of OpenGL capabilities.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
final GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
void setSampleBuffers(final boolean enable)
Defaults to false.
Demos an onscreen AWT GLCanvas being used for RandomTileRenderer rendering to produce a PNG file.
static void main(final String args[])
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.