Wednesday, August 19, 2009

JOGLで複数の直方体の山を描画する

JOGLで複数の直方体の山を描画するには、以下のコードを実行します。


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

public class JoglSample55
{
// 出力画像サイズ
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)0x58/(float)0xff,
(float)0x50/(float)0xff,
(float)0x50/(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.5f, -30.0f);
gl.glTranslatef(-3.0f, -1.5f, -7.0f);

gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_LIGHT1);
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);

// フォグの設定
float fog[]={0.2f,0.2f,0.3f,1.0f};
gl.glFogfv(GL.GL_FOG_COLOR,fog,0);
gl.glFogi(GL.GL_FOG_MODE,GL.GL_EXP);
gl.glFogf(GL.GL_FOG_DENSITY, 0.04f);
gl.glFogf(GL.GL_FOG_START, 0.0f);
gl.glFogf(GL.GL_FOG_END,20.0f);
gl.glEnable(GL.GL_FOG);

// 光源の位置
float l0pos[] = {-5.0f, 10.0f, 0.0f, 1.0f};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, l0pos, 0);
// 環境光
float l0amb[] = {0.1f, 0.1f, 0.1f, 1.0f};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, l0amb, 0);
// 拡散光
float l0dif[] = {0.9f, 0.9f, 0.9f, 1.0f};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, l0dif, 0);
// 鏡面光
float l0spe[] = {1.0f, 1.0f, 1.0f, 1.0f};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, l0spe, 0);

gl.glClear(GL.GL_COLOR_BUFFER_BIT
|GL.GL_DEPTH_BUFFER_BIT );

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

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

float cs = 0.25f;
float gs = 0.3f;
for(int lz=-10;lz<60;lz++){
for(int lx=-10;lx<100;lx++){
gl.glPushMatrix();
float de = (gs*lx-5)*(gs*lx-5)+(gs*lz-3)*(gs*lz-3)+9;
float ch = 27/(de==0f?0.001f:de);
gl.glScalef(1f,ch, 1f);
gl.glTranslatef(lx*gs, cs*ch/2, -1*lz*gs);
glut.glutSolidCube(cs);
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("sample1213a.png"), width, height, true);
context.release();
context.destroy();
}
}


出力画像(sample1213a.png)
JOGLで描画した複数の直方体の山

動作環境
JDK6 Upadate13, JOGL 1.1.1

No comments:

Post a Comment