import com.jogamp.graph.curve.Region; import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.graph.curve.opengl.TextRegionUtil; import com.jogamp.graph.font.Font; import com.jogamp.graph.font.FontFactory; import com.jogamp.graph.geom.SVertex; import com.jogamp.newt.event.*; import com.jogamp.opengl.fixedfunc.GLMatrixFunc; import com.jogamp.opengl.util.PMVMatrix; import com.jogamp.opengl.util.awt.TextRenderer; import com.jogamp.nativewindow.WindowClosingProtocol; import com.jogamp.newt.NewtFactory; import com.jogamp.newt.Screen; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.opengl.*; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.util.Animator; import java.io.File; import java.io.IOException; import static com.jogamp.opengl.GL.GL_MULTISAMPLE; import static com.jogamp.opengl.GL2.GL_COLOR_BUFFER_BIT; public class FontTest implements GLEventListener, KeyListener{ static GLWindow window; static Animator animator; Font font; RegionRenderer renderer; TextRegionUtil util; TextRenderer m_textRenderer; boolean bOldMethod = false; double points[][] = new double[100][2]; long lastFrame = -1; int dropCount = 0; public static void main(String[] args) { new FontTest(); } public FontTest() { double xFactor = bOldMethod ? 1 : 1920; double yFactor = bOldMethod ? 1 : 1080; for (double[] pt : points) { pt[0] = Math.random() * xFactor; pt[1] = Math.random() * yFactor; } final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); capabilities.setAlphaBits(8); capabilities.setDoubleBuffered(true); capabilities.setHardwareAccelerated(true); capabilities.setSampleBuffers(true); capabilities.setNumSamples(2); GLCanvas glcanvas = new GLCanvas(capabilities); glcanvas.addGLEventListener(this); com.jogamp.newt.Display dpy = NewtFactory.createDisplay(null); Screen screen = NewtFactory.createScreen(dpy, 1); window = GLWindow.create(screen, capabilities); window.addGLEventListener(this); window.addKeyListener(this); window.setFullscreen(true); window.setPointerVisible(false); window.setAlwaysOnTop(true); window.setVisible(true); window.setDefaultCloseOperation(WindowClosingProtocol.WindowClosingMode.DISPOSE_ON_CLOSE); animator = new Animator(window); animator.setRunAsFastAsPossible(true); animator.start(); } @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); drawable.setAutoSwapBufferMode(false); gl.setSwapInterval(1); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glEnable(GL_MULTISAMPLE); try { font = FontFactory.get(new File("arial.ttf")); } catch (IOException e) { e.printStackTrace(); } RenderState renderState = RenderState.createRenderState(SVertex.factory()); renderState.setColorStatic(1, 0, 0, 1); renderState.setHintMask(RenderState.BITHINT_BLENDING_ENABLED); renderer = RegionRenderer.create(renderState, RegionRenderer.defaultBlendEnable, RegionRenderer.defaultBlendDisable); renderer.init(gl, Region.VBAA_RENDERING_BIT); util = new TextRegionUtil(Region.VBAA_RENDERING_BIT); m_textRenderer = new TextRenderer(new java.awt.Font("arial", java.awt.Font.PLAIN, 60), true, true); } @Override public void dispose(GLAutoDrawable glAutoDrawable) { } @Override public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL_COLOR_BUFFER_BIT); gl.glColor4f(1, 0, 0, 1); gl.glPushMatrix(); gl.glBegin(GL2.GL_TRIANGLES); gl.glVertex3d(0, 0, 0.0); gl.glVertex3d(-0.1, -0.2, 0.0); gl.glVertex3d(0.1, -0.2, 0.0); gl.glEnd(); gl.glPopMatrix(); if (bOldMethod) { gl.glColor4f(1, 1, 0, 1); for (double[] pt : points) { m_textRenderer.begin3DRendering(); m_textRenderer.draw3D("Test old", (float)pt[0], (float)pt[1], 0, 0.001f); m_textRenderer.end3DRendering(); } } else { PMVMatrix pmv = renderer.getMatrix(); pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); pmv.glLoadIdentity(); float pixelSize = font.getPixelSize(32, 96); for (double[] pt : points) { pmv.glPushMatrix(); pmv.glTranslatef((float)pt[0], (float)pt[1], -1); util.drawString3D(gl, renderer, font, pixelSize, "Test new", null, new int[]{2}); pmv.glPopMatrix(); } } drawable.swapBuffers(); gl.glFinish(); long now = System.currentTimeMillis(); if (lastFrame != -1 && now - lastFrame > 25) { System.out.println(dropCount + ". Dropped frame: " + (now - lastFrame) + "ms"); dropCount++; } lastFrame = now; } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { GL2 gl = drawable.getGL().getGL2(); renderer.enable(gl, true); renderer.reshapeOrtho(w, h, 0.1f, 1000.0f); renderer.enable(gl, false); } @Override public void keyPressed(KeyEvent keyEvent) { if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) { animator.stop(); window.setVisible(false); this.dispose(window); new Thread(() -> window.destroy()); } } @Override public void keyReleased(KeyEvent keyEvent) { } }