Hello! Im having trouble with the lighting in my 3d gravity sim. I am trying to programm stars but the lighting function in processing doesnt give of direct light to the camera, so the stars appear like planets, but they make other planets glow. How can i make the stars glow aswell?
code:
ArrayList<Planet> planets = new ArrayList<Planet>();
PVector camPos = new PVector(0, 0, 0);
PVector camDir = new PVector(0, 0, 0);
float aYZ = 0;
float aXZ;
// Create Planets with 'c'
float PlanetSize;
float Scroll;
boolean PlanetBeingCreated = false;
float distCreatePlanet = 100;
float G = 1;
void setup() {
size(1200, 1000, P3D);
//fullScreen();
perspective(PI / 2, float(width) / float(height), 0.1, 1000000);
aXZ = height / 2;
planets.add(new Planet(new PVector(-100, 0, 0), 50, true));
planets.add(new Planet(new PVector(100, 100, 500), 50,true));
planets.add(new Planet(new PVector(200, 400, 300), 80));
}
void draw() {
background(0);
if (aXZ > height) {
aXZ = height;
} else if (aXZ < 0) {
aXZ = 0;
}
float angle = map (aYZ, 0, width, -PI, TWO_PI);
float camDirX = 700 * cos(angle) + camPos.x;
float camDirY = map(aXZ, 0, height, -1333, 1333) + camPos.y;
float camDirZ = 700 * sin(angle) + camPos.z;
camDir = new PVector(camDirX, camDirY, camDirZ);
camera(camPos.x, camPos.y, camPos.z, camDir.x, camDir.y, camDir.z, 0, 1, 0);
for (Planet p : planets) {
p.emmitLight();
}
ambientLight(100, 100, 100);
for (Planet p : planets) {
p.show();
}
for (Planet p : planets) {
p.update();
}
for (Planet b1 : planets) {
for (Planet b2 : planets) {
if (b1 != b2) {
float r = dist(b1.pos.x, b1.pos.y, b1.pos.z, b2.pos.x, b2.pos.y, b2.pos.z);
PVector p1 = new PVector(b2.pos.x, b2.pos.y, b2.pos.z);
p1 = p1.sub(b1.pos);
float a = G * ((b1.m * b2.m) / (r * r));
b1.applyForce(p1.mult(a).mult(1));
}
}
}
if (PlanetBeingCreated) {
PlanetSize += Scroll;
if (PlanetSize < 1) {
PlanetSize = 1;
}
float SpherePreviewSize = PlanetSize / 1.3;
PVector SpherePreviewPos = camDir.copy().sub(camPos).normalize().mult(distCreatePlanet).add(camPos.copy());
pushMatrix();
translate(SpherePreviewPos.x, SpherePreviewPos.y, SpherePreviewPos.z);
sphere(SpherePreviewSize);
popMatrix();
}
println(PlanetSize);
Scroll = 0;
//Movement
if (keyPressed) {
if (key == 'w') {
camPos = camPos.add(camDir.copy().sub(camPos).normalize());
camDir = camDir.add(camDir.copy().sub(camPos).normalize());
} else if (key == 's') {
camPos = camPos.add(camDir.copy().sub(camPos).normalize().mult(-1));
camDir = camDir.add(camDir.copy().sub(camPos).normalize().mult(-1));
} else if (key == 'W') {
camPos = camPos.add(camDir.copy().sub(camPos).normalize().mult(100));
camDir = camDir.add(camDir.copy().sub(camPos).normalize().mult(100));
} else if (key == 'S') {
camPos = camPos.add(camDir.copy().sub(camPos).normalize().mult(-100));
camDir = camDir.add(camDir.copy().sub(camPos).normalize().mult(-100));
}
}
}
void mouseDragged() {
if (mouseButton == LEFT) {
float dx = mouseX - pmouseX;
float dy = mouseY - pmouseY;
aXZ += dy / 1;
aYZ += dx / 1;
}
}
void keyPressed() {
if (key == 'c') {
PlanetSize = 30;
PlanetBeingCreated = true;
}
}
void keyReleased() {
if (key == 'c') {
PVector CreatePlanetPos = camDir.copy().sub(camPos).normalize().mult(distCreatePlanet).add(camPos.copy());
planets.add(new Planet(CreatePlanetPos, PlanetSize));
PlanetBeingCreated = false;
PlanetSize = 30;
}
}
void mouseWheel(MouseEvent event) {
Scroll = event.getCount();
}
////////////
class Body3D {
PVector pos;
PVector vel = new PVector(0, 0, 0);
PVector acc = new PVector(0, 0, 0);
float m;
void applyForce(PVector acc_) {
acc = acc.add(acc_);
}
void update() {
vel = vel.add(acc.div(m));
acc = new PVector(0, 0, 0);
pos = pos.add(vel);
//stroke(255, 255, 255, 30);
//strokeWeight(2);
//line(vel.x * 50 + pos.x, vel.y * 50 + pos.y, vel.z * 50 + pos.z, pos.x, pos.y, pos.z);
}
}
////////
class Planet extends Body3D {
boolean emmitsLight = false;
Planet(PVector pos_, float m_) {
this.pos = pos_;
this.m = m_;
}
Planet(PVector pos_, float m_, boolean emmitsLight_) {
this.pos = pos_;
this.m = m_;
emmitsLight = emmitsLight_;
}
void emmitLight() {
if (emmitsLight) {
pointLight(255, 200, 200, pos.x, pos.y, pos.z);
}
}
void show() {
pushMatrix();
noStroke();
fill(255);
translate(pos.x, pos.y, pos.z);
sphereDetail(60);
//if (!emmitsLight) {
sphere(this.m / 1.3);
//}
popMatrix();
}
}