Login  Register

Re: Offscreen buffering and antialiasing not working?

Posted by Andre on May 28, 2023; 4:42pm
URL: https://forum.jogamp.org/Offscreen-buffering-and-antialiasing-not-working-tp4042649p4042658.html

@gouessej:
Tried much, did not see it yet. Normally without offscreen it works.
Below, better? see GL2. isnt it save if you store it into a field? Heard of it but no reason.
What or where are the Unit tests?

THX



import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLDrawableFactory;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;

import java.awt.image.BufferedImage;

public class GLOffscreen {

        private GLProfile profile;
        private GLCapabilities capabilities;
        private GLDrawableFactory factory;
        private GLAutoDrawable drawable;
        private GLOffscreenListener listener;
        private AWTGLReadBufferUtil glReadBufferUtil;

        public GLOffscreen(int width, int height) {
                this.profile = GLProfile.getDefault();
                this.capabilities = new GLCapabilities(profile);
                this.capabilities.setHardwareAccelerated(true);
                this.capabilities.setDoubleBuffered(false);
                this.capabilities.setAlphaBits(8);
                this.capabilities.setOnscreen(false);
// this.capabilities.setSampleBuffers(true);
// this.capabilities.setNumSamples(4);
                this.factory = GLDrawableFactory.getFactory(profile);
                this.drawable = factory.createOffscreenAutoDrawable(null, capabilities, null, width, height);
                this.drawable.display();
                this.drawable.getContext().makeCurrent();
                this.glReadBufferUtil = new AWTGLReadBufferUtil(this.profile, true);
        }

        public void addListener(GLOffscreenListener listener) {
                this.listener = listener;
                this.listener.init(this.drawable);
        }

        public void display() {
                this.listener.display(this.drawable);
        }

        public BufferedImage getBufferedImage(int x, int y, int width, int height) {
                GL2 gl = drawable.getGL().getGL2();
                return this.glReadBufferUtil.readPixelsToBufferedImage(gl, x, y, width, height, true);
        }

        public void clear() {
                GL2 gl = drawable.getGL().getGL2();
                gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
        }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import com.jogamp.opengl.GLAutoDrawable;

public interface GLOffscreenListener {

        public abstract void init(GLAutoDrawable drawable);

        public abstract void display(GLAutoDrawable drawable);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;

import kito.gl.GLOffscreen;
import kito.gl.GLOffscreenListener;
import kito.gl.GLUtil;
import kito.util.Check;

public class Example_Jogl_Offscreen_Rendering implements GLOffscreenListener {

        static int size = 1024;
        static GLOffscreen offscreen;

        public static void main(String[] args) throws Exception {
                offscreen = new GLOffscreen(size, size);
                offscreen.addListener(new Example_Jogl_Offscreen_Rendering());
                offscreen.display();
        }

        @Override
        public void init(GLAutoDrawable drawable) {
                GL2 gl = drawable.getGL().getGL2();
                gl.glViewport(0, 0, size, size);
                gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
                gl.glLoadIdentity();
                gl.glOrtho(0d, size, size, 0d, -1d, 1d);
                gl.glDisable(GL2.GL_DEPTH_TEST);
        }

        @Override
        public void display(GLAutoDrawable drawable) {
                GL2 gl = drawable.getGL().getGL2();
                GLUtil.drawOval2(gl, size / 2f, size / 2f, size / 25, .1f, 64, 1f, 1f, 0f);
                new Check(offscreen.getBufferedImage(0, 0, size, size), null);
        }
}
I am a Robot