我正在寻找一种在GLCanvas
没有awt Robot
.
这是我当前的设置:
构造函数:
glcaps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
glcaps.setDoubleBuffered(true);
glcaps.setHardwareAccelerated(true);
glcanvas = new GLCanvas(glcaps);
glcanvas.setSize(720, 720);
glcanvas.addGLEventListener(this);
glcanvas
被声明为实例变量:GLCanvas glcanvas
OpenGL初始化:
@Override
public void init(GLAutoDrawable glad) {
GL2 gl = glad.getGL().getGL2();
glu = new GLU();
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
gl.glClearColor(0f, 0f, 0f, 1f);
// Some camera related code not shown
}
OpenGL显示:
public void display(GLAutoDrawable glad) {
GL2 gl = glad.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
...
// Orient camera and draw a simple cube
...
gl.glFlush();
}
截图方法:
BufferedImage b = new BufferedImage(glcanvas.getWidth(), glcanvas.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = b.createGraphics();
glcanvas.setupPrint(glcanvas.getWidth(), glcanvas.getWidth(), 50, 50, 50);
glcanvas.print(g);
try {
ImageIO.write(b, "png", new File("test.png"));
} catch (IOException ex) {
// Error handling
}
glcanvas.releasePrint();
g.dispose();
这种方法有效,因为在没有崩溃的情况下执行,但是我得到的 png 文件只是黑色的,没有立方体。我也尝试使用 glReadPixels 但这也不起作用,因为它只是给了我一个充满 0 的缓冲区(黑色)。
我认为问题在于我没有glcanvas
从绘图线程中读取。这是错误吗,如果是,我该如何解决?
所有答案表示赞赏!