V

Avatar

Text render for java/processing using opengl

I found out a nice way to write text in Processing with java+opengl.  It easily renders 2d and 3d text. It should be also easy to draw billboard text in 3d space. Here is the source code and an example on how to use it in processing:


import java.text.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;

class VTextRenderer
{
VTextRenderer( String fontName, int size )
{
_fontName = fontName;
_fontSize = size;
_textRender = new TextRenderer( new Font(fontName, Font.TRUETYPE_FONT, size), true, true, null, true );
_textRender.setColor( 1.0f, 1.0, 1.0, 1.0 );
//_textRender.setUseVertexArrays( true );
}

VTextRenderer( String fontName, int size, boolean antialiased, boolean mipmap )
{
_fontName = fontName;
_fontSize = size;
_textRender = new TextRenderer( new Font(fontName, Font.TRUETYPE_FONT, size), antialiased, true, null, mipmap );
_textRender.setColor( 1.0f, 1.0, 1.0, 1.0 );
//_textRender.setUseVertexArrays( true );
}

void print( String str, int x, int y )
{
_textRender.beginRendering( width, height, true );
_textRender.draw( str, x, y );
_textRender.endRendering();
_textRender.flush();
}

void print( String str, float x, float y, float z )
{
print( str, x, y, z, 1.0f );
}

void print( String str, float x, float y, float z, float s )
{
_textRender.begin3DRendering();
_textRender.draw3D( str, x, y, z, s );
_textRender.end3DRendering();
_textRender.flush();
}

void setColor( float c )
{
setColor( c, c, c, 1 );
}

void setColor( float c, float a )
{
setColor( c, c, c, a );
}

void setColor( float r, float g, float b )
{
setColor( r, g, b, 1 );
}

void setColor( float r, float g, float b, float a )
{
_textRender.setColor( r, g, b, a );
}

void setSmoothing( boolean flag )
{
_textRender.setSmoothing( flag );
}

/// ____________________________________________________
/// Members
int _w, _h;

String _fontName;
int _fontSize;
TextRenderer _textRender;
Font font;
}


import processing.opengl.*;

int WIDTH = 800;
int HEIGHT = 600;
float aspectRatio = WIDTH/(float)HEIGHT;

VTextRenderer textRender;

///___________________________________________________________
void setup()
{
size( WIDTH, HEIGHT, OPENGL );
hint( ENABLE_OPENGL_4X_SMOOTH );
frameRate( 60 );

//  logger = new VLog( this, “log.txt” );

aspectRatio = width/(float)height;

//textRender = new VTextRenderer( “Arial”, 100 );
textRender = new VTextRenderer( “Arial”, 100, true, true );
}

///___________________________________________________________
void draw()
{
// Write in 2d
background( 0 );
textRender.print( “vitamin”, 0, 100 );

// Write in 3d
perspective( PI*0.25,  aspectRatio, 1, 1000 );
camera( 0, 0, 100, 0, 0, 0, 0, 1, 0 );
scale( 1, -1, 1 );  // oh well. processing likes the upside down way, i dont know why.
((PGraphicsOpenGL)g).beginGL();
textRender.print( “vitamin”, 0, 0, sin(millis()*0.001)*100, 1/10.0 );
((PGraphicsOpenGL)g).endGL();
}

///___________________________________________________________
void stop()
{
super.stop();
}

No Comments, Comment or Ping

Reply to “Text render for java/processing using opengl”

You must be logged in to post a comment.