0
votes

How can I determine the bounds of the x and y coordinate planes displayed on screen in my OpenGL ES program?

I need to fill the screen with 6 identical shapes, all equal in width & height, but in order to do this I must determine what values the x and y coordinate range across (so I can properly set the shape's vertices). In other words, I need a programmatic way to find out the value of -x and x, and -y and y.

Whats the simplest way to do this? Should I be manipulating/reading the projection matrix or the modelView matrix? Neither?

I know onSurfaceChanged() has access to the layout's height and width, but i'm not certain if these parameters are necessary to find the bounds of the on-screen coordinate bounds.

Below are the code snippets that show how I configure the frustum with the modelView and projection matrices:

public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
    {  
        // Enable depth testing
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);

        // Position the eye in front of the origin.
        final float eyeX = 0.0f;
        final float eyeY = 0.0f;
        final float eyeZ = -0.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.
        Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
        ...
    }

public void onSurfaceChanged(GL10 glUnused, int width, int height)
    {
        // Set the OpenGL viewport to the same size as the surface.
        GLES20.glViewport(0, 0, width, height);

        layoutWidth = width;        //test: string graphics
        layoutHeight = height;      //test: string graphics

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

        screenSixth = findScreenSixths(width);

        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
    }
1
That was an old question based on OpenGL ES 1.0, and the solution suggests using glOrtho. This question is about OpenGL ES 2.0 so it seems unique enough to be asked imoCody
The primary solution I proposed there was to not use any transformations at all, which works equally well in ES 2.0. The approach with glOrtho() was just an alternative. The key is really to not set up any modelview and projection matrices, and that's exactly the same.Reto Koradi
Okay. How can I find the visible bounds despite using these matrices? I need these matrices for usage with my shaders.Cody

1 Answers

1
votes

This problem of yours seems a bit strange. I could understand you want to manipulate the matrix so you will see all the objects on the screen but what you are asking is how to place the spheres so they are on the screen. In this case the on-screen projection of the spheres does not change and there is no reason to use the same matrix as you do for the rest of the scene. You may simply start with the identity and add a frustum to get the correct projection. After that the border values (left, right...) will be at border of your screen for Z value of near. So place the spheres at places such as (left+radius, top+radius, near).

If you still need some specific position of the spheres due to some interaction with other objects then you will most likely need to check the on-screen projections of the billboarded bounding square of the sphere. That means creating a square with the center the same as sphere and a width of twice the radius. Then multiply these square positions with the billboarded version of the sphere matrix. The billboarding can be found over the web to do properly but unless you do some strange operations on the matrices it usually works by simply setting the top left 3x3 part of the matrix to identity.