gpt4 book ai didi

Java LWGJL opengl 缓冲区数据

转载 作者:行者123 更新时间:2023-12-01 17:42:58 30 4
gpt4 key购买 nike

我有一个类代表我的四边形的所有数据,每个类都有一个位置和颜色。我怎样才能将该类的数组传递给opengl?我试过这个:

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBO);

GL15.glBufferData(VBO, MAX_QUADS, GL15.GL_DYNAMIC_DRAW);
ByteBuffer data = GL15.glMapBuffer(GL15.GL_ARRAY_BUFFER, GL15.GL_WRITE_ONLY, null);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

但现在我不知道如何将数组解析为字节,以便可以传递给 GPU。这是数组:

Quad.Data[] vertexData = new Quad.Data[MAX_QUADS];

这是具有位置和颜色的类:

public class Data {
public Vector3f position;
public Vector4f color;
}

最佳答案

您可以将数据类的浮点值放入 FloatBuffer 中,然后使用 glBufferData() 或 glBufferSubData(如果缓冲区已分配)将其传递给 OpenGL。

使用您的代码,它看起来类似于:

//Creating the FloatBuffer and store the Data
//n = number of floats you want to store, in this case 7 for each Quad;
FloatBuffer buffer = MemoryUtil.memAllocFloat(n);

//Do this for each "Data", thus for each Vertex of your Quad
buffer.put(position.x).put(position.y).put(position.z).put(color.r).put(color.g).put(color.b).put(color.a);
//Don't forget flipping the Buffer before passing it to OpenGL, otherwise you will get an EXCEPTION_ACCESS_VIOLATION
buffer.flip();

//Binding the VBO and allocate it
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER);
GL15.glBufferData(VBO, MAX_QUADS, GL15.GL_DYNAMIC_DRAW);

//Sending the Buffer to OpenGL
GL15.glBufferSubData(0, buffer);

//Unbind the Buffer
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

关于Java LWGJL opengl 缓冲区数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60917290/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com