菜鸟教程小白 发表于 2022-12-13 09:29:15

ios - 是否可以在不使用 LibGDX 的情况下从 RoboVM 访问 OpenGL ES?


                                            <p><p>是否可以从 RoboVM <strong>不使用 LibGDX</strong> 访问 iOS 上的 OpenGL ES?如果有,有什么有用的引用资料吗?</p>

<p>我唯一能找到的是 2 年前的这个 super 简单的演示:<br/>      <a href="http://robovm.com/ios-opengles-in-java-on-robovm/" rel="noreferrer noopener nofollow">http://robovm.com/ios-opengles-in-java-on-robovm/</a> <br/>
但它除了 glClearColor 和 glClear 之外没有提供任何功能。</p>

<p>不过,Apple GLKit 框架似乎已实现。我只是找不到所有实际的 glWhatever(...) 函数...</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>是的,这是可能的。为此,您需要两件事:1. 访问 OpenGL ES 函数(如 <code>glClear(...)</code> 等)和 2. <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/" rel="noreferrer noopener nofollow">UIView</a>在可以绘制 GL 图像的应用中。</p>

<p>事实证明第二点很容易。您可以使用 <a href="https://developer.apple.com/library/prerelease/ios/documentation/GLkit/Reference/GLKView_ClassReference/index.html" rel="noreferrer noopener nofollow">GLKView</a> (需要 iOS 5.0)或 <a href="https://developer.apple.com/library/prerelease/ios/documentation/QuartzCore/Reference/CAEAGLLayer_Class/index.html" rel="noreferrer noopener nofollow">CAEAGLLayer</a> (需要 iOS 2.0)如果您感到怀旧。对于这两者,网上有大量关于如何在 Objective-C 中使用它们的教程,这些教程可以很容易地翻译成 RoboVM。所以,我不会在这一点上花太多时间。</p>

<p>访问 OpenGL ES 函数有点困难,因为 RoboVM 不附带开箱即用的定义文件。因此,我们必须使用 <a href="http://docs.robovm.com/advanced-topics/bro.html" rel="noreferrer noopener nofollow">Bro</a> 构建我们自己的.事实证明,一旦你了解 Bro 如何处理 C 字符串、变量指针、IntBuffers 等(这实际上非常漂亮!),它真的非常简单。我在原始问题中链接的 super 简单演示是正确的起点。</p>

<p>为了简洁起见,让我在这里发布我编写的文件的一个非常精简的版本,以说明可以处理不同数据类型的方式:</p>

<pre><code>import java.nio.Buffer;
import java.nio.IntBuffer;

import org.robovm.rt.bro.Bro;
import org.robovm.rt.bro.Struct;
import org.robovm.rt.bro.annotation.Bridge;
import org.robovm.rt.bro.annotation.Library;
import org.robovm.rt.bro.ptr.BytePtr;
import org.robovm.rt.bro.ptr.BytePtr.BytePtrPtr;
import org.robovm.rt.bro.ptr.IntPtr;

@Library(&#34;OpenGLES&#34;)
public class GLES20 {

    public static final int GL_DEPTH_BUFFER_BIT = 0x00000100;
    public static final int GL_STENCIL_BUFFER_BIT = 0x00000400;
    public static final int GL_COLOR_BUFFER_BIT = 0x00004000;
    public static final int GL_FALSE = 0;
    public static final int GL_TRUE = 1;

    private static final int MAX_INFO_LOG_LENGTH = 10*1024;

    private static final ThreadLocal&lt;IntPtr&gt; SINGLE_VALUE =
      new ThreadLocal&lt;IntPtr&gt;() {
            @Override
            protected IntPtr initialValue() {
                return Struct.allocate(IntPtr.class, 1);
            }
      };

    private static final ThreadLocal&lt;BytePtr&gt; INFO_LOG =
      new ThreadLocal&lt;BytePtr&gt;() {
            @Override
            protected BytePtr initialValue() {
                return Struct.allocate(BytePtr.class, MAX_INFO_LOG_LENGTH);
            }
      };

    static {
      Bro.bind(GLES20.class);
    }

    @Bridge
    public static native void glClearColor(float red, float green, float blue, float alpha);

    @Bridge
    public static native void glClear(int mask);

    @Bridge
    public static native void glGetIntegerv(int pname, IntPtr params);

    // DO NOT CALL THE NEXT METHOD WITH A pname THAT RETURNS MORE THAN ONE VALUE!!!
    public static int glGetIntegerv(int pname) {
      IntPtr params = SINGLE_VALUE.get();
      glGetIntegerv(pname, params);
      return params.get();
    }

    @Bridge
    private static native int glGetUniformLocation(int program, BytePtr name);

    public static int glGetUniformLocation(int program, String name) {
      return glGetUniformLocation(program, BytePtr.toBytePtrAsciiZ(name));
    }

    @Bridge
    public static native int glGenFramebuffers(int n, IntPtr framebuffers);

    public static int glGenFramebuffer() {
      IntPtr framebuffers = SINGLE_VALUE.get();
      glGenFramebuffers(1, framebuffers);
      return framebuffers.get();
    }

    @Bridge
    private static native void glShaderSource(int shader, int count, BytePtrPtr string, IntPtr length);

    public static void glShaderSource(int shader, String code) {
      glShaderSource(shader, 1, new BytePtrPtr().set(BytePtr.toBytePtrAsciiZ(code)), null);
    }

    @Bridge
    private static native void glGetShaderInfoLog(int shader, int maxLength, IntPtr length, BytePtr infoLog);

    public static String glGetShaderInfoLog(int shader) {
      BytePtr infoLog = INFO_LOG.get();
      glGetShaderInfoLog(shader, MAX_INFO_LOG_LENGTH, null, infoLog);
      return infoLog.toStringAsciiZ();
    }

    @Bridge
    public static native void glGetShaderPrecisionFormat(int shaderType, int precisionType, IntBuffer range, IntBuffer precision);

    @Bridge
    public static native void glTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int type, IntBuffer data);

    @Bridge
    private static native void glVertexAttribPointer(int index, int size, int type, int normalized, int stride, Buffer pointer);

    public static void glVertexAttribPointer(int index, int size, int type, boolean normalized, int stride, Buffer pointer) {
      glVertexAttribPointer(index, size, type, normalized ? GL_TRUE : GL_FALSE, stride, pointer);
    }
}
</code></pre>

<p>注意大多数方法是如何通过简单的 <em>@Bridge</em> 注释的原生定义公开​​的,但对于某些人来说,在 Java 中定义一个将 <code>String</code> 转换为例如,一个 <code>*char</code> 或从 <code>IntPtr</code> 解压缩结果。</p>

<p>我没有发布我的整个库文件,因为它仍然很不完整,而且它只会使查找如何处理不同参数类型的示例变得更加困难。</p>

<p>为了节省一些工作,您可以从 libGDX 的 <a href="https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/GL20.java" rel="noreferrer noopener nofollow">GL20.java</a> 复制 GL 常量定义。 .和 <a href="https://www.khronos.org/opengles/sdk/docs/man/" rel="noreferrer noopener nofollow">OpenGL ES docs</a>是方法调用签名的很好引用(数据类型 <code>GLenum</code> 和 <code>GLbitfield</code> 对应于 Java <code>int</code>)。</p>

<p>然后您可以通过添加 <code>GLES20.</code> 来静态调用 gl 方法(就像在 Android 上一样),例如:</p>

<pre><code>GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
</code></pre>

<p>事实证明,Bro 非常聪明,您甚至不需要像使用 libGDX 那样在 robovm.xml 中包含 <code><framework>OpenGLES</framework></code> 标记。</p>

<p>还有 - 你知道什么? - 我的应用程序启动速度大约是它仍在使用 libGDX 时的 3 倍。它解决了我遇到的另一个问题(见 <a href="https://stackoverflow.com/questions/31928305/libgdx-displays-black-screen-while-app-is-paused-but-still-visible-e-g-during" rel="noreferrer noopener nofollow">LibGDX displays black screen while app is paused but still visible (e.g. during in-app purchase password dialog) on iOS</a> )。 “耶!”为了摆脱不必要的行李。</p>

<p>让生活有点烦人的一件事是,如果您弄乱了方法的调用签名或内存分配,您的应用程序将简单地崩溃,并在 IDE 中显示非常无用的“由于信号 11 而终止”消息 -控制台,其中不包含有关应用程序死亡位置的信息。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 是否可以在不使用 LibGDX 的情况下从 RoboVM 访问 OpenGL ES?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31755410/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31755410/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 是否可以在不使用 LibGDX 的情况下从 RoboVM 访问 OpenGL ES?