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のまとめ

Saturday, September 26, 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.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.glutSolidTorus(0.5f, 1f, 32, 16)
gl.glPopMatrix()
},

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


出力画像(sample1290a.png)
groovyとJOGLで描画したドーナツ型

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

関連情報
groovyとJOGLのまとめ