Thursday, August 20, 2009

JOGLでピラミッドを描画する

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


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

public class JoglSample56
{
// 出力画像サイズ
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)0xf0,
(float)0xf0/(float)0xf0,
(float)0xf0/(float)0xf0,
1f);

}

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, -1.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_AUTO_NORMAL);
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(40.0f, 1.0f, 0.0f, 0.0f);
// Y軸回転
gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f);

// ピラミッドサイズ
double ph = 2.0;
double ps = 3.0;

drawTriangle(gl,
0.0, ph, 0.0,
ps/2.0, 0.0, -ps/2.0,
-ps/2.0, 0.0, -ps/2.0);

drawTriangle(gl,
0.0, ph, 0.0,
-ps/2.0, 0.0, ps/2.0,
ps/2.0, 0.0, ps/2.0);

drawTriangle(gl,
0.0, ph, 0.0,
-ps/2.0, 0.0, -ps/2.0,
-ps/2.0, 0.0, ps/2.0);

drawTriangle(gl,
0.0, ph, 0.0,
ps/2.0, 0.0, ps/2.0,
ps/2.0, 0.0, -ps/2.0);

gl.glBegin(GL.GL_POLYGON);
gl.glNormal3d(0,-1,0);
gl.glVertex3d(-ps/2, 0.0, ps/2);
gl.glVertex3d(-ps/2, 0.0, -ps/2);
gl.glVertex3d(ps/2, 0.0, -ps/2);
gl.glVertex3d(ps/2, 0.0, ps/2);
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){}

private void drawTriangle(GL gl,
double p1x, double p1y, double p1z,
double p2x, double p2y, double p2z,
double p3x, double p3y, double p3z)
{
gl.glBegin(GL.GL_POLYGON);

gl.glNormal3d((p1x+p2x+p3x)/3,
1, (p1z+p2z+p3z)/3);
gl.glVertex3d(p1x, p1y, p1z);
gl.glNormal3d((p1x+p2x+p3x)/3,
0, (p1z+p2z+p3z)/3);
gl.glVertex3d(p2x, p2y, p2z);
gl.glNormal3d((p1x+p2x+p3x)/3,
0, (p1z+p2z+p3z)/3);
gl.glVertex3d(p3x, p3y, p3z);

gl.glEnd();
}
}
);

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


出力画像(sample1214a.png)
JOGLで描画したピラミッド

動作環境
JDK6 Upadate13, JOGL 1.1.1

No comments:

Post a Comment