TextRenderer Problem - Missing Letters in Render
Posted by Ben on Oct 09, 2016; 11:59pm
URL: https://forum.jogamp.org/TextRenderer-Problem-Missing-Letters-in-Render-tp4037297.html
Hello and thanks in advance for your help. I am trying to use the text render with glList so that my text is loaded once and then displayed as needed, where needed, etc. I am using
private TextRenderer TR = new TextRenderer( new Font("SansSerif", Font.PLAIN, 36));
for the renderer. The list creation function (the returned integer is the list index):
public int glListText3D(GL2 gl, int id, String text, float[] pos, float scale, float[] color) {
int txt = gl.glGenLists(id);
gl.glNewList(txt,GL2.GL_COMPILE);
gl.glNormal3d(0.0, 0.0, 1.0);
TR.setColor(color[0],color[1],color[2],color[3]);
TR.begin3DRendering();
TR.draw3D(text, pos[0], pos[1], pos[2], scale);
TR.end3DRendering();
gl.glEndList();
return txt;
}
I keep a list of indexes (TextGL) by textual keyword so I can reference the ID as required. The display() code for publishing the text:
gl.glPushMatrix();
// rotation and translation for camera viewing
gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f); gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f); // rotation
gl.glTranslatef(coordinate_calculate(crds[0]),coordinate_calculate(crds[1]) + (0.5f * size[1]),coordinate_calculate(crds[2]));
// Scale and Color
gl.glScalef((float)ES.font_scale()*world_size(true),(float)ES.font_scale()*world_size(true),(float)ES.font_scale()*world_size(true));
gl.glColor4f(fnt_clr[0],fnt_clr[1],fnt_clr[2],fnt_clr[3]); // TODO - this does nothing
// Text
gl.glCallList(TextGL.get(labels.get(i)));
gl.glPopMatrix();
The problem I am having is that the results are missing letters. Specifically, E,L,N (and some others).
Another side problem - if I try and use gl.glColor4f() prior to the CallList I get no change in color. Scale and translation work fine.
Sorry this isn't a full code post - the full code is 10,000+ lines and not public.
=====================UPDATE================
Here is a full code example of what is happening. Any help would be appreciated.
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.awt.TextRenderer;
public class figureText extends GLCanvas implements GLEventListener {
private static final long serialVersionUID = 1L;
private TextRenderer TR = new TextRenderer( new Font("SansSerif", Font.PLAIN, 12));
private FPSAnimator animator = null;
private Hashtable<String,Integer> TextGL = new Hashtable<String,Integer>();
private Random random = new Random();
private String[] names = new String[]{"Robert Smith","John Jones","Lawrence Spiegal","Maria Xenoblobazama","This is a really really long name"};
private float view_x = 0.0f, view_y = 0.0f, view_z = 1.0f, world_size = 150.0f;
private boolean start = false;
public static void main(String[] args) {
JFrame window = new JFrame(); window.setBounds(new Rectangle(200,200,800,800));
GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); caps.setDoubleBuffered(true); caps.setHardwareAccelerated(true);
JPanel ogwpanel = new JPanel(new BorderLayout()); final figureText OGW = new figureText(caps); ogwpanel.add(OGW,BorderLayout.CENTER);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(ogwpanel); window.setVisible(true);
}
public figureText(GLCapabilities caps) {
super(caps); addGLEventListener(this);
animator = new FPSAnimator(this,60); animator.start();
setVisible(true);
}
private void setup_data(GL2 gl) {
for(int i=0;i<names.length;i++) {
float[] crds = new float[]{random.nextFloat()*100.0f,random.nextFloat()*100.0f,random.nextFloat()*100.0f};
TextGL.put(names[i],glListText3D(gl,(i+1),names[i],crds,1.0f,new float[]{1.0f,1.0f,1.0f,1.0f}));
}
}
@Override public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
if(!start) { setup_data(gl); start = true; }
gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity();
float vn = 2.0f * world_size * view_z; gl.glOrtho(-vn,vn,-vn,vn,-vn*3.0f,vn*3.0f); // uses world_size without ES.world_size() to allow visual scaling
gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatef(view_x, view_y, -56.0f); // position change
gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glPushMatrix();
Enumeration<String> keys = TextGL.keys(); while(keys.hasMoreElements()) {
gl.glCallList(TextGL.get(keys.nextElement()));
}
gl.glPopMatrix();
gl.glFlush();
}
@Override public void dispose(GLAutoDrawable drawable) { }
@Override public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context
// Setup the environment
gl.glEnable(GL2.GL_DEPTH_TEST); // enables depth testing
gl.glDepthFunc(GL2.GL_LEQUAL); // the type of depth test to do
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); // best perspective correction
gl.glShadeModel(GL2.GL_SMOOTH); // blends colors nicely, and smoothes out lighting
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, new float[]{0.0f,0.0f,0.0f,0.0f},0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, new float[]{ 1.0f, 1.0f, 1.0f, 1.0f }, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, new float[]{ 1.0f, 1.0f, 1.0f, 1.0f }, 0);
gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
}
@Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
// This allows things to remain centered and square irrelevant of the window size
int size = width; if (height < size) { size = height; }
int cX = 0, cY = 0;
if(width < height) { cY = (height - width) / 2; }
else if (height < width) { cX = (width - height) / 2; }
GL2 gl = drawable.getGL().getGL2(); gl.glViewport(cX,cY,size,size);
}
public int glListText3D(GL2 gl, int id, String text, float[] pos, float scale, float[] color) {
int txt = gl.glGenLists(id);
gl.glNewList(txt,GL2.GL_COMPILE);
gl.glNormal3d(0.0, 0.0, 1.0);
TR.setColor(color[0],color[1],color[2],color[3]);
TR.begin3DRendering();
TR.draw3D(text, pos[0], pos[1], pos[2], scale);
TR.end3DRendering();
gl.glEndList();
return txt;
}
}