我正在使用lwjgl在 Java 中构建 OPENGL 应用程序,并遵循thebennybox在 YouTube 上的部分教程我能够使用我构建的 Mesh 类创建矩形。import engine.core.Util;import static org.lwjgl.opengl.GL15.*;import static org.lwjgl.opengl.GL20.*;public class Mesh { private int vbo; private int size; public Mesh() { this.vbo = glGenBuffers(); this.size = 0; } public void addVertices(Vertex[] vertices){ this.size = vertices.length * Vertex.SIZE; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW); } public void draw(){ glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0); glDrawArrays(GL_TRIANGLES, 0, this.size); glDisableVertexAttribArray(0); }}和util帮手import engine.render.Vertex;import org.lwjgl.BufferUtils;import java.nio.FloatBuffer;public class Util { public static FloatBuffer createFloatBuffer(int size){ return BufferUtils.createFloatBuffer(size); } public static FloatBuffer createFlippedBuffer(Vertex[] vertices){ FloatBuffer buffer = createFloatBuffer(vertices.length * Vertex.SIZE); for(int i = 0; i < vertices.length; i++) { buffer.put(vertices[i].getPos().getX()); buffer.put(vertices[i].getPos().getY()); buffer.put(vertices[i].getPos().getZ()); } buffer.flip(); return buffer; }}这是我渲染的方式 this.mesh = new Mesh(); Vertex[] data = new Vertex[]{ //1st triangle new Vertex(new Vector3(0.5f,-0.5f,0)), //RB new Vertex(new Vector3(-0.5f,-0.5f,0)), //LB new Vertex(new Vector3(0.5f,0.5f,0)), //RT //2nd triangle new Vertex(new Vector3(-0.5f,0.5f,0)), //RB new Vertex(new Vector3(0.5f,0.5f,0)), //RT new Vertex(new Vector3(-0.5f,-0.5f,0)), //LB }; mesh.addVertices(data); public void render(){ //update per frame mesh.draw(); }
添加回答
举报
0/150
提交
取消