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();
}





.3ds loader for java/processing

Happy new year 2009

I’ve wanted to load 3d data into my processing projects before, so i wrote a simple obj loader with multiple groups, something that no other library supported at that time. It was simple and had bugs but it worked quite good for what i needed. So after sometime i needed more data for my models but i was too lazy to port my Chaos 3dsmax exporter called Chucky. So, i have  searched the web and i found MRI‘s old java port of his 3ds loader. I still remember his demos and its tutorials back  in the old days… Great coder, miss those days.

Anyway, i’ve downloaded his library and placed in it the processing library’s folder and *poof*, i had a 3ds loader to work with. And it has alot of cool features.

Jeep
3d model from http://www.punkuser.net.
Library credits go to MRI.

In the package i’ve included an example project that renders simply vertex and wireframe face data. It should be easy for anyone with some knowledge to take the documentation and render a full textured/lit’ed scenario. If not, just you wait until i get a new example to put in the pack

Homepage: http://www.pixelnerve.com/processing/libraries/mri3ds
(old) Download MRI 3DS Loader + Processing Examples
(Tested in Processing 1.0.1)

———————————————————-
EDIT:

3ds files dont store face/vertex normals, so i had to compute them myself. ive uploaded updates to the same zip file.

news:
. face+vertex normal calculation (vertex by averaging face normals)
. no smoothing groups used.
. normals debugging
. renders meshes with texture+solid faces.
. mouse+camera control
. extra code from Vitamin

enjoy.





, Next