Login  Register

Re: Steps to build Android Demo Application with JOGL

Posted by Vicky on Nov 16, 2014; 7:39am
URL: https://forum.jogamp.org/Steps-to-build-Android-Demo-Application-with-JOGL-tp4033491p4033558.html

Hi

I had tried to run demo RedSquare example on desktop and android it worked fine.

I had started to work on GL2ES2 and trying to create demo app which plot a point on screen as soon as mouse click is performed in case of desktop and screen click in case of mobile.

Here is my code which I had taken reference from ReqSquare demo example

public class GLPoint extends GLWindow implements GLEventListener,
                com.jogamp.newt.event.MouseListener {

        private ShaderState st;
        private PMVMatrix pmvMatrix;
        private GLUniformData pmvMatrixUniform;
        private GLArrayDataServer vertices;
        private GLArrayDataServer colors;

        private int swapInterval = 0;
        private float aspect = 1.0f;

        private boolean clearBuffers = true;

        private ArrayList<Float> mouseX = new ArrayList<Float>();
        private ArrayList<Float> mouseY = new ArrayList<Float>();

        protected GLPoint(Window arg0) {
                super(arg0);
                this.swapInterval = 1;
                addGLEventListener(this);
                addMouseListener(this);
        }

        public void setAspect(final float aspect) {
                this.aspect = aspect;
        }

        public void setClearBuffers(final boolean v) {
                clearBuffers = v;
        }

        @Override
        public void init(final GLAutoDrawable glad) {

                final GL2ES2 gl = glad.getGL().getGL2ES2();

                System.err.println("RedSquareES2 init on " + Thread.currentThread());
                System.err.println("Chosen GLCapabilities: "
                                + glad.getChosenGLCapabilities());
                System.err.println("INIT GL IS: " + gl.getClass().getName());
                System.err
                                .println(JoglVersion.getGLStrings(gl, null, false).toString());
                if (!gl.hasGLSL()) {
                        System.err.println("No GLSL available, no rendering.");
                        return;
                }
                st = new ShaderState();
                st.setVerbose(true);
                final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER,
                                this.getClass(), "shader", "shader/bin", "RedSquareShader",
                                true);
                final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER,
                                this.getClass(), "shader", "shader/bin", "RedSquareShader",
                                true);
                vp0.defaultShaderCustomization(gl, true, true);
                fp0.defaultShaderCustomization(gl, true, true);
                final ShaderProgram sp0 = new ShaderProgram();
                sp0.add(gl, vp0, System.err);
                sp0.add(gl, fp0, System.err);
                st.attachShaderProgram(gl, sp0, true);

                // setup mgl_PMVMatrix
                pmvMatrix = new PMVMatrix();
                pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
                pmvMatrix.glLoadIdentity();
                pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
                pmvMatrix.glLoadIdentity();
                pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4,
                                pmvMatrix.glGetPMvMatrixf()); // P, Mv
                st.ownUniform(pmvMatrixUniform);
                st.uniform(gl, pmvMatrixUniform);

                // Allocate Vertex Array
                //allocateVertexArray(gl);

                // Allocate Color Array
                //allocateColorArray(gl);

                // OpenGL Render Settings
                gl.glEnable(GL.GL_DEPTH_TEST);
                st.useProgram(gl, false);

                System.err.println(Thread.currentThread() + " RedSquareES2.init FIN");
        }

        public void allocateVertexArray(GL2ES2 gl) {
                vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 3, GL.GL_FLOAT,
                                false, mouseX.size(), GL.GL_STATIC_DRAW);
                for(int i=0;i<mouseX.size();i++) {
                        /*vertices.putf(1);
                        vertices.putf(0);*/
                       
                        /*vertices.putf(111);
                        vertices.putf(230);*/
                       
                        vertices.putf(mouseX.get(i));
                        vertices.putf(mouseY.get(i));
                       
                }
                vertices.seal(gl, true);
                st.ownAttribute(vertices, true);
                vertices.enableBuffer(gl, false);
        }
       
        public void allocateColorArray(GL2ES2 gl) {
                colors = GLArrayDataServer.createGLSL("mgl_Color", 4, GL.GL_FLOAT,
                                false,mouseX.size() , GL.GL_STATIC_DRAW);
                for(int i=0;i<mouseX.size();i++) {
                        colors.putf(0);
                        colors.putf(0);
                        colors.putf(1);
                        colors.putf(1);
                }
                colors.seal(gl, true);
                st.ownAttribute(colors, true);
                colors.enableBuffer(gl, false);
        }
       
        @Override
        public void display(final GLAutoDrawable glad) {

                final GL2ES2 gl = glad.getGL().getGL2ES2();
                if (clearBuffers) {
                        gl.glClearColor(0, 0, 0, 0);
                        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                }
                if (!gl.hasGLSL()) {
                        return;
                }
                st.useProgram(gl, true);
               
                pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
                pmvMatrix.glLoadIdentity();
                pmvMatrix.glTranslatef(0, 0, -10);
                st.uniform(gl, pmvMatrixUniform);

                // Draw a point
                if(mouseX.size()<=0)
                        return;
                allocateVertexArray(gl);
                allocateColorArray(gl);
                vertices.enableBuffer(gl, true);
                colors.enableBuffer(gl, true);
                gl.glDrawArrays(GL.GL_POINTS, 0, mouseX.size());
                vertices.enableBuffer(gl, false);
                colors.enableBuffer(gl, false);
                st.useProgram(gl, false);
        }

        int width,height;
       
        @Override
        public void reshape(final GLAutoDrawable glad, final int x, final int y,
                        final int width, final int height) {
                final GL2ES2 gl = glad.getGL().getGL2ES2();
                if (-1 != swapInterval) {
                        gl.setSwapInterval(swapInterval);
                }
                this.width = width;
                this.height = height;
                reshapeImpl(gl, x, y, width, height, width, height);
        }

        void reshapeImpl(final GL2ES2 gl, final int tileX, final int tileY,
                        final int tileWidth, final int tileHeight, final int imageWidth,
                        final int imageHeight) {

                if (!gl.hasGLSL()) {
                        return;
                }

                st.useProgram(gl, true);
                // Set location in front of camera
                pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
                pmvMatrix.glLoadIdentity();

                // compute projection parameters 'normal' perspective
                final float fovy = 45f;
                final float aspect2 = ((float) imageWidth / (float) imageHeight)
                                / aspect;
                final float zNear = 1f;
                final float zFar = 100f;

                // compute projection parameters 'normal' frustum
                final float top = (float) Math.tan(fovy * ((float) Math.PI) / 360.0f)
                                * zNear;
                final float bottom = -1.0f * top;
                final float left = aspect2 * bottom;
                final float right = aspect2 * top;
                final float w = right - left;
                final float h = top - bottom;

                // compute projection parameters 'tiled'
                final float l = left + tileX * w / imageWidth;
                final float r = l + tileWidth * w / imageWidth;
                final float b = bottom + tileY * h / imageHeight;
                final float t = b + tileHeight * h / imageHeight;

                pmvMatrix.glFrustumf(l, r, b, t, zNear, zFar);

                st.uniform(gl, pmvMatrixUniform);
                st.useProgram(gl, false);

               
               
                System.err
                                .println(Thread.currentThread() + " RedSquareES2.reshape FIN");
        }

        @Override
        public void dispose(final GLAutoDrawable glad) {

                final GL2ES2 gl = glad.getGL().getGL2ES2();
                if (!gl.hasGLSL()) {
                        return;
                }
                st.destroy(gl);
                st = null;
                pmvMatrix = null;
                System.err
                                .println(Thread.currentThread() + " RedSquareES2.dispose FIN");
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
                System.out.println("Mouse Clicked X" + arg0.getX() + " ");
                System.out.println("Mouse Clicked Y" + arg0.getY() + " ");
                float coor[] = getWorldCoordinate(arg0.getX(),arg0.getY());
                System.out.println("Converting.........");
                System.out.println("X="+coor[0]);
                System.out.println("Y="+coor[1]);
                mouseX.add(coor[0]);
                mouseY.add(coor[1]);
        }

        @Override
        public void mouseDragged(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }

        @Override
        public void mouseMoved(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }

        @Override
        public void mouseWheelMoved(MouseEvent arg0) {
                // TODO Auto-generated method stub

        }
       
        private float[] getWorldCoordinate( int x, int y) {
            int[] viewport = {0, 0, width, height};
            float[] obje = new float[3];
            pmvMatrix.gluUnProject((float)x, (float)y, (float)0, viewport, 0, obje , 0);
            return obje;
        }
}

ReqSquare.vp
// Copyright 2010 JogAmp Community. All rights reserved.

#if __VERSION__ >= 130
  #define attribute in
  #define varying out
#endif

uniform mat4    mgl_PMVMatrix[2];
attribute vec4    mgl_Vertex;
attribute vec4    mgl_Color;
varying vec4    frontColor;

void main(void)
{
  frontColor=mgl_Color;
  gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex;
  gl_PointSize = 10.0;
}

ReqSquare.fp
// Copyright 2010 JogAmp Community. All rights reserved.

#if __VERSION__ >= 130
  #define varying in
  out vec4 mgl_FragColor;
#else
  #define mgl_FragColor gl_FragColor  
#endif

varying vec4 frontColor;

void main (void)
{
    mgl_FragColor = frontColor;
}

Points are getting draw surrounding the screen center not on the screen coordinate position where mouse clicked is performed.

Any help would be highly appreciated.

Thanks,
Vicky