Re: TextRenderer - my text won't show
Posted by LordSmoke on Sep 05, 2013; 5:54pm
URL: https://forum.jogamp.org/TextRenderer-my-text-won-t-show-tp4029291p4029991.html
Below is the code Lili/Deedee sent me. Three files: .frag, .vert, and .java. It should render text and draw three lines. The text does not appear. I believe that if you reverse the order of rendering the text appears, but not the lines. This code worked on Linux and the older libraries, but fails in the same manner on Linux and Mac with the newer libraries. Below is also the output from an exception it occassionally (but not always) throws. I suppose it is either something really simple we are not getting or it reveals an underlying bug in the textrenderer. Either way, I am hopeful the community will spot something we haven't. -LS
======= color2D.frag
#version 150 core
in vec3 Color;
out vec4 fragmentColor;
void main(void)
{
fragmentColor = vec4(Color, 1.0);
}
======= color2D.vert
#version 150 core
uniform mat4 MVP;
in vec3 position;
in vec3 color;
out vec3 Color;
void main(void)
{
Color = color;
gl_Position = MVP * vec4(position, 1.0) ;
}
======= MVis_Text_Axis.java
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 static GL4bcImpl 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;
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();
//gl = (GL4bcImpl) drawable.getGL().getGL4bc();
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 -------------------------------*/
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");
intVAOBuffer = Buffers.newDirectIntBuffer(1);
gl.glGenVertexArrays(1, intVAOBuffer);
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 = intVAOBuffer.get(0);
gl.glBindVertexArray(iVao);
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);
}
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(iProgram2D);
gl.glUniformMatrix4fv(iMVP_2D, 1, false, MVP, 0);
gl.glBindVertexArray(iVao);
gl.glDrawArrays(GL3.GL_LINES, 0, 6);
gl.glBindVertexArray(0);
gl.glUseProgram(shaderState.shaderProgram().program());
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);
}
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;
}
}
======= Exception
Exception in thread "main-AWTAnimator" java.lang.RuntimeException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glViewport(<int> 0x0, <int> 0x0, <int> 0x318, <int> 0x304): GL_INVALID_OPERATION ( 1282 0x502),
at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:100)
at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:205)
at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
at javax.media.opengl.Threading.invoke(Threading.java:191)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:483)
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:440)
at com.jogamp.opengl.util.Animator$MainLoop.run(Animator.java:197)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glViewport(<int> 0x0, <int> 0x0, <int> 0x318, <int> 0x304): GL_INVALID_OPERATION ( 1282 0x502),
at javax.media.opengl.DebugGL4bc.checkGLGetError(DebugGL4bc.java:28003)
at javax.media.opengl.DebugGL4bc.glViewport(DebugGL4bc.java:26731)
at jogamp.opengl.GLDrawableHelper.reshape(GLDrawableHelper.java:605)
at jogamp.opengl.GLDrawableHelper.reshape(GLDrawableHelper.java:613)
at javax.media.opengl.awt.GLCanvas$7.run(GLCanvas.java:1050)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1034)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:909)
at javax.media.opengl.awt.GLCanvas$8.run(GLCanvas.java:1065)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)