Saturday, September 19, 2009

groovyとJOGLでビットマップ文字列を描画する

groovyとJOGLでビットマップ文字列を描画するには、以下のコードを実行します。


import static javax.media.opengl.GL.*;
import java.io.*;
import javax.media.opengl.*;
import com.sun.opengl.util.*;

// 出力画像サイズ
width = 300
height = 300

GLDrawableFactory gldf =
GLDrawableFactory.getFactory()
GLCapabilities glc = new GLCapabilities()
glc.setDoubleBuffered(false)
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null)
GL gl = buf.getGL()

buf.addGLEventListener(
[ init: {
// 背景色
gl.glClearColor(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float,
1f)
},

display: {
GLUT glut = new GLUT()
gl.glViewport(0, 0, width, height)

// 透視投影
gl.glMatrixMode(GL_PROJECTION)
gl.glLoadIdentity()
float ratio = height/width as Float
gl.glFrustum(-1.0f, 1.0f, -ratio, ratio,
5.0f, 40.0f)

gl.glMatrixMode(GL_MODELVIEW)
gl.glLoadIdentity()
gl.glTranslatef(0.0f, 0.0f, -12.0f)

gl.glClear(GL_COLOR_BUFFER_BIT)

gl.glPushMatrix()
// ビットマップ文字の描画
gl.glRasterPos2f(-1f, 0.0f)

glut.glutBitmapCharacter(GLUT.BITMAP_8_BY_13, 'A' as char)
glut.glutBitmapCharacter(GLUT.BITMAP_9_BY_15, 'B' as char)
glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_10, 'C' as char)
glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_12, 'D' as char)
glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'E' as char)
glut.glutBitmapCharacter(GLUT.BITMAP_TIMES_ROMAN_10, 'F' as char)
glut.glutBitmapCharacter(GLUT.BITMAP_TIMES_ROMAN_24, 'G' as char)
// ビットマップ文字列の描画
glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, "123");
gl.glPopMatrix()
},

reshape: {},
displayChanged: {}
] as GLEventListener
)
GLContext context =
buf.createContext(null)
context.makeCurrent()
buf.display()
Screenshot.writeToFile(
new File("sample1283a.png"),
width, height, true)
context.release()
context.destroy()


出力画像(sample1283a.png)
groovyとJOGLで描画したビットマップ文字列

動作環境
JDK1.6 Update14, Groovy 1.6.3, JOGL 1.1.1a

関連情報
groovyとJOGLのまとめ

No comments:

Post a Comment