Wednesday, July 22, 2009

JOGLで市松模様の面を描画する

JOGLで市松模様の面を描画するには、以下のコードを実行します。


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

public class JoglSample27
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(-2.0f, -2.0f, -12.0f);

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

gl.glPushMatrix();
// X軸回転
gl.glRotatef(-70.0f, 1.0f, 0.0f, 0.0f);

float tw = 0.4f;
float th = 0.4f;
int xs = 10;
int ys = 10;
for(int ly=0;ly<ys;ly++){
for(int lx=0;lx<xs;lx++){
if( (lx+ly)%2 == 0 ){
gl.glColor3f(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff
);
} else {
gl.glColor3f(
(float)0x40/(float)0xff,
(float)0x40/(float)0xff,
(float)0x40/(float)0xff
);
}
gl.glRectf(lx*tw,ly*th,
(lx+1)*tw,(ly+1)*tw);
}
}

gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1185a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1185a.png)
JOGLで描画した市松模様の面

groovyで画像の角を切り落とす

groovyで画像の角を切り落とすには、以下のコードを実行します。


import javax.imageio.*;
import groovy.swing.j2d.*

// 角のサイズ
dx = 20
dy = 20
img = ImageIO.read(new File("sf.jpg"))
def gr = new GraphicsRenderer()
gr.renderToFile("sample1143a.png",
img.width, img.height){
antialias("on")
polygon(
points: [dx,0, img.width-dx-1,0,
img.width-1,dy,
img.width-1,img.height-dy-1,
img.width-dx-1, img.height-1,
dx, img.height-1,
0, img.height-dy-1,
0, dy],
borderColor: no
){
texturePaint(x: 0, y: 0, image: img )
}
}


元画像(sf.jpg)


出力画像(sample1143a.png)
groovyで角を切り落とした画像

Tuesday, July 21, 2009

Structure Synthでビルを描画する - その3

Structure Synthでビルを描画するには、以下のスクリプトを実行します。

{ color #c0c0c0 } building

rule building {
1 * { y -2 s 9 1 9 } 9 * { y 2 } box
1 * { x -5 z -5 y -1 } 8 * { y 2 } 9 * { x 1 } 9 * { z 1 } 1 * { s 0.95 } box
1 * { y 18 s 7 3 3 } box
}

// 白色の地面
{ y -0.5 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像


関連項目
Structure Synthで町並みを描画する

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

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


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

public class JoglSample26
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

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

gl.glClear(GL.GL_COLOR_BUFFER_BIT);


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

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

gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1184a.png"), width, height, true);
context.release();
context.destroy();
}
}


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

ImageMagickとPHPで画像を影絵スケッチ風に変換する

Imagickで画像を影絵スケッチ風に変換するには、以下のコードを実行します。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"
xml:lang="ja" dir="ltr">
<head>
<title>sample1136(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* 画像サイズ */
$sx = 200;
$sy = 50;

$im = new Imagick("tree1.jpg");

$im->blackThresholdImage('#808080');
$im->whiteThresholdImage('#808080');
$im->negateImage(true);
$im->setImageMatte(true);
$im = $im->fxImage("r", Imagick::CHANNEL_ALPHA);

$im2 = new Imagick();
$im2->newImage($im->getImageWidth(),
$im->getImageHeight(), "#303030");
$im2->sketchImage(8,0,135);

$im->compositeImage($im2, Imagick::COMPOSITE_IN,
0, 0, Imagick::CHANNEL_ALL);

$im->writeImage('sample1136a.png');

$im->destroy();
?>
<img src="sample1136a.png" /><br />

</body>
</html>


元画像(tree1.jpg)


出力画像(sample1136a.png)
Imagickで影絵スケッチ風に変換した画像

Monday, July 20, 2009

Structure Synthでビルを描画する - その2

Structure Synthでビルを描画するには、以下のスクリプトを実行します。

{ color #c0c0c0 } building

rule building {
1 * { x -4 z -6 y 11 s 1 22 1 } 5 * { z 2 } box
1 * { x 4 z -6 y 11 s 1 22 1 } 5 * { z 2 } box
{ y 10.5 s 7 22 9 } box
1 * { y -2 s 9 1 9 } 12 * { y 2 } box
{ y 23 s 8 2 8 } box
{ y 24 s 6 1 6 } box
{ x 2 z 2 y 25 s 2 2 2 } box
}

// 白色の地面
{ y -0.5 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像


関連項目
Structure Synthで町並みを描画する

RMagickで画像を青く滲ませる

RMagickで画像を青く滲ませるには、以下のコードを実行します。


require 'RMagick'
include Magick

images = ImageList.new("sf2.jpg")
images.modulate(0.9,0.5,1).
spread(3).motion_blur(0,7,270).
colorize(0.2, 0.2, 0.2, "#5588ee").
write("sample1156a.png")
exit


元画像(sf2.jpg)


出力画像(sample1156a.png)
RMagickで青く滲ませた画像

動作環境
ruby1.8.6, rmagick2.9.0

groovyで画像を8色グレースケール画像に変換する

groovyで画像を8色グレースケール画像に変換するには、以下のコードを実行します。


import groovy.swing.j2d.*

def gr = new GraphicsRenderer()
gr.renderToFile("sample1142a.png", 200, 200){
antialias("on")
rect( x: 0, y: 0, width: 200, height: 200,
borderColor: no){
texturePaint(x: 0, y: 0, file: 'sf.jpg' )
filters {
grayscale()
// numColorsは8から256
// see com.jhlabs.image.QuantizeFilter.
quantize(numColors: 8)
}
}
}


元画像(sf.jpg)


出力画像(sample1142a.png)
groovyで8色グレースケールに変換した画像

Sunday, July 19, 2009

Structure Synthでビルを描画する - その1

Structure Synthでビルを描画するには、以下のスクリプトを実行します。

{ color #c0c0c0 } building

rule building {
1 * { x -4 z -6 y 10.5 s 1 21 1 } 5 * { z 2 } box
1 * { x 4 z -6 y 10.5 s 1 21 1 } 5 * { z 2 } box
{ y 10.5 s 7 21 9 } box
1 * { y -3 s 9 1 9 } 8 * { y 3 } box
{ y 22 s 8 1 8 } box
}

// 白色の地面
{ y -0.5 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像


関連項目
Structure Synthで町並みを描画する

JOGLで2つのワイヤーフレームの立方体を重ね書きする

JOGLで2つのワイヤーフレームの立方体を重ね書きするには、以下のコードを実行します。


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

public class JoglSample25
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0x40/(float)0xff,
(float)0x40/(float)0xff,
(float)0x40/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

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

gl.glClear(GL.GL_COLOR_BUFFER_BIT);


gl.glPushMatrix();
// X軸回転
gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
// Y軸回転
gl.glRotatef(35.0f, 0.0f, 1.0f, 0.0f);
// 1つ目の立方体
gl.glColor3f(0.3f,0.5f,1.0f);
gl.glLineWidth(6f);
glut.glutWireCube(2.0f);
// 2つ目の立方体
gl.glColor3f(1f,1f,1f);
gl.glLineWidth(2f);
glut.glutWireCube(2.0f);
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1183a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1183a.png)
JOGLで2つのワイヤフレームの立方体を重ね書きした画像

動作環境
JDK6 Upadate13, JOGL 1.1.1

ImageMagickとPHPで画像の上に点線グリッドを描画する

Imagickで画像の上に点線グリッドを描画するには、以下のコードを実行します。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"
xml:lang="ja" dir="ltr">
<head>
<title>sample1135(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* グリッドサイズ */
$xs = 20;
$ys = 20;

$im = new Imagick("sf.jpg");
$iw = $im->getImageWidth();
$ih = $im->getImageWidth();

$idraw = new ImagickDraw();
/* 線の色 */
$idraw->setStrokeColor('#ffffff');
/* 塗りつぶし色 */
$idraw->setFillColor('none');
/* 点線のスタイル */
$idraw->setStrokeDashArray(array(2,2));
for($ly=0;$ly<$ih;$ly+=$ys){
$idraw->line(0,$ly, $iw,$ly);
}
for($lx=0;$lx<$iw;$lx+=$xs){
$idraw->line($lx,0, $lx,$ih);
}
$im->drawImage($idraw);
$im->writeImage('sample1135a.png');

$im->destroy();
?>
<img src="sample1135a.png" /><br />

</body>
</html>


元画像(sf.jpg)


出力画像(sample1135a.png)
Imagickで画像の上に点線を描画した画像

Saturday, July 18, 2009

Structure Synthで渦巻状に四角を描画する

Structure Synthで渦巻状に四角を描画するには、以下のスクリプトを実行します。

{ color #e0e0e0 } pattern1

rule pattern1 {
{ s 2 1 1 } box
{ s 1.02 ry 15 x 0.3 } pattern1
}
// 白色の地面
{ y -0.5 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像

JOGLでティーポットを描画する

JOGLでティーポットを描画するには、以下のコードを実行します。


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

public class JoglSample24
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(-0.5f, -1.0f, -10.0f);

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

gl.glPushMatrix();

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

// X軸回転
gl.glRotatef(-70.0f, 1.0f, 0.0f, 0.0f);
// Y軸回転
gl.glRotatef(15.0f, 0.0f, 1.0f, 0.0f);
// ティーポットを描画
glut.glutSolidTeapot(1.0f,false);
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1182a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1182a.png)
JOGLで描画したティーポット

動作環境
JDK6 Upadate13, JOGL 1.1.1

groovyでグラデーションの放射ストライプを描画する

groovyでグラデーションの放射ストライプを描画するには、以下のコードを実行します。


import groovy.swing.j2d.*

def gr = new GraphicsRenderer()
gr.renderToFile("sample1141a.png", 200, 200){
antialias("on")
background(color: color("white"))
rays(cx:90, cy:110, radius:200, rays:32,
borderColor:no){
radialGradient( cx: 90, cy: 110, radius:150){
stop(offset: 0, color: "#7799ff")
stop(offset: 1, color: "orange")
}
}
}


出力画像(sample1141a.png)
groovyで描画したグラデーションの放射ストライプ

Friday, July 17, 2009

Structure Synthで枠つきタイルを描画する

Structure Synthで枠つきタイルを描画するには、以下のスクリプトを実行します。

3 * { z -4 } 3 * { x 4 color #e0e0e0 } tile
// 枠つきタイル
rule tile {
{ x -1 z -1 s 0.95 } box
{ x 0 z -1 s 0.95 } box
{ x -1 z 0 s 0.95 } box
{ x 0 z 0 s 0.95 } box

{ x -0.5 z -2 s 3.95 1 0.95 } box
{ x -0.5 z 1 s 3.95 1 0.95 } box
{ x -2 z -0.5 s 0.95 1 3.95 } box
{ x 1 z -0.5 s 0.95 1 3.95 } box
}
// 白色の地面
{ y -0.5 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像

JOGLで球を描画する

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


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

public class JoglSample23
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -10.0f);

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

gl.glPushMatrix();
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
// 球を描画
glut.glutSolidSphere(1.5f, 32, 32);
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1181a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1181a.png)
JOGLで描画した球

動作環境
JDK6 Upadate13, JOGL 1.1.1

ImageMagickとPHPで等高線のような画像に変換する

Imagickで等高線のような画像に変換するには、以下のコードを実行します。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"
xml:lang="ja" dir="ltr">
<head>
<title>sample1134(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$im = new Imagick("sf2.jpg");
$im->medianFilterImage(2);
$im->quantizeImage(8, Imagick::COLORSPACE_GRAY, 0, FALSE , false);
$im->edgeImage(1);
$im->negateImage(true);
$im->writeImage('sample1134a.png');

$im->destroy();
?>
<img src="sample1134a.png" /><br />

</body>
</html>


元画像(sf2.jpg)


出力画像(sample1134a.png)
Imagickで等高線のように変換した画像

Thursday, July 16, 2009

Structure Synthで凹みのあるタイル床を描画する

Structure Synthで凹みのあるタイル床を描画するには、以下のスクリプトを実行します。

50 * { z 2 } 50 * { x 2 color #e0e0e0 } tile
// 1つだけ凹みのあるタイルパターン
rule tile {
{ y 0.5 s 0.95 1 0.95 } box
{ x 1 y 0.5 s 0.95 1 0.95 } box
{ x 1 z 1 y 0.25 s 0.95 0.5 0.95 } box
{ z 1 y 0.5 s 0.95 1 0.95 } box
}
// 白色の地面
{ y 0 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像

JOGLでひし形12面体を描画する

JOGLでひし形12面体を描画するには、以下のコードを実行します。


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

public class JoglSample22
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -7.0f);

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

gl.glPushMatrix();
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
// X軸回転
gl.glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
// Y軸回転
gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
// ひし形12面体を描画
glut.glutSolidRhombicDodecahedron();
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1180a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1180a.png)
JOGLで描画したひし形12面体

動作環境
JDK6 Upadate13, JOGL 1.1.1

ImageMagickでドットの荒い放射グラデーションを作成する

ImageMagickでドットの荒い放射グラデーションを作成するには、以下のバッチファイルを実行します。

rem 環境によって変えてね
set im=C:\Progra~1\ImageMagick-6.5.0-Q16
%im%\convert.exe -size 200x200 radial-gradient:#99ccff-#ffffff -resize 10x10 -filter point -resize 200x200 sample1140a.png

出力画像(sample1140a.png)

Wednesday, July 15, 2009

Structure Synthで重み付けをしたランダムな図形描画をおこなう

Structure Synthで重み付けをしたランダムな図形描画をおこなうには、以下のスクリプトのようにruleでw <係数>を指定します。

// ランダムにtileルールを描画
50 * { z 1 } 50 * { x 1 color #e0e0e0 } tile
rule tile {
{ y 0.5 s 0.95 1 0.95 } box
}
// 1/10の重みつけ
rule tile w 0.1 {
{ y 2 s 0.95 4 0.95 } box
}

// 白色の地面
{ y 0 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像

JOGLで8面体を描画する

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


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

public class JoglSample21
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -7.0f);

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

gl.glPushMatrix();
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
// 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();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1179a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1179a.png)
JOGLで描画した8面体

動作環境
JDK6 Upadate13, JOGL 1.1.1

ImageMagickとPHPで抽象的なグラデーション画像に変換する

Imagickで抽象的なグラデーション画像に変換するには、以下のコードを実行します。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"
xml:lang="ja" dir="ltr">
<head>
<title>sample1133(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$im = new Imagick("sf.jpg");
//$im->setImageColorspace(Imagick::COLORSPACE_GRAY);
$im->medianFilterImage(1.5);
$im->quantizeImage(8, Imagick::COLORSPACE_GRAY, 0, FALSE , false);
$im2 = new Imagick();
$im2->newPseudoImage(1, 2, 'gradient:#803010ff-#ffffffff');
/* 任意の2色に変換 */
$im->addImage($im2);
$im->setImageIndex(0);
$im3 = $im->fxImage("v.p{0,(r+g+b)/3}");
$im3->writeImage('sample1133a.png');

$im3->destroy();
$im2->destroy();
$im->destroy();
?>
<img src="sample1133a.png" /><br />

</body>
</html>


元画像(sf.jpg)


出力画像(sample1133a.png)
Imagickで変換した抽象的なグラデーション画像

Tuesday, July 14, 2009

Structure Synthでランダムに凸凹になったタイルを描画する

Structure Synthでランダムに凸凹になったタイルを描画するには、以下のスクリプトを実行します。

50 * { z 1 } 50 * { x 1 color #e0e0e0 } tile
// 同じ名前のルールを複数定義すると、それぞれのルールがランダムに呼び出される
rule tile {
{ y 0.475 s 0.95 0.9 0.95 } box
}
rule tile {
{ y 0.5 s 0.95 1 0.95 } box
}

// 白色の地面
{ y 0 s 200 0.1 200 color white } box


Structure Synthでの出力画像


Sunflowでの出力画像


関連項目
Structure SynthとSunflowのインストールと連携

JOGLで20面体を描画する

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


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

public class JoglSample20
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -7.0f);

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

gl.glPushMatrix();
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
// X軸回転
gl.glRotatef(10.0f, 1.0f, 0.0f, 0.0f);
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
// 20面体を描画
glut.glutSolidIcosahedron();
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1178a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1178a.png)
JOGLで描画した20面体

動作環境
JDK6 Upadate13, JOGL 1.1.1

groovyで引っかいたような文字列を描画する

groovyで引っかいたような文字列を描画するには、以下のコードを実行します。


import groovy.swing.j2d.*

sw = 220
sh = 70
gr = new GraphicsRenderer()
img = gr.render(sw, sh){
background(color: color("#ffffff"))
rect(x:0, y:0, width: sw, height: sh){
filters {
scratch(angle:10, angleVariation:0.8,
color: color("#000000"), density:0.7,
length:0.5, width:0.5, seed:2009)
}
}
}
gr.renderToFile("sample1130a.png", sw, sh){
antialias("on")
background(color: color("#000000"))
font(new java.awt.Font('Tahoma', java.awt.Font.BOLD, 60))
text( x:0, y:0, text: "Groovy", borderColor:no){
texturePaint(x: 0, y: 0, image: img )
}
}


出力画像(sample1130a.png)
GraphicsRendererで描画した引っかいたような文字列

Monday, July 13, 2009

Structure Synthでレンガパターンを描画する

Structure Synthでレンガパターンを描画するには、以下のスクリプトを実行します。

// レンガパターンをy軸に5回、x軸に10回繰り返す
5 * { y 2 } 10 * { x 4 color #e0e0e0 } brick
// レンガパターン
rule brick {
{ x 0.975 y 1.5 s 1.95 0.95 1 } box
{ x 2.975 y 1.5 s 1.95 0.95 1 } box
{ x 0.5 y 0.5 s 1 0.95 1 } box
{ x 2 y 0.5 s 1.95 0.95 1 } box
{ x 3.5 y 0.5 s 1 0.95 1 } box
}

// 白色の地面
{ y 0 s 200 0.1 200 color white } box

Structure Synthで出力した画像


Sunflowで出力した画像

JOGLで12面体を描画する

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


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

public class JoglSample19
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

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

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

gl.glPushMatrix();
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
// X軸回転
gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f);
// 12面体を描画
glut.glutSolidDodecahedron();
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1177a.png"), width, height, true);
context.release();
context.destroy();
}
}


JOGLで描画した12面体

動作環境
JDK6 Upadate13, JOGL 1.1.1

ImageMagickとPHPで背景がぼやける半透明グラデーションを描画する

Imagickで背景がぼやける半透明グラデーションを描画するには、以下のコードを実行します。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"
xml:lang="ja" dir="ltr">
<head>
<title>sample1132(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$hpad = 20;
$im = new Imagick("sf.jpg");
$iw = $im->getImageWidth();
$ih = $im->getImageWidth();
$im2 = $im->clone();
$im2->cropImage($iw-$hpad*2, $ih, $hpad, 0);
$im2->blurImage(0, 5);
$im3 = new Imagick();
$im3->newPseudoImage($iw-$hpad*2, $ih,
"gradient:#00000010-#00000070");
$im2->compositeImage($im3, Imagick::COMPOSITE_OVER,
0, 0, Imagick::CHANNEL_ALL);

$im->compositeImage($im2, Imagick::COMPOSITE_OVER,
$hpad, 0, Imagick::CHANNEL_ALL);
$im->writeImage('sample1132a.png');

$im->destroy();
?>
<img src="sample1132a.png" /><br />

</body>
</html>


元画像(sf.jpg)


出力画像(sample1132a.png)
Imagickで背景がぼやける半透明グラデーションを描画した画像

Sunday, July 12, 2009

Structure Synthで棚を描画する

Structure Synthで棚を描画するには、以下のスクリプトを実行します。

// 水平方向の板
1 * { y -1 } 11 * { y 1 } 1 * { s 10 0.1 1 color #d0d0d0 } box
// 垂直方向の板
1 * { x -6 y 5 } 11 * { x 1 } 1 * { s 0.1 10 1 color #d0d0d0 } box

// 白色の地面
{ y 0 s 200 0.1 200 color white } box

Structure Synthでの出力画像


Sunflowでの出力画像

JOGLで円柱を描画する

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


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

public class JoglSample18
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;

public static void main(String args[])
throws IOException
{
GLDrawableFactory gldf =
GLDrawableFactory.getFactory();
GLCapabilities glc = new GLCapabilities();
glc.setDoubleBuffered(false);
GLPbuffer buf = gldf.createGLPbuffer(
glc, null, width, height, null);

buf.addGLEventListener(
new GLEventListener(){
// 初期化
public void init(GLAutoDrawable dr)
{
GL gl = dr.getGL();
// 背景色
gl.glClearColor(
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
(float)0xf0/(float)0xff,
1f);
}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
GLUT glut = new GLUT();
gl.glViewport(0, 0, width, height);

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

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, -1.0f, -15.0f);

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

gl.glPushMatrix();
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
// X軸回転
gl.glRotatef(-60.0f, 1.0f, 0.0f, 0.0f);
// 円柱を描画
glut.glutSolidCylinder(2.0f, 2.5f, 16, 4);
gl.glPopMatrix();
}

public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}

public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
}
);

GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1176a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1176a.png)
JOGLで描画した円柱

動作環境
JDK6 Upadate13, JOGL 1.1.1

SWFRendererで画像の上に文字列を描画する

SWFRendererで画像の上に文字列を描画するには、以下のコードを実行します。


import java.awt.*
import groovy.swing.j2d.swf.*

def gr = new SWFRenderer()
gr.renderToFile("sample1058a.swf", 200, 200){
image(file: "sf.jpg")
font(new java.awt.Font('Tahoma', java.awt.Font.BOLD, 26))
text( x:10, y:100,
text: "San Francisco",
fill: new Color(0xbb, 0xdd, 0xff, 0xff),
borderColor: new Color(0x11, 0x33, 0x77, 0xff) )
}


出力SWF(sample1058a.swf)
SWFRendererで文字列を描画した画像

動作環境
groovy 1.6.0, JDK6 update12