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.*;
public class JoglSample58
{
// 出力画像サイズ
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(-6.0f, 1.0f, -35.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);
// Y軸回転
gl.glRotatef(10.0f, 0.0f, 1.0f, 0.0f);
// 面の色を設定
gl.glColor3f(
(float)0x77/(float)0xff,
(float)0x99/(float)0xff,
(float)0xff/(float)0xff
);
/* 文字列描画用イメージを作成 */
String message = "JOGL";
Font font = new Font(Font.SANS_SERIF,
Font.PLAIN, 10);
FontRenderContext frc = new FontRenderContext(
new AffineTransform(), false, false);
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());
gr.dispose();
float rs = 0.25f;
float ds = 0.45f;
for(int lz=0;lz<img.getHeight();lz++){
for(int lx=0;lx<img.getWidth();lx++){
gl.glPushMatrix();
gl.glTranslatef(ds*lx, 0f, ds*lz);
if( img.getRGB(lx, lz) != 0 ){
glut.glutSolidSphere(rs, 16, 16);
}
gl.glPopMatrix();
}
}
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("sample1216a.png"), width, height, true);
context.release();
context.destroy();
}
}
出力画像(sample1216a.png)
動作環境
JDK6 Upadate13, JOGL 1.1.1
No comments:
Post a Comment