-1
我目前正在學習使用OpenGl和lwjgl創建小型Java遊戲。我目前正在爲實體制作一個動畫系統,但我遇到了一個問題。OpenGL和lwjgl動畫無法正常工作
Exception in thread "main" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:1548)
at de.simagdo.simagdoRPG.openglObjects.Vao.create(Vao.java:21)
at de.simagdo.simagdoRPG.animation.models.AnimatedModelLoader.createVao(AnimatedModelLoader.java:73)
at de.simagdo.simagdoRPG.animation.models.AnimatedModelLoader.loadEntity(AnimatedModelLoader.java:28)
at de.simagdo.simagdoRPG.engineTester.SceneLoader.loadScene(SceneLoader.java:30)
at de.simagdo.simagdoRPG.engineTester.MainGameLoop.main(MainGameLoop.java:35)
這是我得到的錯誤。
VAO類:
private static final int BYTES_PER_FLOAT = 4;
private static final int BYTES_PER_INT = 4;
public final int id;
private List<Vbo> dataVbos = new ArrayList<>();
private Vbo indexVbo;
private int indexCount;
public static Vao create() {
int id = GL30.glGenVertexArrays();
return new Vao(id);
}
private Vao(int id) {
this.id = id;
}
public int getIndexCount() {
return indexCount;
}
public void bind(int... attributes) {
bind();
for (int i : attributes) {
GL20.glEnableVertexAttribArray(i);
}
}
public void unbind(int... attributes) {
for (int i : attributes) {
GL20.glDisableVertexAttribArray(i);
}
unbind();
}
public void createIndexBuffer(int[] indices) {
this.indexVbo = Vbo.create(GL15.GL_ELEMENT_ARRAY_BUFFER);
indexVbo.bind();
indexVbo.storeData(indices);
this.indexCount = indices.length;
}
public void createAttribute(int attribute, float[] data, int attrSize) {
Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER);
dataVbo.bind();
dataVbo.storeData(data);
GL20.glVertexAttribPointer(attribute, attrSize, GL11.GL_FLOAT, false, attrSize * BYTES_PER_FLOAT, 0);
dataVbo.unbind();
dataVbos.add(dataVbo);
}
public void createIntAttribute(int attribute, int[] data, int attrSize) {
Vbo dataVbo = Vbo.create(GL15.GL_ARRAY_BUFFER);
dataVbo.bind();
dataVbo.storeData(data);
GL30.glVertexAttribIPointer(attribute, attrSize, GL11.GL_INT, attrSize * BYTES_PER_INT, 0);
dataVbo.unbind();
dataVbos.add(dataVbo);
}
public void delete() {
GL30.glDeleteVertexArrays(id);
for (Vbo vbo : dataVbos) {
vbo.delete();
}
indexVbo.delete();
}
private void bind() {
GL30.glBindVertexArray(id);
}
private void unbind() {
GL30.glBindVertexArray(0);
}
的方法,在這裏我使用它:
private static Vao createVao(MeshData data) {
Vao vao = Vao.create();
vao.bind();
vao.createIndexBuffer(data.getIndices());
vao.createAttribute(0, data.getVertices(), 3);
vao.createAttribute(1, data.getTextureCoords(), 2);
vao.createAttribute(2, data.getNormals(), 3);
vao.createIntAttribute(3, data.getJointIds(), 3);
vao.createAttribute(4, data.getVertexWeights(), 3);
vao.unbind();
return vao;
}
謝謝你的幫助。 – Simagdo