Java Code Examples for android.opengl.Matrix#length()
The following examples show how to use
android.opengl.Matrix#length() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Camera.java From react-native-3d-model-view with MIT License | 6 votes |
public void MoveCameraZImpl(float direction) { // Moving the camera requires a little more then adding 1 to the z or // subracting 1. // First we need to get the direction at which we are looking. float xLookDirection = 0, yLookDirection = 0, zLookDirection = 0; // The look direction is the view minus the position (where we are). xLookDirection = xView - xPos; yLookDirection = yView - yPos; zLookDirection = zView - zPos; // Normalize the direction. float dp = Matrix.length(xLookDirection, yLookDirection, zLookDirection); xLookDirection /= dp; yLookDirection /= dp; zLookDirection /= dp; // Call UpdateCamera to move our camera in the direction we want. UpdateCamera(xLookDirection, yLookDirection, zLookDirection, direction); }
Example 2
Source File: Math3DUtils.java From react-native-3d-model-view with MIT License | 6 votes |
/** * Calculate the 2 vectors, that is a line (x1,y1,z1-x2,y2,z2} corresponding to the normal of the specified face. * The calculated line will be positioned exactly in the middle of the face * * @param v0 the first vector of the face * @param v1 the second vector of the face * @param v2 the third vector of the face * @return the 2 vectors (line) corresponding to the face normal */ public static float[][] calculateFaceNormal(float[] v0, float[] v1, float[] v2) { // calculate perpendicular vector to the face. That is to calculate the cross product of v1-v0 x v2-v0 float[] va = new float[]{v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]}; float[] vb = new float[]{v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]}; float[] n = new float[]{va[1] * vb[2] - va[2] * vb[1], va[2] * vb[0] - va[0] * vb[2], va[0] * vb[1] - va[1] * vb[0]}; float modul = Matrix.length(n[0], n[1], n[2]); float[] vn = new float[]{n[0] / modul, n[1] / modul, n[2] / modul}; // calculate center of the face float[] faceCenter = calculateFaceCenter(v0, v1, v2); float[] vn2 = new float[]{faceCenter[0] + vn[0], faceCenter[1] + vn[1], faceCenter[2] + vn[2]}; @SuppressWarnings("unused") String msg = "fNormal(" + v0[0] + "," + v0[1] + "," + v0[2] + "#" + v1[0] + "," + v1[1] + "," + v1[2] + "#" + v2[0] + "," + v2[1] + "," + v2[2] + ")#normal(" + vn[0] + "," + vn[1] + "," + vn[2] + ") center(" + faceCenter[0] + "," + faceCenter[1] + "," + faceCenter[2] + ") to(" + vn2[0] + "," + vn2[1] + "," + vn2[2] + ")"; // Log.d("ObjectV4", msg); return new float[][]{faceCenter, vn2}; }
Example 3
Source File: Math3DUtils.java From react-native-3d-model-view with MIT License | 5 votes |
/** * Normalize the specified vector * * @param a */ public static void normalize(float[] a) { float length = Matrix.length(a[0], a[1], a[2]); a[0] = a[0] / length; a[1] = a[1] / length; a[2] = a[2] / length; }
Example 4
Source File: Math3DUtils.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 5 votes |
/** * Normalize the specified vector * * @param a */ public static void normalize(float[] a) { float length = Matrix.length(a[0], a[1], a[2]); a[0] = a[0] / length; a[1] = a[1] / length; a[2] = a[2] / length; }
Example 5
Source File: Math3DUtils.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 5 votes |
/** * Calculates the distance of the intersection between the specified ray and the target, or return -1 if the ray * doesn't intersect the target * * @param rayPoint1 where the ray starts * @param rayPoint2 where the ray ends * @param target where is the object to intersect * @param precision the radius to test for intersection * @return the distance of intersection * @deprecated */ public static float calculateDistanceOfIntersection(float[] rayPoint1, float[] rayPoint2, float[] target, float precision) { float raySteps = 100f; float objHalfWidth = precision / 2; float length = Matrix.length(rayPoint2[0] - rayPoint1[0], rayPoint2[1] - rayPoint1[1], rayPoint2[2] - rayPoint1[2]); float lengthDiff = length / raySteps; float xDif = (rayPoint2[0] - rayPoint1[0]) / raySteps; float yDif = (rayPoint2[1] - rayPoint1[1]) / raySteps; float zDif = (rayPoint2[2] - rayPoint1[2]) / raySteps; for (int i = 0; i < raySteps; i++) { // @formatter:off if ((rayPoint1[0] + (xDif * i)) > target[0] - objHalfWidth && (rayPoint1[0] + (xDif * i)) < target[0] + objHalfWidth && (rayPoint1[1] + (yDif * i)) > target[1] - objHalfWidth && (rayPoint1[1] + (yDif * i)) < target[1] + objHalfWidth && (rayPoint1[2] + (zDif * i)) > target[2] - objHalfWidth && (rayPoint1[2] + (zDif * i)) < target[2] + objHalfWidth) { // @formatter:on // Log.v(TouchController.TAG, "HIT: i[" + i + "] wz[" + (rayPoint1[2] + (zDif * i)) + "]"); // return new Object[] { i * lengthDiff, new float[] { rayPoint1[0] + (xDif * i), // rayPoint1[1] + (yDif * i), rayPoint1[2] + (zDif * i) } }; return i * lengthDiff; } } return -1; }
Example 6
Source File: Math3DUtils.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 5 votes |
/** * Calculate the 2 vectors, that is a line (x1,y1,z1-x2,y2,z2} corresponding to the normal of the specified face. * The calculated line will be positioned exactly in the middle of the face * * @param v0 the first vector of the face * @param v1 the second vector of the face * @param v2 the third vector of the face * @return the 2 vectors (line) corresponding to the face normal */ public static float[][] getNormalLine(float[] v0, float[] v1, float[] v2) { // calculate perpendicular vector to the face. That is to calculate the cross product of v1-v0 x v2-v0 float[] va = new float[]{v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]}; float[] vb = new float[]{v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]}; float[] n = new float[]{va[1] * vb[2] - va[2] * vb[1], va[2] * vb[0] - va[0] * vb[2], va[0] * vb[1] - va[1] * vb[0]}; float modul = Matrix.length(n[0], n[1], n[2]); float[] vn = new float[]{n[0] / modul, n[1] / modul, n[2] / modul}; return getNormalLine2(v0, v1, v2, vn); }
Example 7
Source File: Math3DUtils.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 5 votes |
/** * Calculate face normal * * @param v0 * @param v1 * @param v2 * @return */ public static float[] calculateNormal(float[] v0, float[] v1, float[] v2) { // calculate perpendicular vector to the face. That is to calculate the cross product of v1-v0 x v2-v0 float[] va = new float[]{v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]}; float[] vb = new float[]{v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]}; float[] n = new float[]{va[1] * vb[2] - va[2] * vb[1], va[2] * vb[0] - va[0] * vb[2], va[0] * vb[1] - va[1] * vb[0]}; float modul = Matrix.length(n[0], n[1], n[2]); return new float[]{n[0] / modul, n[1] / modul, n[2] / modul}; }
Example 8
Source File: Camera.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 5 votes |
private void MoveCameraZImpl(float direction) { // Moving the camera requires a little more then adding 1 to the z or // subracting 1. // First we need to get the direction at which we are looking. float xLookDirection, yLookDirection, zLookDirection; // The look direction is the view minus the position (where we are). xLookDirection = xView - xPos; yLookDirection = yView - yPos; zLookDirection = zView - zPos; // Normalize the direction. float dp = Matrix.length(xLookDirection, yLookDirection, zLookDirection); xLookDirection /= dp; yLookDirection /= dp; zLookDirection /= dp; float x = xPos + xLookDirection * direction; float y = yPos + yLookDirection * direction; float z = zPos + zLookDirection * direction; if (isOutOfBounds(x, y , z)) return; xPos = x; yPos = y; zPos = z; setChanged(true); }
Example 9
Source File: FrameRotationQueue.java From Telegram-FOSS with GNU General Public License v2.0 | 5 votes |
private static void getRotationMatrixFromAngleAxis(float[] matrix, float[] angleAxis) { // Convert coordinates to OpenGL coordinates. // CAMM motion metadata: +x right, +y down, and +z forward. // OpenGL: +x right, +y up, -z forwards float x = angleAxis[0]; float y = -angleAxis[1]; float z = -angleAxis[2]; float angleRad = Matrix.length(x, y, z); if (angleRad != 0) { float angleDeg = (float) Math.toDegrees(angleRad); Matrix.setRotateM(matrix, 0, angleDeg, x / angleRad, y / angleRad, z / angleRad); } else { Matrix.setIdentityM(matrix, 0); } }
Example 10
Source File: FrameRotationQueue.java From MediaSDK with Apache License 2.0 | 5 votes |
private static void getRotationMatrixFromAngleAxis(float[] matrix, float[] angleAxis) { // Convert coordinates to OpenGL coordinates. // CAMM motion metadata: +x right, +y down, and +z forward. // OpenGL: +x right, +y up, -z forwards float x = angleAxis[0]; float y = -angleAxis[1]; float z = -angleAxis[2]; float angleRad = Matrix.length(x, y, z); if (angleRad != 0) { float angleDeg = (float) Math.toDegrees(angleRad); Matrix.setRotateM(matrix, 0, angleDeg, x / angleRad, y / angleRad, z / angleRad); } else { Matrix.setIdentityM(matrix, 0); } }
Example 11
Source File: FrameRotationQueue.java From Telegram with GNU General Public License v2.0 | 5 votes |
private static void getRotationMatrixFromAngleAxis(float[] matrix, float[] angleAxis) { // Convert coordinates to OpenGL coordinates. // CAMM motion metadata: +x right, +y down, and +z forward. // OpenGL: +x right, +y up, -z forwards float x = angleAxis[0]; float y = -angleAxis[1]; float z = -angleAxis[2]; float angleRad = Matrix.length(x, y, z); if (angleRad != 0) { float angleDeg = (float) Math.toDegrees(angleRad); Matrix.setRotateM(matrix, 0, angleDeg, x / angleRad, y / angleRad, z / angleRad); } else { Matrix.setIdentityM(matrix, 0); } }
Example 12
Source File: Math3DUtils.java From react-native-3d-model-view with MIT License | 5 votes |
/** * Calculate face normal * * @param v0 * @param v1 * @param v2 * @return */ public static float[] calculateFaceNormal2(float[] v0, float[] v1, float[] v2) { // calculate perpendicular vector to the face. That is to calculate the cross product of v1-v0 x v2-v0 float[] va = new float[]{v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]}; float[] vb = new float[]{v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]}; float[] n = new float[]{va[1] * vb[2] - va[2] * vb[1], va[2] * vb[0] - va[0] * vb[2], va[0] * vb[1] - va[1] * vb[0]}; float modul = Matrix.length(n[0], n[1], n[2]); float[] vn = new float[]{n[0] / modul, n[1] / modul, n[2] / modul}; return vn; }
Example 13
Source File: Camera.java From react-native-3d-model-view with MIT License | 5 votes |
public void RotateImpl(float rotViewerZ) { if (Float.isNaN(rotViewerZ)) { Log.w("Rot", "NaN"); return; } float xLook = xView - xPos; float yLook = yView - yPos; float zLook = zView - zPos; float vlen = Matrix.length(xLook, yLook, zLook); xLook /= vlen; yLook /= vlen; zLook /= vlen; createRotationMatrixAroundVector(buffer, 24, rotViewerZ, xLook, yLook, zLook); float[] coordinates = new float[] { xPos, yPos, zPos, 1, xView, yView, zView, 1, xUp, yUp, zUp, 1 }; multiplyMMV(buffer, 0, buffer, 24, coordinates, 0); xPos = buffer[0]; yPos = buffer[1]; zPos = buffer[2]; xView = buffer[4 + 0]; yView = buffer[4 + 1]; zView = buffer[4 + 2]; xUp = buffer[8 + 0]; yUp = buffer[8 + 1]; zUp = buffer[8 + 2]; setChanged(true); }
Example 14
Source File: Camera.java From react-native-3d-model-view with MIT License | 5 votes |
private void pointViewToOrigin(){ xView = -xPos; yView = -yPos; zView = -zPos; float length = Matrix.length(xView, yView, zView); xView /= length; yView /= length; zView /= length; }
Example 15
Source File: ReferencedGridFeature.java From geoar-app with Apache License 2.0 | 4 votes |
public ReferencedGridFeature() { // HeightMap map = new HeightMap(); // addChild(map); // setPosition(new float[]{0f,0.00001f,0f}); int xLength = 64; int yLength = 64; final int floatsPerVertex = 3; final int floatsPerColor = 4; final int floatsPerNormal = 3; final float[] vertices = new float[xLength * yLength * floatsPerVertex]; final float[] colors = new float[xLength * yLength * floatsPerColor]; final float[] normals = new float[xLength * yLength * floatsPerNormal]; int offset = 0; int normalOffSet = 0; int colorOffset = 0; for (int y = 0; y < yLength; y++) { for (int x = 0; x < xLength; x++) { final float xRatio = x / (float) (xLength - 1); // Build our heightmap from the top down, so that our // triangles are counter-clockwise. final float yRatio = 1f - (y / (float) (yLength - 1)); final float xPosition = MIN_POSITION + (xRatio * POSITION_RANGE); final float yPosition = MIN_POSITION + (yRatio * POSITION_RANGE); vertices[offset++] = xPosition; vertices[offset++] = ((xPosition * xPosition) + (yPosition * yPosition)) / 20f; vertices[offset++] = yPosition; final float xSlope = (2 * xPosition) / 10f; final float ySlope = (2 * yPosition) / 10f; // Calculate the normal using the cross product of the // slopes. final float[] planeVectorX = { 1f, 0f, xSlope }; final float[] planeVectorY = { 0f, 1f, ySlope }; final float[] normalVector = { (planeVectorX[1] * planeVectorY[2]) - (planeVectorX[2] * planeVectorY[1]), (planeVectorX[2] * planeVectorY[0]) - (planeVectorX[0] * planeVectorY[2]), (planeVectorX[0] * planeVectorY[1]) - (planeVectorX[1] * planeVectorY[0]) }; // Normalize the normal final float length = Matrix.length(normalVector[0], normalVector[1], normalVector[2]); normals[normalOffSet++] = normalVector[0] / length; normals[normalOffSet++] = normalVector[1] / length; normals[normalOffSet++] = normalVector[2] / length; colors[colorOffset++] = 1.f; colors[colorOffset++] = 0.5f; colors[colorOffset++] = 0.5f; colors[colorOffset++] = 0.5f; } } // Now build the index data final int numStripsRequired = yLength - 1; final int numDegensRequired = 2 * (numStripsRequired - 1); final int verticesPerStrip = 2 * xLength - 1; final short[] heightMapIndexData = new short[(verticesPerStrip * verticesPerStrip)]; offset = 0; for (int y = 0; y < yLength; y++) { for (int x = 0; x < xLength - 1; x++) { heightMapIndexData[offset++] = (short) ((y * yLength) + x); heightMapIndexData[offset++] = (short) ((y * yLength) + x + 1); } } for (int x = 0; x < xLength; x++) { for (int y = 0; y < yLength - 1; y++) { heightMapIndexData[offset++] = (short) ((y * yLength) + x); heightMapIndexData[offset++] = (short) (((y + 1) * yLength) + x); } } renderer = BilligerColorShader.getInstance(); drawingMode = GLES20.GL_LINES; setRenderObjectives(vertices, colors, normals, null, heightMapIndexData); }
Example 16
Source File: Vector3f.java From react-native-3d-model-view with MIT License | 4 votes |
public float length(){ return Matrix.length(x,y,z); }
Example 17
Source File: Camera.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 4 votes |
private void RotateImpl(float rotViewerZ) { if (Float.isNaN(rotViewerZ)) { Log.w("Rot", "NaN"); return; } float xLook = xView - xPos; float yLook = yView - yPos; float zLook = zView - zPos; float vlen = Matrix.length(xLook, yLook, zLook); xLook /= vlen; yLook /= vlen; zLook /= vlen; createRotationMatrixAroundVector(buffer, 24, rotViewerZ, xLook, yLook, zLook); // float[] coordinates = new float[] { xPos, yPos, zPos, 1, xView, yView, zView, 1, xUp, yUp, zUp, 1 }; coordinates[0]=xPos; coordinates[1]=yPos; coordinates[2]=zPos; coordinates[3]=1; coordinates[4]=xView; coordinates[5]=yView; coordinates[6]=zView; coordinates[7]=1; coordinates[8]=xUp; coordinates[9]=yUp; coordinates[10]=zUp; coordinates[11]=1; multiplyMMV(buffer, 0, buffer, 24, coordinates, 0); xPos = buffer[0]; yPos = buffer[1]; zPos = buffer[2]; xView = buffer[4]; yView = buffer[4 + 1]; zView = buffer[4 + 2]; xUp = buffer[8]; yUp = buffer[8 + 1]; zUp = buffer[8 + 2]; setChanged(true); }
Example 18
Source File: Object3DBuilder.java From android-3D-model-viewer with GNU Lesser General Public License v3.0 | 4 votes |
private static void buildBones(AnimatedModel animatedModel, Joint joint, float[] parentTransform, float[] parentPoint, int parentJoinIndex, FloatBuffer vertexBuffer){ float[] point = new float[4]; float[] transform = new float[16]; Matrix.multiplyMM(transform,0,parentTransform,0,joint.getBindLocalTransform(),0); Matrix.multiplyMV(point,0,transform,0,new float[]{0,0,0,1},0); float[] v = Math3DUtils.substract(point,parentPoint); float[] point1 = new float[]{point[0],point[1],point[2]-Matrix.length(v[0],v[1],v[2])*0.05f}; float[] point2 = new float[]{point[0],point[1],point[2]+Matrix.length(v[0],v[1],v[2])*0.05f}; float[] normal = Math3DUtils.calculateNormal(parentPoint, point1, point2); // TODO: remove this /*parentPoint = new float[]{vertexBuffer.get((int)(100* Math.random())),vertexBuffer.get((int)(100* Math.random ())),vertexBuffer.get((int)(100* Math.random()))};*/ animatedModel.getVertexArrayBuffer().put(parentPoint[0]); animatedModel.getVertexArrayBuffer().put(parentPoint[1]); animatedModel.getVertexArrayBuffer().put(parentPoint[2]); animatedModel.getVertexArrayBuffer().put(point1[0]); animatedModel.getVertexArrayBuffer().put(point1[1]); animatedModel.getVertexArrayBuffer().put(point1[2]); animatedModel.getVertexArrayBuffer().put(point2[0]); animatedModel.getVertexArrayBuffer().put(point2[1]); animatedModel.getVertexArrayBuffer().put(point2[2]); animatedModel.getVertexNormalsArrayBuffer().put(normal); animatedModel.getVertexNormalsArrayBuffer().put(normal); animatedModel.getVertexNormalsArrayBuffer().put(normal); animatedModel.getJointIds().put(parentJoinIndex); animatedModel.getJointIds().put(parentJoinIndex); animatedModel.getJointIds().put(parentJoinIndex); for (int i=3; i<9; i++) { animatedModel.getJointIds().put(joint.getIndex()); } for (int i=0; i<9; i+=3) { animatedModel.getVertexWeights().put(parentJoinIndex >= 0?1:0); animatedModel.getVertexWeights().put(0); animatedModel.getVertexWeights().put(0); } for (Joint child : joint.getChildren()){ buildBones(animatedModel,child,transform, point, joint.getIndex(), vertexBuffer); } }
Example 19
Source File: Camera.java From react-native-3d-model-view with MIT License | 4 votes |
public void StrafeCam(float dX, float dY) { // Now if we were to call UpdateCamera() we will be moving the camera // foward or backwards. // We don't want that here. We want to strafe. To do so we have to get // the cross product // of our direction and Up direction view. The up was set in SetCamera // to be 1 positive // y. That is because anything positive on the y is considered up. After // we get the // cross product we can save it to the strafe variables so that can be // added to the // camera using UpdateCamera(). float vlen; // Translating the camera requires a directional vector to rotate // First we need to get the direction at which we are looking. // The look direction is the view minus the position (where we are). // Get the Direction of the view. float xLook = 0, yLook = 0, zLook = 0; xLook = xView - xPos; yLook = yView - yPos; zLook = zView - zPos; vlen = Matrix.length(xLook, yLook, zLook); xLook /= vlen; yLook /= vlen; zLook /= vlen; // Next we get the axis which is a perpendicular vector of the view // direction and up values. // We use the cross product of that to get the axis then we normalize // it. float xArriba = 0, yArriba = 0, zArriba = 0; xArriba = xUp - xPos; yArriba = yUp - yPos; zArriba = zUp - zPos; // Normalize the Right. vlen = Matrix.length(xArriba, yArriba, zArriba); xArriba /= vlen; yArriba /= vlen; zArriba /= vlen; // Get the cross product of the direction and the up. float xRight = 0, yRight = 0, zRight = 0; xRight = (yLook * zArriba) - (zLook * yArriba); yRight = (zLook * xArriba) - (xLook * zArriba); zRight = (xLook * yArriba) - (yLook * xArriba); // Normalize the Right. vlen = Matrix.length(xRight, yRight, zRight); xRight /= vlen; yRight /= vlen; zRight /= vlen; // Calculate sky / up float xSky = 0, ySky = 0, zSky = 0; // Get the cross product of the direction and the up. xSky = (yRight * zLook) - (zRight * yLook); ySky = (zRight * xLook) - (xRight * zLook); zSky = (xRight * yLook) - (yRight * xLook); // Normalize the sky / up. vlen = Matrix.length(xSky, ySky, zSky); xSky /= vlen; ySky /= vlen; zSky /= vlen; // UpdateCamera(xRight, yRight, zRight, dX); UpdateCamera(xSky, ySky, zSky, dX); }
Example 20
Source File: Camera.java From react-native-3d-model-view with MIT License | 4 votes |
private void normalize() { float xLook = 0, yLook = 0, zLook = 0; float xRight = 0, yRight = 0, zRight = 0; float xArriba = 0, yArriba = 0, zArriba = 0; float vlen; // Translating the camera requires a directional vector to rotate // First we need to get the direction at which we are looking. // The look direction is the view minus the position (where we are). // Get the Direction of the view. xLook = xView - xPos; yLook = yView - yPos; zLook = zView - zPos; vlen = Matrix.length(xLook, yLook, zLook); xLook /= vlen; yLook /= vlen; zLook /= vlen; // Next we get the axis which is a perpendicular vector of the view // direction and up values. // We use the cross product of that to get the axis then we normalize // it. xArriba = xUp - xPos; yArriba = yUp - yPos; zArriba = zUp - zPos; // Normalize the Right. vlen = Matrix.length(xArriba, yArriba, zArriba); xArriba /= vlen; yArriba /= vlen; zArriba /= vlen; // // Get the cross product of the direction and the up. // xRight = (yLook * zArriba) - (zLook * yArriba); // yRight = (zLook * xArriba) - (xLook * zArriba); // zRight = (xLook * yArriba) - (yLook * xArriba); // // Normalize the Right. // vlen = Matrix.length(xRight, yRight, zRight); // xRight /= vlen; // yRight /= vlen; // zRight /= vlen; xView = xLook + xPos; yView = yLook + yPos; zView = zLook + zPos; xUp = xArriba + xPos; yUp = yArriba + yPos; zUp = zArriba + zPos; }