Trying to use Ortho for drawing 2D

[亡魂溺海] 提交于 2020-01-25 04:06:05

问题


I'm having trouble with setting up an Ortho view for drawing 2D ontop of the 3D scene.

I set up my view like this:

public void onSurfaceChanged( GL10 gl, int width, int height ) {
    gl.glViewport( 0, 0, width, height );
    gl.glMatrixMode( GL10.GL_PROJECTION );
    gl.glLoadIdentity();
    GLU.gluPerspective( gl, 45.0f, ( ( float )width / ( float )height ), 0.1f, 100.0f );
    gl.glMatrixMode( GL10.GL_MODELVIEW );
    gl.glLoadIdentity();
}

and I set up my Ortho view like this:

public void onSurfaceOrtho( GL10 gl ) {
    gl.glOrthof( 0.0f, 100.0f, 100.0f, 0.0f, -0.1f, 0.1f );
    gl.glMatrixMode( GL10.GL_PROJECTION );
    gl.glLoadIdentity();
}

Then I draw my frame like this:

public void onDrawFrame( GL10 gl ) {
    gl.glClear( GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT );
    gl.glLoadIdentity();
    scene.onDrawFrame( gl );
    gl.glPushMatrix();
    onSurfaceOrtho( gl );
    screen.onDrawFrame( gl );
    gl.glPopMatrix();
}

The scene and screen objects are drawing objects for 3D and 2D. The screen object doesn't draw anything yet as I'm just starting with 2D Ortho views. However the scene objects draw a cube. So everything works ok until I add the new code for 2D Ortho drawing - from glPushMatrix() to glPopMatrix(). Now I don't see anything but my clear color.

So what is it the code in the function onSurfaceOrtho() does that clears my screen?

I would expect what has been drawn to the view before the call to set up an Ortho view, to be left alone. I don't call any explicit clear bits, but still it seems like something clears my view - and it is the code in the onSurfaceOrtho() function.

How can I set up an 2D Ortho view correctly so I can draw 2D ontop of my 3D view?


回答1:


So I figured out what to do with a little help from Martinho Fernandes.

I set up two functions:

public void onSurfaceOrtho( GL10 gl ) {
    gl.glMatrixMode( GL10.GL_PROJECTION );
    gl.glLoadIdentity();
    gl.glOrthof( 0.0f, 100.0f, 0.0f, 100.0f, -0.1f, 0.1f );
    gl.glMatrixMode( GL10.GL_MODELVIEW );
    gl.glLoadIdentity();
}

public void onSurfacePerspective( GL10 gl ) {
    gl.glMatrixMode( GL10.GL_PROJECTION );
    gl.glLoadIdentity();
    GLU.gluPerspective( gl, 45.0f, aspect, 0.1f, 100.0f );
    gl.glMatrixMode( GL10.GL_MODELVIEW );
    gl.glLoadIdentity();
}

Then when the frame is draw:

public void onDrawFrame( GL10 gl ) {
    gl.glClear( GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT );
    gl.glLoadIdentity();
    onSurfacePerspective( gl );
    scene.onDrawFrame( gl );
    onSurfaceOrtho( gl );
    screen.onDrawFrame( gl );
}

So for each frame I have to call my function for setting the perspective before drawing 3D, and then call my function for ortho before drawing 2D.

I'm not sure if this have performance issues, or if there are something I can do about it. I know "full" OpenGL have lists that can help a bit here...



来源:https://stackoverflow.com/questions/4189137/trying-to-use-ortho-for-drawing-2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!