Re: TextRenderer - my text won't show
Posted by Deedee on Aug 30, 2013; 3:22pm
URL: https://forum.jogamp.org/TextRenderer-my-text-won-t-show-tp4029291p4029925.html
So the text is a part of a bigger program (of course) where I use several shader programs using several different shaders that I provide. Now I decided to write the smallest possible code (that I could think of) to render text without using any of my other stuff. I am providing the code below using Jogl 2 and OSX 10.8.4 and it works. I don't know yet why the text renderer doesn't agree with the rest of my code and I don't know why the problem doesn't occur under Linux. I haven't tried it on Windows yet. I am in the process of finding a Windows machine that supports OpenGL 3.0 or higher. I hope this code helps you and if you find out how to fix your code, please, let me know. It may solve my problem as well.
import com.jogamp.opengl.util.Animator;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.DebugGL3;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
// for text rendering
import com.jogamp.graph.curve.opengl.RenderState;
import com.jogamp.graph.curve.opengl.TextRenderer;
import com.jogamp.graph.geom.opengl.SVertex;
import com.jogamp.opengl.util.glsl.ShaderState;
import com.jogamp.graph.font.Font;
import com.jogamp.graph.font.FontFactory;
public class MVis_TextOnly implements GLEventListener
{
private TextRenderer tren;
private ShaderState shaderState;
private int fontSet = FontFactory.JAVA;
private Font font;
private GLCanvas canvas;
private static GL3 gl;
public static void main(String[] args)
{
new MVis_TextOnly();
}
public MVis_TextOnly()
{
Frame frame = new Frame("TextOnly");
GLCapabilities capabilities = new GLCapabilities(GLProfile.get(GLProfile.GL3));
canvas = new GLCanvas(capabilities);
canvas.addGLEventListener(this);
frame.add(canvas);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e) {
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setSize(1400, 700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable)
{
try
{
drawable.setGL(new DebugGL3(drawable.getGL().getGL3()));
gl = drawable.getGL().getGL3();
System.out.println("INIT GL3 IS: " + gl.getClass().getName());
gl.setSwapInterval(1);
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
gl.glEnable(GL3.GL_DEPTH_TEST);
/* -------------------- TEXTRENDERER---------------------------------------- */
shaderState = new ShaderState();
RenderState rs = RenderState.createRenderState(shaderState, SVertex.factory());
tren = TextRenderer.create(rs, 0);
tren.init(gl);
tren.setAlpha(gl, 1.0f);
tren.setColorStatic(gl, 0.0f, 0.0f, 0.0f);
font = FontFactory.get(fontSet).getDefault();
/* --------------- END OF TEXTRENDERER -------------------------------*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
gl = drawable.getGL().getGL3();
gl.glViewport(0, 0, width, height);
}
public void display(GLAutoDrawable drawable)
{
gl = drawable.getGL().getGL3();
gl.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
gl.glUseProgram(shaderState.shaderProgram().program());
int[] size = {1, 1, 1};
float[] position = {-10, 0, 0};
tren.scale(gl, 0.05f*1, (0.05f*2)*1, 0.05f*1);
tren.translate(gl, -5f+0f, 0f+0f, 0f+0f);
tren.drawString3D(gl, font, "My Text Goes Here", position, 1, size);
tren.resetModelview(gl);
gl.glUseProgram(0);
}
public void dispose(GLAutoDrawable drawable)
{
gl = drawable.getGL().getGL3();
}
}