public void display() { /* pushMatrix(); noStroke(); float[] matrix = modelViewMatrix.array(); translate(matrix[3], matrix[7], 0); rotateZ(matrix[0]); fill(col); sphere(30); stroke(0); line(0, 0, 0, 0, 50, 0); popMatrix(); */ pushMatrix(); noStroke(); float[] matrix = modelViewMatrix.array(); translate(matrix[12], matrix[13], matrix[14]); rotateY(acos(matrix[0])); fill(col); sphere(30); stroke(0); // line(0, 0, 0, 0, 0, 50); popMatrix(); }
public void drag(int x, int y) { float[] matrix = modelViewMatrix.array(); // println(matrix[12] + " " + matrix[13] + " " + matrix[14] + " " ); matrix[12] = x; matrix[14] = -y; // println(matrix[12] + " " + matrix[13] + " " + matrix[14] + " " ); }
protected float[] getMatrixFloat(FloatBuffer b) { if (pmvMatrix.usesBackingArray()) { return b.array(); } else { int p = b.position(); float[] pm = new float[16]; b.get(pm, p, 16); b.position(p); return pm; } }
@Override public void record(Buffer... samples) throws Exception { if (audio_st == null) { throw new Exception( "No audio output stream (Is audioChannels > 0 and has start() been called?)"); } int ret; int inputSize = samples[0].limit() - samples[0].position(); int inputFormat = AV_SAMPLE_FMT_NONE; int inputChannels = samples.length > 1 ? 1 : audioChannels; int inputDepth = 0; int outputFormat = audio_c.sample_fmt(); int outputChannels = samples_out.length > 1 ? 1 : audioChannels; int outputDepth = av_get_bytes_per_sample(outputFormat); if (samples[0] instanceof ByteBuffer) { inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_U8P : AV_SAMPLE_FMT_U8; inputDepth = 1; for (int i = 0; i < samples.length; i++) { ByteBuffer b = (ByteBuffer) samples[i]; if (samples_in[i] instanceof BytePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((BytePointer) samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { samples_in[i] = new BytePointer(b); } } } else if (samples[0] instanceof ShortBuffer) { inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16; inputDepth = 2; for (int i = 0; i < samples.length; i++) { ShortBuffer b = (ShortBuffer) samples[i]; if (samples_in[i] instanceof ShortPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((ShortPointer) samples_in[i]) .position(0) .put(b.array(), samples[i].position(), inputSize); } else { samples_in[i] = new ShortPointer(b); } } } else if (samples[0] instanceof IntBuffer) { inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_S32P : AV_SAMPLE_FMT_S32; inputDepth = 4; for (int i = 0; i < samples.length; i++) { IntBuffer b = (IntBuffer) samples[i]; if (samples_in[i] instanceof IntPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((IntPointer) samples_in[i]).position(0).put(b.array(), samples[i].position(), inputSize); } else { samples_in[i] = new IntPointer(b); } } } else if (samples[0] instanceof FloatBuffer) { inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_FLTP : AV_SAMPLE_FMT_FLT; inputDepth = 4; for (int i = 0; i < samples.length; i++) { FloatBuffer b = (FloatBuffer) samples[i]; if (samples_in[i] instanceof FloatPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((FloatPointer) samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { samples_in[i] = new FloatPointer(b); } } } else if (samples[0] instanceof DoubleBuffer) { inputFormat = samples.length > 1 ? AV_SAMPLE_FMT_DBLP : AV_SAMPLE_FMT_DBL; inputDepth = 8; for (int i = 0; i < samples.length; i++) { DoubleBuffer b = (DoubleBuffer) samples[i]; if (samples_in[i] instanceof DoublePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((DoublePointer) samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { samples_in[i] = new DoublePointer(b); } } } else { throw new Exception("Audio samples Buffer has unsupported type: " + samples); } if (samples_convert_ctx == null) { samples_convert_ctx = swr_alloc_set_opts( null, audio_c.channel_layout(), outputFormat, audio_c.sample_rate(), audio_c.channel_layout(), inputFormat, audio_c.sample_rate(), 0, null); if (samples_convert_ctx == null) { throw new Exception("swr_alloc_set_opts() error: Cannot allocate the conversion context."); } else if ((ret = swr_init(samples_convert_ctx)) < 0) { throw new Exception( "swr_init() error " + ret + ": Cannot initialize the conversion context."); } } for (int i = 0; i < samples.length; i++) { samples_in[i] .position(samples_in[i].position() * inputDepth) .limit((samples_in[i].position() + inputSize) * inputDepth); } while (samples_in[0].position() < samples_in[0].limit()) { int inputCount = (samples_in[0].limit() - samples_in[0].position()) / (inputChannels * inputDepth); int outputCount = (samples_out[0].limit() - samples_out[0].position()) / (outputChannels * outputDepth); int count = Math.min(inputCount, outputCount); for (int i = 0; i < samples.length; i++) { samples_in_ptr.put(i, samples_in[i]); } for (int i = 0; i < samples_out.length; i++) { samples_out_ptr.put(i, samples_out[i]); } if ((ret = swr_convert(samples_convert_ctx, samples_out_ptr, count, samples_in_ptr, count)) < 0) { throw new Exception("swr_convert() error " + ret + ": Cannot convert audio samples."); } for (int i = 0; i < samples.length; i++) { samples_in[i].position(samples_in[i].position() + ret * inputChannels * inputDepth); } for (int i = 0; i < samples_out.length; i++) { samples_out[i].position(samples_out[i].position() + ret * outputChannels * outputDepth); } if (samples_out[0].position() >= samples_out[0].limit()) { frame.nb_samples(audio_input_frame_size); avcodec_fill_audio_frame( frame, audio_c.channels(), outputFormat, samples_out[0], samples_out[0].limit(), 0); for (int i = 0; i < samples_out.length; i++) { frame.data(i, samples_out[i].position(0)); frame.linesize(i, samples_out[i].limit()); } frame.quality(audio_c.global_quality()); record(frame); } } return; }
/** * Specific method for skinning with tangents to avoid cluttering the classic skinning calculation * with null checks that would slow down the process even if tangents don't have to be computed. * Also the iteration has additional indexes since tangent has 4 components instead of 3 for pos * and norm * * @param maxWeightsPerVert maximum number of weights per vertex * @param mesh the mesh * @param offsetMatrices the offsetMaytrices to apply * @param tb the tangent vertexBuffer */ private void applySkinningTangents(Mesh mesh, Matrix4f[] offsetMatrices, VertexBuffer tb) { int maxWeightsPerVert = mesh.getMaxNumWeights(); if (maxWeightsPerVert <= 0) { throw new IllegalStateException("Max weights per vert is incorrectly set!"); } int fourMinusMaxWeights = 4 - maxWeightsPerVert; // NOTE: This code assumes the vertex buffer is in bind pose // resetToBind() has been called this frame VertexBuffer vb = mesh.getBuffer(Type.Position); FloatBuffer fvb = (FloatBuffer) vb.getData(); fvb.rewind(); VertexBuffer nb = mesh.getBuffer(Type.Normal); FloatBuffer fnb = (FloatBuffer) nb.getData(); fnb.rewind(); FloatBuffer ftb = (FloatBuffer) tb.getData(); ftb.rewind(); // get boneIndexes and weights for mesh ByteBuffer ib = (ByteBuffer) mesh.getBuffer(Type.BoneIndex).getData(); FloatBuffer wb = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData(); ib.rewind(); wb.rewind(); float[] weights = wb.array(); byte[] indices = ib.array(); int idxWeights = 0; TempVars vars = TempVars.get(); float[] posBuf = vars.skinPositions; float[] normBuf = vars.skinNormals; float[] tanBuf = vars.skinTangents; int iterations = (int) FastMath.ceil(fvb.capacity() / ((float) posBuf.length)); int bufLength = 0; int tanLength = 0; for (int i = iterations - 1; i >= 0; i--) { // read next set of positions and normals from native buffer bufLength = Math.min(posBuf.length, fvb.remaining()); tanLength = Math.min(tanBuf.length, ftb.remaining()); fvb.get(posBuf, 0, bufLength); fnb.get(normBuf, 0, bufLength); ftb.get(tanBuf, 0, tanLength); int verts = bufLength / 3; int idxPositions = 0; // tangents has their own index because of the 4 components int idxTangents = 0; // iterate vertices and apply skinning transform for each effecting bone for (int vert = verts - 1; vert >= 0; vert--) { float nmx = normBuf[idxPositions]; float vtx = posBuf[idxPositions++]; float nmy = normBuf[idxPositions]; float vty = posBuf[idxPositions++]; float nmz = normBuf[idxPositions]; float vtz = posBuf[idxPositions++]; float tnx = tanBuf[idxTangents++]; float tny = tanBuf[idxTangents++]; float tnz = tanBuf[idxTangents++]; // skipping the 4th component of the tangent since it doesn't have to be transformed idxTangents++; float rx = 0, ry = 0, rz = 0, rnx = 0, rny = 0, rnz = 0, rtx = 0, rty = 0, rtz = 0; for (int w = maxWeightsPerVert - 1; w >= 0; w--) { float weight = weights[idxWeights]; Matrix4f mat = offsetMatrices[indices[idxWeights++]]; rx += (mat.m00 * vtx + mat.m01 * vty + mat.m02 * vtz + mat.m03) * weight; ry += (mat.m10 * vtx + mat.m11 * vty + mat.m12 * vtz + mat.m13) * weight; rz += (mat.m20 * vtx + mat.m21 * vty + mat.m22 * vtz + mat.m23) * weight; rnx += (nmx * mat.m00 + nmy * mat.m01 + nmz * mat.m02) * weight; rny += (nmx * mat.m10 + nmy * mat.m11 + nmz * mat.m12) * weight; rnz += (nmx * mat.m20 + nmy * mat.m21 + nmz * mat.m22) * weight; rtx += (tnx * mat.m00 + tny * mat.m01 + tnz * mat.m02) * weight; rty += (tnx * mat.m10 + tny * mat.m11 + tnz * mat.m12) * weight; rtz += (tnx * mat.m20 + tny * mat.m21 + tnz * mat.m22) * weight; } idxWeights += fourMinusMaxWeights; idxPositions -= 3; normBuf[idxPositions] = rnx; posBuf[idxPositions++] = rx; normBuf[idxPositions] = rny; posBuf[idxPositions++] = ry; normBuf[idxPositions] = rnz; posBuf[idxPositions++] = rz; idxTangents -= 4; tanBuf[idxTangents++] = rtx; tanBuf[idxTangents++] = rty; tanBuf[idxTangents++] = rtz; // once again skipping the 4th component of the tangent idxTangents++; } fvb.position(fvb.position() - bufLength); fvb.put(posBuf, 0, bufLength); fnb.position(fnb.position() - bufLength); fnb.put(normBuf, 0, bufLength); ftb.position(ftb.position() - tanLength); ftb.put(tanBuf, 0, tanLength); } vars.release(); vb.updateData(fvb); nb.updateData(fnb); tb.updateData(ftb); }
public void checkThirtyDegreeRule( ArrayList<Cam> cameras, ArrayList<Character> characters, int selectedIdx) { if (cameras == null || cameras.isEmpty()) { println("No cameras in the scene!"); } if (characters.size() != 2) { println("Only two characters supported for now"); // TODO (sanjeet): Hack! Fix this once more characters are allowed } Cam selectedCamera = cameras.get(selectedIdx); // TODO The characters.get results in a runtime error because there aren't currently any // characters allocated in the input file. Character ch1 = characters.get(0); Character ch2 = characters.get(1); // Obtaining (x,y,z) for characters and selected camera PVector ch1Location = ch1.getTranslation(); PVector ch2Location = ch2.getTranslation(); PVector selectedCameraLocation = selectedCamera.getTranslation(); PVector cameraPoint = new PVector(); cameraPoint.add(selectedCameraLocation); for (int i = 0; i < 100; i++) { cameraPoint.add(selectedCamera.getZAxis()); } PVector intersection = getTwoLinesIntersection( new PVector(ch1Location.x, ch1Location.z), new PVector(ch2Location.x, ch2Location.z), new PVector(selectedCameraLocation.x, selectedCameraLocation.z), new PVector(cameraPoint.x, cameraPoint.z)); PVector diff = PVector.sub(selectedCameraLocation, intersection); diff.normalize(); FloatBuffer fb = selectedCamera.modelViewMatrix; float[] mat = fb.array(); float[] fbMatrix = new float[mat.length]; for (int i = 0; i < fbMatrix.length; i++) { fbMatrix[i] = mat[i]; } fbMatrix[0] = -diff.x; fbMatrix[1] = diff.y; fbMatrix[2] = -diff.z; fbMatrix[9] = diff.x; fbMatrix[10] = diff.y; fbMatrix[11] = diff.z; fbMatrix[13] = intersection.x; fbMatrix[14] = intersection.y; fbMatrix[15] = intersection.z; PMatrix3D matrix = new PMatrix3D(); matrix.set(fbMatrix); matrix.transpose(); pushMatrix(); applyMatrix(matrix); rotateY(radians(30)); line(0, 0, 0, 0, 0, 1000); rotateY(radians(-2 * 30)); line(0, 0, 0, 0, 0, 1000); popMatrix(); for (int i = 0; i < cameras.size(); i++) { if (i == selectedIdx) { continue; } if (!cameras.get(i).isInView(ch1Location) && !cameras.get(i).isInView(ch2Location)) { continue; } PVector currCamLocation = cameras.get(i).getTranslation(); PVector vect1 = PVector.sub(currCamLocation, intersection); PVector vect2 = PVector.sub(selectedCameraLocation, intersection); float dotP = vect1.dot(vect2) / (vect1.mag() * vect2.mag()); if (acos(dotP) <= PI / 6) { cameras.get(i).setColor(255, 0, 0); } else { cameras.get(i).setColor(0, 0, 255); } } }
public void drag(int x, int y) { float[] matrix = modelViewMatrix.array(); matrix[12] = x; matrix[14] = -y; }
public void display() { pushMatrix(); // println("Yo! Camera!"); // printCamera(); // // println("BAH! Modelview!"); // printMatrix(); stroke(0); // lights(); // translate(160, 160, 0); float[] matrix = modelViewMatrix.array(); // translate(matrix[12], matrix[13], matrix[14]); // rotateY(acos(matrix[0])); // rotateX(asin(matrix[1])); PMatrix3D myMatrix; myMatrix = new PMatrix3D(); myMatrix.set(matrix); /* println("GAA! Transform Matrix"); for (int i=0;i<16;i++){ print(matrix[i]); print (" "); if ((i+1)%4==0) println(""); } println(""); */ myMatrix.transpose(); applyMatrix(myMatrix); /* a and d stuff println("HRMPF! DrawPosition"); printMatrix(); //rotAngle should be 0 and grow by pressing a and shrink by pressing d rotateY(rotAngle); PMatrix3D anotherMatrix=new PMatrix3D(); getMatrix(anotherMatrix); //actually new global transformation for your camera: globalTransform = anotherMatrix * foRealCameraMatrix.invert(); */ line(0, 0, 0, 0, 0, 100); fill(col); box(boxScale); if (isSelected) { rotateY(radians(fov)); line(0, 0, 0, 0, 0, 1000); rotateY(radians(-2 * fov)); line(0, 0, 0, 0, 0, 1000); } if (false) { // float[] matrix1 = modelViewMatrix.array(); fill(color(255, 0, 255)); ellipse(matrix[12], -matrix[14], 20, 20); } popMatrix(); }