From 6b8103fbb62baf170f14f9f6cdac03756ecf0fee Mon Sep 17 00:00:00 2001 From: Emery Ferrari Date: Sat, 9 May 2020 21:46:14 +0900 Subject: [PATCH] jse3d v1.4 --- src/com/emeryferrari/jse3d/Display.java | 106 +++++++++++++++------ src/com/emeryferrari/jse3d/JSE3DConst.java | 2 +- src/com/emeryferrari/jse3d/ThreadMode.java | 4 + 3 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 src/com/emeryferrari/jse3d/ThreadMode.java diff --git a/src/com/emeryferrari/jse3d/Display.java b/src/com/emeryferrari/jse3d/Display.java index 58dc3ff..911e9fb 100644 --- a/src/com/emeryferrari/jse3d/Display.java +++ b/src/com/emeryferrari/jse3d/Display.java @@ -28,6 +28,7 @@ public class Display extends JComponent { static int physicsTimestep = 60; Point3D camPos; private CameraMode mode; + private ThreadMode threadMode; private ArrayList> distance; private double camPosX = 0; private double camPosY = 0; @@ -86,6 +87,7 @@ public Display(Scene scene, String frameTitle, boolean visible, boolean renderPo scrollWheel = true; mode = CameraMode.DRAG; camPos = new Point3D(0, 0, 0); + threadMode = ThreadMode.SINGLETHREADED; } public void startRender() { if (!rendererStarted) { @@ -135,35 +137,76 @@ public void paintComponent(Graphics graphics) { Point[] points = new Point[scene.object[a].points.length]; // WRITTEN BY SAM START for (int i = 0; i < scene.object[a].points.length; i++) { - double zAngle = Math.atan((scene.object[a].points[i].z)/(scene.object[a].points[i].x)); - if (scene.object[a].points[i].x == 0 && scene.object[a].points[i].z == 0) { - zAngle = 0; - } - double mag = Math.sqrt(Math.pow(scene.object[a].points[i].x, 2) + Math.pow(scene.object[a].points[i].z, 2)); - viewAngleY = -(mouse.y-frame.getHeight()/2)/sensitivity; - if (Math.abs(mouse.y-frame.getHeight()/2)>Math.PI/2*sensitivity) { - if (viewAngleY < 0) { - viewAngleY = -Math.PI/2*sensitivity; + if (threadMode == ThreadMode.SINGLETHREADED) { + double zAngle = Math.atan((scene.object[a].points[i].z)/(scene.object[a].points[i].x)); + if (scene.object[a].points[i].x == 0 && scene.object[a].points[i].z == 0) { + zAngle = 0; + } + double mag = Math.sqrt(Math.pow(scene.object[a].points[i].x, 2) + Math.pow(scene.object[a].points[i].z, 2)); + viewAngleY = -(mouse.y-frame.getHeight()/2)/sensitivity; + if (Math.abs(mouse.y-frame.getHeight()/2)>Math.PI/2*sensitivity) { + if (viewAngleY < 0) { + viewAngleY = -Math.PI/2*sensitivity; + } else { + viewAngleY = Math.PI/2*sensitivity; + } + } + viewAngleX = -(mouse.x-frame.getWidth()/2)/sensitivity; + if (scene.object[a].points[i].x < 0) { + xTransform = -mag*scale*Math.cos(viewAngleX+zAngle); + yTransform = -mag*scale*Math.sin(viewAngleX+zAngle)*Math.sin(viewAngleY)+(scene.object[a].points[i].y)*scale*Math.cos(viewAngleY); } else { - viewAngleY = Math.PI/2*sensitivity; + xTransform = mag*scale*Math.cos(viewAngleX+zAngle); + yTransform = mag*scale*Math.sin(viewAngleX+zAngle)*Math.sin(viewAngleY)+(scene.object[a].points[i].y)*scale*Math.cos(viewAngleY); + } + camPosX = scene.camDist*Math.sin(viewAngleX)*Math.cos(viewAngleY); + camPosY = -scene.camDist*Math.sin(viewAngleY); + camPosZ = scene.camDist*Math.cos(viewAngleX)*Math.cos(viewAngleY); + if (!(scene.object[a].points[i].z*Math.cos(viewAngleX)*Math.cos(viewAngleY) + scene.object[a].points[i].x*Math.sin(viewAngleX)*Math.cos(viewAngleY) - scene.object[a].points[i].y*Math.sin(viewAngleY) > scene.camDist)) { + distance.get(a).set(i, new Distance(Math.sqrt(Math.pow(camPosX-(scene.object[a].points[i].x), 2)+Math.pow(camPosY-scene.object[a].points[i].y, 2)+Math.pow(camPosZ-scene.object[a].points[i].z, 2)), i)); + double theta = Math.asin((Math.sqrt(Math.pow(xTransform, 2)+Math.pow(yTransform, 2))/scale)/distance.get(a).get(i).distance); + camScale.get(a).set(i, distance.get(a).get(i).distance*Math.cos(theta)*Math.sin(scene.viewAngle/2)); + points[i] = new Point((int)(frame.getWidth()/2+xTransform/camScale.get(a).get(i)), (int)(frame.getHeight()/2-yTransform/camScale.get(a).get(i))); } - } - viewAngleX = -(mouse.x-frame.getWidth()/2)/sensitivity; - if (scene.object[a].points[i].x < 0) { - xTransform = -mag*scale*Math.cos(viewAngleX+zAngle); - yTransform = -mag*scale*Math.sin(viewAngleX+zAngle)*Math.sin(viewAngleY)+(scene.object[a].points[i].y)*scale*Math.cos(viewAngleY); } else { - xTransform = mag*scale*Math.cos(viewAngleX+zAngle); - yTransform = mag*scale*Math.sin(viewAngleX+zAngle)*Math.sin(viewAngleY)+(scene.object[a].points[i].y)*scale*Math.cos(viewAngleY); - } - camPosX = scene.camDist*Math.sin(viewAngleX)*Math.cos(viewAngleY); - camPosY = -scene.camDist*Math.sin(viewAngleY); - camPosZ = scene.camDist*Math.cos(viewAngleX)*Math.cos(viewAngleY); - if (!(scene.object[a].points[i].z*Math.cos(viewAngleX)*Math.cos(viewAngleY) + scene.object[a].points[i].x*Math.sin(viewAngleX)*Math.cos(viewAngleY) - scene.object[a].points[i].y*Math.sin(viewAngleY) > scene.camDist)) { - distance.get(a).set(i, new Distance(Math.sqrt(Math.pow(camPosX-(scene.object[a].points[i].x), 2)+Math.pow(camPosY-scene.object[a].points[i].y, 2)+Math.pow(camPosZ-scene.object[a].points[i].z, 2)), i)); - double theta = Math.asin((Math.sqrt(Math.pow(xTransform, 2)+Math.pow(yTransform, 2))/scale)/distance.get(a).get(i).distance); - camScale.get(a).set(i, distance.get(a).get(i).distance*Math.cos(theta)*Math.sin(scene.viewAngle/2)); - points[i] = new Point((int)(frame.getWidth()/2+xTransform/camScale.get(a).get(i)), (int)(frame.getHeight()/2-yTransform/camScale.get(a).get(i))); + final int a1 = a; + final int i1 = i; + Thread thread = new Thread() { + @Override + public void run() { + double zAngle = Math.atan((scene.object[a1].points[i1].z)/(scene.object[a1].points[i1].x)); + if (scene.object[a1].points[i1].x == 0 && scene.object[a1].points[i1].z == 0) { + zAngle = 0; + } + double mag = Math.sqrt(Math.pow(scene.object[a1].points[i1].x, 2) + Math.pow(scene.object[a1].points[i1].z, 2)); + viewAngleY = -(mouse.y-frame.getHeight()/2)/sensitivity; + if (Math.abs(mouse.y-frame.getHeight()/2)>Math.PI/2*sensitivity) { + if (viewAngleY < 0) { + viewAngleY = -Math.PI/2*sensitivity; + } else { + viewAngleY = Math.PI/2*sensitivity; + } + } + viewAngleX = -(mouse.x-frame.getWidth()/2)/sensitivity; + if (scene.object[a1].points[i1].x < 0) { + xTransform = -mag*scale*Math.cos(viewAngleX+zAngle); + yTransform = -mag*scale*Math.sin(viewAngleX+zAngle)*Math.sin(viewAngleY)+(scene.object[a1].points[i1].y)*scale*Math.cos(viewAngleY); + } else { + xTransform = mag*scale*Math.cos(viewAngleX+zAngle); + yTransform = mag*scale*Math.sin(viewAngleX+zAngle)*Math.sin(viewAngleY)+(scene.object[a1].points[i1].y)*scale*Math.cos(viewAngleY); + } + camPosX = scene.camDist*Math.sin(viewAngleX)*Math.cos(viewAngleY); + camPosY = -scene.camDist*Math.sin(viewAngleY); + camPosZ = scene.camDist*Math.cos(viewAngleX)*Math.cos(viewAngleY); + if (!(scene.object[a1].points[i1].z*Math.cos(viewAngleX)*Math.cos(viewAngleY) + scene.object[a1].points[i1].x*Math.sin(viewAngleX)*Math.cos(viewAngleY) - scene.object[a1].points[i1].y*Math.sin(viewAngleY) > scene.camDist)) { + distance.get(a1).set(i1, new Distance(Math.sqrt(Math.pow(camPosX-(scene.object[a1].points[i1].x), 2)+Math.pow(camPosY-scene.object[a1].points[i1].y, 2)+Math.pow(camPosZ-scene.object[a1].points[i1].z, 2)), i1)); + double theta = Math.asin((Math.sqrt(Math.pow(xTransform, 2)+Math.pow(yTransform, 2))/scale)/distance.get(a1).get(i1).distance); + camScale.get(a1).set(i1, distance.get(a1).get(i1).distance*Math.cos(theta)*Math.sin(scene.viewAngle/2)); + points[i1] = new Point((int)(frame.getWidth()/2+xTransform/camScale.get(a1).get(i1)), (int)(frame.getHeight()/2-yTransform/camScale.get(a1).get(i1))); + } + } + }; + thread.start(); } // WRITTEN BY SAM END if (renderPoints) { @@ -430,6 +473,15 @@ public void setCameraDistance(double distance) { scene.camDist = distance; } public void setCameraMode(CameraMode mode) { - + this.mode = mode; + } + public void setThreadMode(ThreadMode mode) { + threadMode = mode; + } + public CameraMode getCameraMode() { + return mode; + } + public ThreadMode getThreadMode() { + return threadMode; } } \ No newline at end of file diff --git a/src/com/emeryferrari/jse3d/JSE3DConst.java b/src/com/emeryferrari/jse3d/JSE3DConst.java index 67a9f26..f6d1610 100644 --- a/src/com/emeryferrari/jse3d/JSE3DConst.java +++ b/src/com/emeryferrari/jse3d/JSE3DConst.java @@ -2,6 +2,6 @@ public class JSE3DConst { private JSE3DConst() {} public static final String NAME = "jse3d"; - public static final String VERSION = "v1.3.7"; + public static final String VERSION = "v1.4"; public static final String FULL_NAME = JSE3DConst.NAME + " " + JSE3DConst.VERSION; } \ No newline at end of file diff --git a/src/com/emeryferrari/jse3d/ThreadMode.java b/src/com/emeryferrari/jse3d/ThreadMode.java new file mode 100644 index 0000000..2959c45 --- /dev/null +++ b/src/com/emeryferrari/jse3d/ThreadMode.java @@ -0,0 +1,4 @@ +package com.emeryferrari.jse3d; +public enum ThreadMode { + SINGLETHREADED, MULTITHREADED; +} \ No newline at end of file