Thursday, September 24, 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

def drawQuads(GL gl,
double p1x, double p1y, double p1z,
double p2x, double p2y, double p2z,
double p3x, double p3y, double p3z,
double p4x, double p4y, double p4z)
{
gl.glBegin(GL_QUADS)
gl.glNormal3d(p1x, p1y, p1z)
gl.glVertex3d(p1x, p1y, p1z)
gl.glNormal3d(p2x, p2y, p2z)
gl.glVertex3d(p2x, p2y, p2z)
gl.glNormal3d(p3x, p3y, p3z)
gl.glVertex3d(p3x, p3y, p3z)
gl.glNormal3d(p4x, p4y, p4z)
gl.glVertex3d(p4x, p4y, p4z)
gl.glEnd()
}

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(
0xf0/0xff as Float,
0xf0/0xff as Float,
0xf0/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, -10.0f)

gl.glEnable(GL_LIGHTING);
gl.glEnable(GL_LIGHT0);
gl.glEnable(GL_COLOR_MATERIAL);
gl.glEnable(GL_NORMALIZE);
gl.glEnable(GL_DEPTH_TEST);
// gl.glEnable(GL_CULL_FACE);
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

gl.glPushMatrix()
// 面の色を設定
gl.glColor3f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float
)
// X軸回転
gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(50.0f, 0.0f, 1.0f, 0.0f)

minr = 0.7
maxr = 1.0
slices = 16
lr=2*Math.PI/slices
dh = 0.5
th = 2.5

cx = new double[slices]
cy = new double[slices]

for(li in 0..slices-1){
x1 = minr*Math.cos(lr*(li+1))
y1 = minr*Math.sin(lr*(li+1))
x2 = maxr*Math.cos(lr*(li+1))
y2 = maxr*Math.sin(lr*(li+1))
x3 = maxr*Math.cos(lr*li)
y3 = maxr*Math.sin(lr*li)
x4 = minr*Math.cos(lr*li)
y4 = minr*Math.sin(lr*li)
cx[li] = x4
cy[li] = y4

drawQuads(gl,
x4, y4, -th/2,
x3, y3, -th/2+dh,
x2, y2, -th/2+dh,
x1, y1, -th/2)

drawQuads(gl,
x2, y2, -th/2+dh,
x3, y3, -th/2+dh,
x3, y3, th/2-dh,
x2, y2, th/2-dh)

drawQuads(gl,
x1, y1, th/2,
x2, y2, th/2-dh,
x3, y3, th/2-dh,
x4, y4, th/2)
}

gl.glBegin(GL_POLYGON)
gl.glNormal3d(0,0,-1)
for(li in 0..slices-1){
gl.glVertex3d(cx[li], cy[li], -th/2)
}
gl.glEnd()

gl.glBegin(GL_POLYGON)
gl.glNormal3d(0,0,1)
for(li in 0..slices-1){
gl.glVertex3d(cx[li], cy[li], th/2)
}
gl.glEnd()
gl.glPopMatrix()
},

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


出力画像(sample1288a.png)
groovyとJOGLで描画した樽の形

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

関連情報
groovyとJOGLのまとめ

No comments: