Wednesday, October 21, 2009

groovyとJOGLで複数の球の文字列を描画する

groovyとJOGLで複数の球の文字列を描画するには、以下のコードを実行します。


import static javax.media.opengl.GL.*;
import java.io.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
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(
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(-6.0f, 1.0f, -35.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(70.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(10.0f, 0.0f, 1.0f, 0.0f)

/* 文字列描画用イメージを作成 */
message = "JOGL"
Font font = new Font(Font.SANS_SERIF,
Font.PLAIN, 10)
FontRenderContext frc = new FontRenderContext(
new AffineTransform(), false as boolean,
false as boolean)
TextLayout layout = new TextLayout(
message, font, frc)

BufferedImage img = new BufferedImage(
(int)layout.getBounds().getWidth(),
(int)layout.getAscent() +
(int)layout.getDescent(),
BufferedImage.TYPE_INT_ARGB)


Graphics2D gr = img.createGraphics();
gr.setColor(new Color(0x00, 0x00, 0x00, 0x00))
gr.fill(new Rectangle2D.Double(0,0,
img.getWidth(),img.getHeight()))
/* 文字列を描画 */
gr.setColor(new Color(0xff, 0xff, 0xff))
gr.setFont(font)
layout.draw(gr, 0,
(img.getHeight()-layout.getDescent()) as int)
gr.dispose()

rs = 0.25f
ds = 0.45f
for(lz in 0..img.getHeight()-1){
for(lx in 0..img.getWidth()-1){
gl.glPushMatrix()
gl.glTranslatef(ds*lx as float, 0f, ds*lz as float)
if( img.getRGB(lx, lz) != 0 ){
glut.glutSolidSphere(rs, 16, 16)
}
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1315a.png)
groovyとJOGLで複数の球の文字列を描画した画像

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

関連情報
groovyとJOGLのまとめ

Tuesday, October 20, 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(
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, -1.0f, -30.0f)

gl.glEnable(GL_LIGHTING)
gl.glEnable(GL_LIGHT0)
gl.glEnable(GL_COLOR_MATERIAL)
gl.glEnable(GL_NORMALIZE)
gl.glEnable(GL_AUTO_NORMAL)
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(45.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f)

slices = 12
ch = 5f
cr = 0.25f

for(lx in 0..slices-1){
for(ly in 0..slices-1){
gl.glPushMatrix()
// X軸回転
gl.glRotatef(360f/slices*lx as float, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(360f/slices*ly as float, 0.0f, 1.0f, 0.0f)
// 円柱を描画
glut.glutSolidCylinder(cr, ch, 16, 4)
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1314a.png)
groovyとJOGLで描画した円柱がウニのように広がった画像

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

関連情報
groovyとJOGLのまとめ

Monday, October 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

def drawTriangle(GL gl,
double p1x, double p1y, double p1z,
double p2x, double p2y, double p2z,
double p3x, double p3y, double p3z)
{
gl.glBegin(GL_POLYGON)

gl.glNormal3d((p1x+p2x+p3x)/3,
1, (p1z+p2z+p3z)/3)
gl.glVertex3d(p1x, p1y, p1z)
gl.glNormal3d((p1x+p2x+p3x)/3,
0, (p1z+p2z+p3z)/3)
gl.glVertex3d(p2x, p2y, p2z)
gl.glNormal3d((p1x+p2x+p3x)/3,
0, (p1z+p2z+p3z)/3)
gl.glVertex3d(p3x, p3y, p3z)

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: {
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, -1.0f, -12.0f)

gl.glEnable(GL_LIGHTING)
gl.glEnable(GL_LIGHT0)
gl.glEnable(GL_COLOR_MATERIAL)
gl.glEnable(GL_NORMALIZE)
gl.glEnable(GL_AUTO_NORMAL)
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(40.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f)

// ピラミッドサイズ
ph = 2.0
ps = 3.0

drawTriangle(gl,
0.0, ph, 0.0,
ps/2.0, 0.0, -ps/2.0,
-ps/2.0, 0.0, -ps/2.0)

drawTriangle(gl,
0.0, ph, 0.0,
-ps/2.0, 0.0, ps/2.0,
ps/2.0, 0.0, ps/2.0)

drawTriangle(gl,
0.0, ph, 0.0,
-ps/2.0, 0.0, -ps/2.0,
-ps/2.0, 0.0, ps/2.0)

drawTriangle(gl,
0.0, ph, 0.0,
ps/2.0, 0.0, ps/2.0,
ps/2.0, 0.0, -ps/2.0)

gl.glBegin(GL_POLYGON)
gl.glNormal3d(0,-1,0)
gl.glVertex3d(-ps/2, 0.0, ps/2)
gl.glVertex3d(-ps/2, 0.0, -ps/2)
gl.glVertex3d(ps/2, 0.0, -ps/2)
gl.glVertex3d(ps/2, 0.0, ps/2)
gl.glEnd()

gl.glPopMatrix()
},

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


出力画像(sample1313a.png)
groovyとJOGLで描画したピラミッド

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

関連情報
groovyとJOGLのまとめ

Sunday, October 18, 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(
0x58/0xff as Float,
0x50/0xff as Float,
0x50/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(-3.0f, -1.5f, -7.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)

// フォグの設定
fog=[0.2f,0.2f,0.3f,1.0f]
gl.glFogfv(GL_FOG_COLOR,fog as float[], 0)
gl.glFogi(GL_FOG_MODE,GL.GL_EXP)
gl.glFogf(GL_FOG_DENSITY, 0.04f)
gl.glFogf(GL_FOG_START, 0.0f)
gl.glFogf(GL_FOG_END,20.0f)
gl.glEnable(GL_FOG)

// 光源の位置
l1pos = [-5.0f, 10.0f, 0.0f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_POSITION, l1pos as float[], 0)
// 環境光
l1amb = [0.1f, 0.1f, 0.1f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_AMBIENT, l1amb as float[], 0)
// 拡散光
l1dif = [0.9f, 0.9f, 0.9f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, l1dif as float[], 0)
// 鏡面光
l1spe = [1.0f, 1.0f, 1.0f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_SPECULAR, l1spe as float[], 0)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()

// 面の色を設定
gl.glColor3f(
0xff/0xff as float,
0xff/0xff as float,
0xff/0xff as float
)

// X軸回転
gl.glRotatef(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f)

cs = 0.25f
gs = 0.3f
for(lz in -10..60){
for(lx in -10..100){
gl.glPushMatrix()
de = (gs*lx-5)*(gs*lx-5)+(gs*lz-3)*(gs*lz-3)+9
ch = 27/(de==0f?0.001f:de)
gl.glScalef(1f, ch as float, 1f)
gl.glTranslatef(lx*gs as float,
cs*ch/2 as float, -1*lz*gs as float)
glut.glutSolidCube(cs)
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1312a.png)
groovyとJOGLで描画した直方体の山

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

関連情報
groovyとJOGLのまとめ

Saturday, October 17, 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(
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, 80.0f)

gl.glMatrixMode(GL_MODELVIEW)
gl.glLoadIdentity()
gl.glTranslatef(0.0f, -0.5f, -30.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(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(70.0f, 0.0f, 1.0f, 0.0f)

cs = 0.5f
gs = 0.05f
for(lx in -100..800-1){
gl.glPushMatrix()
gl.glRotatef(5f*lx as float, 1.0f, 0.0f, 0.0f)
gl.glTranslatef(gs*lx as float, 2f, 0f)
gl.glScalef(1f,1f,0.2f)
glut.glutSolidCube(cs)
gl.glPopMatrix()
}

gl.glPopMatrix()
},

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


出力画像(sample1311a.png)
groovyとJOGLで複数の直方体をスプリング状に配置した画像

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

関連情報
groovyとJOGLのまとめ

Friday, October 16, 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(
0x58/0xff as Float,
0x50/0xff as Float,
0x50/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(-3.0f, -1.5f, -7.0f)

gl.glEnable(GL_LIGHTING)
gl.glEnable(GL_LIGHT0)
gl.glEnable(GL_LIGHT1)
gl.glEnable(GL_COLOR_MATERIAL)
gl.glEnable(GL_NORMALIZE)
gl.glEnable(GL_DEPTH_TEST)
gl.glEnable(GL_CULL_FACE)

// フォグの設定
fog=[0.2f,0.2f,0.3f,1.0f]
gl.glFogfv(GL_FOG_COLOR,fog as float[], 0)
gl.glFogi(GL_FOG_MODE,GL.GL_EXP)
gl.glFogf(GL_FOG_DENSITY, 0.04f)
gl.glFogf(GL_FOG_START, 0.0f)
gl.glFogf(GL_FOG_END,20.0f)
gl.glEnable(GL_FOG)

// 光源の位置
l0pos = [-5.0f, 5.0f, 0.0f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_POSITION, l0pos as float[], 0)
// 環境光
l0amb = [0.1f, 0.1f, 0.1f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_AMBIENT, l0amb as float[], 0)
// 拡散光
l0dif = [0.2f, 0.2f, 0.2f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, l0dif as float[], 0)
// 鏡面光
l0spe = [0.3f, 0.3f, 0.3f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_SPECULAR, l0spe as float[], 0)

// 光源の位置
l1pos = [3.0f, 4.0f, -5.0f, 1.0f]
gl.glLightfv(GL_LIGHT1, GL_POSITION, l1pos as float[], 0)
// 環境光
l1amb = [0.2f, 0.2f, 0.1f, 1.0f]
gl.glLightfv(GL_LIGHT1, GL_AMBIENT, l1amb as float[], 0)
// 拡散光
l1dif = [0.5f, 0.5f, 0.3f, 1.0f]
gl.glLightfv(GL_LIGHT1, GL_DIFFUSE, l1dif as float[], 0)
// 鏡面光
l1spe = [0.8f, 0.8f, 0.2f, 1.0f]
gl.glLightfv(GL_LIGHT1, GL_SPECULAR, l1spe as float[], 0)
// スポットライト
l1dir = [0.1f, -1.0f, 0.0f]
gl.glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, l1dir as float[], 0)
gl.glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 30.0f)
gl.glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 5.0f)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()

// 面の色を設定
gl.glColor3f(
0xff/0xff as float,
0xff/0xff as float,
0xff/0xff as float
)

// X軸回転
gl.glRotatef(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f)

cs = 0.4f
gs = 0.5f
xs = 100
zs = 100
for(lz in -3..zs-1){
for(lx in -3..xs-1){
gl.glPushMatrix()
ch = cs * (float)(Math.random()*1.0) as float
gl.glScalef(1f,ch, 1f)
gl.glTranslatef(lx*gs as float,
-(cs-ch)/2 as float, -1*lz*gs as float)
glut.glutSolidCube(cs)
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1310a.png)
groovyとJOGLで描画したスポットライトを使用した画像

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

関連情報
groovyとJOGLのまとめ

Thursday, October 15, 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(
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, -22.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(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f)
// Z軸回転
gl.glRotatef(60.0f, 0.0f, 0.0f, 1.0f)

slices = 32
rad = 3f
cs = 0.25f

for(ly in 0..slices-1){
for(lz in 0..slices-1){
gl.glPushMatrix()
// Y軸回転
gl.glRotatef(360f/slices*ly as float,
0.0f, 1.0f, 0.0f)
// Z軸回転
gl.glRotatef(360f/slices*lz as float,
0.0f, 0.0f, 1.0f)
gl.glTranslatef(0f, rad, 0f)

glut.glutSolidCube(cs)
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1309a.png)
groovyとJOGLで描画した球状に配置した立方体

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

関連情報
groovyとJOGLのまとめ

Wednesday, October 14, 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(
0x58/0xff as Float,
0x50/0xff as Float,
0x50/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(-3.0f, -1.5f, -7.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)

// フォグの設定
fog=[0.1f,0.1f,0.2f,1.0f]
gl.glFogfv(GL_FOG_COLOR,fog as float[], 0)
gl.glFogi(GL_FOG_MODE,GL.GL_EXP)
gl.glFogf(GL_FOG_DENSITY, 0.08f)
gl.glFogf(GL_FOG_START, 0.0f)
gl.glFogf(GL_FOG_END,20.0f)
gl.glEnable(GL_FOG)

// 光源の位置
l1pos = [-5.0f, 5.0f, 0.0f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_POSITION, l1pos as float[], 0)
// 環境光
l1amb = [0.2f, 0.2f, 0.1f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_AMBIENT, l1amb as float[], 0)
// 拡散光
l1dif = [0.9f, 0.9f, 0.9f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, l1dif as float[], 0)
// 鏡面光
l1spe = [0.9f, 0.9f, 0.9f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_SPECULAR, l1spe as float[], 0)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()

// 面の色を設定
gl.glColor3f(
0xff/0xff as float,
0xff/0xff as float,
0xff/0xff as float
)

// X軸回転
gl.glRotatef(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f)

cs = 0.5f
gs = 0.6f
xs = 50
zs = 50
for(lz in -3..zs-1){
for(lx in -3..xs-1){
gl.glPushMatrix()
ch = cs * (float)(Math.random()*1.0) as float
gl.glScalef(1f,ch, 1f)
gl.glTranslatef(lx*gs as float,
-(cs-ch)/2 as float, -1*lz*gs as float)
glut.glutSolidCube(cs)
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1308a.png)
groovyとJOGLで複数の直方体を凸凹に配置した画像

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

関連情報
groovyとJOGLのまとめ

Tuesday, October 13, 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(
0x68/0xff as Float,
0x60/0xff as Float,
0x60/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.5f, -30.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.glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,
GL_TRUE)

gl.glEnable(GL_TEXTURE_2D)

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(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f)

px = -7f
py = 0f
pz = 0f
ry = 0f
for(li in 0..19){
gl.glPushMatrix();

gl.glTranslatef(px as float, py, pz)
// X軸回転
gl.glRotatef(ry as float, 1.0f, 0.0f, 0.0f)
glut.glutSolidCube(0.9f)
px += 1f
ry += 10f
gl.glPopMatrix()
}

gl.glPopMatrix()
},

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


出力画像(sample1307a.png)
groovyとJOGLで描画した複数の立方体を少しずつ回転させた画像

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

関連情報
groovyとJOGLのまとめ

Monday, October 12, 2009

PyWin32とImageMagickで画像をグレースケールの立体的な角丸四角に変換する

PyWin32とImageMagickで画像をグレースケールの立体的な角丸四角に変換するには、以下のコードを実行します。


# coding=UTF-8
import win32com.client

im = win32com.client.Dispatch("ImageMagickObject.MagickImage.1")
im.convert("-size", "200x200", "xc:none",
"-fill", "white", "-draw", "roundrectangle 10,10,190,190,10,10",
"-matte", "-channel", "rgba", "sf.jpg",
"-compose", "src_in", "-composite", "-modulate", "160",
"-colorspace", "gray", "(", "-size", "200x200", "xc:none",
"-fill", "white", "-draw", "roundrectangle 10,10,190,190,10,10",
"-shade", "135x23", "-blur", "0x2", "-normalize", "-matte",
"-channel", "rgba", ")", "-swap", "0,1", "-compose", "overlay",
"-composite", "sample1340a.png")


出力画像(sample1340a.png)
PyWin32とImageMagickでグレースケールの立体的な角丸四角に貼り付けたように変換した画像

動作環境
Python3.1.1, Python for Windows Extensions (Build 214), ImageMagick6.5.5

groovyとJOGLで画像を放射状に配置する

groovyとJOGLで画像を放射状に配置するには、以下のコードを実行します。


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

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

fn = [
"sample1182a.png",
"sample1189a.png",
"sample1191a.png",
"sample1194a.png",
"sample1195a.png",
"sample1196a.png",
"sample1197a.png",
"sample1198a.png",
"sample1199a.png",
"sample1201a.png",
"sample1202a.png",
"sample1206a.png"
]
textures = new Texture[fn.size]

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(
0x68/0xff as Float,
0x60/0xff as Float,
0x60/0xff as Float,
1f)
for(fi in 0..fn.size-1){
textures[fi] =
TextureIO.newTexture(
new File(fn[fi]), true)
textures[fi].enable();
}
},

display: {
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.5f, -12.0f)

gl.glEnable(GL_LIGHTING)
gl.glEnable(GL_LIGHT0)
gl.glEnable(GL_COLOR_MATERIAL)
gl.glEnable(GL_AUTO_NORMAL);
gl.glEnable(GL_NORMALIZE)
gl.glEnable(GL_DEPTH_TEST)
// gl.glEnable(GL_CULL_FACE)
gl.glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,
GL_TRUE)

gl.glEnable(GL_TEXTURE_2D)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()

// X軸回転
gl.glRotatef(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f)

da = 360f / fn.size
ps = 2f
for(qi in 0..fn.size-1){
// テクスチャが張られたポリゴンを描画
textures[qi].bind();
px = ps * (float)Math.cos(
Math.PI * da * qi / 180) as float
pz = ps * (float)Math.sin(
Math.PI * da * qi / 180) as float
nx = ps * (float)Math.cos(
Math.PI * (da * qi+90) / 180) as float
nz = ps * (float)Math.sin(
Math.PI * (da * qi+90) / 180) as float

gl.glBegin(GL_QUADS)
gl.glNormal3f(nx, 0.0f, nz)

gl.glTexCoord2f(0.0f, 0.0f)
gl.glVertex3f(0f, ps/2f as float, 0f)
gl.glTexCoord2f(0.0f, 1.0f)
gl.glVertex3f(0f, -ps/2f as float, 0f)
gl.glTexCoord2f(1.0f, 1.0f)
gl.glVertex3f(px, -ps/2f as float, pz)
gl.glTexCoord2f(1.0f, 0.0f)
gl.glVertex3f(px, ps/2f as float, pz)
gl.glEnd()
}

gl.glPopMatrix()
},

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


出力画像(sample1306a.png)
groovyとJOGLで放射状に複数の画像を配置した画像

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

関連情報
groovyとJOGLのまとめ

Sunday, October 11, 2009

groovyとJOGLでポリゴンに繰り返しテクスチャを貼り付ける

groovyとJOGLでポリゴンに繰り返しテクスチャを貼り付けるには、以下のコードを実行します。


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

// 出力画像サイズ
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(
0xf0/0xff as Float,
0xf0/0xff as Float,
0xf0/0xff as Float,
1f)
Texture texture = TextureIO.newTexture(
new File("sf_r.jpg"), true)
texture.enable()
texture.bind()
},

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, -15.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.glEnable(GL_TEXTURE_2D)
gl.glEnable(GL_TEXTURE_GEN_S)
gl.glEnable(GL_TEXTURE_GEN_T)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()

gl.glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_REPEAT)
gl.glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT)

// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f)

// テクスチャが張られたポリゴンを描画
gl.glBegin(GL_POLYGON)
gl.glTexCoord2f(0.0f, 1.0f)
gl.glVertex3f(-2f, -2f, 0.0f)
gl.glTexCoord2f(1.0f, 1.0f)
gl.glVertex3f(2f, -2f, 0.0f)
gl.glTexCoord2f(1.0f, 0.0f)
gl.glVertex3f(2f, 2f, 0.0f)
gl.glTexCoord2f(0.0f, 0.0f)
gl.glVertex3f(-2f, 2f, 0.0f)
gl.glEnd()

gl.glPopMatrix()
},

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


テクスチャ画像


出力画像(sample1305a.png)
groovyとJOGLで描画した繰り返しテクスチャを貼り付けたポリゴン

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

関連情報
groovyとJOGLのまとめ

Saturday, October 10, 2009

groovyとJOGLで文字列を描画する

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


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

// 出力画像サイズ
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(
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, -15.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(20.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f)

// 球を描画
glut.glutSolidSphere(2.0f, 32, 32)

// 文字列を描画
Font font = new Font("Tahoma", java.awt.Font.PLAIN, 40)
TextRenderer tr = new TextRenderer(font, true, true)
tr.beginRendering(width, height)
tr.setColor(0.5f, 0.5f, 0.5f, 1.0f)
tr.draw("TextRenderer", 20, 40)
tr.endRendering()

gl.glPopMatrix()
},

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


出力画像(sample1304a.png)
groovyとJOGLで描画した文字列

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

関連情報
groovyとJOGLのまとめ

Friday, October 09, 2009

PyWin32とImageMagickでポリラインを描画する

PyWin32とImageMagickでポリラインを描画するには、以下のコードを実行します。


# coding=UTF-8
import win32com.client

im = win32com.client.Dispatch("ImageMagickObject.MagickImage.1")
im.convert("-size", "200x200", "xc:white",
"-stroke", "#113377", "-fill", "#7799ff",
"-draw", "polyline 100,0,0,199,199,199", "sample1339a.png")


出力画像(sample1339a.png)
PyWin32とImageMagickで描画したポリライン

動作環境
Python3.1.1, Python for Windows Extensions (Build 214), ImageMagick6.5.5

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(
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, -15.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(20.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f)

// 環境光を設定
ambient = [0.2f, 0.2f, 0.1f, 1.0f]
gl.glMaterialfv(GL_FRONT_AND_BACK,
GL_AMBIENT, ambient as float[], 0)

// 拡散光を設定
diffuse = [0.1f, 0.1f, 0.1f, 1.0f]
gl.glMaterialfv(GL_FRONT_AND_BACK,
GL_DIFFUSE, diffuse as float[], 0)

// 鏡面光を設定
specular = [0.4f, 0.4f, 0.3f, 1.0f]
gl.glMaterialfv(GL_FRONT_AND_BACK,
GL_SPECULAR, specular as float[], 0)
// specular exponent
gl.glMaterialf(GL_FRONT_AND_BACK,
GL_SHININESS, 128f*0.4f as float)

// 放射光
emission = [0.1f, 0.1f, 0.2f, 1.0f]
gl.glMaterialfv(GL_FRONT_AND_BACK,
GL_EMISSION, emission as float[], 0);

// 球を描画
glut.glutSolidSphere(2.0f, 32, 32)

gl.glPopMatrix()
},

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


出力画像(sample1303a.png)
groovyとJOGLで質感を設定した球

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

関連情報
groovyとJOGLのまとめ

Thursday, October 08, 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(
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, -7.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(20.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f)

// 線の太さを設定
gl.glLineWidth(4f)

gl.glTranslated(-1.5, 0.0, 0.0)
gl.glScalef(0.01f, 0.01f, 0.01f)
glut.glutStrokeString(GLUT.STROKE_ROMAN, "JOGL")

gl.glPopMatrix()
},

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


出力画像(sample1302a.png)


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

関連情報
groovyとJOGLのまとめ

Wednesday, October 07, 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(
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, -20.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(20.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f)

rad = 2.8f
num = 12
for(fa=0.0f;fa<360.0f;fa+=(360.0f/num)){
gl.glPushMatrix()

gl.glTranslated(
rad * Math.cos(Math.PI*fa/180f),
rad * Math.sin(Math.PI*fa/180f),
0.0
)

// 球を描画
glut.glutSolidSphere(1.0f, 32, 32);

gl.glPopMatrix();
}
gl.glPopMatrix()
},

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


出力画像(sample1301a.png)
groovyとJOGLで描画した球が輪のようになった画像

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

関連情報
groovyとJOGLのまとめ

Tuesday, October 06, 2009

PyWin32とImageMagickでポリゴンを描画する

PyWin32とImageMagickでポリゴンを描画するには、以下のコードを実行します。


# coding=UTF-8
import win32com.client

im = win32com.client.Dispatch("ImageMagickObject.MagickImage.1")
im.convert("-size", "200x200", "xc:white",
"-stroke", "#113377", "-fill", "#7799ff",
"-draw", "polygon 100,0,0,199,199,199", "sample1338a.png")


出力画像(sample1338a.png)
PyWin32とImageMagickで描画したポリゴン

動作環境
Python3.1.1, Python for Windows Extensions (Build 214), ImageMagick6.5.5

groovyとJOGLで背景テクスチャの上にポリゴンを描画する

groovyとJOGLで背景テクスチャの上にポリゴンを描画するには、以下のコードを実行します。


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

// 出力画像サイズ
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()

texture = null

buf.addGLEventListener(
[ init: {
texture = TextureIO.newTexture(
new File("sf.jpg"), true)
},

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

// 平行投影
gl.glMatrixMode(GL_PROJECTION)
gl.glLoadIdentity()
gl.glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f)

// 背景テクスチャを描画
gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )
gl.glDisable(GL_DEPTH_TEST)
texture.enable()
gl.glBindTexture(GL_TEXTURE_2D,
texture.getTextureObject())
gl.glBegin(GL_QUADS)
gl.glTexCoord2f(0.0f, 1.0f)
gl.glVertex2f(-1.0f, -1.0f)
gl.glTexCoord2f(0.0f, 0.0f)
gl.glVertex2f(-1.0f, 1.0f)
gl.glTexCoord2f(1.0f, 0.0f)
gl.glVertex2f(1.0f, 1.0f)
gl.glTexCoord2f(1.0f, 1.0f)
gl.glVertex2f(1.0f, -1.0f)
gl.glEnd()
texture.disable()

// 透視投影
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, -7.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.glPushMatrix()
// 面の色を設定
gl.glColor3f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float
)
// X軸回転
gl.glRotatef(10.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f)
// 8面体を描画
glut.glutSolidOctahedron()
gl.glPopMatrix()
},

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


出力画像(sample1300a.png)
groovyとJOGLで背景テクスチャの上にポリゴンを描画した画像

関連情報
JDK1.6 Update14, Groovy 1.6.3, JOGL 1.1.1a

関連情報
groovyとJOGLのまとめ

Monday, October 05, 2009

groovyとJOGLで4面体を描画する

groovyとJOGLで4面体を描画するには、以下のコードを実行します。


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(
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, -7.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(-120.0f, 1.0f, 0.0f, 0.0f)
// 4面体を描画
glut.glutSolidTetrahedron()
gl.glPopMatrix()
},

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


出力画像(sample1299a.png)
groovyとJOGLで描画した4面体

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

関連情報
groovyとJOGLのまとめ

Sunday, October 04, 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(
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, -1.0f, -8.0f)

gl.glEnable(GL_LIGHTING)
gl.glEnable(GL_LIGHT0)
gl.glEnable(GL_COLOR_MATERIAL)
gl.glEnable(GL_NORMALIZE)
gl.glEnable(GL_BLEND)
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
gl.glDisable(GL_DEPTH_TEST)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()
// 面の色を設定
gl.glColor4f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float,
0x20/0xff as Float
)
// X軸回転
gl.glRotatef(-70.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(10.0f, 0.0f, 1.0f, 0.0f)
// Z軸回転
gl.glRotatef(30.0f, 0.0f, 0.0f, 1.0f)
// ティーポットを描画
glut.glutSolidTeapot(1.0f,false)
gl.glPopMatrix()
},

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


出力画像(sample1298a.png)
groovyとJOGLで描画した半透明のティーポット

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

関連情報
groovyとJOGLのまとめ

Saturday, October 03, 2009

PyWin32とImageMagickで角丸四角を描画する

PyWin32とImageMagickで角丸四角を描画するには、以下のコードを実行します。


# coding=UTF-8
import win32com.client

im = win32com.client.Dispatch("ImageMagickObject.MagickImage.1")
im.convert("-size", "200x200", "xc:white",
"-stroke", "#113377", "-fill", "#7799ff",
"-draw", "roundrectangle 10,10,189,189,20,20", "sample1337a.png")


出力画像(sample1337a.png)
PyWin32とImageMagickで描画した角丸四角

動作環境
Python3.1.1, Python for Windows Extensions (Build 214), ImageMagick6.5.5

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, -1.0f, -15.0f)

// ワイヤーフレームにアンチエイリアスをかける
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
gl.glEnable(GL_BLEND)
gl.glEnable(GL_LINE_SMOOTH)

gl.glClear(GL_COLOR_BUFFER_BIT)

gl.glPushMatrix()
// X軸回転
gl.glRotatef(-60.0f, 1.0f, 0.0f, 0.0f)
// 線の色を設定
gl.glColor3f(
0xff/0xff as Float,
0xff/0xff as Float,
0xff/0xff as Float
)
// ワイヤーフレームの円柱を描画
glut.glutWireCylinder(2.0f, 2.5f, 16, 4)
gl.glPopMatrix()
},

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


出力画像()
groovyとJOGLで描画したアンチエイリアスをかけたワイヤーフレームの画像

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

関連情報
groovyとJOGLのまとめ

Friday, October 02, 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(
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)

// 光源の位置
l1pos = [-5.0f, 5.0f, 0.0f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_POSITION, l1pos as float[], 0)
// 環境光
l1amb = [0.2f, 0.2f, 0.1f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_AMBIENT, l1amb as float[], 0)
// 拡散光
l1dif = [0.9f, 0.9f, 0.9f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, l1dif as float[], 0)
// 鏡面光
l1spe = [0.9f, 0.9f, 0.9f, 1.0f]
gl.glLightfv(GL_LIGHT0, GL_SPECULAR, l1spe as float[], 0)


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()
// X軸回転
gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(35.0f, 0.0f, 1.0f, 0.0f)
// 面の色を設定
gl.glColor3f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float
)
// 球を描画
glut.glutSolidSphere(1.5f, 32, 32)
gl.glPopMatrix()
},

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


出力画像()
groovyとJOGLで光源を設定して描画した球

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

関連情報
groovyとJOGLのまとめ

Thursday, October 01, 2009

PyWin32とImageMagickで四角を描画する

PyWin32とImageMagickで四角を描画するには、以下のコードを実行します。


# coding=UTF-8
import win32com.client

im = win32com.client.Dispatch("ImageMagickObject.MagickImage.1")
im.convert("-size", "200x200", "xc:white",
"-stroke", "#113377", "-fill", "#7799ff",
"-draw", "rectangle 10,10,189,189", "sample1336a.png")


出力画像(sample1336a.png)
PyWin32とImageMagickで描画した四角

動作環境
Python3.1.1, Python for Windows Extensions (Build 214), ImageMagick6.5.5

JOGLとJOGLでワイヤーフレームとポリゴンの球を描画する

JOGLと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(
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.glShadeModel(GL_FLAT)
// ポリゴンオフセットを設定
gl.glEnable(GL_POLYGON_OFFSET_FILL)
gl.glPolygonOffset(1f, 1f)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()
// X軸回転
gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(35.0f, 0.0f, 1.0f, 0.0f)
// 球を描画
// 線の色を設定
gl.glColor3f(
0xff/0xff as Float,
0xff/0xff as Float,
0xff/0xff as Float
)
glut.glutWireSphere(1.5f, 16, 16)
// 面の色を設定
gl.glColor3f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float
)
glut.glutSolidSphere(1.5f, 16, 16)
gl.glPopMatrix()
},

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


出力画像()
groovyとJOGLで描画したワイヤーフレームとポリゴンの球

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

関連情報
groovyとJOGLのまとめ

Wednesday, September 30, 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(
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.glShadeModel(GL_FLAT)

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(35.0f, 0.0f, 1.0f, 0.0f)
// 球を描画
glut.glutSolidSphere(1.5f, 16, 16)
gl.glPopMatrix()
},

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


出力画像(sample1294a.png)
groovyとJOGLで描画したフラットシェーディングの球

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

関連情報
groovyとJOGLのまとめ

Tuesday, September 29, 2009

groovyとJOGLでフォグをつけてレンダリングする

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(
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(-3.0f, -1.5f, -7.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)

fog=[1.0f,1.0f,1.0f,1.0f]
gl.glFogfv(GL_FOG_COLOR,fog as float[],0)
gl.glFogi(GL_FOG_MODE,GL.GL_EXP)
gl.glFogf(GL_FOG_DENSITY, 0.08f)
gl.glFogf(GL_FOG_START, 0.0f)
gl.glFogf(GL_FOG_END,10.0f)
gl.glEnable(GL_FOG)

gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()
// X軸回転
gl.glRotatef(15.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f)

cs = 0.3f
gs = 0.6f
xs = 30
zs = 30
for(lz in 0..zs-1){
for(lx in 0..xs-1){
if( (lx+lz)%2 == 0 ){
gl.glColor3f(
0xff/0xff as Float,
0xff/0xff as Float,
0xff/0xff as Float
)
} else {
gl.glColor3f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float
)
}
gl.glPushMatrix()
gl.glTranslatef(
lx*gs as Float, 0.0f, -1*lz*gs as Float)
glut.glutSolidCube(cs)
gl.glPopMatrix()
}
}

gl.glPopMatrix()
},

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


出力画像(sample1293a.png)
groovyとJOGLでフォグをつけてレンダリングした画像

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

関連情報
groovyとJOGLのまとめ

Monday, September 28, 2009

PyWin32とImageMagickで楕円を描画する

PyWin32とImageMagickで楕円を描画するには、以下のコードを実行します。


# coding=UTF-8
import win32com.client

im = win32com.client.Dispatch("ImageMagickObject.MagickImage.1")
im.convert("-size", "200x200", "xc:white",
"-stroke", "#113377", "-fill", "#7799ff",
"-draw", "ellipse 100,100,80,50,0,360", "sample1335a.png")


出力画像(sample1335a.png)
PyWin32とImageMagickで描画した楕円

動作環境
Python3.1.1, Python for Windows Extensions (Build 214), ImageMagick6.5.5

groovyとJOGLで球にテクスチャをマッピングする

groovyとJOGLで球にテクスチャをマッピングするには、以下のコードを実行します。


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

// 出力画像サイズ
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(
0xf0/0xff as Float,
0xf0/0xff as Float,
0xf0/0xff as Float,
1f)
Texture texture = TextureIO.newTexture(
new File("sf_r.jpg"), true)
texture.enable()
texture.bind()
},

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.glEnable(GL_TEXTURE_2D)
gl.glEnable(GL_TEXTURE_GEN_S)
gl.glEnable(GL_TEXTURE_GEN_T)
gl.glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT )

gl.glPushMatrix()

gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE,
GL_SPHERE_MAP);
gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,
GL_SPHERE_MAP);

// 球を描画
glut.glutSolidSphere(1.5f, 32, 32)
gl.glPopMatrix()
},

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


テクスチャ用画像(sf_r.jpg)


出力画像(sample1292a.png)
groovyとJOGLで描画したテクスチャマッピングした球

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

関連情報
groovyとJOGLのまとめ

Sunday, September 27, 2009

groovyとJOGLで円弧を描画する

groovyとJOGLで円弧を描画するには、以下のコードを実行します。


import static javax.media.opengl.GL.*;
import java.io.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
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(
0xf0/0xff as Float,
0xf0/0xff as Float,
0xf0/0xff as Float,
1f)
},

display: {
GLU glu = new GLU()
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, -7.0f)

gl.glClear(GL_COLOR_BUFFER_BIT);

// 面の色を設定
gl.glColor3f(
0x77/0xff as Float,
0x99/0xff as Float,
0xff/0xff as Float
)
gl.glPushMatrix()
// X軸回転
gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f)
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f)
// 円弧を描画する
GLUquadric gluq = glu.gluNewQuadric()
glu.gluPartialDisk(gluq, 0.0, 1.0, 16, 4, -30, 90)
gl.glPopMatrix()
},

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


出力画像(sample1291a.png)
groovyとJOGLで描画した円弧

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

関連情報
groovyとJOGLのまとめ