Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2014 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/11134970 to your computer and use it in GitHub Desktop.
Save anonymous/11134970 to your computer and use it in GitHub Desktop.
JOGL curve textrenderer
import java.io.IOException;
import javax.media.opengl.GL4;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
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.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.math.geom.AABBox;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.PMVMatrix;
public class TextRendering implements GLEventListener{
private RenderState renderState;
private RegionRenderer renderer;
private TextRegionUtil textRenderUtil;
private Font font;
private int texSize[] = new int[] { 0 };
private String fps;
@Override
public void init(GLAutoDrawable drawable) {
GL4 gl = drawable.getGL().getGL4();
try {
font = FontFactory.get(FontFactory.UBUNTU).getDefault();
} catch (IOException e) {
e.printStackTrace();
}
renderState = RenderState.createRenderState(SVertex.factory());
renderer = RegionRenderer.create(renderState, RegionRenderer.defaultBlendEnable, RegionRenderer.defaultBlendDisable);
renderState.setHintMask(RenderState.BITHINT_BLENDING_ENABLED);
textRenderUtil = new TextRegionUtil(0);
gl.glClearColor(1f, 1f, 1f, 1f);
renderer.init(gl, 0);
renderer.reshapeOrtho(drawable.getWidth(), drawable.getHeight(), 0.1f, 1000f);
}
@Override
public void display(GLAutoDrawable drawable) {
GL4 gl = drawable.getGL().getGL4();
gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT);
String text = "Hello World!";
int fontSize = 28;
AABBox textBox = font.getMetricBounds(text, fontSize);
float x = (drawable.getWidth() - textBox.getWidth())/2;
float y = (drawable.getHeight() - textBox.getHeight())/2;
float z = -1000f;
float position[] = new float[]{ x, y, z };
float color[] = new float[] { 0f, 1f, 0f, 1f};
renderString(gl, text, fontSize, color, position);
fps = Float.toString(drawable.getAnimator().getLastFPS());
AABBox fpsBox = font.getMetricBounds(fps, fontSize);
x = 0f;
y = (drawable.getHeight() - fpsBox.getHeight());
z = -1000f;
position = new float[] { x, y, z };
color = new float[] { 1f, 0f, 0f, 1f };
renderString(gl, fps, fontSize, color, position);
}
private void renderString(GL4 gl, String text, int fontSize, float color[], float position[]){
final PMVMatrix pmv = renderer.getMatrix();
pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
pmv.glLoadIdentity();
pmv.glTranslatef(position[0], position[1], position[2]);
renderState.setColorStatic(color[0], color[1], color[2], color[3]);
textRenderUtil.drawString3D(gl, renderer, font, fontSize, text, null, texSize);
}
@Override
public void dispose(GLAutoDrawable drawable) {
GL4 gl = drawable.getGL().getGL4();
renderer.destroy(gl);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
renderer.reshapeOrtho(drawable.getWidth(), drawable.getHeight(), 0.1f, 1000f);
}
public static void main(String args[]){
final GLProfile glp = GLProfile.get(GLProfile.GL4);
final GLCapabilities caps = new GLCapabilities(glp);
caps.setSampleBuffers(true);
caps.setNumSamples(4);
final GLWindow glWindow = GLWindow.create(caps);
final Animator animator = new Animator(glWindow);
animator.setUpdateFPSFrames(1, null);
glWindow.addWindowListener(new WindowAdapter() {
@Override
public void windowDestroyNotify(WindowEvent arg0) {
new Thread() {
@Override
public void run() {
if (animator.isStarted())
animator.stop();
System.exit(0);
}
}.start();
}
});
glWindow.addGLEventListener(new TextRendering());
glWindow.setSize(800, 400);
glWindow.setVisible(true);
animator.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment