28package com.jogamp.opengl.test.junit.graph;
31import java.io.IOException;
33import java.util.Locale;
35import com.jogamp.opengl.GL;
36import com.jogamp.opengl.GL2ES2;
37import com.jogamp.opengl.GLAnimatorControl;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLCapabilities;
40import com.jogamp.opengl.GLCapabilitiesImmutable;
41import com.jogamp.opengl.GLException;
42import com.jogamp.opengl.GLProfile;
43import com.jogamp.opengl.GLRunnable;
45import org.junit.Assert;
46import org.junit.FixMethodOrder;
48import org.junit.runners.MethodSorters;
50import com.jogamp.common.os.Platform;
51import com.jogamp.graph.curve.Region;
52import com.jogamp.graph.curve.opengl.GLRegion;
53import com.jogamp.graph.curve.opengl.RegionRenderer;
54import com.jogamp.graph.font.Font;
55import com.jogamp.graph.font.FontFactory;
56import com.jogamp.graph.font.FontScale;
57import com.jogamp.junit.util.JunitTracer;
58import com.jogamp.newt.Window;
59import com.jogamp.newt.opengl.GLWindow;
60import com.jogamp.opengl.test.junit.util.MiscUtils;
61import com.jogamp.opengl.test.junit.util.UITestCase;
62import com.jogamp.opengl.util.Animator;
63import com.jogamp.opengl.util.GLReadBufferUtil;
71@FixMethodOrder(MethodSorters.NAME_ASCENDING)
73 static final boolean DEBUG =
false;
74 static final boolean TRACE =
false;
75 static long Duration = 2000;
76 static int win_width = 1024;
77 static int win_height = 640;
78 static boolean WaitStartEnd =
false;
79 static boolean TextAnim =
false;
80 static int SceneMSAASamples = 0;
81 static int GraphVBAASamples = 0;
82 static int GraphMSAASamples = 0;
83 static boolean ManualTest =
false;
84 static int SwapInterval = 1;
86 static String fontFileName =
null;
87 static URL fontURL =
null;
88 static int fontSet = 0;
89 static int fontFamily = 0;
90 static int fontStylebits = 0;
91 static float fontSizeFixed = 14f;
93 static int atoi(
final String a) {
95 return Integer.parseInt(a);
96 }
catch (
final Exception ex) {
throw new RuntimeException(ex); }
99 public static void main(
final String args[])
throws IOException {
100 ManualTest = args.length > 0;
101 for(
int i=0; i<args.length; i++) {
102 if(args[i].equals(
"-time")) {
104 Duration = atoi(args[i]);
105 }
else if(args[i].equals(
"-width")) {
107 win_width = atoi(args[i]);
108 }
else if(args[i].equals(
"-height")) {
110 win_height = atoi(args[i]);
111 }
else if(args[i].equals(
"-fontURL")) {
113 fontURL =
new URL(args[i]);
114 }
else if(args[i].equals(
"-fontFile")) {
116 fontFileName = args[i];
117 }
else if(args[i].equals(
"-fontSet")) {
119 fontSet = atoi(args[i]);
120 }
else if(args[i].equals(
"-fontFamily")) {
122 fontFamily = atoi(args[i]);
123 }
else if(args[i].equals(
"-fontStyle")) {
125 fontStylebits = atoi(args[i]);
126 }
else if(args[i].equals(
"-fontSize")) {
128 fontSizeFixed = atoi(args[i]);
129 }
else if(args[i].equals(
"-smsaa")) {
131 SceneMSAASamples = atoi(args[i]);
132 }
else if(args[i].equals(
"-gmsaa")) {
134 GraphMSAASamples = atoi(args[i]);
135 }
else if(args[i].equals(
"-gvbaa")) {
137 GraphVBAASamples = atoi(args[i]);
138 }
else if(args[i].equals(
"-textAnim")) {
140 }
else if(args[i].equals(
"-vsync")) {
143 }
else if(args[i].equals(
"-wait")) {
147 System.err.println(
"Font [set "+fontSet+
", family "+fontFamily+
", style "+fontStylebits+
", size "+fontSizeFixed+
"], fontFileName "+fontFileName);
148 System.err.println(
"Scene MSAA Samples "+SceneMSAASamples);
149 System.err.println(
"Graph MSAA Samples "+GraphMSAASamples);
150 System.err.println(
"Graph VBAA Samples "+GraphVBAASamples);
151 System.err.println(
"swapInterval "+SwapInterval);
153 org.junit.runner.JUnitCore.
main(tstname);
156 static void sleep() {
159 static void sleep(
final long d) {
161 System.err.println(
"** new frame ** (sleep: "+d+
"ms)");
163 }
catch (
final InterruptedException ie) {}
166 static void destroyWindow(
final GLWindow window) {
172 static GLWindow createWindow(
final String title,
final GLCapabilitiesImmutable caps,
final int width,
final int height) {
173 Assert.assertNotNull(caps);
175 final GLWindow window = GLWindow.create(caps);
176 window.setSize(width, height);
177 window.setPosition(10, 10);
178 window.setTitle(title);
179 Assert.assertNotNull(window);
180 window.setVisible(
true);
188 testImpl(SceneMSAASamples, GraphMSAASamples, GraphVBAASamples);
216 public void testImpl(
final int sceneMSAASamples,
final int graphMSAASamples,
final int graphVBAASamples)
throws InterruptedException {
220 if( 0 < sceneMSAASamples ) {
224 System.err.println(
"Requested: "+caps+
", graph[msaaSamples "+graphMSAASamples+
", vbaaSamples "+graphVBAASamples+
"]");
226 final GLWindow window = createWindow(
"text-gvbaa"+graphVBAASamples+
"-gmsaa"+graphMSAASamples+
"-smsaa"+sceneMSAASamples, caps, win_width, win_height);
231 JunitTracer.waitForKey(
"Start");
234 final int renderModes, sampleCount;
235 if( graphVBAASamples > 0 ) {
237 sampleCount = graphVBAASamples;
238 }
else if ( graphMSAASamples > 0 ) {
240 sampleCount = graphMSAASamples;
245 final TextRendererGLEL textGLListener =
new TextRendererGLEL(glp, renderModes, sampleCount);
246 System.err.println(textGLListener.getFontInfo());
259 textGLListener.printScreen(renderModes, drawable,
"./",
"TestTextRendererNEWT00-snap"+screenshot_num,
false);
261 }
catch (
final Exception e) {
269 JunitTracer.waitForKey(
"Stop");
271 destroyWindow(window);
273 int screenshot_num = 0;
275 static final String textX2 =
276 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec sapien tellus. \n"+
277 "Ut purus odio, rhoncus sit amet commodo eget, ullamcorper vel urna. Mauris ultricies \n"+
278 "quam iaculis urna cursus ornare. Nullam ut felis a ante ultrices ultricies nec a elit. \n"+
279 "In hac habitasse platea dictumst. Vivamus et mi a quam lacinia pharetra at venenatis est.\n"+
280 "Morbi quis bibendum nibh. Donec lectus orci, sagittis in consequat nec, volutpat nec nisi.\n"+
281 "Donec ut dolor et nulla tristique varius. In nulla magna, fermentum id tempus quis, semper \n"+
282 "in lorem. Maecenas in ipsum ac justo scelerisque sollicitudin. Quisque sit amet neque lorem,\n" +
283 "-------Press H to change text---------\n";
287 private final GLRegion regionFPS, regionFPSAnim;
289 final float fontSizeMin, fontSizeMax;
291 float fontSizeAnim, fontSizeDelta;
294 TextRendererGLEL(
final GLProfile glp,
final int renderModes,
final int sampleCount) {
295 super(renderModes,
new int[] { sampleCount });
296 setRendererCallbacks(RegionRenderer.defaultBlendEnable, RegionRenderer.defaultBlendDisable);
298 regionFPS = GLRegion.
create(glp, renderModes,
null, 0, 0);
299 regionFPSAnim = GLRegion.
create(glp, renderModes,
null, 0, 0);
300 if(
null != fontURL ) {
303 _font = FontFactory.get(fontURL.openStream(),
true);
304 }
catch (
final IOException e) {
308 }
else if(
null != fontFileName ) {
311 _font = FontFactory.get(getClass(), fontFileName,
false);
312 }
catch (
final IOException e) {
317 font = getFont(fontSet, fontFamily, fontStylebits);
320 staticRGBAColor[0] = 0.1f;
321 staticRGBAColor[1] = 0.1f;
322 staticRGBAColor[2] = 0.1f;
323 staticRGBAColor[3] = 1.0f;
325 this.screenshot =
new GLReadBufferUtil(
false,
false);
327 fontSizeMin = fontSizeFixed;
328 fontSizeMax = fontSizeFixed+8;
329 fontSizeAnim = fontSizeFixed;
330 fontSizeDelta = 0.01f;
334 public void init(
final GLAutoDrawable drawable) {
335 super.init(drawable);
336 final GL2ES2 gl = drawable.getGL().getGL2ES2();
337 gl.setSwapInterval(SwapInterval);
338 gl.glEnable(GL.GL_DEPTH_TEST);
339 t0 = Platform.currentTimeMillis();
341 final Window win = (Window)drawable.getUpstreamWidget();
342 final float[] pixelsPerMM = win.getPixelsPerMM(
new float[2]);
343 final float[] dotsPerInch = FontScale.ppmmToPPI(pixelsPerMM,
new float[2]);
344 dpiV = dotsPerInch[1];
345 ppmmV = pixelsPerMM[1];
346 System.err.println(getFontInfo());
347 System.err.println(
"fontSize "+fontSizeFixed+
", dotsPerMM "+pixelsPerMM[0]+
"x"+pixelsPerMM[1]+
", dpi "+dotsPerInch[0]+
"x"+dotsPerInch[1]+
", pixelSize "+FontScale.toPixels(fontSizeFixed, dotsPerInch[1] ));
351 public void dispose(
final GLAutoDrawable drawable) {
352 final GL2ES2 gl = drawable.getGL().getGL2ES2();
356 super.dispose(drawable);
359 public void printScreen(
final int renderModes,
final GLAutoDrawable drawable,
final String dir,
final String objName,
final boolean exportAlpha)
throws GLException, IOException {
360 final String modeS = Region.getRenderModeString(renderModes);
361 final String bname = String.format((Locale)
null,
"%s-msaa%02d-fontsz%02.1f-%03dx%03d-%s%04d", objName,
362 drawable.getChosenGLCapabilities().getNumSamples(),
363 fontSizeFixed, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), modeS, vbaaSampleCount[0]);
364 final String filename = dir + bname +
".png";
365 if(screenshot.
readPixels(drawable.getGL(),
false)) {
366 screenshot.
write(
new File(filename));
370 String getFontInfo() {
371 final float pixelSize = FontScale.toPixels(fontSizeFixed, dpiV);
372 final float mmSize = pixelSize / ppmmV;
374 return String.format(
"Resolution dpiV %.2f, %.2f px/mm%nFont %s, unitsPerEM %d, size %.2f pt %.2f px %2f mm%n",
380 public void display(
final GLAutoDrawable drawable) {
381 final GL2ES2 gl = drawable.getGL().getGL2ES2();
383 gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
384 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
386 final GLAnimatorControl anim = drawable.getAnimator();
387 final float lfps =
null != anim ? anim.getLastFPS() : 0f;
388 final float tfps =
null != anim ? anim.getTotalFPS() : 0f;
392 final long t1 = Platform.currentTimeMillis();
396 fontSizeAnim += fontSizeDelta;
397 if( fontSizeMin >= fontSizeAnim || fontSizeAnim >= fontSizeMax ) {
398 fontSizeDelta *= -1f;
401 final float pixelSize = FontScale.toPixels(fontSizeFixed, dpiV);
402 final float pixelSizeAnim = FontScale.toPixels(fontSizeAnim, dpiV);
404 final String modeS = Region.getRenderModeString(renderModes);
414 final String text1 = lfps+
" / "+tfps+
" fps, vsync "+gl.getSwapInterval()+
", elapsed "+(t1-t0)/1000.0+
415 " s, fontSize "+fontSizeFixed+
", msaa "+drawable.getChosenGLCapabilities().getNumSamples()+
416 ", "+modeS+
"-samples "+vbaaSampleCount[0];
417 renderString(drawable, font, pixelSize, text1, 0, 0, 0, 0, -1000, regionFPS.
clear(gl));
419 final String text1 = String.format(
"%03.1f/%03.1f fps, vsync %d, elapsed %4.1f s, fontSize %2.2f, msaa %d, %s-samples %d",
420 lfps, tfps, gl.getSwapInterval(), (t1-t0)/1000.0, fontSizeFixed,
421 drawable.getChosenGLCapabilities().getNumSamples(), modeS, vbaaSampleCount[0]);
422 renderString(drawable, font, pixelSize, getFontInfo(), 0, 0, 0, 0, -1000,
true);
423 renderString(drawable, font, pixelSize,
"012345678901234567890123456789", 0, 0, 0, -1000,
true);
424 renderString(drawable, font, pixelSize,
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 0, 0, 0, -1000,
true);
425 renderString(drawable, font, pixelSize,
"Hello World", 0, 0, 0, -1000,
true);
426 renderString(drawable, font, pixelSize,
"4567890123456", 4, 0, 0, -1000,
true);
427 renderString(drawable, font, pixelSize,
"I like JogAmp", 4, 0, 0, -1000,
true);
428 renderString(drawable, font, pixelSize,
"Hello World", 0, 0, 0, -1000,
true);
429 renderString(drawable, font, pixelSize, textX2, 0, 0, 0, -1000,
true);
430 renderString(drawable, font, pixelSize, text1, 0, 0, 0, -1000, regionFPS.
clear(gl));
432 renderString(drawable, font, pixelSizeAnim, text1, 0, 0, 0, -1000, regionFPSAnim.
clear(gl));
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
static final int MSAA_RENDERING_BIT
Rendering-Mode bit for Region.
static final int VBAA_RENDERING_BIT
Rendering-Mode bit for Region.
A GLRegion is the OGL binding of one or more OutlineShapes Defined by its vertices and generated tria...
final GLRegion clear(final GL2ES2 gl)
Clears all buffers, i.e.
final void destroy(final GL2ES2 gl)
Delete and clear the associated OGL objects.
static GLRegion create(final GLProfile glp, int renderModes, final TextureSequence colorTexSeq, final int pass2TexUnit, final int initialVerticesCount, final int initialIndicesCount)
Create a GLRegion using the passed render mode.
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Specifies a set of OpenGL capabilities.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
void setSampleBuffers(final boolean enable)
Defaults to false.
Specifies the the OpenGL profile.
static final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
TestTextRendererNEWT10 Variant.
void testImpl(final int sceneMSAASamples, final int graphMSAASamples, final int graphVBAASamples)
static void main(final String args[])
static int atoi(final String str, final int def)
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
final synchronized boolean stop()
Stops this animator.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
void write(final File dest)
Write the TextureData filled by readPixels(GLAutoDrawable, boolean) to file.
void dispose(final GL gl)
boolean readPixels(final GL gl, final boolean mustFlipVertically)
Read the drawable's pixels to TextureData and Texture, if requested at construction.
int getUnitsPerEM()
Returns the font's units per EM from the 'head' table.
Interface wrapper for font implementation.
String getFullFamilyName()
Shall return the family and subfamily name, separated a dash.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
boolean invoke(boolean wait, GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...