public void useLights(ArrayList<RenderLight> lights, int s, int c) { if (unLPos != GL.BadUniformLocation) { fbLight.clear(); Vector3 pos = new Vector3(); for (int i = 0; i < c; i++) { RenderLight rl = lights.get(s + i); rl.mWorldTransform.mulPos(pos); fbLight.put(pos.x); fbLight.put(pos.y); fbLight.put(pos.z); pos.set(0); } fbLight.rewind(); GL20.glUniform3(unLPos, fbLight); } if (unLIntensity != GL.BadUniformLocation) { fbLight.clear(); for (int i = 0; i < c; i++) { RenderLight rl = lights.get(s + i); fbLight.put((float) rl.sceneLight.intensity.x); fbLight.put((float) rl.sceneLight.intensity.y); fbLight.put((float) rl.sceneLight.intensity.z); } fbLight.rewind(); GL20.glUniform3(unLIntensity, fbLight); } if (unLCount != GL.BadUniformLocation) { GL20.glUniform1i(unLCount, c); } }
@Override public boolean gluUnProject( float winX, float winY, float winZ, float[] model, int modelOffset, float[] project, int projectOffset, int[] view, int viewOffset, float[] obj, int objOffset) { modelb.clear(); modelb.put(model, modelOffset, 16); projectb.clear(); projectb.put(project, projectOffset, 16); viewb.clear(); viewb.put(view, viewOffset, 4); winb.clear(); boolean result = org.lwjgl.util.glu.GLU.gluUnProject(winX, winY, winZ, modelb, projectb, viewb, winb); obj[objOffset] = winb.get(0); obj[objOffset + 1] = winb.get(1); obj[objOffset + 2] = winb.get(2); return result; }
protected void initParticles() { pAndsBuff.clear(); velBuff.clear(); lifespanBuff.clear(); ageBuff.clear(); float[] velocity = dir.dot(speed); for (int i = 0; i < particleCount; i++) { pAndsBuff.put( new float[] { ((float) Math.random() - 0.5f) * 50f, 50f, ((float) Math.random() - 0.5f) * 50f, 0.016f }); velBuff.put(new float[] {velocity[0], velocity[1], velocity[2], 0f}); ageBuff.put(0f); if (i < startCount) { lifespanBuff.put(1f); } else { lifespanBuff.put(0f); } } pAndsBuff.rewind(); velBuff.rewind(); lifespanBuff.rewind(); ageBuff.rewind(); newParticles = (int) (birthRate * timeBetweenAni); }
void updateSource(int source, float x, float y, float z, float vx, float vy, float vz) { sourcePos.clear(); sourceVel.clear(); sourceVel.put(new float[] {vx, vy, vz}); sourcePos.put(new float[] {x, y, z}); sourcePos.flip(); sourceVel.flip(); AL10.alSource(sources.get(source), AL10.AL_POSITION, sourcePos); AL10.alSource(sources.get(source), AL10.AL_VELOCITY, sourceVel); }
int playAsSoundAt( int buffer, int source, float pitch, float gain, boolean loop, float x, float y, float z, float vx, float vy, float vz, boolean localsound) { gain *= soundVolume; if (gain == 0) { gain = 0.001f; } if (soundWorks) { if (sounds) { if (source == -1) { return -1; } AL10.alSourceStop(sources.get(source)); AL10.alSourcef(sources.get(source), AL10.AL_REFERENCE_DISTANCE, minDistance); if (localsound) { AL10.alSourcei(sources.get(source), AL10.AL_SOURCE_RELATIVE, AL10.AL_TRUE); AL10.alSourcef(sources.get(source), AL10.AL_ROLLOFF_FACTOR, 0.0f); } else { AL10.alSourcei(sources.get(source), AL10.AL_SOURCE_RELATIVE, AL10.AL_FALSE); AL10.alSourcef(sources.get(source), AL10.AL_ROLLOFF_FACTOR, rollOff); } AL10.alSourcei(sources.get(source), AL10.AL_BUFFER, buffer); AL10.alSourcef(sources.get(source), AL10.AL_PITCH, pitch); AL10.alSourcef(sources.get(source), AL10.AL_GAIN, gain); AL10.alSourcei(sources.get(source), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE); sourcePos.clear(); sourceVel.clear(); sourceVel.put(new float[] {vx, vy, vz}); sourcePos.put(new float[] {x, y, z}); sourcePos.flip(); sourceVel.flip(); AL10.alSource(sources.get(source), AL10.AL_POSITION, sourcePos); AL10.alSource(sources.get(source), AL10.AL_VELOCITY, sourceVel); AL10.alSourcePlay(sources.get(source)); return source; } } return -1; }
@Override public void run() { ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024 * Float.SIZE / 8); buffer.order(ByteOrder.nativeOrder()); FloatBuffer floatBuffer = buffer.asFloatBuffer(); IntBuffer intBuffer = buffer.asIntBuffer(); float[] floatArray = new float[1024 * 1024]; int[] intArray = new int[1024 * 1024]; // single put tic(); for (int tries = 0; tries < TRIES; tries++) { for (int i = 0; i < floatArray.length; i++) floatBuffer.put(floatArray[i]); floatBuffer.clear(); } toc("single put"); // single indexed put tic(); for (int tries = 0; tries < TRIES; tries++) { for (int i = 0; i < floatArray.length; i++) floatBuffer.put(i, floatArray[i]); floatBuffer.clear(); } toc("single indexed put"); // bulk put tic(); for (int tries = 0; tries < TRIES; tries++) { floatBuffer.put(floatArray); floatBuffer.clear(); } toc("vector put"); // convert bulk put tic(); for (int tries = 0; tries < TRIES; tries++) { for (int i = 0; i < floatArray.length; i++) intArray[i] = Float.floatToIntBits(floatArray[i]); intBuffer.put(intArray); intBuffer.clear(); } toc("convert bulk put"); // jni bulk put tic(); for (int tries = 0; tries < TRIES; tries++) { BufferUtils.copy(floatArray, floatBuffer, floatArray.length, 0); floatBuffer.clear(); } toc("jni bulk put"); }
private void adjustImageScaling() { float outputWidth = mOutputWidth; float outputHeight = mOutputHeight; if (mRotation == Rotation.ROTATION_270 || mRotation == Rotation.ROTATION_90) { outputWidth = mOutputHeight; outputHeight = mOutputWidth; } float ratio1 = outputWidth / mImageWidth; float ratio2 = outputHeight / mImageHeight; float ratioMax = Math.max(ratio1, ratio2); int imageWidthNew = Math.round(mImageWidth * ratioMax); int imageHeightNew = Math.round(mImageHeight * ratioMax); float ratioWidth = imageWidthNew / outputWidth; float ratioHeight = imageHeightNew / outputHeight; float[] cube = CUBE; float[] textureCords = TextureRotationUtil.getRotation(mRotation, mFlipHorizontal, mFlipVertical); if (mScaleType == GPUImage.ScaleType.CENTER_CROP) { float distHorizontal = (1 - 1 / ratioWidth) / 2; float distVertical = (1 - 1 / ratioHeight) / 2; textureCords = new float[] { addDistance(textureCords[0], distHorizontal), addDistance(textureCords[1], distVertical), addDistance(textureCords[2], distHorizontal), addDistance(textureCords[3], distVertical), addDistance(textureCords[4], distHorizontal), addDistance(textureCords[5], distVertical), addDistance(textureCords[6], distHorizontal), addDistance(textureCords[7], distVertical), }; } else { cube = new float[] { CUBE[0] / ratioHeight, CUBE[1] / ratioWidth, CUBE[2] / ratioHeight, CUBE[3] / ratioWidth, CUBE[4] / ratioHeight, CUBE[5] / ratioWidth, CUBE[6] / ratioHeight, CUBE[7] / ratioWidth, }; } mGLCubeBuffer.clear(); mGLCubeBuffer.put(cube).position(0); mGLTextureBuffer.clear(); mGLTextureBuffer.put(textureCords).position(0); }
private static FloatBuffer func_74521_a( float p_74521_0_, float p_74521_1_, float p_74521_2_, float p_74521_3_) { field_74522_a.clear(); field_74522_a.put(p_74521_0_).put(p_74521_1_).put(p_74521_2_).put(p_74521_3_); field_74522_a.flip(); return field_74522_a; }
private static void convertTexCoords2D(FloatBuffer input, Buffer output) { if (output.capacity() < input.capacity()) throw new RuntimeException("Output must be at least as large as input!"); input.clear(); output.clear(); Vector2f temp = new Vector2f(); int vertexCount = input.capacity() / 2; ShortBuffer sb = null; IntBuffer ib = null; if (output instanceof ShortBuffer) sb = (ShortBuffer) output; else if (output instanceof IntBuffer) ib = (IntBuffer) output; else throw new UnsupportedOperationException(); for (int i = 0; i < vertexCount; i++) { BufferUtils.populateFromBuffer(temp, input, i); if (sb != null) { sb.put((short) (temp.getX() * Short.MAX_VALUE)); sb.put((short) (temp.getY() * Short.MAX_VALUE)); } else { int v1 = (int) (temp.getX() * ((float) (1 << 16))); int v2 = (int) (temp.getY() * ((float) (1 << 16))); ib.put(v1).put(v2); } } }
private static void absPut(FloatBuffer b) { int n = b.capacity(); b.clear(); for (int i = 0; i < n; i++) b.put(i, (float) ic(i)); b.limit(n); b.position(0); }
public void setBufferContainer(BufferContainer bufferContainer) { Map<Integer, Buffer> buffers = new HashMap<>(); for (Entry<Integer, Object> entry : bufferContainer.getBuffers().entrySet()) { int layout = entry.getKey(); Object buffer = entry.getValue(); if (buffer instanceof TFloatArrayList) { if (((TFloatArrayList) buffer).isEmpty()) { throw new IllegalStateException("Buffer can't be empty"); } FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(((TFloatArrayList) buffer).size()); floatBuffer.clear(); floatBuffer.put(((TFloatArrayList) buffer).toArray()); floatBuffer.flip(); buffers.put(layout, floatBuffer); } else { throw new IllegalStateException("Buffer different of TFloatArrayList not yet supported"); } } flushingNumVertices = bufferContainer.element; initFlush(buffers); }
private static void bulkPutArray(FloatBuffer b) { int n = b.capacity(); b.clear(); float[] a = new float[n + 7]; for (int i = 0; i < n; i++) a[i + 7] = (float) ic(i); b.put(a, 7, n); b.flip(); }
/** * Gets a colxrow intead of row x col * * @return */ public FloatBuffer getFloatBuffer() { buffer16.clear(); // give col x row ordering for (int col = 0; col < 4; col++) { for (int row = 0; row < 4; row++) buffer16.put((float) matrix4x4[row][col]); } buffer16.rewind(); return buffer16; }
/** * update position (using current position and velocity), color (interpolating between start and * end color), size (interpolating between start and end size), spin (using parent's spin speed) * and current age of particle. If this particle's age is greater than its lifespan, it is set to * status DEAD. * * <p>Note that this only changes the parameters of the Particle, not the geometry the particle is * associated with. * * @param secondsPassed number of seconds passed since last update. * @return true if this particle is not ALIVE (in other words, if it is ready to be reused.) */ public boolean updateAndCheck(final double secondsPassed) { if (status != Status.Alive) { return true; } currentAge += secondsPassed * 1000; // add ms time to age if (currentAge > lifeSpan) { killParticle(); return true; } final Vector3 temp = Vector3.fetchTempInstance(); _position.addLocal(_velocity.multiply(secondsPassed * 1000f, temp)); Vector3.releaseTempInstance(temp); // get interpolated values from appearance ramp: parent.getRamp().getValuesAtAge(currentAge, lifeSpan, currColor, values, parent); // interpolate colors final int verts = ParticleSystem.getVertsForParticleType(type); for (int x = 0; x < verts; x++) { BufferUtils.setInBuffer( currColor, parent.getParticleGeometry().getMeshData().getColorBuffer(), startIndex + x); } // check for tex animation final int newTexIndex = parent.getTexAnimation().getTexIndexAtAge(currentAge, lifeSpan, parent); // Update tex coords if applicable if (currentTexIndex != newTexIndex) { // Only supported in Quad type for now. if (ParticleType.Quad.equals(parent.getParticleType())) { // determine side final float side = (float) Math.sqrt(parent.getTexQuantity()); int index = newTexIndex; if (index >= parent.getTexQuantity()) { index %= parent.getTexQuantity(); } // figure row / col final float row = side - (int) (index / side) - 1; final float col = index % side; // set texcoords final float sU = col / side, eU = (col + 1) / side; final float sV = row / side, eV = (row + 1) / side; final FloatBuffer texs = parent.getParticleGeometry().getMeshData().getTextureCoords(0).getBuffer(); texs.position(startIndex * 2); texs.put(eU).put(sV); texs.put(eU).put(eV); texs.put(sU).put(eV); texs.put(sU).put(sV); texs.clear(); } currentTexIndex = newTexIndex; } return false; }
public void setUniformMat(int loc, boolean transposed, Matrix4f mat) { if (loc != -1) { set(); if (buff16 == null) buff16 = BufferUtils.createFloatBuffer(16); buff16.clear(); mat.store(buff16); buff16.flip(); glUniformMatrix4(loc, transposed, buff16); stop(); } }
private static void bulkPutBuffer(FloatBuffer b) { int n = b.capacity(); b.clear(); FloatBuffer c = FloatBuffer.allocate(n + 7); c.position(7); for (int i = 0; i < n; i++) c.put((float) ic(i)); c.flip(); c.position(7); b.put(c); b.flip(); }
private static void convertToFloat(IntBuffer input, FloatBuffer output) { if (output.capacity() < input.capacity()) throw new RuntimeException("Output must be at least as large as input!"); input.clear(); output.clear(); for (int i = 0; i < input.capacity(); i++) { output.put(((float) input.get() / (float) (1 << 16))); } output.flip(); }
private static void convertToUByte(FloatBuffer input, ByteBuffer output) { if (output.capacity() < input.capacity()) throw new RuntimeException("Output must be at least as large as input!"); input.clear(); output.clear(); for (int i = 0; i < input.capacity(); i++) { output.put((byte) (input.get() * 255f)); } output.flip(); }
/** R_SetupGL */ void R_SetupGL() { // // set up viewport // // int x = (int) Math.floor(r_newrefdef.x * vid.width / vid.width); int x = r_newrefdef.x; // int x2 = (int) Math.ceil((r_newrefdef.x + r_newrefdef.width) * vid.width / vid.width); int x2 = r_newrefdef.x + r_newrefdef.width; // int y = (int) Math.floor(vid.height - r_newrefdef.y * vid.height / vid.height); int y = vid.height - r_newrefdef.y; // int y2 = (int) Math.ceil(vid.height - (r_newrefdef.y + r_newrefdef.height) * vid.height / // vid.height); int y2 = vid.height - (r_newrefdef.y + r_newrefdef.height); int w = x2 - x; int h = y - y2; GL11.glViewport(x, y2, w, h); // // set up projection matrix // float screenaspect = (float) r_newrefdef.width / r_newrefdef.height; GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); MYgluPerspective(r_newrefdef.fov_y, screenaspect, 4, 4096); GL11.glCullFace(GL11.GL_FRONT); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); GL11.glRotatef(-90, 1, 0, 0); // put Z going up GL11.glRotatef(90, 0, 0, 1); // put Z going up GL11.glRotatef(-r_newrefdef.viewangles[2], 1, 0, 0); GL11.glRotatef(-r_newrefdef.viewangles[0], 0, 1, 0); GL11.glRotatef(-r_newrefdef.viewangles[1], 0, 0, 1); GL11.glTranslatef(-r_newrefdef.vieworg[0], -r_newrefdef.vieworg[1], -r_newrefdef.vieworg[2]); GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, r_world_matrix); r_world_matrix.clear(); // // set drawing parms // if (gl_cull.value != 0.0f) GL11.glEnable(GL11.GL_CULL_FACE); else GL11.glDisable(GL11.GL_CULL_FACE); GL11.glDisable(GL11.GL_BLEND); GL11.glDisable(GL11.GL_ALPHA_TEST); GL11.glEnable(GL11.GL_DEPTH_TEST); }
/** 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 float2byte(float[] src, int off, int len, byte[] dst, int off2) { if (len > (BUFFER_SIZE / 4)) { FloatBuffer buffer = ByteBuffer.wrap(dst, off2, len * 4).order(order).asFloatBuffer(); buffer.put(src, off, len); } else { floatBuffer.clear(); floatBuffer.put(src, off, len); byteBuffer.position(0).limit(len * 4); byteBuffer.get(dst, off2, len * 4); } }
protected void regenInfluenceOffsets(int index) { FloatBuffer verts, norms; Vector3f vertex = new Vector3f(); Vector3f normal = new Vector3f(); Geometry geom = getSkin(index); verts = geom.getVertexBuffer(); if (verts == null) { logger.log(Level.FINE, "Skipping skin ''{0}'' because verts uninitialized", geom.getName()); return; } norms = geom.getNormalBuffer(); verts.clear(); norms.clear(); for (ArrayList<BoneInfluence> infs : cache[index]) { vertex.set(verts.get(), verts.get(), verts.get()); normal.set(norms.get(), norms.get(), norms.get()); if (infs == null) continue; if (bindMatrix != null) { bindMatrix.mult(vertex, vertex); bindMatrix.rotateVect(normal); } for (int x = infs.size() - 1; x >= 0; --x) { BoneInfluence infl = infs.get(x); if (infl.bone == null) continue; infl.vOffset = new Vector3f(vertex); infl.bone.bindMatrix.inverseTranslateVect(infl.vOffset); infl.bone.bindMatrix.inverseRotateVect(infl.vOffset); infl.nOffset = new Vector3f(normal); infl.bone.bindMatrix.inverseRotateVect(infl.nOffset); } } }
/** * Play the specified buffer as a sound effect with the specified pitch and gain. * * @param buffer The ID of the buffer to play * @param pitch The pitch to play at * @param gain The gain to play at * @param loop True if the sound should loop * @param x The x position to play the sound from * @param y The y position to play the sound from * @param z The z position to play the sound from * @return source The source that will be used */ int playAsSoundAt(int buffer, float pitch, float gain, boolean loop, float x, float y, float z) { gain *= soundVolume; if (gain == 0) { gain = 0.001f; } if (soundWorks) { if (sounds) { int nextSource = findFreeSource(); if (nextSource == -1) { return -1; } AL10.alSourceStop(sources.get(nextSource)); AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer); AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch); AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain); AL10.alSourcei( sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE); sourcePos.clear(); sourceVel.clear(); sourceVel.put(new float[] {0, 0, 0}); sourcePos.put(new float[] {x, y, z}); sourcePos.flip(); sourceVel.flip(); AL10.alSourcefv(sources.get(nextSource), AL10.AL_POSITION, sourcePos); AL10.alSourcefv(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel); AL10.alSourcePlay(sources.get(nextSource)); return nextSource; } } return -1; }
/** * Processes the given convex shape to retrieve a correctly ordered FloatBuffer to construct the * shape from with a TriMesh. * * @param convexShape the shape to retreieve the vertices from. * @return the vertices as a FloatBuffer, ordered as Triangles. */ private static FloatBuffer getVertices(ConvexShape convexShape) { // Check there is a hull shape to render if (convexShape.getUserPointer() == null) { // create a hull approximation ShapeHull hull = new ShapeHull(convexShape); float margin = convexShape.getMargin(); hull.buildHull(margin); convexShape.setUserPointer(hull); } // Assert state - should have a pointer to a hull (shape) that'll be drawn assert convexShape.getUserPointer() != null : "Should have a shape for the userPointer, instead got null"; ShapeHull hull = (ShapeHull) convexShape.getUserPointer(); // Assert we actually have a shape to render assert hull.numTriangles() > 0 : "Expecting the Hull shape to have triangles"; int numberOfTriangles = hull.numTriangles(); // The number of bytes needed is: (floats in a vertex) * (vertices in a triangle) * (# of // triangles) * (size of float in bytes) final int numberOfFloats = 3 * 3 * numberOfTriangles; FloatBuffer vertices = BufferUtils.createFloatBuffer(numberOfFloats); // Force the limit, set the cap - most number of floats we will use the buffer for vertices.limit(numberOfFloats); // Loop variables final IntArrayList hullIndicies = hull.getIndexPointer(); final List<Vector3f> hullVertices = hull.getVertexPointer(); Vector3f vertexA, vertexB, vertexC; int index = 0; for (int i = 0; i < numberOfTriangles; i++) { // Grab the data for this triangle from the hull vertexA = hullVertices.get(hullIndicies.get(index++)); vertexB = hullVertices.get(hullIndicies.get(index++)); vertexC = hullVertices.get(hullIndicies.get(index++)); // Put the verticies into the vertex buffer vertices.put(vertexA.x).put(vertexA.y).put(vertexA.z); vertices.put(vertexB.x).put(vertexB.y).put(vertexB.z); vertices.put(vertexC.x).put(vertexC.y).put(vertexC.z); } vertices.clear(); return vertices; }
public void adjustVertexData(float xOffset, float yOffset) { newData.clear(); for (int i = 0; i < 12; i += 4) { newData.put(vertexDataAry[i] + xOffset); // new x newData.put(vertexDataAry[i + 1] + yOffset); // new y newData.put(0); // new z newData.put(1); // new w (clip space value) } newData.flip(); glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); glBufferSubData(GL_ARRAY_BUFFER, 0, newData); glBindBuffer(GL_ARRAY_BUFFER, 0); }
/** * 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); } }
private static void convertNormals(FloatBuffer input, ByteBuffer output) { if (output.capacity() < input.capacity()) throw new RuntimeException("Output must be at least as large as input!"); input.clear(); output.clear(); Vector3f temp = new Vector3f(); int vertexCount = input.capacity() / 3; for (int i = 0; i < vertexCount; i++) { BufferUtils.populateFromBuffer(temp, input, i); // offset and scale vector into -128 ... 127 temp.multLocal(127).addLocal(0.5f, 0.5f, 0.5f); // quantize byte v1 = (byte) temp.getX(); byte v2 = (byte) temp.getY(); byte v3 = (byte) temp.getZ(); // store output.put(v1).put(v2).put(v3); } }
void resetToBind() { for (Mesh mesh : targets) { if (isMeshAnimated(mesh)) { FloatBuffer bwBuff = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData(); ByteBuffer biBuff = (ByteBuffer) mesh.getBuffer(Type.BoneIndex).getData(); if (!biBuff.hasArray() || !bwBuff.hasArray()) { mesh.prepareForAnim(true); // prepare for software animation } VertexBuffer bindPos = mesh.getBuffer(Type.BindPosePosition); VertexBuffer bindNorm = mesh.getBuffer(Type.BindPoseNormal); VertexBuffer pos = mesh.getBuffer(Type.Position); VertexBuffer norm = mesh.getBuffer(Type.Normal); FloatBuffer pb = (FloatBuffer) pos.getData(); FloatBuffer nb = (FloatBuffer) norm.getData(); FloatBuffer bpb = (FloatBuffer) bindPos.getData(); FloatBuffer bnb = (FloatBuffer) bindNorm.getData(); pb.clear(); nb.clear(); bpb.clear(); bnb.clear(); // reseting bind tangents if there is a bind tangent buffer VertexBuffer bindTangents = mesh.getBuffer(Type.BindPoseTangent); if (bindTangents != null) { VertexBuffer tangents = mesh.getBuffer(Type.Tangent); FloatBuffer tb = (FloatBuffer) tangents.getData(); FloatBuffer btb = (FloatBuffer) bindTangents.getData(); tb.clear(); btb.clear(); tb.put(btb).clear(); } pb.put(bpb).clear(); nb.put(bnb).clear(); } } }
private static Transform convertPositions(FloatBuffer input, BoundingBox bbox, Buffer output) { if (output.capacity() < input.capacity()) throw new RuntimeException("Output must be at least as large as input!"); Vector3f offset = bbox.getCenter().negate(); Vector3f size = new Vector3f(bbox.getXExtent(), bbox.getYExtent(), bbox.getZExtent()); size.multLocal(2); ShortBuffer sb = null; ByteBuffer bb = null; float dataTypeSize; float dataTypeOffset; if (output instanceof ShortBuffer) { sb = (ShortBuffer) output; dataTypeOffset = shortOff; dataTypeSize = shortSize; } else { bb = (ByteBuffer) output; dataTypeOffset = byteOff; dataTypeSize = byteSize; } Vector3f scale = new Vector3f(); scale.set(dataTypeSize, dataTypeSize, dataTypeSize).divideLocal(size); Vector3f invScale = new Vector3f(); invScale.set(size).divideLocal(dataTypeSize); offset.multLocal(scale); offset.addLocal(dataTypeOffset, dataTypeOffset, dataTypeOffset); // offset = (-modelOffset * shortSize)/modelSize + shortOff // scale = shortSize / modelSize input.clear(); output.clear(); Vector3f temp = new Vector3f(); int vertexCount = input.capacity() / 3; for (int i = 0; i < vertexCount; i++) { BufferUtils.populateFromBuffer(temp, input, i); // offset and scale vector into -32768 ... 32767 // or into -128 ... 127 if using bytes temp.multLocal(scale); temp.addLocal(offset); // quantize and store if (sb != null) { short v1 = (short) temp.getX(); short v2 = (short) temp.getY(); short v3 = (short) temp.getZ(); sb.put(v1).put(v2).put(v3); } else { byte v1 = (byte) temp.getX(); byte v2 = (byte) temp.getY(); byte v3 = (byte) temp.getZ(); bb.put(v1).put(v2).put(v3); } } Transform transform = new Transform(); transform.setTranslation(offset.negate().multLocal(invScale)); transform.setScale(invScale); return transform; }
public void setLighting(LightList list) { // XXX: This is abuse of setLighting() to // apply fixed function bindings // and do other book keeping. if (list == null || list.size() == 0) { glDisable(GL_LIGHTING); applyFixedFuncBindings(false); setModelView(worldMatrix, viewMatrix); return; } // Number of lights set previously int numLightsSetPrev = lightList.size(); // If more than maxLights are defined, they will be ignored. // The GL1 renderer is not permitted to crash due to a // GL1 limitation. It must render anything that the GL2 renderer // can render (even incorrectly). lightList.clear(); materialAmbientColor.set(0, 0, 0, 0); for (int i = 0; i < list.size(); i++) { Light l = list.get(i); if (l.getType() == Light.Type.Ambient) { // Gather materialAmbientColor.addLocal(l.getColor()); } else { // Add to list lightList.add(l); // Once maximum lights reached, exit loop. if (lightList.size() >= maxLights) { break; } } } applyFixedFuncBindings(true); glEnable(GL_LIGHTING); fb16.clear(); fb16.put(materialAmbientColor.r) .put(materialAmbientColor.g) .put(materialAmbientColor.b) .put(1) .flip(); glLightModel(GL_LIGHT_MODEL_AMBIENT, fb16); if (context.matrixMode != GL_MODELVIEW) { glMatrixMode(GL_MODELVIEW); context.matrixMode = GL_MODELVIEW; } // Lights are already in world space, so just convert // them to view space. glLoadMatrix(storeMatrix(viewMatrix, fb16)); for (int i = 0; i < lightList.size(); i++) { int glLightIndex = GL_LIGHT0 + i; Light light = lightList.get(i); Light.Type lightType = light.getType(); ColorRGBA col = light.getColor(); Vector3f pos; // Enable the light glEnable(glLightIndex); // OGL spec states default value for light ambient is black switch (lightType) { case Directional: DirectionalLight dLight = (DirectionalLight) light; fb16.clear(); fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip(); glLight(glLightIndex, GL_DIFFUSE, fb16); glLight(glLightIndex, GL_SPECULAR, fb16); pos = tempVec.set(dLight.getDirection()).negateLocal().normalizeLocal(); fb16.clear(); fb16.put(pos.x).put(pos.y).put(pos.z).put(0.0f).flip(); glLight(glLightIndex, GL_POSITION, fb16); glLightf(glLightIndex, GL_SPOT_CUTOFF, 180); break; case Point: PointLight pLight = (PointLight) light; fb16.clear(); fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip(); glLight(glLightIndex, GL_DIFFUSE, fb16); glLight(glLightIndex, GL_SPECULAR, fb16); pos = pLight.getPosition(); fb16.clear(); fb16.put(pos.x).put(pos.y).put(pos.z).put(1.0f).flip(); glLight(glLightIndex, GL_POSITION, fb16); glLightf(glLightIndex, GL_SPOT_CUTOFF, 180); if (pLight.getRadius() > 0) { // Note: this doesn't follow the same attenuation model // as the one used in the lighting shader. glLightf(glLightIndex, GL_CONSTANT_ATTENUATION, 1); glLightf(glLightIndex, GL_LINEAR_ATTENUATION, pLight.getInvRadius() * 2); glLightf( glLightIndex, GL_QUADRATIC_ATTENUATION, pLight.getInvRadius() * pLight.getInvRadius()); } else { glLightf(glLightIndex, GL_CONSTANT_ATTENUATION, 1); glLightf(glLightIndex, GL_LINEAR_ATTENUATION, 0); glLightf(glLightIndex, GL_QUADRATIC_ATTENUATION, 0); } break; case Spot: SpotLight sLight = (SpotLight) light; fb16.clear(); fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip(); glLight(glLightIndex, GL_DIFFUSE, fb16); glLight(glLightIndex, GL_SPECULAR, fb16); pos = sLight.getPosition(); fb16.clear(); fb16.put(pos.x).put(pos.y).put(pos.z).put(1.0f).flip(); glLight(glLightIndex, GL_POSITION, fb16); Vector3f dir = sLight.getDirection(); fb16.clear(); fb16.put(dir.x).put(dir.y).put(dir.z).put(1.0f).flip(); glLight(glLightIndex, GL_SPOT_DIRECTION, fb16); float outerAngleRad = sLight.getSpotOuterAngle(); float innerAngleRad = sLight.getSpotInnerAngle(); float spotCut = outerAngleRad * FastMath.RAD_TO_DEG; float spotExpo = 0.0f; if (outerAngleRad > 0) { spotExpo = (1.0f - (innerAngleRad / outerAngleRad)) * 128.0f; } glLightf(glLightIndex, GL_SPOT_CUTOFF, spotCut); glLightf(glLightIndex, GL_SPOT_EXPONENT, spotExpo); if (sLight.getSpotRange() > 0) { glLightf(glLightIndex, GL_LINEAR_ATTENUATION, sLight.getInvSpotRange()); } else { glLightf(glLightIndex, GL_LINEAR_ATTENUATION, 0); } break; default: throw new UnsupportedOperationException("Unrecognized light type: " + lightType); } } // Disable lights after the index for (int i = lightList.size(); i < numLightsSetPrev; i++) { glDisable(GL_LIGHT0 + i); } // This will set view matrix as well. setModelView(worldMatrix, viewMatrix); }