尝试使用 Ortho 绘制 2D [英] Trying to use Ortho for drawing 2D

查看:68
本文介绍了尝试使用 Ortho 绘制 2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置用于在 3D 场景之上绘制 2D 的正交视图时遇到问题.

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

我这样设置我的视图:

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();
}

场景和屏幕对象是 3D 和 2D 的绘图对象.屏幕对象还没有绘制任何东西,因为我刚开始使用 2D Ortho 视图.然而,场景对象绘制一个立方体.所以一切正常,直到我为 2D Ortho 绘图添加新代码 - 从 glPushMatrix() 到 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.

那么 onSurfaceOrtho() 函数中的代码是什么来清除我的屏幕?

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

我希望在调用设置正交视图之前已绘制到视图中的内容被保留下来.我没有调用任何明确的清除位,但它似乎仍然清除了我的观点 - 这是 onSurfaceOrtho() 函数中的代码.

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.

如何正确设置 2D 正交视图,以便在 3D 视图上绘制 2D?

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

推荐答案

所以我想出了在 Martinho Fernandes 的帮助下该怎么做.

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

我设置了两个函数:

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();
}

然后当框架被绘制时:

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 );
}

所以对于每一帧,我必须在绘制 3D 之前调用我的函数来设置透视图,然后在绘制 2D 之前调用我的函数来设置正射.

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.

我不确定这是否存在性能问题,或者我是否可以对此做些什么.我知道完整"的 OpenGL 有一些列表,可以在这里有所帮助......

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...

这篇关于尝试使用 Ortho 绘制 2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆