package _testcase; import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.text.NumberFormat; import javax.media.j3d.Appearance; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.Geometry; import javax.media.j3d.GeometryArray; import javax.media.j3d.GeometryUpdater; import javax.media.j3d.IndexedTriangleArray; import javax.media.j3d.J3DGraphics2D; import javax.media.j3d.Locale; import javax.media.j3d.PolygonAttributes; import javax.media.j3d.RenderingAttributes; import javax.swing.JFrame; import javax.swing.JPanel; import javax.vecmath.Color3f; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.util.FastMath; import com.sun.j3d.utils.geometry.Text2D; import com.sun.j3d.utils.universe.ConfiguredUniverse; import com.sun.j3d.utils.universe.SimpleUniverse; public class notusingtransform3D { public static void main(String[] args) { System.setProperty("sun.awt.noerasebackground", "true"); new notusingtransform3D(); } float x = 0, y = 0, z = 0, dX = 1, dY = 1, dZ = 1; double rotX = 0, rotY = 0, rotZ = 0, dRotX = 0.05, dRotY = 0.05, dRotZ = 0.05; double zoomScaleX = 0, zoomScaleY = 0; IndexedTriangleArray[] cube_one, cube_two, cube_three; String legendText = "a"; float[] arrayOfRefs_one, arrayOfRefs_n_one, arrayOfRefs_two, arrayOfRefs_three; static int[] cube_idx = { // from // https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_05 // front 0, 1, 2, 2, 3, 0, // right 1, 5, 6, 6, 2, 1, // back 7, 6, 5, 5, 4, 7, // left 4, 0, 3, 3, 7, 4, // bottom 4, 5, 1, 1, 0, 4, // top 3, 2, 6, 6, 7, 3 }; static float[] cube_colors = { // front colors 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // back colors 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f }; float[][] cube1_pt = { { -2, +0, -10 }, { -1, +0, -10 }, { -1, +1, -10 }, { -2, +1, -10 }, { -2, +0, -11 }, { -1, +0, -11 }, { -1, +1, -11 }, { -2, +1, -11 } }; float[][] cube2_pt = { { -3, +0, -14 }, { -1, +0, -14 }, { -3, +2, -14 }, { -1, +2, -14 }, { -3, +2, -12 }, { -3, +0, -12 }, { -1, +2, -12 }, { -1, +0, -12 }, }; float[][] cube3_pt = { { -2, +0, -15 }, { -2, +3, -15 }, { -1, +0, -15 }, { -1, +3, -15 }, { -2, +3, -17 }, { -1, +3, -17 }, { -2, +0, -17 }, { -1, +0, -17 }, }; static float[][] cube1_normals = {{0,0,1},{1,0,0},{0,0,-1},{-1,0,0},{0,-1,0},{0,1,0}}; // front, right, back, left, bottom, top static int[] cube_normals_idx = {0,0,1,1,2,2,3,3,4,4,5,5}; int FFTCount=0; IndexedTriangleArray[] fFT; boolean[] iFF; public RealMatrix translationM(double tx, double ty, double tz) { double[][] matrixData = { { 1, 0, 0, tx }, { 0, 1, 0, ty }, { 0, 0, 1, tz }, { 0, 0, 0, 1 } }; RealMatrix mat = MatrixUtils.createRealMatrix(matrixData); return mat; } public RealMatrix rotateX(double fi) { double[][] matrixData = { { 1, 0, 0, 0 }, { 0, FastMath.cos(fi), -FastMath.sin(fi), 0 }, { 0, FastMath.sin(fi), FastMath.cos(fi), 0 }, { 0, 0, 0, 1 } }; RealMatrix mat = MatrixUtils.createRealMatrix(matrixData); return mat; } public RealMatrix rotateY(double fi) { double[][] matrixData = { { FastMath.cos(fi), 0, FastMath.sin(fi), 0 }, { 0, 1, 0, 0 }, { -FastMath.sin(fi), 0, FastMath.cos(fi), 0 }, { 0, 0, 0, 1 } }; RealMatrix mat = MatrixUtils.createRealMatrix(matrixData); return mat; } public RealMatrix rotateZ(double fi) { double[][] matrixData = { { FastMath.cos(fi), -FastMath.sin(fi), 0, 0 }, { FastMath.sin(fi), FastMath.cos(fi), 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; RealMatrix mat = MatrixUtils.createRealMatrix(matrixData); return mat; } public RealMatrix scale(double scaleX, double scaleY, double scaleZ) { double[][] matrixData = { { scaleX, 0, 0, 0 }, { 0, scaleY, 0, 0 }, { 0, 0, scaleZ, 0 }, { 0, 0, 0, 1 } }; RealMatrix mat = MatrixUtils.createRealMatrix(matrixData); return mat; } public RealMatrix quaternion(float x, float y, float z, float w) { double[][] quatData = { { x }, { y }, { z }, { w } }; RealMatrix quat = MatrixUtils.createRealMatrix(quatData); return quat; } public void translateArray(float[] cube, float deltaX, float deltaY, float deltaZ) { RealMatrix quat; for (int i = 0; i < 8; i++) { quat = quaternion(cube[i * 3 + 0], cube[i * 3 + 1], cube[i * 3 + 2], 1); quat = quat.preMultiply(translationM(deltaX, deltaY, deltaZ)); cube[i * 3 + 0] = (float) quat.getEntry(0, 0); cube[i * 3 + 1] = (float) quat.getEntry(1, 0); cube[i * 3 + 2] = (float) quat.getEntry(2, 0); } } public void rotateArrayX(float[] cube, double fi) { RealMatrix quat; for (int i = 0; i < 8; i++) { quat = quaternion(cube[i * 3 + 0], cube[i * 3 + 1], cube[i * 3 + 2], 1); quat = quat.preMultiply(rotateX(fi)); cube[i * 3 + 0] = (float) quat.getEntry(0, 0); cube[i * 3 + 1] = (float) quat.getEntry(1, 0); cube[i * 3 + 2] = (float) quat.getEntry(2, 0); } } public void rotateArrayY(float[] cube, double fi) { RealMatrix quat; for (int i = 0; i < 8; i++) { quat = quaternion(cube[i * 3 + 0], cube[i * 3 + 1], cube[i * 3 + 2], 1); quat = quat.preMultiply(rotateY(fi)); cube[i * 3 + 0] = (float) quat.getEntry(0, 0); cube[i * 3 + 1] = (float) quat.getEntry(1, 0); cube[i * 3 + 2] = (float) quat.getEntry(2, 0); } } public void rotateArrayZ(float[] cube, double fi) { RealMatrix quat; for (int i = 0; i < 8; i++) { quat = quaternion(cube[i * 3 + 0], cube[i * 3 + 1], cube[i * 3 + 2], 1); quat = quat.preMultiply(rotateZ(fi)); cube[i * 3 + 0] = (float) quat.getEntry(0, 0); cube[i * 3 + 1] = (float) quat.getEntry(1, 0); cube[i * 3 + 2] = (float) quat.getEntry(2, 0); } } public void zoom(float[] cube, double scaleX, double scaleY, double scaleZ) { RealMatrix quat; for (int i = 0; i < 8; i++) { quat = quaternion(cube[i * 3 + 0], cube[i * 3 + 1], cube[i * 3 + 2], 1); quat = quat.preMultiply(scale(scaleX, scaleY, scaleZ)); cube[i * 3 + 0] = (float) quat.getEntry(0, 0); cube[i * 3 + 1] = (float) quat.getEntry(1, 0); cube[i * 3 + 2] = (float) quat.getEntry(2, 0); } } public static IndexedTriangleArray[] ccr1(float cube_pts[][]) { float[] flat_pts = flatten(cube_pts); float[] flat_normals = flatten(cube1_normals); IndexedTriangleArray[] dozenOfSeparateTriangles = new IndexedTriangleArray[12]; for (int i = 0; i < dozenOfSeparateTriangles.length; i++) { IndexedTriangleArray triangleGeometry = new IndexedTriangleArray(3, GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.COLOR_3 | GeometryArray.BY_REFERENCE, 3); triangleGeometry.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE); ccr2(triangleGeometry, flat_pts, flat_normals, i); dozenOfSeparateTriangles[i] = triangleGeometry; } return dozenOfSeparateTriangles; } public static float[] flatten(float cube_pts[][]) { float[] flat_pts = new float[cube_pts.length * 3]; for (int j = 0; j < cube_pts.length; j++) { flat_pts[j * 3 + 0] = cube_pts[j][0]; flat_pts[j * 3 + 1] = cube_pts[j][1]; flat_pts[j * 3 + 2] = cube_pts[j][2]; } return flat_pts; } public static void ccr2(IndexedTriangleArray geom, float flat_pts[],float flat_normals[], int i) { geom.setCoordRefFloat(flat_pts); geom.setCoordinateIndices(0, subsetOf3(cube_idx, i * 3)); geom.setColorRefFloat(cube_colors); geom.setColorIndices(0, subsetOf3(cube_idx, i * 3)); //geom.setNormalRefFloat(flat_normals); //geom.setNormals(0, flat_normals); } public static int[] subsetOf3(int[] cubeIndices, int startIndex) { int[] subset = new int[3]; subset[0] = cubeIndices[startIndex]; subset[1] = cubeIndices[startIndex + 1]; subset[2] = cubeIndices[startIndex + 2]; return subset; } public static float[] subsetOf3(float[] points, int startIndex) { float[] subset = new float[3]; subset[0] = points[startIndex]; subset[1] = points[startIndex + 1]; subset[2] = points[startIndex + 2]; return subset; } /* public float[] sortZAscending(IndexedTriangleArray[] fFTriangles, float[][] cube_pts) { float[] flat_pts = flatten(cube_pts); float[] zCenter = new float[fFTriangles.length]; for (int i = 0; i < fFTriangles.length; i++) { int[] indices = { 0, 0, 0 }; float[] p1 = { 0, 0, 0 }, p2 = { 0, 0, 0 }, p3 = { 0, 0, 0 }; fFTriangles[i].getCoordinateIndices(0, indices); for (int j = 0; j < 3; j++) { p1 = subsetOf3(flat_pts, indices[j] * 3); p2 = subsetOf3(flat_pts, indices[j] * 3); p3 = subsetOf3(flat_pts, indices[j] * 3); } zCenter[i] = calculateZCenter(p1,p2,p3); } return zCenter; } */ public float calculateZCenter(float[] p1, float[] p2, float[] p3) { float centerZ =0; centerZ = (p1[2]+p2[2]+p3[2])/3; return centerZ; } public IndexedTriangleArray[] frontFacingTriangles(IndexedTriangleArray[] triangles, boolean[] isFrontFacing) { IndexedTriangleArray[] fFT = new IndexedTriangleArray[FFTCount]; int counter=0; for(int i=0; i