Commit b06f0b77 authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman 💥

feat: oOPN is more interesting now.

parent 67af92e6
......@@ -25,24 +25,26 @@
*/
package com.aparapi.examples.oopnbody;
import java.util.List;
public final class Body{
protected final float delT = .005f;
protected final float espSqr = 1.0f;
public static Body[] allBodies;
public Body(float _x, float _y, float _z, float _m) {
x = _x;
y = _y;
z = _z;
final private boolean isHeavy;
public Body(float x, float y, float z, float _m, boolean isHeavy) {
this.x = x;
this.y = y;
this.z = z;
m = _m;
this.isHeavy = isHeavy;
}
float x, y, z, m, vx, vy, vz;
public boolean isHeavy() {
return isHeavy;
}
public float getX() {
return x;
}
......@@ -55,6 +57,18 @@ public final class Body{
return z;
}
public float getRadius() {
return (float) Math.sqrt(x * x + y * y + z* z);
}
public float getTheta() {
return (float) Math.acos(z / getRadius());
}
public float getPhi() {
return (float) Math.atan(y / x);
}
public float getVx() {
return vx;
}
......@@ -87,6 +101,30 @@ public final class Body{
z = _z;
}
public void setRadius(float radius) {
final float theta = getTheta();
final float phi = getPhi();
x = (float) (radius * Math.cos(theta) * Math.sin(phi));
y = (float) (radius * Math.sin(theta) * Math.sin(phi));
z = (float) (radius * Math.cos(phi));
}
public void setTheta(float theta) {
final float radius = getRadius();
final float phi = getPhi();
x = (float) (radius * Math.cos(theta) * Math.sin(phi));
y = (float) (radius * Math.sin(theta) * Math.sin(phi));
z = (float) (radius * Math.cos(phi));
}
public void setPhi(float phi) {
final float radius = getRadius();
final float theta = getTheta();
x = (float) (radius * Math.cos(theta) * Math.sin(phi));
y = (float) (radius * Math.sin(theta) * Math.sin(phi));
z = (float) (radius * Math.cos(phi));
}
public void setVx(float _vx) {
vx = _vx;
}
......
......@@ -62,16 +62,12 @@ under those regulations, please refer to the U.S. Bureau of Industry and Securit
*/
package com.aparapi.examples.oopnbody;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
......@@ -91,7 +87,6 @@ import javax.swing.JTextField;
import javax.swing.WindowConstants;
import com.aparapi.Kernel;
import com.aparapi.ProfileInfo;
import com.aparapi.Range;
import com.jogamp.opengl.util.FPSAnimator;
......@@ -115,11 +110,9 @@ public class Main{
public static class NBodyKernel extends Kernel{
protected final float delT = .005f;
protected final float delT = 0.5f;
protected final float espSqr = 1.0f;
protected final float mass = 5f;
protected final float espSqr = 10000.0f;
private final Range range;
......@@ -138,7 +131,13 @@ public class Main{
for (int body = 0; body < range.getGlobalSize(0); body++) {
final float theta = (float) (Math.random() * Math.PI * 2);
final float phi = (float) (Math.random() * Math.PI * 2);
final float radius = (float) (Math.random() * maxDist);
final float radius;
final float seed = (float) (Math.random() * 0.01f) + 0.99f;
if ((body % 2) == 0) {
radius = ((float) (seed * maxDist)) / 1f;
} else {
radius = ((float) (seed * maxDist)) / 0.5f;
}
// get the 3D dimensional coordinates
float x = (float) (radius * Math.cos(theta) * Math.sin(phi));
......@@ -147,11 +146,11 @@ public class Main{
// divide into two 'spheres of bodies' by adjusting x
if ((body % 2) == 0) {
x += maxDist * 1.5;
x += maxDist * 10.0f;
bodies[body] = new Body(x, y, z, 0.1f, false);
} else {
x -= maxDist * 1.5;
bodies[body] = new Body(x, y, z, 1f, true);
}
bodies[body] = new Body(x, y, z, 5f);
}
Body.allBodies = bodies;
......@@ -206,15 +205,11 @@ public class Main{
int sz = range.getGlobalSize(0);
for (int i = 0; i < sz; i++) {
if (i < (sz / 2)) {
Body currBody = bodies[i];
if(currBody.isHeavy())
gl.glColor3f(1f, 0f, 0f);
} else if (i < (sz * 0.666)) {
gl.glColor3f(0f, 1f, 0f);
} else {
else
gl.glColor3f(0f, 0f, 1f);
}
Body currBody = bodies[i];
gl.glTexCoord2f(0, 1);
gl.glVertex3f(currBody.getX(), currBody.getY() + 1, currBody.getZ());
......@@ -286,23 +281,33 @@ public class Main{
final Dimension dimension = new Dimension(Integer.getInteger("width", 742 - 64), Integer.getInteger("height", 742 - 64));
canvas.setPreferredSize(dimension);
final Perspective perspective = new Perspective(0f, 0f, -800f, 0f, 0f, 0f, 1f);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
switch(e.getKeyCode()) {
case 37:
perspective.setPhi(perspective.getPhi() - 0.1f);
break;
case 38:
perspective.setTheta(perspective.getTheta() + 0.1f);
break;
case 39:
perspective.setPhi(perspective.getPhi() + 0.1f);
break;
case 40:
perspective.setTheta(perspective.getTheta() - 0.1f);
break;
}
return false;
}
});
canvas.addGLEventListener(new GLEventListener(){
private double ratio;
private final float xeye = 0f;
private final float yeye = 0f;
private final float zeye = 100f;
private final float xat = 0f;
private final float yat = 0f;
private final float zat = 0f;
public final float zoomFactor = 1.0f;
private int frames;
private long last = System.currentTimeMillis();
......@@ -323,7 +328,7 @@ public class Main{
final GLU glu = new GLU();
glu.gluPerspective(45f, ratio, 1f, 1000f);
glu.gluLookAt(xeye, yeye, zeye * zoomFactor, xat, yat, zat, 0f, 1f, 0f);
glu.gluLookAt(perspective.getXeye(), perspective.getYeye(), perspective.getZeye(), perspective.getXat(), perspective.getYat(), perspective.getZat(), 0f, 1f, 0f);
if (running) {
//Arrays.parallel(bodies.toArray(new Body[1])).forEach(b -> {b.nextMove();});
kernel.execute(kernel.range);
......
/**
* Copyright (c) 2016 - 2017 Syncleus, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aparapi.examples.oopnbody;
class Perspective {
private float xeye;
private float yeye;
private float zeye;
private float xat;
private float yat;
private float zat;
public float zoomFactor;
public Perspective() {
}
public Perspective(float xeye, float yeye, float zeye, float xat, float yat, float zat, float zoomFactor) {
this.xeye = xeye;
this.yeye = yeye;
this.zeye = zeye;
this.xat = xat;
this.yat = yat;
this.zat = zat;
this.zoomFactor = zoomFactor;
}
public float getXeye() {
return xeye;
}
public void setXeye(float xeye) {
this.xeye = xeye;
}
public float getYeye() {
return yeye;
}
public void setYeye(float yeye) {
this.yeye = yeye;
}
public float getZeye() {
return zeye;
}
public void setZeye(float zeye) {
this.zeye = zeye;
}
public float getXat() {
return xat;
}
public void setXat(float xat) {
this.xat = xat;
}
public float getYat() {
return yat;
}
public void setYat(float yat) {
this.yat = yat;
}
public float getZat() {
return zat;
}
public void setZat(float zat) {
this.zat = zat;
}
public float getZoomFactor() {
return zoomFactor;
}
public void setZoomFactor(float zoomFactor) {
this.zoomFactor = zoomFactor;
}
public float getRadius() {
return (float) Math.sqrt(xeye * xeye + yeye * yeye + zeye * zeye);
}
public float getTheta() {
if( getRadius() == 0f)
return 0f;
return (float) Math.acos(zeye / getRadius());
}
public float getPhi() {
if(xeye == 0f)
return 0f;
return (float) Math.atan(yeye / xeye);
}
public void setRadius(float radius) {
final float theta = getTheta();
final float phi = getPhi();
xeye = radius * ((float)Math.cos(phi)) * ((float)Math.sin(theta));
yeye = radius * ((float)Math.sin(theta)) * ((float)Math.sin(phi));
zeye = radius * ((float)Math.cos(theta));
}
public void setTheta(float theta) {
final float radius = getRadius();
final float phi = getPhi();
xeye = radius * ((float)Math.cos(phi)) * ((float)Math.sin(theta));
yeye = radius * ((float)Math.sin(theta)) * ((float)Math.sin(phi));
zeye = radius * ((float)Math.cos(theta));
}
public void setPhi(float phi) {
final float radius = getRadius();
final float theta = getTheta();
xeye = radius * ((float)Math.cos(phi)) * ((float)Math.sin(theta));
yeye = radius * ((float)Math.sin(theta)) * ((float)Math.sin(phi));
zeye = radius * ((float)Math.cos(theta));
}
@Override
public String toString() {
return "Perspective{" +
" xeye=" + xeye +
", yeye=" + yeye +
", zeye=" + zeye +
" radius=" + getRadius() +
", theta=" + getTheta() +
", phi=" + getPhi() +
", xat=" + xat +
", yat=" + yat +
", zat=" + zat +
", zoomFactor=" + zoomFactor +
'}';
}
}
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment