简体   繁体   English

将Java顶点对象数组传递给OpenGL入口点

[英]Pass array of Java vertex objects to OpenGL entry points

I am starting to port my games over to Android from iOS and I've run into a problem. 我开始将游戏从iOS移植到Android,但遇到了问题。

In my standard workflow on iOS I would store my vertex info in an array of structs: 在iOS上的标准工作流中,我会将顶点信息存储在一系列结构中:

typedef struct{
    float x, y, z;
} Vector3;

Vector3 verts[];

That sort of thing. 之类的东西。

Then when it came time to send my vertex data to GL, I would just point to the verts array and it would treat it like an array of floats. 然后,当需要将我的顶点数据发送到GL时,我将指向verts数组,并将其视为浮点数组。

glVertexAttribPointer(Vertex_POSITION, 3, GL_FLOAT, 0, 0, (void *)verts);

How do I do this in Java? 如何在Java中执行此操作?

I tried making a Vector3 class and putting a few of them into an array, but it throws an error when I try to stuff that array into GL. 我试图制作一个Vector3类并将其中的一些放入数组中,但是当我尝试将该数组填充到GL中时会引发错误。

With the direction you are going, I don't think this can work directly. 按照您的方向,我认为这不能直接起作用。 When you have an array of objects in Java (like an array of your Vector3 class ), the array contains a sequence of references to those objects, and each object is allocated in a separate chunk of memory. 当您在Java中拥有一个对象数组(例如Vector3类的数组)时,该数组包含对这些对象的引用序列 ,并且每个对象都分配在单独的内存块中。

What you need to pass to OpenGL entry points like glVertexAttribPointer() or glBufferData() is a contiguous block of memory containing the data. 您需要传递给诸如glVertexAttribPointer()glBufferData()类的OpenGL入口点的是包含数据的连续内存块 An array of objects does not have that layout, so it's simply not possible to use it directly. 对象数组没有这种布局,因此根本无法直接使用它。

You have various options: 您有多种选择:

  • Don't use arrays of objects to store your data. 不要使用对象数组来存储数据。 Instead, use a data structure that stores the data in a contiguous block of memory. 而是,使用将数据存储在连续的内存块中的数据结构。 For float values, this can be a float[] , or a FloatBuffer . 对于float值,这可以是float[]FloatBuffer The Java entry points in Android take buffers as arguments, so using buffers is the most straightforward approach. Android中的Java入口点将缓冲区作为参数,因此使用缓冲区是最简单的方法。 You should be able to find plenty of examples in Android OpenGL examples. 您应该能够在Android OpenGL示例中找到大量示例。
  • Copy the data to temporary buffers before making the API calls. 在进行API调用之前,将数据复制到临时缓冲区。 This is obviously not very appealing, since extra data copies cause unproductive overhead. 这显然不是很吸引人,因为额外的数据副本会导致不必要的开销。
  • Write your OpenGL code in C++, and compile it with the NDK. 用C ++编写OpenGL代码,然后使用NDK进行编译。 This might also save you a lot of work if you're porting C++ code from another platform. 如果要从另一个平台移植C ++代码,这也可以节省很多工作。 If you intermixed the OpenGL code with Objective-C on iOS, it will still take work. 如果您将OpenGL代码与iOS上的Objective-C混合在一起,则仍然可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM