private void buildLeavesVBO(GL gl) { // Allocate buffer memory FloatBuffer leafVertexBuffer = BufferUtil.newFloatBuffer(81 * 6 * 3); FloatBuffer leafTextureBuffer = BufferUtil.newFloatBuffer(81 * 6 * 2); nLeafVertices = tree.createLeavesVBO(leafVertexBuffer, leafTextureBuffer); leafVertexBuffer.flip(); leafTextureBuffer.flip(); // Create VBO Id if (branchVBOIds[0] != 0) { gl.glDeleteBuffersARB(2, leafVBOIds, 0); } gl.glGenBuffersARB(2, leafVBOIds, 0); // Get Valid Names // Load buffer into graphics card memory // Vertex coordinates gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, leafVBOIds[0]); // Bind The Buffer gl.glBufferDataARB( GL.GL_ARRAY_BUFFER_ARB, nLeafVertices * 3 * BufferUtil.SIZEOF_FLOAT, leafVertexBuffer, GL.GL_STATIC_DRAW_ARB); gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, leafVBOIds[1]); gl.glBufferDataARB( GL.GL_ARRAY_BUFFER_ARB, nLeafVertices * 2 * BufferUtil.SIZEOF_FLOAT, leafTextureBuffer, GL.GL_STATIC_DRAW_ARB); leafVertexBuffer = null; leafTextureBuffer = null; }
public static void deleteGLResource(int id, int type) { if (type == GL_TEXTURE_OBJECT) { if (glTextureObjects.contains(id)) { int[] temp = {id}; gl.glDeleteTextures(1, temp, 0); glTextureObjects.remove(id); } } else if (type == GL_VERTEX_BUFFER) { if (glVertexBuffers.contains(id)) { int[] temp = {id}; gl.glDeleteBuffersARB(1, temp, 0); glVertexBuffers.remove(id); } } else if (type == GL_FRAME_BUFFER) { if (glFrameBuffers.contains(id)) { int[] temp = {id}; gl.glDeleteFramebuffersEXT(1, temp, 0); glFrameBuffers.remove(id); } } else if (type == GL_RENDER_BUFFER) { if (glRenderBuffers.contains(id)) { int[] temp = {id}; gl.glDeleteRenderbuffersEXT(1, temp, 0); glRenderBuffers.remove(id); } } else if (type == GLSL_PROGRAM) { if (glslPrograms.contains(id)) { gl.glDeleteProgram(id); glslPrograms.remove(id); } } else if (type == GLSL_SHADER) { if (glslShaders.contains(id)) { gl.glDeleteShader(id); glslShaders.remove(id); } } }
private void buildBranchVBO(GL gl) { // Allocate buffer memory FloatBuffer branchVertices = BufferUtil.newFloatBuffer( 40 * 24 * 3); // 40 branches, 24 vertices per branch, 3 coordinates per vertex FloatBuffer normalBuffer = BufferUtil.newFloatBuffer(40 * 24 * 3); FloatBuffer textureCoordBuffer = BufferUtil.newFloatBuffer(40 * 24 * 2); // FloatBuffer branchAttributes = BufferUtil.newFloatBuffer(40 * 24 * 4); // FloatBuffer branchBaseCenters = BufferUtil.newFloatBuffer(40 * 24 * 3); // FloatBuffer branchTransformationsRow1 = BufferUtil.newFloatBuffer(40 * 24 * 4); // FloatBuffer branchTransformationsRow2 = BufferUtil.newFloatBuffer(40 * 24 * 4); // FloatBuffer branchTransformationsRow3 = BufferUtil.newFloatBuffer(40 * 24 * 4); // FloatBuffer branchTransformationsRow4 = BufferUtil.newFloatBuffer(40 * 24 * 4); // FloatBuffer colorBuffer = BufferUtil.newFloatBuffer(40 * 24 * 4); if (tree.needsGeometryCreation()) { // Create the tree - creates child branches and leaves and adds vertices to the buffer nBranchVertices = tree.create(branchVertices, normalBuffer, textureCoordBuffer); // branchAttributes, branchBaseCenters, branchTransformationsRow1, // branchTransformationsRow2, branchTransformationsRow3, branchTransformationsRow4); } else { // hasFreshlyGrown returns true here // Grow the tree - updates bounding box of each branch and adds vertices to the buffer // System.out.println("Calling trunk.grow()"); nBranchVertices = tree.grow(branchVertices, normalBuffer, textureCoordBuffer); // branchAttributes, branchBaseCenters, branchTransformationsRow1, // branchTransformationsRow2, branchTransformationsRow3, branchTransformationsRow4); } branchVertices.flip(); normalBuffer.flip(); textureCoordBuffer.flip(); // branchAttributes.flip(); // branchBaseCenters.flip(); // branchTransformationsRow1.flip(); // branchTransformationsRow2.flip(); // branchTransformationsRow3.flip(); // branchTransformationsRow4.flip(); // Create VBO Id if (branchVBOIds[0] != 0) { gl.glDeleteBuffersARB(1, branchVBOIds, 0); } // System.out.println("branchVBOIds[2] = " + branchVBOIds[2]); gl.glGenBuffersARB(1, branchVBOIds, 0); // Get Valid Names // Load buffer into graphics card memory // Vertex coordinates gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[0]); // Bind The Buffer gl.glBufferDataARB( GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 3 * BufferUtil.SIZEOF_FLOAT, branchVertices, GL.GL_STATIC_DRAW_ARB); // //Vertex attributes // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[1]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchAttributes, GL.GL_STATIC_DRAW_ARB); // // //branch base centers // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[2]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 3 * // BufferUtil.SIZEOF_FLOAT, branchBaseCenters, GL.GL_STATIC_DRAW_ARB); // // //branch transformations // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[3]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow1, GL.GL_STATIC_DRAW_ARB); // // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[4]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow2, GL.GL_STATIC_DRAW_ARB); // // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[5]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow3, GL.GL_STATIC_DRAW_ARB); // // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[6]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow4, GL.GL_STATIC_DRAW_ARB); // Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card branchVertices = null; textureCoordBuffer = null; // branchAttributes = null; // branchBaseCenters = null; // branchTransformationsRow1 = null; // branchTransformationsRow2 = null; // branchTransformationsRow3 = null; // branchTransformationsRow4 = null; // colorBuffer = null; }
private void buildHeapVBO(GL gl) { // Allocate buffer memory FloatBuffer branchVertices = BufferUtil.newFloatBuffer( 24 * 3); // 1 bounding box of heap, 24 vertices of the box, 3 coordinates per vertex FloatBuffer textureCoordBuffer = BufferUtil.newFloatBuffer(24 * 3); // FloatBuffer branchAttributes = BufferUtil.newFloatBuffer(24 * 4); // FloatBuffer branchBaseCenters = BufferUtil.newFloatBuffer(24 * 3); // FloatBuffer branchTransformationsRow1 = BufferUtil.newFloatBuffer(24 * 4); // FloatBuffer branchTransformationsRow2 = BufferUtil.newFloatBuffer(24 * 4); // FloatBuffer branchTransformationsRow3 = BufferUtil.newFloatBuffer(24 * 4); // FloatBuffer branchTransformationsRow4 = BufferUtil.newFloatBuffer(24 * 4); // Create bounding box for the heap nBranchVertices = tree.createHeapBoundingBox(branchVertices, textureCoordBuffer); // branchAttributes, branchBaseCenters, branchTransformationsRow1, branchTransformationsRow2, // branchTransformationsRow3, branchTransformationsRow4); branchVertices.flip(); textureCoordBuffer.flip(); // branchAttributes.flip(); // branchBaseCenters.flip(); // branchTransformationsRow1.flip(); // branchTransformationsRow2.flip(); // branchTransformationsRow3.flip(); // branchTransformationsRow4.flip(); if (branchVBOIds[0] != 0) { gl.glDeleteBuffersARB(1, branchVBOIds, 0); } // Create VBO Id gl.glGenBuffersARB(1, branchVBOIds, 0); // Get Valid Names // Load buffer into graphics card memory // Vertex coordinates gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[0]); // Bind The Buffer gl.glBufferDataARB( GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 3 * BufferUtil.SIZEOF_FLOAT, branchVertices, GL.GL_STATIC_DRAW_ARB); // //Vertex attributes // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[1]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchAttributes, GL.GL_STATIC_DRAW_ARB); // // //branch base centers // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[2]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 3 * // BufferUtil.SIZEOF_FLOAT, branchBaseCenters, GL.GL_STATIC_DRAW_ARB); // // //branch transformations // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[3]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow1, GL.GL_STATIC_DRAW_ARB); // // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[4]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow2, GL.GL_STATIC_DRAW_ARB); // // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[5]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow3, GL.GL_STATIC_DRAW_ARB); // // gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, branchVBOIds[6]); // gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, nBranchVertices * 4 * // BufferUtil.SIZEOF_FLOAT, branchTransformationsRow4, GL.GL_STATIC_DRAW_ARB); // Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card branchVertices = null; textureCoordBuffer = null; // branchAttributes = null; // branchBaseCenters = null; // branchTransformationsRow1 = null; // branchTransformationsRow2 = null; // branchTransformationsRow3 = null; // branchTransformationsRow4 = null; }
// Releases any remaining OpenGL resources (including CG resources). public static void deleteAllGLResources() { if (!glTextureObjects.isEmpty()) { Object[] glids = glTextureObjects.toArray(); for (int i = 0; i < glids.length; i++) { int id = ((Integer) glids[i]).intValue(); int[] temp = {id}; gl.glDeleteTextures(1, temp, 0); } glTextureObjects.clear(); } if (!glVertexBuffers.isEmpty()) { Object[] glids = glVertexBuffers.toArray(); for (int i = 0; i < glids.length; i++) { int id = ((Integer) glids[i]).intValue(); int[] temp = {id}; gl.glDeleteBuffersARB(1, temp, 0); } glVertexBuffers.clear(); } if (!glFrameBuffers.isEmpty()) { Object[] glids = glFrameBuffers.toArray(); for (int i = 0; i < glids.length; i++) { int id = ((Integer) glids[i]).intValue(); int[] temp = {id}; gl.glDeleteFramebuffersEXT(1, temp, 0); } glFrameBuffers.clear(); } if (!glRenderBuffers.isEmpty()) { Object[] glids = glRenderBuffers.toArray(); for (int i = 0; i < glids.length; i++) { int id = ((Integer) glids[i]).intValue(); int[] temp = {id}; gl.glDeleteRenderbuffersEXT(1, temp, 0); } glRenderBuffers.clear(); } if (!glslPrograms.isEmpty()) { Object[] glids = glslPrograms.toArray(); for (int i = 0; i < glids.length; i++) { int id = ((Integer) glids[i]).intValue(); gl.glDeleteProgram(id); } glslPrograms.clear(); } if (!glslShaders.isEmpty()) { Object[] glids = glslShaders.toArray(); for (int i = 0; i < glids.length; i++) { int id = ((Integer) glids[i]).intValue(); gl.glDeleteShader(id); } glslShaders.clear(); } if (!cgContexts.isEmpty()) { Object[] glids = cgContexts.toArray(); for (int i = 0; i < glids.length; i++) { Object id = glids[i]; CgGL.cgDestroyContext((CGcontext) id); } cgContexts.clear(); } if (!cgPrograms.isEmpty()) { Object[] glids = cgPrograms.toArray(); for (int i = 0; i < glids.length; i++) { Object id = glids[i]; CgGL.cgDestroyProgram((CGprogram) id); } cgPrograms.clear(); } if (!cgEffects.isEmpty()) { Object[] glids = cgEffects.toArray(); for (int i = 0; i < glids.length; i++) { Object id = glids[i]; CgGL.cgDestroyEffect((CGeffect) id); } cgEffects.clear(); } }