Example usage for org.lwjgl.opengl GL30 glGenVertexArrays

List of usage examples for org.lwjgl.opengl GL30 glGenVertexArrays

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL30 glGenVertexArrays.

Prototype

@NativeType("void")
public static int glGenVertexArrays() 

Source Link

Document

Generates vertex array object names.

Usage

From source file:wrath.client.graphics.Model.java

License:Open Source License

@Override
public void reload() {
    float[] varray = null;
    float[] narray = null;
    int[] iarray;

    File modelFile = new File("assets/models/" + name);
    if (modelFile.exists()) {
        ArrayList<String> src = readObjFile(modelFile);
        ArrayList<Vector3f> verticies = new ArrayList<>();
        ArrayList<Vector2f> texCoords = new ArrayList<>();
        ArrayList<Vector3f> normals = new ArrayList<>();
        ArrayList<Integer> indicies = new ArrayList<>();
        float[] tarray = null;

        boolean tmp = true;
        for (String inp : src) {
            String[] buf = inp.split(" ");
            if (inp.startsWith("v "))
                verticies.add(new Vector3f(Float.parseFloat(buf[1]), Float.parseFloat(buf[2]),
                        Float.parseFloat(buf[3])));
            else if (inp.startsWith("vt "))
                texCoords.add(new Vector2f(Float.parseFloat(buf[1]), Float.parseFloat(buf[2])));
            else if (inp.startsWith("vn "))
                normals.add(new Vector3f(Float.parseFloat(buf[1]), Float.parseFloat(buf[2]),
                        Float.parseFloat(buf[3])));
            else if (inp.startsWith("f ")) {
                if (tmp) {
                    varray = new float[verticies.size() * 3];
                    narray = new float[verticies.size() * 3];
                    tarray = new float[verticies.size() * 2];
                    tmp = false;//from w  w w . ja v a  2  s. c o  m
                }

                for (int x = 1; x <= 3; x++) {
                    String[] curDat;
                    if (buf[x].contains("//")) {
                        curDat = buf[x].split("//");

                        int ptr = Integer.parseInt(curDat[0]) - 1;
                        indicies.add(ptr);
                        Vector3f norm = normals.get(Integer.parseInt(curDat[1]) - 1);
                        narray[ptr * 3] = norm.x;
                        narray[ptr * 3 + 1] = norm.y;
                        narray[ptr * 3 + 2] = norm.z;
                    } else {
                        curDat = buf[x].split("/");

                        int ptr = Integer.parseInt(curDat[0]) - 1;
                        indicies.add(ptr);
                        Vector2f tex = texCoords.get(Integer.parseInt(curDat[1]) - 1);
                        tarray[ptr * 2] = tex.x;
                        tarray[ptr * 2 + 1] = 1 - tex.y;
                        Vector3f norm = normals.get(Integer.parseInt(curDat[2]) - 1);
                        narray[ptr * 3] = norm.x;
                        narray[ptr * 3 + 1] = norm.y;
                        narray[ptr * 3 + 2] = norm.z;
                    }
                }
            }
        }

        int i = 0;
        for (Vector3f ve : verticies) {
            varray[i] = ve.x;
            varray[i + 1] = ve.y;
            varray[i + 2] = ve.z;
            i += 3;
        }
        iarray = new int[indicies.size()];
        for (int z = 0; z < indicies.size(); z++)
            iarray[z] = indicies.get(z);

        textureCoords = tarray;
        indiciesLen = iarray.length;
    } else {
        varray = verticies;
        narray = normals;
        iarray = indicies;
        indiciesLen = indicies.length;
    }

    // Generating VAO
    vao = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(vao);

    // Generating Verticies VBO
    int vtvboid = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vtvboid);
    FloatBuffer vbuffer = BufferUtils.createFloatBuffer(varray.length);
    vbuffer.put(varray);
    vbuffer.flip();
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vbuffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(VERTICIES_ATTRIB_INDEX, 3, GL11.GL_FLOAT, false, 0, 0);

    // Generating Normals VBO
    int nmvboid = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, nmvboid);
    FloatBuffer nbuffer = BufferUtils.createFloatBuffer(narray.length);
    nbuffer.put(narray);
    nbuffer.flip();
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, nbuffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(NORMALS_ATTRIB_INDEX, 3, GL11.GL_FLOAT, false, 0, 0);

    // Generating Indicies VBO
    int invboid = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, invboid);
    IntBuffer ibuffer = BufferUtils.createIntBuffer(iarray.length);
    ibuffer.put(iarray);
    ibuffer.flip();
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, ibuffer, GL15.GL_STATIC_DRAW);

    // Generating Texture VBO
    int texvboid = GL15.glGenBuffers();
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, texvboid);
    FloatBuffer tbuffer = BufferUtils.createFloatBuffer(textureCoords.length);
    tbuffer.put(textureCoords);
    tbuffer.flip();
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tbuffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(TEXTURE_ATTRIB_INDEX, 2, GL11.GL_FLOAT, false, 0, 0);

    // Creating Model Object
    vbos.add(vtvboid);
    vbos.add(invboid);
    vbos.add(nmvboid);
    vbos.add(texvboid);
    Game.getCurrentInstance().getLogger().println("Reloaded model '" + name + "'!");
    if (defaultShaders)
        this.attachShader(ShaderProgram.DEFAULT_SHADER);

    // Unbinding OpenGL Objects
    GL30.glBindVertexArray(0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}