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