/** Process and setup the <code>TextureState</code> and texture UV buffer. */ private void processTexture() { FloatBuffer textureBuffer = BufferUtils.createVector2Buffer(this.vertices.length); float maxU = 1; float maxV = 1; float minU = 0; float minV = 0; int index = 0; for (IVertex vertex : this.vertices) { BufferUtils.setInBuffer(vertex.getTextureCoords(), textureBuffer, index); if (vertex.getTextureCoords().x > maxU) maxU = vertex.getTextureCoords().x; else if (vertex.getTextureCoords().x < minU) minU = vertex.getTextureCoords().x; if (vertex.getTextureCoords().y > maxV) maxV = vertex.getTextureCoords().y; else if (vertex.getTextureCoords().y < minV) minV = vertex.getTextureCoords().y; index++; } this.setTextureCoords(new TexCoords(textureBuffer)); // Get texture state. TextureState state = (TextureState) this.getRenderState(StateType.Texture); if (state == null) { state = DisplaySystem.getDisplaySystem().getRenderer().createTextureState(); this.setRenderState(state); } // Set color map. if (this.color != null) state.setTexture(this.loadTexture(this.color, maxU, maxV), 0); // Set normal map. if (this.normal != null) state.setTexture(this.loadTexture(this.normal, maxU, maxV), 1); // Set specular map. if (this.specular != null) state.setTexture(this.loadTexture(this.specular, maxU, maxV), 2); }
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; }
/** * This is a <b>VERY </b> brute force method of detecting if two TriMesh objects intersect. * * @param mesh1 The first TriMesh. * @param mesh2 The second TriMesh. * @return True if they intersect, false otherwise. */ public static boolean meshIntersection(TriMesh mesh1, TriMesh mesh2) { IntBuffer indexA = mesh1.getIndexBuffer(); IntBuffer indexB = mesh2.getIndexBuffer(); TransformMatrix aTransform = new TransformMatrix(); aTransform.setRotationQuaternion(mesh1.getWorldRotation()); aTransform.setTranslation(mesh1.getWorldTranslation()); aTransform.setScale(mesh1.getWorldScale()); TransformMatrix bTransform = new TransformMatrix(); bTransform.setRotationQuaternion(mesh2.getWorldRotation()); bTransform.setTranslation(mesh2.getWorldTranslation()); bTransform.setScale(mesh2.getWorldScale()); Vector3f[] vertA = BufferUtils.getVector3Array(mesh1.getVertexBuffer()); for (int i = 0; i < vertA.length; i++) aTransform.multPoint(vertA[i]); Vector3f[] vertB = BufferUtils.getVector3Array(mesh2.getVertexBuffer()); for (int i = 0; i < vertB.length; i++) bTransform.multPoint(vertB[i]); for (int i = 0; i < mesh1.getTriangleCount(); i++) { for (int j = 0; j < mesh2.getTriangleCount(); j++) { if (intersection( vertA[indexA.get(i * 3 + 0)], vertA[indexA.get(i * 3 + 1)], vertA[indexA.get(i * 3 + 2)], vertB[indexB.get(j * 3 + 0)], vertB[indexB.get(j * 3 + 1)], vertB[indexB.get(j * 3 + 2)])) return true; } } return false; }
/** Process and setup the vertex position buffer. */ private void processVertex() { FloatBuffer vertexBuffer = this.getVertexBuffer(); if (vertexBuffer == null) { vertexBuffer = BufferUtils.createVector3Buffer(this.vertices.length); this.setVertexBuffer(vertexBuffer); } vertexBuffer.clear(); for (int i = 0; i < this.vertices.length; i++) { this.vertices[i].resetInformation(); this.vertices[i].processPosition(); BufferUtils.setInBuffer(this.vertices[i].getPosition(), vertexBuffer, i); } }
public void orthogonalLineFit(FloatBuffer points) { if (points == null) { return; } points.rewind(); // compute average of points int length = points.remaining() / 3; BufferUtils.populateFromBuffer(origin, points, 0); for (int i = 1; i < length; i++) { BufferUtils.populateFromBuffer(compVec1, points, i); origin.addLocal(compVec1); } origin.multLocal(1f / (float) length); // compute sums of products float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f; float sumYY = 0.0f, sumYZ = 0.0f, sumZZ = 0.0f; points.rewind(); for (int i = 0; i < length; i++) { BufferUtils.populateFromBuffer(compVec1, points, i); compVec1.subtract(origin, compVec2); sumXX += compVec2.x * compVec2.x; sumXY += compVec2.x * compVec2.y; sumXZ += compVec2.x * compVec2.z; sumYY += compVec2.y * compVec2.y; sumYZ += compVec2.y * compVec2.z; sumZZ += compVec2.z * compVec2.z; } // find the smallest eigen vector for the direction vector compMat1.m00 = sumYY + sumZZ; compMat1.m01 = -sumXY; compMat1.m02 = -sumXZ; compMat1.m10 = -sumXY; compMat1.m11 = sumXX + sumZZ; compMat1.m12 = -sumYZ; compMat1.m20 = -sumXZ; compMat1.m21 = -sumYZ; compMat1.m22 = sumXX + sumYY; compEigen1.calculateEigen(compMat1); direction = compEigen1.getEigenVector(0); }
/** * @see Ear * @author Joshua Slack * @version $Id: OpenALEar.java 4133 2009-03-19 20:40:11Z blaine.dev $ */ public class OpenALEar extends Ear { private FloatBuffer orientBuf = BufferUtils.createFloatBuffer(6); @Override public void update(float dt) { super.update(dt); Vector3f pos = getPosition(); Vector3f vel = getCurrVelocity(); Vector3f up = getUpVector(); Vector3f dir = getFacingVector(); AL10.alListener3f(AL10.AL_POSITION, pos.x, pos.y, pos.z); AL10.alListener3f(AL10.AL_VELOCITY, vel.x, vel.y, vel.z); orientBuf.rewind(); orientBuf.put(dir.x); orientBuf.put(dir.y); orientBuf.put(dir.z); orientBuf.put(up.x); orientBuf.put(up.y); orientBuf.put(up.z); orientBuf.rewind(); AL10.alListener(AL10.AL_ORIENTATION, orientBuf); } }
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 normal position buffer. * * @param init The <code>Boolean</code> initialization flag. */ private void processNormal(boolean init) { // Triangles have to process the normal first in case the vertices are not in order. for (int i = 0; i < this.triangles.length; i++) { this.triangles[i].processNormal(); } // Average vertex normals with same vertex positions. if (init) this.averageNormal(); // Put into buffer. FloatBuffer normalBuffer = this.getNormalBuffer(); if (normalBuffer == null) { normalBuffer = BufferUtils.createVector3Buffer(this.vertices.length); this.setNormalBuffer(normalBuffer); } normalBuffer.clear(); for (int i = 0; i < this.vertices.length; i++) { BufferUtils.setInBuffer(this.vertices[i].getNormal(), normalBuffer, i); } }
/** 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); }
protected void duUpdateGeometryNormals() { Vector3f[] vert = computeVertices(); // returns 8 setNormalBuffer(BufferUtils.createVector3Buffer(getNormalBuffer(), 8)); Vector3f norm = new Vector3f(); getNormalBuffer().clear(); for (int i = 0; i < 8; i++) { norm.set(vert[i]).normalizeLocal(); getNormalBuffer().put(norm.x).put(norm.y).put(norm.z); } }
/** * Loads the fragment program into a byte array. * * @see com.jme.scene.state.FragmentProgramState#load(java.net.URL) */ public void load(String programContents) { try { byte[] bytes = programContents.getBytes(); program = BufferUtils.createByteBuffer(bytes.length); program.put(bytes); program.rewind(); programID = -1; setNeedsRefresh(true); } catch (Exception e) { logger.severe("Could not load fragment program: " + e); logger.logp(Level.SEVERE, getClass().getName(), "load(URL)", "Exception", e); } }
/** * 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)); } }
protected void duUpdateGeometryVertices() { setVertexBuffer(BufferUtils.createVector3Buffer(getVertexBuffer(), 8)); Vector3f[] vert = computeVertices(); // returns 8 getVertexBuffer().clear(); getVertexBuffer().put(vert[0].x).put(vert[0].y).put(vert[0].z); getVertexBuffer().put(vert[1].x).put(vert[1].y).put(vert[1].z); getVertexBuffer().put(vert[2].x).put(vert[2].y).put(vert[2].z); getVertexBuffer().put(vert[3].x).put(vert[3].y).put(vert[3].z); getVertexBuffer().put(vert[4].x).put(vert[4].y).put(vert[4].z); getVertexBuffer().put(vert[5].x).put(vert[5].y).put(vert[5].z); getVertexBuffer().put(vert[6].x).put(vert[6].y).put(vert[6].z); getVertexBuffer().put(vert[7].x).put(vert[7].y).put(vert[7].z); }
/** * Update variableID for uniform shadervariable if needed. * * @param variable shadervaribale to update ID on * @param programID shader program context ID */ public static void updateUniformLocation(ShaderVariable variable, int programID) { if (variable.variableID == -1) { ByteBuffer nameBuf = BufferUtils.createByteBuffer(variable.name.getBytes().length + 1); nameBuf.clear(); nameBuf.put(variable.name.getBytes()); nameBuf.rewind(); variable.variableID = ARBShaderObjects.glGetUniformLocationARB(programID, nameBuf); if (variable.variableID == -1) { logger.severe("Shader uniform [" + variable.name + "] could not be located in shader"); } } }
protected void duUpdateGeometryTextures() { if (getTextureCoords().get(0) == null) { getTextureCoords().set(0, new TexCoords(BufferUtils.createVector2Buffer(24))); FloatBuffer tex = getTextureCoords().get(0).coords; tex.put(1).put(0); // 0 tex.put(0).put(0); // 1 tex.put(0).put(1); // 2 tex.put(1).put(1); // 3 tex.put(1).put(0); // 4 tex.put(0).put(0); // 5 tex.put(1).put(1); // 6 tex.put(0).put(1); // 7 } }
/** * Update variableID for attribute shadervariable if needed. * * @param variable shadervaribale to update ID on * @param programID shader program context ID */ public static void updateAttributeLocation(ShaderVariable variable, int programID) { if (variable.variableID == -1) { ByteBuffer nameBuf = BufferUtils.createByteBuffer(variable.name.getBytes().length + 1); nameBuf.clear(); nameBuf.put(variable.name.getBytes()); nameBuf.rewind(); variable.variableID = ARBVertexShader.glGetAttribLocationARB(programID, nameBuf); if (variable.variableID == -1) { logger.severe("Shader attribute [" + variable.name + "] could not be located in shader"); } } }
/** * <code>print</code> renders the specified string to a given (x,y) location. The x, y location is * in terms of screen coordinates. There are currently two sets of fonts supported: NORMAL and * ITALICS. * * @param r * @param x the x screen location to start the string render. * @param y the y screen location to start the string render. * @param text the String to render. * @param set the mode of font: NORMAL or ITALICS. */ public void print(Renderer r, float x, float y, Vector3f scale, StringBuffer text, int set) { RendererRecord matRecord = (RendererRecord) DisplaySystem.getDisplaySystem().getCurrentContext().getRendererRecord(); if (set > 1) { set = 1; } else if (set < 0) { set = 0; } boolean alreadyOrtho = r.isInOrthoMode(); if (!alreadyOrtho) r.setOrtho(); else { matRecord.switchMode(GL11.GL_MODELVIEW); GL11.glPushMatrix(); GL11.glLoadIdentity(); } GL11.glTranslatef(x, y, 0); GL11.glScalef(scale.x, scale.y, scale.z); GL11.glListBase(base - 32 + (128 * set)); // Put the string into a "pointer" if (text.length() > scratch.capacity()) { scratch = BufferUtils.createByteBuffer(text.length()); } else { scratch.clear(); } int charLen = text.length(); for (int z = 0; z < charLen; z++) scratch.put((byte) text.charAt(z)); scratch.flip(); matRecord.setCurrentColor(fontColor); // call the list for each letter in the string. GL11.glCallLists(scratch); // set color back to white matRecord.setCurrentColor(1, 1, 1, 1); if (!alreadyOrtho) { r.unsetOrtho(); } else { matRecord.switchMode(GL11.GL_MODELVIEW); GL11.glPopMatrix(); } }
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); }
/** * Loads the fragment program into a byte array. * * @see com.jme.scene.state.FragmentProgramState#load(java.net.URL) */ public void load(java.net.URL file) { InputStream inputStream = null; try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(16 * 1024); inputStream = new BufferedInputStream(file.openStream()); byte[] buffer = new byte[1024]; int byteCount = -1; // Read the byte content into the output stream first while ((byteCount = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, byteCount); } // Set data with byte content from stream byte data[] = outputStream.toByteArray(); // Release resources inputStream.close(); outputStream.close(); program = BufferUtils.createByteBuffer(data.length); program.put(data); program.rewind(); programID = -1; setNeedsRefresh(true); } catch (Exception e) { logger.severe("Could not load fragment program: " + e); logger.logp(Level.SEVERE, getClass().getName(), "load(URL)", "Exception", e); } finally { // Ensure that the stream is closed, even if there is an exception. if (inputStream != null) { try { inputStream.close(); } catch (IOException closeFailure) { logger.log(Level.WARNING, "Failed to close the fragment program", closeFailure); } } } }
/** * <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++; } } }
public class LightStateRecord extends StateRecord { private ArrayList<LightRecord> lightList = new ArrayList<LightRecord>(); private int lightMask; private int backLightMask; private boolean twoSidedOn; public ColorRGBA globalAmbient = new ColorRGBA(-1, -1, -1, -1); private boolean enabled; private boolean localViewer; private boolean separateSpecular; // buffer for light colors. public FloatBuffer lightBuffer = BufferUtils.createColorBuffer(1); public int getBackLightMask() { return backLightMask; } public void setBackLightMask(int backLightMask) { this.backLightMask = backLightMask; } public LightRecord getLightRecord(int index) { if (lightList.size() <= index) { return null; } return lightList.get(index); } public void setLightRecord(LightRecord lr, int index) { while (lightList.size() <= index) { lightList.add(null); } lightList.set(index, lr); } public int getLightMask() { return lightMask; } public void setLightMask(int lightMask) { this.lightMask = lightMask; } public boolean isTwoSidedOn() { return twoSidedOn; } public void setTwoSidedOn(boolean twoSidedOn) { this.twoSidedOn = twoSidedOn; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public boolean isLocalViewer() { return localViewer; } public void setLocalViewer(boolean localViewer) { this.localViewer = localViewer; } public boolean isSeparateSpecular() { return separateSpecular; } public void setSeparateSpecular(boolean seperateSpecular) { this.separateSpecular = seperateSpecular; } @Override public void invalidate() { super.invalidate(); for (LightRecord record : lightList) { record.invalidate(); } lightMask = -1; backLightMask = -1; twoSidedOn = false; enabled = false; localViewer = false; separateSpecular = false; globalAmbient.set(-1, -1, -1, -1); } @Override public void validate() { super.validate(); for (LightRecord record : lightList) { record.validate(); } } }
/** * Constructor instantiates a new <code>LWJGLFont</code> object. The initial color is set to * white. */ public LWJGLFont() { fontColor = new ColorRGBA(1, 1, 1, 1); scratch = BufferUtils.createByteBuffer(1); buildDisplayList(); }