Skip to content

Commit

Permalink
Have brute force render going in c++ and fixed JS FOV.
Browse files Browse the repository at this point in the history
  • Loading branch information
mthiffau committed Nov 26, 2016
1 parent f094d21 commit e9bdaa3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
90 changes: 75 additions & 15 deletions nativeatlas/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ using namespace std;

const uint32_t kMaxLeafSize = 8;

Vector3d basisProjection(const Vector3d& vec,
const uint32_t kImageWidth = 1280;
const uint32_t kImageHeight = 720;

static Vector3d basisProjection(const Vector3d& vec,
const Vector3d& basis_a,
const Vector3d& basis_b,
const Vector3d& basis_c) {
Expand All @@ -23,6 +26,32 @@ Vector3d basisProjection(const Vector3d& vec,
vec.dot(basis_c));
}

int render_count = 0;
int overlap_count = 0;
static void renderStar(Image& image,
const uint32_t sx,
const uint32_t sy,
const double brightness,
const Vector3d& color) {
// Calculate RGBA
//ColorRGB rgba(color.x()/255, color.y()/255, color.z()/255);
//rgba.alpha(brightness);
const double A = 10000;

ColorRGB pxColor = image.pixelColor(sx, sy);
pxColor.red(1.0);
pxColor.green(1.0);
pxColor.blue(1.0);
double alpha = pxColor.alpha();
if (alpha != 1.0)
overlap_count++;
//cout << "Overlapping pixel!" << endl;
pxColor.alpha(fmax(alpha - (A*brightness), 0.0));
image.pixelColor(sx, sy, pxColor);

render_count++;
}

int main(int argc, char* argv[]) {
//Initialize ImageMagick
InitializeMagick(*argv);
Expand All @@ -49,30 +78,61 @@ int main(int argc, char* argv[]) {
tree.addStar(&(stars[i]));
}

Vector3d cameraPosition(0, 0, 0);
Vector3d cameraDirection(0, 1, 0);
Vector3d upDirection(0, 0, 1);
Vector3d right = cameraDirection.cross(upDirection);

// Run a search for visible stars
/*
vector<const StarTree*> searchList{&tree};
vector<const Star*> foundStars;
visibleStars(cameraPosition, 0.01, searchList, foundStars);

// Create an image from the stars
Image image("1280x720", "black");
for (vector<const Star*>::iterator it = foundStars.begin();
it != foundStars.end(); ++it) {
const Vector3d& position = (*it)->position();
visibleStars(cameraPosition, 0.0001, searchList, foundStars);
cout << foundStars.size() << endl;
*/

Vector3d cameraPosition(0, -2500.0, 0);
Vector3d cameraDirection(0, 1, 0);
Vector3d upDirection(0, 0, 1);
Vector3d right = cameraDirection.cross(upDirection);

// Background image (black)
Image bgImage(Geometry(kImageWidth, kImageHeight), Color("black"));

// Create clear image for stars
Image starImage(Geometry(kImageWidth, kImageHeight), Color("black"));
starImage.matte(true);
starImage.transparent(Color("black"));
assert(((ColorRGB)(starImage.pixelColor(0,0))).red() == 0.0);
assert(((ColorRGB)(starImage.pixelColor(0,0))).green() == 0.0);
assert(((ColorRGB)(starImage.pixelColor(0,0))).blue() == 0.0);
assert(starImage.pixelColor(0,0).alpha() == 1.0);

// for (vector<const Star*>::iterator it = foundStars.begin();
// it != foundStars.end(); ++it) {
for (vector<Star>::iterator star = stars.begin();
star != stars.end(); ++star) {
const Vector3d& position = star->position();
const Vector3d translated = position - cameraPosition;
const Vector3d projected = basisProjection(translated,
right,
cameraDirection,
upDirection);

if (projected.y() < 0.00001) {
continue;
}
const uint32_t sx = (kImageWidth / 2.0) +
(kImageWidth / 2.0) * projected.x() / projected.y();
const uint32_t sy = (kImageHeight / 2.0) +
(kImageWidth / 2.0) * projected.z() / projected.y();
if (sx < 0 || sx >= kImageWidth) continue;
if (sy < 0 || sy >= kImageHeight) continue;
const double brightness = star->lum() /
translated.squaredNorm();
renderStar(starImage, sx, sy, brightness, star->color());
}

bgImage.composite(starImage, 0, 0, PlusCompositeOp);

cout << render_count << endl;
cout << overlap_count << endl;

cout << foundStars.size() << endl;
bgImage.write("image.png");

return 0;
}
8 changes: 5 additions & 3 deletions server/static/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ function doOneFrame() {
if (projected.y < 0.00001) {
continue;
}
var sx = canvas.width / 2 + 300 * projected.x / projected.y;
var sy = canvas.height / 2 + 300 * projected.z / projected.y;
var sx = (canvas.width / 2) +
(canvas.width / 2) * projected.x / projected.y;
var sy = (canvas.height / 2) +
(canvas.width / 2) * projected.z / projected.y;
if (sx < 0 || sx > canvas.width) continue;
if (sy < 0 || sy > canvas.height) continue;
var brightness = 255 * star.lum / projected.squaredLength();
var brightness = 255 * star.lum / translated.squaredLength();
var color = {"r":star.r, "g":star.g, "b":star.b};
renderStar(context, sx, sy, brightness, color);
}
Expand Down

0 comments on commit e9bdaa3

Please sign in to comment.