Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.DoubleBuffer;
- import java.nio.IntBuffer;
- import ...
- public class Main {
- static List<double[]> vertices;
- static List<int[]> faces;
- public static void main(String[] args) {
- try {
- create();
- } catch (Exception ex) {
- }
- }
- private static void create() throws Exception {
- PixelFormat pixelFormat = new PixelFormat();
- ContextAttribs contextAtrributes = new ContextAttribs(3, 3).withProfileCore(true);
- Display.setDisplayMode(new DisplayMode(640, 480));
- Display.create(pixelFormat, contextAtrributes);
- GL11.glDisable(GL11.GL_CULL_FACE);
- vertices = new ArrayList<double[]>();
- faces = new ArrayList<int[]>();
- vertices.add(new double[] { -1, -1, 0 });
- vertices.add(new double[] { 0, 1, 0 });
- vertices.add(new double[] { 1, -1, 0 });
- vertices.add(new double[] { 0, -1, 1 });
- faces.add(new int[] { 0, 1, 3 });
- faces.add(new int[] { 3, 1, 2 });
- faces.add(new int[] { 2, 1, 0 });
- faces.add(new int[] { 0, 2, 3 });
- vbo = GL15.glGenBuffers();
- ibo = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, createVertexBuffer(vertices), GL15.GL_STATIC_DRAW);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
- GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, createFaceBuffer(faces), GL15.GL_STATIC_DRAW);
- Shader s = new Shader(new ResourceLocation("/shader.vs"), new ResourceLocation("/shader.fs"));
- s.load();
- s.compile();
- s.bind();
- while (!Display.isCloseRequested()) {
- render();
- Display.update();
- Display.sync(60);
- }
- Display.destroy();
- }
- private static int vbo = -1, ibo = -1;
- private static void render() {
- for (int i = 0; i < 1; i++)
- GL20.glEnableVertexAttribArray(i);
- // Set pointer at the vertices' position
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
- GL20.glVertexAttribPointer(0, 3, GL11.GL_DOUBLE, false, Vertex.BYTES, 0);
- // Draw faces
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
- GL11.glDrawElements(GL11.GL_TRIANGLES, vertices.size(), GL11.GL_UNSIGNED_INT, 0);
- // Disable attributes
- for (int i = 0; i < 1; i++)
- GL20.glDisableVertexAttribArray(i);
- }
- public static DoubleBuffer createVertexBuffer(List<double[]> vertices) {
- DoubleBuffer buffer = BufferUtils.createDoubleBuffer(vertices.size() * 3);
- for (double[] d : vertices)
- buffer.put(d);
- buffer.flip();
- return buffer;
- }
- public static IntBuffer createFaceBuffer(List<int[]> faces) {
- IntBuffer buffer = BufferUtils.createIntBuffer(faces.size() * 3);
- for (int[] f : faces)
- buffer.put(f);
- buffer.flip();
- return buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement