Это работает в учебнике пятилетней давности в Windows. А я использую MacOS.
Когда я получаю исходный код руководства и пробую сам, он тоже не работает. Так что это либо проблема Mac, либо, возможно, в OpenGL есть что-то новое.
Он должен отображать белый прямоугольник в окне, но по логике вещей ничего не происходит.
Вот Класс Mesh.java:
Код: Выделить всё
package net.kiube.engine.graphics;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.system.MemoryUtil;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
public class Mesh {
private Vertex[] vertices;
private int[] indices;
private int vao, pbo, ibo;
public Mesh(Vertex[] vertices, int[] indices) {
this.vertices = vertices;
this.indices = indices;
}
public void create() {
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
FloatBuffer positionBuffer = MemoryUtil.memAllocFloat(vertices.length * 3);
float[] positionData = new float[vertices.length * 3];
for (int i = 0; i < vertices.length; i++) {
positionData[i * 3] = vertices[i].getPosition().getX();
positionData[i * 3 + 1] = vertices[i].getPosition().getY();
positionData[i * 3 + 2] = vertices[i].getPosition().getZ();
}
positionBuffer.put(positionData).flip();
pbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, pbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, positionBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();
ibo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
public Vertex[] getVertices() {
return vertices;
}
public int[] getIndices() {
return indices;
}
public int getVAO() {
return vao;
}
public int getPBO() {
return pbo;
}
public int getIBO() {
return ibo;
}
}
Код: Выделить всё
package net.kiube.engine.graphics;
import net.kiube.engine.utils.GLErrors;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;
public class Renderer {
public void renderMesh(Mesh mesh) {
GLErrors.checkErrors(); // I get 1282 error here
GL30.glBindVertexArray(mesh.getVAO());
GL30.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIBO());
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL30.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
}
Код: Выделить всё
public static Mesh mesh = new Mesh(new Vertex[] {
new Vertex(new Vector3f(-0.5f, 0.5f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.0f))
}, new int[] {
0, 1, 2,
0, 3, 2
});
Подробнее здесь: https://stackoverflow.com/questions/792 ... sing-lwjgl