来源:
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
public class Loader {
private List<Integer> vaos = new ArrayList<Integer>();
private List<Integer> vbos = new ArrayList<Integer>();
public RawModel loadToVAO(float[] positions) {
int vaoID = createVAO();
storeDataInAttributeList(0,positions);
unbindVAO();
return new RawModel(vaoID,positions.length/3);
}
public void cleanUP() {
for(int vao:vaos) {
GL30.glDeleteVertexArrays(vao);
}
for(int vbo:vbos) {
GL30.glDeleteVertexArrays(vbo);
}
}
private int createVAO() {
int vaoID = GL30.glGenVertexArrays();
vaos.add(vaoID);
GL30.glBindVertexArray(vaoID);
return vaoID;
}
private void storeDataInAttributeList(int attributeNumber,float[] data) {
int vaoID = GL15.glGenBuffers();
vbos.add(vaoID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vaoID);
FloatBuffer buffer = storeDataImFloatBuffer(data);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void unbindVAO(){
GL30.glBindVertexArray(0);
}
private FloatBuffer storeDataImFloatBuffer(float[] data) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
}
错误:
FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called.
The JVM will abort execution.
at org.lwjgl.opengl.GL30C.nglGenVertexArrays(Native Method)
at org.lwjgl.opengl.GL30C.glGenVertexArrays(GL30C.java:2420)
at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:2369) at Loader.createVAO(Loader.java:32)
at Loader.loadToVAO(Loader.java:16) at MainGameLoop.main(MainGameLoop.java:21)
2条答案
按热度按时间b4lqfgs41#
您尚未调用glfwMakeConextCurrent(Window)或GL.createCapables()来实际创建OpenGL上下文。在窗口创建之后添加这两行。
ghg1uchk2#
如果您使用的是MacOS,请在LWJGL 3中创建显示后执行此操作: