import static javax.media.opengl.GL.*;
import java.io.*;
import javax.media.opengl.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.texture.*;
// 出力画像サイズ
width = 300
height = 300
GLDrawableFactory gldf = 
  GLDrawableFactory.getFactory()
GLCapabilities glc = new GLCapabilities()
glc.setDoubleBuffered(false)
GLPbuffer buf = gldf.createGLPbuffer(
  glc, null, width, height, null)
GL gl = buf.getGL()
texture = null
buf.addGLEventListener(
[ init: {
    texture = TextureIO.newTexture(
      new File("sf.jpg"), true)
  },
  display: {
    GLUT glut = new GLUT()
    gl.glViewport(0, 0, width, height)
    // 平行投影
    gl.glMatrixMode(GL_PROJECTION)
    gl.glLoadIdentity()
    gl.glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f)
    // 背景テクスチャを描画
    gl.glClear(GL_COLOR_BUFFER_BIT 
      | GL_DEPTH_BUFFER_BIT )
    gl.glDisable(GL_DEPTH_TEST)
    texture.enable()
    gl.glBindTexture(GL_TEXTURE_2D, 
      texture.getTextureObject())
    gl.glBegin(GL_QUADS)
    gl.glTexCoord2f(0.0f, 1.0f)
    gl.glVertex2f(-1.0f, -1.0f)
    gl.glTexCoord2f(0.0f, 0.0f)
    gl.glVertex2f(-1.0f, 1.0f)
    gl.glTexCoord2f(1.0f, 0.0f)
    gl.glVertex2f(1.0f, 1.0f)
    gl.glTexCoord2f(1.0f, 1.0f)
    gl.glVertex2f(1.0f, -1.0f)
    gl.glEnd()
    texture.disable()
    // 透視投影
    gl.glMatrixMode(GL_PROJECTION)
    gl.glLoadIdentity()
    float ratio = height/width as Float
    gl.glFrustum(-1.0f, 1.0f, -ratio, ratio,
      5.0f, 40.0f)
    gl.glMatrixMode(GL_MODELVIEW)
    gl.glLoadIdentity()
    gl.glTranslatef(0.0f, 0.0f, -7.0f)
    gl.glEnable(GL_LIGHTING)
    gl.glEnable(GL_LIGHT0)
    gl.glEnable(GL_COLOR_MATERIAL)
    gl.glEnable(GL_NORMALIZE)
    gl.glEnable(GL_DEPTH_TEST)
    gl.glEnable(GL_CULL_FACE)
    gl.glPushMatrix()
    // 面の色を設定
    gl.glColor3f(
      0x77/0xff as Float, 
      0x99/0xff as Float, 
      0xff/0xff as Float
    )
    // X軸回転
    gl.glRotatef(10.0f, 1.0f, 0.0f, 0.0f)
    // Y軸回転
    gl.glRotatef(20.0f, 0.0f, 1.0f, 0.0f)
    // 8面体を描画
    glut.glutSolidOctahedron()
    gl.glPopMatrix()
  },
  reshape: {},
  displayChanged: {}
] as GLEventListener
)
GLContext context = 
  buf.createContext(null)
context.makeCurrent()
buf.display()
Screenshot.writeToFile(
  new File("sample1300a.png"), 
  width, height, true)
context.release()
context.destroy()
出力画像(sample1300a.png)

関連情報
JDK1.6 Update14, Groovy 1.6.3, JOGL 1.1.1a
関連情報
groovyとJOGLのまとめ
No comments:
Post a Comment