在 Android 上设置 2D OpenGL 编程的视口

发布于 2024-11-06 11:18:00 字数 1107 浏览 1 评论 0原文

我正在尝试在 Android 上使用 OpenGL 进行 2D 编程。现在,我正处于准备阶段;尝试配置我的视口。我已经加载和绘制了纹理,但我无法让它们按照我想要的方式绘制。如果可能的话,我希望原点位于屏幕的左上角,向下增加,因为这就是我的代码(从我以前使用画布时继承的)的设置方式。我认为这很容易;类似于翻译和翻转的东西。当我尝试修改配置视口的语句时,我惊讶地发现我的应用程序显然没有受到它们的影响。请参阅下面的代码,了解 GLRenderer 的 onSurfaceChanged() 方法:

public void onSurfaceChanged(GL10 gl, int width, int height) {
    this.width = width;
    this.height = height;

    gl.glViewport(0, 0, width, height);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(gl, 0, width, height, 0);
    gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
}

我可以更改这些值,甚至可以完全注释掉这些行(glColor4x 除外),并且图形仍然显示完全相同。图形按预期大小显示,没有变形,如果将左下坐标作为原点,图形甚至会显示在正确的位置。下面是我的纹理的绘制例程,作为表示一个可绘制对象的类的一部分:

public void draw(GL10 gl, float x, float y) {
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    cropSignature = OpenGLUtil.setTextureCrop(gl,srcRectParam,cropSignature);
    ((GL11Ext)gl).glDrawTexfOES(x, y, 0, width, height);
}

其中 x、y 是要绘制的坐标,宽度、高度是纹理的尺寸,在该对象的实例化时指定。

glDrawTexfOES 是否完全绕过我建立的视口设置?如果是的话我应该怎么画呢?如何将 OpenGL 原点设置为左上角并向下增加?或者这是不好的做法,我应该反转代码的其余部分以遵守这一点吗?

I'm trying to use OpenGL for 2D programming on the Android. Right now, I'm at the setup stage; trying to configure my viewport. I've got textures loading and drawing, but I'm having trouble getting them to draw like I want. If possible, I want the origin to be at the top left of the screen, increasing downwards, as that's how my code (carried over from when I previously used the canvas) is set up. I figured this would be easy; something along the lines of translate and flip. When I tried modifying the statements that configure the viewport, I was surprised to see that apparently my application isn't even affected by them. See the code below for the onSurfaceChanged() method of the GLRenderer:

public void onSurfaceChanged(GL10 gl, int width, int height) {
    this.width = width;
    this.height = height;

    gl.glViewport(0, 0, width, height);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(gl, 0, width, height, 0);
    gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
}

I can change these values, or even comment out these lines (except for the glColor4x one) completely and the graphics still display exactly the same. The graphics display at their expected size, with no distortion, and even in the right spot if you take the bottom-left coordinate to be the origin. Here's the drawing routine for my textures, as part of a class representing one drawable object:

public void draw(GL10 gl, float x, float y) {
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    cropSignature = OpenGLUtil.setTextureCrop(gl,srcRectParam,cropSignature);
    ((GL11Ext)gl).glDrawTexfOES(x, y, 0, width, height);
}

Where x,y are the coordinates to draw at, and width,height are the dimensions of the texture, specified on instantiation of this object.

Is the glDrawTexfOES completely bypassing the viewport settings I established? If so, how should I be drawing? And how can I set the OpenGL origin to be at the top-left, increasing downwards? Or is that bad practice, and should I be inverting the rest of my code to comply with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2024-11-13 11:18:01

将其更改为如下所示:

// call only one time, when texture needs to be loaded
// (also sometimes neccesary after resuming a paused app)
public void loadTexture() {
    textureId = gl.glGenTextures(1);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    cropSignature = OpenGLUtil.setTextureCrop(gl,srcRectParam,cropSignature);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    this.width = width;
    this.height = height;
}

public void draw(GL10 gl, float x, float y) {
    gl.glViewport(0, 0, width, height);

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    if(origin_upper_left) {
        GLU.gluOrtho2D(gl, 0, width, 0, height);
    } else {
        GLU.gluOrtho2D(gl, 0, width, height, 0);
    }

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
    ((GL11Ext)gl).glDrawTexfOES(x, y, 0, width, height);
}

Change it to something like this:

// call only one time, when texture needs to be loaded
// (also sometimes neccesary after resuming a paused app)
public void loadTexture() {
    textureId = gl.glGenTextures(1);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    cropSignature = OpenGLUtil.setTextureCrop(gl,srcRectParam,cropSignature);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    this.width = width;
    this.height = height;
}

public void draw(GL10 gl, float x, float y) {
    gl.glViewport(0, 0, width, height);

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    if(origin_upper_left) {
        GLU.gluOrtho2D(gl, 0, width, 0, height);
    } else {
        GLU.gluOrtho2D(gl, 0, width, height, 0);
    }

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
    ((GL11Ext)gl).glDrawTexfOES(x, y, 0, width, height);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文