private Node constructMesh() { Node toReturn = new Node("MD3 File"); for (int i = 0; i < head.numSurface; i++) { vkc = new KeyframeController(); MD3Surface thisSurface = surfaces[i]; TriMesh object = new TriMesh(thisSurface.name); object.setIndexBuffer(BufferUtils.createIntBuffer(thisSurface.triIndexes)); object.setVertexBuffer(BufferUtils.createFloatBuffer(thisSurface.verts[0])); object.setNormalBuffer(BufferUtils.createFloatBuffer(thisSurface.norms[0])); object.setTextureCoords(TexCoords.makeNew(thisSurface.texCoords)); toReturn.attachChild(object); vkc.setMorphingMesh(object); for (int j = 0; j < head.numFrames; j++) { TriMesh etm = new TriMesh(); etm.setVertexBuffer(BufferUtils.createFloatBuffer(thisSurface.verts[j])); etm.setNormalBuffer(BufferUtils.createFloatBuffer(thisSurface.norms[j])); vkc.setKeyframe(j, etm); } vkc.setActive(true); vkc.setSpeed(5); object.addController(vkc); toReturn.addController(vkc); } nullAll(); return toReturn; }
protected void duUpdateGeometryIndices() { setMode(TriMesh.Mode.Strip); if (getIndexBuffer() == null) { int[] indices = {1, 0, 4, 5, 7, 0, 3, 1, 2, 4, 6, 7, 2, 3}; setIndexBuffer(BufferUtils.createIntBuffer(indices)); } }
/** Process and setup the index buffer. */ private void processIndex() { IntBuffer indexBuffer = BufferUtils.createIntBuffer(this.triangles.length * 3); indexBuffer.clear(); for (ITriangle triangle : this.triangles) { for (int j = 0; j < 3; j++) { indexBuffer.put(triangle.getVertex(j).getIndex()); } } indexBuffer.flip(); this.setIndexBuffer(indexBuffer); }
/** * Queries OpenGL for errors in the fragment program. Errors are logged as SEVERE, noting both the * line number and message. */ private void checkProgramError() { if (GL11.glGetError() == GL11.GL_INVALID_OPERATION) { // retrieve the error position IntBuffer errorloc = BufferUtils.createIntBuffer(16); GL11.glGetInteger(ARBProgram.GL_PROGRAM_ERROR_POSITION_ARB, errorloc); logger.severe( "Error " + GL11.glGetString(ARBProgram.GL_PROGRAM_ERROR_STRING_ARB) + " in fragment program on line " + errorloc.get(0)); } }
private void create() { // first assert that the program is loaded if (program == null) { logger.severe("Attempted to apply unloaded fragment program state."); return; } IntBuffer buf = BufferUtils.createIntBuffer(1); ARBProgram.glGenProgramsARB(buf); ARBProgram.glBindProgramARB(ARBFragmentProgram.GL_FRAGMENT_PROGRAM_ARB, buf.get(0)); ARBProgram.glProgramStringARB( ARBFragmentProgram.GL_FRAGMENT_PROGRAM_ARB, ARBProgram.GL_PROGRAM_FORMAT_ASCII_ARB, program); checkProgramError(); programID = buf.get(0); }
/** * <code>tessellate</code> generates the <code>BezierMesh</code> vertices from the supplied patch * and detail level. This method is called when patch is set, and therefore, should normally have * to be called. However, if patch is changed externally, and you wish to update the mesh, a call * to <code>tessellate</code> is appropriate. */ public void tessellate() { if (patch == null) { return; } int u = 0, v; float py, px, pyold; int detailLevel = patch.getDetailLevel(); Vector3f[] temp = new Vector3f[4]; Vector3f[] last = new Vector3f[detailLevel + 1]; temp[0] = patch.getAnchor(0, 3); temp[1] = patch.getAnchor(1, 3); temp[2] = patch.getAnchor(2, 3); temp[3] = patch.getAnchor(3, 3); for (v = 0; v <= detailLevel; v++) { px = ((float) v) / ((float) detailLevel); last[v] = calcBerstein(px, temp); } u = 1; setVertexCount(((detailLevel * 2) + 2) * detailLevel); setVertexBuffer(BufferUtils.createVector3Buffer(getVertexCount())); setTextureCoords(new TexCoords(BufferUtils.createFloatBuffer(getVertexCount() * 2), 2), 0); setNormalBuffer(BufferUtils.createVector3Buffer(getVertexCount())); setTriangleQuantity(detailLevel * detailLevel * 6); setIndexBuffer(BufferUtils.createIntBuffer(getTriangleCount() * 3)); getVertexBuffer().clear(); FloatBuffer src = getTextureCoords().get(0).coords; src.clear(); for (u = 1; u <= detailLevel; u++) { py = ((float) u) / ((float) detailLevel); pyold = (u - 1.0f) / (detailLevel); temp[0] = calcBerstein(py, patch.getAnchors()[0]); temp[1] = calcBerstein(py, patch.getAnchors()[1]); temp[2] = calcBerstein(py, patch.getAnchors()[2]); temp[3] = calcBerstein(py, patch.getAnchors()[3]); for (v = 0; v <= detailLevel; v++) { px = ((float) v) / ((float) detailLevel); src.put(pyold).put(px); getVertexBuffer().put(last[v].x).put(last[v].y).put(last[v].z); last[v] = calcBerstein(px, temp); src.put(py).put(px); getVertexBuffer().put(last[v].x).put(last[v].y).put(last[v].z); } } int index = -1; for (int i = 0; i < getTriangleCount(); i = i + 6) { index++; if (i > 0 && i % (detailLevel * 6) == 0) { index += 1; } getIndexBuffer().put(2 * index); getIndexBuffer().put((2 * index) + 1); getIndexBuffer().put((2 * index) + 2); getIndexBuffer().put((2 * index) + 3); getIndexBuffer().put((2 * index) + 2); getIndexBuffer().put((2 * index) + 1); } setNormalBuffer(BufferUtils.createVector3Buffer(getVertexCount())); Vector3f oppositePoint = new Vector3f(); Vector3f adjacentPoint = new Vector3f(); Vector3f rootPoint = new Vector3f(); Vector3f tempNorm = new Vector3f(); int adj = 0, opp = 0, normalIndex = 0; for (int i = 0; i < detailLevel; i++) { for (int j = 0; j < (detailLevel * 2) + 2; j++) { BufferUtils.populateFromBuffer(rootPoint, getVertexBuffer(), normalIndex); if (j % 2 == 0) { if (i == 0) { if (j < (detailLevel * 2)) { // right cross up adj = normalIndex + 1; opp = normalIndex + 2; } else { // down cross right adj = normalIndex - 1; opp = normalIndex + 1; } } else { int ind = normalIndex - (detailLevel * 2 + 1); getNormalBuffer().rewind(); tempNorm.x = getNormalBuffer().get(ind * 3); tempNorm.y = getNormalBuffer().get(ind * 3 + 1); tempNorm.z = getNormalBuffer().get(ind * 3 + 2); tempNorm.normalizeLocal(); BufferUtils.setInBuffer(tempNorm, getNormalBuffer(), normalIndex); normalIndex++; continue; } } else { if (j < (detailLevel * 2) + 1) { // up cross left adj = normalIndex + 2; opp = normalIndex - 1; } else { // left cross down adj = normalIndex - 1; opp = normalIndex - 2; } } BufferUtils.populateFromBuffer(adjacentPoint, getVertexBuffer(), adj); BufferUtils.populateFromBuffer(oppositePoint, getVertexBuffer(), opp); tempNorm .set(adjacentPoint) .subtractLocal(rootPoint) .crossLocal(oppositePoint.subtractLocal(rootPoint)) .normalizeLocal(); BufferUtils.setInBuffer(tempNorm, getNormalBuffer(), normalIndex); normalIndex++; } } }