使用 OpenGL 代替 Canvas - Android

Using OpenGL to replace Canvas - Android(使用 OpenGL 代替 Canvas - Android)
本文介绍了使用 OpenGL 代替 Canvas - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试用更快的 opengl-es 表面替换我已经拥有的基于 Canvas 的渲染系统,但是,我似乎无法让 openGL 渲染器以充当 2d 字段的方式符合要求,而不是透视图.

I am attempting to replace the Canvas-based rendering system that I already have with the faster opengl-es surface, however, I can't seem to get an openGL renderer to conform in such a way that it acts as 2d field, rather than a perspective view.

我当前的渲染器代码如下:

My current code for the renderer looks as follows:

     @Override
     public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 1.0f);

        gl.glShadeModel(GL10.GL_FLAT);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
        gl.glEnable(GL10.GL_TEXTURE_2D);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glDisable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glDisable(GL10.GL_DITHER);
        gl.glDisable(GL10.GL_LIGHTING);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    }

如何设置渲染器以使平移转换与屏幕上的像素匹配?(所以向右平移 5 会将其移动 5 个像素)

How would I setup the renderer so that the translate transformation would match up with the pixels on the screen? (so translating 5 to the right would move it 5 pixels)

推荐答案

请注意,在 openGL 中,Y 坐标是倒置的.否则一切都是一样的.
至于正确的标志,我建议你查看开源的安卓游戏副本岛:http://code.google.com/p/replicaisland/

Please note that in openGL that Y co-ordinate is inverted. Otherwise all is the same.
As for the correct flags, I recommend you check out the open source android game replica island: http://code.google.com/p/replicaisland/

这是我在自己的代码中使用的:

Here's what I use in my own code:

public void onSurfaceChanged(GL10 gl, int width, int height) 
{
    mViewWidth = width;
    mViewHeight = height;

    gl.glViewport(0, 0, mViewWidth,  mViewHeight);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) 
{
    gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glShadeModel(GL10.GL_FLAT);
    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 

    gl.glViewport(0, 0, mViewWidth,  mViewHeight);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glShadeModel(GL10.GL_FLAT);
    gl.glEnable(GL10.GL_TEXTURE_2D);

    GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}

在哪里 mViewWidth &mViewHeight 是显示的大小.

Where mViewWidth & mViewHeight are the size of the display.

这篇关于使用 OpenGL 代替 Canvas - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)