2014-04-10 9 views
1

오프 스크린 드로어 블에 쓰고 이미지에 쓰는 자바 OpenGL (JOGL) 메서드를 작성하려고합니다. 화면 상 drawable과 GLP 버퍼를 사용할 때 이것이 작동하는지 확인했지만, 현재 상태의 출력 이미지는 단색 검정색입니다. 코드는 다음과 같습니다.Java OpenGL이 이미지에 오프 스크린 버퍼를 그립니다.

GLProfile glp = GLProfile.getDefault(); 
GLCapabilities caps = new GLCapabilities(glp); 
caps.setOnscreen(false); 

// create the offscreen drawable 
GLDrawableFactory factory = GLDrawableFactory.getFactory(glp); 
GLOffscreenAutoDrawable drawable = factory.createOffscreenAutoDrawable(null,caps,null,width,height); 
drawable.display(); 
drawable.getContext().makeCurrent(); 

// a series of x/y coordinates 
FloatBuffer buffer = generateData(); 

GL2 gl = drawable.getGL().getGL2(); 

// use pixel coordinates 
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION); 
gl.glLoadIdentity();  
gl.glOrtho(0d, width, height, 0d, -1d, 1d); 

// draw some points to the drawable 
gl.glPointSize(4f); 
gl.glColor3f(1f,0f,0f);  
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); 
gl.glVertexPointer(2, GL2.GL_FLOAT, 0, buffer); 
gl.glDrawArrays(GL2.GL_POINTS, 0, numPoints); 

BufferedImage im = new AWTGLReadBufferUtil(drawable.getGLProfile(), false).readPixelsToBufferedImage(drawable.getGL(), 0, 0, width, height, true /* awtOrientation */); 
ImageIO.write(im,"png",new File("im.png")); 
+0

Github 프로젝트 jogl-demos와 단위 테스트를 살펴 보셨습니까? 내가 아는 한 GLP 버퍼는 더 이상 사용되지 않습니다. JOGL의 어떤 버전을 사용합니까? 오히려 공식 포럼에서 질문을하고 우리가 실행할 수있는 완전한 소스 코드를 제공하십시오 : http://forum.jogamp.org 어쩌면 결여 된 바인딩이나 아주 사소한 것이있을 것입니다, 나는 몇 달 전에 비슷한 실수를했습니다. – gouessej

+0

감사합니다. GLPBuffer는 더 이상 사용되지 않지만 createOffscreenDrawable과 함께 작동하는 데 문제가 있습니다. 조깅 포럼에 예제를 게시 할 것입니다. 감사. –

+0

비슷한 실수를하는 사람이 해결책을 찾을 수 있도록 작업 소스 코드를 포럼에 게시하면 매우 기쁩니다. http://forum.jogamp.org/Render-offscreen-buffer-to-image-td4032144 .html – gouessej

답변

1

이 조금 오래된,하지만 난 나를 위해 작동하는 것 같다 문제에 대한 해결책을 발견했다. 난 그냥 같은, 바로 그릴 수에 .display()를 호출하기 전에 일반 GLEventListener 객체를 추가 :

//... 
drawable.addGLEventListener(new OffscreenJOGL()); 
drawable.display(); 

//Move drawing code to OffscreenJOGL 

BufferedImage im = new AWTGLReadBufferUtil(drawable.getGLProfile(), false).readPixelsToBufferedImage(drawable.getGL(), 0, 0, width, height, true /* awtOrientation */); 
ImageIO.write(im,"png",new File("im.png")); 

코드를 사용자 정의 OffscreenJOGL 클래스에 있어야 이제 그리려면 init(...), reshape(...)display(...) 방법에 따라. 현재 컨텍스트를 설정하려면 init(...) 메서드가 OffscreenJOGL이어야합니다. 그렇지 않으면 예외가 발생합니다.

class OffscreenJOGL implements GLEventListener { 
    public void init(GLAutoDrawable drawable) { 
     drawable.getContext().makeCurrent(); 
     //Other init code here 
    } 

    public void display(GLAutodrawable drawable) { 
     //Code for drawing here 
    } 

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { 
     //Called at least once after init(...) and before display(...) 
    } 

    public void dispose(GLAutoDrawable drawable) { 
     //Dispose code here 
    } 
}