0

I'm starting to learn OpenGL, and are using the following site: http://www.learnopengles.com/android-lesson-one-getting-started/

But it seems that I got a problem at this part (works fine in portrait mode):

private float[] mViewMatrix = new float[16];

/** Store the projection matrix. This is used to project the scene onto a 2D viewport. */
private float[] mProjectionMatrix = new float[16];

/** Allocate storage for the final combined matrix. This will be passed into the shader program. */
private float[] mMVPMatrix = new float[16];

/** This will be used to pass in the transformation matrix. */
private int mMVPMatrixHandle;

/** This will be used to pass in model position information. */
private int mPositionHandle;

/** This will be used to pass in model color information. */
private int mColorHandle;

/** How many bytes per float. */
private final int mBytesPerFloat = 4;

/** How many elements per vertex. */
private final int mStrideBytes = 7 * mBytesPerFloat;    

/** Size of the position data in elements. */
private final int mPositionDataSize = 3;

/** Offset of the color data. */
private final int mColorOffset = 3;

/** Size of the color data in elements. */
private final int mColorDataSize = 4;

public void onSurfaceChanged(GL10 gl, int width, int height)
{       
    GLES20.glViewport(0, 0, width, height);

    // Create a new perspective projection matrix. The height will stay the same
    // 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 = 10.0f;

    System.out.println("Height: " + height);
    System.out.println("Width: " + width);

    Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
}

public void onDrawFrame(GL10 gl)
{
        final float eyeX = 0.0f; 
        final float eyeY = 0.0f;
        final float eyeZ = 1.5f;

        final float lookY = 0.0f; //Y direction of what user see
        final float lookZ = -5.0f;  //Z direction of what user see

        // 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;

        GLES20.glClearColor(red, green, blue, clearcoloralpha);
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);    
        Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, xax, lookY, lookZ, upX, upY, upZ);

        // Draw the triangle facing straight on.        
        for(int i = 0; i < Triangles.size(); i++)
        {
            Matrix.setIdentityM(Triangles.get(i).getModelMatrix(), 0);

            if(Triangles.get(i).Rotate())
            {
                Triangles.get(i).rotation = (360.0f / 10000.0f) * ((int) Triangles.get(i).last);
                Triangles.get(i).last+=20;

                //Rotates the matrix by rotation degrees
                Matrix.rotateM(Triangles.get(i).getModelMatrix(), 0, Triangles.get(i).rotation, 0.0f, 0.0f, 1.0f);
            }
            else
                Matrix.rotateM(Triangles.get(i).getModelMatrix(), 0, Triangles.get(i).rotation, 0.0f, 0.0f, 1.0f);

            drawTriangle(Triangles.get(i).getFloatBuffer(),Triangles.get(i));
        }
}

private void drawTriangle(final FloatBuffer aTriangleBuffer, Triangle tri)
{       
    aTriangleBuffer.position(0);
    GLES20.glVertexAttribPointer(mPositionHandle, tri.DataSize, GLES20.GL_FLOAT, false, mStrideBytes, aTriangleBuffer);                
    GLES20.glEnableVertexAttribArray(mPositionHandle);        

    aTriangleBuffer.position(3);
    GLES20.glVertexAttribPointer(mColorHandle, tri.ColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, aTriangleBuffer);                
    GLES20.glEnableVertexAttribArray(mColorHandle);

    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, tri.getModelMatrix(), 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);     
}

But when I try to move a triangle (to the left or right) in landscape mode the triangles get "cut off" (does not display the whole triangle) when moving them to far to one of the sides. It seems that they are been acted on as if they were outside the screen when they actually are not. As mentioned it seems to work fine in portrait mode.

Height is 752 and Width 1280 in landscape mode (Galaxy Tab 2).

Does this have something to do with the Project Matrix which is set here?

Thanks for any help!

Araw
  • 2,410
  • 3
  • 29
  • 57
  • 1
    Is 752x1280 correct for landscape mode? I think it should be the other way around. – harism Jan 06 '13 at 13:02
  • @harism You're totally right, thanks for pointing it out. Post is updated :) – Araw Jan 06 '13 at 13:05
  • may need to see more of your code. this looks ok to me ... – twigg Jan 06 '13 at 14:25
  • 1
    @jtwigg Thanks for your reply. Added more of the code. The onSurfaceCreated() is the same as in the tutorial and it takes a lot space (which is why I don't paste it here). – Araw Jan 06 '13 at 14:31
  • @jtwigg As I wrote this I think I found the error: When I press on the screen I change the camera position "Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, xax, lookY, lookZ, upX, upY, upZ);" //xax is the coordinate that gets changed. The xax is the coordinate of the camera, So I change the cameras position. Is that the problem? – Araw Jan 06 '13 at 14:33
  • i've just started learning opengles 2 for android and i followed this tutorial a few days back, got it working with the values: final float eyeX = 0.0f; final float eyeY = 0.0f; final float eyeZ = 1.5f; final float lookX = 0.0f; final float lookY = 0.0f; final float lookZ = -5.0f; final float upX = 0.0f; final float upY = 1.0f; final float upZ = 0.0f; – twigg Jan 06 '13 at 14:47
  • 1
    @jtwigg Yeah, that works fine, because then the camera always will be focusing on the same spot. I want to make the triangle move to the left/right. But the way I was doing it was be moving the camera position. So that's why it seemed that it cut the triangle :) So I should change the coordinates of the triangle itself, not the camera position :) – Araw Jan 06 '13 at 14:59
  • awesome. don't change the triangle coords though, just translate the triangle matrix :D – twigg Jan 06 '13 at 15:01
  • @jtwigg Thanks for for the Matrix.translate() tip. Works perfectly. Write it as an answer and I'll accept it :) – Araw Jan 06 '13 at 15:06

1 Answers1

0

You were right, the problem was you were moving you camera :D

xax should have stayed as 0.0f

Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, xax, lookY, lookZ, upX, upY, upZ);

twigg
  • 146
  • 6