OpenGL-ES Enabling orthographic mode 2D

2019-07-15 09:57发布

问题:

I'm trying to have no size difference from sprites, if you increase the z to far away.
however i have no luck, it still gets smaller:

||EDIT||

I now have these methods

 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    float _width = 320f;
    float _height = 480f;
    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0, _width, 0, _height, 1, 100); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity();  

    // Load textures ,
    gl.glEnable(GL10.GL_TEXTURE_2D);
    for (int a = 0; a < squares.length; a++) {
        squares[a].loadGLTexture(gl, context);
    }
}  

.

public void onDrawFrame(GL10 gl) {
    //Clear Screen And Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    gl.glLoadIdentity();                    //Reset The Current Modelview Matrix
    gl.glTranslatef(.0f, 1.0f, locZ);
    squares[0].draw(gl);
    gl.glLoadIdentity();
    gl.glTranslatef(0.5f, 0.f, locZ);
    squares[1].draw(gl);        
    gl.glLoadIdentity();
    gl.glTranslatef(-0.5f, -0.5f, locZ);
    squares[2].draw(gl);
    gl.glLoadIdentity();
    gl.glTranslatef(-0.5f, -0.5f, locZ);
    squares[3].draw(gl);


    //change zvalues
    if(locZ >= 4.0f){
        speedZ *= -1.0f;
        locZ = 3.9f;
    }
    else if(locZ <= -4.0){
        speedZ *= -1.0f;
        locZ = -3.9f;
    }

    locZ += speedZ;

}  

I'm changing the z-values, and therefor the distance from the 'camera', and expecting that since I don't want to use perspective(orthographic mode), the sizes of the squares should stay constant. But they don't. Hope this helps some more.

回答1:

You have bad glOrtho parameters:

gl.glOrthof(0, width, 0, height, 0.01f, 100.0f);

Or

gl.glOrthof(0, width, height, 0, 0.01f, 100.0f);

EDIT: forget to reset matrix - glLoadIdentity.

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    /* SET NEW PROJECTION HERE: ortho or perspective */
    gl.glOrthof(0, _width, 0, _height, 0.001f, 100); 

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    /* SET NEW MODELVIEW MATRIX( space transformation ) HERE and DRAW YOUR STUFF */

    //change zvalues
    if(locZ >= 99.0f){
        speedZ *= -1.0f;
        locZ = 99.0f;
    }
    else if(locZ <= 1.0){
        speedZ *= -1.0f;
        locZ = 1.0f;
    }

}

These steps have to be done before rendering 2D, resp. moving from 3D projection to 2D projection, not when creating texture or any object. Don't know much about public void onSurfaceCreated, but it doesn't seem to be part of rendering loop.



回答2:

So the origin is in the middle of your GLSurfaceView, it's not a bad idea to do something like:

gl.glOrthof(-width/2, width/2, -height/2, height/2, 0.1f, 100.0f);


回答3:

here you could have two methods; one to switch to orthoscopic view in which one openGLUnit = one screen pixel for drawing in 2d on screen. Then the next method switches it back to 3d drawing. Do your 2d drawing after rendering 3d and first call the switchToOrtho method and when your finished call the switchBackToFrustum method.

public void switchToOrtho() {
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrthof(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -5, 1);           glMatrixMode(GL_MODELVIEW);    glLoadIdentity();
}

public void switchBackToFrustum() {
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}