Java Code Examples for org.lwjgl.system.MemoryUtil#memAllocFloat()
The following examples show how to use
org.lwjgl.system.MemoryUtil#memAllocFloat() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstancedMesh.java From lwjglbook with Apache License 2.0 | 5 votes |
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; glBindVertexArray(vaoId); instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; // Model matrix for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected or Scaling (for particles) glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
Example 2
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; FloatBuffer weightsBuffer = null; IntBuffer jointIndicesBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights vboId = glGenBuffers(); vboIdList.add(vboId); weightsBuffer = MemoryUtil.memAllocFloat(weights.length); weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices vboId = glGenBuffers(); vboIdList.add(vboId); jointIndicesBuffer = MemoryUtil.memAllocInt(jointIndices.length); jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (weightsBuffer != null) { MemoryUtil.memFree(weightsBuffer); } if (jointIndicesBuffer != null) { MemoryUtil.memFree(jointIndicesBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 3
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; FloatBuffer weightsBuffer = null; IntBuffer jointIndicesBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights vboId = glGenBuffers(); vboIdList.add(vboId); weightsBuffer = MemoryUtil.memAllocFloat(weights.length); weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices vboId = glGenBuffers(); vboIdList.add(vboId); jointIndicesBuffer = MemoryUtil.memAllocInt(jointIndices.length); jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (weightsBuffer != null) { MemoryUtil.memFree(weightsBuffer); } if (jointIndicesBuffer != null) { MemoryUtil.memFree(jointIndicesBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 4
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, int[] indices, Texture texture) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; IntBuffer indicesBuffer = null; try { this.texture = texture; vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 5
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] colours, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer colourBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO posVboId = glGenBuffers(); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Colour VBO colourVboId = glGenBuffers(); colourBuffer = MemoryUtil.memAllocFloat(colours.length); colourBuffer.put(colours).flip(); glBindBuffer(GL_ARRAY_BUFFER, colourVboId); glBufferData(GL_ARRAY_BUFFER, colourBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); // Index VBO idxVboId = glGenBuffers(); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idxVboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (colourBuffer != null) { MemoryUtil.memFree(colourBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 6
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 7
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 8
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 9
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; FloatBuffer weightsBuffer = null; IntBuffer jointIndicesBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights vboId = glGenBuffers(); vboIdList.add(vboId); weightsBuffer = MemoryUtil.memAllocFloat(weights.length); weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices vboId = glGenBuffers(); vboIdList.add(vboId); jointIndicesBuffer = MemoryUtil.memAllocInt(jointIndices.length); jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (weightsBuffer != null) { MemoryUtil.memFree(weightsBuffer); } if (jointIndicesBuffer != null) { MemoryUtil.memFree(jointIndicesBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 10
Source File: InstancedMesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; glBindVertexArray(vaoId); // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
Example 11
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; FloatBuffer weightsBuffer = null; IntBuffer jointIndicesBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights vboId = glGenBuffers(); vboIdList.add(vboId); weightsBuffer = MemoryUtil.memAllocFloat(weights.length); weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices vboId = glGenBuffers(); vboIdList.add(vboId); jointIndicesBuffer = MemoryUtil.memAllocInt(jointIndices.length); jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (weightsBuffer != null) { MemoryUtil.memFree(weightsBuffer); } if (jointIndicesBuffer != null) { MemoryUtil.memFree(jointIndicesBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 12
Source File: InstancedMesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; glBindVertexArray(vaoId); // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
Example 13
Source File: InstancedMesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; glBindVertexArray(vaoId); // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
Example 14
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 15
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 16
Source File: InstancedMesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; glBindVertexArray(vaoId); // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
Example 17
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] colours, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer colourBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO posVboId = glGenBuffers(); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Colour VBO colourVboId = glGenBuffers(); colourBuffer = MemoryUtil.memAllocFloat(colours.length); colourBuffer.put(colours).flip(); glBindBuffer(GL_ARRAY_BUFFER, colourVboId); glBufferData(GL_ARRAY_BUFFER, colourBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); // Index VBO idxVboId = glGenBuffers(); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idxVboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (colourBuffer != null) { MemoryUtil.memFree(colourBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 18
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; FloatBuffer weightsBuffer = null; IntBuffer jointIndicesBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights vboId = glGenBuffers(); vboIdList.add(vboId); weightsBuffer = MemoryUtil.memAllocFloat(weights.length); weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices vboId = glGenBuffers(); vboIdList.add(vboId); jointIndicesBuffer = MemoryUtil.memAllocInt(jointIndices.length); jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (weightsBuffer != null) { MemoryUtil.memFree(weightsBuffer); } if (jointIndicesBuffer != null) { MemoryUtil.memFree(jointIndicesBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 19
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }
Example 20
Source File: Mesh.java From lwjglbook with Apache License 2.0 | 4 votes |
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { vertexCount = indices.length; vboIdList = new ArrayList<>(); vaoId = glGenVertexArrays(); glBindVertexArray(vaoId); // Position VBO int vboId = glGenBuffers(); vboIdList.add(vboId); posBuffer = MemoryUtil.memAllocFloat(positions.length); posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); } else { // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO vboId = glGenBuffers(); vboIdList.add(vboId); indicesBuffer = MemoryUtil.memAllocInt(indices.length); indicesBuffer.put(indices).flip(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } finally { if (posBuffer != null) { MemoryUtil.memFree(posBuffer); } if (textCoordsBuffer != null) { MemoryUtil.memFree(textCoordsBuffer); } if (vecNormalsBuffer != null) { MemoryUtil.memFree(vecNormalsBuffer); } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); } } }