import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.InputEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.media.opengl.DebugGL; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLJPanel; import javax.media.opengl.glu.GLU; import javax.swing.JComponent; import javax.swing.JFrame; import com.sun.opengl.util.GLUT; import com.sun.opengl.util.j2d.TextRenderer; public class JoglLabelTest { /** * @param args */ public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new JoglLabelTest.JoglLabelTestComponent()); f.pack(); f.setVisible(true); } public static class JoglLabelTestComponent extends JComponent implements GLEventListener, MouseWheelListener { Font labelFont = new Font("Arial", Font.PLAIN, 10); GLCanvas g3dHardware; GLJPanel g3dSoftware; GLDrawable g3d; boolean useHardware = false; public JoglLabelTestComponent() { GLCapabilities capabilities = new GLCapabilities(); capabilities.setHardwareAccelerated(true); // We want hardware acceleration capabilities.setDoubleBuffered(true); // And double buffering this.setLayout(new BorderLayout()); g3dHardware = new GLCanvas(capabilities); g3dHardware.addGLEventListener(this); g3dHardware.addMouseWheelListener(this); g3dHardware.setAutoSwapBufferMode(false); g3dSoftware = new GLJPanel(capabilities); g3dSoftware.setDoubleBuffered(false); g3dSoftware.addGLEventListener(this); g3dSoftware.addMouseWheelListener(this); if (useHardware) { this.add(g3dHardware, BorderLayout.CENTER); g3d = g3dHardware; } else { this.add(g3dSoftware, BorderLayout.CENTER); g3d = g3dSoftware; } } TextRenderer lastRenderer; public void display(GLAutoDrawable glDrawable) { final GL gl = glDrawable.getGL(); final GLU glu = new GLU(); final GLUT glut = new GLUT(); // Clear buffers gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); // ///////////////////// // // LABEL RENDERING // // ///////////////////// gl.glDisable(GL.GL_DEPTH_TEST); gl.glDisable(GL.GL_LIGHTING); if (lastRenderer != null) { lastRenderer.dispose(); } TextRenderer textPlain = new TextRenderer(labelFont, true, true); textPlain.beginRendering(glDrawable.getWidth(), glDrawable.getHeight()); textPlain.setColor(1, 1, 1, 1); textPlain.draw("This is a test label of tremendous length", 0, 0); textPlain.endRendering(); // don't dispose of this yet, so we can use -Djogl.debug.TextRenderer lastRenderer = textPlain; gl.glFlush(); // Rendering done, flush GL commands } public void mouseWheelMoved(MouseWheelEvent e) { int delta = e.getWheelRotation(); if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0) { float size = labelFont.getSize(); labelFont = labelFont.deriveFont(size + delta); repaint(); } } public void displayChanged(GLAutoDrawable glDrawable, boolean arg1, boolean arg2) { } public void init(GLAutoDrawable glDrawable) { glDrawable.setGL(new DebugGL(glDrawable.getGL())); GL gl = glDrawable.getGL();// Get the GL object from glDrawable GLU glu = new GLU(); gl.glClearColor(0, 0, 0, 0f); // White, when changing this, fading of filtered nodes must be updated as well float[] light_position = new float[] { 10f, 10f, 10f, 0f }; // w=0 for spot light float[] light_ambient = new float[] { 1, 1f, 1f, 1f }; // No ambient light float[] light_diffuse = new float[] { 1f, 1f, 1f, 1f }; // White diffuse light float[] light_specular = new float[] { 1f, 1f, 1f, 1f }; // White specular light // Set up the light gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position, 0); gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, light_ambient, 0); gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_diffuse, 0); gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light_specular, 0); gl.glEnable(GL.GL_LIGHT0); gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_DEPTH_TEST); gl.glEnable(GL.GL_NORMALIZE); gl.glEnable(GL.GL_CULL_FACE); gl.glCullFace(GL.GL_BACK); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glShadeModel(GL.GL_SMOOTH); } public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height) { } } }