Login  Register

Code for Peer Review

Posted by Pixelapp on Jul 30, 2012; 4:27pm
URL: https://forum.jogamp.org/Code-for-Peer-Review-tp4025708.html

Please review this code. I got it straight from the Google's Android Website.
If you find any poor programming techniques please feel free to let me know.

If you want to take for a spin right away, please import this source code https://dl.dropbox.com/u/60452878/TestCase.zip from my drop box account.

Also, I'm stress testing this code as of this writing.

If afterwards this code looks healthy to you guys I'll incorporate it to my application once and for all.

package pixelapp.testcase;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.glu.GLU;

import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;

public class MyRenderer implements GLEventListener
{
    private FloatBuffer mFVertexBuffer;
    private FloatBuffer mTexBuffer;
    private ShortBuffer mIndexBuffer;
   
    private FloatBuffer mFVertexBuffer2;
    private FloatBuffer mTexBuffer2;
    private ShortBuffer mIndexBuffer2;
   
    private FloatBuffer mFVertexBuffer3;
    private FloatBuffer mTexBuffer3;
    private ShortBuffer mIndexBuffer3;
   
    private FloatBuffer mFVertexBuffer4;
    private FloatBuffer mTexBuffer4;
    private ShortBuffer mIndexBuffer4;
   
    private FloatBuffer mFVertexBuffer5;
    private FloatBuffer mTexBuffer5;
    private ShortBuffer mIndexBuffer5;
   
    private FloatBuffer mFVertexBuffer6;
    private FloatBuffer mTexBuffer6;
    private ShortBuffer mIndexBuffer6;
       
        private Texture tex;
        private static ClassLoader cl;
        private GLU glu = new GLU();
       
        private int count = 0; // Used for animation
       
        private FPSAnimator animator;
       
    private float[] coords =
    {
            // X, Y, Z
    0.741524f, 0.997209f, -0.542264f,
    0.360065f, 0.995797f, -0.542264f,
    0.358783f, 0.649583f, -0.542264f,
    0.741524f, 0.649583f, -0.542264f,
    };
   
   
    private short[] indices =
    {
    0, 1, 2, 3, 0, 2,
    };
   
   
    private float[] textureCoords =
    {
    1, 1, 0, 1,
    0, 0, 1, 0,
    };
   
   
    private float[] coords2 =
    {
            // X, Y, Z
    -0.610267f, 0.000745f, -0.542264f,
    -0.991726f, -0.000666f, -0.542264f,
    -0.993008f, -0.346880f, -0.542264f,
    -0.610267f, -0.346880f, -0.542264f,
    };
   

    private short[] indices2 =
    {
    0, 1, 2,
    3, 0, 2,
    };
   
   
    private float[] textureCoords2 =
    {
    1, 1, 0, 1,
    0, 0, 1, 0,
    };
   
   
    private float[] coords3 =
    {
            // X, Y, Z
    0.077216f, 0.997209f, -0.542264f,
    -0.304243f, 0.995797f, -0.542264f,
    -0.305526f, 0.649583f, -0.542264f,
    0.077216f, 0.649583f, -0.542264f,
    };
   
   
    private short[] indices3 =
    {
    0, 1, 2,
    3, 0, 2,
    };
   
   
    private float[] textureCoords3 =
    {
    1, 1, 0, 1,
    0, 0, 1, 0,
    };
   
   
    private float[] coords4 =
    {
            // X, Y, Z
    0.108114f, 0.000745f, -0.542264f,
    -0.273345f, -0.000666f, -0.542264f,
    -0.274628f, -0.346880f, -0.542264f,
    0.108114f, -0.346880f, -0.542264f,
    };
   
    private short[] indices4 =
    {
    0, 1, 2,
    3, 0, 2,
    };
   
   
    private float[] textureCoords4 =
    {
    1, 1, 0, 1,
    0, 0, 1, 0,
    };
   
   
    private float[] coords5 =
    {
            // X, Y, Z
    0.746427f, 0.000745f, -0.542264f,
    0.364968f, -0.000666f, -0.542264f,
    0.363685f, -0.346880f, -0.542264f,
    0.746427f, -0.346880f, -0.542264f,
    };
   
    private short[] indices5 =
    {
    0, 1, 2,
    3, 0, 2,
    };
   
   
    private float[] textureCoords5 =
    {
    1, 1, 0, 1, 0, 0, 1, 0,
    };
   
   
    private float[] coords6 =
    {
            // X, Y, Z
    -0.602542f, 0.997209f, -0.542264f,
    -0.984001f, 0.995797f, -0.542264f,
    -0.985284f, 0.649583f, -0.542264f,
    -0.602542f, 0.649583f, -0.542264f,
    };
   
   
    private short[] indices6 =
    {
    0, 1, 2,
    3, 0, 2,
    };
   
   
    private float[] textureCoords6 =
    {
    1, 1, 0, 1,
    0, 0, 1, 0,
    };
   
       
        public MyRenderer()
        {
                cl = this.getClass().getClassLoader();
               
        // Buffers to be passed to gl*Pointer() functions
        // must be direct, i.e., they must be placed on the
        // native heap where the garbage collector cannot
        // move them.
        //
        // Buffers with multi-byte datatypes (e.g., short, int, float)
        // must have their byte order set to native order

        ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();

        ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
        tbb.order(ByteOrder.nativeOrder());
        mTexBuffer = tbb.asFloatBuffer();

        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
        ibb.order(ByteOrder.nativeOrder());
        mIndexBuffer = ibb.asShortBuffer();
       
       
       


        ByteBuffer vbb2 = ByteBuffer.allocateDirect(coords2.length * 4);
        vbb2.order(ByteOrder.nativeOrder());
        mFVertexBuffer2 = vbb2.asFloatBuffer();

        ByteBuffer tbb2 = ByteBuffer.allocateDirect(textureCoords2.length * 4);
        tbb2.order(ByteOrder.nativeOrder());
        mTexBuffer2 = tbb2.asFloatBuffer();

        ByteBuffer ibb2 = ByteBuffer.allocateDirect(indices2.length * 2);
        ibb2.order(ByteOrder.nativeOrder());
        mIndexBuffer2 = ibb2.asShortBuffer();
       
       
       
       
        ByteBuffer vbb3 = ByteBuffer.allocateDirect(coords3.length * 4);
        vbb3.order(ByteOrder.nativeOrder());
        mFVertexBuffer3 = vbb3.asFloatBuffer();

        ByteBuffer tbb3 = ByteBuffer.allocateDirect(textureCoords3.length * 4);
        tbb3.order(ByteOrder.nativeOrder());
        mTexBuffer3 = tbb3.asFloatBuffer();

        ByteBuffer ibb3 = ByteBuffer.allocateDirect(indices3.length * 2);
        ibb3.order(ByteOrder.nativeOrder());
        mIndexBuffer3 = ibb3.asShortBuffer();
       
       
       
       
        ByteBuffer vbb4 = ByteBuffer.allocateDirect(coords4.length * 4);
        vbb4.order(ByteOrder.nativeOrder());
        mFVertexBuffer4 = vbb4.asFloatBuffer();

        ByteBuffer tbb4 = ByteBuffer.allocateDirect(textureCoords4.length * 4);
        tbb4.order(ByteOrder.nativeOrder());
        mTexBuffer4 = tbb4.asFloatBuffer();

        ByteBuffer ibb4 = ByteBuffer.allocateDirect(indices4.length * 2);
        ibb4.order(ByteOrder.nativeOrder());
        mIndexBuffer4 = ibb4.asShortBuffer();
       
       
       
       
        ByteBuffer vbb5 = ByteBuffer.allocateDirect(coords5.length * 4);
        vbb5.order(ByteOrder.nativeOrder());
        mFVertexBuffer5 = vbb5.asFloatBuffer();

        ByteBuffer tbb5 = ByteBuffer.allocateDirect(textureCoords5.length * 4);
        tbb5.order(ByteOrder.nativeOrder());
        mTexBuffer5 = tbb5.asFloatBuffer();

        ByteBuffer ibb5 = ByteBuffer.allocateDirect(indices5.length * 2);
        ibb5.order(ByteOrder.nativeOrder());
        mIndexBuffer5 = ibb5.asShortBuffer();
       
       
       
       
       
        ByteBuffer vbb6 = ByteBuffer.allocateDirect(coords6.length * 4);
        vbb6.order(ByteOrder.nativeOrder());
        mFVertexBuffer6 = vbb6.asFloatBuffer();

        ByteBuffer tbb6 = ByteBuffer.allocateDirect(textureCoords6.length * 4);
        tbb6.order(ByteOrder.nativeOrder());
        mTexBuffer6 = tbb6.asFloatBuffer();

        ByteBuffer ibb6 = ByteBuffer.allocateDirect(indices6.length * 2);
        ibb6.order(ByteOrder.nativeOrder());
        mIndexBuffer6 = ibb6.asShortBuffer();

       
                GLProfile glp = GLProfile.getDefault();
                GLCapabilities caps = new GLCapabilities(glp);
                GLWindow miCanvas = GLWindow.create(caps);
               
        miCanvas.setTitle("Test Case");
       
        miCanvas.setPointerVisible(false);
       
        miCanvas.addWindowListener(new WindowAdapter() {
            public void windowDestroyNotify(WindowEvent arg0) {
                System.exit(0);
            };
        });
               
       
                // Indicate Canvas to detect the events of OpenGL in this Class.
                miCanvas.addGLEventListener(this);
               
                // Add a key listener to this program.
                //miCanvas.addFocusListener(new MyFocusListener());

                //miCanvas.setFocusable(true); // To receive key event
                miCanvas.requestFocus();



       

       

                // Size of the window
                miCanvas.setSize(600, 600);

                // Eliminates the windows frames look.
                miCanvas.setUndecorated(true);



                miCanvas.setFullscreen(true);

                // Renders continuously JOGL2.
                animator = new FPSAnimator(miCanvas, 60);
                animator.add(miCanvas);
                animator.start();
               


                // Make visible the object of most weight
                miCanvas.setVisible(true);
        }

        /**
         * @param args
         */
        public static void main(String[] args)
        {
                new MyRenderer();
        }

        public void display(GLAutoDrawable drawable)
        {
                GL2 gl = drawable.getGL().getGL2(); // Initialize the variable GL
               
        /*
         * By default, OpenGL enables features that improve quality
         * but reduce performance. One might want to tweak that
         * especially on software renderer.
         */
        gl.glDisable(GL2.GL_DITHER);

        gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE,
                GL2.GL_MODULATE);

        /*
         * Usually, the first thing one might want to do is to clear
         * the screen. The most efficient way of doing this is to use
         * glClear().
         */

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

        /*
         * Now we're ready to draw some 3D objects
         */

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();

        glu.gluLookAt( 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);

        gl.glActiveTexture(GL2.GL_TEXTURE0);
        tex.bind(gl);
       
        // Animation for testing
                count++;
               
                if (count == 5)
                {
                        count = 0;
                }
       
                for (int index = 0; index < coords.length; index += 3)
                {
                        coords[index] -= 0.1;
                       
                        if(count == 0)
                        {
                                coords[index] += 0.5;
                        }
                }
               
               
                for (int index = 0; index < coords2.length; index += 3)
                {
                        coords2[index] -= 0.1;
                       
                        if(count == 0)
                        {
                                coords2[index] += 0.5;
                        }
                }
               
               
                for (int index = 0; index < coords3.length; index += 3)
                {
                        coords3[index] -= 0.1;
                       
                        if(count == 0)
                        {
                                coords3[index] += 0.5;
                        }
                }
               
                for (int index = 0; index < coords4.length; index += 3)
                {
                        coords4[index] -= 0.1;
                       
                        if(count == 0)
                        {
                                coords4[index] += 0.5;
                        }
                }
               
                for (int index = 0; index < coords5.length; index += 3)
                {
                        coords5[index] -= 0.1;
                       
                        if(count == 0)
                        {
                                coords5[index] += 0.5;
                        }
                }
               
                for (int index = 0; index < coords6.length; index += 3)
                {
                        coords6[index] -= 0.1;
                       
                        if(count == 0)
                        {
                                coords6[index] += 0.5;
                        }
                }
        // End of Animation for testing
               
        mFVertexBuffer.put(coords);

        mTexBuffer.put(textureCoords);

        mIndexBuffer.put(indices);

        mFVertexBuffer.position(0);
        mTexBuffer.position(0);
        mIndexBuffer.position(0);
       
       

        mFVertexBuffer2.put(coords2);

        mTexBuffer2.put(textureCoords2);

        mIndexBuffer2.put(indices2);

        mFVertexBuffer2.position(0);
        mTexBuffer2.position(0);
        mIndexBuffer2.position(0);
       
       
       
        mFVertexBuffer3.put(coords3);

        mTexBuffer3.put(textureCoords3);

        mIndexBuffer3.put(indices3);

        mFVertexBuffer3.position(0);
        mTexBuffer3.position(0);
        mIndexBuffer3.position(0);
       
       
       
        mFVertexBuffer4.put(coords4);

        mTexBuffer4.put(textureCoords4);

        mIndexBuffer4.put(indices4);

        mFVertexBuffer4.position(0);
        mTexBuffer4.position(0);
        mIndexBuffer4.position(0);
       
       
       
       
        mFVertexBuffer5.put(coords5);

        mTexBuffer5.put(textureCoords5);

        mIndexBuffer5.put(indices5);

        mFVertexBuffer5.position(0);
        mTexBuffer5.position(0);
        mIndexBuffer5.position(0);
       
       
       
       
        mFVertexBuffer6.put(coords6);

        mTexBuffer6.put(textureCoords6);

        mIndexBuffer6.put(indices6);

        mFVertexBuffer6.position(0);
        mTexBuffer6.position(0);
        mIndexBuffer6.position(0);
       
        gl.glEnable(GL2.GL_TEXTURE_2D);
       
        gl.glPushMatrix();
        gl.glFrontFace(GL2.GL_CCW);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, mFVertexBuffer);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, mTexBuffer);
        gl.glDrawElements(GL2.GL_TRIANGLE_STRIP, indices.length,
                GL2.GL_UNSIGNED_SHORT, mIndexBuffer);
        gl.glPopMatrix();
       
       
       
        gl.glPushMatrix();
        gl.glFrontFace(GL2.GL_CCW);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, mFVertexBuffer2);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, mTexBuffer2);
        gl.glDrawElements(GL2.GL_TRIANGLE_STRIP, indices2.length,
                GL2.GL_UNSIGNED_SHORT, mIndexBuffer2);
        gl.glPopMatrix();
       
       
       
        gl.glPushMatrix();
        gl.glFrontFace(GL2.GL_CCW);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, mFVertexBuffer3);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, mTexBuffer3);
        gl.glDrawElements(GL2.GL_TRIANGLE_STRIP, indices3.length,
                GL2.GL_UNSIGNED_SHORT, mIndexBuffer3);
        gl.glPopMatrix();
       
       
       
       
        gl.glPushMatrix();
        gl.glFrontFace(GL2.GL_CCW);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, mFVertexBuffer4);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, mTexBuffer4);
        gl.glDrawElements(GL2.GL_TRIANGLE_STRIP, indices4.length,
                GL2.GL_UNSIGNED_SHORT, mIndexBuffer4);
        gl.glPopMatrix();
       
       
       
        gl.glPushMatrix();
        gl.glFrontFace(GL2.GL_CCW);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, mFVertexBuffer5);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, mTexBuffer5);
        gl.glDrawElements(GL2.GL_TRIANGLE_STRIP, indices5.length,
                GL2.GL_UNSIGNED_SHORT, mIndexBuffer5);
        gl.glPopMatrix();
       
       
        gl.glPushMatrix();
        gl.glFrontFace(GL2.GL_CCW);
        gl.glVertexPointer(3, GL2.GL_FLOAT, 0, mFVertexBuffer6);
        gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, mTexBuffer6);
        gl.glDrawElements(GL2.GL_TRIANGLE_STRIP, indices6.length,
                GL2.GL_UNSIGNED_SHORT, mIndexBuffer6);
        gl.glPopMatrix();
               
        }

        public void dispose(GLAutoDrawable drawable)
        {
               
        }

        public void init(GLAutoDrawable drawable)
        {
                GL2 gl = drawable.getGL().getGL2(); // Initialize the variable GL
               
        /*
         * By default, OpenGL enables features that improve quality
         * but reduce performance. One might want to tweak that
         * especially on software renderer.
         */
        gl.glDisable(GL2.GL_DITHER);

        /*
         * Some one-time OpenGL initialization can be made here
         * probably based on features of this particular context
         */
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT,
                GL2.GL_FASTEST);

        gl.glClearColor(.5f, .5f, .5f, 1);
        gl.glShadeModel(GL2.GL_SMOOTH);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glEnable(GL2.GL_TEXTURE_2D);
       

    try {
   
    tex = TextureIO.newTexture(
    cl.getResource("pixelapp/testcase/images/boat.jpg"), false, null);
    tex.setTexParameterf(gl, GL2.GL_TEXTURE_MIN_FILTER,
    GL2.GL_LINEAR);
    tex.setTexParameterf(gl, GL2.GL_TEXTURE_MAG_FILTER,
    GL2.GL_LINEAR);
    tex.setTexParameterf(gl,  GL2.GL_TEXTURE_WRAP_S,
                    GL2.GL_REPEAT);
    tex.setTexParameterf(gl,  GL2.GL_TEXTURE_WRAP_S,
                    GL2.GL_REPEAT);
   
    } catch (Exception e) {
    System.out.println("Error loading texture");
    }


   

               
        }

        public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int w,
                        int h)
        {
                GL2 gl = drawable.getGL().getGL2(); // Initialize the variable GL
               
        gl.glViewport(0, 0, w, h);

        /*
        * Set our projection matrix. This doesn't have to be done
        * each time we draw, but usually a new projection needs to
        * be set when the viewport is resized.
        */

        float ratio = (float) w / h;
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
        }

}