Thursday, August 13, 2009

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

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


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

public class JoglSample49
{
// 出力画像サイズ
private static int width = 300;
private static int height = 300;
// テクスチャ
private static Texture textures[] = null;

private static String 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"
};

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)0x68/(float)0xff,
(float)0x60/(float)0xff,
(float)0x60/(float)0xff,
1f);

try
{
textures = new Texture[fn.length];
for(int fi=0;fi<fn.length;fi++){
textures[fi] =
TextureIO.newTexture(
new File(fn[fi]), true);
textures[fi].enable();
}
}
catch(IOException ioex){
ioex.printStackTrace();
}

}

public void display(GLAutoDrawable dr)
{
GL gl = dr.getGL();
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.5f, -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_AUTO_NORMAL);
gl.glEnable(GL.GL_DEPTH_TEST);
// gl.glEnable(GL.GL_CULL_FACE);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glClear(GL.GL_COLOR_BUFFER_BIT
|GL.GL_DEPTH_BUFFER_BIT );
gl.glLightModeli(GL.GL_LIGHT_MODEL_TWO_SIDE,
GL.GL_TRUE);

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

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

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

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

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("sample1207a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1207a.png)
JOGLで放射状に画像を配置した画像

No comments: