JOGLで凹んだポリゴンを描画するには、以下のコードを実行します。
import java.io.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
public class JoglSample59
{
// 出力画像サイズ
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();
GLU glu = new GLU();
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.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
);
GLUtessellator tess = glu.gluNewTess();
TessCB tcb = new TessCB(gl, glu);
glu.gluTessCallback(tess, GLU.GLU_TESS_VERTEX, tcb);
glu.gluTessCallback(tess, GLU.GLU_TESS_BEGIN, tcb);
glu.gluTessCallback(tess, GLU.GLU_TESS_END, tcb);
glu.gluTessCallback(tess, GLU.GLU_TESS_ERROR, tcb);
double va[][] = {
{1.0, -1.0, 0.0,},
{1.0, 1.0, 0.0},
{-1.0, 1.0, 0.0},
{-1.0, -1.0, 0.0},
{0.0, -0.5, 0.0}
};
glu.gluTessBeginPolygon(tess, null);
glu.gluTessBeginContour(tess);
for(int al=0;al<va.length;al++){
glu.gluTessVertex(tess, va[al], 0, va[al]);
}
glu.gluTessEndContour(tess);
glu.gluTessEndPolygon(tess);
gl.glPopMatrix();
}
public void reshape(
GLAutoDrawable dr,
int x, int y,
int width, int height){}
public void displayChanged(
GLAutoDrawable dr,
boolean modeChanged,
boolean deviceChanged){}
class TessCB extends
GLUtessellatorCallbackAdapter
{
private GL gl = null;
private GLU glu = null;
public TessCB(GL gla, GLU glua)
{
gl = gla;
glu = glua;
}
public void begin(int type)
{
gl.glBegin(type);
}
public void end()
{
gl.glEnd();
}
public void vertex(Object vertexData)
{
if( vertexData instanceof double[] ){
gl.glVertex3dv(
(double[])vertexData, 0);
}
}
public void error(int errnum)
{
System.out.println(
glu.gluErrorString(errnum));
System.exit(0);
}
}
}
);
GLContext context = buf.createContext(null);
context.makeCurrent();
buf.display();
Screenshot.writeToFile(
new File("sample1217a.png"), width, height, true);
context.release();
context.destroy();
}
}
出力画像(sample1217a.png)
動作環境
JDK6 Upadate13, JOGL 1.1.1