skip to Main Content

I am new to OpenGL, and have recently successfully drawn my first shapes with the guide on the Android Developers website. Right now I am trying to only focus on the upper half of the graph on my OpenGL 2D rendering. So as you guys know, right smack in the middle of the screen is (0,0). However now I want the origin to be at the middle of the bottom of the screen, while maintaining the y axis 0.5f value to be at the middle of the screen and 1f at the top of the screen. In essence, the negative y axis portion is not in view.

These are the coordinates of my square:

float squareCoords[] = {
    //   x  ,   y   ,  z 
       -0.5f,  0.5f , 0.0f //top left

       -0.5f,   0f  , 0.0f //bottom left

        0.5f,   0f  , 0.0f //bottom right

        0.5f,  0.5f , 0.0f //top right

};

This is how I want the square to look on the screen

Ive tried using the camera to focus view but it makes the view bigger(the max y-value and x-value increases, and the object becomes smaller) in the renderer class:

Matrix.setLookAtM(viewMatrix,0,0,0.5f,3,0f,0.5f,0f,0f,1.0f,0.0f)

Does it have something to with GLES20.glViewport? The best I can come up with from online research is a function ortho2d() but it seems like Android Studio does not support it. Any help appreciated.Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    Found the soln after further trial and error

    In the renderer class at onSurfaceChanged(GL10 unused, int width ,int height){ //edit the Matrix.frustumM method, value for parameter bottom should be set to 0

    Matrix.frustumM(projectionMatrix,0,left,right,bottom,top,near,far)
    

    }

    Should help with people who want to change the position of their origins to any other location as well, simply change the left,right,top,bottom values.

    or refer to this stackoverflow: Set origin to top-left corner of screen in OpenGL ES 2 for other possible solutions.


  2. You can use Matrix.orthoM

    Matrix.orthoM(
        projectionMatrix, 0,
        left, right,
        bottom, top,
        near, far
    );
    

    And multiply it with viewMatrix set with Matrix.setLookAtM to obtain View-Projection matrix:

    Matrix.multiplyMM(
        vpMatrix, 0,
        projectionMatrix, 0, viewMatrix, 0
    );
    

    Then use vpMatrix in your shader

    void main() {
        gl_Position = u_VpMatrix * a_Position;
    }
    
    ...
    
    int uVpMatrix = glGetUniformLocation(program, "u_VpMatrix");
    
    ...
    
    glUniformMatrix4fv(uVpMatrix, 1, false, vpMatrix, 0);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search