Re: TextRenderer - my text won't show
Posted by Deedee on Sep 06, 2013; 6:27pm
URL: https://forum.jogamp.org/TextRenderer-my-text-won-t-show-tp4029291p4030001.html
Here is the code. This is the same example that LordSmoke posted - axis and text.
import com.jogamp.opengl.util.Animator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
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.common.nio.Buffers;
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_Text_Axis implements GLEventListener
{
private TextRenderer tren;
private ShaderState shaderState;
private int fontSet = FontFactory.UBUNTU;
private Font font;
private GLCanvas canvas;
private static GL3 gl;
private float[] vertexArray = new float[]{
-150f, 0f, 0f,
150f, 0f, 0f,
0f, -150f, 0f,
0f, 150f, 0f,
0f, 0f, -150f,
0f, 0f, 150f};
private float[] colorArray = new float[]{
1f, 0f, 1f,
1f, 0f, 1f,
1f, 0f, 1f,
1f, 0f, 1f,
1f, 0f, 1f,
1f, 0f, 1f};
private int iVertex2D;
private int iFragment2D;
private int iProgram2D;
private int iPosition_2D;
private int iColor_2D;
private int iMVP_2D;
private static FloatBuffer dataVertexBuffer;
private static FloatBuffer dataColorBuffer;
private static float[] MVP = new float[]{
200f, 0f, 0f, 0f,
0f, 200f, 0f, 0f,
0f, 0f, -2.333333f, 149.99994f,
0f, 0f, -1f, 350f};
private static IntBuffer intBuffer;
private static IntBuffer intVAOBuffer;
private static int[] iVao = new int[2];
public static void main(String[] args)
{
new MVis_Text_Axis();
}
public MVis_Text_Axis()
{
Frame frame = new Frame("Text + Axis");
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(800, 800);
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);
intVAOBuffer = Buffers.newDirectIntBuffer(2);
gl.glGenVertexArrays(2, intVAOBuffer);
iVao[0] = intVAOBuffer.get(0);
gl.glBindVertexArray(iVao[0]);
/* -------------------- 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 -------------------------------*/
gl.glBindVertexArray(0);
iVertex2D = initVertexShader(gl, loadShaderFile("color2D.vert"));
iFragment2D = initFragmentShader(gl, loadShaderFile("color2D.frag"));
iProgram2D = initShaderProgram(gl, iVertex2D, iFragment2D);
iMVP_2D = gl.glGetUniformLocation(iProgram2D, "MVP");
iPosition_2D = gl.glGetAttribLocation(iProgram2D, "position");
iColor_2D = gl.glGetAttribLocation(iProgram2D, "color");
dataVertexBuffer = Buffers.newDirectFloatBuffer(18);
dataColorBuffer = Buffers.newDirectFloatBuffer(18);
for (int i = 0; i < 18; i++)
{
dataVertexBuffer.put((float)vertexArray[i]);
dataColorBuffer.put((float)colorArray[i]);
}
dataVertexBuffer.rewind();
dataColorBuffer.rewind();
iVao[1] = intVAOBuffer.get(1);
gl.glBindVertexArray(iVao[1]);
intBuffer = Buffers.newDirectIntBuffer(2);
gl.glGenBuffers(2, intBuffer);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, intBuffer.get(0));
gl.glBufferData(GL3.GL_ARRAY_BUFFER, dataVertexBuffer.capacity() * 4, dataVertexBuffer, GL3.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(iPosition_2D);
gl.glVertexAttribPointer(iPosition_2D, 3, GL3.GL_FLOAT, false, 0, 0);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, intBuffer.get(1));
gl.glBufferData(GL3.GL_ARRAY_BUFFER, dataColorBuffer.capacity() * 4, dataColorBuffer, GL3.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(iColor_2D);
gl.glVertexAttribPointer(iColor_2D, 3, GL3.GL_FLOAT, false, 0, 0);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
gl.glBindVertexArray(0);
}
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.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
gl.glUseProgram(shaderState.shaderProgram().program());
gl.glBindVertexArray(iVao[0]);
int[] size = {1, 1, 1};
float[] position = {-10, 0, 0};
tren.scale(gl, 0.1f, 0.1f, 0.1f);
tren.translate(gl, -5f+0f, 0f+0f, 0f+0f);
tren.drawString3D(gl, font, "Text Rendering Problems", position, 1, size);
tren.resetModelview(gl);
gl.glBindVertexArray(0);
gl.glUseProgram(iProgram2D);
gl.glUniformMatrix4fv(iMVP_2D, 1, false, MVP, 0);
gl.glBindVertexArray(iVao[1]);
gl.glDrawArrays(GL3.GL_LINES, 0, 6);
gl.glBindVertexArray(0);
}
public void dispose(GLAutoDrawable drawable)
{
gl.glDeleteProgram(iProgram2D);
gl.glDeleteShader(iVertex2D);
gl.glDeleteShader(iFragment2D);
}
private int initVertexShader(GL3 gl, String[] vp)
{
int vo = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
gl.glShaderSource(vo, 1, vp, null);
gl.glCompileShader(vo);
printShaderlog(gl, vo);
return vo;
}
private int initFragmentShader(GL3 gl, String[] fp)
{
int fo = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);
gl.glShaderSource(fo, 1, fp, null);
gl.glCompileShader(fo);
printShaderlog(gl, fo);
return fo;
}
private int initShaderProgram(GL3 gl, int vo, int fo)
{
int po = gl.glCreateProgram();
if (vo > 0)
{
gl.glAttachShader(po, vo);
}
if (fo > 0)
{
gl.glAttachShader(po, fo);
}
gl.glBindFragDataLocation(po, 0, "fragmentColor");
gl.glLinkProgram(po);
gl.glValidateProgram(po);
printProgramlog(gl, po);
return po;
}
private void printShaderlog(GL3 gl, int shader)
{
IntBuffer intBuffer = Buffers.newDirectIntBuffer(1);
gl.glGetShaderiv(shader, GL3.GL_INFO_LOG_LENGTH, intBuffer);
int infoLength = intBuffer.get(0);
if (infoLength > 1)
{
ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(infoLength);
gl.glGetShaderInfoLog(shader, infoLength, intBuffer, byteBuffer);
byteBuffer.rewind();
byte dst[] = new byte[byteBuffer.capacity()];
byteBuffer.get(dst, 0, byteBuffer.capacity());
String message = new String(dst);
gl.glDeleteShader(shader);
System.out.println(message);
throw new IllegalStateException(message);
}
}
private void printProgramlog(GL3 gl, int program)
{
IntBuffer intBuffer = Buffers.newDirectIntBuffer(1);
gl.glGetProgramiv(program, GL3.GL_INFO_LOG_LENGTH, intBuffer);
int infoLength = intBuffer.get(0);
if (infoLength > 1)
{
ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(infoLength);
gl.glGetProgramInfoLog(program, infoLength, intBuffer, byteBuffer);
byteBuffer.rewind();
byte dst[] = new byte[byteBuffer.capacity()];
byteBuffer.get(dst, 0, byteBuffer.capacity());
String message = new String(dst);
gl.glDeleteProgram(program);
System.out.println(message);
throw new IllegalStateException(message);
}
}
public String[] loadShaderFile(String fileName) throws IOException
{
String path = "/" + fileName;
InputStream inputStream = path.getClass().getResourceAsStream(path);
if (inputStream == null)
{
throw new IllegalStateException("Can not load shader file: " + path);
}
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for(String str = bufferedReader.readLine(); str != null; str = bufferedReader.readLine())
{
stringBuffer.append(str).append('\n');
}
String[] result = { stringBuffer.toString() };
inputStream.close();
bufferedReader.close();
return result;
}
}