android_gl 0 Newbie Poster

I am trying to create a triangle in openGL 2.0 but some some reason background is shows up but no triangle. I am following this tutorial: http://developer.android.com/training/graphics/opengl/environment.html
i have the same code as on tutorial but for some reason the triangle is not showing up.
may be i missed some thing or i am doing some thing wrong?

i have two class.
GlRender.java
GLTriangle.java

i think the problem is in GLTriangle draw method.

public class GlRender implements Renderer {

    private GLTriangle glTriangleObj;

    private final float[] mModelMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];
    private final float[] mRotationMatrix = new float[16];

    /************************/
    /*** create() method ***/
    /************************/
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Set the background color
        // 0 to 1
        // 1 = no transparent
        GLES20.glClearColor(0.8f, 0.2f, 0.2f, 1f);
        gl.glClearDepthf(1f);

        glTriangleObj = new GLTriangle();
        // glSquare = new Square(); //etc...

        // postion the eye behind the origin
        final float eyeX = 0.0f;
        final float eyeY = 0.0f;
        final float eyeZ = 1.5f;

        // we are looking toward the distance
        final float lookX = 0.0f;
        final float lookY = 0.0f;
        final float lookZ = -5.0f;

        // Set our up vector. This is where our head would be pointing were we
        // holding the camera.
        final float upX = 0.0f;
        final float upY = 1.0f;
        final float upZ = 0.0f;

        // Set the view matrix. This matrix can be said to represent the camera
        // position.
        // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination
        // of a model and
        // view matrix. In OpenGL 2, we can keep track of these matrices
        // separately if we choose.
        Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY,
                lookZ, upX, upY, upZ);
    }

    /********************/
    /*** paint method ***/
    /********************/
    @Override
    public void onDrawFrame(GL10 gl) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

        // do a complete rotation every 10 seconds
        // long time = SystemClock.uptimeMillis() % 10000L;
        // float angleInDegree = (360.0f / 10000.0f) * ((int) time);
        // draw the triangle face straight on
        // Matrix.setIdentityM(mModelMatrix, 0);
        // Matrix.rotateM(mModelMatrix, 0, angleInDegree, 0.0f, 0.0f, 1.0f);

        // Set the camera position (View matrix)
        Matrix.setLookAtM(mViewMatrix, 0, 0, -2, 0, 0f, 0f, 0f, 0f, 2.0f, 0.0f);

        // Calculate the projection and view transformation
        Matrix.multiplyMM(mModelMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

        glTriangleObj.draw(mModelMatrix);
    }

    /*****************************/
    /*** landscape to portrait ***/
    /*****************************/
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if (height == 0)
            height = 1;

        // Set the OpenGL viewport to the same size as the surface.
        GLES20.glViewport(0, 0, width, height);

        // while the width will vary as per aspect ratio.
        final float ratio = (float) width / height;
        final float left = -ratio;
        final float right = ratio;
        final float bottom = -1.0f;
        final float top = 1.0f;
        final float near = 1.0f;
        final float far = 25.0f;

        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near,
                far);
    }
}
GLTriangle.java
[CODE]public class GLTriangle {
    // always same
    private final String vertexShaderCode = "attribute vec4 vPosition;"
            + "void main() {" + "  gl_Position = vPosition;" + "}";

    private final String fragmentShaderCode = "precision mediump float;"
            + "uniform vec4 vColor;" + "void main() {"
            + "  gl_FragColor = vColor;" + "}";

    // always start at zero when ploting a point as defult
    // trinagle Coordinates - vertex = 3(x y z)
    private float trinagleCoords[] = { 0f, 1f, // V 0 - top
            1f, -1, // V 1 - right bottom
            -1f, 1f // V 2 - left bottom
    };
    private int COORDS_PER_VERTEX = 2; // number of points
    private FloatBuffer vertexBuffer;

    private short[] drawOrder = { 0, 1, 2 }; // order
    private ShortBuffer orderBuffer;

    float color[] = { 0.5f, 1f, 0f, 1f };

    private int vertexCount = trinagleCoords.length / COORDS_PER_VERTEX;
    private int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex

    // used to pass in the transformation matrix
    private final int mProgram;
    // used to pass in model poition information
    private int mPositionHandle;
    // used to pass inmodel color information
    private int mColorHandle;

    private int mMVPMatrixHandle;

    public GLTriangle() {
        // vertices - has to be 4
        ByteBuffer byteBuffer = ByteBuffer
                .allocateDirect(trinagleCoords.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(trinagleCoords);
        vertexBuffer.position(0);

        // draw order - has to be 2
        ByteBuffer pointByteBuffer = ByteBuffer
                .allocateDirect(drawOrder.length * 2);
        pointByteBuffer.order(ByteOrder.nativeOrder());
        orderBuffer = pointByteBuffer.asShortBuffer();
        orderBuffer.put(drawOrder);
        orderBuffer.position(0);

        // shader - always same
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
                fragmentShaderCode);
        // create empty OpenGL ES Program
        mProgram = GLES20.glCreateProgram();
        // add the vertex shader to program
        GLES20.glAttachShader(mProgram, vertexShader);
        // add the fragment shader to program
        GLES20.glAttachShader(mProgram, fragmentShader);
        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);
    }

    /************/
    /*** same ***/
    /************/
    public static int loadShader(int type, String shaderCode) {

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }

    public void draw(float[] mModelMatrix) {
        // Add program to OpenGL environment
        GLES20.glUseProgram(mProgram);

        // get handle to vertex shader's vPosition member
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

        // get handle to fragment shader's vColor member
        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

        // Set color for drawing the triangle
        GLES20.glUniform4fv(mColorHandle, 1, color, 0);

        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);

        // get handle to shape's transformation matrix
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mModelMatrix, 0);

        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.